Completed
Push — master ( 1dd330...3f2151 )
by Daniel
04:08
created

DomDynamicSelectByDanielGP   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 33
c 6
b 0
f 0
lcom 1
cbo 0
dl 0
loc 166
rs 9.4

11 Methods

Rating   Name   Duplication   Size   Complexity  
A buildSelectId() 0 8 2
A setOptionGroupEnd() 0 9 3
A calculateSelectOptionsSize() 0 8 3
A calculateSelectOptionsSizeForced() 0 10 3
A eventOnChange() 0 14 4
A featureArraySimpleTranslated() 0 18 3
A normalizeFeatureArray() 0 11 3
A setArrayToSelectNotReadOnly() 0 16 2
A setOptionGroupFooterHeader() 0 13 3
A setOptionSelected() 0 11 4
A setOptionsForSelect() 0 19 3
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\common_lib;
30
31
/**
32
 * DOM component functions
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait DomDynamicSelectByDanielGP
37
{
38
39
    protected function buildSelectId($selectName, $featArray)
40
    {
41
        $selectId = str_replace(['[', ']'], ['', ''], $selectName);
42
        if (isset($featArray['id_no'])) {
43
            $selectId .= $featArray['id_no'];
44
        }
45
        return $selectId;
46
    }
47
48
    /**
49
     * Calculate the optimal for all options within a select tag
50
     *
51
     * @param array $aElements
52
     * @param array $aFeatures
53
     * @return string|int
54
     */
55
    private function calculateSelectOptionsSize($aElements, $aFeatures = [])
56
    {
57
        $selectSize = $this->calculateSelectOptionsSizeForced($aElements, $aFeatures);
58
        if ((in_array('include_null', $aFeatures)) && ($selectSize != '1')) {
59
            $selectSize++;
60
        }
61
        return $selectSize;
62
    }
63
64
    private function calculateSelectOptionsSizeForced($aElements, $aFeatures = [])
65
    {
66
        if (isset($aFeatures['size'])) {
67
            if ($aFeatures['size'] == 0) {
68
                return count($aElements);
69
            }
70
            return min(count($aElements), $aFeatures['size']);
71
        }
72
        return 1;
73
    }
74
75
    private function eventOnChange($featArray)
76
    {
77
        $sReturn = null;
78
        if (array_key_exists('additional_javascript_action', $featArray)) {
79
            $sReturn[] = $featArray['additional_javascript_action'];
80
        }
81
        if (array_key_exists('autosubmit', $featArray)) {
82
            $sReturn[] = 'submit();';
83
        }
84
        if (is_array($sReturn)) {
85
            return ' onchange="javascript:' . implode('', $sReturn) . '"';
86
        }
87
        return $sReturn;
88
    }
89
90
    private function featureArraySimpleTranslated($featArray, $identifier)
91
    {
92
        $translation = [
93
            'disabled'       => ' disabled',
94
            'hidden'         => ' style="visibility: hidden;',
95
            'include_null'   => '<option value="NULL">&nbsp;</option>',
96
            'multiselect'    => ' multiple="multiple"',
97
            'style'          => null,
98
            'styleForOption' => null,
99
        ];
100
        if (array_key_exists($identifier, $featArray)) {
101
            if (is_null($translation[$identifier])) {
102
                return ' ' . $identifier . '="' . $featArray[$identifier] . '"';
103
            }
104
            return $translation[$identifier];
105
        }
106
        return '';
107
    }
108
109
    private function normalizeFeatureArray($featArray)
110
    {
111
        $nonAsociative = ['autosubmit', 'disabled', 'hidden', 'include_null', 'multiselect'];
112
        foreach ($featArray as $key => $value) {
113
            if (in_array($value, $nonAsociative)) {
114
                $featArray[$value] = $value;
115
                unset($featArray[$key]);
116
            }
117
        }
118
        return $featArray;
119
    }
