Completed
Push — master ( a26bed...c8fc06 )
by Daniel
02:25
created

MySQLiAdvancedOutput::getFieldOutputNumericAI()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 12
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 results
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait MySQLiAdvancedOutput
37
{
38
39
    use MySQLiByDanielGP;
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 (!defined('MYSQL_DATABASE')) {
53
                define('MYSQL_DATABASE', 'information_schema');
54
            }
55
            return [$tblSrc, MYSQL_DATABASE];
56
        }
57
        return explode('.', str_replace('`', '', $tblSrc));
58
    }
59
60
    private function establishDefaultEnumSet($fldType)
61
    {
62
        $dfltArray = [
63
            'enum' => ['additional' => ['size' => 1], 'suffix' => ''],
64
            'set'  => ['additional' => ['size' => 5, 'multiselect'], 'suffix' => '[]'],
65
        ];
66
        return $dfltArray[$fldType];
67
    }
68
69
    private function fixTableSource($refTbl)
70
    {
71
        $outS = [];
72
        if (substr($refTbl, 0, 1) !== '`') {
73
            $outS[] = '`';
74
        }
75
        $psT = strpos($refTbl, '.`');
76
        if ($psT !== false) {
77
            $refTbl = substr($refTbl, $psT + 2, strlen($refTbl) - $psT);
78
        }
79
        $outS[] = $refTbl;
80
        if (substr($refTbl, -1) !== '`') {
81
            $outS[] = '`';
82
        }
83
        return implode('', $outS);
84
    }
85
86
    private function getFieldCompletionType($details)
87
    {
88
        $inputFeatures = ['display' => '***', 'ftrs' => ['title' => 'Mandatory', 'class' => 'inputMandatory']];
89
        if ($details['IS_NULLABLE'] == 'YES') {
90
            $inputFeatures = ['display' => '~', 'ftrs' => ['title' => 'Optional', 'class' => 'inputOptional']];
91
        }
92
        return $this->setStringIntoTag($inputFeatures['display'], 'span', $inputFeatures['ftrs']);
93
    }
94
95
    /**
96
     * Returns the name of a field for displaying
97
     *
98
     * @param array $details
99
     * @return string
100
     */
101
    private function getFieldNameForDisplay($details)
