Completed
Push — master ( 2eb9e0...6fec5f )
by Daniel
02:30
created

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