Completed
Push — master ( 6fec5f...f7f475 )
by Daniel
02:25
created

MySQLiAdvancedOutput::getLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
     * Prepares the output of text fields defined w. FKs
274
     *
275
     * @param array $foreignKeysArray
276
     * @param array $value
277
     * @param array $iar
278
     * @return string
279
     */
280
    private function getFieldOutputTextFK($foreignKeysArray, $value, $iar)
281
    {
282
        $query   = $this->sQueryGenericSelectKeyValue([
283
            $foreignKeysArray[$value['COLUMN_NAME']][1],
284
            $foreignKeysArray[$value['COLUMN_NAME']][2],
285
            $foreignKeysArray[$value['COLUMN_NAME']][0]
286
        ]);
287
        echo '<hr/>' . $query . '<hr/>';
288
        $inAdtnl = ['size' => 1];
289
        if ($value['IS_NULLABLE'] == 'YES') {
290
            $inAdtnl = array_merge($inAdtnl, ['include_null']);
291
        }
292
        if ($iar !== []) {
293
            $inAdtnl = array_merge($inAdtnl, $iar);
294
        }
295
        $slct = [
296
            'Options' => $this->setMySQLquery2Server($query, 'array_key_value'),
297
            'Value'   => $this->getFieldValue($value),
298
        ];
299
        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...
300
    }
301
302
    /**
303
     * Returns a Text field 2 use in a form
304
     *
305
     * @param string $fieldType
306
     * @param array $value
307
     * @param array $iar
308
     * @return string
309
     */
310
    private function getFieldOutputTextLarge($fieldType, $value, $iar = [])
311
    {
312
        if (!in_array($fieldType, ['blob', 'text'])) {
313
            return '';
314
        }
315
        $inAdtnl = [
316
            'name' => $value['COLUMN_NAME'],
317
            'id'   => $value['COLUMN_NAME'],
318
            'rows' => 4,
319
            'cols' => 55,
320
        ];
321
        if ($iar !== []) {
322
            $inAdtnl = array_merge($inAdtnl, $iar);
323
        }
324
        return $this->setStringIntoTag($this->getFieldValue($value), 'textarea', $inAdtnl);
325
    }
326
327
    /**
328
     * Prepares the output of text fields w/o FKs
329
     *
330
     * @param array $value
331
     * @param array $iar
332
     * @return string
333
     */
334
    private function getFieldOutputTextNonFK($value, $iar)
335
    {
336
        $fldNos  = $this->setFieldNumbers($value);
337
        $inAdtnl = [
338
            'type'      => ($value['COLUMN_NAME'] == 'password' ? 'password' : 'text'),
339
            'name'      => $value['COLUMN_NAME'],
340
            'id'        => $value['COLUMN_NAME'],
341
            'size'      => min(30, $fldNos['M']),
342
            'maxlength' => min(255, $fldNos['M']),
343
            'value'     => $this->getFieldValue($value),
344
        ];
345
        if ($iar !== []) {
346
            $inAdtnl = array_merge($inAdtnl, $iar);
347
        }
348
        return $this->setStringIntoShortTag('input', $inAdtnl);
349
    }
350
351
    /**
352
     * Prepares the text output fields
353
     *
354
     * @param string $tbl
355
     * @param array $value
356
     * @return null|array
357
     */
358
    private function getFieldOutputTextPrerequisites($tbl, $value)
359
    {
360
        $foreignKeysArray = null;
361
        if (($tbl != 'user_rights') && ($value['COLUMN_NAME'] != 'eid')) {
362
            $database = $this->advCache['workingDatabase'];
363
            if (strpos($tbl, '`.`')) {
364
                $database = substr($tbl, 0, strpos($tbl, '`.`'));
365
            }
366
            $foreignKeysArray = $this->getForeignKeysToArray($database, $tbl, $value['COLUMN_NAME']);
367
        }
368
        return $foreignKeysArray;
369
    }