102
    {
103
        $tableUniqueId = $details['TABLE_SCHEMA'] . '.' . $details['TABLE_NAME'];
104
        if ($details['COLUMN_COMMENT'] != '') {
105
            return $details['COLUMN_COMMENT'];
106
        } elseif (isset($this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']])) {
107
            return $this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']];
108
        }
109
        return $details['COLUMN_NAME'];
110
    }
111
112
    /**
113
     * Returns a Date field 2 use in a form
114
     *
115
     * @param array $value
116
     * @return string
117
     */
118
    private function getFieldOutputDate($value)
119
    {
120
        $defaultValue = $this->getFieldValue($value);
121
        if (is_null($defaultValue)) {
122
            $defaultValue = date('Y-m-d');
123
        }
124
        $inA = [
125
            'type'      => 'text',
126
            'name'      => $value['Field'],
127
            'id'        => $value['Field'],
128
            'value'     => $defaultValue,
129
            'size'      => 10,
130
            'maxlength' => 10,
131
            'onfocus'   => implode('', [
132
                'javascript:NewCssCal(\'' . $value['Field'],
133
                '\',\'yyyyMMdd\',\'dropdown\',false,\'24\',false);',
134
            ]),
135
        ];
136
        return $this->setStringIntoShortTag('input', $inA) . $this->setCalendarControl($value['Field']);
137
    }
138
139
    /**
140
     * Returns a Enum or Set field to use in form
141
     *
142
     * @param string $tblSrc
143
     * @param string $fldType
144
     * @param array $val
145
     * @param array $iar
146
     * @return string
147
     */
148
    private function getFieldOutputEnumSet($tblSrc, $fldType, $val, $iar = [])
149
    {
150
        $adnlThings = $this->establishDefaultEnumSet($fldType);
151
        if (array_key_exists('readonly', $val)) {
152
            return $this->getFieldOutputEnumSetReadOnly($val, $adnlThings);
153
        }
154
        $inAdtnl = $adnlThings['additional'];
155
        if ($iar !== []) {
156
            $inAdtnl = array_merge($inAdtnl, $iar);
157
        }
158
        $vlSlct    = explode(',', $this->getFieldValue($val));
159
        $slctOptns = $this->getSetOrEnum2Array($tblSrc, $val['COLUMN_NAME']);
160
        return $this->setArrayToSelect($slctOptns, $vlSlct, $val['COLUMN_NAME'] . $adnlThings['suffix'], $inAdtnl);
161
    }
162
163
    private function getFieldOutputEnumSetReadOnly($val, $adnlThings)
164
    {
165
        $inputFeatures = [
166
            'name'     => $val['COLUMN_NAME'] . $adnlThings['suffix'],
167
            'id'       => $val['COLUMN_NAME'],
168
            'readonly' => 'readonly',
169
            'class'    => 'input_readonly',
170
            'size'     => 50,
171
            'value'    => $this->getFieldValue($val),
172
        ];
173
        return $this->setStringIntoShortTag('input', $inputFeatures);
174
    }
175
176
    /**
177
     * Returns a Numeric field 2 use in a form
178
     *
179
     * @param string $tblSrc
180
     * @param array $value
181
     * @param array $iar
182
     * @return string
183
     */
184
    private function getFieldOutputNumeric($tblSrc, $value, $iar = [])
185
    {
186
        if ($value['EXTRA'] == 'auto_increment') {
187
            return $this->getFieldOutputNumericAI($tblSrc, $value, $iar);
188
        }
189
        $database = $this->advCache['workingDatabase'];
190
        $fkArray  = $this->getForeignKeysToArray($database, $tblSrc, $value['COLUMN_NAME']);
191
        if (is_null($fkArray)) {
192
            $fldNos = $this->setFieldNumbers($value);
193
            return $this->getFieldOutputTT($value, min(50, $fldNos['l']), $iar);
194
        }
195
        $query         = $this->sQueryGenericSelectKeyValue([
196
            $fkArray[$value['COLUMN_NAME']][1],
197
            $fkArray[$value['COLUMN_NAME']][2],
198
            $fkArray[$value['COLUMN_NAME']][0],
199
        ]);
200
        $selectOptions = $this->setMySQLquery2Server($query, 'array_key_value')['result'];
201
        $selectValue   = $this->getFieldValue($value);
202
        $inAdtnl       = ['size' => 1];
203
        if ($value['IS_NULLABLE'] == 'YES') {
204
            $inAdtnl = array_merge($inAdtnl, ['include_null']);
205
        }
206
        if ($iar !== []) {
207
            $inAdtnl = array_merge($inAdtnl, $iar);
208
        }
209
        return $this->setArrayToSelect($selectOptions, $selectValue, $value['COLUMN_NAME'], $inAdtnl);
210
    }
211
212
    private function getFieldOutputNumericAI($tblSrc, $value, $iar = [])
0 ignored issues
show
Unused Code introduced by
The parameter $tblSrc is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
213
    {
214
        if ($this->getFieldValue($value) == '') {
215
            $spF = ['id' => $value['COLUMN_NAME'], 'style' => 'font-style:italic;'];
216
            return $this->setStringIntoTag('auto-numar', 'span', $spF);
217
        }
218
        $inAdtnl = [
219
            'type'  => 'hidden',
220
            'name'  => $value['COLUMN_NAME'],
221
            'id'    => $value['COLUMN_NAME'],
222
            'value' => $this->getFieldValue($value),
223
        ];
224
        if ($iar !== []) {
225
            $inAdtnl = array_merge($inAdtnl, $iar);
226
        }
227
        return '<b>' . $this->getFieldValue($value) . '</b>' . $this->setStringIntoShortTag('input', $inAdtnl);
228
    }
229
230
    /**
231
     * Returns a Char field 2 use in a form
232
     *
233
     * @param string $tbl
234
     * @param string $fieldType
235
     * @param array $value
236
     * @param array $iar
237
     * @return string
238
     */
239
    private function getFieldOutputText($tbl, $fieldType, $value, $iar = [])
240
    {
241
        if (!in_array($fieldType, ['char', 'tinytext', 'varchar'])) {
242
            return '';
243
        }
244
        $foreignKeysArray = $this->getFieldOutputTextPrerequisites($tbl, $value);
245
        if (is_null($foreignKeysArray)) {
246
            return $this->getFieldOutputTextFK($foreignKeysArray, $value, $iar);
247
        }
248
        return $this->getFieldOutputTextNonFK($value, $iar);
249
    }
250
251
    private function getFieldOutputTextFK($foreignKeysArray, $value, $iar)
252
    {
253
        $query   = $this->sQueryGenericSelectKeyValue([
254
            $foreignKeysArray[$value['COLUMN_NAME']][1],
255
            $foreignKeysArray[$value['COLUMN_NAME']][2],
256
            $foreignKeysArray[$value['COLUMN_NAME']][0]
257
        ]);
258
        $inAdtnl = ['size' => 1];
259
        if ($value['IS_NULLABLE'] == 'YES') {
260
            $inAdtnl = array_merge($inAdtnl, ['include_null']);
261
        }
262
        if ($iar !== []) {
263
            $inAdtnl = array_merge($inAdtnl, $iar);
264
        }
265
        $slct = [
266
            'Options' => $this->setMySQLquery2Server($query, 'array_key_value'),
267
            'Value'   => $this->getFieldValue($value),
268
        ];
269
        return $this->setArrayToSelect($slct['Options'], $slct['Value'], $value['COLUMN_NAME'], $inAdtnl);
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
    private function getFieldOutputTextNonFK($value, $iar)
298
    {
299
        $fldNos  = $this->setFieldNumbers($value);
300
        $inAdtnl = [
301
            'type'      => ($value['COLUMN_NAME'] == 'password' ? 'password' : 'text'),
302
            'name'      => $value['COLUMN_NAME'],
303
            'id'        => $value['COLUMN_NAME'],
304
            'size'      => min(30, $fldNos['l']),
305
            'maxlength' => min(255, $fldNos['l']),
306
            'value'     => $this->getFieldValue($value),
307
        ];
308
        if ($iar !== []) {
309
            $inAdtnl = array_merge($inAdtnl, $iar);
310
        }
311
        return $this->setStringIntoShortTag('input', $inAdtnl);
312
    }
313
314
    private function getFieldOutputTextPrerequisites($tbl, $value)
315
    {
316
        $foreignKeysArray = null;
317
        if (($tbl != 'user_rights') && ($value['COLUMN_NAME'] != 'eid')) {
318
            $database = $this->advCache['workingDatabase'];
319
            if (strpos($tbl, '`.`')) {
320
                $database = substr($tbl, 0, strpos($tbl, '`.`'));
321
            }
322
            $foreignKeysArray = $this->getForeignKeysToArray($database, $tbl, $value['COLUMN_NAME']);
323
        }
324
        return $foreignKeysArray;
325
    }
326
327
    private function getFieldOutputTT($value, $szN, $iar = [])
328
    {
329
        $inAdtnl = [
330
            'id'        => $value['COLUMN_NAME'],
331
            'maxlength' => $szN,
332
            'name'      => $value['COLUMN_NAME'],
333
            'size'      => $szN,
334
            'type'      => 'text',
335
            'value'     => $this->getFieldValue($value),
336
        ];
337
        if ($iar !== []) {
338
            $inAdtnl = array_merge($inAdtnl, $iar);
339
        }
340
        return $this->setStringIntoShortTag('input', $inAdtnl);
341
    }
342
343
    /**
344
     * Returns a Time field 2 use in a form
345
     *
346
     * @param array $value
347
     * @param array $iar
348
     * @return string
349
     */
350
    private function getFieldOutputTime($value, $iar = [])
351
    {
352
        return $this->getFieldOutputTT($value, 8, $iar);
353
    }
354
355
    /**
356
     * Returns a Timestamp field 2 use in a form
357
     *
358
     * @param array $value
359
     * @param array $iar
360
     * @return string
361
     */
362
    private function getFieldOutputTimestamp($value, $iar = [])
363
    {
364
        $input = $this->getFieldOutputTT($value, 19, $iar);
365
        if (!array_key_exists('readonly', $iar)) {
366
            $input .= $this->setCalendarControlWithTime($value['COLUMN_NAME']);
367
        }
368
        return $input;
369
    }
370
371
    /**
372
     * Returns a Year field 2 use in a form
373
     *
374
     * @param array $details
375
     * @param array $iar
376
     * @return string
377
     */
378
    private function getFieldOutputYear($tblName, $details, $iar)
379
    {
380
        $listOfValues = [];
381
        for ($cntr = 1901; $cntr <= 2155; $cntr++) {
382
            $listOfValues[$cntr] = $cntr;
383
        }
384
        if ($iar == []) {
385
            $slDflt = $this->getFieldValue($details);
386
            return $this->setArrayToSelect($listOfValues, $slDflt, $details['COLUMN_NAME'], ['size' => 1]);
387
        }
388
        return $this->getFieldOutputText($tblName, 'varchar', $details, $iar);
389
    }
390
391
    /**
392
     * Returns given value for a field from $_REQUEST
393
     *
394
     * @param array $details
395
     * @return string
396
     */
397
    private function getFieldValue($details)
398
    {
399
        $this->initializeSprGlbAndSession();
400
        $rqCN = $this->tCmnRequest->request->get($details['COLUMN_NAME']);
401
        if (!is_null($rqCN)) {
402
            if (($details['IS_NULLABLE'] == 'YES') && ($rqCN == '')) {
403
                return 'NULL';
404
            }
405
            return $rqCN;
406
        }
407
        return $this->getFieldValueWithoutUserInput($details);
408
    }
409
410
    /**
411
     * Handles field value ignoring any input from the user
412
     *
413
     * @param array $details
414
     * @return string
415
     */
416
    private function getFieldValueWithoutUserInput($details)
417
    {
418
        if ($details['COLUMN_DEFAULT'] === null) {
419
            if ($details['IS_NULLABLE'] == 'YES') {
420
                return 'NULL';
421
            }
422
            return '';
423
        }
424
        return $details['COLUMN_DEFAULT'];
425
    }
426
427
    private function getForeignKeysQuery($value)
428
    {
429
        $flt = [
430
            'TABLE_SCHEMA' => $value['REFERENCED_TABLE_SCHEMA'],
431
            'TABLE_NAME'   => $value['REFERENCED_TABLE_NAME'],
432
            'DATA_TYPE'    => ['char', 'varchar', 'text'],
433
        ];
434
        return $this->sQueryMySqlColumns($flt);
435
    }
436
437
    /**
438
     * Returns an array with fields referenced by a Foreign key
439
     *
440
     * @param string $database
441
     * @param string $tblName
442
     * @param string|array $onlyCol
443
     * @return array
444
     */
445
    private function getForeignKeysToArray($database, $tblName, $onlyCol = '')
446
    {
447
        $this->setTableForeginKeyCache($database, $this->fixTableSource($tblName));
448
        $array2return = null;
449
        if (isset($this->advCache['tableFKs'][$database][$tblName])) {
450
            foreach ($this->advCache['tableFKs'][$database][$tblName] as $value) {
451
                if ($value['COLUMN_NAME'] == $onlyCol) {
452
                    $query                  = $this->getForeignKeysQuery($value);
453
                    $targetTblTxtFlds       = $this->setMySQLquery2Server($query, 'full_array_key_numbered')['result'];
454
                    $array2return[$onlyCol] = [
455
                        $this->glueDbTb($value['REFERENCED_TABLE_SCHEMA'], $value['REFERENCED_TABLE_NAME']),
456
                        $value['REFERENCED_COLUMN_NAME'],
457
                        '`' . $targetTblTxtFlds[0]['COLUMN_NAME'] . '`',
458
                    ];
459
                }
460
            }
461
        }
462
        return $array2return;
463
    }
464
465
    private function getLabel($details)
466
    {
467
        return $this->setStringIntoTag($this->getFieldNameForDisplay($details), 'span', ['class' => 'fake_label']);
468
    }
469
470
    /**
471
     * Returns an array with possible values of a SET or ENUM column
472
     *
473
     * @param string $refTbl
474
     * @param string $refCol
475
     * @return array
476
     */
477
    protected function getSetOrEnum2Array($refTbl, $refCol)
478
    {
479
        $dat = $this->establishDatabaseAndTable($this->fixTableSource($refTbl));
480
        foreach ($this->advCache['tableStructureCache'][$dat[0]][$dat[1]] as $value) {
481
            if ($value['COLUMN_NAME'] == $refCol) {
482
                $clndVls = explode(',', str_replace([$value['DATA_TYPE'], '(', "'", ')'], '', $value['COLUMN_TYPE']));
483
                $enmVls  = array_combine($clndVls, $clndVls);
484
                if ($value['IS_NULLABLE'] == 'YES') {
485
                    $enmVls['NULL'] = '';
486
                }
487
            }
488
        }
489
        ksort($enmVls);
490
        return $enmVls;
491
    }
492
493
    /**
494
     * Returns a timestamp field value
495
     *
496
     * @param array $dtl
497
     * @return array
498
     */
499
    private function getTimestamping($dtl)
500
    {
501
        $inM = $this->setStringIntoTag($this->getFieldValue($dtl), 'span');
502
        if (in_array($this->getFieldValue($dtl), ['', 'CURRENT_TIMESTAMP', 'NULL'])) {
503
            $mCN = [
504
                'InsertDateTime'        => 'data/timpul ad. informatiei',
505
                'ModificationDateTime'  => 'data/timpul modificarii inf.',
506
                'modification_datetime' => 'data/timpul modificarii inf.',
507
            ];
508
            if (array_key_exists($dtl['COLUMN_NAME'], $mCN)) {
509
                $inM = $this->setStringIntoTag($mCN[$dtl['COLUMN_NAME']], 'span', ['style' => 'font-style:italic;']);
510
            }
511
        }
512
        return ['label' => $this->getLabel($dtl), 'input' => $inM];
513
    }
514
515
    private function glueDbTb($dbName, $tbName)
516
    {
517
        return '`' . $dbName . '`.`' . $tbName . '`';
518
    }
519
520
    /**
521
     * Manages features flag
522
     *
523
     * @param string $fieldName
524
     * @param array $features
525
     * @return string
526
     */
527
    private function handleFeatures($fieldName, $features)
528
    {
529
        $rOly  = $this->handleFeaturesSingle($fieldName, $features, 'readonly');
530
        $rDbld = $this->handleFeaturesSingle($fieldName, $features, 'disabled');
531
        $rNl   = [];
532
        if (isset($features['include_null']) && in_array($fieldName, $features['include_null'])) {
533
            $rNl = ['include_null'];
534
        }
535
        return array_merge([], $rOly, $rDbld, $rNl);
536
    }
537
538
    private function handleFeaturesSingle($fieldName, $features, $featureKey)
539
    {
540
        $fMap    = [
541
            'readonly' => ['readonly', 'class', 'input_readonly'],
542
            'disabled' => ['disabled']
543
        ];
544
        $aReturn = [];
545
        if (array_key_exists($featureKey, $features)) {
546
            if (array_key_exists($fieldName, $features[$featureKey])) {
547
                $aReturn[$featureKey][$fMap[$featureKey][0]] = $fMap[$featureKey][0];
548
                if (count($fMap[$featureKey]) > 1) {
549
                    $aReturn[$featureKey][$fMap[$featureKey][1]] = $fMap[$featureKey][2];
550
                }
551
            }
552
        }
553
        return $aReturn;
554
    }
555
556
    /**
557
     * Returns a generic form based on a given table
558
     *
559
     * @param string $tblSrc Table Source
560
     * @param array $feat
561
     * @param string|array $hiddenInfo List of hidden fields
562
     *
563
     * @return string Form to add/modify detail for a single row within a table
564
     */
565
    protected function setFormGenericSingleRecord($tblSrc, $feat, $hiddenInfo = '')
566
    {
567
        echo $this->setStringIntoTag('', 'div', [
568
            'id' => 'loading'
569
        ]); // Ajax container
570
        if (strpos($tblSrc, '.') !== false) {
571
            $tblSrc = explode('.', str_replace('`', '', $tblSrc))[1];
572
        }
573
        $this->setTableCache($tblSrc);
574
        $sReturn = [];
575
        if (count($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc]) != 0) {
576
            foreach ($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc] as $value) {
577
                $sReturn[] = $this->setNeededField($tblSrc, $value, $feat);
578
            }
579
        }
580
        $btn                  = [];
581
        $btn[]                = $this->setStringIntoShortTag('input', [
582
            'type'  => 'submit',
583
            'id'    => 'submit',
584
            'style' => 'margin-left:220px;',
585
            'value' => $this->lclMsgCmn('i18n_Form_ButtonSave'),
586
        ]);
587
        $adtnlScriptAfterForm = $this->setJavascriptContent(implode('', [
588
            '$(document).ready(function(){',
589
            '$("form#' . $feat['id'] . '").submit(function(){',
590
            '$("input").attr("readonly", true);',
591
            '$("input[type=submit]").attr("disabled", "disabled");',
592
            '$("input[type=submit]").attr("value", "' . $this->lclMsgCmn('i18n_Form_ButtonSaving') . '");',
593
            '});',
594
            '});',
595
        ]));
596
        if (isset($feat['insertAndUpdate'])) {
597
            $btn[] = $this->setStringIntoShortTag('input', [
598
                'type'  => 'hidden',
599
                'id'    => 'insertAndUpdate',
600
                'name'  => 'insertAndUpdate',
601
                'value' => 'insertAndUpdate'
602
            ]);
603
        }
604
        $sReturn[] = $this->setStringIntoTag(implode('', $btn), 'div');
605
        if (isset($hiddenInfo)) {
606
            if (is_array($hiddenInfo)) {
607
                foreach ($hiddenInfo as $key => $value) {
608
                    $hiddenInput = $this->setStringIntoShortTag('input', [
609
                        'type'  => 'hidden',
610
                        'name'  => $key,
611
                        'id'    => $key,
612
                        'value' => $value,
613
                    ]);
614
                    $sReturn[]   = $this->setStringIntoTag($hiddenInput, 'div');
615
                }
616
            }
617
        }
618
        return $this->setStringIntoTag(implode('', $sReturn), 'form', [
619
                    'id'     => $feat['id'],
620
                    'action' => $feat['action'],
621
                    'method' => $feat['method']
622
                ])
623
                . $adtnlScriptAfterForm;
624
    }
