Completed
Push — master ( 33c4fa...8fada7 )
by Daniel
02:22
created

MySQLiAdvancedOutput::handleFeaturesSingle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 17
rs 9.2
cc 4
eloc 11
nc 4
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 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
     * Adjust table name with proper sufix and prefix
77
     *
78
     * @param string $refTbl
79
     * @return string
80
     */
81
    private function fixTableSource($refTbl)
82
    {
83
        $outS = [];
84
        if (substr($refTbl, 0, 1) !== '`') {
85
            $outS[] = '`';
86
        }
87
        $psT = strpos($refTbl, '.`');
88
        if ($psT !== false) {
89
            $refTbl = substr($refTbl, $psT + 2, strlen($refTbl) - $psT);
90
        }
91
        $outS[] = $refTbl;
92
        if (substr($refTbl, -1) !== '`') {
93
            $outS[] = '`';
94
        }
95
        return implode('', $outS);
96
    }
97
98
    /**
99
     * Creates a mask to differentiate between Mandatory and Optional fields
100
     *
101
     * @param array $details
102
     * @return string
103
     */
104
    private function getFieldCompletionType($details)
105
    {
106
        $inputFeatures = ['display' => '***', 'ftrs' => ['title' => 'Mandatory', 'class' => 'inputMandatory']];
107
        if ($details['IS_NULLABLE'] == 'YES') {
108
            $inputFeatures = ['display' => '~', 'ftrs' => ['title' => 'Optional', 'class' => 'inputOptional']];
109
        }
110
        return $this->setStringIntoTag($inputFeatures['display'], 'span', $inputFeatures['ftrs']);
111
    }
112
113
    /**
114
     * Returns the name of a field for displaying
115
     *
116
     * @param array $details
117
     * @return string
118
     */
119
    private function getFieldNameForDisplay($details)
