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
|
|
|
* Useful functions to get quick MySQL content |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait MySQLiByDanielGPnumbers |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
use DomComponentsByDanielGP; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Creates 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
|
|
|
foreach ($sRtrn as $key => $value) { |
152
|
|
|
$sRtrn[$key] = $this->setNumberFormat($value); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
return $sRtrn; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Establishes numbers of fields |
160
|
|
|
* |
161
|
|
|
* @param array $fieldDetails |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
private function setFieldSpecific($fieldDetails) |
165
|
|
|
{ |
166
|
|
|
if (in_array($fieldDetails['DATA_TYPE'], ['char', 'varchar', 'tinytext', 'text', 'mediumtext', 'longtext'])) { |
167
|
|
|
return ['M' => $fieldDetails['CHARACTER_MAXIMUM_LENGTH']]; |
168
|
|
|
} elseif (in_array($fieldDetails['DATA_TYPE'], ['decimal', 'numeric'])) { |
169
|
|
|
return ['M' => $fieldDetails['NUMERIC_PRECISION'], 'd' => $fieldDetails['NUMERIC_SCALE']]; |
170
|
|
|
} elseif (in_array($fieldDetails['DATA_TYPE'], ['bigint', 'int', 'mediumint', 'smallint', 'tinyint'])) { |
171
|
|
|
return $this->setFldLmtsExact($fieldDetails['DATA_TYPE'], $fieldDetails['COLUMN_TYPE']); |
172
|
|
|
} |
173
|
|
|
return $this->setFieldSpecificElse($fieldDetails); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* |
178
|
|
|
* @param array $fieldDetails |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
private function setFieldSpecificElse($fieldDetails) |
182
|
|
|
{ |
183
|
|
|
$map = ['date' => 10, 'datetime' => 19, 'enum' => 65536, 'set' => 64, 'time' => 8, 'timestamp' => 19]; |
184
|
|
|
if (array_key_exists($fieldDetails['DATA_TYPE'], $map)) { |
185
|
|
|
return ['M' => $map[$fieldDetails['DATA_TYPE']]]; |
186
|
|
|
} |
187
|
|
|
return ['M' => '???']; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
private function setFldLmtsExact($dTp, $cTp) |
191
|
|
|
{ |
192
|
|
|
$xct = [ |
193
|
|
|
'bigint' => ['l' => -9223372036854775808, 'L' => 9223372036854775807, 's' => 21, 'sUS' => 20], |
194
|
|
|
'int' => ['l' => -2147483648, 'L' => 2147483647, 's' => 11, 'sUS' => 10], |
195
|
|
|
'mediumint' => ['l' => -8388608, 'L' => 8388607, 's' => 9, 'sUS' => 8], |
196
|
|
|
'smallint' => ['l' => -32768, 'L' => 32767, 's' => 6, 'sUS' => 5], |
197
|
|
|
'tinyint' => ['l' => -128, 'L' => 127, 's' => 4, 'sUS' => 3], |
198
|
|
|
]; |
199
|
|
|
$aReturn = null; |
200
|
|
|
if (array_key_exists($dTp, $xct)) { |
201
|
|
|
$aReturn = ['m' => $xct[$dTp]['l'], 'M' => $xct[$dTp]['L'], 'l' => $xct[$dTp]['s']]; |
202
|
|
|
if (strpos($cTp, 'unsigned') !== false) { |
203
|
|
|
$aReturn = ['m' => 0, 'M' => ($xct[$dTp]['L'] - $xct[$dTp]['l']), 'l' => $xct[$dTp]['sUS']]; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
return $aReturn; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Form default buttons |
211
|
|
|
* |
212
|
|
|
* @param array $feat |
213
|
|
|
* @param array $hiddenInfo |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
protected function setFormButtons($feat, $hiddenInfo = []) |
217
|
|
|
{ |
218
|
|
|
$btn = []; |
219
|
|
|
$btn[] = '<input type="submit" id="submit" style="margin-left:220px;" value="' |
220
|
|
|
. $this->lclMsgCmn('i18n_Form_ButtonSave') . '" />'; |
221
|
|
|
if (isset($feat['insertAndUpdate'])) { |
222
|
|
|
$btn[] = '<input type="hidden" id="insertAndUpdate" name="insertAndUpdate" value="insertAndUpdate" />'; |
223
|
|
|
} |
224
|
|
|
if ($hiddenInfo != []) { |
225
|
|
|
foreach ($hiddenInfo as $key => $value) { |
226
|
|
|
$btn[] = '<input type="hidden" id="' . $key . '" name="' . $key . '" value="' . $value . '" />'; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
return '<div>' . implode('', $btn) . '</div>'; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* |
234
|
|
|
* @param array $prm |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
|
|
protected function setMySQLqueryValidateInputs($prm) |
238
|
|
|
{ |
239
|
|
|
$rMap = $this->setMySQLqueryValidationMap(); |
240
|
|
|
if (array_key_exists($prm['returnType'], $rMap)) { |
241
|
|
|
$elC = [$prm['NoOfRows'], $rMap[$prm['returnType']]['r'][0], $rMap[$prm['returnType']]['r'][1]]; |
242
|
|
|
if (filter_var($elC[0], FILTER_VALIDATE_INT, ['min_range' => $elC[1], 'max_range' => $elC[2]]) === false) { |
243
|
|
|
$suffix2 = (string) $rMap[$prm['returnType']][2]; |
244
|
|
|
$msg = $this->lclMsgCmn('i18n_MySQL_QueryResultExpected' . $suffix2); |
245
|
|
|
return [false, sprintf($msg, $prm['NoOfColumns'])]; |
246
|
|
|
} |
247
|
|
|
$elR = [$prm['NoOfColumns'], $rMap[$prm['returnType']]['c'][0], $rMap[$prm['returnType']]['c'][1]]; |
248
|
|
|
if (filter_var($elR[0], FILTER_VALIDATE_INT, ['min_range' => $elR[1], 'max_range' => $elR[2]])) { |
249
|
|
|
return [true, '']; |
250
|
|
|
} |
251
|
|
|
$suffix1 = (string) $rMap[$prm['returnType']][1]; |
252
|
|
|
$msg = $this->lclMsgCmn('i18n_MySQL_QueryResultExpected' . $suffix1); |
253
|
|
|
return [false, sprintf($msg, $prm['NoOfColumns'])]; |
254
|
|
|
} |
255
|
|
|
return [false, $prm['returnType'] . ' is not defined!']; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
private function setMySQLqueryValidationMap() |
259
|
|
|
{ |
260
|
|
|
$lngKey = 'full_array_key_numbered_with_record_number_prefix'; |
261
|
|
|
return [ |
262
|
|
|
'array_first_key_rest_values' => ['r' => [1, 999999], 'c' => [2, 99], 'AtLeast2ColsResultedOther'], |
263
|
|
|
'array_key_value' => ['r' => [1, 999999], 'c' => [2, 2], '2ColumnsResultedOther'], |
264
|
|
|
'array_key_value2' => ['r' => [1, 999999], 'c' => [2, 2], '2ColumnsResultedOther'], |
265
|
|
|
'array_key2_value' => ['r' => [1, 999999], 'c' => [2, 2], '2ColumnsResultedOther'], |
266
|
|
|
'array_numbered' => ['r' => [1, 999999], 'c' => [1, 1], '1ColumnResultedOther'], |
267
|
|
|
'array_pairs_key_value' => ['r' => [1, 1], 'c' => [1, 99], '1RowManyColumnsResultedOther'], |
268
|
|
|
'full_array_key_numbered' => ['r' => [1, 999999], 'c' => [1, 99], '1OrMoreRows0Resulted'], |
269
|
|
|
'full_array_key_numbered_with_prefix' => ['r' => [1, 999999], 'c' => [1, 99], '1OrMoreRows0Resulted'], |
270
|
|
|
$lngKey => ['r' => [1, 999999], 'c' => [1, 99], '1OrMoreRows0Resulted'], |
271
|
|
|
'value' => ['r' => [1, 1], 'c' => [1, 1], '1ResultedOther'], |
272
|
|
|
]; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|