Source of file MultiValueField.php
Size: 4,883 Bytes - Last Modified: 2021-12-23T10:33:21+00:00
/var/www/docs.ssmods.com/process/src/src/ORM/FieldType/MultiValueField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 | <?php namespace Symbiote\MultiValueField\ORM\FieldType; use SilverStripe\ORM\FieldType\DBComposite; use SilverStripe\ORM\FieldType\DBVarchar; use SilverStripe\ORM\ArrayList; use SilverStripe\View\ArrayData; /** * A DB field that serialises an array before writing it to the db, and returning the array * back to the end user. * * @author Marcus Nyeholt <marcus@symbiote.com.au> */ class MultiValueField extends DBComposite { /** * @param array */ private static $composite_db = array( "Value" => "Text", ); /** * * @var boolean */ protected $changed = false; /** * Returns the value of this field. * @return mixed */ public function getValue() { $value = $this->value; if (is_null($value)) { $value = $this->getField('Value'); } $this->value = is_string($value) ? $this->unserializeData($value) : $value; return $this->value; } public function getValues() { return $this->getValue(); } /** * Set the value on the field. Ensures the underlying composite field * logic that looks for Value will trigger if the value set is * * * For a multivalue field, this will deserialise the value if it is a string * * @param mixed $value * @param array $record * @return $this */ public function setValue($value, $record = null, $markChanged = true) { $this->changed = $this->changed || $markChanged; if (!is_null($value)) { // so that subsequent getValue calls re-load the value item correctly $this->value = null; if (!is_string($value)) { $value = $this->serializeValue($value); } $value = ['Value' => $value]; } return parent::setValue($value, $record, $markChanged); } /** * Serializes a value object to a json string * * @param array|object $value * @return string */ protected function serializeValue($value) { if (is_string($value)) { return $value; } if (is_object($value) || is_array($value)) { return json_encode($value); } } /** * Unserialises data, depending on new or old format * * @param string $data * * @return array */ protected function unserializeData($data) { $value = null; // if we're not deserialised yet, do so if (is_string($data) && strlen($data) > 1) { // are we json encoded? if ($data[1] === ':') { $value = \unserialize($data); } else { $value = \json_decode($data, true); } } return $value; } /** * (non-PHPdoc). * * @see core/model/fieldtypes/DBField#prepValueForDB($value) */ public function prepValueForDB($value) { if ($value instanceof MultiValueField) { $value = $value->getValue(); } if (is_object($value) || is_array($value)) { $value = json_encode($value); } return parent::prepValueForDB($value); } public function isChanged() { return $this->changed; } public function scaffoldFormField($title = null, $params = null) { return new \Symbiote\MultiValueField\Fields\MultiValueTextField($this->name, $title); } /** * Convert to a textual list of items. */ public function csv() { return $this->Implode(','); } /** * Return all items separated by a separator, defaulting to a comma and * space. * * @param string $separator * * @return string */ public function Implode($separator = ', ') { return implode($separator, $this->getValue()); } public function __toString() { if ($this->getValue()) { return $this->csv(); } return ''; } public function ItemByKey() { $values = $this->getValue(); if (array_keys($values) == range(0, count($values) - 1)) { $values = []; } return new ArrayData($values); } public function Items() { return $this->forTemplate(); } public function forTemplate() { $items = []; $value = $this->getValue(); if ($value) { foreach ($value as $key => $item) { $v = new DBVarchar('Value'); $v->setValue($item); $obj = new ArrayData([ 'Value' => $v, 'Key' => $key, 'Title' => $item, ]); $items[] = $obj; } } return new ArrayList($items); } } |