Пример #1
0
 /**
  * Form/submit action for adding or customizing a field.
  *
  * This method allows for the creation of custom fields linked to any customizable
  * module in X2Engine.  This is used by "Manage Fields." It is used to reload the
  * form via AJAX.
  * 
  * @param bool $search If set to 1/true, perform a lookup for an existing field
  * @param bool $save If set to 1/true, attempt to save the model; otherwise just echo the form.
  */
 public function actionCreateUpdateField($search = 0, $save = 0, $override = 0)
 {
     $changedType = false;
     if ($search) {
         // A field is being looked up, to populate form fields for customizing
         // an existing field
         $new = false;
         if (isset($_POST['Fields'])) {
             $model = Fields::model()->findByAttributes(array_intersect_key($_POST['Fields'], array_fill_keys(array('modelName', 'fieldName'), null)));
         }
     } else {
         // Requesting the form
         $new = true;
     }
     if (!isset($model) || !(bool) $model) {
         // If the field model wasn't found, create the object
         $model = new Fields();
     }
     if (isset($_POST['Fields']) && ($model->isNewRecord || $override)) {
         $oldType = $model->type;
         $model->attributes = $_POST['Fields'];
         // field name exists if model refers to actual db record
         if ($model->fieldName && $model->type !== $oldType) {
             $changedType = true;
         }
     }
     $message = '';
     $error = false;
     if (isset($_POST['Fields']) && $save) {
         $model->attributes = $_POST['Fields'];
         if (!isset($_POST['Fields']['linkType'])) {
             // This can be removed if we ever make the linkType attribute more structured
             // (i.e. field type-specific link type validation rules)
             $model->linkType = null;
         }
         // Set the default value
         if (isset($_POST['AmorphousModel'])) {
             $aModel = $_POST['AmorphousModel'];
             $model->defaultValue = $model->parseValue($aModel['customized_field']);
         }
         $new = $model->isNewRecord;
         $model->modified = 1;
         // The field has been modified
         if ($new) {
             // The field should be marked as custom since the user is adding it
             $model->custom = 1;
         }
         if ($model->save()) {
             $message = $new ? Yii::t('admin', 'Field added.') : Yii::t('admin', 'Field modified successfully.');
             if ($new) {
                 $model = new Fields();
             }
         } else {
             $error = true;
             $message = Yii::t('admin', 'Please correct the following errors.');
         }
     }
     $dummyModel = new AmorphousModel();
     $dummyModel->addField($model, 'customized_field');
     $dummyModel->setAttribute('customized_field', $model->defaultValue);
     $this->renderPartial('createUpdateField', array('model' => $model, 'new' => $new, 'dummyModel' => $dummyModel, 'message' => $message, 'error' => $error, 'changedType' => $changedType));
 }