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 results |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait MySQLiAdvancedOutput |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
use DomComponentsByDanielGP, |
40
|
|
|
MySQLiByDanielGP; |
41
|
|
|
|
42
|
|
|
protected $advCache = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Establish Database and Table intended to work with |
46
|
|
|
* (in case the DB is ommited get the default one) |
47
|
|
|
* |
48
|
|
|
* @param string $tblSrc |
49
|
|
|
*/ |
50
|
|
|
private function establishDatabaseAndTable($tblSrc) |
51
|
|
|
{ |
52
|
|
|
if (strpos($tblSrc, '.') === false) { |
53
|
|
|
if (!defined('MYSQL_DATABASE')) { |
54
|
|
|
define('MYSQL_DATABASE', 'information_schema'); |
55
|
|
|
} |
56
|
|
|
return [$tblSrc, MYSQL_DATABASE]; |
57
|
|
|
} |
58
|
|
|
return explode('.', str_replace('`', '', $tblSrc)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function establishDefaultEnumSet($fldType) |
62
|
|
|
{ |
63
|
|
|
$dfltArray = [ |
64
|
|
|
'enum' => ['additional' => ['size' => 1], 'suffix' => ''], |
65
|
|
|
'set' => ['additional' => ['size' => 5, 'multiselect'], 'suffix' => '[]'], |
66
|
|
|
]; |
67
|
|
|
return $dfltArray[$fldType]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function fixTableSource($refTbl) |
71
|
|
|
{ |
72
|
|
|
$outS = []; |
73
|
|
|
if (substr($refTbl, 0, 1) !== '`') { |
74
|
|
|
$outS[] = '`'; |
75
|
|
|
} |
76
|
|
|
$psT = strpos($refTbl, '.`'); |
77
|
|
|
if ($psT !== false) { |
78
|
|
|
$refTbl = substr($refTbl, $psT + 2, strlen($refTbl) - $psT); |
79
|
|
|
} |
80
|
|
|
$outS[] = $refTbl; |
81
|
|
|
if (substr($refTbl, -1) !== '`') { |
82
|
|
|
$outS[] = '`'; |
83
|
|
|
} |
84
|
|
|
return implode('', $outS); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function getFieldCompletionType($details) |
88
|
|
|
{ |
89
|
|
|
$inputFeatures = ['display' => '***', 'ftrs' => ['title' => 'Mandatory', 'class' => 'inputMandatory']]; |
90
|
|
|
if ($details['IS_NULLABLE'] == 'YES') { |
91
|
|
|
$inputFeatures = ['display' => '~', 'ftrs' => ['title' => 'Optional', 'class' => 'inputOptional']]; |
92
|
|
|
} |
93
|
|
|
return $this->setStringIntoTag($inputFeatures['display'], 'span', $inputFeatures['ftrs']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the name of a field for displaying |
98
|
|
|
* |
99
|
|
|
* @param array $details |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
private function getFieldNameForDisplay($details) |
103
|
|
|
{ |
104
|
|
|
$tableUniqueId = $details['TABLE_SCHEMA'] . '.' . $details['TABLE_NAME']; |
105
|
|
|
if ($details['COLUMN_COMMENT'] != '') { |
106
|
|
|
return $details['COLUMN_COMMENT']; |
107
|
|
|
} elseif (isset($this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']])) { |
108
|
|
|
return $this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']]; |
109
|
|
|
} |
110
|
|
|
return $details['COLUMN_NAME']; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns a Enum or Set field to use in form |
115
|
|
|
* |
116
|
|
|
* @param string $tblSrc |
117
|
|
|
* @param string $fldType |
118
|
|
|
* @param array $val |
119
|
|
|
* @param array $iar |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
private function getFieldOutputEnumSet($tblSrc, $fldType, $val, $iar = []) |
123
|
|
|
{ |
124
|
|
|
$adnlThings = $this->establishDefaultEnumSet($fldType); |
125
|
|
|
if (array_key_exists('readonly', $val)) { |
126
|
|
|
return $this->getFieldOutputEnumSetReadOnly($val, $adnlThings); |
127
|
|
|
} |
128
|
|
|
$inAdtnl = $adnlThings['additional']; |
129
|
|
|
if ($iar !== []) { |
130
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
131
|
|
|
} |
132
|
|
|
$vlSlct = explode(',', $this->getFieldValue($val)); |
133
|
|
|
$slctOptns = $this->getSetOrEnum2Array($tblSrc, $val['COLUMN_NAME']); |
134
|
|
|
return $this->setArrayToSelect($slctOptns, $vlSlct, $val['COLUMN_NAME'] . $adnlThings['suffix'], $inAdtnl); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function getFieldOutputEnumSetReadOnly($val, $adnlThings) |
138
|
|
|
{ |
139
|
|
|
$inputFeatures = [ |
140
|
|
|
'name' => $val['COLUMN_NAME'] . $adnlThings['suffix'], |
141
|
|
|
'id' => $val['COLUMN_NAME'], |
142
|
|
|
'readonly' => 'readonly', |
143
|
|
|
'class' => 'input_readonly', |
144
|
|
|
'size' => 50, |
145
|
|
|
'value' => $this->getFieldValue($val), |
146
|
|
|
]; |
147
|
|
|
return $this->setStringIntoShortTag('input', $inputFeatures); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns a Numeric field 2 use in a form |
152
|
|
|
* |
153
|
|
|
* @param string $tblSrc |
154
|
|
|
* @param array $value |
155
|
|
|
* @param array $features |
|
|
|
|
156
|
|
|
* @param array $iar |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
private function getFieldOutputNumeric($tblSrc, $value, $iar = []) |
160
|
|
|
{ |
161
|
|
|
if ($value['EXTRA'] == 'auto_increment') { |
162
|
|
|
if ($this->getFieldValue($value) == '') { |
163
|
|
|
return $this->setStringIntoTag('auto-numar', 'span', [ |
164
|
|
|
'id' => $value['COLUMN_NAME'], |
165
|
|
|
'style' => 'font-style:italic;', |
166
|
|
|
]); |
167
|
|
|
} |
168
|
|
|
$inAdtnl = [ |
169
|
|
|
'type' => 'hidden', |
170
|
|
|
'name' => $value['COLUMN_NAME'], |
171
|
|
|
'id' => $value['COLUMN_NAME'], |
172
|
|
|
'value' => $this->getFieldValue($value), |
173
|
|
|
]; |
174
|
|
|
if ($iar !== []) { |
175
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
176
|
|
|
} |
177
|
|
|
return $this->setStringIntoTag($this->getFieldValue($value), 'b') |
178
|
|
|
. $this->setStringIntoShortTag('input', $inAdtnl); |
179
|
|
|
} |
180
|
|
|
$database = $this->advCache['workingDatabase']; |
181
|
|
|
$fkArray = $this->getForeignKeysToArray($database, $tblSrc, $value['COLUMN_NAME']); |
182
|
|
|
if (is_null($fkArray)) { |
183
|
|
|
$fldNos = $this->setFieldNumbers($value); |
184
|
|
|
$inAdtnl = [ |
185
|
|
|
'type' => 'text', |
186
|
|
|
'name' => $value['COLUMN_NAME'], |
187
|
|
|
'id' => $value['COLUMN_NAME'], |
188
|
|
|
'value' => $this->getFieldValue($value), |
189
|
|
|
'size' => min(50, $fldNos['l']), |
190
|
|
|
'maxlength' => min(50, $fldNos['l']) |
191
|
|
|
]; |
192
|
|
|
if ($iar !== []) { |
193
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
194
|
|
|
} |
195
|
|
|
return $this->setStringIntoShortTag('input', $inAdtnl); |
196
|
|
|
} |
197
|
|
|
$query = $this->sQueryGenericSelectKeyValue([ |
198
|
|
|
$fkArray[$value['COLUMN_NAME']][1], |
199
|
|
|
$fkArray[$value['COLUMN_NAME']][2], |
200
|
|
|
$fkArray[$value['COLUMN_NAME']][0] |
201
|
|
|
]); |
202
|
|
|
$selectOptions = $this->setMySQLquery2Server($query, 'array_key_value')['result']; |
203
|
|
|
$selectValue = $this->getFieldValue($value); |
204
|
|
|
$inAdtnl = ['size' => 1]; |
205
|
|
|
if ($value['IS_NULLABLE'] == 'YES') { |
206
|
|
|
$inAdtnl = array_merge($inAdtnl, ['include_null']); |
207
|
|
|
} |
208
|
|
|
if ($iar !== []) { |
209
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
210
|
|
|
} |
211
|
|
|
return $this->setArrayToSelect($selectOptions, $selectValue, $value['COLUMN_NAME'], $inAdtnl); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Returns a Char field 2 use in a form |
216
|
|
|
* |
217
|
|
|
* @param string $tbl |
218
|
|
|
* @param string $fieldType |
219
|
|
|
* @param array $value |
220
|
|
|
* @param array $features |
|
|
|
|
221
|
|
|
* @param array $iar |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
private function getFieldOutputText($tbl, $fieldType, $value, $iar = []) |
225
|
|
|
{ |
226
|
|
|
if (!in_array($fieldType, ['char', 'tinytext', 'varchar'])) { |
227
|
|
|
return ''; |
228
|
|
|
} |
229
|
|
|
$foreignKeysArray = $this->getFieldOutputTextPrerequisites($tbl, $value); |
230
|
|
|
if (is_null($foreignKeysArray)) { |
231
|
|
|
return $this->getFieldOutputTextFK($foreignKeysArray, $value, $iar); |
232
|
|
|
} |
233
|
|
|
return $this->getFieldOutputTextNonFK($value, $iar); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
private function getFieldOutputTextFK($foreignKeysArray, $value, $iar) |
237
|
|
|
{ |
238
|
|
|
$query = $this->storedQuery('generic_select_key_value', [ |
|
|
|
|
239
|
|
|
$foreignKeysArray[$value['COLUMN_NAME']][1], |
240
|
|
|
$foreignKeysArray[$value['COLUMN_NAME']][2], |
241
|
|
|
$foreignKeysArray[$value['COLUMN_NAME']][0] |
242
|
|
|
]); |
243
|
|
|
$inAdtnl = ['size' => 1]; |
244
|
|
|
if ($value['IS_NULLABLE'] == 'YES') { |
245
|
|
|
$inAdtnl = array_merge($inAdtnl, ['include_null']); |
246
|
|
|
} |
247
|
|
|
if ($iar !== []) { |
248
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
249
|
|
|
} |
250
|
|
|
$slct = [ |
251
|
|
|
'Options' => $this->setQuery2Server($query, 'array_key_value'), |
|
|
|
|
252
|
|
|
'Value' => $this->getFieldValue($value), |
253
|
|
|
]; |
254
|
|
|
return $this->setArrayToSelect($slct['Options'], $slct['Value'], $value['COLUMN_NAME'], $inAdtnl); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Returns a Text field 2 use in a form |
259
|
|
|
* |
260
|
|
|
* @param string $table_source |
|
|
|
|
261
|
|
|
* @param string $fieldType |
262
|
|
|
* @param array $value |
263
|
|
|
* @param array $features |
|
|
|
|
264
|
|
|
* @param array $iar |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
|
|
private function getFieldOutputTextLarge($fieldType, $value, $iar = []) |
268
|
|
|
{ |
269
|
|
|
if (!in_array($fieldType, ['blob', 'text'])) { |
270
|
|
|
return ''; |
271
|
|
|
} |
272
|
|
|
$inAdtnl = [ |
273
|
|
|
'name' => $value['COLUMN_NAME'], |
274
|
|
|
'id' => $value['COLUMN_NAME'], |
275
|
|
|
'rows' => 4, |
276
|
|
|
'cols' => 55, |
277
|
|
|
]; |
278
|
|
|
if ($iar !== []) { |
279
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
280
|
|
|
} |
281
|
|
|
return $this->setStringIntoTag($this->getFieldValue($value), 'textarea', $inAdtnl); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
private function getFieldOutputTextNonFK($value, $iar) |
285
|
|
|
{ |
286
|
|
|
$fldNos = $this->setFieldNumbers($value); |
287
|
|
|
$inAdtnl = [ |
288
|
|
|
'type' => ($value['COLUMN_NAME'] == 'password' ? 'password' : 'text'), |
289
|
|
|
'name' => $value['COLUMN_NAME'], |
290
|
|
|
'id' => $value['COLUMN_NAME'], |
291
|
|
|
'size' => min(30, $fldNos['l']), |
292
|
|
|
'maxlength' => min(255, $fldNos['l']), |
293
|
|
|
'value' => $this->getFieldValue($value), |
294
|
|
|
]; |
295
|
|
|
if ($iar !== []) { |
296
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
297
|
|
|
} |
298
|
|
|
return $this->setStringIntoShortTag('input', $inAdtnl); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
private function getFieldOutputTextPrerequisites($tbl, $value) |
302
|
|
|
{ |
303
|
|
|
$foreignKeysArray = null; |
304
|
|
|
if (($tbl != 'user_rights') && ($value['COLUMN_NAME'] != 'eid')) { |
305
|
|
|
$database = $this->advCache['workingDatabase']; |
306
|
|
|
if (strpos($tbl, '`.`')) { |
307
|
|
|
$database = substr($tbl, 0, strpos($tbl, '`.`')); |
308
|
|
|
} |
309
|
|
|
$foreignKeysArray = $this->getForeignKeysToArray($database, $tbl, $value['COLUMN_NAME']); |
310
|
|
|
} |
311
|
|
|
return $foreignKeysArray; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
private function getFieldOutputTT($value, $szN, $iar = []) |
315
|
|
|
{ |
316
|
|
|
$inAdtnl = [ |
317
|
|
|
'id' => $value['COLUMN_NAME'], |
318
|
|
|
'maxlength' => $szN, |
319
|
|
|
'name' => $value['COLUMN_NAME'], |
320
|
|
|
'size' => $szN, |
321
|
|
|
'type' => 'text', |
322
|
|
|
'value' => $this->getFieldValue($value), |
323
|
|
|
]; |
324
|
|
|
if ($iar !== []) { |
325
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
326
|
|
|
} |
327
|
|
|
return $this->setStringIntoShortTag('input', $inAdtnl); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Returns a Time field 2 use in a form |
332
|
|
|
* |
333
|
|
|
* @param array $value |
334
|
|
|
* @param array $iar |
335
|
|
|
* @return string |
336
|
|
|
*/ |
337
|
|
|
private function getFieldOutputTime($value, $iar = []) |
338
|
|
|
{ |
339
|
|
|
return $this->getFieldOutputTT($value, 8, $iar); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Returns a Timestamp field 2 use in a form |
344
|
|
|
* |
345
|
|
|
* @param array $value |
346
|
|
|
* @param array $iar |
347
|
|
|
* @return string |
348
|
|
|
*/ |
349
|
|
|
private function getFieldOutputTimestamp($value, $iar = []) |
350
|
|
|
{ |
351
|
|
|
$input = $this->getFieldOutputTT($value, 19, $iar); |
352
|
|
|
if (!array_key_exists('readonly', $iar)) { |
353
|
|
|
$input .= $this->setCalendarControlWithTime($value['COLUMN_NAME']); |
354
|
|
|
} |
355
|
|
|
return $input; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Returns a Year field 2 use in a form |
360
|
|
|
* |
361
|
|
|
* @param array $details |
362
|
|
|
* @param array $iar |
363
|
|
|
* @return string |
364
|
|
|
*/ |
365
|
|
|
private function getFieldOutputYear($details, $iar) |
366
|
|
|
{ |
367
|
|
|
for ($c = 1901; $c <= 2155; $c++) { |
368
|
|
|
$listOfValues[$c] = $c; |
|
|
|
|
369
|
|
|
} |
370
|
|
|
if ($iar == []) { |
371
|
|
|
$slDflt = $this->getFieldValue($details); |
372
|
|
|
return $this->setArrayToSelect($listOfValues, $slDflt, $details['COLUMN_NAME'], ['size' => 1]); |
|
|
|
|
373
|
|
|
} |
374
|
|
|
return $this->getFieldOutputText('varchar', $details, $iar); |
|
|
|
|
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Returns given value for a field from $_REQUEST |
379
|
|
|
* |
380
|
|
|
* @param array $details |
381
|
|
|
* @return string |
382
|
|
|
*/ |
383
|
|
|
private function getFieldValue($details) |
384
|
|
|
{ |
385
|
|
|
$this->initializeSprGlbAndSession(); |
386
|
|
|
$rqCN = $this->tCmnRequest->request->get($details['COLUMN_NAME']); |
387
|
|
|
if (!is_null($rqCN)) { |
388
|
|
|
if (($details['IS_NULLABLE'] == 'YES') && ($rqCN == '')) { |
389
|
|
|
return 'NULL'; |
390
|
|
|
} |
391
|
|
|
return $rqCN; |
392
|
|
|
} |
393
|
|
|
return $this->getFieldValueWithoutUserInput($details); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Handles field value ignoring any input from the user |
398
|
|
|
* |
399
|
|
|
* @param array $details |
400
|
|
|
* @return string |
401
|
|
|
*/ |
402
|
|
|
private function getFieldValueWithoutUserInput($details) |
403
|
|
|
{ |
404
|
|
|
if ($details['COLUMN_DEFAULT'] === null) { |
405
|
|
|
if ($details['IS_NULLABLE'] == 'YES') { |
406
|
|
|
return 'NULL'; |
407
|
|
|
} |
408
|
|
|
return ''; |
409
|
|
|
} |
410
|
|
|
return $details['COLUMN_DEFAULT']; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
private function getForeignKeysQuery($value) |
414
|
|
|
{ |
415
|
|
|
$flt = [ |
416
|
|
|
'TABLE_SCHEMA' => $value['REFERENCED_TABLE_SCHEMA'], |
417
|
|
|
'TABLE_NAME' => $value['REFERENCED_TABLE_NAME'], |
418
|
|
|
'DATA_TYPE' => ['char', 'varchar', 'text'], |
419
|
|
|
]; |
420
|
|
|
return $this->sQueryMySqlColumns($flt); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Returns an array with fields referenced by a Foreign key |
425
|
|
|
* |
426
|
|
|
* @param string $database |
427
|
|
|
* @param string $tblName |
428
|
|
|
* @param string|array $onlyCols |
|
|
|
|
429
|
|
|
* @return array |
430
|
|
|
*/ |
431
|
|
|
private function getForeignKeysToArray($database, $tblName, $onlyCol = '') |
432
|
|
|
{ |
433
|
|
|
$this->setTableForeginKeyCache($database, $this->fixTableSource($tblName)); |
434
|
|
|
$array2return = null; |
435
|
|
|
if (isset($this->advCache['tableFKs'][$database][$tblName])) { |
436
|
|
|
foreach ($this->advCache['tableFKs'][$database][$tblName] as $value) { |
437
|
|
|
if ($value['COLUMN_NAME'] == $onlyCol) { |
438
|
|
|
$query = $this->getForeignKeysQuery($value); |
439
|
|
|
$targetTblTxtFlds = $this->setMySQLquery2Server($query, 'full_array_key_numbered')['result']; |
440
|
|
|
$array2return[$onlyCol] = [ |
441
|
|
|
$this->glueDbTb($value['REFERENCED_TABLE_SCHEMA'], $value['REFERENCED_TABLE_NAME']), |
442
|
|
|
$value['REFERENCED_COLUMN_NAME'], |
443
|
|
|
'`' . $targetTblTxtFlds[0]['COLUMN_NAME'] . '`', |
444
|
|
|
]; |
445
|
|
|
} |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
return $array2return; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
private function getLabel($details) |
452
|
|
|
{ |
453
|
|
|
return $this->setStringIntoTag($this->getFieldNameForDisplay($details), 'span', ['class' => 'fake_label']); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Returns an array with possible values of a SET or ENUM column |
458
|
|
|
* |
459
|
|
|
* @param string $refTbl |
460
|
|
|
* @param string $refCol |
461
|
|
|
* @return array |
462
|
|
|
*/ |
463
|
|
|
protected function getSetOrEnum2Array($refTbl, $refCol) |
464
|
|
|
{ |
465
|
|
|
$dat = $this->establishDatabaseAndTable($this->fixTableSource($refTbl)); |
466
|
|
|
foreach ($this->advCache['tableStructureCache'][$dat[0]][$dat[1]] as $value) { |
467
|
|
|
if ($value['COLUMN_NAME'] == $refCol) { |
468
|
|
|
$clndVls = explode(',', str_replace([$value['DATA_TYPE'], '(', "'", ')'], '', $value['COLUMN_TYPE'])); |
469
|
|
|
$enmVls = array_combine($clndVls, $clndVls); |
470
|
|
|
if ($value['IS_NULLABLE'] == 'YES') { |
471
|
|
|
$enmVls['NULL'] = ''; |
472
|
|
|
} |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
ksort($enmVls); |
476
|
|
|
return $enmVls; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Returns a timestamp field value |
481
|
|
|
* |
482
|
|
|
* @param array $dtl |
483
|
|
|
* @return array |
484
|
|
|
*/ |
485
|
|
|
private function getTimestamping($dtl) |
486
|
|
|
{ |
487
|
|
|
$inM = $this->setStringIntoTag($this->getFieldValue($dtl), 'span'); |
488
|
|
|
if (in_array($this->getFieldValue($dtl), ['', 'CURRENT_TIMESTAMP', 'NULL'])) { |
489
|
|
|
$mCN = [ |
490
|
|
|
'InsertDateTime' => 'data/timpul ad. informatiei', |
491
|
|
|
'ModificationDateTime' => 'data/timpul modificarii inf.', |
492
|
|
|
'modification_datetime' => 'data/timpul modificarii inf.', |
493
|
|
|
]; |
494
|
|
|
if (array_key_exists($dtl['COLUMN_NAME'], $mCN)) { |
495
|
|
|
$inM = $this->setStringIntoTag($mCN[$dtl['COLUMN_NAME']], 'span', ['style' => 'font-style:italic;']); |
496
|
|
|
} |
497
|
|
|
} |
498
|
|
|
return ['label' => $this->getLabel($dtl), 'input' => $inM]; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
private function glueDbTb($dbName, $tbName) |
502
|
|
|
{ |
503
|
|
|
return '`' . $dbName . '`.`' . $tbName . '`'; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Manages features flag |
508
|
|
|
* |
509
|
|
|
* @param string $fieldName |
510
|
|
|
* @param array $features |
511
|
|
|
* @return string |
512
|
|
|
*/ |
513
|
|
|
private function handleFeatures($fieldName, $features) |
514
|
|
|
{ |
515
|
|
|
$rOly = $this->handleFeaturesSingle($fieldName, $features, 'readonly'); |
516
|
|
|
$rDbld = $this->handleFeaturesSingle($fieldName, $features, 'disabled'); |
517
|
|
|
$rNl = []; |
518
|
|
|
if (isset($features['include_null']) && in_array($fieldName, $features['include_null'])) { |
519
|
|
|
$rNl = ['include_null']; |
520
|
|
|
} |
521
|
|
|
return array_merge([], $rOly, $rDbld, $rNl); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
private function handleFeaturesSingle($fieldName, $features, $featureKey) |
525
|
|
|
{ |
526
|
|
|
$fMap = [ |
527
|
|
|
'readonly' => ['readonly', 'class', 'input_readonly'], |
528
|
|
|
'disabled' => ['disabled'] |
529
|
|
|
]; |
530
|
|
|
$aReturn = []; |
531
|
|
|
if (array_key_exists($featureKey, $features)) { |
532
|
|
|
if (array_key_exists($fieldName, $features[$featureKey])) { |
533
|
|
|
$aReturn[$featureKey][$fMap[$featureKey][0]] = $fMap[$featureKey][0]; |
534
|
|
|
if (count($fMap[$featureKey]) > 1) { |
535
|
|
|
$aReturn[$featureKey][$fMap[$featureKey][1]] = $fMap[$featureKey][2]; |
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
} |
539
|
|
|
return $aReturn; |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* Returns a generic form based on a given table |
544
|
|
|
* |
545
|
|
|
* @param string $tblSrc Table Source |
546
|
|
|
* @param array $feat |
547
|
|
|
* @param string/array $hiddenInfo List of hidden fields |
|
|
|
|
548
|
|
|
* |
549
|
|
|
* @return string Form to add/modify detail for a single row within a table |
550
|
|
|
*/ |
551
|
|
|
protected function setFormGenericSingleRecord($tblSrc, $feat, $hiddenInfo = '') |
552
|
|
|
{ |
553
|
|
|
echo $this->setStringIntoTag('', 'div', [ |
554
|
|
|
'id' => 'loading' |
555
|
|
|
]); // Ajax container |
556
|
|
|
if (strpos($tblSrc, '.') !== false) { |
557
|
|
|
$tblSrc = explode('.', str_replace('`', '', $tblSrc))[1]; |
558
|
|
|
} |
559
|
|
|
$this->setTableCache($tblSrc); // will populate $this->advCache['tableStructureCache'][$dt[0]][$dt[1]] |
|
|
|
|
560
|
|
|
if (count($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc]) != 0) { |
561
|
|
|
foreach ($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc] as $value) { |
562
|
|
|
$sReturn[] = $this->setNeededField($tblSrc, $value, $feat); |
|
|
|
|
563
|
|
|
} |
564
|
|
|
} |
565
|
|
|
$btn[] = $this->setStringIntoShortTag('input', [ |
|
|
|
|
566
|
|
|
'type' => 'submit', |
567
|
|
|
'id' => 'submit', |
568
|
|
|
'style' => 'margin-left:220px;', |
569
|
|
|
'value' => $this->lclMsgCmn('i18n_Form_ButtonSave'), |
570
|
|
|
]); |
571
|
|
|
$adtnlScriptAfterForm = $this->setJavascriptContent(implode('', [ |
572
|
|
|
'$(document).ready(function(){', |
573
|
|
|
'$("form#' . $feat['id'] . '").submit(function(){', |
574
|
|
|
'$("input").attr("readonly", true);', |
575
|
|
|
'$("input[type=submit]").attr("disabled", "disabled");', |
576
|
|
|
'$("input[type=submit]").attr("value", "' . $this->lclMsgCmn('i18n_Form_ButtonSaving') . '");', |
577
|
|
|
'});', |
578
|
|
|
'});', |
579
|
|
|
])); |
580
|
|
|
if (isset($feat['insertAndUpdate'])) { |
581
|
|
|
$btn[] = $this->setStringIntoShortTag('input', [ |
582
|
|
|
'type' => 'hidden', |
583
|
|
|
'id' => 'insertAndUpdate', |
584
|
|
|
'name' => 'insertAndUpdate', |
585
|
|
|
'value' => 'insertAndUpdate' |
586
|
|
|
]); |
587
|
|
|
} |
588
|
|
|
$sReturn[] = $this->setStringIntoTag(implode('', $btn), 'div'); |
|
|
|
|
589
|
|
|
if (isset($hiddenInfo)) { |
590
|
|
|
if (is_array($hiddenInfo)) { |
591
|
|
|
foreach ($hiddenInfo as $key => $value) { |
592
|
|
|
$hiddenInput = $this->setStringIntoShortTag('input', [ |
593
|
|
|
'type' => 'hidden', |
594
|
|
|
'name' => $key, |
595
|
|
|
'id' => $key, |
596
|
|
|
'value' => $value, |
597
|
|
|
]); |
598
|
|
|
$sReturn[] = $this->setStringIntoTag($hiddenInput, 'div'); |
599
|
|
|
} |
600
|
|
|
} |
601
|
|
|
} |
602
|
|
|
return $this->setStringIntoTag(implode('', $sReturn), 'form', [ |
603
|
|
|
'id' => $feat['id'], |
604
|
|
|
'action' => $feat['action'], |
605
|
|
|
'method' => $feat['method'] |
606
|
|
|
]) |
607
|
|
|
. $adtnlScriptAfterForm; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
protected function setTableLocaleFields($localizationStrings) |
611
|
|
|
{ |
612
|
|
|
$this->advCache['tableStructureLocales'] = $localizationStrings; |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* Analyse the field and returns the proper line 2 use in forms |
617
|
|
|
* |
618
|
|
|
* @param string $tableSource |
619
|
|
|
* @param array $details |
620
|
|
|
* @param array $features |
621
|
|
|
* @return string|array |
622
|
|
|
*/ |
623
|
|
|
private function setNeededField($tableSource, $details, $features) |
|
|
|
|
624
|
|
|
{ |
625
|
|
|
if (isset($features['hidden'])) { |
626
|
|
|
if (in_array($details['COLUMN_NAME'], $features['hidden'])) { |
627
|
|
|
return null; |
628
|
|
|
} |
629
|
|
|
} |
630
|
|
|
$fieldLabel = $this->getFieldNameForDisplay($details); |
631
|
|
|
if ($fieldLabel == 'hidden') { |
632
|
|
|
return null; |
633
|
|
|
} |
634
|
|
|
switch ($details['COLUMN_NAME']) { |
635
|
|
|
case 'host': |
636
|
|
|
$sReturn['label'] = $this->setStringIntoTag('Numele calculatorului', 'label', [ |
|
|
|
|
637
|
|
|
'for' => $details['COLUMN_NAME'] |
638
|
|
|
]); |
639
|
|
|
$sReturn['input'] = $this->setStringIntoShortTag('input', [ |
640
|
|
|
'type' => 'input', |
641
|
|
|
'size' => 15, |
642
|
|
|
'readonly' => 'readonly', |
643
|
|
|
'value' => gethostbyaddr($_SERVER['REMOTE_ADDR']) |
644
|
|
|
]); |
645
|
|
|
break; |
646
|
|
|
case 'InsertDateTime': |
647
|
|
|
case 'modification_datetime': |
648
|
|
|
case 'ModificationDateTime': |
649
|
|
|
$sReturn = call_user_func_array([$this, 'getTimestamping'], [$details]); |
650
|
|
|
break; |
651
|
|
|
default: |
652
|
|
|
$aLabel = [ |
653
|
|
|
'for' => $details['COLUMN_NAME'], |
654
|
|
|
'id' => $details['COLUMN_NAME'] . '_label' |
655
|
|
|
]; |
656
|
|
|
if (isset($features['disabled'])) { |
657
|
|
|
if (in_array($details['COLUMN_NAME'], $features['disabled'])) { |
658
|
|
|
$aLabel = array_merge($aLabel, ['style' => 'color: grey;']); |
659
|
|
|
} |
660
|
|
|
} |
661
|
|
|
$sReturn['label'] = $this->setStringIntoTag($fieldLabel, 'label', $aLabel); |
|
|
|
|
662
|
|
|
$result = $this->setNeededFieldByType($tableSource, $details, $features); |
663
|
|
|
if ($details['COLUMN_NAME'] == 'ChoiceId') { |
664
|
|
|
$result = $this->setStringIntoShortTag('input', [ |
665
|
|
|
'type' => 'text', |
666
|
|
|
'name' => $details['COLUMN_NAME'], |
667
|
|
|
'value' => $_REQUEST[$details['COLUMN_NAME']] |
668
|
|
|
]); |
669
|
|
|
} |
670
|
|
|
$sReturn['input'] = $result; |
671
|
|
|
break; |
672
|
|
|
} |
673
|
|
|
$finalReturn[] = $sReturn['label']; |
|
|
|
|
674
|
|
|
$finalReturn[] = $this->setStringIntoTag($sReturn['input'], 'span', ['class' => 'labell']); |
675
|
|
|
$wrkDb = $this->advCache['workingDatabase']; |
676
|
|
|
if (isset($this->tableFKsCache[$wrkDb][$tableSource])) { |
|
|
|
|
677
|
|
|
if (in_array($details['COLUMN_NAME'], $this->advCache['FKcol'][$wrkDb][$tableSource])) { |
678
|
|
|
$finalReturn[] = $this->getFieldLength($details); |
|
|
|
|
679
|
|
|
} |
680
|
|
|
} |
681
|
|
|
return $this->setStringIntoTag(implode('', $finalReturn), 'div'); |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* Analyse the field type and returns the proper lines 2 use in forms |
686
|
|
|
* |
687
|
|
|
* @param string $tblName |
688
|
|
|
* @param array $details |
689
|
|
|
* @param array $features |
690
|
|
|
* @return string|array |
691
|
|
|
*/ |
692
|
|
|
private function setNeededFieldByType($tblName, $details, $features) |
693
|
|
|
{ |
694
|
|
|
$sReturn = null; |
695
|
|
|
if (isset($features['special']) && isset($features['special'][$details['COLUMN_NAME']])) { |
696
|
|
|
$slctOpt = $this->setQuery2Server($features['special'][$details['COLUMN_NAME']], 'array_key_value'); |
|
|
|
|
697
|
|
|
$sReturn = $this->setArrayToSelect($slctOpt, $this->getFieldValue($details), $details['COLUMN_NAME'], [ |
698
|
|
|
'size' => 1 |
699
|
|
|
]); |
700
|
|
|
} else { |
701
|
|
|
$iar = $this->handleFeatures($details['COLUMN_NAME'], $features); |
702
|
|
|
switch ($details['DATA_TYPE']) { |
703
|
|
|
case 'bigint': |
704
|
|
|
case 'int': |
705
|
|
|
case 'mediumint': |
706
|
|
|
case 'smallint': |
707
|
|
|
case 'tinyint': |
708
|
|
|
case 'float': |
709
|
|
|
case 'double': |
710
|
|
|
case 'decimal': |
711
|
|
|
case 'numeric': |
712
|
|
|
$sReturn = $this->getFieldOutputNumeric($tblName, $details, $iar); |
713
|
|
|
break; |
714
|
|
|
case 'char': |
715
|
|
|
case 'tinytext': |
716
|
|
|
case 'varchar': |
717
|
|
|
$sReturn = $this->getFieldOutputText($tblName, $details['DATA_TYPE'], $details, $iar); |
718
|
|
|
break; |
719
|
|
|
case 'date': |
720
|
|
|
$sReturn = $this->getFieldOutputDate($details['DATA_TYPE'], $details, $iar); |
|
|
|
|
721
|
|
|
break; |
722
|
|
|
case 'datetime': |
723
|
|
|
case 'timestamp': |
724
|
|
|
$sReturn = $this->getFieldOutputTimestamp($details, $iar); |
725
|
|
|
break; |
726
|
|
|
case 'enum': |
727
|
|
|
case 'set': |
728
|
|
|
$sReturn = $this->getFieldOutputEnumSet($tblName, $details['DATA_TYPE'], $details, $iar); |
729
|
|
|
break; |
730
|
|
|
case 'text': |
731
|
|
|
case 'blob': |
732
|
|
|
$sReturn = $this->getFieldOutputTextLarge($details['DATA_TYPE'], $details, $iar); |
733
|
|
|
break; |
734
|
|
|
case 'time': |
735
|
|
|
$sReturn = $this->getFieldOutputTime($details, $iar); |
736
|
|
|
break; |
737
|
|
|
case 'year': |
738
|
|
|
$sReturn = $this->getFieldOutputYear($details, $iar); |
739
|
|
|
break; |
740
|
|
|
} |
741
|
|
|
} |
742
|
|
|
return $this->getFieldCompletionType($details) . $sReturn; |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
/** |
746
|
|
|
* create a Cache for given table to use it in many places |
747
|
|
|
* |
748
|
|
|
* @param type $tblSrc |
749
|
|
|
*/ |
750
|
|
|
private function setTableCache($tblSrc) |
751
|
|
|
{ |
752
|
|
|
$dat = $this->establishDatabaseAndTable($this->fixTableSource($tblSrc)); |
753
|
|
|
if (!isset($this->advCache['tableStructureCache'][$dat[0]][$dat[1]])) { |
754
|
|
|
switch ($dat[1]) { |
755
|
|
|
case 'user_rights': |
756
|
|
|
$this->advCache['workingDatabase'] = 'usefull_security'; |
757
|
|
|
break; |
758
|
|
|
default: |
759
|
|
|
$this->advCache['workingDatabase'] = $dat[0]; |
760
|
|
|
break; |
761
|
|
|
} |
762
|
|
|
$this->advCache['tableStructureCache'][$dat[0]][$dat[1]] = $this->getMySQLlistColumns([ |
763
|
|
|
'TABLE_SCHEMA' => $dat[0], |
764
|
|
|
'TABLE_NAME' => $dat[1], |
765
|
|
|
]); |
766
|
|
|
$this->setTableForeginKeyCache($dat[0], $dat[1]); |
767
|
|
|
} |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
private function setTableForeginKeyCache($dbName, $tblName) |
771
|
|
|
{ |
772
|
|
|
$frgnKs = $this->getMySQLlistIndexes([ |
773
|
|
|
'TABLE_SCHEMA' => $dbName, |
774
|
|
|
'TABLE_NAME' => $tblName, |
775
|
|
|
'REFERENCED_TABLE_NAME' => 'NOT NULL', |
776
|
|
|
]); |
777
|
|
|
if (!is_null($frgnKs)) { |
778
|
|
|
$this->advCache['tableFKs'][$dbName][$tblName] = $frgnKs; |
779
|
|
|
$this->advCache['FKcol'][$dbName][$tblName] = array_column($frgnKs, 'COLUMN_NAME', 'CONSTRAINT_NAME'); |
780
|
|
|
} |
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
private function setViewDeleteFeedbacks() |
784
|
|
|
{ |
785
|
|
|
return [ |
786
|
|
|
'Confirmation' => $this->lclMsgCmn('i18n_Action_Confirmation'), |
787
|
|
|
'Failed' => $this->lclMsgCmn('i18n_ActionDelete_Failed'), |
788
|
|
|
'Impossible' => $this->lclMsgCmn('i18n_ActionDelete_Impossible'), |
789
|
|
|
'Success' => $this->lclMsgCmn('i18n_ActionDelete_Success'), |
790
|
|
|
]; |
791
|
|
|
} |
792
|
|
|
|
793
|
|
|
private function setViewDeletePackedFinal($sReturn) |
794
|
|
|
{ |
795
|
|
|
$finalJavascript = $this->setJavascriptContent(implode('', [ |
796
|
|
|
'$("#DeleteFeedback").fadeOut(4000, function() {', |
797
|
|
|
'$(this).remove();', |
798
|
|
|
'});', |
799
|
|
|
])); |
800
|
|
|
return '<div id="DeleteFeedback">' . $sReturn . '</div>' . $finalJavascript; |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
/** |
804
|
|
|
* Automatic handler for Record deletion |
805
|
|
|
* |
806
|
|
|
* @param string $tbl |
807
|
|
|
* @param string $idn |
808
|
|
|
* @return string |
809
|
|
|
*/ |
810
|
|
|
protected function setViewModernDelete($tbl, $idn) |
|
|
|
|
811
|
|
|
{ |
812
|
|
|
$tMsg = $this->setViewDeleteFeedbacks(); |
813
|
|
|
if ($tbl == '') { |
814
|
|
|
$sReturn = $this->setFeedbackModern('error', $tMsg['Confirmation'], $tMsg['Impossible']); |
815
|
|
|
} else { |
816
|
|
|
$this->setMySQLquery2Server($this->sQueryToDeleteSingleIdentifier([$tbl, $idn, $_REQUEST[$idn]])); |
817
|
|
|
$sReturn = $this->setFeedbackModern('error', $tMsg['Confirmation'], $tMsg['Failed']) |
818
|
|
|
. '(' . $this->mySQLconnection->error . ')'; |
819
|
|
|
if ($this->mySQLconnection->affected_rows > 0) { |
820
|
|
|
$sReturn = $this->setFeedbackModern('check', $tMsg['Confirmation'], $tMsg['Success']); |
821
|
|
|
} |
822
|
|
|
} |
823
|
|
|
return $this->setViewDeletePackedFinal($sReturn); |
824
|
|
|
} |
825
|
|
|
} |
826
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.