public function testSanitizePostByTypeForSavingMappingData() { $language = Yii::app()->getLanguage(); $this->assertEquals($language, 'en'); $postData = array('column_0' => array('attributeIndexOrDerivedType' => 'date', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => '5/4/2011'))), 'column_1' => array('attributeIndexOrDerivedType' => 'dateTime', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => '5/4/2011 5:45 PM')))); $sanitizedPostData = ImportWizardFormPostUtil::sanitizePostByTypeForSavingMappingData('ImportModelTestItem', $postData); $compareDateTime = DateTimeUtil::convertDateTimeLocaleFormattedDisplayToDbFormattedDateTimeWithSecondsAsZero('5/4/2011 5:45 PM'); $compareData = array('column_0' => array('attributeIndexOrDerivedType' => 'date', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => '2011-05-04'))), 'column_1' => array('attributeIndexOrDerivedType' => 'dateTime', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => $compareDateTime)))); $this->assertEquals($compareData, $sanitizedPostData); //now do German (de) to check a different locale. Yii::app()->setLanguage('de'); $postData = array('column_0' => array('attributeIndexOrDerivedType' => 'date', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => '04.05.2011'))), 'column_1' => array('attributeIndexOrDerivedType' => 'dateTime', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => '04.05.2011 17:45')))); $sanitizedPostData = ImportWizardFormPostUtil::sanitizePostByTypeForSavingMappingData('ImportModelTestItem', $postData); $compareDateTime = DateTimeUtil::convertDateTimeLocaleFormattedDisplayToDbFormattedDateTimeWithSecondsAsZero('04.05.2011 17:45'); $compareData = array('column_0' => array('attributeIndexOrDerivedType' => 'date', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => '2011-05-04'))), 'column_1' => array('attributeIndexOrDerivedType' => 'dateTime', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => $compareDateTime)))); $this->assertEquals($compareData, $sanitizedPostData); //reset language back to english Yii::app()->setLanguage('en'); //test sanitizing a bad datetime $postData = array('column_0' => array('attributeIndexOrDerivedType' => 'dateTime', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => 'wang chung')))); $sanitizedPostData = ImportWizardFormPostUtil::sanitizePostByTypeForSavingMappingData('ImportModelTestItem', $postData); $this->assertNull($sanitizedPostData['column_0']['mappingRulesData']['DefaultValueModelAttributeMappingRuleForm']['defaultValue']); //sanitize an empty datetime $postData = array('column_0' => array('attributeIndexOrDerivedType' => 'dateTime', 'mappingRulesData' => array('DefaultValueModelAttributeMappingRuleForm' => array('defaultValue' => '')))); $sanitizedPostData = ImportWizardFormPostUtil::sanitizePostByTypeForSavingMappingData('ImportModelTestItem', $postData); $this->assertEmpty($sanitizedPostData['column_0']['mappingRulesData']['DefaultValueModelAttributeMappingRuleForm']['defaultValue']); }
/** * Step 4. Import mapping */ public function actionStep4($id) { $import = Import::getById((int) $id); $importWizardForm = ImportWizardUtil::makeFormByImport($import); $importWizardForm->setScenario('saveMappingData'); $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 = ZurmoRedBean::$writer->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 - Map Fields'); $importRulesClassName = ImportRulesUtil::getImportRulesClassNameByType($importWizardForm->importRulesType); $stepToUse = ImportStepsAndProgressBarForWizardView::resolveAfterUploadStepByImportClassName(3, $importRulesClassName); $progressBarAndStepsView = new ImportStepsAndProgressBarForWizardView($importRulesClassName, $stepToUse); $importView = new ImportWizardMappingView($this->getId(), $this->getModule()->getId(), $importWizardForm, $pagerContent, $mappingDataMetadata, $mappingDataMappingRuleFormsAndElementTypes, $mappableAttributeIndicesAndDerivedTypes, $importRulesClassName::getRequiredAttributesLabelsData(), $title); echo $this->getImportPageView($progressBarAndStepsView, $importView)->render(); }