120
    {
121
        $tableUniqueId = $details['TABLE_SCHEMA'] . '.' . $details['TABLE_NAME'];
122
        if ($details['COLUMN_COMMENT'] != '') {
123
            return $details['COLUMN_COMMENT'];
124
        } elseif (isset($this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']])) {
125
            return $this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']];
126
        }
127
        return $details['COLUMN_NAME'];
128
    }
129
130
    /**
131
     * Returns a Date field 2 use in a form
132
     *
133
     * @param array $value
134
     * @return string
135
     */
136
    private function getFieldOutputDate($value)
137
    {
138
        $defaultValue = $this->getFieldValue($value);
139
        if (is_null($defaultValue)) {
140
            $defaultValue = date('Y-m-d');
141
        }
142
        $inA = [
143
            'type'      => 'text',
144
            'name'      => $value['Field'],
145
            'id'        => $value['Field'],
146
            'value'     => $defaultValue,
147
            'size'      => 10,
148
            'maxlength' => 10,
149
            'onfocus'   => implode('', [
150
                'javascript:NewCssCal(\'' . $value['Field'],
151
                '\',\'yyyyMMdd\',\'dropdown\',false,\'24\',false);',
152
            ]),
153
        ];
154
        return $this->setStringIntoShortTag('input', $inA) . $this->setCalendarControl($value['Field']);
155
    }
156
157
    /**
158
     * Returns a Enum or Set field to use in form
159
     *
160
     * @param string $tblSrc
161
     * @param string $fldType
162
     * @param array $val
163
     * @param array $iar
164
     * @return string
165
     */
166
    private function getFieldOutputEnumSet($tblSrc, $fldType, $val, $iar = [])
167
    {
168
        $adnlThings = $this->establishDefaultEnumSet($fldType);
169
        if (array_key_exists('readonly', $val)) {
170
            return $this->getFieldOutputEnumSetReadOnly($val, $adnlThings);
171
        }
172
        $inAdtnl = $adnlThings['additional'];
173
        if ($iar !== []) {
174
            $inAdtnl = array_merge($inAdtnl, $iar);
175
        }
176
        $vlSlct    = explode(',', $this->getFieldValue($val));
177
        $slctOptns = $this->getSetOrEnum2Array($tblSrc, $val['COLUMN_NAME']);
178
        return $this->setArrayToSelect($slctOptns, $vlSlct, $val['COLUMN_NAME'] . $adnlThings['suffix'], $inAdtnl);
179
    }
180
181
    /**
182
     * Creats an input for ENUM or SET if marked Read-Only
183
     *
184
     * @param array $val
185
     * @param array $adnlThings
186
     * @return string
187
     */
188
    private function getFieldOutputEnumSetReadOnly($val, $adnlThings)
189
    {
190
        $inputFeatures = [
191
            'name'     => $val['COLUMN_NAME'] . $adnlThings['suffix'],
192
            'id'       => $val['COLUMN_NAME'],
193
            'readonly' => 'readonly',
194
            'class'    => 'input_readonly',
195
            'size'     => 50,
196
            'value'    => $this->getFieldValue($val),
197
        ];
198
        return $this->setStringIntoShortTag('input', $inputFeatures);
199
    }
200
201
    /**
202
     * Returns a Numeric field 2 use in a form
203
     *
204
     * @param string $tblSrc
205
     * @param array $value
206
     * @param array $iar
207
     * @return string
208
     */
209
    private function getFieldOutputNumeric($tblSrc, $value, $iar = [])
210
    {
211
        if ($value['EXTRA'] == 'auto_increment') {
212
            return $this->getFieldOutputNumericAI($value, $iar);
213
        }
214
        $fkArray = $this->getForeignKeysToArray($this->advCache['workingDatabase'], $tblSrc, $value['COLUMN_NAME']);
215
        if (is_null($fkArray)) {
216
            $fldNos = $this->setFieldNumbers($value);
217
            return $this->getFieldOutputTT($value, min(50, $fldNos['l']), $iar);
218
        }
219
        return $this->getFieldOutputNumericNonFK($fkArray, $value, $iar);
220
    }
221
222
    /**
223
     * Handles creation of Auto Increment numeric field type output
224
     *
225
     * @param array $value
226
     * @param array $iar
227
     * @return string
228
     */
229
    private function getFieldOutputNumericAI($value, $iar = [])
230
    {
231
        if ($this->getFieldValue($value) == '') {
232
            $spF = ['id' => $value['COLUMN_NAME'], 'style' => 'font-style:italic;'];
233
            return $this->setStringIntoTag('auto-numar', 'span', $spF);
234
        }
235
        $inAdtnl = [
236
            'type'  => 'hidden',
237
            'name'  => $value['COLUMN_NAME'],
238
            'id'    => $value['COLUMN_NAME'],
239
            'value' => $this->getFieldValue($value),
240
        ];
241
        if ($iar !== []) {
242
            $inAdtnl = array_merge($inAdtnl, $iar);
243
        }
244
        return '<b>' . $this->getFieldValue($value) . '</b>' . $this->setStringIntoShortTag('input', $inAdtnl);
245
    }
246
247
    /**
248
     * Builds field output type for numeric types if not FK
249
     *
250
     * @param array $fkArray
251
     * @param array $value
252
     * @param array $iar
253
     * @return string
254
     */
255
    private function getFieldOutputNumericNonFK($fkArray, $value, $iar = [])
256
    {
257
        $query         = $this->sQueryGenericSelectKeyValue([
258
            $fkArray[$value['COLUMN_NAME']][1],
259
            $fkArray[$value['COLUMN_NAME']][2],
260
            $fkArray[$value['COLUMN_NAME']][0],
261
        ]);
262
        $selectOptions = $this->setMySQLquery2Server($query, 'array_key_value')['result'];
263
        $selectValue   = $this->getFieldValue($value);
264
        $inAdtnl       = ['size' => 1];
265
        if ($value['IS_NULLABLE'] == 'YES') {
266
            $inAdtnl = array_merge($inAdtnl, ['include_null']);
267
        }
268
        if ($iar !== []) {
269
            $inAdtnl = array_merge($inAdtnl, $iar);
270
        }
271
        return $this->setArrayToSelect($selectOptions, $selectValue, $value['COLUMN_NAME'], $inAdtnl);
272
    }
273
274
    /**
275
     * Returns a Char field 2 use in a form
276
     *
277
     * @param string $tbl
278
     * @param string $fieldType
279
     * @param array $value
280
     * @param array $iar
281
     * @return string
282
     */
283
    private function getFieldOutputText($tbl, $fieldType, $value, $iar = [])
284
    {
285
        if (!in_array($fieldType, ['char', 'tinytext', 'varchar'])) {
286
            return '';
287
        }
288
        $foreignKeysArray = $this->getFieldOutputTextPrerequisites($tbl, $value);
289
        if (!is_null($foreignKeysArray)) {
290
            return $this->getFieldOutputTextFK($foreignKeysArray, $value, $iar);
291
        }
292
        return $this->getFieldOutputTextNonFK($value, $iar);
293
    }
294
295
    /**
296
     * Prepares the output of text fields defined w. FKs
297
     *
298
     * @param array $foreignKeysArray
299
     * @param array $value
300
     * @param array $iar
301
     * @return string
302
     */
303
    private function getFieldOutputTextFK($foreignKeysArray, $value, $iar)
304
    {
305
        $query   = $this->sQueryGenericSelectKeyValue([
306
            $foreignKeysArray[$value['COLUMN_NAME']][1],
307
            $foreignKeysArray[$value['COLUMN_NAME']][2],
308
            $foreignKeysArray[$value['COLUMN_NAME']][0]
309
        ]);
310
        echo '<hr/>' . $query . '<hr/>';
311
        $inAdtnl = ['size' => 1];
312
        if ($value['IS_NULLABLE'] == 'YES') {
313
            $inAdtnl = array_merge($inAdtnl, ['include_null']);
314
        }
315
        if ($iar !== []) {
316
            $inAdtnl = array_merge($inAdtnl, $iar);
317
        }
318
        $slct = [
319
            'Options' => $this->setMySQLquery2Server($query, 'array_key_value'),
320
            'Value'   => $this->getFieldValue($value),
321
        ];
322
        return $this->setArrayToSelect($slct['Options'], $slct['Value'], $value['COLUMN_NAME'], $inAdtnl);
0 ignored issues
show
Bug introduced by
It seems like $slct['Options'] can also be of type string; however, danielgp\common_lib\DomC...lGP::setArrayToSelect() does only seem to accept array, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
323
    }
324
325
    /**
326
     * Returns a Text field 2 use in a form
327
     *
328
     * @param string $fieldType
329
     * @param array $value
330
     * @param array $iar
331
     * @return string
332
     */
333
    private function getFieldOutputTextLarge($fieldType, $value, $iar = [])
334
    {
335
        if (!in_array($fieldType, ['blob', 'text'])) {
336
            return '';
337
        }
338
        $inAdtnl = [
339
            'name' => $value['COLUMN_NAME'],
340
            'id'   => $value['COLUMN_NAME'],
341
            'rows' => 4,
342
            'cols' => 55,
343
        ];
344
        if ($iar !== []) {
345
            $inAdtnl = array_merge($inAdtnl, $iar);
346
        }
347
        return $this->setStringIntoTag($this->getFieldValue($value), 'textarea', $inAdtnl);
348
    }
349
350
    /**
351
     * Prepares the output of text fields w/o FKs
352
     *
353
     * @param array $value
354
     * @param array $iar
355
     * @return string
356
     */
357
    private function getFieldOutputTextNonFK($value, $iar)
358
    {
359
        $fldNos  = $this->setFieldNumbers($value);
360
        $inAdtnl = [
361
            'type'      => ($value['COLUMN_NAME'] == 'password' ? 'password' : 'text'),
362
            'name'      => $value['COLUMN_NAME'],
363
            'id'        => $value['COLUMN_NAME'],
364
            'size'      => min(30, $fldNos['M']),
365
            'maxlength' => min(255, $fldNos['M']),
366
            'value'     => $this->getFieldValue($value),
367
        ];
368
        if ($iar !== []) {
369
            $inAdtnl = array_merge($inAdtnl, $iar);
370
        }
371
        return $this->setStringIntoShortTag('input', $inAdtnl);
372
    }
373
374
    /**
375
     * Prepares the text output fields
376
     *
377
     * @param string $tbl
378
     * @param array $value
379
     * @return null|array
380
     */
381
    private function getFieldOutputTextPrerequisites($tbl, $value)
382
    {
383
        $foreignKeysArray = null;
384
        if (($tbl != 'user_rights') && ($value['COLUMN_NAME'] != 'eid')) {
385
            $database = $this->advCache['workingDatabase'];
386
            if (strpos($tbl, '`.`')) {
387
                $database = substr($tbl, 0, strpos($tbl, '`.`'));
388
            }
389
            $foreignKeysArray = $this->getForeignKeysToArray($database, $tbl, $value['COLUMN_NAME']);
390
        }
391
        return $foreignKeysArray;
392
    }
393
394
    /**
395
     * Builds output as text input type
396
     *
397
     * @param array $value
398
     * @param integer $szN
399
     * @param array $iar
400
     * @return string
401
     */
402
    private function getFieldOutputTT($value, $szN, $iar = [])
403
    {
404
        $inAdtnl = [
405
            'id'        => $value['COLUMN_NAME'],
406
            'maxlength' => $szN,
407
            'name'      => $value['COLUMN_NAME'],
408
            'size'      => $szN,
409
            'type'      => 'text',
410
            'value'     => $this->getFieldValue($value),
411
        ];
412
        if ($iar !== []) {
413
            $inAdtnl = array_merge($inAdtnl, $iar);
414
        }
415
        return $this->setStringIntoShortTag('input', $inAdtnl);
416
    }
417
418
    /**
419
     * Returns a Time field 2 use in a form
420
     *
421
     * @param array $value
422
     * @param array $iar
423
     * @return string
424
     */
425
    private function getFieldOutputTime($value, $iar = [])
426
    {
427
        return $this->getFieldOutputTT($value, 8, $iar);
428
    }
429
430
    /**
431
     * Returns a Timestamp field 2 use in a form
432
     *
433
     * @param array $dtl
434
     * @param array $iar
435
     * @return string
436
     */
437
    private function getFieldOutputTimestamp($dtl, $iar = [])
438
    {
439
        if (($dtl['COLUMN_DEFAULT'] == 'CURRENT_TIMESTAMP') || ($dtl['EXTRA'] == 'on update CURRENT_TIMESTAMP')) {
440
            return $this->getTimestamping($dtl)['input'];
441
        }
442
        $input = $this->getFieldOutputTT($dtl, 19, $iar);
443
        if (!array_key_exists('readonly', $iar)) {
444
            $input .= $this->setCalendarControlWithTime($dtl['COLUMN_NAME']);
445
        }
446
        return $input;
447
    }
448
449
    /**
450
     * Returns a Year field 2 use in a form
451
     *
452
     * @param array $details
453
     * @param array $iar
454
     * @return string
455
     */
456
    private function getFieldOutputYear($tblName, $details, $iar)
457
    {
458
        $listOfValues = [];
459
        for ($cntr = 1901; $cntr <= 2155; $cntr++) {
460
            $listOfValues[$cntr] = $cntr;
461
        }
462
        if ($iar == []) {
463
            $slDflt = $this->getFieldValue($details);
464
            return $this->setArrayToSelect($listOfValues, $slDflt, $details['COLUMN_NAME'], ['size' => 1]);
465
        }
466
        return $this->getFieldOutputText($tblName, 'varchar', $details, $iar);
467
    }
468
469
    /**
470
     * prepares the query to detect FKs
471
     *
472
     * @param array $value
473
     * @return string
474
     */
475
    private function getForeignKeysQuery($value)
476
    {
477
        $flt = [
478
            'TABLE_SCHEMA' => $value['REFERENCED_TABLE_SCHEMA'],
479
            'TABLE_NAME'   => $value['REFERENCED_TABLE_NAME'],
480
            'DATA_TYPE'    => ['char', 'varchar', 'text'],
481
        ];
482
        return $this->sQueryMySqlColumns($flt);
483
    }
484
485
    /**
486
     * Returns an array with fields referenced by a Foreign key
487
     *
488
     * @param string $database
489
     * @param string $tblName
490
     * @param string|array $onlyCol
491
     * @return array
492
     */
493
    private function getForeignKeysToArray($database, $tblName, $onlyCol = '')
494
    {
495
        $this->setTableForeginKeyCache($database, $this->fixTableSource($tblName));
496
        $array2return = null;
497
        if (isset($this->advCache['tableFKs'][$database][$tblName])) {
498
            foreach ($this->advCache['tableFKs'][$database][$tblName] as $value) {
499
                if ($value['COLUMN_NAME'] == $onlyCol) {
500
                    $query                  = $this->getForeignKeysQuery($value);
501
                    $targetTblTxtFlds       = $this->setMySQLquery2Server($query, 'full_array_key_numbered')['result'];
502
                    $array2return[$onlyCol] = [
503
                        $this->glueDbTb($value['REFERENCED_TABLE_SCHEMA'], $value['REFERENCED_TABLE_NAME']),
504
                        $value['REFERENCED_COLUMN_NAME'],
505
                        '`' . $targetTblTxtFlds[0]['COLUMN_NAME'] . '`',
506
                    ];
507
                }
508
            }
509
        }
510
        return $array2return;
511
    }
512
513
    /**
514
     * Build label html tag
515
     *
516
     * @param array $details
517
     * @return string
518
     */
519
    private function getLabel($details)
520
    {
521
        return '<span class="fake_label">' . $this->getFieldNameForDisplay($details) . '</span>';
522
    }
523
524
    /**
525
     * Returns an array with possible values of a SET or ENUM column
526
     *
527
     * @param string $refTbl
528
     * @param string $refCol
529
     * @return array
530
     */
531
    protected function getSetOrEnum2Array($refTbl, $refCol)
532
    {
533
        $dat = $this->establishDatabaseAndTable($refTbl);
534
        foreach ($this->advCache['tableStructureCache'][$dat[0]][$dat[1]] as $value) {
535
            if ($value['COLUMN_NAME'] == $refCol) {
536
                $clndVls = explode(',', str_replace([$value['DATA_TYPE'], '(', "'", ')'], '', $value['COLUMN_TYPE']));
537
                $enmVls  = array_combine($clndVls, $clndVls);
538
                if ($value['IS_NULLABLE'] === 'YES') {
539
                    $enmVls['NULL'] = '';
540
                }
541
            }
542
        }
543
        ksort($enmVls);
544
        return $enmVls;
545
    }
546
547
    /**
548
     * Returns a timestamp field value
549
     *
550
     * @param array $dtl
551
     * @return array
552
     */
553
    private function getTimestamping($dtl)
554
    {
555
        $inM = $this->setStringIntoTag($this->getFieldValue($dtl), 'span');
556
        if (in_array($this->getFieldValue($dtl), ['', 'CURRENT_TIMESTAMP', 'NULL'])) {
557
            $mCN = [
558
                'InsertDateTime'        => 'data/timpul ad. informatiei',
559
                'ModificationDateTime'  => 'data/timpul modificarii inf.',
560
                'modification_datetime' => 'data/timpul modificarii inf.',
561
            ];
562
            if (array_key_exists($dtl['COLUMN_NAME'], $mCN)) {
563
                $inM = $this->setStringIntoTag($mCN[$dtl['COLUMN_NAME']], 'span', ['style' => 'font-style:italic;']);
564
            }
565
        }
566
        return ['label' => $this->getLabel($dtl), 'input' => $inM];
567
    }
568
569
    /**
570
     * Glues Database and Table into 1 single string
571
     *
572
     * @param string $dbName
573
     * @param string $tbName
574
     * @return string
575
     */
576
    private function glueDbTb($dbName, $tbName)
577
    {
578
        return '`' . $dbName . '`.`' . $tbName . '`';
579
    }
580
581
    /**
582
     * Manages features flag
583
     *
584
     * @param string $fieldName
585
     * @param array $features
586
     * @return string
587
     */
588
    private function handleFeatures($fieldName, $features)
589
    {
590
        $rOly  = $this->handleFeaturesSingle($fieldName, $features, 'readonly');
591
        $rDbld = $this->handleFeaturesSingle($fieldName, $features, 'disabled');
592
        $rNl   = [];
593
        if (isset($features['include_null']) && in_array($fieldName, $features['include_null'])) {
594
            $rNl = ['include_null'];
595
        }
596
        return array_merge([], $rOly, $rDbld, $rNl);
597
    }
598
599
    /**
600
     * Handles the features
601
     *
602
     * @param string $fieldName
603
     * @param array $features
604
     * @param string $featureKey
605
     * @return array
606
     */
607
    private function handleFeaturesSingle($fieldName, $features, $featureKey)
608
    {
609
        $fMap    = [
610
            'readonly' => ['readonly', 'class', 'input_readonly'],
611
            'disabled' => ['disabled']
612
        ];
613
        $aReturn = [];
614
        if (array_key_exists($featureKey, $features)) {
615
            if (array_key_exists($fieldName, $features[$featureKey])) {
616
                $aReturn[$featureKey][$fMap[$featureKey][0]] = $fMap[$featureKey][0];
617
                if (count($fMap[$featureKey]) > 1) {
618
                    $aReturn[$featureKey][$fMap[$featureKey][1]] = $fMap[$featureKey][2];
619
                }
620
            }
621
        }
622
        return $aReturn;
623
    }
624
625
    /**
626
     * Builds field output w. special column name
627
     *
628
     * @param string $tableSource
629
     * @param array $dtl
630
     * @param array $features
631
     * @param string $fieldLabel
632
     * @return array
633
     */
634
    private function setField($tableSource, $dtl, $features, $fieldLabel)
635
    {
636
        if ($dtl['COLUMN_NAME'] == 'host') {
637
            $inVl = gethostbyaddr($this->tCmnRequest->server->get('REMOTE_ADDR'));
638
            return [
639
                'label' => '<label for="' . $dtl['COLUMN_NAME'] . '">Numele calculatorului</label>',
640
                'input' => '<input type="text" name="host" size="15" readonly value="' . $inVl . '" />',
641
            ];
642
        }
643
        $result = $this->setFieldInput($tableSource, $dtl, $features);
644
        return ['label' => $this->setFieldLabel($dtl, $features, $fieldLabel), 'input' => $result];
645
    }
646
647
    /**
648
     * Builds field output w. another special column name
649
     *
650
     * @param string $tableSource
651
     * @param array $dtl
652
     * @param array $features
653
     * @return string
654
     */
655
    private function setFieldInput($tableSource, $dtl, $features)
656
    {
657
        if ($dtl['COLUMN_NAME'] == 'ChoiceId') {
658
            return '<input type="text" name="ChoiceId" value="'
659
                    . $this->tCmnRequest->request->get($dtl['COLUMN_NAME']) . '" />';
660
        }
661
        return $this->setNeededFieldByType($tableSource, $dtl, $features);
662
    }
663
664
    /**
665
     * Prepares the label for inputs
666
     *
667
     * @param array $details
668
     * @param array $features
669
     * @param string $fieldLabel
670
     * @return string
671
     */
672
    private function setFieldLabel($details, $features, $fieldLabel)
673
    {
674
        $aLabel = ['for' => $details['COLUMN_NAME'], 'id' => $details['COLUMN_NAME'] . '_label'];
675
        if (isset($features['disabled'])) {
676
            if (in_array($details['COLUMN_NAME'], $features['disabled'])) {
677
                $aLabel = array_merge($aLabel, ['style' => 'color: grey;']);
678
            }
679
        }
680
        return $this->setStringIntoTag($fieldLabel, 'label', $aLabel);
681
    }
682
683
    /**
684
     * Form default buttons
685
     *
686
     * @param array $feat
687
     * @param array $hiddenInfo
688
     * @return string
689
     */
690
    private function setFormButtons($feat, $hiddenInfo = [])
691
    {
692
        $btn   = [];
693
        $btn[] = '<input type="submit" id="submit" style="margin-left:220px;" value="'
694
                . $this->lclMsgCmn('i18n_Form_ButtonSave') . '" />';
695
        if (isset($feat['insertAndUpdate'])) {
696
            $btn[] = '<input type="hidden" id="insertAndUpdate" name="insertAndUpdate" value="insertAndUpdate" />';
697
        }
698
        if ($hiddenInfo != []) {
699
            foreach ($hiddenInfo as $key => $value) {
700
                $btn[] = '<input type="hidden" id="' . $key . '" name="' . $key . '" value="' . $value . '" />';
701
            }
702
        }
703
        return '<div>' . implode('', $btn) . '</div>';
704
    }
705
706
    /**
707
     * Builds javascript to avoid multiple form submission
708
     *
709
     * @param string $frmId
710
     * @return string
711
     */
712
    private function setFormJavascriptFinal($frmId)
713
    {
714
        $cnt = implode(PHP_EOL, [
715
            '$(document).ready(function(){',
716
            '$("form#' . $frmId . '").submit(function(){',
717
            '$("form#' . $frmId . ' input[type=checkbox]").attr("readonly", true);',
718
            '$("form#' . $frmId . ' input[type=password]").attr("readonly", true);',
719
            '$("form#' . $frmId . ' input[type=radio]").attr("readonly", true);',
720
            '$("form#' . $frmId . ' input[type=text]").attr("readonly", true);',
721
            '$("form#' . $frmId . ' textarea").attr("readonly", true);',
722
            '$("form#' . $frmId . ' select").attr("readonly", true);',
723
            '$("input[type=submit]").attr("disabled", "disabled");',
724
            '$("input[type=submit]").attr("value", "' . $this->lclMsgCmn('i18n_Form_ButtonSaving') . '");',
725
            '});',
726
            '});',
727
        ]);
728
        return $this->setJavascriptContent(PHP_EOL . $cnt . PHP_EOL);
729
    }
730
731
    /**
732
     * Returns a generic form based on a given table
733
     *
734
     * @param string $tblSrc
735
     * @param array $feat
736
     * @param array $hdnInf
737
     *
738
     * @return string Form to add/modify detail for a single row within a table
739
     */
740
    protected function setFormGenericSingleRecord($tblSrc, $feat, $hdnInf = [])
741
    {
742
        echo $this->setStringIntoTag('', 'div', ['id' => 'loading']);
743
        $this->setTableCache($tblSrc);
744
        if (strpos($tblSrc, '.') !== false) {
745
            $tblSrc = explode('.', str_replace('`', '', $tblSrc))[1];
746
        }
747
        $sReturn = [];
748
        if (count($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc]) != 0) {
749
            foreach ($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc] as $value) {
750
                $sReturn[] = $this->setNeededField($tblSrc, $value, $feat);
751
            }
752
        }
