Source of file TimeDropdownField.php
Size: 7,159 Bytes - Last Modified: 2021-12-23T10:00:38+00:00
/var/www/docs.ssmods.com/process/src/code/fields/TimeDropdownField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 | <?php /** * TimeDropdownField * * @author Kalyptus SPRL <thomas@kalyptus.be> */ class TimeDropdownField extends DropdownField { /** * @config * @var array */ private static $default_config = array( 'timeformat' => null, 'use_strtotime' => true, 'datavalueformat' => 'HH:mm:ss' ); /** * @var array */ protected $config; /** * @var String */ protected $locale = null; /** * @var Zend_Date Just set if the date is valid. * {@link $value} will always be set to aid validation, * and might contain invalid values. */ protected $valueObj = null; public function __construct($name, $title = null, $value = "") { if (!$this->locale) { $this->locale = i18n::get_locale(); } $this->config = $this->config()->default_config; if (!$this->getConfig('timeformat')) { $this->setConfig('timeformat', Config::inst()->get('i18n', 'time_format')); } $source = $this->hoursRange(0, 86400, 3600 / 4, 'H:i'); parent::__construct($name, $title, $source, $value); } public function hoursRange($lower = 0, $upper = 86400, $step = 3600, $format = '') { $times = array(); if (empty($format)) { $format = 'g:i a'; } foreach (range($lower, $upper, $step) as $increment) { $increment = gmdate('H:i', $increment); list( $hour, $minutes ) = explode(':', $increment); $date = new DateTime($hour . ':' . $minutes); $times[(string) $increment . ':00'] = $date->format($format); } return $times; } public function Field($properties = array()) { $config = array( 'timeformat' => $this->getConfig('timeformat') ); $config = array_filter($config); $this->addExtraClass(Convert::raw2json($config)); return parent::Field($properties); } public function Type() { return 'time dropdown'; } /** * Parses a time into a Zend_Date object * * @param string $value Raw value * @param string $format Format string to check against * @param string $locale Optional locale to parse against * @param boolean $exactMatch Flag indicating that the date must be in this * exact format, and is unchanged after being parsed and written out * * @return Zend_Date Returns the Zend_Date, or null if not in the specified format */ protected function parseTime($value, $format, $locale = null, $exactMatch = false) { // Check if the date is in the correct format if (!Zend_Date::isDate($value, $format)) { return null; } // Parse the value $valueObject = new Zend_Date($value, $format, $locale); // For exact matches, ensure the value preserves formatting after conversion if ($exactMatch && ($value !== $valueObject->get($format))) { return null; } else { return $valueObject; } } /** * Sets the internal value to ISO date format. * * @param String|Array $val */ public function setValue($val) { // Fuzzy matching through strtotime() to support a wider range of times, // e.g. 11am. This means that validate() might not fire. // Note: Time formats are assumed to be less ambiguous than dates across locales. if ($this->getConfig('use_strtotime') && !empty($val)) { if ($parsedTimestamp = strtotime($val)) { $parsedObj = new Zend_Date($parsedTimestamp, Zend_Date::TIMESTAMP); $val = $parsedObj->get($this->getConfig('timeformat')); unset($parsedObj); } } if (empty($val)) { $this->value = null; $this->valueObj = null; } // load ISO time from database (usually through Form->loadDataForm()) // Requires exact format to prevent false positives from locale specific times else if ($this->valueObj = $this->parseTime($val, $this->getConfig('datavalueformat'), null, true)) { $this->value = $this->valueObj->get($this->getConfig('timeformat')); } // Set in current locale (as string) else if ($this->valueObj = $this->parseTime($val, $this->getConfig('timeformat'), $this->locale)) { $this->value = $this->valueObj->get($this->getConfig('timeformat')); } // Fallback: Set incorrect value so validate() can pick it up elseif (is_string($val)) { $this->value = $val; $this->valueObj = null; } else { $this->value = null; $this->valueObj = null; } return $this; } /** * @return String ISO 8601 date, suitable for insertion into database */ public function dataValue() { if ($this->valueObj) { return $this->valueObj->toString($this->getConfig('datavalueformat')); } else { return null; } } /** * Validate this field * * @param Validator $validator * @return bool */ public function validate($validator) { // Don't validate empty fields if (empty($this->value)) { return true; } if (!Zend_Date::isDate($this->value, $this->getConfig('timeformat'), $this->locale)) { $validator->validationError( $this->name, _t( 'TimeField.VALIDATEFORMAT', "Please enter a valid time format ({format})", array('format' => $this->getConfig('timeformat')) ), "validation", false ); return false; } return true; } /** * @return string */ public function getLocale() { return $this->locale; } /** * @param String $locale */ public function setLocale($locale) { $this->locale = $locale; return $this; } /** * @param string $name * @param mixed $val */ public function setConfig($name, $val) { $this->config[$name] = $val; return $this; } /** * @param String $name Optional, returns the whole configuration array if empty * @return mixed|array */ public function getConfig($name = null) { if ($name) { return isset($this->config[$name]) ? $this->config[$name] : null; } else { return $this->config; } } /** * Creates a new readonly field specified below */ public function performReadonlyTransformation() { return $this->castedCopy('TimeField_Readonly'); } public function castedCopy($class) { $copy = parent::castedCopy($class); if ($copy->hasMethod('setConfig')) { $config = $this->getConfig(); foreach ($config as $k => $v) { $copy->setConfig($k, $v); } } return $copy; } } |