Example #1
0
 /**
  * Handle reconstructing and validating Action associations
  * @param Action $model Action to reconstruct association
  */
 protected function reconstructImportedActionAssoc(Actions &$model)
 {
     $exportableModules = array_merge(array_keys(Modules::getExportableModules()), array('None'));
     $exportableModules = array_map('lcfirst', $exportableModules);
     $model->associationType = lcfirst($model->associationType);
     if (!in_array($model->associationType, $exportableModules)) {
         // Invalid association type
         $model->addError('associationType', Yii::t('admin', 'Unknown associationType.'));
     } else {
         if (isset($model->associationId) && $model->associationId !== '0') {
             $associatedModel = X2Model::model($model->associationType)->findByPk($model->associationId);
             if ($associatedModel) {
                 $model->associationName = $associatedModel->nameId;
             }
         } else {
             if (!isset($model->associationId) && isset($model->associationName)) {
                 // Retrieve associationId
                 $staticAssociationModel = X2Model::model($model->associationType);
                 if ($staticAssociationModel->hasAttribute('name') && !ctype_digit($model->associationName)) {
                     $associationModelParams = array('name' => $model->associationName);
                 } else {
                     $associationModelParams = array('id' => $model->associationName);
                 }
                 $lookup = $staticAssociationModel->findByAttributes($associationModelParams);
                 if (isset($lookup)) {
                     $model->associationId = $lookup->id;
                     $model->associationName = $lookup->hasAttribute('nameId') ? $lookup->nameId : $lookup->name;
                 }
             }
         }
     }
 }
Example #2
0
 /**
  * The general model import page
  */
 public function actionImportModels()
 {
     // Determine the model selected by the user
     if (isset($_GET['model']) || isset($_POST['model'])) {
         $_SESSION['model'] = isset($_GET['model']) ? $_GET['model'] : $_POST['model'];
     }
     // Retrieve specified export delimeter and enclosure
     $_SESSION['delimeter'] = isset($_POST['delimeter']) ? $_POST['delimeter'] : ',';
     $_SESSION['enclosure'] = isset($_POST['enclosure']) ? $_POST['enclosure'] : '"';
     if (isset($_FILES['data'])) {
         $temp = CUploadedFile::getInstanceByName('data');
         $temp->saveAs($filePath = $this->safePath('data.csv'));
         ini_set('auto_detect_line_endings', 1);
         // Account for Mac based CSVs if possible
         $_SESSION['csvLength'] = 0;
         if (file_exists($filePath)) {
             $fp = fopen($filePath, 'r+');
             $csvLength = $this->calculateCsvLength($filePath);
             $this->fixCsvLineEndings($filePath);
         } else {
             throw new Exception('There was an error saving the models file.');
         }
         list($meta, $x2attributes) = $this->initializeModelImporter($fp);
         $preselectedMap = false;
         if (isset($_POST['x2maps'])) {
             // Use an existing import map from the app
             $importMap = $this->loadImportMap($_POST['x2maps']);
             if (empty($importMap)) {
                 $_SESSION['errors'] = Yii::t('admin', 'Unable to load import map');
                 $this->redirect('importModels');
             }
             $_SESSION['importMap'] = $this->normalizeImportMap($importMap['mapping'], $meta);
             $_SESSION['mapName'] = $importMap['name'];
             // Make sure $importMap is consistent with and without an uploaded import map
             $importMap = $_SESSION['importMap'];
             $preselectedMap = true;
         } else {
             if (CUploadedFile::getInstanceByName('mapping') instanceof CUploadedFile && CUploadedFile::getInstanceByName('mapping')->size > 0) {
                 $this->loadUploadedImportMap();
                 $_SESSION['importMap'] = $this->normalizeImportMap($_SESSION['importMap'], $meta);
                 $preselectedMap = true;
                 $importMap = $_SESSION['importMap'];
             } else {
                 // Set up import map via the internal function
                 $this->createImportMap($x2attributes, $meta);
                 $importMap = $_SESSION['importMap'];
                 // We need the flipped version to display to users more easily which
                 // of their fields maps to what X2 field
                 $importMap = array_flip($importMap);
             }
         }
         $sampleRecords = $this->prepareImportSampleRecords($meta, $fp);
         fclose($fp);
         // Remove the import failures column; the user doesn't need to know about it
         $meta = array_filter($meta, function ($x) {
             return $x !== 'X2_Import_Failures';
         });
         $this->render('processModels', array('attributes' => $x2attributes, 'meta' => $meta, 'csvLength' => isset($csvLength) ? $csvLength : null, 'fields' => $_SESSION['fields'], 'model' => str_replace(' ', '', $_SESSION['model']), 'sampleRecords' => $sampleRecords, 'importMap' => $importMap, 'preselectedMap' => $preselectedMap));
     } else {
         $modelList = Modules::getExportableModules();
         $errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : "";
         $this->render('importModels', array('model' => isset($_SESSION['model']) ? $_SESSION['model'] : '', 'modelList' => $modelList, 'errors' => $errors));
     }
 }
