Completed
Push — master ( 4fc9f8...d0e06e )
by Julito
12:04
created

DatePicker   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 173
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 6 1
A toHtml() 0 29 3
A getTemplate() 0 46 4
A getElementJS() 0 47 1
A __construct() 0 9 2
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('Reset %s'), $this->_label).'">
55
                        <span class="fa fa-trash text-danger" aria-hidden="true"></span>
56
                        <span class="sr-only">'.sprintf(get_lang('Reset %s'), $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
                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...
97
            case FormValidator::LAYOUT_HORIZONTAL:
98
                return '
99
                <div class="form-group row {error_class}">
100
                    <label {label-for} class="col-sm-'.$size[0].' col-form-label {extra_label_class}" >
101
                        <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
102
                        {label}
103
                    </label>
104
                    <div class="col-sm-'.$size[1].'">
105
                        {icon}
106
107
                        {element}
108
109
                        <!-- BEGIN label_2 -->
110
                            <p class="help-block">{label_2}</p>
111
                        <!-- END label_2 -->
112
113
                        <!-- BEGIN error -->
114
                            <span class="help-inline help-block">{error}</span>
115
                        <!-- END error -->
116
                    </div>
117
                    <div class="col-sm-'.$size[2].'">
118
                        <!-- BEGIN label_3 -->
119
                            {label_3}
120
                        <!-- END label_3 -->
121
                    </div>
122
                </div>';
123
                break;
124
            case FormValidator::LAYOUT_BOX_NO_LABEL:
125
                return '{element}';
126
                break;
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
140
        $js .= "<script>                    
141
            $(function() {
142
                var txtDate = $('#$id'),
143
                    inputGroup = txtDate.parents('.input-group'),
144
                    txtDateAlt = $('#{$id}_alt'),
145
                    txtDateAltText = $('#{$id}_alt_text');
146
                    
147
                txtDate
148
                    .hide()
149
                    .datepicker({
150
                        defaultDate: '".$this->getValue()."',
151
                        dateFormat: 'yy-mm-dd',
152
                        altField: '#{$id}_alt',
153
                        altFormat: \"".get_lang('MM dd, yy')."\",
154
                        showOn: 'both',
155
                        buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
156
                        buttonImageOnly: true,
157
                        buttonText: '".get_lang('Select date')."',
158
                        changeMonth: true,
159
                        changeYear: true,
160
                        yearRange: 'c-60y:c+5y'
161
                    })
162
                    .on('change', function (e) {
163
                        txtDateAltText.text(txtDateAlt.val());
164
                    });
165
                    
166
                txtDateAltText.on('click', function () {
167
                    txtDate.datepicker('show');
168
                });
169
170
                inputGroup
171
                    .find('button')
172
                    .on('click', function (e) {
173
                        e.preventDefault();
174
175
                        $('#$id, #{$id}_alt').val('');
176
                        $('#{$id}_alt_text').html('');
177
                    });
178
            });
179
        </script>";
180
181
        return $js;
182
    }
183
}
184