753
        $frmFtrs = ['id' => $feat['id'], 'action' => $feat['action'], 'method' => $feat['method']];
754
        return $this->setStringIntoTag(implode('', $sReturn) . $this->setFormButtons($feat, $hdnInf), 'form', $frmFtrs)
755
                . $this->setFormJavascriptFinal($feat['id']);
756
    }
757
758
    /**
759
     * Analyse the field and returns the proper line 2 use in forms
760
     *
761
     * @param string $tableSource
762
     * @param array $details
763
     * @param array $features
764
     * @return string|array
765
     */
766
    private function setNeededField($tableSource, $details, $features)
767
    {
768
        if (isset($features['hidden'])) {
769
            if (in_array($details['COLUMN_NAME'], $features['hidden'])) {
770
                return null;
771
            }
772
        }
773
        $fieldLabel = $this->getFieldNameForDisplay($details);
774
        if ($fieldLabel == 'hidden') {
775
            return null;
776
        }
777
        return $this->setNeededFieldFinal($tableSource, $details, $features, $fieldLabel);
778
    }
779
780
    /**
781
     * Analyse the field type and returns the proper lines 2 use in forms
782
     *
783
     * @param string $tblName
784
     * @param array $dtls
785
     * @param array $features
786
     * @return string|array
787
     */
