/** * 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)); }
/** * Check that the default value is appropriate given the type of the field. * * @param string $attribute * @param array $params */ public function validDefault($attribute, $params = array()) { if ($this->fieldName == '') { return; } // Nothing is possible without the field name. Validation will fail for it accordingly. // Use the amorphous model for "proxy" validation, and use a "dummy" // field model (because we'll need to set the name differently to make // things easier on ourselves, given how user input for field name might // not be appropriate for a property name) $dummyModel = new AmorphousModel(); $dummyField = new Fields(); foreach ($this->attributes as $name => $value) { $dummyField->{$name} = $value; } $dummyField->fieldName = 'customized_field'; $dummyModel->scenario = 'insert'; $dummyModel->addField($dummyField, 'customized_field'); $dummyModel->setAttribute('customized_field', $this->{$attribute}); $dummyModel->validate(); if ($dummyModel->hasErrors('customized_field')) { foreach ($dummyModel->errors['customized_field'] as $error) { $this->addError($attribute, str_replace($dummyField->attributeLabel, $dummyField->getAttributeLabel($attribute), $error)); } } }