370
371
    /**
372
     * Builds output as text input type
373
     *
374
     * @param array $value
375
     * @param integer $szN
376
     * @param array $iar
377
     * @return string
378
     */
379
    private function getFieldOutputTT($value, $szN, $iar = [])
380
    {
381
        $inAdtnl = [
382
            'id'        => $value['COLUMN_NAME'],
383
            'maxlength' => $szN,
384
            'name'      => $value['COLUMN_NAME'],
385
            'size'      => $szN,
386
            'type'      => 'text',
387
            'value'     => $this->getFieldValue($value),
388
        ];
389
        if ($iar !== []) {
390
            $inAdtnl = array_merge($inAdtnl, $iar);
391
        }
392
        return $this->setStringIntoShortTag('input', $inAdtnl);
393
    }
394
395
    /**
396
     * Returns a Time field 2 use in a form
397
     *
398
     * @param array $value
399
     * @param array $iar
400
     * @return string
401
     */
402
    private function getFieldOutputTime($value, $iar = [])
403
    {
404
        return $this->getFieldOutputTT($value, 8, $iar);
405
    }
406
407
    /**
408
     * Returns a Timestamp field 2 use in a form
409
     *
410
     * @param array $dtl
411
     * @param array $iar
412
     * @return string
413
     */
414
    private function getFieldOutputTimestamp($dtl, $iar = [])
415
    {
416
        if (($dtl['COLUMN_DEFAULT'] == 'CURRENT_TIMESTAMP') || ($dtl['EXTRA'] == 'on update CURRENT_TIMESTAMP')) {
417
            return $this->getTimestamping($dtl)['input'];
418
        }
419
        $input = $this->getFieldOutputTT($dtl, 19, $iar);
420
        if (!array_key_exists('readonly', $iar)) {
421
            $input .= $this->setCalendarControlWithTime($dtl['COLUMN_NAME']);
422
        }
423
        return $input;
424
    }
425
426
    /**
427
     * Returns a Year field 2 use in a form
428
     *
429
     * @param array $details
430
     * @param array $iar
431
     * @return string
432
     */
433
    private function getFieldOutputYear($tblName, $details, $iar)
434
    {
435
        $listOfValues = [];
436
        for ($cntr = 1901; $cntr <= 2155; $cntr++) {
437
            $listOfValues[$cntr] = $cntr;
438
        }
439
        if ($iar == []) {
440
            $slDflt = $this->getFieldValue($details);
441
            return $this->setArrayToSelect($listOfValues, $slDflt, $details['COLUMN_NAME'], ['size' => 1]);
442
        }
443
        return $this->getFieldOutputText($tblName, 'varchar', $details, $iar);
444
    }
445
446
    /**
447
     * Returns an array with fields referenced by a Foreign key
448
     *
449
     * @param string $database
450
     * @param string $tblName
451
     * @param string|array $onlyCol
452
     * @return array
453
     */
454
    private function getForeignKeysToArray($database, $tblName, $onlyCol = '')
455
    {
456
        $this->setTableForeginKeyCache($database, $this->fixTableSource($tblName));
457
        $array2return = null;
458
        if (isset($this->advCache['tableFKs'][$database][$tblName])) {
459
            foreach ($this->advCache['tableFKs'][$database][$tblName] as $value) {
460
                if ($value['COLUMN_NAME'] == $onlyCol) {
461
                    $query                  = $this->getForeignKeysQuery($value);
462
                    $targetTblTxtFlds       = $this->setMySQLquery2Server($query, 'full_array_key_numbered')['result'];
463
                    $array2return[$onlyCol] = [
464
                        $this->glueDbTb($value['REFERENCED_TABLE_SCHEMA'], $value['REFERENCED_TABLE_NAME']),
465
                        $value['REFERENCED_COLUMN_NAME'],
466
                        '`' . $targetTblTxtFlds[0]['COLUMN_NAME'] . '`',
467
                    ];
468
                }
469
            }
470
        }
471
        return $array2return;
472
    }
