public function populateFromPostData($data)
 {
     $fieldPrefix = 'Default-';
     if (empty($data['Default']) && !empty($data[$fieldPrefix . 'Year']) && !empty($data[$fieldPrefix . 'Month']) && !empty($data[$fieldPrefix . 'Day'])) {
         $data['Default'] = $data['Year'] . '-' . $data['Month'] . '-' . $data['Day'];
     }
     parent::populateFromPostData($data);
 }
 public function getFieldValidationOptions()
 {
     $fields = parent::getFieldValidationOptions();
     $min = $this->getSetting('MinValue') ? $this->getSetting('MinValue') : '';
     $max = $this->getSetting('MaxValue') ? $this->getSetting('MaxValue') : '';
     $extraFields = new FieldList(new NumericField($this->getSettingName('MinValue'), _t('EditableField.MINVALUE', 'Min Value'), $min), new NumericField($this->getSettingName('MaxValue'), _t('EditableField.MAXVALUE', 'Max Value'), $max));
     $fields->merge($extraFields);
     return $fields;
 }
 public function getFieldConfiguration()
 {
     $levels = ['1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6'];
     $level = $this->getSetting('Level') ? $this->getSetting('Level') : 3;
     $label = _t('EditableFieldHeading.LEVEL', 'Select Heading Level');
     $options = parent::getFieldConfiguration();
     $options->push(new DropdownField($this->getSettingName("Level"), $label, $levels, $level));
     return $options;
 }
 public function getFieldConfiguration()
 {
     $fields = parent::getFieldConfiguration();
     $min = $this->getSetting('MinLength') ? $this->getSetting('MinLength') : '';
     $max = $this->getSetting('MaxLength') ? $this->getSetting('MaxLength') : '';
     $rows = $this->getSetting('Rows') ? $this->getSetting('Rows') : '1';
     $extraFields = new FieldList(new FieldGroup(_t('EditableFieldText.TEXTLENGTH', 'Text length'), new NumericField($this->getSettingName('MinLength'), "", $min), new NumericField($this->getSettingName('MaxLength'), " - ", $max)), new NumericField($this->getSettingName('Rows'), _t('EditableFieldText.NUMBERROWS', 'Number of rows'), $rows));
     $fields->merge($extraFields);
     return $fields;
 }
 /**
  * On before saving this object we need to go through and keep an eye on
  * all our option fields that are related to this field in the form
  *
  * @param array
  */
 public function populateFromPostData($data)
 {
     parent::populateFromPostData($data);
     // get the current options
     $fieldSet = $this->Options();
     // go over all the current options and check if ID and Title still exists
     foreach ($fieldSet as $option) {
         if (isset($data[$option->ID]) && isset($data[$option->ID]['Title']) && $data[$option->ID]['Title'] != "field-node-deleted") {
             $option->populateFromPostData($data[$option->ID]);
         } else {
             $option->delete();
         }
     }
 }
 /**
  * main function called to update column in database
  *
  */
 public function update()
 {
     //get params from request
     $this->primaryKey = yii::app()->request->getParam('pk');
     $this->attribute = yii::app()->request->getParam('name');
     $this->value = yii::app()->request->getParam('value');
     $this->scenario = yii::app()->request->getParam('scenario');
     //checking params
     if (empty($this->attribute)) {
         throw new CException(Yii::t('EditableSaver.editable', 'Property "attribute" should be defined.'));
     }
     $this->model = new $this->modelClass();
     $isFormModel = $this->model instanceof CFormModel;
     $isMongo = EditableField::isMongo($this->model);
     if (empty($this->primaryKey) && !$isFormModel) {
         throw new CException(Yii::t('EditableSaver.editable', 'Property "primaryKey" should be defined.'));
     }
     //loading model
     if ($isMongo) {
         $this->model = $this->model->findByPk(new MongoID($this->primaryKey));
     } elseif (!$isFormModel) {
         $this->model = $this->model->findByPk($this->primaryKey);
     }
     if (!$this->model) {
         throw new CException(Yii::t('EditableSaver.editable', 'Model {class} not found by primary key "{pk}"', array('{class}' => get_class($this->model), '{pk}' => is_array($this->primaryKey) ? CJSON::encode($this->primaryKey) : $this->primaryKey)));
     }
     //keep parent model for mongo
     $originalModel = $this->model;
     //resolve model only for mongo! we should check attribute safety
     if ($isMongo) {
         $resolved = EditableField::resolveModels($this->model, $this->attribute);
         $this->model = $resolved['model'];
         //can be related model now
         $this->attribute = $resolved['attribute'];
         $staticModel = $resolved['staticModel'];
     } else {
         $staticModel = $this->model;
     }
     //set scenario for main model
     if ($this->scenario) {
         $originalModel->setScenario($this->scenario);
     }
     //is attribute safe
     if (!$this->model->isAttributeSafe($this->attribute)) {
         throw new CException(Yii::t('editable', 'Model {class} rules do not allow to update attribute "{attr}"', array('{class}' => get_class($this->model), '{attr}' => $this->attribute)));
     }
     //setting new value
     $this->setAttribute($this->attribute, $this->value);
     //validate attribute
     $this->model->validate(array($this->attribute));
     $this->checkErrors();
     //trigger beforeUpdate event
     $this->beforeUpdate();
     $this->checkErrors();
     //remove virtual attributes (which NOT in DB table)
     if (!$isMongo) {
         $this->changedAttributes = array_intersect($this->changedAttributes, $originalModel->attributeNames());
         if (count($this->changedAttributes) == 0) {
             //can not pass empty array in model->save() method!
             $this->changedAttributes = null;
         }
     }
     //saving (no validation, only changed attributes) note: for mongo save all!
     if ($isMongo) {
         $result = $originalModel->save(false, null);
     } elseif (!$isFormModel) {
         $result = $originalModel->save(false, $this->changedAttributes);
     } else {
         $result = true;
     }
     if ($result) {
         $this->afterUpdate();
     } else {
         $this->error(Yii::t('EditableSaver.editable', 'Error while saving record!'));
     }
 }
 public function getFieldConfiguration()
 {
     $options = parent::getFieldConfiguration();
     $options->push(new CheckboxField("Fields[{$this->ID}][CustomSettings][Default]", _t('EditableField.CHECKEDBYDEFAULT', 'Checked by Default?'), $this->getSetting('Default')));
     return $options;
 }
 /**
  * Action to create field option (ie. dropdown, checkbox). Called from ajax request.
  *
  * @param $record
  *
  * @return bool|SS_HTTPResponse|void
  * @throws SS_HTTPResponse_Exception
  */
 public function addOptionField($record)
 {
     // Check security token
     if (!SecurityToken::inst()->checkRequest($this->request)) {
         return $this->httpError(400);
     }
     // Can user edit fields details
     if (!$this->canEdit()) {
         return Security::permissionFailure();
     }
     // The parent id of the option field (the id of the field (ie. dropdown)
     $parent = isset($record['Parent']) ? $record['Parent'] : false;
     if ($parent) {
         $parentObj = EditableField::get()->byID($parent);
         $optionClass = $parentObj && $parentObj->exists() ? $parentObj->getRelationClass('Options') : 'EditableFieldOption';
         // Work out the sort by getting the sort of the last field in the form +1
         $sqlQuery = new SQLQuery();
         $sqlQuery = $sqlQuery->setSelect("MAX(\"Sort\")")->setFrom("\"EditableFieldOption\"")->setWhere("\"ParentID\" = " . (int) $parent);
         $sort = $sqlQuery->execute()->value() + 1;
         if ($parent) {
             $object = Injector::inst()->create($optionClass);
             $object->write();
             $object->ParentID = $parent;
             $object->Sort = $sort;
             $object->Name = 'option' . $object->ID;
             $object->write();
             return $object->EditSegment();
         }
     }
     return false;
 }