Completed
Push — master ( 0fd38e...0cd0d8 )
by Daniel
06:06
created

calculateSelectOptionsSizeForced()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 3
eloc 6
nc 3
nop 2
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
        if (is_array($aElements)) {
58
            if (!is_array($aFeatures)) {
59
                $aFeatures = [];
60
            }
61
            $selectSize = $this->calculateSelectOptionsSizeForced($aElements, $aFeatures);
62
            if ((in_array('include_null', $aFeatures)) && ($selectSize != '1')) {
63
                $selectSize++;
64
            }
65
            return $selectSize;
66
        }
67
        return '';
68
    }
69
70
    private function calculateSelectOptionsSizeForced($aElements, $aFeatures = [])
71
    {
72
        if (isset($aFeatures['size'])) {
73
            if ($aFeatures['size'] == 0) {
74
                return count($aElements);
75
            }
76
            return min(count($aElements), $aFeatures['size']);
77
        }
78
        return 1;
79
    }
80
81
    private function eventOnChange($featArray)
82
    {
83
        $tempString = '';
84
        if (in_array('autosubmit', $featArray)) {
85
            return ' onchange="javascript:' . $tempString . 'submit();"';
86
        }
87
        if (in_array('additional_javascript_action', $featArray)) {
88
            return ' onchange="javascript:' . $featArray['additional_javascript_action'] . '"';
89
        }
90
        return '';
91
    }
92
93
    private function featureArraySimpleDirect($featArray, $identifier)
94
    {
95
        if (in_array($identifier, $featArray)) {
96
            return implode(' ', $identifier, '="', $identifier, '"');
97
        }
98
        return '';
99
    }
100
101
    private function featureArraySimpleTranslated($featArray, $identifier)
102
    {
103
        $translation = [
104
            'hidden'      => ' style="visibility: hidden;',
105
            'multiselect' => ' multiple="multiple"',
106
        ];
107
        if (in_array($identifier, $featArray)) {
108
            return $translation[$identifier];
109
        }
110
        return '';
111
    }
112
113
    private function featureArrayAssociative($featArray, $identifier)
114
    {
115
        if (in_array($identifier, $featArray)) {
116
            return ' ' . $identifier . '="' . $featArray[$identifier] . '"';
117
        }
118
        return '';
119
    }
120
121
    protected function setArrayToSelectNotReadOnly($aElements, $sDefaultValue, $selectName, $featArray = null)
122
    {
123
        if (!is_array($featArray)) {
124
            $featArray = [];
125
        }
126
        return '<select name="' . $selectName . '" '
127
                . 'id="' . $this->buildSelectId($selectName, $featArray) . '" '
128
                . 'size="' . $this->calculateSelectOptionsSize($aElements, $featArray) . '"'
129
                . $this->eventOnChange($featArray)
130
                . $this->featureArraySimpleDirect($featArray, 'disabled')
131
                . $this->featureArraySimpleTranslated($featArray, 'hidden')
132
                . $this->featureArraySimpleTranslated($featArray, 'multiselect')
133
                . $this->featureArrayAssociative($featArray, 'style')
134
                . '>'
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 setOptionEmptyWithNullValue($featArray)
163
    {
164
        if (in_array('include_null', $featArray)) {
165
            return '<option value="NULL">&nbsp;</option>';
166
        }
167
        return '';
168
    }
169
170
    private function setOptionSelected($optionValue, $sDefaultValue, $featArray)
171
    {
172
        if (is_array($sDefaultValue)) {
173
            if (in_array($optionValue, $sDefaultValue)) {
174
                return ' selected="selected"';
175
            }
176
        }
177
        if (strcasecmp($optionValue, $sDefaultValue) === 0) {
178
            return ' selected="selected"';
179
        }
180
        if (isset($featArray['defaultValue_isSubstring'])) {
181
            $defaultValueArray = explode($featArray['defaultValue_isSubstring'], $sDefaultValue);
182
            if (in_array($optionValue, $defaultValueArray)) {
183
                return ' selected="selected"';
184
            }
185
        }
186
        return '';
187
    }
188
189
    private function setOptionStyle($featArray)
190
    {
191
        if (array_key_exists('styleForOption', $featArray)) {
192
            return ' style="' . $featArray['style'] . '"';
193
        }
194
        return '';
195
    }
196
197
    /**
198
     * Creates all the child tags required to populate a SELECT tag
199
     *
200
     * @param array $aElements
201
     * @param string|array $sDefaultValue
202
     * @param array $featArray
203
     * @return string
204
     */
205
    private function setOptionsForSelect($aElements, $sDefaultValue, $featArray = null)
206
    {
207
        if (is_null($featArray)) {
208
            $featArray = [];
209
        }
210
        $sReturn  = [];
211
        $crtGroup = null;
212
        foreach ($aElements as $key => $value) {
213
            $aFH       = $this->setOptionGroupFooterHeader($featArray, $value, $crtGroup);
214
            $crtGroup  = $aFH['crtGroup'];
215
            $sReturn[] = $aFH['groupFooterHeader']
216
                    . '<option value="' . $key . '"' . $this->setOptionSelected($key, $sDefaultValue, $featArray)
217
                    . $this->setOptionStyle($featArray) . '>'
218
                    . str_replace(['&', $crtGroup], ['&amp;', ''], $value) . '</option>';
219
        }
220
        $sReturn[] = $this->setOptionGroupEnd($crtGroup, $featArray);
221
        return $this->setOptionEmptyWithNullValue($featArray) . implode('', $sReturn);
222
    }
223
}
224