473
474
    /**
475
     * Build label html tag
476
     *
477
     * @param array $details
478
     * @return string
479
     */
480
    private function getLabel($details)
481
    {
482
        return '<span class="fake_label">' . $this->getFieldNameForDisplay($details) . '</span>';
483
    }
484
485
    /**
486
     * Returns an array with possible values of a SET or ENUM column
487
     *
488
     * @param string $refTbl
489
     * @param string $refCol
490
     * @return array
491
     */
492
    protected function getSetOrEnum2Array($refTbl, $refCol)
493
    {
494
        $dat = $this->establishDatabaseAndTable($refTbl);
495
        foreach ($this->advCache['tableStructureCache'][$dat[0]][$dat[1]] as $value) {
496
            if ($value['COLUMN_NAME'] == $refCol) {
497
                $clndVls = explode(',', str_replace([$value['DATA_TYPE'], '(', "'", ')'], '', $value['COLUMN_TYPE']));
498
                $enmVls  = array_combine($clndVls, $clndVls);
499
                if ($value['IS_NULLABLE'] === 'YES') {
500
                    $enmVls['NULL'] = '';
501
                }
502
            }
503
        }
504
        ksort($enmVls);
505
        return $enmVls;
506
    }
507
508
    /**
509
     * Returns a timestamp field value
510
     *
511
     * @param array $dtl
512
     * @return array
513
     */
514
    private function getTimestamping($dtl)
515
    {
516
        $inM = $this->setStringIntoTag($this->getFieldValue($dtl), 'span');
517
        if (in_array($this->getFieldValue($dtl), ['', 'CURRENT_TIMESTAMP', 'NULL'])) {
518
            $mCN = [
519
                'InsertDateTime'        => 'data/timpul ad. informatiei',
520
                'ModificationDateTime'  => 'data/timpul modificarii inf.',
521
                'modification_datetime' => 'data/timpul modificarii inf.',
522
            ];
523
            if (array_key_exists($dtl['COLUMN_NAME'], $mCN)) {
524
                $inM = $this->setStringIntoTag($mCN[$dtl['COLUMN_NAME']], 'span', ['style' => 'font-style:italic;']);
525
            }
526
        }
527
        return ['label' => $this->getLabel($dtl), 'input' => $inM];
528
    }
529
530
    /**
531
     * Glues Database and Table into 1 single string
532
     *
533
     * @param string $dbName
534
     * @param string $tbName
535
     * @return string
536
     */
537
    private function glueDbTb($dbName, $tbName)
538
    {
539
        return '`' . $dbName . '`.`' . $tbName . '`';
540
    }
541
542
    /**
543
     * Builds field output w. special column name
544
     *
545
     * @param string $tableSource
546
     * @param array $dtl
547
     * @param array $features
548
     * @param string $fieldLabel
549
     * @return array
550
     */
551
    private function setField($tableSource, $dtl, $features, $fieldLabel)
552
    {
553
        if ($dtl['COLUMN_NAME'] == 'host') {
554
            $inVl = gethostbyaddr($this->tCmnRequest->server->get('REMOTE_ADDR'));
555
            return [
556
                'label' => '<label for="' . $dtl['COLUMN_NAME'] . '">Numele calculatorului</label>',
557
                'input' => '<input type="text" name="host" size="15" readonly value="' . $inVl . '" />',
558
            ];
559
        }
560
        $result = $this->setFieldInput($tableSource, $dtl, $features);
561
        return ['label' => $this->setFieldLabel($dtl, $features, $fieldLabel), 'input' => $result];
562
    }
563
564
    /**
565
     * Builds field output w. another special column name
566
     *
567
     * @param string $tableSource
568
     * @param array $dtl
569
     * @param array $features
570
     * @return string
571
     */
