/** * Helper function to prepare a lot of the necessary information for a data * import. A large amount of this data is stored in the session so as to be * preserved between the AJAX requests which will occur as a part of the import * process. */ public function actionPrepareImport() { $fp = fopen($this->safePath(), 'r+'); // The first row should be just the version number of the data $version = fgetcsv($fp, 0, $_SESSION['delimeter'], $_SESSION['enclosure']); $version = $version[0]; $tempMeta = fgetcsv($fp, 0, $_SESSION['delimeter'], $_SESSION['enclosure']); while ("" === end($tempMeta)) { // Clear all blank rows from the metadata array_pop($tempMeta); } $model = array_pop($tempMeta); // The last column should be the model class $_SESSION['metaData'] = $tempMeta; // Store the current metadata $_SESSION['model'] = $model; // Store the current class $_SESSION['lastFailed'] = ""; /* * THIS IS ESSENTIAL. The ftell function reads the current position in the * file so we know where to start from next time. All AJAX based imports * will neeed to use this function. */ $_SESSION['offset'] = ftell($fp); fclose($fp); $criteria = new CDbCriteria(); $criteria->order = "importId DESC"; $criteria->limit = 1; $import = Imports::model()->find($criteria); if (isset($import)) { // Set the ID of the current import to be 1 higher than the last one $_SESSION['importId'] = $import->importId + 1; } else { $_SESSION['importId'] = 1; } $failedImport = fopen($this->safePath('failedImport.csv'), 'w+'); // Prepare a CSV for any failed records fputcsv($failedImport, array(Yii::app()->params->version), $_SESSION['delimeter'], $_SESSION['enclosure']); fclose($failedImport); echo json_encode(array($version)); }
/** * Helper function to return the next importId * @return int Next import ID */ private function getNextImportId() { $criteria = new CDbCriteria(); $criteria->order = "importId DESC"; $criteria->limit = 1; $import = Imports::model()->find($criteria); // Figure out which import this is so we can set up the Imports models // for this import. if (isset($import)) { $importId = $import->importId + 1; } else { $importId = 1; } return $importId; }