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

SelectAjax::toHtml()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 97
Code Lines 51

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 97
rs 4.6952
cc 8
eloc 51
nc 128
nop 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
* A drop down list with all languages to use with QuickForm
6
*/
7
class SelectAjax extends HTML_QuickForm_select
8
{
9
    /**
10
     * Class constructor
11
     */
12
    public function __construct($elementName = null, $elementLabel = null, $options = null, $attributes = null)
13
    {
14
        parent::__construct($elementName, $elementLabel, $options, $attributes);
15
    }
16
17
    /**
18
     * The ajax call must contain an array of id and text
19
     * @return string
20
     */
21
    public function toHtml()
22
    {
23
        $html = api_get_asset('select2/dist/js/select2.min.js');
24
25
        $iso = api_get_language_isocode(api_get_interface_language());
26
        $languageCondition = '';
27
28
        if (file_exists(api_get_path(SYS_PATH) . "web/assets/select2/dist/js/i18n/$iso.js")) {
29
            $html .= api_get_asset("select2/dist/js/i18n/$iso.js");
30
            $languageCondition = "language: '$iso',";
31
        }
32
33
        $html .= api_get_css(api_get_path(WEB_PATH).'web/assets/select2/dist/css/select2.min.css');
34
35
        $formatResult = $this->getAttribute('formatResult');
36
37
        $formatCondition = null;
38
39
        if (!empty($formatResult)) {
40
            $formatCondition = ',
41
                templateResult : '.$formatResult.',
42
                templateSelection : '.$formatResult;
43
        }
44
45
        $width = 'element';
46
        $givenWidth = '100%';
47
        if (!empty($givenWidth)) {
48
            $width = $givenWidth;
49
        }
50
51
        //Get the minimumInputLength for select2
52
        $minimumInputLength = $this->getAttribute('minimumInputLength') > 3 ?
53
            $this->getAttribute('minimumInputLength') :
54
            3
55
        ;
56
57
        $plHolder = $this->getAttribute('placeholder');
58
        if (empty($plHolder)) {
59
            $plHolder = get_lang('SelectAnOption');
60
        }
61
62
        $id = $this->getAttribute('id');
63
64
        if (empty($id)) {
65
            $id = $this->getAttribute('name');
66
            $this->setAttribute('id', $id);
67
        }
68
69
        $url = $this->getAttribute('url');
70
71
        if (!$url) {
72
            $url = $this->getAttribute('url_function');
73
        } else {
74
            $url = "'$url'";
75
        }
76
77
        $html .= <<<JS
78
            <script>
79
                $(function(){
80
                    $('#{$this->getAttribute('id')}').select2({
81
                        $languageCondition
82
                        placeholder: '$plHolder',
83
                        allowClear: true,
84
                        width: '$width',
85
                        minimumInputLength: '$minimumInputLength',
86
                        ajax: {
87
                            url: $url,
88
                            dataType: 'json',
89
                            data: function(params) {
90
                                return {
91
                                    q: params.term, // search term
92
                                    page_limit: 10,
93
                                };
94
                            },
95
                            processResults: function (data, page) {
96
                                //parse the results into the format expected by Select2
97
                                return {
98
                                    results: data.items
99
                                };
100
                            }
101
                            $formatCondition
102
                        }
103
                    });
104
                });
105
            </script>
106
JS;
107
108
        $this->removeAttribute('formatResult');
109
        $this->removeAttribute('minimumInputLength');
110
        $this->removeAttribute('placeholder');
111
        $this->removeAttribute('class');
112
        $this->removeAttribute('url');
113
        $this->removeAttribute('url_function');
114
        $this->setAttribute('style', 'width: 100%;');
115
116
        return parent::toHtml() . $html;
117
    }
118
}
119