'g:i A', 'minutesInterval' => 15, 'minuteOptions' => array(), 'required' => false, 'emptyOptions' => false, 'smartOptions' => true, ); protected function _getInfo($name, $value = null, $attribs = null, $options = null, $listsep = null) { $info = parent::_getInfo($name, $value, $attribs, $options, $listsep); foreach ($this->_params as $paramName => $paramValue) { if (isset($info['attribs'][$paramName])) { $info[$paramName] = $info['attribs'][$paramName]; unset($info['attribs'][$paramName]); } else { $info[$paramName] = $paramValue; } } foreach (array_keys($info) as $key) { if (isset($info['attribs'][$key])) { unset($info['attribs'][$key]); } } return $info; } protected function _getValuePart($part, $value) { if (false == ($time = strtotime($value))) { return null; } return date($part, $time); } public function formTime($name, $value = null, $attribs = null) { $info = $this->_getInfo($name, $value, $attribs); $id = null;; $required = false; $format = null; $minutes = 0; $minutesInterval = 15; $emptyOptions = false; $smartOptions = true; $options = []; extract($info); $content = $this->view->formHidden($name, $value); $this->_initScript(['id' => $id, 'format' => $format]); $formatChars = str_split($format); foreach ($formatChars as $char) { switch ($char) { case 'H': $hours = 0; while ($hours < 24) { $options[sprintf('%02d', $hours)] = sprintf('%02d', $hours); $hours++; } break; case 's': $seconds = 0; while ($seconds < 60) { $options[sprintf('%02d', $minutes)] = sprintf('%02d', $seconds); $seconds++; } break; case 'g': $options = array_keys(array_fill(1, 12, '')); $options = array_combine($options, $options); break; case 'i': if (empty($minuteOptions)) { $options = array(); $minutes = 0; while ($minutes < 60) { $options[sprintf('%02d', $minutes)] = sprintf('%02d', $minutes); $minutes+= $minutesInterval; } } else { $options = $minuteOptions; } $_value = $this->_getValuePart($char, $value); if ($_value && !array_key_exists($_value, $options) && $smartOptions) { $options[$_value] = $_value; ksort($options); } break; case 'a': case 'A': if ($char == 'a') { $options = array('am' => 'am', 'pm' => 'pm'); } else { $options = array('AM' => 'AM', 'PM' => 'PM'); } break; default: $content .= $char; continue 2; } $_value = $this->_getValuePart($char, $value); $_attribs = array('class' => 'time_' . $char, 'data-part' => $char, 'data-target' => $id); if (!$required || $emptyOptions) { $options = array('' => '') + (array) $options; } $content .= $this->formSelect($_value, $_attribs, $options); } return $content; } protected function formSelect($value, $attribs, $options) { $value = array_map('strval', (array) $value); $xhtml = '_htmlAttribs($attribs) . ">\n "; $list = array(); foreach ((array) $options as $opt_value => $opt_label) { $list[] = $this->_build($opt_value, $opt_label, $value); } $xhtml .= implode("\n ", $list) . "\n"; return $xhtml; } protected function _initScript($options) { /** @var Qs_Doc $doc */ $doc = Zend_Registry::get('doc'); $doc->addScript('js/lib/form/element/timeSelect.js'); $doc->addScript('js/moment.js'); $doc->addInitObject('lib.form.element.TimeSelect', [$options]); return $this; } protected function _build($value, $label, $selected, $disable = [], $optionClasses = array()) { if (is_bool($disable)) { $disable = array(); } $class = null; if (array_key_exists($value, $optionClasses)) { $class = $optionClasses[$value]; } $opt = '"; return $opt; } }