calculateSelectOptionsSizeForced()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
    /**
40
     *
41
     * @param string $selectName
42
     * @param array $featArray
43
     * @return string
44
     */
45
    protected function buildSelectId($selectName, $featArray)
46
    {
47
        $selectId = str_replace(['[', ']'], ['', ''], $selectName);
48
        if (isset($featArray['id_no'])) {
49
            $selectId .= $featArray['id_no'];
50
        }
51
        return $selectId;
52
    }
53
54
    /**
55
     * Calculate the optimal for all options within a select tag
56
     *
57
     * @param array $aElements
58
     * @param array $aFeatures
59
     * @return string|int
60
     */
61
    private function calculateSelectOptionsSize($aElements, $aFeatures = [])
62
    {
63
        $selectSize = $this->calculateSelectOptionsSizeForced($aElements, $aFeatures);
64
        if ((in_array('include_null', $aFeatures)) && ($selectSize != '1')) {
65
            $selectSize++;
66
        }
67
        return $selectSize;
68
    }
69
70
    /**
71
     *
72
     * @param array $aElements
73
     * @param array $aFeatures
74
     * @return int
75
     */
76
    private function calculateSelectOptionsSizeForced($aElements, $aFeatures = [])
77
    {
78
        if (isset($aFeatures['size'])) {
79
            if ($aFeatures['size'] == 0) {
80
                return count($aElements);
81
            }
82
            return min(count($aElements), $aFeatures['size']);
83
        }
84
        return 1;
85
    }
86
87
    /**
88
     *
89
     * @param array $featArray
90
     * @return string
91
     */
92
    private function eventOnChange($featArray)
93
    {
94
        $sReturn = [];
95
        if (array_key_exists('additional_javascript_action', $featArray)) {
96
            $sReturn[] = $featArray['additional_javascript_action'];
97
        }
98
        if (array_key_exists('autosubmit', $featArray)) {
99
            $sReturn[] = 'submit();';
100
        }
101
        if ($sReturn != []) {
102
            return ' onchange="javascript:' . implode('', $sReturn) . '"';
103
        }
104
        return '';
105
    }
106
107
    private function featureArraySimpleTranslated($featArray, $identifier)
108
    {
109
        $translation = [
110
            'disabled'       => ' disabled',
111
            'hidden'         => ' style="visibility: hidden;',
112
            'include_null'   => '<option value="NULL">&nbsp;</option>',
113
            'multiselect'    => ' multiple="multiple"',
114
            'style'          => null,
115
            'styleForOption' => null,
116
        ];
117
        if (array_key_exists($identifier, $featArray)) {
118
            if (is_null($translation[$identifier])) {
119
                return ' ' . $identifier . '="' . $featArray[$identifier] . '"';
120
            }
121
            return $translation[$identifier];
122
        }
123
        return '';
124
    }
125
126
    private function normalizeFeatureArray($featArray)
127
    {
128
        $nonAsociative = ['autosubmit', 'disabled', 'hidden', 'include_null', 'multiselect'];
129
        foreach ($featArray as $key => $value) {
130
            if (in_array($value, $nonAsociative)) {
131
                $featArray[$value] = $value;
132
                unset($featArray[$key]);
133
            }
134
        }
135
        return $featArray;
136
    }
137
138
    protected function setArrayToSelectNotReadOnly($aElements, $sDefaultValue, $selectName, $ftArray = null)
139
    {
140
        if (!is_array($ftArray)) {
141
            $ftArray = [];
142
        }
143
        $featArray = $this->normalizeFeatureArray($ftArray);
144
        return '<select name="' . $selectName . '" '
145
            . 'id="' . $this->buildSelectId($selectName, $featArray) . '" '
146
            . 'size="' . $this->calculateSelectOptionsSize($aElements, $featArray) . '"'
147
            . $this->eventOnChange($featArray)
148
            . $this->featureArraySimpleTranslated($featArray, 'disabled')
149
            . $this->featureArraySimpleTranslated($featArray, 'hidden')
150
            . $this->featureArraySimpleTranslated($featArray, 'multiselect')
151
            . $this->featureArraySimpleTranslated($featArray, 'style')
152
            . '>' . $this->setOptionsForSelect($aElements, $sDefaultValue, $featArray) . '</select>';
153
    }
154
155
    private function setOptionGroupEnd($crtGroup, $featArray)
156
    {
157
        if (isset($featArray['grouping'])) {
158
            if ($crtGroup != '') {
159
                return '</optgroup>';
160
            }
161
        }
162
        return '';
163
    }
164
165
    private function setOptionGroupFooterHeader($featArray, $crtValue, $crtGroup)
166
    {
167
        $sReturn = [];
168
        if (isset($featArray['grouping'])) {
169
            $tempString = substr($crtValue, 0, strpos($crtValue, $featArray['grouping']) + 1);
170
            if ($crtGroup != $tempString) {
171
                $sReturn[] = $this->setOptionGroupEnd($crtGroup, $featArray);
172
                $crtGroup  = $tempString;
173
                $sReturn[] = '<optgroup label="' . str_replace($featArray['grouping'], '', $crtGroup) . '">';
174
            }
175
        }
176
        return ['crtGroup' => $crtGroup, 'groupFooterHeader' => implode('', $sReturn)];
177
    }
178
179
    private function setOptionSelected($optionValue, $sDefaultValue)
180
    {
181
        if (is_array($sDefaultValue)) {
182
            if (in_array($optionValue, $sDefaultValue)) {
183
                return ' selected="selected"';
184
            }
185
        } elseif (strcasecmp($optionValue, $sDefaultValue) === 0) {
186
            return ' selected="selected"';
187
        }
188
        return '';
189
    }
190
191
    /**
192
     * Creates all the child tags required to populate a SELECT tag
193
     *
194
     * @param array $aElements
195
     * @param string|array $sDefaultValue
196
     * @param array $featArray
197
     * @return string
198
     */
199
    private function setOptionsForSelect($aElements, $sDefaultValue, $featArray = null)
200
    {
201
        if (is_null($featArray)) {
202
            $featArray = [];
203
        }
204
        $sReturn  = [];
205
        $crtGroup = null;
206
        foreach ($aElements as $key => $value) {
207
            $aFH       = $this->setOptionGroupFooterHeader($featArray, $value, $crtGroup);
208
            $crtGroup  = $aFH['crtGroup'];
209
            $sReturn[] = $aFH['groupFooterHeader']
210
                . '<option value="' . $key . '"' . $this->setOptionSelected($key, $sDefaultValue)
211
                . $this->featureArraySimpleTranslated($featArray, 'styleForOption') . '>'
212
                . str_replace(['&', $crtGroup], ['&amp;', ''], $value) . '</option>';
213
        }
214
        $sReturn[] = $this->setOptionGroupEnd($crtGroup, $featArray);
215
        return $this->featureArraySimpleTranslated($featArray, 'include_null') . implode('', $sReturn);
216
    }
217
}
218