Source of file LookupField.php
Size: 3,445 Bytes - Last Modified: 2021-12-23T10:37:53+00:00
/var/www/docs.ssmods.com/process/src/src/Model/LookupField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 | <?php namespace Somar\NZBN\Model; use ArrayAccess; use SilverStripe\Core\Config\Configurable; use SilverStripe\Forms\Form; use SilverStripe\Forms\TextField; use SilverStripe\ORM\Map; use SilverStripe\ORM\SS_List; use SilverStripe\View\Requirements; class LookupField extends TextField { use Configurable; /** * @config * @var string */ private static $button_text = 'Lookup'; /** * Associative or numeric array of all dropdown items, * with array key as the submitted field value, and the array value as a * natural language description shown in the interface element. * * @var array */ protected $source; /** * Returns an input field. * * @param string $name * @param null|string $title * @param array $source * @param string $value * @param null|int $maxLength Max characters to allow for this field. If this value is stored * against a DB field with a fixed size it's recommended to set an appropriate max length * matching this size. * @param null|Form $form */ public function __construct($name, $title = null, $source = null, $value = '', $maxLength = null, $form = null) { Requirements::javascript('somar/silverstripe-nzbn:client/dist/js/nzbn.js'); Requirements::css('somar/silverstripe-nzbn:client/dist/css/nzbn.css'); if ($source) { $this->setSource($source); } if ($maxLength) { $this->setMaxLength($maxLength); } if ($form) { $this->setForm($form); } parent::__construct($name, $title, $value); } /** * @return array */ public function getAttributes() { $parent = parent::getAttributes(); $parent['class'] .= ' text'; $maxLength = $this->getMaxLength(); $attributes = array(); if ($maxLength) { $attributes['maxLength'] = $maxLength; $attributes['size'] = min($maxLength, 30); } $source = $this->getSource(); $attributes['data-nzbn'] = json_encode($source); return array_merge( $parent, $attributes ); } /** * Gets the source array not including any empty default values. * * @return array|ArrayAccess */ public function getSource() { return $this->source; } /** * Set the source for this list * * @param mixed $source * @return $this */ public function setSource($source) { $this->source = $this->getListMap($source); return $this; } /** * Given a list of values, extract the associative map of id => title * * @param mixed $source * @return array Associative array of ids and titles */ protected function getListMap($source) { // Extract source as an array if ($source instanceof SS_List) { $source = $source->map(); } if ($source instanceof Map) { $source = $source->toArray(); } if (!is_array($source) && !($source instanceof ArrayAccess)) { user_error('$source passed in as invalid type', E_USER_ERROR); } return $source; } /** * @return string */ protected function getButtonText() { return self::config()->get('button_text'); } } |