Source of file MultiValueDropdownField.php
Size: 3,700 Bytes - Last Modified: 2021-12-23T10:33:21+00:00
/var/www/docs.ssmods.com/process/src/src/Fields/MultiValueDropdownField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 | <?php namespace Symbiote\MultiValueField\Fields; use Symbiote\MultiValueField\ORM\FieldType\MultiValueField; use SilverStripe\CMS\Controllers\ContentController; use SilverStripe\Control\Controller; use SilverStripe\View\HTML; use SilverStripe\View\Requirements; use SilverStripe\Core\Convert; /** * A multivalued field that uses dropdown boxes for selecting the value for each * * @author Marcus Nyeholt <marcus@symbiote.com.au> */ class MultiValueDropdownField extends MultiValueTextField { protected $source; public function __construct($name, $title = null, $source = [], $value = null) { parent::__construct($name, ($title === null) ? $name : $title, $value); $this->source = $source; } /** * @return array */ public function getSource() { return $this->source; } /** * @param array $source * @return $this */ public function setSource(array $source) { $this->source = $source; return $this; } public function Field($properties = []) { if (Controller::curr() instanceof ContentController) { Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js'); } Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js'); Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css'); $name = $this->name.'[]'; $fields = []; if ($this->value) { foreach ($this->value as $i => $v) { if ($this->readonly) { $fieldAttr = [ 'class' => 'mventryfield mvdropdownReadonly '.($this->extraClass() ? $this->extraClass() : ''), 'id' => $this->id().MultiValueTextField::KEY_SEP.$i, 'name' => $name, 'tabindex' => $this->getAttribute('tabindex') ]; $fields[] = HTML::createTag('span', $fieldAttr, Convert::raw2xml($v)); } else { $fields[] = $this->createSelectList($i, $name, $this->source, $v); } } } else { $i = -1; } if (!$this->readonly) { $fields[] = $this->createSelectList($i + 1, $name, $this->source); } return '<ul id="'.$this->id().'" class="multivaluefieldlist '.$this->extraClass().'"><li>'.implode( '</li><li>', $fields ).'</li></ul>'; } public function Type() { return 'dropdown multivaluedropdown'; } protected function createSelectList($number, $name, $values, $selected = '') { $options = HTML::createTag( 'option', [ 'selected' => $selected == '' ? 'selected' : '', 'value' => '' ], '' ); foreach ($values as $index => $title) { $attrs = ['value' => $index]; if ($index == $selected) { $attrs['selected'] = 'selected'; } $options .= HTML::createTag('option', $attrs, Convert::raw2xml($title)); } $attrs = [ 'class' => 'mventryfield mvdropdown '.($this->extraClass() ? $this->extraClass() : ''), 'id' => $this->id().MultiValueTextField::KEY_SEP.$number, 'name' => $name, 'tabindex' => $this->getAttribute('tabindex') ]; if ($this->disabled) { $attrs['disabled'] = 'disabled'; } return HTML::createTag('select', $attrs, $options); } } |