Completed
Push — master ( f41061...196bfc )
by Julito
09:03
created

DateTimePicker::getTemplate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 46
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 1
dl 0
loc 46
rs 9.7998
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
     * Constructor.
11
     */
12
    public function __construct($elementName = null, $elementLabel = null, $attributes = null)
13
    {
14
        if (!isset($attributes['id'])) {
15
            $attributes['id'] = $elementName;
16
        }
17
        $attributes['class'] = 'form-control';
18
        parent::__construct($elementName, $elementLabel, $attributes);
19
        $this->_appendName = true;
0 ignored issues
show
Bug Best Practice introduced by
The property _appendName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
    }
21
22
    /**
23
     * HTML code to display this datepicker.
24
     *
25
     * @return string
26
     */
27
    public function toHtml()
28
    {
29
        if ($this->_flagFrozen) {
30
            return $this->getFrozenHtml();
31
        }
32
33
        $id = $this->getAttribute('id');
34
        $value = $this->getValue();
35
36
        $formattedValue = '';
37
        if (!empty($value)) {
38
            $formattedValue = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
39
        }
40
41
        $label = $this->getLabel();
42
        if (is_array($label) && isset($label[0])) {
43
            $label = $label[0];
44
        }
45
46
        $resetFieldX = sprintf(get_lang('ResetFieldX'), $label);
47
48
        return '
49
            <div class="input-group mb-3" id="date_time_wrapper_'.$id.'">
50
                <span class="input-group-prepend">
51
                    <input '.$this->_getAttrString($this->_attributes).'>
52
                </span>
53
                <p class="form-control disabled" id="'.$id.'_alt_text">'.$formattedValue.'</p>
54
                <input class="form-control" type="hidden" id="'.$id.'_alt" value="'.$value.'">
55
                <div class="input-group-append">
56
                    <button class="btn btn-light" type="button"
57
                            title="'.$resetFieldX.'">
58
                        <span class="fa fa-trash text-danger" aria-hidden="true"></span>
59
                        <span class="sr-only">'.$resetFieldX.'</span>
60
                    </button>
61
                </div>
62
            </div>
63
        '.$this->getElementJS();
64
    }
65
66
    /**
67
     * @param string $value
68
     */
69
    public function setValue($value)
70
    {
71
        $value = substr($value, 0, 16);
72
        $this->updateAttributes(['value' => $value]);
73
    }
74
75
    /**
76
     * @param string $layout
77
     *
78
     * @return string
79
     */
80
    public function getTemplate($layout)
81
    {
82
        $size = $this->calculateSize();
83
84
        switch ($layout) {
85
            case FormValidator::LAYOUT_INLINE:
86
                return '
87
                <div class="form-group {error_class}">
88
                    <label {label-for} >
89
                        <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
90
                        {label}
91
                    </label>
92
93
                    {element}
94
                </div>';
95
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
96
            case FormValidator::LAYOUT_HORIZONTAL:
97
                return '
98
                <div class="form-group row {error_class}">
99
                    <label {label-for} class="col-sm-'.$size[0].' col-form-label {extra_label_class}" >
100
                        <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
101
                        {label}
102
                    </label>
103
                    <div class="col-sm-'.$size[1].'">
104
                        {icon}
105
106
                        {element}
107
108
                        <!-- BEGIN label_2 -->
109
                            <p class="help-block">{label_2}</p>
110
                        <!-- END label_2 -->
111
112
                        <!-- BEGIN error -->
113
                            <span class="help-inline help-block">{error}</span>
114
                        <!-- END error -->
115
                    </div>
116
                    <div class="col-sm-'.$size[2].'">
117
                        <!-- BEGIN label_3 -->
118
                            {label_3}
119
                        <!-- END label_3 -->
120
                    </div>
121
                </div>';
122
                break;
123
            case FormValidator::LAYOUT_BOX_NO_LABEL:
124
                return '{element}';
125
                break;
126
        }
127
    }
128
129
    /**
130
     * Get the necessary javascript for this datepicker.
131
     *
132
     * @return string
133
     */
134
    private function getElementJS()
135
    {
136
        $js = null;
137
        $id = $this->getAttribute('id');
138
        //timeFormat: 'hh:mm'
139
        $js .= "<script>
140
            $(function() {
141
                var txtDateTime = $('#$id'),
142
                    inputGroup = txtDateTime.parents('.input-group'),
143
                    txtDateTimeAlt = $('#{$id}_alt'),
144
                    txtDateTimeAltText = $('#{$id}_alt_text');
145
146
                txtDateTime
147
                    .hide()
148
                    .datetimepicker({
149
                        defaultDate: '".$this->getValue()."',
150
                        dateFormat: 'yy-mm-dd',
151
                        timeFormat: 'HH:mm',
152
                        altField: '#{$id}_alt',
153
                        altFormat: \"".get_lang('DateFormatLongNoDayJS')."\",
154
                        altTimeFormat: \"".get_lang('TimeFormatNoSecJS')."\",
155
                        altSeparator: \" ".get_lang('AtTime')." \",
156
                        altFieldTimeOnly: false,
157
                        showOn: 'both',
158
                        buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
159
                        buttonImageOnly: true,
160
                        buttonText: '".get_lang('SelectDate')."',
161
                        changeMonth: true,
162
                        changeYear: true
163
                    })
164
                    .on('change', function (e) {
165
                        txtDateTimeAltText.text(txtDateTimeAlt.val());
166
                    });
167
                    
168
                txtDateTimeAltText.on('click', function () {
169
                    txtDateTime.datepicker('show');
170
                });
171
172
                inputGroup
173
                    .find('button')
174
                    .on('click', function (e) {
175
                        e.preventDefault();
176
177
                        $('#$id, #{$id}_alt').val('');
178
                        $('#{$id}_alt_text').html('');
179
                    });
180
            });
181
        </script>";
182
183
        return $js;
184
    }
185
}
186