/** * Basic setup. */ public function preProcess() { $config = CRM_Core_Config::singleton(); $this->_uploadMaxSize = (int) ini_get('upload_max_filesize'); // check for post max size CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE); }
/** * Common form elements. */ public function buildQuickForm() { $config = CRM_Core_Config::singleton(); $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE); $uploadSize = round($uploadFileSize / (1024 * 1024), 2); $this->assign('uploadSize', $uploadSize); $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE); $this->setMaxFileSize($uploadFileSize); $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(1 => $uploadSize, 2 => $uploadFileSize)), 'maxfilesize', $uploadFileSize); $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile'); $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File'); $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers')); $this->add('text', 'fieldSeparator', ts('Import Field Separator'), array('size' => 2), TRUE); $this->setDefaults(array('fieldSeparator' => $config->fieldSeparator)); //get the saved mapping details $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type', 'Import ' . static::IMPORT_ENTITY, 'name')); $this->assign('savedMapping', $mappingArray); $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray); if ($loadedMapping = $this->get('loadedMapping')) { $this->assign('loadedMapping', $loadedMapping); $this->setDefaults(array('savedMapping' => $loadedMapping)); } //build date formats CRM_Core_Form_Date::buildAllowedDateFormats($this); $this->addButtons(array(array('type' => 'upload', 'name' => ts('Continue'), 'spacing' => ' ', 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Cancel')))); }
/** * Basic setup. */ public function preProcess() { $this->_uploadMaxSize = (int) ini_get('upload_max_filesize'); // check for post max size CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE); // This is a temp hack for the fact we really don't need to hard-code each setting in the tpl but // we haven't worked through NOT doing that. These settings have been un-hardcoded. $this->assign('pure_config_settings', array('empoweredBy', 'max_attachments', 'maxFileSize', 'secondDegRelPermissions', 'recentItemsMaxCount', 'recentItemsProviders', 'dedupe_default_limit')); }
/** * Set variables up before form is built. */ public function preProcess() { //Test database user privilege to create table(Temporary) CRM-4725 $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); $daoTestPrivilege = new CRM_Core_DAO(); $daoTestPrivilege->query("CREATE TEMPORARY TABLE import_job_permission_one(test int) ENGINE=InnoDB"); $daoTestPrivilege->query("CREATE TEMPORARY TABLE import_job_permission_two(test int) ENGINE=InnoDB"); $daoTestPrivilege->query("DROP TEMPORARY TABLE IF EXISTS import_job_permission_one, import_job_permission_two"); unset($errorScope); if ($daoTestPrivilege->_lastError) { CRM_Core_Error::fatal(ts('Database Configuration Error: Insufficient permissions. Import requires that the CiviCRM database user has permission to create temporary tables. Contact your site administrator for assistance.')); } $results = array(); $config = CRM_Core_Config::singleton(); $handler = opendir($config->uploadDir); $errorFiles = array('sqlImport.errors', 'sqlImport.conflicts', 'sqlImport.duplicates', 'sqlImport.mismatch'); // check for post max size avoid when called twice $snippet = CRM_Utils_Array::value('snippet', $_GET, 0); if (empty($snippet)) { CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE); } while ($file = readdir($handler)) { if ($file != '.' && $file != '..' && in_array($file, $errorFiles) && !is_writable($config->uploadDir . $file)) { $results[] = $file; } } closedir($handler); if (!empty($results)) { CRM_Core_Error::fatal(ts('<b>%1</b> file(s) in %2 directory are not writable. Listed file(s) might be used during the import to log the errors occurred during Import process. Contact your site administrator for assistance.', array(1 => implode(', ', $results), 2 => $config->uploadDir))); } $this->_dataSourceIsValid = FALSE; $this->_dataSource = CRM_Utils_Request::retrieve('dataSource', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'GET'); $this->_params = $this->controller->exportValues($this->_name); if (!$this->_dataSource) { //considering dataSource as base criteria instead of hidden_dataSource. $this->_dataSource = CRM_Utils_Array::value('dataSource', $_POST, CRM_Utils_Array::value('dataSource', $this->_params)); $this->assign('showOnlyDataSourceFormPane', FALSE); } else { $this->assign('showOnlyDataSourceFormPane', TRUE); } $dataSources = $this->_getDataSources(); if ($this->_dataSource && isset($dataSources[$this->_dataSource])) { $this->_dataSourceIsValid = TRUE; $this->assign('showDataSourceFormPane', TRUE); $dataSourcePath = explode('_', $this->_dataSource); $templateFile = "CRM/Contact/Import/Form/" . $dataSourcePath[3] . ".tpl"; $this->assign('dataSourceFormTemplateFile', $templateFile); } elseif ($this->_dataSource) { throw new \CRM_Core_Exception("Invalid data source"); } }
/** * This is function is called by the form object to get the DataSource's * form snippet. It should add all fields necesarry to get the data * uploaded to the temporary table in the DB. * * @param CRM_Core_Form $form * * @return void * (operates directly on form argument) */ public function buildQuickForm(&$form) { $form->add('hidden', 'hidden_dataSource', 'CRM_Import_DataSource_CSV'); $config = CRM_Core_Config::singleton(); $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE); $uploadSize = round($uploadFileSize / (1024 * 1024), 2); $form->assign('uploadSize', $uploadSize); $form->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE); $form->setMaxFileSize($uploadFileSize); $form->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(1 => $uploadSize, 2 => $uploadFileSize)), 'maxfilesize', $uploadFileSize); $form->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File'); $form->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile'); $form->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers')); }
/** * Generate and assign an arbitrary value to a field of a test object. * * @param string $fieldName * @param array $fieldDef * @param int $counter * The globally-unique ID of the test object. */ protected function assignTestValue($fieldName, &$fieldDef, $counter) { $dbName = $fieldDef['name']; $daoName = get_class($this); $handled = FALSE; if (!$handled && $dbName == 'contact_sub_type') { //coming up with a rule to set this is too complex let's not set it $handled = TRUE; } // Pick an option value if needed if (!$handled && $fieldDef['type'] !== CRM_Utils_Type::T_BOOLEAN) { $options = $daoName::buildOptions($dbName, 'create'); if ($options) { $this->{$dbName} = key($options); $handled = TRUE; } } if (!$handled) { switch ($fieldDef['type']) { case CRM_Utils_Type::T_INT: case CRM_Utils_Type::T_FLOAT: case CRM_Utils_Type::T_MONEY: if (isset($fieldDef['precision'])) { // $object->$dbName = CRM_Utils_Number::createRandomDecimal($value['precision']); $this->{$dbName} = CRM_Utils_Number::createTruncatedDecimal($counter, $fieldDef['precision']); } else { $this->{$dbName} = $counter; } break; case CRM_Utils_Type::T_BOOLEAN: if (isset($fieldDef['default'])) { $this->{$dbName} = $fieldDef['default']; } elseif ($fieldDef['name'] == 'is_deleted' || $fieldDef['name'] == 'is_test') { $this->{$dbName} = 0; } else { $this->{$dbName} = 1; } break; case CRM_Utils_Type::T_DATE: case CRM_Utils_Type::T_TIMESTAMP: case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME: $this->{$dbName} = '19700101'; if ($dbName == 'end_date') { // put this in the future $this->{$dbName} = '20200101'; } break; case CRM_Utils_Type::T_TIME: CRM_Core_Error::fatal("T_TIME shouldn't be used."); //$object->$dbName='000000'; //break; //$object->$dbName='000000'; //break; case CRM_Utils_Type::T_CCNUM: $this->{$dbName} = '4111 1111 1111 1111'; break; case CRM_Utils_Type::T_URL: $this->{$dbName} = 'http://www.civicrm.org'; break; case CRM_Utils_Type::T_STRING: case CRM_Utils_Type::T_BLOB: case CRM_Utils_Type::T_MEDIUMBLOB: case CRM_Utils_Type::T_TEXT: case CRM_Utils_Type::T_LONGTEXT: case CRM_Utils_Type::T_EMAIL: default: // WAS: if (isset($value['enumValues'])) { // TODO: see if this works with all pseudoconstants if (isset($fieldDef['pseudoconstant'], $fieldDef['pseudoconstant']['callback'])) { if (isset($fieldDef['default'])) { $this->{$dbName} = $fieldDef['default']; } else { $options = CRM_Core_PseudoConstant::get($daoName, $fieldName); if (is_array($options)) { $this->{$dbName} = $options[0]; } else { $defaultValues = explode(',', $options); $this->{$dbName} = $defaultValues[0]; } } } else { $this->{$dbName} = $dbName . '_' . $counter; $maxlength = CRM_Utils_Array::value('maxlength', $fieldDef); if ($maxlength > 0 && strlen($this->{$dbName}) > $maxlength) { $this->{$dbName} = substr($this->{$dbName}, 0, $fieldDef['maxlength']); } } } } }
/** * @param $value * @param $precision * @param $expectedValue * @dataProvider truncDecimalCases */ public function testCreateTruncatedDecimal($value, $precision, $expectedValue) { list($sigFigs, $decFigs) = $precision; $this->assertEquals($expectedValue, CRM_Utils_Number::createTruncatedDecimal($value, $precision), "assert createTruncatedValue({$value}, ({$sigFigs},{$decFigs})) == {$expectedValue}"); }
/** * Function to actually build the form - this appears to be entirely code that should be in a shared base class in core * * @return None * @access public */ public function buildQuickForm() { // Setting Upload File Size. $config = CRM_Core_Config::singleton(); // This conditional block is important as the "maxImportFileSize" has been changed to "maxFileSize" // in the newer versions. In order to remove version support, remove this block and // replace by $uploadFileSize = $config->maxFileSize. if (!empty($config->maxImportFileSize)) { $uploadFileSize = $config->maxImportFileSize; } else { $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE); } if ($uploadFileSize >= 8388608) { $uploadFileSize = 8388608; } $uploadSize = round($uploadFileSize / (1024 * 1024), 2); $this->assign('uploadSize', $uploadSize); $this->add('file', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE); $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile'); $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(1 => $uploadSize, 2 => $uploadFileSize)), 'maxfilesize', $uploadFileSize); $this->setMaxFileSize($uploadFileSize); $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File'); $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers')); if ($this->isDuplicateOptions) { $duplicateOptions = array(); $duplicateOptions[] = $this->createElement('radio', NULL, NULL, ts('Skip'), CRM_Import_Parser::DUPLICATE_SKIP); $duplicateOptions[] = $this->createElement('radio', NULL, NULL, ts('Update'), CRM_Import_Parser::DUPLICATE_UPDATE); $duplicateOptions[] = $this->createElement('radio', NULL, NULL, ts('No Duplicate Checking'), CRM_Import_Parser::DUPLICATE_NOCHECK); $this->addGroup($duplicateOptions, 'onDuplicate', ts('On Duplicate Entries')); } //get the saved mapping details $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type', $this->_mappingType, 'name')); $this->assign('savedMapping', $mappingArray); $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray); if ($loadedMapping = $this->get('loadedMapping')) { $this->assign('loadedMapping', $loadedMapping); $this->setDefaults(array('savedMapping' => $loadedMapping)); } $this->setDefaults(array('onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP)); if ($this->_enableContactOptions) { $this->addContactOptions(); } $this->setDefaults(array('contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL)); $this->addElement('text', 'fieldSeparator', ts('Import Field Separator'), array('size' => 2)); //build date formats CRM_Core_Form_Date::buildAllowedDateFormats($this); $this->addButtons(array(array('type' => 'upload', 'name' => ts('Continue >>'), 'spacing' => ' ', 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Cancel')))); }