Passed
Push — 1.10.x ( e22269...acc912 )
by
unknown
49:56
created

DateTimePicker   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 172
Duplicated Lines 56.98 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 17
lcom 1
cbo 2
dl 98
loc 172
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toHtml() 23 23 3
A setValue() 0 9 1
B getElementJS() 0 28 1
C getTemplate() 75 75 10
A __construct() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Form element to select a date and hour.
6
 */
7
class DateTimePicker extends HTML_QuickForm_text
8
{
9
    /**
10
     * Constructor
11
     */
12
    public function __construct($elementName = null, $elementLabel = null, $attributes = null)
13
    {
14
        if (!isset($attributes['id'])) {
15
            $attributes['id'] = $elementName;
16
        }
17
        $attributes['class'] = 'form-control';
18
        parent::__construct($elementName, $elementLabel, $attributes);
19
        $this->_appendName = true;
20
        $this->_type = 'date_time_picker';
21
    }
22
23
    /**
24
     * HTML code to display this datepicker
25
     * @return string
26
     */
27 View Code Duplication
    public function toHtml()
28
    {
29
        if ($this->_flagFrozen) {
30
            return $this->getFrozenHtml();
31
        }
32
33
        $id = $this->getAttribute('id');
34
        $value = $this->getValue();
35
        $label = $this->getLabel();
36
37
        if (!empty($value)) {
38
            $value = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
39
        }
40
41
        return $this->getElementJS() . '
42
            <div class="input-group">
43
                <span class="input-group-addon">
44
                    <input ' . $this->_getAttrString($this->_attributes) . '>
45
                </span>
46
                <input class="form-control" type="text" readonly id="' . $id . '_alt" value="' . $value . '">
47
            </div>
48
        ';
49
    }
50
51
    /**
52
     * @param string $value
53
     */
54
    function setValue($value)
55
    {
56
        $value = substr($value, 0, 16);
57
        $this->updateAttributes(
58
            array(
59
                'value'=>$value
60
            )
61
        );
62
    }
63
64
    /**
65
     * Get the necessary javascript for this datepicker
66
     * @return string
67
     */
68
    private function getElementJS()
69
    {
70
        $js = null;
71
        $id = $this->getAttribute('id');
72
        //timeFormat: 'hh:mm'
73
        $js .= "<script>
74
            $(function() {
75
                $('#$id').hide().datetimepicker({
76
                    defaultDate: '" . $this->getValue() . "',
77
                    dateFormat: 'yy-mm-dd',
78
                    timeFormat: 'HH:mm',
79
                    altField: '#{$id}_alt',
80
                    altFormat: \"" . get_lang('DateFormatLongNoDayJS') . "\",
81
                    altTimeFormat: \"" . get_lang('TimeFormatNoSecJS') . "\",
82
                    altSeparator: \" " . get_lang('AtTime') . " \",
83
                    altFieldTimeOnly: false,
84
                    showOn: 'both',
85
                    buttonImage: '" . Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true) . "',
86
                    buttonImageOnly: true,
87
                    buttonText: '" . get_lang('SelectDate') . "',
88
                    changeMonth: true,
89
                    changeYear: true
90
                });
91
            });
92
        </script>";
93
94
        return $js;
95
    }
96
97
    /**
98
     * @param string $layout
99
     *
100
     * @return string
101
     */
102 View Code Duplication
    public function getTemplate($layout)
103
    {
104
        $size = $this->getColumnsSize();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $size is correct as $this->getColumnsSize() (which targets HTML_QuickForm_text::getColumnsSize()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
105
        $id = $this->getAttribute('id');
106
        $value = $this->getValue();
107
108
        if (empty($size)) {
109
            $sizeTemp = $this->getInputSize();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $sizeTemp is correct as $this->getInputSize() (which targets HTML_QuickForm_text::getInputSize()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
110
            if (empty($size)) {
111
                $sizeTemp = 8;
112
            }
113
            $size = array(2, $sizeTemp, 2);
114
        } else {
115
            if (is_array($size)) {
116
                if (count($size) != 3) {
117
                    $sizeTemp = $this->getInputSize();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $sizeTemp is correct as $this->getInputSize() (which targets HTML_QuickForm_text::getInputSize()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
118
                    if (empty($size)) {
119
                        $sizeTemp = 8;
120
                    }
121
                    $size = array(2, $sizeTemp, 2);
122
                }
123
                // else just keep the $size array as received
124
            } else {
125
                $size = array(2, intval($size), 2);
126
            }
127
        }
128
129
        if (!empty($value)) {
130
            $value = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
131
        }
132
133
        switch ($layout) {
134
            case FormValidator::LAYOUT_INLINE:
135
                return '
136
                <div class="form-group {error_class}">
137
                    <label {label-for} >
138
                        <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
139
                        {label}
140
                    </label>
141
142
                    {element}
143
                </div>';
144
                break;
145
            case FormValidator::LAYOUT_HORIZONTAL:
146
                return '
147
                <div class="form-group {error_class}">
148
                    <label {label-for} class="col-sm-'.$size[0].' control-label" >
149
                        <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
150
                        {label}
151
                    </label>
152
                    <div class="col-sm-'.$size[1].'">
153
                        {icon}
154
155
                        {element}
156
157
                        <!-- BEGIN label_2 -->
158
                            <p class="help-block">{label_2}</p>
159
                        <!-- END label_2 -->
160
161
                        <!-- BEGIN error -->
162
                            <span class="help-inline">{error}</span>
163
                        <!-- END error -->
164
                    </div>
165
                    <div class="col-sm-'.$size[2].'">
166
                        <!-- BEGIN label_3 -->
167
                            {label_3}
168
                        <!-- END label_3 -->
169
                    </div>
170
                </div>';
171
                break;
172
            case FormValidator::LAYOUT_BOX_NO_LABEL:
173
                return '{element}';
174
                break;
175
        }
176
    }
177
178
}
179