Passed
Push — master ( ad6b2a...ec8fd5 )
by Julito
28:29 queued 12s
created

DatePicker::getElementJS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 62
rs 9.6666
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
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Form element to select a date.
7
 *
8
 * Class DatePicker
9
 */
10
class DatePicker extends HTML_QuickForm_text
11
{
12
    /**
13
     * @param string       $elementName
14
     * @param string|array $elementLabel
15
     * @param array        $attributes
16
     */
17
    public function __construct($elementName, $elementLabel = null, $attributes = null)
18
    {
19
        if (!isset($attributes['id'])) {
20
            $attributes['id'] = $elementName;
21
        }
22
        $attributes['class'] = 'form-control';
23
24
        parent::__construct($elementName, $elementLabel, $attributes);
25
        $this->_appendName = true;
26
    }
27
28
    /**
29
     * HTML code to display this datepicker.
30
     *
31
     * @return string
32
     */
33
    public function toHtml()
34
    {
35
        if ($this->_flagFrozen) {
36
            return $this->getFrozenHtml();
37
        }
38
39
        $id = $this->getAttribute('id');
40
        $value = $this->getValue();
41
42
        if (!empty($value)) {
43
            $value = api_format_date($value, DATE_FORMAT_LONG_NO_DAY);
44
        }
45
46
        return '
47
            <div id="'.$id.'" class="input-group mb-3">
48
                <input '.$this->_getAttrString($this->_attributes).'
49
                    class="form-control" type="text" value="'.$value.'" data-input>
50
                <div class="input-group-prepend" id="button-addon3">
51
                    <button class="btn btn-outline-secondary"  type="button" data-toggle>
52
                        <i class="fas fa-calendar-alt"></i>
53
                    </button>
54
                    <button class="btn btn-outline-secondary" type="button" data-clear>
55
                        <i class="fas fa-times"></i>
56
                    </button>
57
              </div>
58
            </div>
59
        '.$this->getElementJS();
60
    }
61
62
    /**
63
     * @param string $value
64
     */
65
    public function setValue($value)
66
    {
67
        $value = substr($value, 0, 16);
68
        $this->updateAttributes(
69
            [
70
                'value' => $value,
71
            ]
72
        );
73
    }
74
75
    /**
76
     * Get the necessary javascript for this datepicker.
77
     *
78
     * @return string
79
     */
80
    private function getElementJS()
81
    {
82
        $js = null;
83
        $id = $this->getAttribute('id');
84
        //timeFormat: 'hh:mm'
85
        $js .= "<script>
86
            $(function() {
87
                var config = {
88
                    altInput: true,
89
                    altFormat: '".get_lang('F d, Y')."',
90
                    enableTime: false,
91
                    dateFormat: 'Y-m-d',
92
                    wrap: true
93
                };
94
                $('#{$id}').flatpickr(config);
95
             });
96
        </script>";
97
98
        return $js;
99
100
        $js .= "<script>
0 ignored issues
show
Unused Code introduced by
AssignConcatNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
101
            $(function() {
102
                var txtDate = $('#$id'),
103
                    inputGroup = txtDate.parents('.input-group'),
104
                    txtDateAlt = $('#{$id}_alt'),
105
                    txtDateAltText = $('#{$id}_alt_text');
106
107
                txtDate
108
                    .hide()
109
                    .datepicker({
110
                        defaultDate: '".$this->getValue()."',
111
                        dateFormat: 'yy-mm-dd',
112
                        altField: '#{$id}_alt',
113
                        altFormat: \"".get_lang('MM dd, yy')."\",
114
                        showOn: 'both',
115
                        buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
116
                        buttonImageOnly: true,
117
                        buttonText: '".get_lang('Select date')."',
118
                        changeMonth: true,
119
                        changeYear: true,
120
                        yearRange: 'c-60y:c+5y'
121
                    })
122
                    .on('change', function (e) {
123
                        txtDateAltText.text(txtDateAlt.val());
124
                    });
125
126
                txtDateAltText.on('click', function () {
127
                    txtDate.datepicker('show');
128
                });
129
130
                inputGroup
131
                    .find('button')
132
                    .on('click', function (e) {
133
                        e.preventDefault();
134
135
                        $('#$id, #{$id}_alt').val('');
136
                        $('#{$id}_alt_text').html('');
137
                    });
138
            });
139
        </script>";
140
141
        return $js;
142
    }
143
}
144