Passed
Push — master ( ad6b2a...ec8fd5 )
by Julito
28:29 queued 12s
created

DateTimePicker::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Form element to select a date and hour.
7
 */
8
class DateTimePicker extends HTML_QuickForm_text
9
{
10
    /**
11
     * DateTimePicker constructor.
12
     *
13
     * @param string       $elementName
14
     * @param string|array $elementLabel
15
     * @param array        $attributes
16
     */
17
    public function __construct($elementName, $elementLabel = null, $attributes = null)
18
    {
19
        if (!isset($attributes['id'])) {
20
            $attributes['id'] = $elementName;
21
        }
22
        $attributes['class'] = 'form-control';
23
        parent::__construct($elementName, $elementLabel, $attributes);
24
        $this->_appendName = true;
25
    }
26
27
    /**
28
     * HTML code to display this datepicker.
29
     *
30
     * @return string
31
     */
32
    public function toHtml()
33
    {
34
        if ($this->_flagFrozen) {
35
            return $this->getFrozenHtml();
36
        }
37
38
        $id = $this->getAttribute('id');
39
        $value = $this->getValue();
40
41
        $formattedValue = '';
42
        if (!empty($value)) {
43
            $formattedValue = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
44
        }
45
46
        $label = $this->getLabel();
47
        if (is_array($label) && isset($label[0])) {
48
            $label = $label[0];
49
        }
50
51
        //$resetFieldX = sprintf(get_lang('Reset %s'), $label);
52
53
        return '
54
            <div id="'.$id.'" class="input-group mb-3">
55
                <input '.$this->_getAttrString($this->_attributes).'
56
                    class="form-control" type="text" value="'.$value.'" data-input>
57
                <div class="input-group-prepend" id="button-addon3">
58
                    <button class="btn btn-outline-secondary"  type="button" data-toggle>
59
                        <i class="fas fa-calendar-alt"></i>
60
                    </button>
61
                    <button class="btn btn-outline-secondary" type="button" data-clear>
62
                        <i class="fas fa-times"></i>
63
                    </button>
64
              </div>
65
            </div>
66
        '.$this->getElementJS();
67
68
        /*return '
69
            <div class="input-group mb-3" id="date_time_wrapper_'.$id.'">
70
                <span class="input-group-prepend">
71
                    <input '.$this->_getAttrString($this->_attributes).'>
72
                </span>
73
                <p class="form-control disabled" id="'.$id.'_alt_text">'.$formattedValue.'</p>
74
                <input class="form-control" type="hidden" id="'.$id.'_alt" value="'.$value.'">
75
                <div class="input-group-append">
76
                    <button class="btn btn-light" type="button"
77
                            title="'.$resetFieldX.'">
78
                        <span class="fa fa-trash text-danger" aria-hidden="true"></span>
79
                        <span class="sr-only">'.$resetFieldX.'</span>
80
                    </button>
81
                </div>
82
            </div>
83
        '.$this->getElementJS();*/
84
    }
85
86
    /**
87
     * @param string $value
88
     */
89
    public function setValue($value)
90
    {
91
        $value = substr($value, 0, 16);
92
        $this->updateAttributes(['value' => $value]);
93
    }
94
95
    /**
96
     * Get the necessary javascript for this datepicker.
97
     *
98
     * @return string
99
     */
100
    private function getElementJS()
101
    {
102
        $js = null;
103
        $id = $this->getAttribute('id');
104
        //timeFormat: 'hh:mm'
105
        $js .= "<script>
106
            $(function() {
107
                var config = {
108
                    altInput: true,
109
                    altFormat: '".get_lang('F d, Y')." ".get_lang('at')." H:i',
110
                    enableTime: true,
111
                    dateFormat: 'Y-m-d H:i',
112
                    time_24hr: true,
113
                    wrap: true
114
                };
115
                $('#{$id}').flatpickr(config);
116
117
                /*
118
119
                var txtDateTime = $('#$id'),
120
                    inputGroup = txtDateTime.parents('.input-group'),
121
                    txtDateTimeAlt = $('#{$id}_alt'),
122
                    txtDateTimeAltText = $('#{$id}_alt_text');
123
                txtDateTime
124
                    .hide()
125
                    .datetimepicker({
126
                        defaultDate: '".$this->getValue()."',
127
                        dateFormat: 'yy-mm-dd',
128
                        timeFormat: 'HH:mm',
129
                        altField: '#{$id}_alt',
130
                        altFormat: \"".get_lang('MM dd, yy')."\",
131
                        altTimeFormat: \"".get_lang('HH:mm')."\",
132
                        altSeparator: \" ".get_lang(' at')." \",
133
                        altFieldTimeOnly: false,
134
                        showOn: 'both',
135
                        buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
136
                        buttonImageOnly: true,
137
                        buttonText: '".get_lang('Select date')."',
138
                        changeMonth: true,
139
                        changeYear: true
140
                    })
141
                    .on('change', function (e) {
142
                        txtDateTimeAltText.text(txtDateTimeAlt.val());
143
                    });
144
145
                txtDateTimeAltText.on('click', function () {
146
                    txtDateTime.datepicker('show');
147
                });
148
149
                inputGroup
150
                    .find('button')
151
                    .on('click', function (e) {
152
                        e.preventDefault();
153
154
                        $('#$id, #{$id}_alt').val('');
155
                        $('#{$id}_alt_text').html('');
156
                    });
157
                */
158
            });
159
        </script>";
160
161
        return $js;
162
    }
163
}
164