public function testResolveMappingDataForView() { $testTableName = 'testimporttable'; $this->assertTrue(ImportTestHelper::createTempTableByFileNameAndTableName('importTest.csv', $testTableName)); $mappingData = ImportMappingUtil::makeMappingDataByTableName($testTableName); $compareData = array('column_0' => array('type' => 'importColumn', 'attributeIndexOrDerivedType' => null, 'mappingRulesData' => null), 'column_1' => array('type' => 'importColumn', 'attributeIndexOrDerivedType' => null, 'mappingRulesData' => null), 'column_2' => array('type' => 'importColumn', 'attributeIndexOrDerivedType' => null, 'mappingRulesData' => null)); $this->assertEquals($compareData, $mappingData); $mappingData['column_3'] = array('type' => 'extraColumn', 'attributeIndexOrDerivedType' => 'xyz', 'mappingRulesData' => null); $headerRow = ImportDatabaseUtil::getFirstRowByTableName($testTableName); $sampleBean = ImportDatabaseUtil::getSubset($testTableName, null, 1, 1); $mappingDataMetadata = ImportWizardMappingViewUtil::resolveMappingDataForView($mappingData, current($sampleBean), $headerRow); $compareData = array('column_0' => array('type' => 'importColumn', 'attributeIndexOrDerivedType' => null, 'mappingRulesData' => null, 'headerValue' => 'name', 'sampleValue' => 'abc'), 'column_1' => array('type' => 'importColumn', 'attributeIndexOrDerivedType' => null, 'mappingRulesData' => null, 'headerValue' => 'phone', 'sampleValue' => '123'), 'column_2' => array('type' => 'importColumn', 'attributeIndexOrDerivedType' => null, 'mappingRulesData' => null, 'headerValue' => 'industry', 'sampleValue' => 'a'), 'column_3' => array('type' => 'extraColumn', 'attributeIndexOrDerivedType' => 'xyz', 'mappingRulesData' => null, 'headerValue' => null, 'sampleValue' => null)); $this->assertEquals($compareData, $mappingDataMetadata); $sampleBean = ImportDatabaseUtil::getSubset($testTableName, null, 1); $mappingDataMetadata = ImportWizardMappingViewUtil::resolveMappingDataForView($mappingData, current($sampleBean), null); $compareData = array('column_0' => array('type' => 'importColumn', 'attributeIndexOrDerivedType' => null, 'mappingRulesData' => null, 'sampleValue' => 'name'), 'column_1' => array('type' => 'importColumn', 'attributeIndexOrDerivedType' => null, 'mappingRulesData' => null, 'sampleValue' => 'phone'), 'column_2' => array('type' => 'importColumn', 'attributeIndexOrDerivedType' => null, 'mappingRulesData' => null, 'sampleValue' => 'industry'), 'column_3' => array('type' => 'extraColumn', 'attributeIndexOrDerivedType' => 'xyz', 'mappingRulesData' => null, 'sampleValue' => null)); $this->assertEquals($compareData, $mappingDataMetadata); }
/** * Given an import data's table name, create a basic mapping data array that has the correct starting * elements set as null. This will ensure the mapping data array is always structured correctly. Each key * will be a column name from the table. Throws an exception if the table is missing rows. * @param string $tableName * @return array $mappingData */ public static function makeMappingDataByTableName($tableName) { assert('is_string($tableName)'); $firstRowData = ImportDatabaseUtil::getFirstRowByTableName($tableName); if (count($firstRowData) == 1 || count($firstRowData) == 0) { throw new NoRowsInTableException(); } //Handle scenario where every column is null. Similiar to scenario below it with no data in file but //making a row anyways. $allValuesAreNull = true; foreach ($firstRowData as $columnName => $value) { if ($value != null && $columnName != 'id') { $allValuesAreNull = false; break; } } if ($allValuesAreNull) { throw new NoRowsInTableException(); } //handles scenario where there is no data in the file, but because there are a few bytes, //it creates a single row. if (count($firstRowData) == 2) { foreach ($firstRowData as $columnName => $value) { if (!in_array($columnName, ImportDatabaseUtil::getReservedColumnNames()) && $value == null) { if (ImportDatabaseUtil::getCount($tableName) == 1) { throw new NoRowsInTableException(); } } } } $mappingData = array(); foreach ($firstRowData as $columnName => $notUsed) { if (!in_array($columnName, ImportDatabaseUtil::getReservedColumnNames())) { $mappingData[$columnName] = array('type' => 'importColumn', 'attributeIndexOrDerivedType' => null, 'mappingRulesData' => null); } } return $mappingData; }
/** * Get the meta data and merge with standard CGridView column elements * to create a column array that fits the CGridView columns API * @return array */ protected function getCGridViewColumns() { $columns = array(); if ($this->rowsAreExpandable()) { $firstColumn = array('class' => 'ImportDrillDownColumn', 'id' => $this->gridId . $this->gridIdSuffix . '-rowDrillDown', 'expandableContentType' => static::getExpandableContentType(), 'htmlOptions' => array('class' => 'hasDrillDownLink')); array_push($columns, $firstColumn); } array_push($columns, $this->resolveSecondColumn()); $headerRow = ImportDatabaseUtil::getFirstRowByTableName($this->dataProvider->getTableName()); foreach ($headerRow as $columnName => $label) { if (!in_array($columnName, ImportDatabaseUtil::getReservedColumnNames()) && $this->mappingData[$columnName]['type'] == 'importColumn' && $this->mappingData[$columnName]['attributeIndexOrDerivedType'] != null) { if (!$this->dataProvider->hasHeaderRow()) { $label = static::resolveColumnCountByName($columnName); } $params = array(); $columnAdapter = new BeanStringListViewColumnAdapter($columnName, $this, $params); $column = $columnAdapter->renderGridViewData(); $column['header'] = static::resolveHeaderColumnContent($columnName, $label); $this->columnLabelsByName[$columnName] = $column['header']; if (!isset($column['class'])) { $column['class'] = 'DataColumn'; } array_push($columns, $column); } } return $columns; }
/** * @depends testGetColumnCountByTableName */ public function testGetFirstRowByTableName() { $firstRowData = ImportDatabaseUtil::getFirstRowByTableName('testimporttable'); $compareData = array('id' => 1, 'column_0' => 'name', 'column_1' => 'phone', 'column_2' => 'industry', 'status' => null, 'serializedmessages' => null); $this->assertEquals($compareData, $firstRowData); }
/** * Step 4. Import mapping */ public function actionStep4($id) { $import = Import::getById((int) $id); $importWizardForm = ImportWizardUtil::makeFormByImport($import); $importWizardForm->setScenario('saveMappingData'); $tempTableName = $import->getTempTableName(); $importRulesClassName = ImportRulesUtil::getImportRulesClassNameByType($importWizardForm->importRulesType); if (isset($_POST[get_class($importWizardForm)])) { $reIndexedPostData = ImportMappingUtil::reIndexExtraColumnNamesByPostData($_POST[get_class($importWizardForm)]); $sanitizedPostData = ImportWizardFormPostUtil::sanitizePostByTypeForSavingMappingData($importWizardForm->importRulesType, $reIndexedPostData); ImportWizardUtil::setFormByPostForStep4($importWizardForm, $sanitizedPostData); $mappingDataMappingRuleFormsAndElementTypes = MappingRuleFormAndElementTypeUtil::makeFormsAndElementTypesByMappingDataAndImportRulesType($importWizardForm->mappingData, $importWizardForm->importRulesType); $validated = MappingRuleFormAndElementTypeUtil::validateMappingRuleForms($mappingDataMappingRuleFormsAndElementTypes); if ($validated) { //Still validate even if MappingRuleForms fails, so all errors are captured and returned. $this->attemptToValidateImportWizardFormAndSave($importWizardForm, $import, 'step5'); } else { $importWizardForm->validate(); $importWizardForm->addError('mappingData', Zurmo::t('ImportModule', 'There are errors with some of your mapping rules. Please fix.')); } } else { $mappingDataMappingRuleFormsAndElementTypes = MappingRuleFormAndElementTypeUtil::makeFormsAndElementTypesByMappingDataAndImportRulesType($importWizardForm->mappingData, $importWizardForm->importRulesType); } $dataProvider = $this->makeDataProviderForSampleRow($import, (bool) $importWizardForm->firstRowIsHeaderRow); if ($importWizardForm->firstRowIsHeaderRow) { $headerRow = ImportDatabaseUtil::getFirstRowByTableName($import->getTempTableName()); assert('$headerRow != null'); } else { $headerRow = null; } $sampleData = $dataProvider->getData(); assert('count($sampleData) == 1'); $sample = current($sampleData); $pagerUrl = Yii::app()->createUrl('import/default/sampleRow', array('id' => $import->id)); $pagerContent = ImportDataProviderPagerUtil::renderPagerAndHeaderTextContent($dataProvider, $pagerUrl); $mappingDataMetadata = ImportWizardMappingViewUtil::resolveMappingDataForView($importWizardForm->mappingData, $sample, $headerRow); $mappableAttributeIndicesAndDerivedTypes = $importRulesClassName::getMappableAttributeIndicesAndDerivedTypes(); $title = Zurmo::t('ImportModule', 'Import Wizard: Step 4 of 6'); $importView = new ImportWizardMappingView($this->getId(), $this->getModule()->getId(), $importWizardForm, $pagerContent, $mappingDataMetadata, $mappingDataMappingRuleFormsAndElementTypes, $mappableAttributeIndicesAndDerivedTypes, $importRulesClassName::getRequiredAttributesLabelsData(), $title); $view = new ImportPageView(ZurmoDefaultAdminViewUtil::makeStandardViewForCurrentUser($this, $importView)); echo $view->render(); }