625
626
    protected function setTableLocaleFields($localizationStrings)
627
    {
628
        $this->advCache['tableStructureLocales'] = $localizationStrings;
629
    }
630
631
    /**
632
     * Analyse the field and returns the proper line 2 use in forms
633
     *
634
     * @param string $tableSource
635
     * @param array $details
636
     * @param array $features
637
     * @return string|array
638
     */
639
    private function setNeededField($tableSource, $details, $features)
0 ignored issues
show
Coding Style introduced by
setNeededField uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setNeededField uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
640
    {
641
        if (isset($features['hidden'])) {
642
            if (in_array($details['COLUMN_NAME'], $features['hidden'])) {
643
                return null;
644
            }
645
        }
646
        $fieldLabel = $this->getFieldNameForDisplay($details);
647
        if ($fieldLabel == 'hidden') {
648
            return null;
649
        }
650
        $sReturn = [];
651
        switch ($details['COLUMN_NAME']) {
652
            case 'host':
653
                $sReturn['label'] = $this->setStringIntoTag('Numele calculatorului', 'label', [
654
                    'for' => $details['COLUMN_NAME']
655
                ]);
656
                $sReturn['input'] = $this->setStringIntoShortTag('input', [
657
                    'type'     => 'input',
658
                    'size'     => 15,
659
                    'readonly' => 'readonly',
660
                    'value'    => gethostbyaddr($_SERVER['REMOTE_ADDR'])
661
                ]);
662
                break;
663
            case 'InsertDateTime':
664
            case 'modification_datetime':
665
            case 'ModificationDateTime':
666
                $sReturn          = call_user_func_array([$this, 'getTimestamping'], [$details]);
667
                break;
668
            default:
669
                $aLabel           = [
670
                    'for' => $details['COLUMN_NAME'],
671
                    'id'  => $details['COLUMN_NAME'] . '_label'
672
                ];
673
                if (isset($features['disabled'])) {
674
                    if (in_array($details['COLUMN_NAME'], $features['disabled'])) {
675
                        $aLabel = array_merge($aLabel, ['style' => 'color: grey;']);
676
                    }
677
                }
678
                $sReturn['label'] = $this->setStringIntoTag($fieldLabel, 'label', $aLabel);
679
                $result           = $this->setNeededFieldByType($tableSource, $details, $features);
680
                if ($details['COLUMN_NAME'] == 'ChoiceId') {
681
                    $result = $this->setStringIntoShortTag('input', [
682
                        'type'  => 'text',
683
                        'name'  => $details['COLUMN_NAME'],
684
                        'value' => $_REQUEST[$details['COLUMN_NAME']]
685
                    ]);
686
                }
687
                $sReturn['input'] = $result;
688
                break;
689
        }
690
        $finalReturn = $sReturn['label'] . $this->setStringIntoTag($sReturn['input'], 'span', ['class' => 'labell']);
691
        $wrkDb       = $this->advCache['workingDatabase'];
692
        if (isset($this->advCache['tableFKs'][$wrkDb][$tableSource])) {
693
            if (in_array($details['COLUMN_NAME'], $this->advCache['FKcol'][$wrkDb][$tableSource])) {
694
                $finalReturn .= $this->setFieldNumbers($details);
695
            }
696
        }
697
        return $this->setStringIntoTag($finalReturn, 'div');
698
    }
