1<?php
2/**
3 * Class helper_plugin_bureaucracyau_fieldtime
4 *
5 * A time in the format (h)h:mm(:ss)
6 */
7class helper_plugin_bureaucracyau_fieldtime extends helper_plugin_bureaucracyau_fieldtextbox {
8    /**
9     * Arguments:
10     *  - cmd
11     *  - label
12     *  - ^ (optional)
13     *
14     * @param array $args The tokenized definition, only split at spaces
15     */
16    public function initialize($args) {
17        parent::initialize($args);
18        $attr = array(
19            'class' => 'timefield edit',
20            'maxlength'=>'8'
21        );
22        if(!isset($this->opt['optional'])) {
23            $attr['required'] = 'required';
24            $attr['class'] .= ' required';
25        }
26        $this->tpl = form_makeTextField('@@NAME@@', '@@VALUE@@', '@@DISPLAY@@', '@@ID@@', '@@CLASS@@', $attr);
27    }
28
29    /**
30     * Validate field input
31     *
32     * @throws Exception when empty or wrong time format
33     */
34    protected function _validate() {
35        parent::_validate();
36
37        $value = $this->getParam('value');
38        if (!is_null($value) && !preg_match('/^\d{1,2}:\d{2}(?::\d{2})?$/', $value)) {
39            throw new Exception(sprintf($this->getLang('e_time'),hsc($this->getParam('display'))));
40        }
41    }
42}
43