Source of file Select2Field.php
Size: 6,228 Bytes - Last Modified: 2021-12-23T10:05:09+00:00
/var/www/docs.ssmods.com/process/src/src/Select2Field.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 | <?php class Select2Field extends TypeAheadField { public $valField = 'ID'; /** * @var boolean $hasEmptyDefault Show the first <option> element as * empty (not having a value), with an optional label defined through * {@link $emptyString}. By default, the <select> element will be * rendered with the first option from {@link $source} selected. */ public $hasEmptyDefault = false; /** * @var string $emptyString The title shown for an empty default selection, * e.g. "Select...". */ public $emptyString = ''; public $allowHTML = false; private $_fetched; public function __construct( $name, $title = null, $value = null, $sourceList = null, $sourceField = null, $refField = '', $valField = '' ) { // Swap around to make more compatible with default dropdown // if (is_array($value) || ($value instanceof ArrayAccess)) { // $currValue = $sourceList; // $sourceList = $value; // $value = $currValue; // } parent::__construct($name, $title, $value, $sourceList, $sourceField, $refField, $valField); } public function setHasEmptyDefault($flag = true) { $this->hasEmptyDefault = $flag; return $this; } public function getHasEmptyDefault() { return $this->hasEmptyDefault; } public function setEmptyString($str) { $this->setHasEmptyDefault(true); $this->emptyString = $str; return $this; } public function getEmptyString() { return $this->emptyString; } function getAttributes() { $this->extraClasses[] = 'has-chzn chzn-done no-chzn'; $vars = [ 'data-minimum-input-length' => $this->minSearchLength, 'data-require-selection' => $this->requireSelection, 'data-allow-html' => $this->allowHTML, ]; if ($this->hasEmptyDefault) { $placeholder = $this->emptyString ?: _t('Select2DropdownField.SELECT___', 'Select...'); } else { $placeholder = ' '; } if ($placeholder) { $vars['placeholder'] = $placeholder; $vars['data-placeholder'] = $placeholder; } $attrs = array_merge( parent::getAttributes(), $vars, [ 'type' => 'hidden', ] ); if(isset($attrs['data-local']) && !$this->allowHTML) { $this->_fetched = json_decode($attrs['data-local'], true); unset($attrs['data-local']); } if(isset($attrs['data-prefetch-url'])) { unset($attrs['data-prefetch-url']); } if (isset($attrs['data-suggest-url']) && is_string($attrs['data-suggest-url'])) { $attrs['data-suggest-url'] = str_replace(['q=%QUERY', 'q=%25QUERY'], '', $attrs['data-suggest-url']); } if(isset($attrs['data-suggest-url']) && $list = $this->ListToUse) { $count = 0; if(is_array($list)) { $count = count($list); } else if($list instanceof SS_List) { $count = $list->count(); } if($count && ($prefetch = $this->_fetched ?: json_decode($this->prefetch()->getBody(), true)) && $count == count($prefetch)) { unset($attrs['data-suggest-url']); unset($attrs['data-minimum-input-length']); } elseif(!$count) { unset($attrs['data-suggest-url']); } if($count && isset($prefetch)) { $this->_fetched = $prefetch; } } if(isset($attrs['multiple']) && $attrs['multiple'] && isset($attrs['name'])) { $attrs['name'] .= '[]'; } if(isset($attrs['data-local'])) { $value = $this->Value(); if($value instanceof DataList) { $value = $value->column($this->valField); } $attrs['data-local-value'] = is_array($value) ? json_encode($value) : $value; } return $attrs; } protected function includeJs() { Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js'); Requirements::javascript(SS_MWM_FORMFIELDS_DIR . '/thirdparty/js/select2.full.js'); Requirements::javascript(SS_MWM_FORMFIELDS_DIR . '/js/select2.init.js'); } protected function includeCss() { Requirements::css(SS_MWM_FORMFIELDS_DIR . '/thirdparty/css/select2.css'); Requirements::css(SS_MWM_FORMFIELDS_DIR . '/css/select2.field.css'); } public function Options() { if($this->allowHTML) { return ArrayList::create(); } $prefetch = $this->_fetched ?: json_decode($this->prefetch()->getBody(), true); $this->_fetched = $prefetch; if(empty($prefetch)) { return ArrayList::create(); } $value = $this->Value(); if($value instanceof DataList) { $value = $value->column($this->valField); } return ArrayList::create(array_map(function($item) use($value) { return $this->convertToOption($item, $value); }, $prefetch)); } protected function convertToOption($item, $value) { $data = ArrayData::create([ 'Value' => isset($item['id']) ? $item['id'] : '', 'Title' => isset($item['text']) ? $item['text'] : '', 'Disabled' => isset($item['disabled']) ? $item['disabled'] : false, 'Locked' => isset($item['locked']) ? $item['locked'] : false, 'Selected' => is_array($value) ? isset($item['id']) && in_array($item['id'], $value) : isset($item['id']) && $item['id'] == $value, ]); if(!empty($item['children'])) { $data->Children = ArrayList::create(array_map(function ($item) use ($value) { return $this->convertToOption($item, $value); }, $item['children'])); } return $data; } } |