Source of file LinkField.php
Size: 7,632 Bytes - Last Modified: 2021-12-23T10:02:30+00:00
/var/www/docs.ssmods.com/process/src/code/LinkField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 | <?php /** * A link field which will store a link in the database. * * @author Simon Elvery * @package silverstripe-link-field */ class LinkField extends DBField implements CompositeDBField { /** * @var int The PageID for this link. */ protected $page_id; /** * @var string A custom URL for this link */ protected $custom_url; /** * @var boolean Is this record changed or not? */ protected $isChanged = false; /** * Similiar to {@link DataObject::$db}, * holds an array of composite field names. * Don't include the fields "main name", * it will be prefixed in {@link requireField()}. * * @var array $composite_db */ private static $composite_db = array( 'PageID' => 'Int', 'CustomURL' => 'Varchar(2000)' ); /** * Set the value of this field in various formats. * Used by {@link DataObject->getField()}, {@link DataObject->setCastedField()} * {@link DataObject->dbObject()} and {@link DataObject->write()}. * * As this method is used both for initializing the field after construction, * and actually changing its values, it needs a {@link $markChanged} * parameter. * * @param DBField|array $value * @param array $record Map of values loaded from the database * @param boolean $markChanged Indicate wether this field should be marked changed. * Set to FALSE if you are initializing this field after construction, rather * than setting a new value. */ public function setValue($value, $record = null, $markChanged = true) { if ($value instanceof LinkField && $value->exists()) { $this->setPageID($value->getPageID(), $markChanged); $this->setCustomURL($value->getCustomURL(), $markChanged); } elseif ( $record && (isset($record[$this->name . 'PageID']) || isset($record[$this->name . 'CustomURL'])) ) { $this->setPageID( (isset($record[$this->name . 'PageID'])) ? $record[$this->name . 'PageID'] : null, $markChanged ); $this->setCustomURL( (isset($record[$this->name . 'CustomURL'])) ? $record[$this->name . 'CustomURL'] : null, $markChanged ); } elseif (is_array($value)) { if (array_key_exists('PageID', $value)) { $this->setPageID($value['PageID'], $markChanged); } if (array_key_exists('CustomURL', $value)) { $this->setCustomURL($value['CustomURL'], $markChanged); } } else { // user_error('Invalid value in LinkField->setValue()', E_USER_ERROR); } } /** * Used in constructing the database schema. * Add any custom properties defined in {@link $composite_db}. * Should make one or more calls to {@link DB::requireField()}. */ public function requireField() { $fields = $this->compositeDatabaseFields(); if ($fields) { foreach ($fields as $name => $type) { DB::requireField($this->tableName, $this->name.$name, $type); } } } /** * Add the custom internal values to an INSERT or UPDATE * request passed through the ORM with {@link DataObject->write()}. * Fields are added in $manipulation['fields']. Please ensure * these fields are escaped for database insertion, as no * further processing happens before running the query. * Use {@link DBField->prepValueForDB()}. * Ensure to write NULL or empty values as well to allow * unsetting a previously set field. Use {@link DBField->nullValue()} * for the appropriate type. * * @param array $manipulation */ public function writeToManipulation(&$manipulation) { if ($this->getPageID()) { $manipulation['fields'][$this->name.'PageID'] = $this->prepValueForDB((int)$this->getPageID()); } else { $manipulation['fields'][$this->name.'PageID'] = DBField::create_field('Int', $this->getPageID())->nullValue(); } if ($this->getCustomURL()) { $manipulation['fields'][$this->name.'CustomURL'] = $this->prepValueForDB($this->getCustomURL()); } else { $manipulation['fields'][$this->name.'CustomURL'] = DBField::create_field('Varchar', $this->getCustomURL())->nullValue(); } } /** * Add all columns which are defined through {@link requireField()} * and {@link $composite_db}, or any additional SQL that is required * to get to these columns. Will mostly just write to the {@link SQLQuery->select} * array. * * @param SQLQuery $query */ public function addToQuery(&$query) { parent::addToQuery($query); } /** * Return array in the format of {@link $composite_db}. * Used by {@link DataObject->hasOwnDatabaseField()}. * @return array */ public function compositeDatabaseFields() { return static::$composite_db; } /** * Determines if the field has been changed since its initialization. * Most likely relies on an internal flag thats changed when calling * {@link setValue()} or any other custom setters on the object. * * @return boolean */ public function isChanged() { return $this->isChanged; } /** * Determines if any of the properties in this field have a value, * meaning at least one of them is not NULL. * * @return boolean */ public function exists() { return (($this->page_id !== null && $this->page_id > 0) || $this->custom_url !== null); } public function getPageID() { return $this->page_id; } public function setPageID($page_id, $markChanged = true) { $this->isChanged = $markChanged; $this->page_id = $page_id; } public function getCustomURL() { return $this->custom_url; } public function setCustomURL($url, $markChanged = true) { $this->isChanged = $markChanged; $this->custom_url = $url; } /** * Returns a CompositeField instance used as a default * for form scaffolding. * * Used by {@link SearchContext}, {@link ModelAdmin}, {@link DataObject::scaffoldFormFields()} * * @param string $title Optional. Localized title of the generated instance * @return FormField */ public function scaffoldFormField($title = null) { $field = new LinkFormField($this->name); return $field; } public function Page() { if ($this->getPageID() && $page = DataObject::get_by_id('Page', $this->getPageID())) { return $page; } return null; } public function getURL() { return ($this->Page()) ? $this->Page()->Link() : Convert::raw2htmlatt($this->getCustomURL()); } public function __toString() { return (string) $this->getURL(); } public function forTemplate() { return $this->getURL(); } public function Absolute() { $relative = $this->getURL(); return (Director::is_site_url($relative) && Director::is_relative_url($relative)) ? Controller::join_links(Director::protocolAndHost(), $relative) : $relative; } } |