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 MySQLiByDanielGPstructures; |
40
|
|
|
|
41
|
|
|
protected $advCache = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Establish Database and Table intended to work with |
45
|
|
|
* (in case the DB is ommited get the default one) |
46
|
|
|
* |
47
|
|
|
* @param string $tblSrc |
48
|
|
|
*/ |
49
|
|
|
private function establishDatabaseAndTable($tblSrc) |
50
|
|
|
{ |
51
|
|
|
if (strpos($tblSrc, '.') === false) { |
52
|
|
|
if (!array_key_exists('workingDatabase', $this->advCache)) { |
53
|
|
|
$this->advCache['workingDatabase'] = $this->getMySqlCurrentDatabase(); |
54
|
|
|
} |
55
|
|
|
return [$this->advCache['workingDatabase'], $tblSrc]; |
56
|
|
|
} |
57
|
|
|
return explode('.', str_replace('`', '', $tblSrc)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Establishes the defaults for ENUM or SET field |
62
|
|
|
* |
63
|
|
|
* @param string $fldType |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
private function establishDefaultEnumSet($fldType) |
67
|
|
|
{ |
68
|
|
|
$dfltArray = [ |
69
|
|
|
'enum' => ['additional' => ['size' => 1], 'suffix' => ''], |
70
|
|
|
'set' => ['additional' => ['size' => 5, 'multiselect'], 'suffix' => '[]'], |
71
|
|
|
]; |
72
|
|
|
return $dfltArray[$fldType]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Creates a mask to differentiate between Mandatory and Optional fields |
77
|
|
|
* |
78
|
|
|
* @param array $details |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
private function getFieldCompletionType($details) |
82
|
|
|
{ |
83
|
|
|
$inputFeatures = ['display' => '***', 'ftrs' => ['title' => 'Mandatory', 'class' => 'inputMandatory']]; |
84
|
|
|
if ($details['IS_NULLABLE'] == 'YES') { |
85
|
|
|
$inputFeatures = ['display' => '~', 'ftrs' => ['title' => 'Optional', 'class' => 'inputOptional']]; |
86
|
|
|
} |
87
|
|
|
return $this->setStringIntoTag($inputFeatures['display'], 'span', $inputFeatures['ftrs']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the name of a field for displaying |
92
|
|
|
* |
93
|
|
|
* @param array $details |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
private function getFieldNameForDisplay($details) |
97
|
|
|
{ |
98
|
|
|
$tableUniqueId = $details['TABLE_SCHEMA'] . '.' . $details['TABLE_NAME']; |
99
|
|
|
if ($details['COLUMN_COMMENT'] != '') { |
100
|
|
|
return $details['COLUMN_COMMENT']; |
101
|
|
|
} elseif (isset($this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']])) { |
102
|
|
|
return $this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']]; |
103
|
|
|
} |
104
|
|
|
return $details['COLUMN_NAME']; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns a Date field 2 use in a form |
109
|
|
|
* |
110
|
|
|
* @param array $value |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
private function getFieldOutputDate($value) |
114
|
|
|
{ |
115
|
|
|
$defaultValue = $this->getFieldValue($value); |
116
|
|
|
if (is_null($defaultValue)) { |
117
|
|
|
$defaultValue = date('Y-m-d'); |
118
|
|
|
} |
119
|
|
|
$inA = [ |
120
|
|
|
'type' => 'text', |
121
|
|
|
'name' => $value['Field'], |
122
|
|
|
'id' => $value['Field'], |
123
|
|
|
'value' => $defaultValue, |
124
|
|
|
'size' => 10, |
125
|
|
|
'maxlength' => 10, |
126
|
|
|
'onfocus' => implode('', [ |
127
|
|
|
'javascript:NewCssCal(\'' . $value['Field'], |
128
|
|
|
'\',\'yyyyMMdd\',\'dropdown\',false,\'24\',false);', |
129
|
|
|
]), |
130
|
|
|
]; |
131
|
|
|
return $this->setStringIntoShortTag('input', $inA) . $this->setCalendarControl($value['Field']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns a Enum or Set field to use in form |
136
|
|
|
* |
137
|
|
|
* @param string $tblSrc |
138
|
|
|
* @param string $fldType |
139
|
|
|
* @param array $val |
140
|
|
|
* @param array $iar |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
private function getFieldOutputEnumSet($tblSrc, $fldType, $val, $iar = []) |
144
|
|
|
{ |
145
|
|
|
$adnlThings = $this->establishDefaultEnumSet($fldType); |
146
|
|
|
if (array_key_exists('readonly', $val)) { |
147
|
|
|
return $this->getFieldOutputEnumSetReadOnly($val, $adnlThings); |
148
|
|
|
} |
149
|
|
|
$inAdtnl = $adnlThings['additional']; |
150
|
|
|
if ($iar !== []) { |
151
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
152
|
|
|
} |
153
|
|
|
$vlSlct = explode(',', $this->getFieldValue($val)); |
154
|
|
|
$slctOptns = $this->getSetOrEnum2Array($tblSrc, $val['COLUMN_NAME']); |
155
|
|
|
return $this->setArrayToSelect($slctOptns, $vlSlct, $val['COLUMN_NAME'] . $adnlThings['suffix'], $inAdtnl); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Creats an input for ENUM or SET if marked Read-Only |
160
|
|
|
* |
161
|
|
|
* @param array $val |
162
|
|
|
* @param array $adnlThings |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
private function getFieldOutputEnumSetReadOnly($val, $adnlThings) |
166
|
|
|
{ |
167
|
|
|
$inputFeatures = [ |
168
|
|
|
'name' => $val['COLUMN_NAME'] . $adnlThings['suffix'], |
169
|
|
|
'id' => $val['COLUMN_NAME'], |
170
|
|
|
'readonly' => 'readonly', |
171
|
|
|
'class' => 'input_readonly', |
172
|
|
|
'size' => 50, |
173
|
|
|
'value' => $this->getFieldValue($val), |
174
|
|
|
]; |
175
|
|
|
return $this->setStringIntoShortTag('input', $inputFeatures); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns a Numeric field 2 use in a form |
180
|
|
|
* |
181
|
|
|
* @param string $tblSrc |
182
|
|
|
* @param array $value |
183
|
|
|
* @param array $iar |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
private function getFieldOutputNumeric($tblSrc, $value, $iar = []) |
187
|
|
|
{ |
188
|
|
|
if ($value['EXTRA'] == 'auto_increment') { |
189
|
|
|
return $this->getFieldOutputNumericAI($value, $iar); |
190
|
|
|
} |
191
|
|
|
$fkArray = $this->getForeignKeysToArray($this->advCache['workingDatabase'], $tblSrc, $value['COLUMN_NAME']); |
192
|
|
|
if (is_null($fkArray)) { |
193
|
|
|
$fldNos = $this->setFieldNumbers($value); |
194
|
|
|
return $this->getFieldOutputTT($value, min(50, $fldNos['l']), $iar); |
195
|
|
|
} |
196
|
|
|
return $this->getFieldOutputNumericNonFK($fkArray, $value, $iar); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Handles creation of Auto Increment numeric field type output |
201
|
|
|
* |
202
|
|
|
* @param array $value |
203
|
|
|
* @param array $iar |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
private function getFieldOutputNumericAI($value, $iar = []) |
207
|
|
|
{ |
208
|
|
|
if ($this->getFieldValue($value) == '') { |
209
|
|
|
$spF = ['id' => $value['COLUMN_NAME'], 'style' => 'font-style:italic;']; |
210
|
|
|
return $this->setStringIntoTag('auto-numar', 'span', $spF); |
211
|
|
|
} |
212
|
|
|
$inAdtnl = [ |
213
|
|
|
'type' => 'hidden', |
214
|
|
|
'name' => $value['COLUMN_NAME'], |
215
|
|
|
'id' => $value['COLUMN_NAME'], |
216
|
|
|
'value' => $this->getFieldValue($value), |
217
|
|
|
]; |
218
|
|
|
if ($iar !== []) { |
219
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
220
|
|
|
} |
221
|
|
|
return '<b>' . $this->getFieldValue($value) . '</b>' . $this->setStringIntoShortTag('input', $inAdtnl); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Builds field output type for numeric types if not FK |
226
|
|
|
* |
227
|
|
|
* @param array $fkArray |
228
|
|
|
* @param array $value |
229
|
|
|
* @param array $iar |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
private function getFieldOutputNumericNonFK($fkArray, $value, $iar = []) |
233
|
|
|
{ |
234
|
|
|
$query = $this->sQueryGenericSelectKeyValue([ |
235
|
|
|
$fkArray[$value['COLUMN_NAME']][1], |
236
|
|
|
$fkArray[$value['COLUMN_NAME']][2], |
237
|
|
|
$fkArray[$value['COLUMN_NAME']][0], |
238
|
|
|
]); |
239
|
|
|
$selectOptions = $this->setMySQLquery2Server($query, 'array_key_value')['result']; |
240
|
|
|
$selectValue = $this->getFieldValue($value); |
241
|
|
|
$inAdtnl = ['size' => 1]; |
242
|
|
|
if ($value['IS_NULLABLE'] == 'YES') { |
243
|
|
|
$inAdtnl = array_merge($inAdtnl, ['include_null']); |
244
|
|
|
} |
245
|
|
|
if ($iar !== []) { |
246
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
247
|
|
|
} |
248
|
|
|
return $this->setArrayToSelect($selectOptions, $selectValue, $value['COLUMN_NAME'], $inAdtnl); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Returns a Char field 2 use in a form |
253
|
|
|
* |
254
|
|
|
* @param string $tbl |
255
|
|
|
* @param string $fieldType |
256
|
|
|
* @param array $value |
257
|
|
|
* @param array $iar |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
|
|
private function getFieldOutputText($tbl, $fieldType, $value, $iar = []) |
261
|
|
|
{ |
262
|
|
|
if (!in_array($fieldType, ['char', 'tinytext', 'varchar'])) { |
263
|
|
|
return ''; |
264
|
|
|
} |
265
|
|
|
$foreignKeysArray = $this->getFieldOutputTextPrerequisites($tbl, $value); |
266
|
|
|
if (!is_null($foreignKeysArray)) { |
267
|
|
|
return $this->getFieldOutputTextFK($foreignKeysArray, $value, $iar); |
268
|
|
|
} |
269
|
|
|
return $this->getFieldOutputTextNonFK($value, $iar); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Returns a Text field 2 use in a form |
274
|
|
|
* |
275
|
|
|
* @param string $fieldType |
276
|
|
|
* @param array $value |
277
|
|
|
* @param array $iar |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
private function getFieldOutputTextLarge($fieldType, $value, $iar = []) |
281
|
|
|
{ |
282
|
|
|
if (!in_array($fieldType, ['blob', 'text'])) { |
283
|
|
|
return ''; |
284
|
|
|
} |
285
|
|
|
$inAdtnl = [ |
286
|
|
|
'name' => $value['COLUMN_NAME'], |
287
|
|
|
'id' => $value['COLUMN_NAME'], |
288
|
|
|
'rows' => 4, |
289
|
|
|
'cols' => 55, |
290
|
|
|
]; |
291
|
|
|
if ($iar !== []) { |
292
|
|
|
$inAdtnl = array_merge($inAdtnl, $iar); |
293
|
|
|
} |
294
|
|
|
return $this->setStringIntoTag($this->getFieldValue($value), 'textarea', $inAdtnl); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Prepares the text output fields |
299
|
|
|
* |
300
|
|
|
* @param string $tbl |
301
|
|
|
* @param array $value |
302
|
|
|
* @return null|array |
303
|
|
|
*/ |
304
|
|
|
private function getFieldOutputTextPrerequisites($tbl, $value) |
305
|
|
|
{ |
306
|
|
|
$foreignKeysArray = null; |
307
|
|
|
if (($tbl != 'user_rights') && ($value['COLUMN_NAME'] != 'eid')) { |
308
|
|
|
$database = $this->advCache['workingDatabase']; |
309
|
|
|
if (strpos($tbl, '`.`')) { |
310
|
|
|
$database = substr($tbl, 0, strpos($tbl, '`.`')); |
311
|
|
|
} |
312
|
|
|
$foreignKeysArray = $this->getForeignKeysToArray($database, $tbl, $value['COLUMN_NAME']); |
313
|
|
|
} |
314
|
|
|
return $foreignKeysArray; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Returns a Time field 2 use in a form |
319
|
|
|
* |
320
|
|
|
* @param array $value |
321
|
|
|
* @param array $iar |
322
|
|
|
* @return string |
323
|
|
|
*/ |
324
|
|
|
private function getFieldOutputTime($value, $iar = []) |
325
|
|
|
{ |
326
|
|
|
return $this->getFieldOutputTT($value, 8, $iar); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Returns a Timestamp field 2 use in a form |
331
|
|
|
* |
332
|
|
|
* @param array $dtl |
333
|
|
|
* @param array $iar |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
|
|
private function getFieldOutputTimestamp($dtl, $iar = []) |
337
|
|
|
{ |
338
|
|
|
if (($dtl['COLUMN_DEFAULT'] == 'CURRENT_TIMESTAMP') || ($dtl['EXTRA'] == 'on update CURRENT_TIMESTAMP')) { |
339
|
|
|
return $this->getTimestamping($dtl)['input']; |
340
|
|
|
} |
341
|
|
|
$input = $this->getFieldOutputTT($dtl, 19, $iar); |
342
|
|
|
if (!array_key_exists('readonly', $iar)) { |
343
|
|
|
$input .= $this->setCalendarControlWithTime($dtl['COLUMN_NAME']); |
344
|
|
|
} |
345
|
|
|
return $input; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Returns a Year field 2 use in a form |
350
|
|
|
* |
351
|
|
|
* @param array $details |
352
|
|
|
* @param array $iar |
353
|
|
|
* @return string |
354
|
|
|
*/ |
355
|
|
|
private function getFieldOutputYear($tblName, $details, $iar) |
356
|
|
|
{ |
357
|
|
|
$listOfValues = []; |
358
|
|
|
for ($cntr = 1901; $cntr <= 2155; $cntr++) { |
359
|
|
|
$listOfValues[$cntr] = $cntr; |
360
|
|
|
} |
361
|
|
|
if ($iar == []) { |
362
|
|
|
$slDflt = $this->getFieldValue($details); |
363
|
|
|
return $this->setArrayToSelect($listOfValues, $slDflt, $details['COLUMN_NAME'], ['size' => 1]); |
364
|
|
|
} |
365
|
|
|
return $this->getFieldOutputText($tblName, 'varchar', $details, $iar); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Returns an array with fields referenced by a Foreign key |
370
|
|
|
* |
371
|
|
|
* @param string $database |
372
|
|
|
* @param string $tblName |
373
|
|
|
* @param string|array $onlyCol |
374
|
|
|
* @return array |
375
|
|
|
*/ |
376
|
|
|
private function getForeignKeysToArray($database, $tblName, $onlyCol = '') |
377
|
|
|
{ |
378
|
|
|
$this->setTableForeignKeyCache($database, $this->fixTableSource($tblName)); |
379
|
|
|
$array2return = null; |
380
|
|
|
if (isset($this->advCache['tableFKs'][$database][$tblName])) { |
381
|
|
|
foreach ($this->advCache['tableFKs'][$database][$tblName] as $value) { |
382
|
|
|
if ($value['COLUMN_NAME'] == $onlyCol) { |
383
|
|
|
$query = $this->getForeignKeysQuery($value); |
384
|
|
|
$targetTblTxtFlds = $this->setMySQLquery2Server($query, 'full_array_key_numbered')['result']; |
385
|
|
|
$array2return[$onlyCol] = [ |
386
|
|
|
$this->glueDbTb($value['REFERENCED_TABLE_SCHEMA'], $value['REFERENCED_TABLE_NAME']), |
387
|
|
|
$value['REFERENCED_COLUMN_NAME'], |
388
|
|
|
'`' . $targetTblTxtFlds[0]['COLUMN_NAME'] . '`', |
389
|
|
|
]; |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
return $array2return; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Build label html tag |
398
|
|
|
* |
399
|
|
|
* @param array $details |
400
|
|
|
* @return string |
401
|
|
|
*/ |
402
|
|
|
private function getLabel($details) |
403
|
|
|
{ |
404
|
|
|
return '<span class="fake_label">' . $this->getFieldNameForDisplay($details) . '</span>'; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Returns an array with possible values of a SET or ENUM column |
409
|
|
|
* |
410
|
|
|
* @param string $refTbl |
411
|
|
|
* @param string $refCol |
412
|
|
|
* @return array |
413
|
|
|
*/ |
414
|
|
|
protected function getSetOrEnum2Array($refTbl, $refCol) |
415
|
|
|
{ |
416
|
|
|
$dat = $this->establishDatabaseAndTable($refTbl); |
417
|
|
|
foreach ($this->advCache['tableStructureCache'][$dat[0]][$dat[1]] as $value) { |
418
|
|
|
if ($value['COLUMN_NAME'] == $refCol) { |
419
|
|
|
$clndVls = explode(',', str_replace([$value['DATA_TYPE'], '(', "'", ')'], '', $value['COLUMN_TYPE'])); |
420
|
|
|
$enmVls = array_combine($clndVls, $clndVls); |
421
|
|
|
if ($value['IS_NULLABLE'] === 'YES') { |
422
|
|
|
$enmVls['NULL'] = ''; |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
ksort($enmVls); |
427
|
|
|
return $enmVls; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Returns a timestamp field value |
432
|
|
|
* |
433
|
|
|
* @param array $dtl |
434
|
|
|
* @return array |
435
|
|
|
*/ |
436
|
|
|
private function getTimestamping($dtl) |
437
|
|
|
{ |
438
|
|
|
$fieldValue = $this->getFieldValue($dtl); |
439
|
|
|
$inM = $this->setStringIntoTag($fieldValue, 'span'); |
440
|
|
|
if (in_array($fieldValue, ['', 'CURRENT_TIMESTAMP', 'NULL'])) { |
441
|
|
|
$mCN = [ |
442
|
|
|
'InsertDateTime' => 'data/timpul ad. informatiei', |
443
|
|
|
'ModificationDateTime' => 'data/timpul modificarii inf.', |
444
|
|
|
'modification_datetime' => 'data/timpul modificarii inf.', |
445
|
|
|
]; |
446
|
|
|
if (array_key_exists($dtl['COLUMN_NAME'], $mCN)) { |
447
|
|
|
$inM = $this->setStringIntoTag($mCN[$dtl['COLUMN_NAME']], 'span', ['style' => 'font-style:italic;']); |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
return ['label' => $this->getLabel($dtl), 'input' => $inM]; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Builds field output w. special column name |
455
|
|
|
* |
456
|
|
|
* @param string $tableSource |
457
|
|
|
* @param array $dtl |
458
|
|
|
* @param array $features |
459
|
|
|
* @param string $fieldLabel |
460
|
|
|
* @return array |
461
|
|
|
*/ |
462
|
|
|
private function setField($tableSource, $dtl, $features, $fieldLabel) |
463
|
|
|
{ |
464
|
|
|
if ($dtl['COLUMN_NAME'] == 'host') { |
465
|
|
|
$inVl = gethostbyaddr($this->tCmnRequest->server->get('REMOTE_ADDR')); |
466
|
|
|
return [ |
467
|
|
|
'label' => '<label for="' . $dtl['COLUMN_NAME'] . '">Numele calculatorului</label>', |
468
|
|
|
'input' => '<input type="text" name="host" size="15" readonly value="' . $inVl . '" />', |
469
|
|
|
]; |
470
|
|
|
} |
471
|
|
|
$result = $this->setFieldInput($tableSource, $dtl, $features); |
472
|
|
|
return ['label' => $this->setFieldLabel($dtl, $features, $fieldLabel), 'input' => $result]; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* Builds field output w. another special column name |
477
|
|
|
* |
478
|
|
|
* @param string $tableSource |
479
|
|
|
* @param array $dtl |
480
|
|
|
* @param array $features |
481
|
|
|
* @return string |
482
|
|
|
*/ |
483
|
|
|
private function setFieldInput($tableSource, $dtl, $features) |
484
|
|
|
{ |
485
|
|
|
if ($dtl['COLUMN_NAME'] == 'ChoiceId') { |
486
|
|
|
return '<input type="text" name="ChoiceId" value="' |
487
|
|
|
. $this->tCmnRequest->request->get($dtl['COLUMN_NAME']) . '" />'; |
488
|
|
|
} |
489
|
|
|
return $this->setNeededFieldByType($tableSource, $dtl, $features); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Returns a generic form based on a given table |
494
|
|
|
* |
495
|
|
|
* @param string $tblSrc |
496
|
|
|
* @param array $feat |
497
|
|
|
* @param array $hdnInf |
498
|
|
|
* |
499
|
|
|
* @return string Form to add/modify detail for a single row within a table |
500
|
|
|
*/ |
501
|
|
|
protected function setFormGenericSingleRecord($tblSrc, $feat, $hdnInf = []) |
502
|
|
|
{ |
503
|
|
|
echo $this->setStringIntoTag('', 'div', ['id' => 'loading']); |
504
|
|
|
$this->setTableCache($tblSrc); |
505
|
|
|
if (strpos($tblSrc, '.') !== false) { |
506
|
|
|
$tblSrc = explode('.', str_replace('`', '', $tblSrc))[1]; |
507
|
|
|
} |
508
|
|
|
$sReturn = []; |
509
|
|
|
if (count($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc]) != 0) { |
510
|
|
|
foreach ($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc] as $value) { |
511
|
|
|
$sReturn[] = $this->setNeededField($tblSrc, $value, $feat); |
512
|
|
|
} |
513
|
|
|
} |
514
|
|
|
$frmFtrs = ['id' => $feat['id'], 'action' => $feat['action'], 'method' => $feat['method']]; |
515
|
|
|
return $this->setStringIntoTag(implode('', $sReturn) . $this->setFormButtons($feat, $hdnInf), 'form', $frmFtrs) |
516
|
|
|
. $this->setFormJavascriptFinal($feat['id']); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Analyse the field and returns the proper line 2 use in forms |
521
|
|
|
* |
522
|
|
|
* @param string $tableSource |
523
|
|
|
* @param array $details |
524
|
|
|
* @param array $features |
525
|
|
|
* @return string|array |
526
|
|
|
*/ |
527
|
|
|
private function setNeededField($tableSource, $details, $features) |
528
|
|
|
{ |
529
|
|
|
if (isset($features['hidden'])) { |
530
|
|
|
if (in_array($details['COLUMN_NAME'], $features['hidden'])) { |
531
|
|
|
return null; |
532
|
|
|
} |
533
|
|
|
} |
534
|
|
|
$fieldLabel = $this->getFieldNameForDisplay($details); |
535
|
|
|
if ($fieldLabel == 'hidden') { |
536
|
|
|
return null; |
537
|
|
|
} |
538
|
|
|
return $this->setNeededFieldFinal($tableSource, $details, $features, $fieldLabel); |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Analyse the field type and returns the proper lines 2 use in forms |
543
|
|
|
* |
544
|
|
|
* @param string $tblName |
545
|
|
|
* @param array $dtls |
546
|
|
|
* @param array $features |
547
|
|
|
* @return string|array |
548
|
|
|
*/ |
549
|
|
|
private function setNeededFieldByType($tblName, $dtls, $features) |
550
|
|
|
{ |
551
|
|
|
if (isset($features['special']) && isset($features['special'][$dtls['COLUMN_NAME']])) { |
552
|
|
|
$sOpt = $this->setMySQLquery2Server($features['special'][$dtls['COLUMN_NAME']], 'array_key_value'); |
553
|
|
|
return $this->setArrayToSelect($sOpt, $this->getFieldValue($dtls), $dtls['COLUMN_NAME'], ['size' => 1]); |
|
|
|
|
554
|
|
|
} |
555
|
|
|
return $this->setNeededFieldKnown($tblName, $dtls, $features); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
private function setNeededFieldKnown($tblName, $dtls, $features) |
559
|
|
|
{ |
560
|
|
|
$iar = $this->handleFeatures($dtls['COLUMN_NAME'], $features); |
561
|
|
|
$sReturn = ''; |
562
|
|
|
$numTypes = ['bigint', 'int', 'mediumint', 'smallint', 'tinyint', 'float', 'double', 'decimal', 'numeric']; |
563
|
|
|
if (in_array($dtls['DATA_TYPE'], $numTypes)) { |
564
|
|
|
$sReturn = $this->getFieldOutputNumeric($tblName, $dtls, $iar); |
565
|
|
|
} elseif (in_array($dtls['DATA_TYPE'], ['char', 'tinytext', 'varchar', 'enum', 'set', 'text', 'blob'])) { |
566
|
|
|
$sReturn = $this->setNeededFieldTextRelated($tblName, $dtls, $iar); |
567
|
|
|
} elseif (in_array($dtls['DATA_TYPE'], ['date', 'datetime', 'time', 'timestamp', 'year'])) { |
568
|
|
|
$sReturn = $this->setNeededFieldSingleType($tblName, $dtls, $iar); |
569
|
|
|
} |
570
|
|
|
return $this->getFieldCompletionType($dtls) . $sReturn; |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
private function setNeededFieldFinal($tableSource, $details, $features, $fieldLabel) |
574
|
|
|
{ |
575
|
|
|
$sReturn = $this->setField($tableSource, $details, $features, $fieldLabel); |
576
|
|
|
$lmts = $this->setFieldNumbers($details); |
577
|
|
|
return '<div>' . $sReturn['label'] |
578
|
|
|
. $this->setStringIntoTag($sReturn['input'], 'span', ['class' => 'labell']) |
579
|
|
|
. '<span style="font-size:x-small;font-style:italic;"> (max. ' |
580
|
|
|
. $lmts['M'] . (isset($lmts['d']) ? ' w. ' . $lmts['d'] . ' decimals' : '') . ')</span>' |
581
|
|
|
. '</div>'; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
private function setNeededFieldSingleType($tblName, $dtls, $iar) |
585
|
|
|
{ |
586
|
|
|
if ($dtls['DATA_TYPE'] == 'date') { |
587
|
|
|
return $this->getFieldOutputDate($dtls); |
588
|
|
|
} elseif ($dtls['DATA_TYPE'] == 'time') { |
589
|
|
|
return $this->getFieldOutputTime($dtls, $iar); |
590
|
|
|
} elseif (in_array($dtls['DATA_TYPE'], ['datetime', 'timestamp'])) { |
591
|
|
|
return $this->getFieldOutputTimestamp($dtls, $iar); |
592
|
|
|
} |
593
|
|
|
return $this->getFieldOutputYear($tblName, $dtls, $iar); |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
private function setNeededFieldTextRelated($tblName, $dtls, $iar) |
597
|
|
|
{ |
598
|
|
|
if (in_array($dtls['DATA_TYPE'], ['char', 'tinytext', 'varchar'])) { |
599
|
|
|
return $this->getFieldOutputText($tblName, $dtls['DATA_TYPE'], $dtls, $iar); |
600
|
|
|
} elseif (in_array($dtls['DATA_TYPE'], ['text', 'blob'])) { |
601
|
|
|
return $this->getFieldOutputTextLarge($dtls['DATA_TYPE'], $dtls, $iar); |
602
|
|
|
} |
603
|
|
|
return $this->getFieldOutputEnumSet($tblName, $dtls['DATA_TYPE'], $dtls, $iar); |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* create a Cache for given table to use it in many places |
608
|
|
|
* |
609
|
|
|
* @param string $tblSrc |
610
|
|
|
*/ |
611
|
|
|
private function setTableCache($tblSrc) |
612
|
|
|
{ |
613
|
|
|
$dat = $this->establishDatabaseAndTable($tblSrc); |
614
|
|
|
if (!isset($this->advCache['tableStructureCache'][$dat[0]][$dat[1]])) { |
615
|
|
|
$this->advCache['workingDatabase'] = $dat[0]; |
616
|
|
|
$this->advCache['tableStructureCache'][$dat[0]][$dat[1]] = $this->getMySQLlistColumns([ |
617
|
|
|
'TABLE_SCHEMA' => $dat[0], |
618
|
|
|
'TABLE_NAME' => $dat[1], |
619
|
|
|
]); |
620
|
|
|
$this->setTableForeignKeyCache($dat[0], $dat[1]); |
621
|
|
|
} |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
private function setTableForeignKeyCache($dbName, $tblName) |
625
|
|
|
{ |
626
|
|
|
$frgnKs = $this->getMySQLlistIndexes([ |
627
|
|
|
'TABLE_SCHEMA' => $dbName, |
628
|
|
|
'TABLE_NAME' => $tblName, |
629
|
|
|
'REFERENCED_TABLE_NAME' => 'NOT NULL', |
630
|
|
|
]); |
631
|
|
|
if (!is_null($frgnKs)) { |
632
|
|
|
$this->advCache['tableFKs'][$dbName][$tblName] = $frgnKs; |
633
|
|
|
$this->advCache['FKcol'][$dbName][$tblName] = array_column($frgnKs, 'COLUMN_NAME', 'CONSTRAINT_NAME'); |
634
|
|
|
} |
635
|
|
|
} |
636
|
|
|
} |
637
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.