120
121
    protected function setArrayToSelectNotReadOnly($aElements, $sDefaultValue, $selectName, $featArray = null)
122
    {
123
        if (!is_array($featArray)) {
124
            $featArray = [];
125
        }
126
        $featArray = $this->normalizeFeatureArray($featArray);
127
        return '<select name="' . $selectName . '" '
128
                . 'id="' . $this->buildSelectId($selectName, $featArray) . '" '
129
                . 'size="' . $this->calculateSelectOptionsSize($aElements, $featArray) . '"'
130
                . $this->eventOnChange($featArray)
131
                . $this->featureArraySimpleTranslated($featArray, 'disabled')
132
                . $this->featureArraySimpleTranslated($featArray, 'hidden')
133
                . $this->featureArraySimpleTranslated($featArray, 'multiselect')
134
                . $this->featureArraySimpleTranslated($featArray, 'style')
135
                . '>' . $this->setOptionsForSelect($aElements, $sDefaultValue, $featArray) . '</select>';
136
    }
137
138
    private function setOptionGroupEnd($crtGroup, $featArray)
139
    {
140
        if (isset($featArray['grouping'])) {
141
            if ($crtGroup != '') {
142
                return '</optgroup>';
143
            }
144
        }
145
        return '';
146
    }
147
148
    private function setOptionGroupFooterHeader($featArray, $crtValue, $crtGroup)
149
    {
150
        $sReturn = [];
151
        if (isset($featArray['grouping'])) {
152
            $tempString = substr($crtValue, 0, strpos($crtValue, $featArray['grouping']) + 1);
153
            if ($crtGroup != $tempString) {
154
                $sReturn[] = $this->setOptionGroupEnd($crtGroup, $featArray);
155
                $crtGroup  = $tempString;
156
                $sReturn[] = '<optgroup label="' . str_replace($featArray['grouping'], '', $crtGroup) . '">';
157
            }
158
        }
159
        return ['crtGroup' => $crtGroup, 'groupFooterHeader' => implode('', $sReturn)];
160
    }
161
162
    private function setOptionSelected($optionValue, $sDefaultValue, $featArray)
0 ignored issues
show
Unused Code introduced by
The parameter $featArray is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
163
    {
164
        if (is_array($sDefaultValue)) {
165
            if (in_array($optionValue, $sDefaultValue)) {
166
                return ' selected="selected"';
167
            }
168
        } elseif (strcasecmp($optionValue, $sDefaultValue) === 0) {
169
            return ' selected="selected"';
170
        }
171
        return '';
172
    }
173
174
    /**
175
     * Creates all the child tags required to populate a SELECT tag
176
     *
177
     * @param array $aElements
178
     * @param string|array $sDefaultValue
179
     * @param array $featArray
180
     * @return string
181
     */
182
    private function setOptionsForSelect($aElements, $sDefaultValue, $featArray = null)
183
    {
184
        if (is_null($featArray)) {
185
            $featArray = [];
186
        }
187
        $sReturn  = [];
188
        $crtGroup = null;
189
        foreach ($aElements as $key => $value) {
190
            $aFH       = $this->setOptionGroupFooterHeader($featArray, $value, $crtGroup);
191
            $crtGroup  = $aFH['crtGroup'];
192
            $sReturn[] = $aFH['groupFooterHeader']
193
                    . '<option value="' . $key . '"'
194
                    . $this->setOptionSelected($key, $sDefaultValue, $featArray)
195
                    . $this->featureArraySimpleTranslated($featArray, 'styleForOption') . '>'
196
                    . str_replace(['&', $crtGroup], ['&amp;', ''], $value) . '</option>';
197
        }
198
        $sReturn[] = $this->setOptionGroupEnd($crtGroup, $featArray);
199
        return $this->featureArraySimpleTranslated($featArray, 'include_null') . implode('', $sReturn);
200
    }
201
}
202