Example #3
0
 /**
  * The general model import page
  */
 public function actionImportModels()
 {
     // Determine the model selected by the user
     if (isset($_GET['model']) || isset($_POST['model'])) {
         $_SESSION['model'] = isset($_GET['model']) ? $_GET['model'] : $_POST['model'];
     }
     // Retrieve specified export delimeter and enclosure
     $_SESSION['delimeter'] = isset($_POST['delimeter']) ? $_POST['delimeter'] : ',';
     $_SESSION['enclosure'] = isset($_POST['enclosure']) ? $_POST['enclosure'] : '"';
     // Retrive the default map option selected by the user, otherwise DO NOT MAP by default
     $defaultMapping = isset($_POST['defaultMapOption']) ? $_POST['defaultMapOption'] : '';
     if (isset($_FILES['data'])) {
         $temp = CUploadedFile::getInstanceByName('data');
         $temp->saveAs($filePath = $this->safePath('data.csv'));
         ini_set('auto_detect_line_endings', 1);
         // Account for Mac based CSVs if possible
         $_SESSION['csvLength'] = 0;
         if (file_exists($filePath)) {
             $fp = fopen($filePath, 'r+');
             $csvLength = $this->calculateCsvLength($filePath);
             $this->fixCsvLineEndings($filePath);
         } else {
             throw new Exception('There was an error saving the models file.');
         }
         list($meta, $x2attributes) = $this->initializeModelImporter($fp);
         $preselectedMap = false;
         if (isset($_POST['x2maps'])) {
             // Use an existing import map from the app
             $importMap = $this->loadImportMap($_POST['x2maps']);
             if (empty($importMap)) {
                 $_SESSION['errors'] = Yii::t('admin', 'Unable to load import map');
                 $this->redirect('importModels');
             }
             $_SESSION['importMap'] = $this->normalizeImportMap($importMap['mapping'], $meta);
             $_SESSION['mapName'] = $importMap['name'];
             // Make sure $importMap is consistent with and without an uploaded import map
             $importMap = $_SESSION['importMap'];
             $preselectedMap = true;
         } else {
             if (CUploadedFile::getInstanceByName('mapping') instanceof CUploadedFile && CUploadedFile::getInstanceByName('mapping')->size > 0) {
                 $this->loadUploadedImportMap();
                 $_SESSION['importMap'] = $this->normalizeImportMap($_SESSION['importMap'], $meta);
                 $preselectedMap = true;
                 $importMap = $_SESSION['importMap'];
             } else {
                 // Set up import map via the internal function
                 $this->createImportMap($x2attributes, $meta);
                 $importMap = $_SESSION['importMap'];
                 // We need the flipped version to display to users more easily which
                 // of their fields maps to what X2 field
                 $importMap = array_flip($importMap);
             }
         }
         $sampleRecords = $this->prepareImportSampleRecords($meta, $fp);
         fclose($fp);
         // Remove the import failures column; the user doesn't need to know about it
         $meta = array_filter($meta, function ($x) {
             return $x !== 'X2_Import_Failures';
         });
         // Retrieve link-type fields and associations to present a selector for the
         // field in the related model to search on
         $linkFields = Fields::model()->findAllByAttributes(array('modelName' => $_SESSION['model'], 'type' => 'link'));
         $linkFieldModelMap = array();
         foreach ($linkFields as $field) {
             $linkFieldModelMap[$field['fieldName']] = $field['linkType'];
         }
         $possibleModels = array_keys(Modules::getExportableModules());
         foreach ($possibleModels as $model) {
             $attributes = Fields::model()->findAllByAttributes(array('modelName' => $model));
             foreach ($attributes as $attr) {
                 $listData[$attr['fieldName']] = $attr['attributeLabel'];
             }
             $dropdown = CHtml::dropDownList('attr', 'name', $listData, array('class' => 'linkMatchSelector'));
             $linkedRecordDropdowns[$model] = $dropdown;
         }
         $this->render('processModels', array('defaultMapping' => $defaultMapping, 'attributes' => $x2attributes, 'meta' => $meta, 'csvLength' => isset($csvLength) ? $csvLength : null, 'fields' => $_SESSION['fields'], 'model' => str_replace(' ', '', $_SESSION['model']), 'sampleRecords' => $sampleRecords, 'importMap' => $importMap, 'preselectedMap' => $preselectedMap, 'linkFieldModelMap' => $linkFieldModelMap, 'linkedRecordDropdowns' => $linkedRecordDropdowns));
     } else {
         $modelList = Modules::getExportableModules();
         $errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : "";
         $this->render('importModels', array('model' => isset($_SESSION['model']) ? $_SESSION['model'] : '', 'modelList' => $modelList, 'errors' => $errors));
     }
 }