Date   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 112
ccs 0
cts 75
cp 0
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A inputAttributes() 0 25 3
A labelAttributes() 0 5 1
A renderScript() 0 26 2
A renderInner() 0 41 4
1
<?php
2
namespace Rocket\UI\Forms\Fields;
3
4
/**
5
 * Manage date fields
6
 */
7
8
/**
9
 * Creates a date field with Form_element
10
 *
11
 * @author Stéphane Goetz
12
 */
13
class Date extends Field
14
{
15
    /**
16
     * Adds some specific attributes to be able to use the date picker automatically
17
     */
18
    protected function inputAttributes()
19
    {
20
        parent::inputAttributes();
21
22
        $this->input_attributes['type'] = 'text';
23
        $this->input_attributes['class'][] = 'date';
24
25
        if (!array_key_exists('options', $this->params)) {
26
            $this->params['options'] = [];
27
        }
28
29
        $this->params['options'] += [
30
            'datepicker' => true,
31
            'formatSubmit' => 'dd/mm/yyyy',
32
            'format' => 'dd/mm/yyyy',
33
            'firstDay' => 1,
34
            'selectMonths' => true,
35
            'selectYears' => true,
36
        ];
37
38
        if (!$this->params['options']['datepicker']) {
39
            $this->params['validate'] = false;
40
            $this->params['multifield'] = true;
41
        }
42
    }
43
44
    protected function labelAttributes()
45
    {
46
        parent::labelAttributes();
47
        unset($this->label_attributes['for']);
48
    }
49
50
    /**
51
     * Adds the script logic to the picker
52
     */
53
    protected function renderScript()
54
    {
55
        if (!$this->params['options']['datepicker']) {
56
            $this->getJS()->ready("
57
            $('#{$this->id}_day, #{$this->id}_month, #{$this->id}_year').change(function(){
58
                var day = $('#{$this->id}_day').val() || '1';
59
                var month = $('#{$this->id}_month').val() || '1';
60
                var year = $('#{$this->id}_year').val() || new Date().getFullYear();
61
62
                $('#{$this->id}').val(day + '/' + month + '/' + year);
63
            });
64
65
            $('#{$this->id}').change(function(){
66
                var cal = $(this).val().split('/');
67
68
                $('#{$this->id}_day').val(cal[0]);
69
                $('#{$this->id}_month').val(cal[1]);
70
                $('#{$this->id}_year').val(cal[2]);
71
            });
72
            ");
73
74
            return;
75
        }
76
77
        $this->getJS()->ready('$( "#' . $this->id . '").pickadate(' . json_encode($this->params['options']) . ');');
78
    }
79
80
    /**
81
     * Render the inner field
82
     */
83
    protected function renderInner()
84
    {
85
        if ($this->params['options']['datepicker']) {
86
            return parent::renderInner();
87
        }
88
89
        $attr = $this->input_attributes;
90
        $date = explode('/', $attr['value']) + [1 => '', 2 => ''];
91
92
        $field_main = array_merge($attr, ['type' => 'hidden']);
93
        $this->result .= '<input' . $this->renderAttributes($field_main) . ' />';
94
95
        $this->result .= '<div class=row><div class=col-xs-3>';
96
97
        //day
98
        $field_date = array_merge($attr, ['id' => $attr['id'] . '_day', 'name' => $attr['id'] . '_day', 'placeholder' => 'Day']);
99
        $field_date['value'] = $date[0];
100
        $this->result .= '<input' . $this->renderAttributes($field_date) . ' />';
101
102
        $this->result .= '</div><div class=col-xs-5>';
103
104
        //month
105
        $field_date = array_merge($attr, ['id' => $attr['id'] . '_month', 'name' => $attr['id'] . '_month']);
106
        unset($field_date['value']);
107
        $this->result .= '<select' . $this->renderAttributes($field_date) . '>';
108
        foreach (range(1, 12) as $month) {
109
            $checked = ($month == $date[1]) ? ' selected=selected' : '';
110
            $this->result .= "<option value='$month'$checked>" . strftime('%B', mktime(0, 0, 0, $month)) . '</option>';
111
        }
112
113
        $this->result .= '</select>';
114
115
        $this->result .= '</div><div class=col-xs-4>';
116
117
        //year
118
        $field_time = array_merge($attr, ['id' => $attr['id'] . '_year', 'name' => $attr['id'] . '_year', 'placeholder' => 'Year']);
119
        $field_time['value'] = $date[2];
120
        $this->result .= '<input' . $this->renderAttributes($field_time) . ' />';
121
122
        $this->result .= '</div></div>';
123
    }
124
}
125