Ejemplo n.º 1
0
 public function testProcessStatusAndMessagesForEachRow()
 {
     Yii::app()->user->userModel = User::getByUsername('super');
     $import = new Import();
     $serializedData['importRulesType'] = 'ImportModelTestItem';
     $import->serializedData = serialize($serializedData);
     $this->assertTrue($import->save());
     $testTableName = $import->getTempTableName();
     $this->assertTrue(ImportTestHelper::createTempTableByFileNameAndTableName('importTest.csv', $testTableName));
     $count = ImportDatabaseUtil::getCount($testTableName);
     $this->assertEquals(5, $count);
     //Now add import results.
     $resultsUtil = new ImportResultsUtil($import);
     $rowDataResultsUtil = new ImportRowDataResultsUtil(2);
     $rowDataResultsUtil->setStatusToUpdated();
     $rowDataResultsUtil->addMessage('the first message');
     $resultsUtil->addRowDataResults($rowDataResultsUtil);
     $rowDataResultsUtil = new ImportRowDataResultsUtil(3);
     $rowDataResultsUtil->setStatusToCreated();
     $rowDataResultsUtil->addMessage('the second message');
     $resultsUtil->addRowDataResults($rowDataResultsUtil);
     $rowDataResultsUtil = new ImportRowDataResultsUtil(4);
     $rowDataResultsUtil->setStatusToError();
     $rowDataResultsUtil->addMessage('the third message');
     $resultsUtil->addRowDataResults($rowDataResultsUtil);
     $resultsUtil->processStatusAndMessagesForEachRow();
     $sql = 'select * from ' . $testTableName . ' where id != 1';
     $tempTableData = R::getAll($sql);
     $compareData = array(array('id' => 2, 'column_0' => 'abc', 'column_1' => '123', 'column_2' => 'a', 'status' => 1, 'serializedmessages' => serialize(array('the first message'))), array('id' => 3, 'column_0' => 'def', 'column_1' => '563', 'column_2' => 'b', 'status' => 2, 'serializedmessages' => serialize(array('the second message'))), array('id' => 4, 'column_0' => 'efg', 'column_1' => '456', 'column_2' => 'a', 'status' => 3, 'serializedmessages' => serialize(array('the third message'))), array('id' => 5, 'column_0' => 'we1s', 'column_1' => null, 'column_2' => 'b', 'status' => null, 'serializedmessages' => null));
     $this->assertEquals($compareData, $tempTableData);
 }
 protected function getArray()
 {
     $data = array(ImportResultsConfigurationForm::FILTERED_BY_ALL => Zurmo::t('Core', 'All'));
     $data[ImportRowDataResultsUtil::CREATED] = ImportRowDataResultsUtil::getStatusLabelByType(ImportRowDataResultsUtil::CREATED);
     $data[ImportRowDataResultsUtil::UPDATED] = ImportRowDataResultsUtil::getStatusLabelByType(ImportRowDataResultsUtil::UPDATED);
     $data[ImportRowDataResultsUtil::ERROR] = ImportRowDataResultsUtil::getStatusLabelByType(ImportRowDataResultsUtil::ERROR);
     return $data;
 }
 public function testSetAndGetStatus()
 {
     $resultsUtil = new ImportRowDataResultsUtil(55);
     $this->assertNull($resultsUtil->getStatus());
     $resultsUtil->setStatusToUpdated();
     $this->assertEquals(1, $resultsUtil->getStatus());
     $resultsUtil->setStatusToCreated();
     $this->assertEquals(2, $resultsUtil->getStatus());
     $resultsUtil->setStatusToError();
     $this->assertEquals(3, $resultsUtil->getStatus());
 }
 public static function resolveResultStatusLabel($data)
 {
     return ImportRowDataResultsUtil::getStatusLabelAndVisualIdentifierContentByType((int) $data->status);
 }
