public function testMakeFormByImport() { //Test a brand new import object. $import = new Import(); $form = ImportWizardUtil::makeFormByImport($import); $this->assertTrue($form instanceof ImportWizardForm); $this->assertEquals(null, $form->importRulesType); //Test an import object with existing data and a data element that does not go into the form. $import = new Import(); $dataToSerialize = array('importRulesType' => 'test', 'anElementToIgnore' => 'something'); $import->serializedData = serialize($dataToSerialize); $form = ImportWizardUtil::makeFormByImport($import); $this->assertTrue($form instanceof ImportWizardForm); $this->assertEquals('test', $form->importRulesType); $this->assertEquals(null, $form->fileUploadData); $this->assertEquals(null, $form->firstRowIsHeaderRow); $this->assertTrue($form->explicitReadWriteModelPermissions instanceof ExplicitReadWriteModelPermissions); $this->assertEquals(null, $form->mappingData); $this->assertFalse($form->isAttribute('anElementToIgnore')); }
/** * Ajax action called from user interface to upload an import file. If a file for this import model is * already uploaded, then this will overwrite it. * @param string $filesVariableName * @param string $id (should be integer, but php type casting doesn't work so well) */ public function actionUploadFile($filesVariableName, $id) { assert('is_string($filesVariableName)'); $import = Import::getById((int) $id); $importWizardForm = ImportWizardUtil::makeFormByImport($import); $importWizardForm->setAttributes($_POST['ImportWizardForm']); if (!$importWizardForm->validateRowColumnDelimeterIsNotEmpty()) { $fileUploadData = array('error' => Zurmo::t('ImportModule', 'Error: Invalid delimiter')); } elseif (!$importWizardForm->validateRowColumnEnclosureIsNotEmpty()) { $fileUploadData = array('error' => Zurmo::t('ImportModule', 'Error: Invalid qualifier')); } else { try { $uploadedFile = ImportUploadedFileUtil::getByNameCatchErrorAndEnsureFileIsACSV($filesVariableName); assert('$uploadedFile instanceof CUploadedFile'); ImportUploadedFileUtil::convertWindowsAndMacLineEndingsIntoUnixLineEndings($uploadedFile->getTempName()); $fileHandle = fopen($uploadedFile->getTempName(), 'r'); if ($fileHandle !== false) { $tempTableName = $import->getTempTableName(); try { $tableCreated = ImportDatabaseUtil::makeDatabaseTableByFileHandleAndTableName($fileHandle, $tempTableName, $importWizardForm->rowColumnDelimiter, $importWizardForm->rowColumnEnclosure, $importWizardForm->firstRowIsHeaderRow); if (!$tableCreated) { throw new FailedFileUploadException(Zurmo::t('ImportModule', 'Failed to create temporary database table from CSV.')); } } catch (BulkInsertFailedException $e) { throw new FailedFileUploadException($e->getMessage()); } catch (TooManyColumnsFailedException $e) { throw new FailedFileUploadException($e->getMessage()); } $fileUploadData = array('name' => $uploadedFile->getName(), 'type' => $uploadedFile->getType(), 'size' => $uploadedFile->getSize()); ImportWizardUtil::setFormByFileUploadDataAndTableName($importWizardForm, $fileUploadData, $tempTableName); ImportWizardUtil::setImportSerializedDataFromForm($importWizardForm, $import); if (!$import->save()) { throw new FailedFileUploadException(Zurmo::t('ImportModule', 'Import model failed to save.')); } } else { throw new FailedFileUploadException(Zurmo::t('ImportModule', 'Failed to open the uploaded file.')); } $fileUploadData['id'] = $import->id; } catch (FailedFileUploadException $e) { $fileUploadData = array('error' => Zurmo::t('Core', 'Error') . ' ' . $e->getMessage()); ImportWizardUtil::clearFileAndRelatedDataFromImport($import); } } echo CJSON::encode(array($fileUploadData)); Yii::app()->end(0, false); }