/** * The import assumes we have human readable data in the CSV and will thus need to convert. This * method converts link, date, and dateTime fields to the appropriate machine friendly data. * @param string $modelName The model class being imported * @param X2Model $model The currently importing model record * @param string $fieldName Field to set * @param string $importAttribute Value to set field * @returns X2Model $model */ protected function importRecordAttribute($modelName, X2Model $model, $fieldName, $importAttribute) { $fieldRecord = Fields::model()->findByAttributes(array('modelName' => $modelName, 'fieldName' => $fieldName)); // Skip setting the attribute if it has already been set or if the entry from // the CSV is empty. if (empty($importAttribute) && ($importAttribute !== 0 && $importAttribute !== '0')) { return $model; } if ($fieldName === 'actionDescription' && $modelName === 'Actions') { $text = new ActionText(); $text->text = $importAttribute; if (isset($model->id)) { $text->actionId = $model->id; } $this->setCurrentActionText($text->attributes); return $model; } // ensure the provided id is valid if (strtolower($fieldName) === 'id' && (!preg_match('/^\\d+$/', $importAttribute) || $importAttribute >= 4294967295)) { $model->id = $importAttribute; $model->addError('id', Yii::t('importexport', "ID '{$importAttribute}' is not valid.")); return $model; } switch ($fieldRecord->type) { case "link": $model = $this->importRecordLinkAttribute($modelName, $model, $fieldRecord, $importAttribute); break; case "dateTime": case "date": if (Formatter::parseDateTime($importAttribute) !== false) { $model->{$fieldName} = Formatter::parseDateTime($importAttribute); } break; case "visibility": switch ($importAttribute) { case 'Private': $model->{$fieldName} = 0; break; case 'Public': $model->{$fieldName} = 1; break; case 'User\'s Groups': $model->{$fieldName} = 2; break; default: $model->{$fieldName} = $importAttribute; } break; default: $model->{$fieldName} = $importAttribute; } return $model; }