Ejemplo n.º 5
0
 /**
  * Process mapped columns to check for matched record by column
  * @param ImportRules $importRules
  * @param RedBeanModel $model
  * @param array $mappingData
  * @param RedBean_OODBBean $rowBean
  * @param ImportSanitizeResultsUtil $importSanitizeResultsUtil
  * @param array $skippedColumns
  */
 protected static function processDedupeAttributesToCheckForSkipIfRequired(ImportRules $importRules, RedBeanModel $model, $mappingData, $rowBean, ImportRowDataResultsUtil $importRowDataResultsUtil, ImportSanitizeResultsUtil $importSanitizeResultsUtil, &$skippedColumns)
 {
     assert('$rowBean instanceof RedBean_OODBBean');
     $isSkipped = false;
     //Process the dedupe fields
     foreach ($importRules->getDedupeAttributes() as $dedupeField) {
         $sourceColumnName = static::getMappedColumnNameByMappingData($dedupeField, $mappingData);
         if (false !== $sourceColumnName) {
             $columnMappingData = $mappingData[$sourceColumnName];
             $attributeValueData = static::getAttributeValueData($importRules, $sourceColumnName, $columnMappingData, $rowBean, $importSanitizeResultsUtil);
             if (!$importSanitizeResultsUtil->shouldSaveModel()) {
                 $importRowDataResultsUtil->addMessages($importSanitizeResultsUtil->getMessages());
                 $importRowDataResultsUtil->setStatusToError();
                 $isSkipped = true;
             }
             $skippedColumns[] = $sourceColumnName;
         }
     }
     return $isSkipped;
 }
Ejemplo n.º 6
0
 /**
  * Given a row of data, resolve each value of the row for import and either create or update an existing model.
  * @param object $importRules
  * @param array $rowData
  * @param array $mappingData
  * @param object $importRowDataResultsUtil
  */
 public static function importByImportRulesRowData(ImportRules $importRules, $rowBean, $mappingData, ImportRowDataResultsUtil $importRowDataResultsUtil, ExplicitReadWriteModelPermissions $explicitReadWriteModelPermissions)
 {
     assert('$rowBean instanceof RedBean_OODBBean');
     assert('is_array($mappingData)');
     $makeNewModel = true;
     $modelClassName = $importRules->getModelClassName();
     $externalSystemId = null;
     $importSanitizeResultsUtil = new ImportSanitizeResultsUtil();
     $afterSaveActionsData = array();
     //Process the 'id' column first if available.
     if (false !== ($idColumnName = static::getIdColumnNameByMappingData($mappingData))) {
         $columnMappingData = $mappingData[$idColumnName];
         $attributeImportRules = AttributeImportRulesFactory::makeByImportRulesTypeAndAttributeIndexOrDerivedType($importRules::getType(), $columnMappingData['attributeIndexOrDerivedType']);
         $valueReadyToSanitize = static::resolveValueToSanitizeByValueAndColumnType($rowBean->{$idColumnName}, $columnMappingData['type']);
         $attributeValueData = $attributeImportRules->resolveValueForImport($valueReadyToSanitize, $idColumnName, $columnMappingData, $importSanitizeResultsUtil);
         assert('count($attributeValueData) == 0 || count($attributeValueData) == 1');
         if (isset($attributeValueData['id']) && $attributeValueData['id'] != null) {
             $model = $modelClassName::getById($attributeValueData['id']);
             $makeNewModel = false;
         } elseif (isset($attributeValueData[ExternalSystemIdUtil::EXTERNAL_SYSTEM_ID_COLUMN_NAME]) && $attributeValueData[ExternalSystemIdUtil::EXTERNAL_SYSTEM_ID_COLUMN_NAME] != null) {
             $externalSystemId = $attributeValueData[ExternalSystemIdUtil::EXTERNAL_SYSTEM_ID_COLUMN_NAME];
         }
     }
     if ($makeNewModel) {
         $model = new $modelClassName();
         $model->setScenario('importModel');
     }
     //Process the rest of the mapped colummns. ignoring owner.
     foreach ($mappingData as $columnName => $columnMappingData) {
         if ($columnMappingData['attributeIndexOrDerivedType'] != null && $columnMappingData['attributeIndexOrDerivedType'] != 'owner' && $idColumnName != $columnName) {
             static::sanitizeValueAndPopulateModel($rowBean, $importRules, $model, $columnName, $modelClassName, $columnMappingData, $importSanitizeResultsUtil, $afterSaveActionsData);
         }
     }
     //Process the owner column if present
     foreach ($mappingData as $columnName => $columnMappingData) {
         if ($columnMappingData['attributeIndexOrDerivedType'] != null && $columnMappingData['attributeIndexOrDerivedType'] == 'owner' && $idColumnName != $columnName) {
             static::sanitizeValueAndPopulateModel($rowBean, $importRules, $model, $columnName, $modelClassName, $columnMappingData, $importSanitizeResultsUtil, $afterSaveActionsData);
         }
     }
     $validated = $model->validate();
     if ($validated && $importSanitizeResultsUtil->shouldSaveModel()) {
         $saved = $model->save();
         if ($saved) {
             static::processAfterSaveActions($afterSaveActionsData, $model);
             if ($externalSystemId != null) {
                 ExternalSystemIdUtil::updateByModel($model, $externalSystemId);
             }
             $importRowDataResultsUtil->addMessage(Zurmo::t('ImportModule', '{modelLabel} saved correctly: {linkToModel}', array('{modelLabel}' => $model->getModelLabelByTypeAndLanguage('Singular'), '{linkToModel}' => static::resolveLinkMessageToModel($model))));
             if ($makeNewModel) {
                 if ($model instanceof SecurableItem) {
                     try {
                         $resolved = ExplicitReadWriteModelPermissionsUtil::resolveExplicitReadWriteModelPermissions($model, $explicitReadWriteModelPermissions);
                         $importRowDataResultsUtil->setStatusToCreated();
                         if (!$resolved) {
                             $importRowDataResultsUtil->addMessage('The record saved, but there was a problem ' . 'setting the security permissions. It will at least be viewable by the owner.');
                             $importRowDataResultsUtil->setStatusToError();
                         }
                     } catch (AccessDeniedSecurityException $e) {
                         $importRowDataResultsUtil->addMessage('The record saved, but you do not have permissions ' . 'to set the security the way you did. The record will only be viewable by the owner.');
                         $importRowDataResultsUtil->setStatusToError();
                     }
                 } else {
                     $importRowDataResultsUtil->setStatusToCreated();
                 }
             } else {
                 $importRowDataResultsUtil->setStatusToUpdated();
             }
         } else {
             $importRowDataResultsUtil->addMessage('The record failed to save. Reason unknown.');
             $importRowDataResultsUtil->setStatusToError();
         }
     } else {
         if (!$importSanitizeResultsUtil->shouldSaveModel()) {
             $importRowDataResultsUtil->addMessages($importSanitizeResultsUtil->getMessages());
         }
         $messages = RedBeanModelErrorsToMessagesUtil::makeMessagesByModel($model);
         if (count($messages) > 0) {
             $importRowDataResultsUtil->addMessages($messages);
         }
         $importRowDataResultsUtil->setStatusToError();
     }
 }