572
    private function setFieldInput($tableSource, $dtl, $features)
573
    {
574
        if ($dtl['COLUMN_NAME'] == 'ChoiceId') {
575
            return '<input type="text" name="ChoiceId" value="'
576
                    . $this->tCmnRequest->request->get($dtl['COLUMN_NAME']) . '" />';
577
        }
578
        return $this->setNeededFieldByType($tableSource, $dtl, $features);
579
    }
580
581
    /**
582
     * Returns a generic form based on a given table
583
     *
584
     * @param string $tblSrc
585
     * @param array $feat
586
     * @param array $hdnInf
587
     *
588
     * @return string Form to add/modify detail for a single row within a table
589
     */
590
    protected function setFormGenericSingleRecord($tblSrc, $feat, $hdnInf = [])
591
    {
592
        echo $this->setStringIntoTag('', 'div', ['id' => 'loading']);
593
        $this->setTableCache($tblSrc);
594
        if (strpos($tblSrc, '.') !== false) {
595
            $tblSrc = explode('.', str_replace('`', '', $tblSrc))[1];
596
        }
597
        $sReturn = [];
598
        if (count($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc]) != 0) {
599
            foreach ($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc] as $value) {
600
                $sReturn[] = $this->setNeededField($tblSrc, $value, $feat);
601
            }
602
        }
603
        $frmFtrs = ['id' => $feat['id'], 'action' => $feat['action'], 'method' => $feat['method']];
604
        return $this->setStringIntoTag(implode('', $sReturn) . $this->setFormButtons($feat, $hdnInf), 'form', $frmFtrs)
605
                . $this->setFormJavascriptFinal($feat['id']);
606
    }
607
608
    /**
609
     * Analyse the field and returns the proper line 2 use in forms
610
     *
611
     * @param string $tableSource
612
     * @param array $details
613
     * @param array $features
614
     * @return string|array
615
     */
616
    private function setNeededField($tableSource, $details, $features)
617
    {
618
        if (isset($features['hidden'])) {
619
            if (in_array($details['COLUMN_NAME'], $features['hidden'])) {
620
                return null;
621
            }
622
        }
623
        $fieldLabel = $this->getFieldNameForDisplay($details);
624
        if ($fieldLabel == 'hidden') {
625
            return null;
626
        }
627
        return $this->setNeededFieldFinal($tableSource, $details, $features, $fieldLabel);
628
    }
629
630
    /**
631
     * Analyse the field type and returns the proper lines 2 use in forms
632
     *
633
     * @param string $tblName
634
     * @param array $dtls
635
     * @param array $features
636
     * @return string|array
637
     */
638
    private function setNeededFieldByType($tblName, $dtls, $features)
639
    {
640
        if (isset($features['special']) && isset($features['special'][$dtls['COLUMN_NAME']])) {
641
            $sOpt = $this->setMySQLquery2Server($features['special'][$dtls['COLUMN_NAME']], 'array_key_value');
642
            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 641 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...
643
        }
644
        return $this->setNeededFieldKnown($tblName, $dtls, $features);
645
    }
646
647
    private function setNeededFieldKnown($tblName, $dtls, $features)
648
    {
649
        $iar      = $this->handleFeatures($dtls['COLUMN_NAME'], $features);
650
        $sReturn  = '';
651
        $numTypes = ['bigint', 'int', 'mediumint', 'smallint', 'tinyint', 'float', 'double', 'decimal', 'numeric'];
652
        if (in_array($dtls['DATA_TYPE'], $numTypes)) {
653
            $sReturn = $this->getFieldOutputNumeric($tblName, $dtls, $iar);
654
        } elseif (in_array($dtls['DATA_TYPE'], ['char', 'tinytext', 'varchar', 'enum', 'set', 'text', 'blob'])) {
655
            $sReturn = $this->setNeededFieldTextRelated($tblName, $dtls, $iar);
656
        } elseif (in_array($dtls['DATA_TYPE'], ['date', 'datetime', 'time', 'timestamp', 'year'])) {
657
            $sReturn = $this->setNeededFieldSingleType($tblName, $dtls, $iar);
658
        }
659
        return $this->getFieldCompletionType($dtls) . $sReturn;
660
    }