788
    private function setNeededFieldByType($tblName, $dtls, $features)
789
    {
790
        if (isset($features['special']) && isset($features['special'][$dtls['COLUMN_NAME']])) {
791
            $sOpt = $this->setMySQLquery2Server($features['special'][$dtls['COLUMN_NAME']], 'array_key_value');
792
            return $this->setArrayToSelect($sOpt, $this->getFieldValue($dtls), $dtls['COLUMN_NAME'], ['size' => 1]);
0 ignored issues
show
Bug introduced by
It seems like $sOpt defined by $this->setMySQLquery2Ser...']], 'array_key_value') on line 791 can also be of type string; however, danielgp\common_lib\DomC...lGP::setArrayToSelect() does only seem to accept array, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
793
        }
794
        return $this->setNeededFieldKnown($tblName, $dtls, $features);
795
    }
796
797
    private function setNeededFieldKnown($tblName, $dtls, $features)
798
    {
799
        $iar      = $this->handleFeatures($dtls['COLUMN_NAME'], $features);
800
        $sReturn  = '';
801
        $numTypes = ['bigint', 'int', 'mediumint', 'smallint', 'tinyint', 'float', 'double', 'decimal', 'numeric'];
802
        if (in_array($dtls['DATA_TYPE'], $numTypes)) {
803
            $sReturn = $this->getFieldOutputNumeric($tblName, $dtls, $iar);
804
        } elseif (in_array($dtls['DATA_TYPE'], ['char', 'tinytext', 'varchar', 'enum', 'set', 'text', 'blob'])) {
805
            $sReturn = $this->setNeededFieldTextRelated($tblName, $dtls, $iar);
806
        } elseif (in_array($dtls['DATA_TYPE'], ['date', 'datetime', 'time', 'timestamp', 'year'])) {
807
            $sReturn = $this->setNeededFieldSingleType($tblName, $dtls, $iar);
808
        }
809
        return $this->getFieldCompletionType($dtls) . $sReturn;
810
    }