Ejemplo n.º 7
0
 protected function makeImportCompleteView(Import $import, ImportWizardForm $importWizardForm, ImportDataProvider $dataProvider, $setCurrentPageToFirst = false, $pageSize = null)
 {
     if ($pageSize == null) {
         $pageSize = Yii::app()->pagination->resolveActiveForCurrentUserByType('listPageSize');
     }
     $config = array('pagination' => array('pageSize' => $pageSize));
     $unserializedData = unserialize($import->serializedData);
     $importCompleteView = new ImportWizardCreateUpdateModelsCompleteView($this->getId(), $this->getModule()->getId(), $importWizardForm, $dataProvider, $unserializedData['mappingData'], (int) ImportRowDataResultsUtil::getCreatedCount($import->getTempTableName()), (int) ImportRowDataResultsUtil::getUpdatedCount($import->getTempTableName()), (int) ImportRowDataResultsUtil::getErrorCount($import->getTempTableName()));
     return $importCompleteView;
 }
Ejemplo n.º 8
0
 protected function makeImportCompleteView(Import $import, ImportWizardForm $importWizardForm, $setCurrentPageToFirst = false)
 {
     $pageSize = Yii::app()->pagination->resolveActiveForCurrentUserByType('listPageSize');
     $config = array('pagination' => array('pageSize' => $pageSize));
     if ($setCurrentPageToFirst) {
         $config['pagination']['currentPage'] = 0;
     }
     $importErrorsDataProvider = new ImportDataProvider($import->getTempTableName(), (bool) $importWizardForm->firstRowIsHeaderRow, $config, ImportRowDataResultsUtil::ERROR);
     $errorListView = new ImportErrorsListView($this->getId(), $this->getModule()->getId(), 'NotUsed', $importErrorsDataProvider);
     $importCompleteView = new ImportWizardCreateUpdateModelsCompleteView($this->getId(), $this->getModule()->getId(), $importWizardForm, (int) ImportRowDataResultsUtil::getCreatedCount($import->getTempTableName()), (int) ImportRowDataResultsUtil::getUpdatedCount($import->getTempTableName()), (int) ImportRowDataResultsUtil::getErrorCount($import->getTempTableName()), $errorListView);
     return $importCompleteView;
 }