699
700
    /**
701
     * Analyse the field type and returns the proper lines 2 use in forms
702
     *
703
     * @param string $tblName
704
     * @param array $details
705
     * @param array $features
706
     * @return string|array
707
     */
708
    private function setNeededFieldByType($tblName, $details, $features)
709
    {
710
        if (isset($features['special']) && isset($features['special'][$details['COLUMN_NAME']])) {
711
            $slctOpt = $this->setMySQLquery2Server($features['special'][$details['COLUMN_NAME']], 'array_key_value');
712
            return $this->setArrayToSelect($slctOpt, $this->getFieldValue($details), $details['COLUMN_NAME'], [
713
                        'size' => 1
714
            ]);
715
        }
716
        $iar      = $this->handleFeatures($details['COLUMN_NAME'], $features);
717
        $sReturn  = '';
718
        $numTypes = ['bigint', 'int', 'mediumint', 'smallint', 'tinyint', 'float', 'double', 'decimal', 'numeric'];
719
        if (in_array($details['DATA_TYPE'], $numTypes)) {
720
            $sReturn = $this->getFieldOutputNumeric($tblName, $details, $iar);
721
        } elseif (in_array($details['DATA_TYPE'], ['char', 'tinytext', 'varchar'])) {
722
            $sReturn = $this->getFieldOutputText($tblName, $details['DATA_TYPE'], $details, $iar);
723
        } elseif ($details['DATA_TYPE'] == 'date') {
724
            $sReturn = $this->getFieldOutputDate($details);
725
        } elseif (in_array($details['DATA_TYPE'], ['datetime', 'timestamp'])) {
726
            $sReturn = $this->getFieldOutputTimestamp($details, $iar);
727
        } elseif (in_array($details['DATA_TYPE'], ['enum', 'set'])) {
728
            $sReturn = $this->getFieldOutputEnumSet($tblName, $details['DATA_TYPE'], $details, $iar);
729
        } elseif (in_array($details['DATA_TYPE'], ['text', 'blob'])) {
730
            $sReturn = $this->getFieldOutputTextLarge($details['DATA_TYPE'], $details, $iar);
731
        } elseif ($details['DATA_TYPE'] == 'time') {
732
            $sReturn = $this->getFieldOutputTime($details, $iar);
733
        } elseif ($details['DATA_TYPE'] == 'year') {
734
            $sReturn = $this->getFieldOutputYear($tblName, $details, $iar);
735
        }
736
        return $this->getFieldCompletionType($details) . $sReturn;
737
    }