811
812
    private function setNeededFieldFinal($tableSource, $details, $features, $fieldLabel)
813
    {
814
        $sReturn = $this->setField($tableSource, $details, $features, $fieldLabel);
815
        $lmts    = $this->setFieldNumbers($details);
816
        return '<div>' . $sReturn['label']
817
                . $this->setStringIntoTag($sReturn['input'], 'span', ['class' => 'labell'])
818
                . '<span style="font-size:x-small;font-style:italic;">&nbsp;(max. '
819
                . $lmts['M'] . (isset($lmts['d']) ? ' w. ' . $lmts['d'] . ' decimals' : '') . ')</span>'
820
                . '</div>';
821
    }
822
823
    private function setNeededFieldSingleType($tblName, $dtls, $iar)
824
    {
825
        if ($dtls['DATA_TYPE'] == 'date') {
826
            return $this->getFieldOutputDate($dtls);
827
        } elseif ($dtls['DATA_TYPE'] == 'time') {
828
            return $this->getFieldOutputTime($dtls, $iar);
829
        } elseif (in_array($dtls['DATA_TYPE'], ['datetime', 'timestamp'])) {
830
            return $this->getFieldOutputTimestamp($dtls, $iar);
831
        }
832
        return $this->getFieldOutputYear($tblName, $dtls, $iar);
833
    }
