Completed
Push — master ( 0cd0d8...e93819 )
by Daniel
03:27
created

DomDynamicSelectByDanielGP   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 36
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 175
rs 8.8

13 Methods

Rating   Name   Duplication   Size   Complexity  
A buildSelectId() 0 8 2
A calculateSelectOptionsSize() 0 8 3
A calculateSelectOptionsSizeForced() 0 10 3
A eventOnChange() 0 11 3
A featureArraySimpleDirect() 0 7 2
A featureArraySimpleTranslated() 0 12 2
A featureArrayAssociative() 0 7 2
A setArrayToSelectNotReadOnly() 0 16 2
A setOptionGroupEnd() 0 9 3
A setOptionGroupFooterHeader() 0 13 3
B setOptionSelected() 0 18 6
A setOptionStyle() 0 7 2
A setOptionsForSelect() 0 18 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
        $tempString = '';
0 ignored issues
show
Unused Code introduced by
$tempString is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
        if (in_array('autosubmit', $featArray)) {
79
            return ' onchange="javascript:' . $featArray['additional_javascript_action'] . 'submit();"';
80
        }
81
        if (in_array('additional_javascript_action', $featArray)) {
82
            return ' onchange="javascript:' . $featArray['additional_javascript_action'] . '"';
83
        }
84
        return '';
85
    }
86
87
    private function featureArraySimpleDirect($featArray, $identifier)
88
    {
89
        if (in_array($identifier, $featArray)) {
90
            return implode(' ', $identifier, '="', $identifier, '"');
91
        }
92
        return '';
93
    }
94
95
    private function featureArraySimpleTranslated($featArray, $identifier)
96
    {
97
        $translation = [
98
            'hidden'       => ' style="visibility: hidden;',
99
            'multiselect'  => ' multiple="multiple"',
100
            'include_null' => '<option value="NULL">&nbsp;</option>',
101
        ];
102
        if (in_array($identifier, $featArray)) {
103
            return $translation[$identifier];
104
        }
105
        return '';
106
    }
107
108
    private function featureArrayAssociative($featArray, $identifier)
109
    {
110
        if (in_array($identifier, $featArray)) {
111
            return ' ' . $identifier . '="' . $featArray[$identifier] . '"';
112
        }
113
        return '';
114
    }
115
116
    protected function setArrayToSelectNotReadOnly($aElements, $sDefaultValue, $selectName, $featArray = null)
117
    {
118
        if (!is_array($featArray)) {
119
            $featArray = [];
120
        }
121
        return '<select name="' . $selectName . '" '
122
                . 'id="' . $this->buildSelectId($selectName, $featArray) . '" '
123
                . 'size="' . $this->calculateSelectOptionsSize($aElements, $featArray) . '"'
124
                . $this->eventOnChange($featArray)
125
                . $this->featureArraySimpleDirect($featArray, 'disabled')
126
                . $this->featureArraySimpleTranslated($featArray, 'hidden')
127
                . $this->featureArraySimpleTranslated($featArray, 'multiselect')
128
                . $this->featureArrayAssociative($featArray, 'style')
129
                . '>'
130
                . $this->setOptionsForSelect($aElements, $sDefaultValue, $featArray) . '</select>';
131
    }
132
133
    private function setOptionGroupEnd($crtGroup, $featArray)
134
    {
135
        if (isset($featArray['grouping'])) {
136
            if ($crtGroup != '') {
137
                return '</optgroup>';
138
            }
139
        }
140
        return '';
141
    }
142
143
    private function setOptionGroupFooterHeader($featArray, $crtValue, $crtGroup)
144
    {
145
        $sReturn = [];
146
        if (isset($featArray['grouping'])) {
147
            $tempString = substr($crtValue, 0, strpos($crtValue, $featArray['grouping']) + 1);
148
            if ($crtGroup != $tempString) {
149
                $sReturn[] = $this->setOptionGroupEnd($crtGroup, $featArray);
150
                $crtGroup  = $tempString;
151
                $sReturn[] = '<optgroup label="' . str_replace($featArray['grouping'], '', $crtGroup) . '">';
152
            }
153
        }
154
        return ['crtGroup' => $crtGroup, 'groupFooterHeader' => implode('', $sReturn)];
155
    }
156
157
    private function setOptionSelected($optionValue, $sDefaultValue, $featArray)
158
    {
159
        if (is_array($sDefaultValue)) {
160
            if (in_array($optionValue, $sDefaultValue)) {
161
                return ' selected="selected"';
162
            }
163
        }
164
        if (strcasecmp($optionValue, $sDefaultValue) === 0) {
165
            return ' selected="selected"';
166
        }
167
        if (isset($featArray['defaultValue_isSubstring'])) {
168
            $defaultValueArray = explode($featArray['defaultValue_isSubstring'], $sDefaultValue);
169
            if (in_array($optionValue, $defaultValueArray)) {
170
                return ' selected="selected"';
171
            }
172
        }
173
        return '';
174
    }
175
176
    private function setOptionStyle($featArray)
177
    {
178
        if (array_key_exists('styleForOption', $featArray)) {
179
            return ' style="' . $featArray['styleForOption'] . '"';
180
        }
181
        return '';
182
    }
183
184
    /**
185
     * Creates all the child tags required to populate a SELECT tag
186
     *
187
     * @param array $aElements
188
     * @param string|array $sDefaultValue
189
     * @param array $featArray
190
     * @return string
191
     */
192
    private function setOptionsForSelect($aElements, $sDefaultValue, $featArray = null)
193
    {
194
        if (is_null($featArray)) {
195
            $featArray = [];
196
        }
197
        $sReturn  = [];
198
        $crtGroup = null;
199
        foreach ($aElements as $key => $value) {
200
            $aFH       = $this->setOptionGroupFooterHeader($featArray, $value, $crtGroup);
201
            $crtGroup  = $aFH['crtGroup'];
202
            $sReturn[] = $aFH['groupFooterHeader']
203
                    . '<option value="' . $key . '"' . $this->setOptionSelected($key, $sDefaultValue, $featArray)
204
                    . $this->setOptionStyle($featArray) . '>'
205
                    . str_replace(['&', $crtGroup], ['&amp;', ''], $value) . '</option>';
206
        }
207
        $sReturn[] = $this->setOptionGroupEnd($crtGroup, $featArray);
208
        return $this->featureArraySimpleTranslated($featArray, 'include_null') . implode('', $sReturn);
209
    }
210
}
211