738
739
    /**
740
     * create a Cache for given table to use it in many places
741
     *
742
     * @param type $tblSrc
743
     */
744
    private function setTableCache($tblSrc)
745
    {
746
        $dat = $this->establishDatabaseAndTable($this->fixTableSource($tblSrc));
747
        if (!isset($this->advCache['tableStructureCache'][$dat[0]][$dat[1]])) {
748
            switch ($dat[1]) {
749
                case 'user_rights':
750
                    $this->advCache['workingDatabase'] = 'usefull_security';
751
                    break;
752
                default:
753
                    $this->advCache['workingDatabase'] = $dat[0];
754
                    break;
755
            }
756
            $this->advCache['tableStructureCache'][$dat[0]][$dat[1]] = $this->getMySQLlistColumns([
757
                'TABLE_SCHEMA' => $dat[0],
758
                'TABLE_NAME'   => $dat[1],
759
            ]);
760
            $this->setTableForeginKeyCache($dat[0], $dat[1]);
761
        }
762
    }
763
764
    private function setTableForeginKeyCache($dbName, $tblName)
765
    {
766
        $frgnKs = $this->getMySQLlistIndexes([
767
            'TABLE_SCHEMA'          => $dbName,
768
            'TABLE_NAME'            => $tblName,
769
            'REFERENCED_TABLE_NAME' => 'NOT NULL',
770
        ]);
771
        if (!is_null($frgnKs)) {
772
            $this->advCache['tableFKs'][$dbName][$tblName] = $frgnKs;
773
            $this->advCache['FKcol'][$dbName][$tblName]    = array_column($frgnKs, 'COLUMN_NAME', 'CONSTRAINT_NAME');
774
        }
775
    }