661
662
    private function setNeededFieldFinal($tableSource, $details, $features, $fieldLabel)
663
    {
664
        $sReturn = $this->setField($tableSource, $details, $features, $fieldLabel);
665
        $lmts    = $this->setFieldNumbers($details);
666
        return '<div>' . $sReturn['label']
667
                . $this->setStringIntoTag($sReturn['input'], 'span', ['class' => 'labell'])
668
                . '<span style="font-size:x-small;font-style:italic;">&nbsp;(max. '
669
                . $lmts['M'] . (isset($lmts['d']) ? ' w. ' . $lmts['d'] . ' decimals' : '') . ')</span>'
670
                . '</div>';
671
    }
672
673
    private function setNeededFieldSingleType($tblName, $dtls, $iar)
674
    {
675
        if ($dtls['DATA_TYPE'] == 'date') {
676
            return $this->getFieldOutputDate($dtls);
677
        } elseif ($dtls['DATA_TYPE'] == 'time') {
678
            return $this->getFieldOutputTime($dtls, $iar);
679
        } elseif (in_array($dtls['DATA_TYPE'], ['datetime', 'timestamp'])) {
680
            return $this->getFieldOutputTimestamp($dtls, $iar);
681
        }
682
        return $this->getFieldOutputYear($tblName, $dtls, $iar);
683
    }
684
685
    private function setNeededFieldTextRelated($tblName, $dtls, $iar)
686
    {
687
        if (in_array($dtls['DATA_TYPE'], ['char', 'tinytext', 'varchar'])) {
688
            return $this->getFieldOutputText($tblName, $dtls['DATA_TYPE'], $dtls, $iar);
689
        } elseif (in_array($dtls['DATA_TYPE'], ['text', 'blob'])) {
690
            return $this->getFieldOutputTextLarge($dtls['DATA_TYPE'], $dtls, $iar);
691
        }
692
        return $this->getFieldOutputEnumSet($tblName, $dtls['DATA_TYPE'], $dtls, $iar);
693
    }
694
695
    /**
696
     * create a Cache for given table to use it in many places
697
     *
698
     * @param string $tblSrc
699
     */
700
    private function setTableCache($tblSrc)
701
    {
702
        $dat = $this->establishDatabaseAndTable($tblSrc);
703
        if (!isset($this->advCache['tableStructureCache'][$dat[0]][$dat[1]])) {
704
            $this->advCache['workingDatabase']                       = $dat[0];
705
            $this->advCache['tableStructureCache'][$dat[0]][$dat[1]] = $this->getMySQLlistColumns([
706
                'TABLE_SCHEMA' => $dat[0],
707
                'TABLE_NAME'   => $dat[1],
708
            ]);
709
            $this->setTableForeginKeyCache($dat[0], $dat[1]);
710
        }
711
    }
712
713
    private function setTableForeginKeyCache($dbName, $tblName)
714
    {
715
        $frgnKs = $this->getMySQLlistIndexes([
716
            'TABLE_SCHEMA'          => $dbName,
717
            'TABLE_NAME'            => $tblName,
718
            'REFERENCED_TABLE_NAME' => 'NOT NULL',
719
        ]);
720
        if (!is_null($frgnKs)) {
721
            $this->advCache['tableFKs'][$dbName][$tblName] = $frgnKs;
722
            $this->advCache['FKcol'][$dbName][$tblName]    = array_column($frgnKs, 'COLUMN_NAME', 'CONSTRAINT_NAME');
723
        }
724
    }
725
}
726