Passed
Push — webservicelpcreate ( d8cb35 )
by
unknown
13:48
created

DateTimePicker::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Form element to select a date and hour.
6
 */
7
class DateTimePicker extends HTML_QuickForm_text
8
{
9
    /**
10
     * DateTimePicker constructor.
11
     *
12
     * @param string       $elementName
13
     * @param string|array $elementLabel
14
     * @param array        $attributes
15
     */
16
    public function __construct($elementName, $elementLabel = null, $attributes = null)
17
    {
18
        if (!isset($attributes['id'])) {
19
            $attributes['id'] = $elementName;
20
        }
21
        $attributes['class'] = 'form-control';
22
        parent::__construct($elementName, $elementLabel, $attributes);
23
        $this->_appendName = true;
24
    }
25
26
    /**
27
     * HTML code to display this datepicker.
28
     *
29
     * @return string
30
     */
31
    public function toHtml()
32
    {
33
        if ($this->_flagFrozen) {
34
            return $this->getFrozenHtml();
35
        }
36
37
        $id = $this->getAttribute('id');
38
        $value = $this->getValue();
39
40
        $formattedValue = '';
41
        if (!empty($value)) {
42
            $formattedValue = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
43
        }
44
45
        $label = $this->getLabel();
46
        if (is_array($label) && isset($label[0])) {
47
            $label = $label[0];
48
        }
49
50
        $resetFieldX = sprintf(get_lang('ResetFieldX'), $label);
51
52
        return '
53
            <div class="input-group" id="date_time_wrapper_'.$id.'">
54
                <span class="input-group-addon cursor-pointer">
55
                    <input '.$this->_getAttrString($this->_attributes).'>
56
                </span>
57
                <p class="form-control disabled" id="'.$id.'_alt_text">'.$formattedValue.'</p>
58
                <input class="form-control" type="hidden" id="'.$id.'_alt" value="'.$value.'">
59
                <span class="input-group-btn">
60
                    <button class="btn btn-default" type="button"
61
                            title="'.$resetFieldX.'">
62
                        <span class="fa fa-trash text-danger" aria-hidden="true"></span>
63
                        <span class="sr-only">'.$resetFieldX.'</span>
64
                    </button>
65
                </span>
66
            </div>
67
        '.$this->getElementJS();
68
    }
69
70
    /**
71
     * @param string $value
72
     */
73
    public function setValue($value)
74
    {
75
        $value = substr($value, 0, 16);
76
        $this->updateAttributes(['value' => $value]);
77
    }
78
79
    /**
80
     * @param string $layout
81
     *
82
     * @return string
83
     */
84
    public function getTemplate($layout)
85
    {
86
        $size = $this->calculateSize();
87
88
        switch ($layout) {
89
            case FormValidator::LAYOUT_INLINE:
90
                return '
91
                <div class="form-group {error_class}">
92
                    <label {label-for} >
93
                        <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
94
                        {label}
95
                    </label>
96
97
                    {element}
98
                </div>';
99
            case FormValidator::LAYOUT_HORIZONTAL:
100
                return '
101
                <div class="form-group {error_class}">
102
                    <label {label-for} class="col-sm-'.$size[0].' control-label {extra_label_class}" >
103
                        <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
104
                        {label}
105
                    </label>
106
                    <div class="col-sm-'.$size[1].'">
107
                        {icon}
108
109
                        {element}
110
111
                        <!-- BEGIN label_2 -->
112
                            <p class="help-block">{label_2}</p>
113
                        <!-- END label_2 -->
114
115
                        <!-- BEGIN error -->
116
                            <span class="help-inline help-block">{error}</span>
117
                        <!-- END error -->
118
                    </div>
119
                    <div class="col-sm-'.$size[2].'">
120
                        <!-- BEGIN label_3 -->
121
                            {label_3}
122
                        <!-- END label_3 -->
123
                    </div>
124
                </div>';
125
            case FormValidator::LAYOUT_BOX_NO_LABEL:
126
                return '{element}';
127
        }
128
    }
129
130
    /**
131
     * Get the necessary javascript for this datepicker.
132
     *
133
     * @return string
134
     */
135
    private function getElementJS()
136
    {
137
        $js = null;
138
        $id = $this->getAttribute('id');
139
        //timeFormat: 'hh:mm'
140
        $js .= "<script>
141
            $(function() {
142
                var txtDateTime = $('#$id'),
143
                    inputGroup = txtDateTime.parents('.input-group'),
144
                    txtDateTimeAlt = $('#{$id}_alt'),
145
                    txtDateTimeAltText = $('#{$id}_alt_text');
146
147
                txtDateTime
148
                    .hide()
149
                    .datetimepicker({
150
                        defaultDate: '".$this->getValue()."',
151
                        dateFormat: 'yy-mm-dd',
152
                        timeFormat: 'HH:mm',
153
                        altField: '#{$id}_alt',
154
                        altFormat: \"".get_lang('DateFormatLongNoDayJS')."\",
155
                        altTimeFormat: \"".get_lang('TimeFormatNoSecJS')."\",
156
                        altSeparator: \" ".get_lang('AtTime')." \",
157
                        altFieldTimeOnly: false,
158
                        showOn: 'both',
159
                        buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
160
                        buttonImageOnly: true,
161
                        buttonText: '".get_lang('SelectDate')."',
162
                        changeMonth: true,
163
                        changeYear: true
164
                    })
165
                    .on('change', function (e) {
166
                        txtDateTimeAltText.text(txtDateTimeAlt.val());
167
                    });
168
                    
169
                txtDateTimeAltText.on('click', function () {
170
                    txtDateTime.datepicker('show');
171
                });
172
173
                inputGroup
174
                    .find('button')
175
                    .on('click', function (e) {
176
                        e.preventDefault();
177
178
                        $('#$id, #{$id}_alt').val('');
179
                        $('#{$id}_alt_text').html('');
180
                    });
181
            });
182
        </script>";
183
184
        return $js;
185
    }
186
}
187