776
777
    private function setViewDeleteFeedbacks()
778
    {
779
        return [
780
            'Confirmation' => $this->lclMsgCmn('i18n_Action_Confirmation'),
781
            'Failed'       => $this->lclMsgCmn('i18n_ActionDelete_Failed'),
782
            'Impossible'   => $this->lclMsgCmn('i18n_ActionDelete_Impossible'),
783
            'Success'      => $this->lclMsgCmn('i18n_ActionDelete_Success'),
784
        ];
785
    }
786
787
    private function setViewDeletePackedFinal($sReturn)
788
    {
789
        $finalJavascript = $this->setJavascriptContent(implode('', [
790
            '$("#DeleteFeedback").fadeOut(4000, function() {',
791
            '$(this).remove();',
792
            '});',
793
        ]));
794
        return '<div id="DeleteFeedback">' . $sReturn . '</div>' . $finalJavascript;
795
    }
796
797
    /**
798
     * Automatic handler for Record deletion
799
     *
800
     * @param string $tbl
801
     * @param string $idn
802
     * @return string
803
     */
804
    protected function setViewModernDelete($tbl, $idn)
0 ignored issues
show
Coding Style introduced by
setViewModernDelete uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
805
    {
806
        $tMsg = $this->setViewDeleteFeedbacks();
807
        if ($tbl == '') {
808
            $sReturn = $this->setFeedbackModern('error', $tMsg['Confirmation'], $tMsg['Impossible']);
809
        } else {
810
            $this->setMySQLquery2Server($this->sQueryToDeleteSingleIdentifier([$tbl, $idn, $_REQUEST[$idn]]));
811
            $sReturn = $this->setFeedbackModern('error', $tMsg['Confirmation'], $tMsg['Failed'])
812
                    . '(' . $this->mySQLconnection->error . ')';
813
            if ($this->mySQLconnection->affected_rows > 0) {
814
                $sReturn = $this->setFeedbackModern('check', $tMsg['Confirmation'], $tMsg['Success']);
815
            }
816
        }
817
        return $this->setViewDeletePackedFinal($sReturn);
818
    }
819
}
820