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

DatePicker::toHtml()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 29
rs 9.8333
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Form element to select a date.
6
 *
7
 * Class DatePicker
8
 */
9
class DatePicker extends HTML_QuickForm_text
10
{
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
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
        if (!empty($value)) {
42
            $value = api_format_date($value, DATE_FORMAT_LONG_NO_DAY);
43
        }
44
45
        return '
46
            <div class="input-group">
47
                <span class="input-group-addon cursor-pointer">
48
                    <input '.$this->_getAttrString($this->_attributes).'>
49
                </span>
50
                <p class="form-control disabled" id="'.$id.'_alt_text">'.$value.'</p>
51
                <input class="form-control" type="hidden" id="'.$id.'_alt" value="'.$value.'">
52
                <span class="input-group-btn">
53
                    <button class="btn btn-default" type="button"
54
                            title="'.sprintf(get_lang('ResetFieldX'), $this->_label).'">
55
                        <span class="fa fa-trash text-danger" aria-hidden="true"></span>
56
                        <span class="sr-only">'.sprintf(get_lang('ResetFieldX'), $this->_label).'</span>
57
                    </button>
58
                </span>
59
            </div>
60
        '.$this->getElementJS();
61
    }
62
63
    /**
64
     * @param string $value
65
     */
66
    public function setValue($value)
67
    {
68
        $value = substr($value, 0, 16);
69
        $this->updateAttributes(
70
            [
71
                'value' => $value,
72
            ]
73
        );
74
    }
75
76
    /**
77
     * @param string $layout
78
     *
79
     * @return string
80
     */
81
    public function getTemplate($layout)
82
    {
83
        $size = $this->calculateSize();
84
85
        switch ($layout) {
86
            case FormValidator::LAYOUT_INLINE:
87
                return '
88
                <div class="form-group {error_class}">
89
                    <label {label-for} >
90
                        <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
91
                        {label}
92
                    </label>
93
94
                    {element}
95
                </div>';
96
            case FormValidator::LAYOUT_HORIZONTAL:
97
                return '
98
                <div class="form-group {error_class}">
99
                    <label {label-for} class="col-sm-'.$size[0].' control-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
            case FormValidator::LAYOUT_BOX_NO_LABEL:
123
                return '{element}';
124
        }
125
    }
126
127
    /**
128
     * Get the necessary javascript for this datepicker.
129
     *
130
     * @return string
131
     */
132
    private function getElementJS()
133
    {
134
        $js = null;
135
        $id = $this->getAttribute('id');
136
137
        $js .= "<script>                    
138
            $(function() {
139
                var txtDate = $('#$id'),
140
                    inputGroup = txtDate.parents('.input-group'),
141
                    txtDateAlt = $('#{$id}_alt'),
142
                    txtDateAltText = $('#{$id}_alt_text');
143
                    
144
                txtDate
145
                    .hide()
146
                    .datepicker({
147
                        defaultDate: '".$this->getValue()."',
148
                        dateFormat: 'yy-mm-dd',
149
                        altField: '#{$id}_alt',
150
                        altFormat: \"".get_lang('DateFormatLongNoDayJS')."\",
151
                        showOn: 'both',
152
                        buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
153
                        buttonImageOnly: true,
154
                        buttonText: '".get_lang('SelectDate')."',
155
                        changeMonth: true,
156
                        changeYear: true,
157
                        yearRange: 'c-60y:c+5y'
158
                    })
159
                    .on('change', function (e) {
160
                        txtDateAltText.text(txtDateAlt.val());
161
                    });
162
                    
163
                txtDateAltText.on('click', function () {
164
                    txtDate.datepicker('show');
165
                });
166
167
                inputGroup
168
                    .find('button')
169
                    .on('click', function (e) {
170
                        e.preventDefault();
171
172
                        $('#$id, #{$id}_alt').val('');
173
                        $('#{$id}_alt_text').html('');
174
                    });
175
            });
176
        </script>";
177
178
        return $js;
179
    }
180
}
181