Datetime   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 82
ccs 0
cts 58
cp 0
rs 10
c 0
b 0
f 0
wmc 3
lcom 2
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A inputAttributes() 0 5 1
A renderScript() 0 41 1
A renderInner() 0 22 1
1
<?php
2
namespace Rocket\UI\Forms\Fields;
3
4
/**
5
 * Mangage dateTime fields
6
 */
7
8
/**
9
 * Creates a datefield with Form_element
10
 *
11
 * @author Stéphane Goetz
12
 */
13
class Datetime 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
        $this->input_attributes['type'] = 'text';
22
    }
23
24
    /**
25
     * Adds the script logic to the picker
26
     */
27
    protected function renderScript()
28
    {
29
        $options_date = json_encode_with_functions(
30
            [
31
                'firstDay' => 1,
32
                'format' => 'dd/mm/yyyy',
33
                'selectMonths' => true,
34
                'selectYears' => true,
35
                'onSet' => "function(item) {
36
                    if ( ! 'select' in item ) return;
37
                    setTimeout( function() { $('#{$this->id}').get(0).updateDate(); }, 0 );
38
                }",
39
            ]
40
        );
41
42
        $options_time = json_encode_with_functions(
43
            [
44
                'format' => 'H:i',
45
                'onSet' => "function(item) {
46
                    if ( ! 'select' in item ) return;
47
                    setTimeout( function() { $('#{$this->id}').get(0).updateDate(); }, 0 );
48
                }",
49
            ]
50
        );
51
52
        $this->getJS()->ready(
53
            "
54
            var element = $('#{$this->id}')
55
            element.data('date', $('#{$this->id}_date').pickadate($options_date).pickadate('picker'));
56
            element.data('time', $('#{$this->id}_time').pickatime($options_time).pickatime('picker'));
57
58
            element.get(0).updateDate = function() {
59
                var main = $(this);
60
                main.val(
61
                    main.data('date').get('select', 'dd/mm/yyyy') + ' ' + main.data('time').get('select', 'HH:i')
62
                );
63
            }
64
65
            "
66
        );
67
    }
68
69
    /**
70
     * Render the inner field
71
     */
72
    protected function renderInner()
73
    {
74
        $attr = $this->input_attributes;
75
        $date = explode(' ', $attr['value']) + [1 => ''];
76
77
        $field_main = array_merge($attr, ['type' => 'hidden']);
78
        $this->result .= '<input' . $this->renderAttributes($field_main) . ' />';
79
80
        $this->result .= '<div class=row><div class=col-xs-8>';
81
82
        $field_date = array_merge($attr, ['id' => $attr['id'] . '_date', 'name' => $attr['id'] . '_date']);
83
        $field_date['value'] = $date[0];
84
        $this->result .= '<input' . $this->renderAttributes($field_date) . ' />';
85
86
        $this->result .= '</div><div class=col-xs-4>';
87
88
        $field_time = array_merge($attr, ['id' => $attr['id'] . '_time', 'name' => $attr['id'] . '_time']);
89
        $field_time['value'] = $date[1];
90
        $this->result .= '<input' . $this->renderAttributes($field_time) . ' />';
91
92
        $this->result .= '</div></div>';
93
    }
94
}
95