/**
  * 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);
 }
 /**
  * @depends testSetFormByPostForStep1
  */
 public function testSetFormByFileUploadData()
 {
     $explicitReadWriteModelPermissions = new ExplicitReadWriteModelPermissions();
     $explicitReadWriteModelPermissions->addReadOnlyPermitable(new Group());
     $this->assertEquals(1, $explicitReadWriteModelPermissions->getReadOnlyPermitablesCount());
     $fileUploadData = array('a', 'b');
     $testTableName = 'testimporttable';
     $this->assertTrue(ImportTestHelper::createTempTableByFileNameAndTableName('importTest.csv', $testTableName, true));
     $importWizardForm = new ImportWizardForm();
     $importWizardForm->importRulesType = 'testAbc';
     $importWizardForm->explicitReadWriteModelPermissions = $explicitReadWriteModelPermissions;
     ImportWizardUtil::setFormByFileUploadDataAndTableName($importWizardForm, $fileUploadData, $testTableName);
     $this->assertEquals(array('a', 'b'), $importWizardForm->fileUploadData);
     $this->assertEquals('testAbc', $importWizardForm->importRulesType);
     $this->assertEquals(0, $importWizardForm->explicitReadWriteModelPermissions->getReadOnlyPermitablesCount());
     $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, $importWizardForm->mappingData);
 }