public function saveInto(DataObjectInterface $record) { if ($record->hasField($this->name) && $record->escapeTypeForField($this->name) != 'xml') { throw new Exception('HtmlEditorField->saveInto(): This field should save into a HTMLText or HTMLVarchar field.'); } $htmlValue = Injector::inst()->create('HTMLValue', $this->value); // Sanitise if requested if ($this->config()->sanitise_server_side) { $santiser = Injector::inst()->create('HtmlEditorSanitiser', HtmlEditorConfig::get_active()); $santiser->sanitise($htmlValue); } // Resample images and add default attributes if ($images = $htmlValue->getElementsByTagName('img')) { foreach ($images as $img) { // strip any ?r=n data from the src attribute $img->setAttribute('src', preg_replace('/([^\\?]*)\\?r=[0-9]+$/i', '$1', $img->getAttribute('src'))); // Resample the images if the width & height have changed. if ($image = File::find(urldecode(Director::makeRelative($img->getAttribute('src'))))) { $width = (int) $img->getAttribute('width'); $height = (int) $img->getAttribute('height'); if ($width && $height && ($width != $image->getWidth() || $height != $image->getHeight())) { //Make sure that the resized image actually returns an image: $resized = $image->ResizedImage($width, $height); if ($resized) { $img->setAttribute('src', $resized->getRelativePath()); } } } // Add default empty title & alt attributes. if (!$img->getAttribute('alt')) { $img->setAttribute('alt', ''); } if (!$img->getAttribute('title')) { $img->setAttribute('title', ''); } // Use this extension point to manipulate images inserted using TinyMCE, e.g. add a CSS class, change default title // $image is the image, $img is the DOM model $this->extend('processImage', $image, $img); } } // optionally manipulate the HTML after a TinyMCE edit and prior to a save $this->extend('processHTML', $htmlValue); // Store into record $record->{$this->name} = $htmlValue->getContent(); }
public function saveInto(DataObjectInterface $record) { if ($record->hasField($this->name) && $record->escapeTypeForField($this->name) != 'xml') { throw new Exception('HtmlEditorField->saveInto(): This field should save into a HTMLText or HTMLVarchar field.'); } // Resample images $value = Image::regenerate_html_links($this->value); $htmlValue = Injector::inst()->create('HTMLValue', $value); // Sanitise if requested if ($this->config()->sanitise_server_side) { $santiser = Injector::inst()->create('HtmlEditorSanitiser', HtmlEditorConfig::get_active()); $santiser->sanitise($htmlValue); } // optionally manipulate the HTML after a TinyMCE edit and prior to a save $this->extend('processHTML', $htmlValue); // Store into record $record->{$this->name} = $htmlValue->getContent(); }
public function saveInto(DataObjectInterface $record) { if ($record->hasField($this->name) && $record->escapeTypeForField($this->name) != 'xml') { throw new Exception('HtmlEditorField->saveInto(): This field should save into a HTMLText or HTMLVarchar field.'); } $htmlValue = Injector::inst()->create('HTMLValue', $this->value); // Sanitise if requested if ($this->config()->sanitise_server_side) { $santiser = Injector::inst()->create('HtmlEditorSanitiser', HtmlEditorConfig::get_active()); $santiser->sanitise($htmlValue); } // Resample images and add default attributes if ($images = $htmlValue->getElementsByTagName('img')) { foreach ($images as $img) { // strip any ?r=n data from the src attribute $img->setAttribute('src', preg_replace('/([^\\?]*)\\?r=[0-9]+$/i', '$1', $img->getAttribute('src'))); // Resample the images if the width & height have changed. // TODO: look for -10x here? $filename = RetinaImage::removeFilenameAppender(urldecode(Director::makeRelative($img->getAttribute('src'))), '-10x'); $image = File::find($filename); // try to find it using the legacy way if (!$image) { $image = File::find(urldecode(Director::makeRelative($img->getAttribute('src')))); } if ($image) { $imagemap = $image->toMap(); $retinaimage = RetinaImage::create(); foreach ($imagemap as $key => $value) { $retinaimage->{$key} = $value; } $width = $img->getAttribute('width'); $height = $img->getAttribute('height'); if ($width && $height && ($width != $retinaimage->getWidth() || $height != $retinaimage->getHeight()) || !$img->hasAttribute('srcset') && RetinaImage::$forceretina) { //Make sure that the resized image actually returns an image: if (!is_numeric($width) || !is_numeric($height)) { $width = (int) ($retinaimage->getWidth() / 2); $height = (int) ($retinaimage->getHeight() / 2); } $resized = $retinaimage->ResizedImage($width, $height); $url = $resized->getRelativePath(); $onex10 = $retinaimage->insertFilenameAppender($url, '-10x'); $onex15 = $retinaimage->insertFilenameAppender($url, '-15x'); $onex20 = $retinaimage->insertFilenameAppender($url, '-20x'); if ($resized) { $img->setAttribute('src', $onex10); } // srcset=\"$onex10 1x, $onex15 1.5x, $onex20 2x\" $img->setAttribute('srcset', "{$onex10} 1x, {$onex15} 1.5x, {$onex20} 2x"); } } // Add default empty title & alt attributes. if (!$img->getAttribute('alt')) { $img->setAttribute('alt', ''); } if (!$img->getAttribute('title')) { $img->setAttribute('title', ''); } } } // Store into record $record->{$this->name} = $htmlValue->getContent(); }
/** * Saves the field into a record * @param DataObjectInterface $record * @return FileAttachmentField */ public function saveInto(DataObjectInterface $record) { $fieldname = $this->getName(); if (!$fieldname) { return $this; } // Handle deletions. This is a bit of a hack. A workaround for having a single form field // post two params. $deletions = Controller::curr()->getRequest()->postVar('__deletion__' . $this->getName()); if ($deletions) { foreach ($deletions as $id) { $this->deleteFileByID($id); } } if ($relation = $this->getRelation()) { $relation->setByIDList($this->Value()); } elseif ($record->has_one($fieldname)) { $record->{"{$fieldname}ID"} = $this->Value() ?: 0; } elseif ($record->hasField($fieldname)) { $record->{$fieldname} = is_array($this->Value()) ? implode(',', $this->Value()) : $this->Value(); } return $this; }
function saveInto(DataObjectInterface $record) { $relation = $this->getRelation(); if ($relation) { $submittedTags = explode($this->getDelimiter(), $this->value); $tagClass = $this->getTagClass(); $tagLabelField = $this->getTagLabelField(); $tagObjects = DataList::create($tagClass)->filter($tagLabelField, $submittedTags); if ($tagObjects->Count() < count($submittedTags)) { // filter out the tags that exist already $tagsAsKeys = array_flip($submittedTags); foreach ($tagObjects as $tag) { $label = $tag->{$tagLabelField}; unset($tagsAsKeys[$label]); } foreach ($tagsAsKeys as $label => $value) { $tagObject = new $tagClass(); $tagObject->{$tagLabelField} = $label; $tagObject->write(); $tagObjects->add($tagObject); } } $relationList = $this->form->record->{$this->name}(); $oldTags = $relationList->map('ID', $tagLabelField)->toArray(); $relationList->removeAll(); $relationList->addMany($tagObjects->toArray()); if ($this->deleteUnusedTags) { $deletedTags = array_diff($oldTags, $tagObjects->map('ID', $tagLabelField)->toArray()); if (count($deletedTags) > 0) { $relationTable = $relation[4]; foreach ($deletedTags as $id => $title) { $query = new SQLQuery(); $query->select = array('ID', $tagLabelField); $query->from = array($relationTable); $query->where = array("ID = " . $id); $count = $query->Count(); if ($count == 0) { DataObject::delete_by_id($tagClass, $id); } } } } } else { if ($record->hasField($this->name)) { $record->setCastedField($this->name, $this->dataValue()); } else { // @TODO: better error handling } } }
/** * 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); } }
/** * 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; }