Completed
Push — master ( f7f475...9b2539 )
by Daniel
02:18
created

MySQLiByDanielGPnumbers::getFieldOutputTT()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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