function saveInto(DataObjectInterface $record)
 {
     $fieldName = $this->name;
     $fieldNameID = $fieldName . 'ID';
     $record->{$fieldNameID} = 0;
     if ($val = $this->value[$this->htmlListField]) {
         if ($val != 'undefined') {
             $record->{$fieldNameID} = $val;
         }
     }
     $record->write();
 }
 /**
  * Save value to dataobject
  *
  * @see forms/CheckboxSetField::saveInto()
  */
 public function saveInto(DataObjectInterface $record)
 {
     $fieldName = $this->getName();
     $valueArray = is_array($this->value) && isset($this->value[0]) && strpos($this->value[0], ',') ? explode(',', $this->value[0]) : $this->value;
     if ($fieldName && ($record->has_many($fieldName) || $record->many_many($fieldName))) {
         // Set related records
         $record->{$fieldName}()->setByIDList($valueArray);
     } else {
         $record->{$fieldName} = is_array($this->value) ? implode(',', $this->value) : $this->value;
         $record->write();
     }
 }
 public function saveInto(DataObjectInterface $record)
 {
     if ($this->record) {
         // HACK: Use a fake Form object to save data into fields
         $form = new Form($this->record, $this->name . '-form', $this->FieldList(false), new FieldList());
         $form->loadDataFrom($this->value);
         $form->saveInto($this->record);
         // Save extra data into field
         if (count($this->extraData)) {
             $this->record->castedUpdate($this->extraData);
         }
         if (!$this->record->ID && count($this->defaultFromParent)) {
             foreach ($this->defaultFromParent as $pField => $rField) {
                 if (is_numeric($pField)) {
                     if ($this->record->{$rField}) {
                         continue;
                     }
                     $this->record->setCastedField($rField, $record->{$rField});
                 } else {
                     if ($this->record->{$pField}) {
                         continue;
                     }
                     $this->record->setCastedField($rField, $record->{$pField});
                 }
             }
         }
         if (count($this->overrideFromParent)) {
             foreach ($this->overrideFromParent as $pField => $rField) {
                 if (is_numeric($pField)) {
                     $this->record->setCastedField($rField, $record->{$rField});
                 } else {
                     $this->record->setCastedField($rField, $record->{$pField});
                 }
             }
         }
         $this->record->write();
         $fieldName = substr($this->name, -2) == 'ID' ? $this->name : $this->name . 'ID';
         $record->{$fieldName} = $this->record->ID;
         unset($form);
     }
 }
Exemple #4
0
 /**
  * Save the current value of this field into a DataObject.
  * If the field it is saving to is a has_many or many_many relationship,
  * it is saved by setByIDList(), otherwise it creates a comma separated
  * list for a standard DB text/varchar field.
  *
  * @param DataObject $record The record to save into
  */
 public function saveInto(DataObjectInterface $record)
 {
     if ($this->multiple) {
         $fieldname = $this->name;
         $relation = $fieldname && $record && $record->hasMethod($fieldname) ? $record->{$fieldname}() : null;
         if ($fieldname && $record && $relation && $relation instanceof RelationList) {
             $idList = is_array($this->value) ? array_values($this->value) : array();
             if (!$record->ID) {
                 $record->write();
                 // record needs to have an ID in order to set relationships
                 $relation = $fieldname && $record && $record->hasMethod($fieldname) ? $record->{$fieldname}() : null;
             }
             $relation->setByIDList($idList);
         } elseif ($fieldname && $record) {
             if ($this->value) {
                 $this->value = str_replace(',', '{comma}', $this->value);
                 $record->{$fieldname} = implode(",", $this->value);
             } else {
                 $record->{$fieldname} = null;
             }
         }
     } else {
         parent::saveInto($record);
     }
 }
 /**
  * Update the permission set associated with $record DataObject
  *
  * @param DataObject $record
  */
 public function saveInto(DataObjectInterface $record)
 {
     $fieldname = $this->name;
     $managedClass = $this->managedClass;
     // Remove all "privileged" permissions if the currently logged-in user is not an admin
     $privilegedPermissions = Permission::config()->privileged_permissions;
     if (!Permission::check('ADMIN')) {
         foreach ($this->value as $id => $bool) {
             if (in_array($id, $privilegedPermissions)) {
                 unset($this->value[$id]);
             }
         }
     }
     // remove all permissions and re-add them afterwards
     $permissions = $record->{$fieldname}();
     foreach ($permissions as $permission) {
         $permission->delete();
     }
     if ($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
         if (!$record->ID) {
             $record->write();
         }
         // We need a record ID to write permissions
         $idList = array();
         if ($this->value) {
             foreach ($this->value as $id => $bool) {
                 if ($bool) {
                     $perm = new $managedClass();
                     $perm->{$this->filterField} = $record->ID;
                     $perm->Code = $id;
                     $perm->write();
                 }
             }
         }
     }
 }
 /**
  * Update the permission set associated with $record DataObject
  *
  * @param DataObject $record
  */
 public function saveInto(DataObjectInterface $record)
 {
     $fieldname = $this->name;
     $managedClass = $this->managedClass;
     // remove all permissions and re-add them afterwards
     $permissions = $record->{$fieldname}();
     foreach ($permissions as $permission) {
         $permission->delete();
     }
     if ($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
         if (!$record->ID) {
             $record->write();
         }
         // We need a record ID to write permissions
         $idList = array();
         if ($this->value) {
             foreach ($this->value as $id => $bool) {
                 if ($bool) {
                     $perm = new $managedClass();
                     $perm->{$this->filterField} = $record->ID;
                     $perm->Code = $id;
                     $perm->write();
                 }
             }
         }
     }
 }
 function saveInto(\DataObjectInterface $record)
 {
     // If tags are enabled, saving into a relation will not work properly
     if ($this->tags) {
         $fieldname = str_replace('[]', '', $this->name);
         $relation = $fieldname && $record && $record->hasMethod($fieldname) ? $record->{$fieldname}() : null;
         if ($fieldname && $record && $relation && ($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) {
             $idList = is_array($this->value) ? array_values($this->value) : array();
             if (!$record->ID) {
                 $record->write();
                 // record needs to have an ID in order to set relationships
                 $relation = $fieldname && $record && $record->hasMethod($fieldname) ? $record->{$fieldname}() : null;
             }
             $newIdList = array();
             // Tags will be a list of comma separated tags by title
             $class = $relation->dataClass();
             $filterField = 'Title';
             $newList = $class::get()->filter($filterField, $idList);
             $newListMap = $newList->map($filterField, 'ID');
             // Tag will either already exist or need to be created
             foreach ($idList as $id) {
                 if (isset($newListMap[$id])) {
                     $newIdList[] = $newListMap[$id];
                 } else {
                     $obj = new $class();
                     $obj->Title = trim(str_replace(self::SEPARATOR, '', $id));
                     $obj->write();
                     $newIdList[] = $obj->ID;
                 }
             }
             $relation->setByIDList($newIdList);
         } elseif ($fieldname && $record) {
             if ($this->value) {
                 if (is_array($this->value)) {
                     $this->value = implode(self::SEPARATOR, $this->value);
                 }
                 $record->{$fieldname} = $this->value;
             } else {
                 $record->{$fieldname} = null;
             }
         }
     } else {
         return parent::saveInto($record);
     }
 }