834
835
    private function setNeededFieldTextRelated($tblName, $dtls, $iar)
836
    {
837
        if (in_array($dtls['DATA_TYPE'], ['char', 'tinytext', 'varchar'])) {
838
            return $this->getFieldOutputText($tblName, $dtls['DATA_TYPE'], $dtls, $iar);
839
        } elseif (in_array($dtls['DATA_TYPE'], ['text', 'blob'])) {
840
            return $this->getFieldOutputTextLarge($dtls['DATA_TYPE'], $dtls, $iar);
841
        }
842
        return $this->getFieldOutputEnumSet($tblName, $dtls['DATA_TYPE'], $dtls, $iar);
843
    }
844
845
    /**
846
     * create a Cache for given table to use it in many places
847
     *
848
     * @param string $tblSrc
849
     */
850
    private function setTableCache($tblSrc)
851
    {
852
        $dat = $this->establishDatabaseAndTable($tblSrc);
853
        if (!isset($this->advCache['tableStructureCache'][$dat[0]][$dat[1]])) {
854
            $this->advCache['workingDatabase']                       = $dat[0];
855
            $this->advCache['tableStructureCache'][$dat[0]][$dat[1]] = $this->getMySQLlistColumns([
856
                'TABLE_SCHEMA' => $dat[0],
857
                'TABLE_NAME'   => $dat[1],
858
            ]);
859
            $this->setTableForeginKeyCache($dat[0], $dat[1]);
860
        }
861
    }
862
863
    private function setTableForeginKeyCache($dbName, $tblName)
864
    {
865
        $frgnKs = $this->getMySQLlistIndexes([
866
            'TABLE_SCHEMA'          => $dbName,
867
            'TABLE_NAME'            => $tblName,
868
            'REFERENCED_TABLE_NAME' => 'NOT NULL',
869
        ]);
870
        if (!is_null($frgnKs)) {
871
            $this->advCache['tableFKs'][$dbName][$tblName] = $frgnKs;
872
            $this->advCache['FKcol'][$dbName][$tblName]    = array_column($frgnKs, 'COLUMN_NAME', 'CONSTRAINT_NAME');
873
        }
874
    }
875
}
876