Source of file HasOneCompositeField.php
Size: 3,878 Bytes - Last Modified: 2021-12-23T10:05:22+00:00
/var/www/docs.ssmods.com/process/src/code/HasOneCompositeField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 | <?php /** * Milkyway Multimedia * HasOneCompositeField.php * * A compositefield that saves the containing fields * into a has_one relationship * * @todo No deletion of object supported... * @todo Saving has_many and many_many not tested... * * @package milkyway/silverstripe-hasonecompositefield * @author Mellisa Hankins <mell@milkywaymultimedia.com.au> */ class HasOneCompositeField extends CompositeField { /** * @var DataObjectInterface */ protected $record; /** * @var array */ protected $extraData = array(); /** * @var array */ protected $defaultFromParent = array(); /** * @var array */ protected $overrideFromParent = array(); public function __construct($name, $record, FieldList $fields = null) { $this->name = $name; $this->record = $record; if(!$fields) { if($this->record->hasMethod('getHasOneCMSFields')) $fields = $this->record->getHasOneCMSFields($name); else $fields = $this->record->getCMSFields(); if($fields) $fields = $fields->dataFields(); } parent::__construct($fields); } public function setExtraData($data = array()) { $this->extraData = $data; return $this; } public function getExtraData() { return $this->extraData; } public function setDefaultFromParent($data = array()) { $this->defaultFromParent = $data; return $this; } public function getDefaultFromParent() { return $this->defaultFromParent; } public function setOverrideFromParent($data = array()) { $this->overrideFromParent = $data; return $this; } public function getOverrideFromParent() { return $this->overrideFromParent; } public function hasData() { return true; } public function isComposite() { return false; } 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); } } public function FieldList($prependName = true) { $fields = parent::FieldList(); if($fields && $fields->exists()) { if($this->value && (is_array($this->value) || ($this->value instanceof DataObjectInterface))) $value = $this->value; else $value = $this->record; if($value) { // HACK: Use a fake Form object to save data into fields $form = new Form($this->record, $this->name . '-form', $fields, new FieldList()); $form->loadDataFrom($value); $fields->setForm($this->form); unset($form); } if($prependName) $this->prependName($fields); } return $fields; } protected function prependName(FieldList $fields) { foreach($fields as $field){ if($field->isComposite()) $this->prependName($field->FieldList()); $field->setName($this->name . '[' . $field->Name . ']'); } } } |