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