コード例 #1
0
 public static function createTempTableByFileNameAndTableName($fileName, $tableName, $firstRowIsHeader = false, $pathToFiles = null, $delimiter = ',', $enclosure = '"')
 {
     assert('is_string($fileName)');
     assert('is_string($tableName)');
     if ($pathToFiles == null) {
         $pathToFiles = Yii::getPathOfAlias('application.modules.import.tests.unit.files');
     }
     $filePath = $pathToFiles . DIRECTORY_SEPARATOR . $fileName;
     // Use this for just those two files, because orginal files will be overwritten
     // For these two files, we make backup
     if ($fileName == 'importTestMacOsLineEndingsCopy.csv' || $fileName == 'importTestWindowsCopy.csv') {
         ImportUploadedFileUtil::convertWindowsAndMacLineEndingsIntoUnixLineEndings($filePath);
     }
     $fileHandle = fopen($filePath, 'r');
     if ($fileHandle !== false) {
         $created = ImportDatabaseUtil::makeDatabaseTableByFileHandleAndTableName($fileHandle, $tableName, $delimiter, $enclosure, $firstRowIsHeader);
         assert('$created');
         return true;
     }
     return false;
 }
コード例 #2
0
 /**
  * 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);
 }