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); }
/** * SaveInto checks if set-methods are available and use them instead of setting the values directly. saveInto * initiates a new LinkField class object to pass through the values to the setter method. */ function saveInto(DataObjectInterface $dataObject) { $fieldName = $this->name; if ($dataObject->hasMethod("set{$fieldName}")) { $dataObject->{$fieldName} = DBField::create('AlternativeField', array("SelectedValue" => $this->fieldSelectedValue->Value(), "AlternativeValue" => $this->fieldAlternativeValue->Value())); } else { $dataObject->{$fieldName}->setSelectedValue($this->fieldSelectedValue->Value()); $dataObject->{$fieldName}->setAlternativeValue($this->fieldAlternativeValue->Value()); } }
/** * SaveInto checks if set-methods are available and use them instead of setting the values directly. saveInto * initiates a new LinkField class object to pass through the values to the setter method. */ public function saveInto(DataObjectInterface $dataObject) { $fieldName = $this->name; if ($dataObject->hasMethod("set{$fieldName}")) { $dataObject->{$fieldName} = DBField::create('LinkField', array("PageID" => $this->fieldPageID->Value(), "CustomURL" => $this->fieldCustomURL->Value())); } else { $dataObject->{$fieldName}->setPageID($this->fieldPageID->Value()); $dataObject->{$fieldName}->setCustomURL($this->fieldCustomURL->Value()); } }
public function saveInto(\DataObjectInterface $record) { $fieldname = $this->name; $relation = $fieldname && $record && $record->hasMethod($fieldname) ? $record->{$fieldname}() : null; $value = $this->dataValue(); if ($relation) { // TODO: Save to relation } else { if (is_array($value)) { $this->value = json_encode(array_values($value)); } } parent::saveInto($record); }
/** * 30/06/2009 - Enhancement: * SaveInto checks if set-methods are available and use them * instead of setting the values in the money class directly. saveInto * initiates a new Money class object to pass through the values to the setter * method. * */ function saveInto(DataObjectInterface $dataObject) { $fieldName = $this->name; if ($dataObject->hasMethod("set{$fieldName}")) { $dataObject->{$fieldName} = DBField::create_field('WTLink', array("Type" => $this->fieldType->Value(), "Internal" => $this->internalField->Value(), "External" => $this->externalField->Value(), "Email" => $this->emailField->Value(), "File" => $this->fileField->Value(), "TargetBlank" => $this->targetBlankField->Value())); } else { if (!empty($dataObject->{$fieldName})) { $dataObject->{$fieldName}->setType($this->fieldType->Value()); $dataObject->{$fieldName}->setInternal($this->internalField->Value()); $dataObject->{$fieldName}->setExternal($this->externalField->Value()); $dataObject->{$fieldName}->setEmail($this->emailField->Value()); $dataObject->{$fieldName}->setFile($this->fileField->Value()); $dataObject->{$fieldName}->setTargetBlank($this->targetBlankField->Value()); } } }
/** * Save the contents of this form into the given data object. * It will make use of setCastedField() to do this. * * @param $dataObject The object to save data into * @param $fieldList An optional list of fields to process. This can be useful when you have a * form that has some fields that save to one object, and some that save to another. */ function saveInto(DataObjectInterface $dataObject, $fieldList = null) { $dataFields = $this->fields->saveableFields(); $lastField = null; $config = HTMLPurifier_Config::createDefault(); // Remove any CSS or inline styles $config->set('CSS.AllowedProperties', array()); $purifier = new HTMLPurifier($config); if ($dataFields) { foreach ($dataFields as $field) { // Skip fields that have been excluded if ($fieldList && is_array($fieldList) && !in_array($field->Name(), $fieldList)) { continue; } $saveMethod = "save{$field->getName()}"; //purify $value = $field->dataValue(); $class = get_class($field); if (is_string($value) && ($class == 'TextField' || $class == "TextareaField")) { $field->setValue($purifier->purify($value)); } if ($field->getName() == "ClassName") { $lastField = $field; } else { if ($dataObject->hasMethod($saveMethod)) { $dataObject->{$saveMethod}($field->dataValue()); } else { if ($field->getName() != "ID") { $field->saveInto($dataObject); } } } } } if ($lastField) { $lastField->saveInto($dataObject); } }
/** * 30/06/2009 - Enhancement: * SaveInto checks if set-methods are available and use them * instead of setting the values in the money class directly. saveInto * initiates a new Money class object to pass through the values to the setter * method. * * (see @link MoneyFieldTest_CustomSetter_Object for more information) */ public function saveInto(DataObjectInterface $dataObject) { $fieldName = $this->name; if ($dataObject->hasMethod("set{$fieldName}")) { $dataObject->{$fieldName} = DBField::create_field('Money', array("Currency" => $this->fieldCurrency->dataValue(), "Amount" => $this->fieldAmount->dataValue())); } else { $dataObject->{$fieldName}->setCurrency($this->fieldCurrency->dataValue()); $dataObject->{$fieldName}->setAmount($this->fieldAmount->dataValue()); } }
/** * 30/06/2009 - Enhancement: * SaveInto checks if set-methods are available and use them * instead of setting the values in the money class directly. saveInto * initiates a new Money class object to pass through the values to the setter * method. * * (see @link MoneyFieldTest_CustomSetter_Object for more information) */ public function saveInto(DataObjectInterface $dataObject) { $fieldName = $this->name; if ($dataObject->hasMethod("set{$fieldName}")) { $dataObject->{$fieldName} = DBField::create_field('PostCodeLocation', array("Postcode" => $this->fieldPostcode->Value(), "Country" => $this->fieldCountry->Value(), "Latitude" => $this->fieldLatitude->Value(), "Longditude" => $this->fieldLongditude->Value())); } else { $dataObject->{$fieldName}->setPostcode($this->fieldPostcode->Value()); $dataObject->{$fieldName}->setCountry($this->fieldCountry->Value()); $dataObject->{$fieldName}->setLatitude($this->fieldLatitude->Value()); $dataObject->{$fieldName}->setLongditude($this->fieldLongditude->Value()); } }
/** * Save the contents of this form into the given data object. * It will make use of setCastedField() to do this. * * @param $dataObject The object to save data into * @param $fieldList An optional list of fields to process. This can be useful when you have a * form that has some fields that save to one object, and some that save to another. */ public function saveInto(DataObjectInterface $dataObject, $fieldList = null) { $dataFields = $this->fields->saveableFields(); $lastField = null; if ($dataFields) { foreach ($dataFields as $field) { // Skip fields that have been exlcuded if ($fieldList && is_array($fieldList) && !in_array($field->getName(), $fieldList)) { continue; } $saveMethod = "save{$field->getName()}"; if ($field->getName() == "ClassName") { $lastField = $field; } else { if ($dataObject->hasMethod($saveMethod)) { $dataObject->{$saveMethod}($field->dataValue()); } else { if ($field->getName() != "ID") { $field->saveInto($dataObject); } } } } } if ($lastField) { $lastField->saveInto($dataObject); } }
/** * Save the contents of this form into the given data object. * It will make use of setCastedField() to do this. */ function saveInto(DataObjectInterface $dataObject) { $dataFields = $this->fields->dataFields(); $lastField = null; if ($dataFields) { foreach ($dataFields as $field) { $saveMethod = "save{$field->Name()}"; if ($field->Name() == "ClassName") { $lastField = $field; } else { if ($dataObject->hasMethod($saveMethod)) { $dataObject->{$saveMethod}($field->dataValue()); } else { if ($field->Name() != "ID") { $field->saveInto($dataObject); } } } } } if ($lastField) { $lastField->saveInto($dataObject); } }
/** * Save the results into the form * Calls function $record->onChange($items) before saving to the assummed * Component set. */ public function saveInto(DataObjectInterface $record) { // Detect whether this field has actually been updated if ($this->value !== 'unchanged') { $items = array(); $fieldName = $this->name; $saveDest = $record->{$fieldName}(); if (!$saveDest) { user_error("TreeMultiselectField::saveInto() Field '{$fieldName}' not found on" . " {$record->class}.{$record->ID}", E_USER_ERROR); } 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; } } $saveDest->setByIDList($items); } }
/** * 30/06/2009 - Enhancement: * SaveInto checks if set-methods are available and use them * instead of setting the values in the money class directly. saveInto * initiates a new Money class object to pass through the values to the setter * method. * * (see @link MoneyFieldTest_CustomSetter_Object for more information) */ public function saveInto(DataObjectInterface $dataObject) { $fieldName = $this->getName(); if ($dataObject->hasMethod("set{$fieldName}")) { $dataObject->{$fieldName} = DBField::create_field('Money', array("Currency" => $this->fieldCurrency->dataValue(), "Amount" => $this->fieldAmount->dataValue())); } else { $currencyField = "{$fieldName}Currency"; $amountField = "{$fieldName}Amount"; $dataObject->{$currencyField} = $this->fieldCurrency->dataValue(); $dataObject->{$amountField} = $this->fieldAmount->dataValue(); } }
/** * Save the current value of this MultiSelectField 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) { $fieldName = $this->getName(); if (empty($fieldName) || empty($record)) { return; } $relation = $record->hasMethod($fieldName) ? $record->{$fieldName}() : null; // Detect DB relation or field $items = $this->getValueArray(); if ($relation instanceof Relation) { // Save ids into relation $relation->setByIDList($items); } elseif ($record->hasField($fieldName)) { // Save dataValue into field $record->{$fieldName} = $this->stringEncode($items); } }
/** * 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 DataObjectInterface $record The record to save into */ public function saveInto(DataObjectInterface $record) { $fieldname = $this->name; $relation = $fieldname && $record && $record->hasMethod($fieldname) ? $record->{$fieldname}() : null; if ($fieldname && $record && $relation && ($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) { // save the correct values $idList = array(); if ($this->value) { $idList = is_array($this->value) ? array_values($this->value) : explode(',', $this->value); } $relation->setByIDList($idList); // put them in the right order // This may be drastic overkill, this bit is pulled form // GridFieldOrderable. I suspect we could simplify here? $ids = $idList; $items = $list = $relation; $field = $this->getSortField(); // $items = $list->byIDs($ids)->sort($field); // // // Ensure that each provided ID corresponded to an actual object. // if(count($items) != count($ids)) { // $this->httpError(404); // } // Populate each object we are sorting with a sort value. $this->populateSortValues($items); // Generate the current sort values. $current = $items->map('ID', $field)->toArray(); // Perform the actual re-ordering. $this->reorderItems($list, $current, $ids); } elseif ($fieldname && $record) { $record->{$fieldname} = $this->getStringValue(); } }
public function saveInto(\DataObjectInterface $record) { if ($this->depth == 1) { // Reset records to write for top-level MultiRecordField. self::$_new_records_to_write = array(); self::$_existing_records_to_write = array(); self::$_records_to_delete = array(); } $class_id_field = $this->Value(); if (!$class_id_field) { return $this; } $list = $this->list; // Workaround for #5775 - Fix bug where ListboxField writes to $record, making // UnsavedRelationList redundant. // https://github.com/silverstripe/silverstripe-framework/pull/5775 $relationName = $this->getName(); $relation = $record->hasMethod($relationName) ? $record->{$relationName}() : null; if ($relation) { // When ListboxField (or other) has saved a new record in its 'saveInto' function if ($record->ID && $list instanceof UnsavedRelationList) { if ($this->config()->enable_patch_5775 === false) { throw new Exception("ListboxField or another FormField called DataObject::write() when it wasn't meant to on your unsaved record. https://github.com/silverstripe/silverstripe-framework/pull/5775 ---- Enable 'enable_patch_5775' in your config YML against " . __CLASS__ . " to enable a workaround."); } if ($relation instanceof ElementalArea) { // Hack to support Elemental $relation = $relation->Elements(); } else { if ($relation instanceof DataObject) { throw new Exception("Unable to use enable_patch_5775 workaround as \"" . $record->class . "\"::\"" . $relationName . "\"() does not return a DataList."); } } $list = $relation; } } $flatList = array(); if ($list instanceof DataList) { $flatList = array(); foreach ($list as $r) { $flatList[$r->ID] = $r; } } else { if (!$list instanceof UnsavedRelationList) { throw new Exception('Expected SS_List, but got "' . $list->class . '" in ' . __CLASS__); } } $sortFieldName = $this->getSortFieldName(); foreach ($class_id_field as $class => $id_field) { // Create and add records to list foreach ($id_field as $idString => $subRecordData) { if (strpos($idString, 'o-multirecordediting') !== FALSE) { throw new Exception('Invalid template ID passed in ("' . $idString . '"). This should have been replaced by MultiRecordField.js. Is your JavaScript broken?'); } $idParts = explode('_', $idString); $id = 0; $subRecord = null; if ($idParts[0] === 'new') { if (!isset($idParts[1])) { throw new Exception('Missing ID part of "new_" identifier.'); } $id = (int) $idParts[1]; if (!$id && $id > 0) { throw new Exception('Invalid ID part of "new_" identifier. Positive Non-Zero Integers only are accepted.'); } // New record $subRecord = $class::create(); } else { $id = $idParts[0]; // Find existing $id = (int) $id; if (!isset($flatList[$id])) { throw new Exception('Record #' . $id . ' on "' . $class . '" does not exist in this DataList context. (From ID string: ' . $idString . ')'); } $subRecord = $flatList[$id]; } // Detect if record was deleted if (isset($subRecordData['multirecordfield_delete']) && $subRecordData['multirecordfield_delete']) { if ($subRecord && $subRecord->exists()) { self::$_records_to_delete[] = $subRecord; } continue; } // maybetodo(Jake): To improve performance, maybe add 'dumb fields' config where it just gets the fields available // on an unsaved record and just re-uses them for each instance. Of course // this means conditional fields based on parent values/db values wont work. $fields = $this->getRecordDataFields($subRecord); $fields = $fields->dataFields(); if (!$fields) { throw new Exception($class . ' is returning 0 fields.'); } // foreach ($subRecordData as $fieldName => $fieldData) { if ($sortFieldName !== $fieldName && !isset($fields[$fieldName]) && strpos($fieldName, '_ClassName') == false) { // todo(Jake): Say whether its missing the field from getCMSFields or getMultiRecordFields or etc. throw new Exception('Missing field "' . $fieldName . '" from "' . $subRecord->class . '" fields based on data sent from client. (Could be a hack attempt)'); } if (isset($fields[$fieldName])) { $field = $fields[$fieldName]; if (!$field instanceof MultiRecordField) { $value = $fieldData->value; } else { $value = $fieldData; } if ($field) { // NOTE(Jake): Added for FileAttachmentField as it uses the name used in the request for // file deletion. $field->MultiRecordEditing_Name = $this->getUniqueFieldName($field->getName(), $subRecord); $field->setValue($value); // todo(Jake): Some field types (ie. UploadField/FileAttachmentField) directly modify the record // on 'saveInto', meaning people -could- circumvent certain permission checks // potentially. Must test this or defer extensions of 'FileField' to 'saveInto' later. $field->saveInto($subRecord); $field->MultiRecordField_SavedInto = true; } } } // Handle sort if its not manually handled on the form if ($sortFieldName && !isset($fields[$sortFieldName])) { $newSortValue = $id; // Default to order added if (isset($subRecordData[$sortFieldName])) { $newSortValue = $subRecordData[$sortFieldName]; } if ($newSortValue) { $subRecord->{$sortFieldName} = $newSortValue; } } // Check if sort value is invalid $sortValue = $subRecord->{$sortFieldName}; if ($sortValue <= 0) { throw new Exception('Invalid sort value (' . $sortValue . ') on #' . $subRecord->ID . ' for class ' . $subRecord->class . '. Sort value must be greater than 0.'); } if (!$subRecord->doValidate()) { throw new ValidationException('Failed validation on ' . $subRecord->class . '::doValidate() on record #' . $subRecord->ID); } if ($subRecord->exists()) { self::$_existing_records_to_write[] = $subRecord; } else { // NOTE(Jake): I used to directly add the record to the list here, but // if it's a HasManyList/ManyManyList, it will create the record // before doing permission checks. self::$_new_records_to_write[] = array(self::NEW_RECORD => $subRecord, self::NEW_LIST => $list); } } } // The top-most MutliRecordField handles all the permission checking/saving at once if ($this->depth == 1) { // Remove records from list that haven't been changed to avoid unnecessary // permission check and ->write overhead foreach (self::$_existing_records_to_write as $i => $subRecord) { $hasRecordChanged = false; $changedFields = $subRecord->getChangedFields(true); foreach ($changedFields as $field => $data) { $hasRecordChanged = $hasRecordChanged || $data['before'] != $data['after']; } if (!$hasRecordChanged) { // Remove from list, stops the record from calling ->write() unset(self::$_existing_records_to_write[$i]); } } // // Check permissions on everything at once // (includes records added in nested-nested-nested-etc MultiRecordField's) // $currentMember = Member::currentUser(); $recordsPermissionUnable = array(); foreach (self::$_new_records_to_write as $subRecordAndList) { $subRecord = $subRecordAndList[self::NEW_RECORD]; // Check each new record to see if you can create them if (!$subRecord->canCreate($currentMember)) { $recordsPermissionUnable['canCreate'][$subRecord->class][$subRecord->ID] = true; } } foreach (self::$_existing_records_to_write as $subRecord) { // Check each existing record to see if you can edit them if (!$subRecord->canEdit($currentMember)) { $recordsPermissionUnable['canEdit'][$subRecord->class][$subRecord->ID] = true; } } foreach (self::$_records_to_delete as $subRecord) { // Check each record deleting to see if you can delete them if (!$subRecord->canDelete($currentMember)) { $recordsPermissionUnable['canDelete'][$subRecord->class][$subRecord->ID] = true; } } if ($recordsPermissionUnable) { /** * Output a nice exception/error message telling you exactly what records/classes * the permissions failed on. * * eg. * Current member #7 does not have permission. * * Unable to "canCreate" records: * - ElementGallery (26) * * Unable to "canEdit" records: * - ElementGallery (24,23,22) * - ElementGallery_Item (16,23,17,18,19,20,22,21) */ $message = ''; foreach ($recordsPermissionUnable as $permissionFunction => $classAndID) { $message .= "\n" . 'Unable to "' . $permissionFunction . '" records: ' . "\n"; foreach ($classAndID as $class => $idAsKeys) { $message .= '- ' . $class . ' (' . implode(',', array_keys($idAsKeys)) . ')' . "\n"; } } throw new Exception('Current member #' . Member::currentUserID() . ' does not have permission.' . "\n" . $message); } // Add new records into the appropriate list foreach (self::$_new_records_to_write as $subRecordAndList) { $list = $subRecordAndList[self::NEW_LIST]; if ($list instanceof UnsavedRelationList || $list instanceof RelationList) { $subRecord = $subRecordAndList[self::NEW_RECORD]; // NOTE(Jake): Adding an empty record into an existing ManyManyList/HasManyList -seems- to create that record. $list->add($subRecord); } else { throw new Exception('Unsupported SS_List type "' . $list->class . '"'); } } // Debugging (for looking at UnsavedRelationList's to ensure $_new_records_to_write is working) // NOTE(Jake): Added to debug Frontend Objects module support //Debug::dump($record); Debug::dump($relation_class_id_field); exit('Exited at: '.__CLASS__.'::'.__FUNCTION__);// Debug raw request information tree // Save existing items foreach (self::$_existing_records_to_write as $subRecord) { // NOTE(Jake): Records are checked above to see if they've been changed. // If they haven't been changed, they're removed from the 'self::$_existing_records_to_write' list. $subRecord->write(); } // Remove deleted items foreach (self::$_records_to_delete as $subRecord) { $subRecord->delete(); } } }
/** * 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 */ public function saveInto(DataObjectInterface $record) { $fieldname = $this->name; $relation = $fieldname && $record && $record->hasMethod($fieldname) ? $record->{$fieldname}() : null; if ($fieldname && $record && $relation && ($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) { $idList = array(); if ($this->value) { foreach ($this->value as $id => $bool) { if ($bool) { $idList[] = $id; } } } $relation->setByIDList($idList); } elseif ($fieldname && $record) { if ($this->value) { $this->value = str_replace(',', '{comma}', $this->value); $record->{$fieldname} = implode(',', (array) $this->value); } 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); } } }
/** * Returns all fields on the object which should be shown * in the output. Can be customised through {@link self::setCustomFields()}. * * @todo Allow for custom getters on the processed object (currently filtered through inheritedDatabaseFields) * @todo Field level permission checks * * @param DataObjectInterface|DataObject $obj * @return array */ protected function getFieldsForObj($obj) { $dbFields = array(); // if custom fields are specified, only select these if (is_array($this->customFields)) { foreach ($this->customFields as $fieldName) { // @todo Possible security risk by making methods accessible - implement field-level security if ($obj->hasField($fieldName) || $obj->hasMethod("get{$fieldName}")) { $dbFields[$fieldName] = $fieldName; } } } else { // by default, all database fields are selected $dbFields = $obj->db(); } if (is_array($this->customAddFields)) { foreach ($this->customAddFields as $fieldName) { // @todo Possible security risk by making methods accessible - implement field-level security if ($obj->hasField($fieldName) || $obj->hasMethod("get{$fieldName}")) { $dbFields[$fieldName] = $fieldName; } } } // add default required fields $dbFields = array_merge($dbFields, array('ID' => 'Int')); if (is_array($this->removeFields)) { $dbFields = array_diff_key($dbFields, array_combine($this->removeFields, $this->removeFields)); } return $dbFields; }
/** * 30/06/2009 - Enhancement: * SaveInto checks if set-methods are available and use them * instead of setting the values in the money class directly. saveInto * initiates a new Money class object to pass through the values to the setter * method. * * (see @link MoneyFieldTest_CustomSetter_Object for more information) */ function saveInto(DataObjectInterface $dataObject) { $fieldName = $this->name; if ($dataObject->hasMethod("set{$fieldName}")) { $dataObject->{$fieldName} = DBField::create('GeoLocation', array("Address" => $this->fieldAddress->Value(), "Latitude" => $this->fieldLatitude->Value(), "Longditude" => $this->fieldLongditude->Value())); } else { $dataObject->{$fieldName}->setAddress($this->fieldAddress->Value()); $dataObject->{$fieldName}->setLatitude($this->fieldLatitude->Value()); $dataObject->{$fieldName}->setLongditude($this->fieldLongditude->Value()); } }
public function saveInto(DataObjectInterface $record) { // Check required relation details are available $fieldname = $this->getName(); if (!$fieldname) { return $this; } // Get details to save $idList = $this->getItemIDs(); // Check type of relation $relation = $record->hasMethod($fieldname) ? $record->{$fieldname}() : null; if ($relation && ($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) { // has_many or many_many $relation->setByIDList($idList); } elseif ($record->has_one($fieldname)) { // has_one $record->{"{$fieldname}ID"} = $idList ? reset($idList) : 0; } return $this; }
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); } }
/** * 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); } }
/** * Save the current value 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) { $fieldname = $this->name; $relation = $fieldname && $record && $record->hasMethod($fieldname) ? $record->{$fieldname}() : null; if ($fieldname && $record && $relation && ($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) { $idList = array(); if ($this->value) { foreach ($this->value as $id => $text) { if (preg_match('/^__new__/', $id)) { $id = $this->source->newObject(array($this->labelField => $text))->write(); } $idList[] = $id; } } $relation->setByIDList($idList); } elseif ($fieldname && $record) { if ($this->value) { $this->value = str_replace(',', '{comma}', $this->value); $record->{$fieldname} = implode(',', (array) $this->value); } else { $record->{$fieldname} = ''; } } }
public function saveInto(DataObjectInterface $record) { if ($record->hasMethod("set{$this->name}")) { $values = []; foreach ($this->children as $point => $field) { $values[$point] = $field[$point]->Value(); } $record->{$this->name} = DBField::create_field('Milkyway\\SS\\FocusArea\\DBField', $values); } else { foreach ($this->children as $point => $field) { $record->{$this->name}->{$point} = $field->Value(); } } }
/** * SaveInto checks if set-methods are available and use them instead of setting the values directly. saveInto * initiates a new LinkField class object to pass through the values to the setter method. */ public function saveInto(DataObjectInterface $dataObject) { $fieldName = $this->name; if ($dataObject->hasMethod("set{$fieldName}")) { $dataObject->{$fieldName} = DBField::create('NamedLinkField', array("PageID" => $this->fieldPageID->Value(), "CustomURL" => $this->fieldCustomURL->Value(), "Title" => $this->fieldTitle->Value(), "Linkmode" => $this->fieldLinkmode->Value())); } else { if (!is_object($dataObject->{$fieldName})) { $dataObject->{$fieldName} = NamedLinkField::create(); } $dataObject->{$fieldName}->setPageID($this->fieldPageID->Value()); $dataObject->{$fieldName}->setCustomURL($this->fieldCustomURL->Value()); $dataObject->{$fieldName}->setTitle($this->fieldTitle->Value()); $dataObject->{$fieldName}->setLinkmode($this->fieldLinkmode->Value()); } }