コード例 #1
0
 /**
  * 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();
     }
 }
 /**
  * Called when a grid field is saved, converts the StatefulGridFieldList to a RelationList
  * @param {GridField} $field
  * @param {DataObjectInterface} $record
  */
 public function handleSave(GridField $grid, DataObjectInterface $record)
 {
     $list = $grid->getList();
     if ($list instanceof StatefulGridFieldList) {
         $relationName = $list->getRelationName();
         if ($record->has_many($relationName)) {
             $list->changeToList($record->getComponents($list->getRelationName()));
         } else {
             if ($record->many_many($relationName)) {
                 $list->changeToList($record->getManyManyComponents($list->getRelationName()));
             } else {
                 throw new InvalidArgumentException('Record does not have a has_many or many_many relationship called "' . $relationName . '"', null, null);
             }
         }
     }
 }
コード例 #3
0
 /**
  * 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();
                 }
             }
         }
     }
 }
コード例 #4
0
 /**
  * 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();
                 }
             }
         }
     }
 }
 /**
  * Save the current value of this CheckboxSetField 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
  */
 function saveInto(DataObjectInterface $record)
 {
     $fieldname = $this->name;
     //check if dataobject has a field name the same as this->name
     if ($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
         $idList = array();
         if ($this->value) {
             foreach ($this->value as $id => $bool) {
                 if ($bool) {
                     $idList[] = $id;
                 }
                 //TODO: include support for setting $record->has_many($this->childsorce) and many_many($this->childsource) values
             }
         }
         $record->{$fieldname}()->setByIDList($idList);
     } elseif ($fieldname && $record) {
         if ($this->value) {
             $record->{$fieldname} = $this->dataValue();
         } else {
             $record->{$fieldname} = '';
         }
     }
 }
 /**
  *
  * TO DO: explain how this works or what it does.
  */
 public function saveInto(DataObjectInterface $record)
 {
     if ($this->value !== 'unchanged') {
         $items = array();
         $fieldName = $this->name;
         if ($this->value) {
             $items = preg_split("/ *, */", trim($this->value));
         }
         // Allows you to modify the items on your object before save
         $funcName = "onChange{$fieldName}";
         if ($record->hasMethod($funcName)) {
             $result = $record->{$funcName}($items);
             if (!$result) {
                 return;
             }
         }
         if ($fieldName && ($record->has_many($fieldName) || $record->many_many($fieldName))) {
             // Set related records
             $record->{$fieldName}()->setByIDList($items);
         } else {
             $record->{$fieldName} = implode(',', $items);
         }
     }
 }
 function saveInto(DataObjectInterface $record)
 {
     $fieldname = $this->name;
     $this->value['Email']['Value'] = 'Email';
     $this->value['Email']['Required'] = 1;
     $value = ArrayLib::invert($this->value);
     if ($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
         $idList = array();
         if ($value) {
             foreach ($value['Value'] as $id => $bool) {
                 if ($bool) {
                     $idList[] = $id;
                 }
             }
         }
         $record->{$fieldname}()->setByIDList($idList);
     } elseif ($fieldname && $record) {
         if ($value) {
             if (is_array($value)) {
                 foreach ($value as $k => $items) {
                     if (is_array($items)) {
                         foreach ($items as $key => $val) {
                             if (!$val) {
                                 unset($value[$k][$key]);
                             } else {
                                 if ($k == 'Value') {
                                     $value[$k][$key] = str_replace(", ", "{comma}", $val);
                                 }
                             }
                         }
                     }
                 }
             }
             foreach ($value as $k => $v) {
                 if ($k == 'Value') {
                     $record->{$fieldname} = implode(",", $v);
                 } else {
                     $record->{$k} = Convert::array2json($v);
                 }
             }
         } else {
             $record->{$fieldname} = '';
         }
     }
 }