Source of file DependentListboxField.php
Size: 4,527 Bytes - Last Modified: 2021-12-24T06:47:16+00:00
/var/www/docs.ssmods.com/process/src/src/Forms/DependentListboxField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 | <?php namespace Firebrand\Hail\Forms; use SilverStripe\Control\HTTPResponse; use SilverStripe\Core\Convert; use SilverStripe\Forms\FormField; use SilverStripe\Forms\ListboxField; use SilverStripe\ORM\Map; use SilverStripe\View\Requirements; /** * ListBoxDependentField is a field that will refresh its source depending on another field's value * * It is an adaptation of the DependentDropdown module to work with Listbox * Was also modified to work with ListBox as the depending field * * Heavily inspired by https://github.com/sheadawson/silverstripe-dependentdropdownfield * * @package silverstripe-hail * @author Marc Espiard, Firebrand * @version 1.0 * */ class DependentListboxField extends ListboxField { /** * @var array */ private static $allowed_actions = [ 'load', ]; /** * @var */ protected $depends; /** * @var */ protected $unselected; /** * @var \Closure */ protected $sourceCallback; /** * DependentListboxField constructor. * @param string $name * @param string $title * @param \Closure $source * @param string $value * @param $size */ public function __construct($name, $title = null, \Closure $source, $value = '', $size = null) { parent::__construct($name, $title, [], $value, $size); // we are unable to store Closure as a normal source $this->sourceCallback = $source; $this ->addExtraClass('dependent-dropdown') ->addExtraClass('dropdown'); } /** * @param $request * @return HTTPResponse */ public function load($request) { $response = new HTTPResponse(); $response->addHeader('Content-Type', 'application/json'); $items = call_user_func($this->sourceCallback, $request->getVar('val')); $results = []; if ($items) { foreach ($items as $k => $v) { $results[] = ['k' => $k, 'v' => $v]; } } $response->setBody(Convert::array2json($results)); return $response; } /** * @return mixed */ public function getDepends() { return $this->depends; } /** * @param FormField $field * @return $this */ public function setDepends(FormField $field) { $this->depends = $field; return $this; } /** * @return mixed */ public function getUnselectedString() { return $this->unselected; } /** * @param $string * @return $this */ public function setUnselectedString($string) { $this->unselected = $string; return $this; } /** * @return array|\ArrayAccess|mixed */ public function getSource() { $val = $this->depends->Value(); if ( !$val && method_exists($this->depends, 'getHasEmptyDefault') && !$this->depends->getHasEmptyDefault() ) { $dependsSource = array_keys($this->depends->getSource()); $val = isset($dependsSource[0]) ? $dependsSource[0] : null; } if (!$val) { $source = []; } else { $source = call_user_func($this->sourceCallback, $val); if ($source instanceof Map) { $source = $source->toArray(); } } if ($this->getHasEmptyDefault()) { return ['' => $this->getEmptyString()] + (array)$source; } else { return $source; } } /** * @param array $properties * @return string */ public function Field($properties = []) { Requirements::javascript( 'firebrandhq/silverstripe-hail: client/dist/js/dependentlistboxfield.js' ); $this->setAttribute('data-link', $this->Link('load')); $this->setAttribute('data-depends', $this->getDepends()->getName()); $this->setAttribute('data-empty', $this->getEmptyString()); $this->setAttribute('data-unselected', $this->getUnselectedString()); return parent::Field($properties); } public function getHasEmptyDefault() { return false; } public function getEmptyString() { return ''; } } |