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

DateTimeRangePicker::getTemplate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 69
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 4
nop 1
dl 0
loc 69
rs 9.552
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 DateTimeRangePicker extends DateRangePicker
10
{
11
    /**
12
     * HTML code to display this datepicker.
13
     *
14
     * @return string
15
     */
16
    public function toHtml()
17
    {
18
        if ($this->_flagFrozen) {
19
            return $this->getFrozenHtml();
20
        }
21
22
        $id = $this->getAttribute('id');
23
        $dateRange = $this->getValue();
24
25
        $value = '';
26
        if (!empty($dateRange)) {
27
            $dates = $this->parseDateRange($dateRange);
28
            $value = api_format_date($dates['date'], DATE_FORMAT_LONG_NO_DAY);
29
        }
30
31
        return '
32
            <div class="input-group">
33
                <span class="input-group-addon cursor-pointer">
34
                    <input '.$this->_getAttrString($this->_attributes).'>
35
                </span>
36
                <p class="form-control disabled" id="'.$id.'_alt_text">'.$value.'</p>
37
                <input class="form-control" type="hidden" id="'.$id.'_alt" value="'.$value.'">
38
                <span class="input-group-btn">
39
                    <button class="btn btn-default" type="button"
40
                            title="'.sprintf(get_lang('ResetFieldX'), $this->_label).'">
41
                        <span class="fa fa-trash text-danger" aria-hidden="true"></span>
42
                        <span class="sr-only">'.sprintf(get_lang('ResetFieldX'), $this->_label).'</span>
43
                    </button>
44
                </span>                
45
            </div>            
46
        '.$this->getElementJS();
47
    }
48
49
    /**
50
     * @param string $layout
51
     *
52
     * @return string
53
     */
54
    public function getTemplate($layout)
55
    {
56
        $size = $this->calculateSize();
57
        $id = $this->getAttribute('id');
58
59
        switch ($layout) {
60
            case FormValidator::LAYOUT_INLINE:
61
                return '
62
                <div class="form-group {error_class}">
63
                    <label {label-for} >
64
                        <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
65
                        {label}
66
                    </label>
67
                    {element}
68
                </div>';
69
                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...
70
            case FormValidator::LAYOUT_HORIZONTAL:
71
                return '
72
                <span id="'.$id.'_date_time_wrapper">
73
                <div class="form-group {error_class}">
74
                    <label {label-for} class="col-sm-'.$size[0].' control-label" >
75
                        <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
76
                        {label}
77
                    </label>
78
                    <div class="col-sm-'.$size[1].'">
79
                        {icon}
80
                        {element}
81
82
                        <!-- BEGIN label_2 -->
83
                        <p class="help-block">{label_2}</p>
84
                        <!-- END label_2 -->
85
86
                        <!-- BEGIN error -->
87
                        <span class="help-inline help-block">{error}</span>
88
                        <!-- END error -->
89
                    </div>
90
                    <div class="col-sm-'.$size[2].'">
91
                        <!-- BEGIN label_3 -->
92
                            {label_3}
93
                        <!-- END label_3 -->
94
                    </div>            
95
                </div>                
96
                <div class="form-group {error_class}">
97
                    <label class="col-sm-'.$size[0].' control-label" >
98
                        <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
99
                        '.get_lang('Hour').'
100
                    </label>
101
                    <div class="col-sm-'.$size[1].'"> 
102
                        <div class="input-group"> 
103
                            <p id="'.$id.'_time_range">                    
104
                                <input type="text" id="'.$id.'_time_range_start" name="'.$id.'_time_range_start" class="time start" autocomplete="off"> 
105
                                '.get_lang('To').'
106
                                <input type="text" id="'.$id.'_time_range_end" name="'.$id.'_time_range_end" class="time end " autocomplete="off">
107
                            </p>
108
                        </div>                   
109
                    </div>
110
                </div> 
111
                </span>                   
112
                ';
113
                break;
114
            case FormValidator::LAYOUT_BOX_NO_LABEL:
115
                return '
116
                        <label {label-for}>{label}</label>
117
                        <div class="input-group">
118
                            
119
                            {icon}
120
                            {element}
121
                        </div>';
122
                break;
123
        }
124
    }
125
126
    /**
127
     * @param array $dateRange
128
     *
129
     * @return array
130
     */
131
    public function parseDateRange($dateRange)
132
    {
133
        $dateRange = Security::remove_XSS($dateRange);
134
        $dates = explode('@@', $dateRange);
135
        $dates = array_map('trim', $dates);
136
        $start = isset($dates[0]) ? $dates[0] : '';
137
        $end = isset($dates[1]) ? $dates[1] : '';
138
139
        $date = substr($start, 0, 10);
140
        $start = isset($dates[0]) ? $dates[0] : '';
141
        //$start = substr($start, 11, strlen($start));
142
        //$end = substr($end, 11, strlen($end));
143
144
        return [
145
            'date' => $date,
146
            'start_time' => $start,
147
            'end_time' => $end,
148
        ];
149
    }
150
151
    /**
152
     * @param string $value
153
     */
154
    public function setValue($value)
155
    {
156
        $this->updateAttributes(
157
            [
158
                'value' => $value,
159
            ]
160
        );
161
    }
162
163
    /**
164
     * Get the necessary javascript for this datepicker.
165
     *
166
     * @return string
167
     */
168
    private function getElementJS()
169
    {
170
        $js = null;
171
        $id = $this->getAttribute('id');
172
173
        $dateRange = $this->getValue();
174
175
        $defaultDate = '';
176
        $startTime = '';
177
        $endTime = '';
178
        if (!empty($dateRange)) {
179
            $dates = $this->parseDateRange($dateRange);
180
            $defaultDate = $dates['date'];
181
            $startTime = $dates['start_time'];
182
            $endTime = $dates['end_time'];
183
        }
184
185
        $js .= "<script>                    
186
            $(function() {
187
                var txtDate = $('#$id'),
188
                    inputGroup = txtDate.parents('.input-group'),
189
                    txtDateAlt = $('#{$id}_alt'),
190
                    txtDateAltText = $('#{$id}_alt_text');
191
                    
192
                txtDate
193
                    .hide()
194
                    .datepicker({
195
                        defaultDate: '".$defaultDate."',
196
                        dateFormat: 'yy-mm-dd',
197
                        altField: '#{$id}_alt',
198
                        altFormat: \"".get_lang('DateFormatLongNoDayJS')."\",
199
                        showOn: 'both',
200
                        buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
201
                        buttonImageOnly: true,
202
                        buttonText: '".get_lang('SelectDate')."',
203
                        changeMonth: true,
204
                        changeYear: true,
205
                        yearRange: 'c-60y:c+5y'
206
                    })
207
                    .on('change', function (e) {
208
                        txtDateAltText.text(txtDateAlt.val());
209
                    });
210
                                        
211
                txtDateAltText.on('click', function () {
212
                    txtDate.datepicker('show');
213
                });
214
                
215
                inputGroup
216
                    .find('button')
217
                    .on('click', function (e) {
218
                        e.preventDefault();
219
                        $('#$id, #{$id}_alt').val('');
220
                        $('#{$id}_alt_text').html('');
221
                    });
222
                
223
                $('#".$id."_time_range .time').timepicker({
224
                    'showDuration': true,
225
                    'timeFormat': 'H:i:s',
226
                    'scrollDefault': 'now',                    
227
                });
228
                
229
                $('#".$id."_time_range_start').timepicker('setTime', new Date('".$startTime."'));
230
                $('#".$id."_time_range_end').timepicker('setTime', new Date('".$endTime."'));
231
                
232
                var timeOnlyExampleEl = document.getElementById('".$id."_time_range');
233
                var timeOnlyDatepair = new Datepair(timeOnlyExampleEl);                
234
            });
235
        </script>";
236
237
        return $js;
238
    }
239
}
240