Completed
Push — master ( 9b2539...51b667 )
by Daniel
02:30
created

getFieldOutputEnumSetReadOnly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 9
nc 1
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
 * Usefull functions to get quick MySQL content
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait MySQLiByDanielGPnumbers
37
{
38
39
    use DomComponentsByDanielGP;
40
41
    /**
42
     * Creats an input for ENUM or SET if marked Read-Only
43
     *
44
     * @param array $val
45
     * @param array $adnlThings
46
     * @return string
47
     */
48
    protected function getFieldOutputEnumSetReadOnly($val, $adnlThings)
49
    {
50
        $inputFeatures = [
51
            'name'     => $val['COLUMN_NAME'] . $adnlThings['suffix'],
52
            'id'       => $val['COLUMN_NAME'],
53
            'readonly' => 'readonly',
54
            'class'    => 'input_readonly',
55
            'size'     => 50,
56
            'value'    => $this->getFieldValue($val),
57
        ];
58
        return $this->setStringIntoShortTag('input', $inputFeatures);
59
    }
60
61
    /**
62
     * Builds output as text input type
63
     *
64
     * @param array $value
65
     * @param integer $szN
66
     * @param array $iar
67
     * @return string
68
     */
69
    protected function getFieldOutputTT($value, $szN, $iar = [])
70
    {
71
        $inAdtnl = [
72
            'id'        => $value['COLUMN_NAME'],
73
            'maxlength' => $szN,
74
            'name'      => $value['COLUMN_NAME'],
75
            'size'      => $szN,
76
            'type'      => 'text',
77
            'value'     => $this->getFieldValue($value),
78
        ];
79
        if ($iar !== []) {
80
            $inAdtnl = array_merge($inAdtnl, $iar);
81
        }
82
        return $this->setStringIntoShortTag('input', $inAdtnl);
83
    }
84
85
    /**
86
     * Returns given value for a field from REQUEST global variable
87
     *
88
     * @param array $details
89
     * @return string
90
     */
91
    protected function getFieldValue($details)
92
    {
93
        $this->initializeSprGlbAndSession();
94
        $rqCN = $this->tCmnRequest->request->get($details['COLUMN_NAME']);
95
        if (!is_null($rqCN)) {
96
            if (($details['IS_NULLABLE'] == 'YES') && ($rqCN == '')) {
97
                return 'NULL';
98
            }
99
            return $rqCN;
100
        }
101
        return $this->getFieldValueWithoutUserInput($details);
102
    }
103
104
    /**
105
     * Handles field value ignoring any input from the user
106
     *
107
     * @param array $details
108
     * @return string
109
     */
110
    private function getFieldValueWithoutUserInput($details)
111
    {
112
        if ($details['COLUMN_DEFAULT'] === null) {
113
            if ($details['IS_NULLABLE'] == 'YES') {
114
                return 'NULL';
115
            }
116
            return '';
117
        }
118
        return $details['COLUMN_DEFAULT'];
119
    }
120
121
    /**
122
     * Prepares the label for inputs
123
     *
124
     * @param array $details
125
     * @param array $features
126
     * @param string $fieldLabel
127
     * @return string
128
     */
129
    protected function setFieldLabel($details, $features, $fieldLabel)
130
    {
131
        $aLabel = ['for' => $details['COLUMN_NAME'], 'id' => $details['COLUMN_NAME'] . '_label'];
132
        if (isset($features['disabled'])) {
133
            if (in_array($details['COLUMN_NAME'], $features['disabled'])) {
134
                $aLabel = array_merge($aLabel, ['style' => 'color: grey;']);
135
            }
136
        }
137
        return $this->setStringIntoTag($fieldLabel, 'label', $aLabel);
138
    }
139
140
    /**
141
     * Returns maximum length for a given MySQL field
142
     *
143
     * @param array $fieldDetails
144
     * @param boolean $outputFormated
145
     * @return array
146
     */
147
    protected function setFieldNumbers($fieldDetails, $outputFormated = false)
148
    {
149
        $sRtrn = $this->setFieldSpecific($fieldDetails);
150
        if ($outputFormated) {
151
            if (is_array($sRtrn)) {
152
                foreach ($sRtrn as $key => $value) {
153
                    $sRtrn[$key] = $this->setNumberFormat($value);
154
                }
155
            }
156
        }
157
        return $sRtrn;
158
    }
159
160
    /**
161
     * Establishes numbers of fields
162
     *
163
     * @param array $fieldDetails
164
     * @return array
165
     */
166
    private function setFieldSpecific($fieldDetails)
167
    {
168
        if (in_array($fieldDetails['DATA_TYPE'], ['char', 'varchar', 'tinytext', 'text', 'mediumtext', 'longtext'])) {
169
            return ['M' => $fieldDetails['CHARACTER_MAXIMUM_LENGTH']];
170
        } elseif (in_array($fieldDetails['DATA_TYPE'], ['decimal', 'numeric'])) {
171
            return ['M' => $fieldDetails['NUMERIC_PRECISION'], 'd' => $fieldDetails['NUMERIC_SCALE']];
172
        } elseif (in_array($fieldDetails['DATA_TYPE'], ['bigint', 'int', 'mediumint', 'smallint', 'tinyint'])) {
173
            return $this->setFldLmtsExact($fieldDetails['DATA_TYPE']);
174
        }
175
        return $this->setFieldSpecificElse($fieldDetails);
176
    }
177
178
    private function setFieldSpecificElse($fieldDetails)
179
    {
180
        $map = ['date' => 10, 'datetime' => 19, 'enum' => 65536, 'set' => 64, 'time' => 8, 'timestamp' => 19];
181
        if (array_key_exists($fieldDetails['DATA_TYPE'], $map)) {
182
            return ['M' => $map[$fieldDetails['DATA_TYPE']]];
183
        }
184
        return ['M' => '???'];
185
    }
186
187
    private function setFldLmtsExact($cTp)
188
    {
189
        $xct     = [
190
            'bigint'    => ['l' => -9223372036854775808, 'L' => 9223372036854775807, 's' => 21, 'sUS' => 20],
191
            'int'       => ['l' => -2147483648, 'L' => 2147483647, 's' => 11, 'sUS' => 10],
192
            'mediumint' => ['l' => -8388608, 'L' => 8388607, 's' => 9, 'sUS' => 8],
193
            'smallint'  => ['l' => -32768, 'L' => 32767, 's' => 6, 'sUS' => 5],
194
            'tinyint'   => ['l' => -128, 'L' => 127, 's' => 4, 'sUS' => 3],
195
        ];
196
        $aReturn = null;
197
        if (array_key_exists($cTp, $xct)) {
198
            $aReturn = ['m' => $xct[$cTp]['l'], 'M' => $xct[$cTp]['L'], 'l' => $xct[$cTp]['s']];
199
            if (strpos($cTp, 'unsigned') !== false) {
200
                $aReturn = ['m' => 0, 'M' => ($xct[$cTp]['L'] - $xct[$cTp]['l']), 'l' => $xct[$cTp]['sUS']];
201
            }
202
        }
203
        return $aReturn;
204
    }
205
206
    /**
207
     * Form default buttons
208
     *
209
     * @param array $feat
210
     * @param array $hiddenInfo
211
     * @return string
212
     */
213
    protected function setFormButtons($feat, $hiddenInfo = [])
214
    {
215
        $btn   = [];
216
        $btn[] = '<input type="submit" id="submit" style="margin-left:220px;" value="'
217
                . $this->lclMsgCmn('i18n_Form_ButtonSave') . '" />';
218
        if (isset($feat['insertAndUpdate'])) {
219
            $btn[] = '<input type="hidden" id="insertAndUpdate" name="insertAndUpdate" value="insertAndUpdate" />';
220
        }
221
        if ($hiddenInfo != []) {
222
            foreach ($hiddenInfo as $key => $value) {
223
                $btn[] = '<input type="hidden" id="' . $key . '" name="' . $key . '" value="' . $value . '" />';
224
            }
225
        }
226
        return '<div>' . implode('', $btn) . '</div>';
227
    }
228
229
    protected function setMySQLqueryValidateInputs($prm)
230
    {
231
        $rMap = $this->setMySQLqueryValidationMap();
232
        if (array_key_exists($prm['returnType'], $rMap)) {
233
            $elC = [$prm['NoOfRows'], $rMap[$prm['returnType']]['r'][0], $rMap[$prm['returnType']]['r'][1]];
234
            if (filter_var($elC[0], FILTER_VALIDATE_INT, ['min_range' => $elC[1], 'max_range' => $elC[2]]) === false) {
235
                $msg = $this->lclMsgCmn('i18n_MySQL_QueryResultExpected' . $rMap[$prm['returnType']][2]);
236
                return [false, sprintf($msg, $prm['NoOfColumns'])];
237
            }
238
            $elR = [$prm['NoOfColumns'], $rMap[$prm['returnType']]['c'][0], $rMap[$prm['returnType']]['c'][1]];
239
            if (filter_var($elR[0], FILTER_VALIDATE_INT, ['min_range' => $elR[1], 'max_range' => $elR[2]])) {
240
                return [true, ''];
241
            }
242
            $msg = $this->lclMsgCmn('i18n_MySQL_QueryResultExpected' . $rMap[$prm['returnType']][1]);
243
            return [false, sprintf($msg, $prm['NoOfColumns'])];
244
        }
245
        return [false, $prm['returnType'] . ' is not defined!'];
246
    }
247
248
    private function setMySQLqueryValidationMap()
249
    {
250
        $lngKey = 'full_array_key_numbered_with_record_number_prefix';
251
        return [
252
            'array_first_key_rest_values'         => ['r' => [1, 999999], 'c' => [2, 99], 'AtLeast2ColsResultedOther'],
253
            'array_key_value'                     => ['r' => [1, 999999], 'c' => [2, 2], '2ColumnsResultedOther'],
254
            'array_key_value2'                    => ['r' => [1, 999999], 'c' => [2, 2], '2ColumnsResultedOther'],
255
            'array_key2_value'                    => ['r' => [1, 999999], 'c' => [2, 2], '2ColumnsResultedOther'],
256
            'array_numbered'                      => ['r' => [1, 999999], 'c' => [1, 1], '1ColumnResultedOther'],
257
            'array_pairs_key_value'               => ['r' => [1, 1], 'c' => [1, 99], '1RowManyColumnsResultedOther'],
258
            'full_array_key_numbered'             => ['r' => [1, 999999], 'c' => [1, 99], '1OrMoreRows0Resulted'],
259
            'full_array_key_numbered_with_prefix' => ['r' => [1, 999999], 'c' => [1, 99], '1OrMoreRows0Resulted'],
260
            $lngKey                               => ['r' => [1, 999999], 'c' => [1, 99], '1OrMoreRows0Resulted'],
261
            'value'                               => ['r' => [1, 1], 'c' => [1, 1], '1ResultedOther'],
262
        ];
263
    }
264
}
265