Source of file FlatpickrField.php
Size: 6,239 Bytes - Last Modified: 2021-12-23T10:00:38+00:00
/var/www/docs.ssmods.com/process/src/code/fields/FlatpickrField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 | <?php /** * @link https://chmln.github.io/flatpickr */ class FlatpickrField extends TextField { /** * Override locale. If empty will default to current locale * * @var string */ protected $locale = null; /** * Config array * * @var array */ protected $config = []; /** * @var string */ protected $rangeInput; /** * @var string */ protected $disableDateFunctionName; private static $default_config = [ 'altInput' => true, ]; public function __construct($name, $title = null, $value = '', $maxLength = null, $form = null) { parent::__construct($name, $title, $value, $maxLength, $form); $this->config = self::config()->default_config; } public function Type() { return 'flatpickr'; } public function extraClass() { return 'text ' . parent::extraClass(); } public function getConfig($key) { if (isset($this->config)) { return $this->config[$key]; } } /** * @return this */ public function setConfig($key, $value) { if ($value) { $this->config[$key] = $value; } else { unset($this->config[$key]); } return $this; } public function getEnableTime() { return $this->getConfig('enableTime'); } public function setEnableTime($value) { return $this->setConfig('enableTime', $value); } public function getNoCalendar() { return $this->getConfig('noCalendar'); } public function setNoCalendar($value) { return $this->setConfig('noCalendar', $value); } public function getAltInput() { return $this->getConfig('altInput'); } public function setAltInput($value) { return $this->setConfig('altInput', $value); } public function getMinDate() { return $this->getConfig('minDate'); } public function setMinDate($value) { return $this->setConfig('minDate', $value); } public function getMaxDate() { return $this->getConfig('maxDate'); } public function setMaxDate($value) { return $this->setConfig('maxDate', $value); } public function getDefaultDate() { return $this->getConfig('defaultDate'); } public function setDefaultDate($value) { return $this->setConfig('defaultDate', $value); } public function getDisabledDates() { return $this->getConfig('disable'); } /** * Disable: * - specific dates (iso format) * - range of dates (object with from/to keys) * * @param array $value * @return self */ public function setDisabledDates($value) { return $this->setConfig('disable', $value); } /** * Get locale to use for this field * * @return string */ public function getLocale() { return $this->locale ? : i18n::get_locale(); } /** * Determines the presented/processed format based on locale defaults, * instead of explicitly setting {@link setDateFormat()}. * Only applicable with {@link setHTML5(false)}. * * @param string $locale * @return self */ public function setLocale($locale) { $this->locale = $locale; return $this; } /** * This is required (and ignored) because DBDate use this to scaffold the field * * @param boolean $bool * @return self */ public function setHTML5($bool) { return $this; } /** * Get range input * * @return string */ public function getRangeInput() { return $this->rangeInput; } /** * Set range input * * Warning : currently start and end values are stored * in the same input and require extra processing * Use with caution! * * @param string|FormField $rangeInput Range input * * @return self */ public function setRangeInput($rangeInput) { if ($rangeInput instanceof FormField) { // Prevent any further init on this field $rangeInput->addExtraClass("flatpickr-init"); $rangeInput = $rangeInput->ID(); } $rangeInput = '#' . trim($rangeInput, '#'); $this->rangeInput = $rangeInput; return $this; } /** * Get the value of disableDateFunctionName * * @return string */ public function getDisableDateFunctionName() { return $this->disableDateFunctionName; } /** * Set the value of disableDateFunctionName * * @param string $disableDateFunctionName * * @return self */ public function setDisableDateFunctionName($disableDateFunctionName) { $this->disableDateFunctionName = $disableDateFunctionName; return $this; } public function Field($properties = array()) { // Set lang based on locale $lang = substr($this->getLocale(), 0, 2); if ($lang != 'en') { $this->setConfig('locale', $lang); } Requirements::css('https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.3.2/flatpickr.min.css'); Requirements::javascript('https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.3.2/flatpickr.js'); // Locale if ($lang != 'en') { Requirements::javascript("https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.3.2/l10n/$lang.js"); } // Range $this->setAttribute('data-flatpickr', json_encode($this->config)); if ($this->rangeInput) { $this->setAttribute('data-rangeinput', $this->rangeInput); Requirements::javascript("https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.3.2/plugins/rangePlugin.js"); } if($this->disableDateFunctionName) { $this->setAttribute('data-disabledfunc', $this->disableDateFunctionName); } Requirements::javascript(FORM_EXTRAS_PATH . '/javascript/FlatpickrField.js'); return parent::Field($properties); } } |