Passed
Push — master ( 176828...fe6fe2 )
by Daniel
02:39
created

MySQLiAdvancedOutput   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 203
rs 9.68
c 0
b 0
f 0
wmc 34

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldOutputEnumSet() 0 13 3
A setNeededFieldSingleType() 0 10 4
A getSetOrEnum2Array() 0 14 4
A getFieldOutputYear() 0 11 3
A setNeededFieldTextRelated() 0 8 3
A getFieldOutputTextLarge() 0 15 3
A setNeededFieldKnown() 0 13 4
A getFieldOutputTimestamp() 0 10 4
A getFieldOutputText() 0 10 3
A getTimestamping() 0 16 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
 * useful functions to get quick results
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait MySQLiAdvancedOutput
37
{
38
39
    use MySQLiByDanielGPtables;
40
41
    /**
42
     * Returns a Enum or Set field to use in form
43
     *
44
     * @param string $tblSrc
45
     * @param string $fldType
46
     * @param array $val
47
     * @param array $iar
48
     * @return string
49
     */
50
    private function getFieldOutputEnumSet($tblSrc, $fldType, $val, $iar = [])
51
    {
52
        $adnlThings = $this->establishDefaultEnumSet($fldType);
53
        if (array_key_exists('readonly', $val)) {
54
            return $this->getFieldOutputEnumSetReadOnly($val, $adnlThings);
55
        }
56
        $inAdtnl = $adnlThings['additional'];
57
        if ($iar !== []) {
58
            $inAdtnl = array_merge($inAdtnl, $iar);
59
        }
60
        $vlSlct    = explode(',', $this->getFieldValue($val));
61
        $slctOptns = $this->getSetOrEnum2Array($tblSrc, $val['COLUMN_NAME']);
62
        return $this->setArrayToSelect($slctOptns, $vlSlct, $val['COLUMN_NAME'] . $adnlThings['suffix'], $inAdtnl);
63
    }
64
65
    /**
66
     * Returns a Char field 2 use in a form
67
     *
68
     * @param string $tbl
69
     * @param string $fieldType
70
     * @param array $value
71
     * @param array $iar
72
     * @return string
73
     */
74
    private function getFieldOutputText($tbl, $fieldType, $value, $iar = [])
75
    {
76
        if (!in_array($fieldType, ['char', 'tinytext', 'varchar'])) {
77
            return '';
78
        }
79
        $foreignKeysArray = $this->getFieldOutputTextPrerequisites($tbl, $value);
80
        if ($foreignKeysArray !== []) {
81
            return $this->getFieldOutputTextFK($foreignKeysArray, $value, $iar);
82
        }
83
        return $this->getFieldOutputTextNonFK($value, $iar);
84
    }
85
86
    /**
87
     * Returns a Text field 2 use in a form
88
     *
89
     * @param string $fieldType
90
     * @param array $value
91
     * @param array $iar
92
     * @return string
93
     */
94
    protected function getFieldOutputTextLarge($fieldType, $value, $iar = [])
95
    {
96
        if (!in_array($fieldType, ['blob', 'text'])) {
97
            return '';
98
        }
99
        $inAdtnl = [
100
            'name' => $value['COLUMN_NAME'],
101
            'id'   => $value['COLUMN_NAME'],
102
            'rows' => 4,
103
            'cols' => 55,
104
        ];
105
        if ($iar !== []) {
106
            $inAdtnl = array_merge($inAdtnl, $iar);
107
        }
108
        return $this->setStringIntoTag($this->getFieldValue($value), 'textarea', $inAdtnl);
109
    }
110
111
    /**
112
     * Returns a Timestamp field 2 use in a form
113
     *
114
     * @param array $dtl
115
     * @param array $iar
116
     * @return string
117
     */
118
    private function getFieldOutputTimestamp($dtl, $iar = [])
119
    {
120
        if (($dtl['COLUMN_DEFAULT'] == 'CURRENT_TIMESTAMP') || ($dtl['EXTRA'] == 'on update CURRENT_TIMESTAMP')) {
121
            return $this->getTimestamping($dtl)['input'];
122
        }
123
        $input = $this->getFieldOutputTT($dtl, 19, $iar);
124
        if (!array_key_exists('readonly', $iar)) {
125
            $input .= $this->setCalendarControlWithTime($dtl['COLUMN_NAME']);
126
        }
127
        return $input;
128
    }
129
130
    /**
131
     * Returns a Year field 2 use in a form
132
     *
133
     * @param array $details
134
     * @param array $iar
135
     * @return string
136
     */
137
    private function getFieldOutputYear($tblName, $details, $iar)
138
    {
139
        $listOfValues = [];
140
        for ($cntr = 1901; $cntr <= 2155; $cntr++) {
141
            $listOfValues[$cntr] = $cntr;
142
        }
143
        if ($iar == []) {
144
            $slDflt = $this->getFieldValue($details);
145
            return $this->setArrayToSelect($listOfValues, $slDflt, $details['COLUMN_NAME'], ['size' => 1]);
146
        }
147
        return $this->getFieldOutputText($tblName, 'varchar', $details, $iar);
148
    }
149
150
    /**
151
     * Returns an array with possible values of a SET or ENUM column
152
     *
153
     * @param string $refTbl
154
     * @param string $refCol
155
     * @return array
156
     */
157
    protected function getSetOrEnum2Array($refTbl, $refCol)
158
    {
159
        $dat = $this->establishDatabaseAndTable($refTbl);
160
        foreach ($this->advCache['tableStructureCache'][$dat[0]][$dat[1]] as $vle) {
161
            if ($vle['COLUMN_NAME'] == $refCol) {
162
                $kVl = explode('\',\'', substr($vle['COLUMN_TYPE'], strlen($vle['DATA_TYPE']) + 2, -2));
163
                $fVl = array_combine($kVl, $kVl);
164
                if ($vle['IS_NULLABLE'] === 'YES') {
165
                    $fVl['NULL'] = '';
166
                }
167
            }
168
        }
169
        ksort($fVl);
170
        return $fVl;
171
    }
172
173
    /**
174
     * Returns a timestamp field value
175
     *
176
     * @param array $dtl
177
     * @return array
178
     */
179
    private function getTimestamping($dtl)
180
    {
181
        $fieldValue = $this->getFieldValue($dtl);
182
        $inM        = $this->setStringIntoTag($fieldValue, 'span');
183
        if (in_array($fieldValue, ['', 'CURRENT_TIMESTAMP', 'NULL'])) {
184
            $mCN = [
185
                'InsertDateTime'        => 'data/timpul ad. informatiei',
186
                'ModificationDateTime'  => 'data/timpul modificarii inf.',
187
                'modification_datetime' => 'data/timpul modificarii inf.',
188
            ];
189
            if (array_key_exists($dtl['COLUMN_NAME'], $mCN)) {
190
                $inM = $this->setStringIntoTag($mCN[$dtl['COLUMN_NAME']], 'span', ['style' => 'font-style:italic;']);
191
            }
192
        }
193
        $lbl = '<span class="fake_label">' . $this->getFieldNameForDisplay($dtl) . '</span>';
194
        return ['label' => $lbl, 'input' => $inM];
195
    }
196
197
    protected function setNeededFieldKnown($tblName, $dtls, $features)
198
    {
199
        $iar      = $this->handleFeatures($dtls['COLUMN_NAME'], $features);
200
        $sReturn  = '';
201
        $numTypes = ['bigint', 'int', 'mediumint', 'smallint', 'tinyint', 'float', 'double', 'decimal', 'numeric'];
202
        if (in_array($dtls['DATA_TYPE'], $numTypes)) {
203
            $sReturn = $this->getFieldOutputNumeric($tblName, $dtls, $iar);
204
        } elseif (in_array($dtls['DATA_TYPE'], ['char', 'tinytext', 'varchar', 'enum', 'set', 'text', 'blob'])) {
205
            $sReturn = $this->setNeededFieldTextRelated($tblName, $dtls, $iar);
206
        } elseif (in_array($dtls['DATA_TYPE'], ['date', 'datetime', 'time', 'timestamp', 'year'])) {
207
            $sReturn = $this->setNeededFieldSingleType($tblName, $dtls, $iar);
208
        }
209
        return $this->getFieldCompletionType($dtls) . $sReturn;
210
    }
211
212
    private function setNeededFieldSingleType($tblName, $dtls, $iar)
213
    {
214
        if ($dtls['DATA_TYPE'] == 'date') {
215
            return $this->getFieldOutputDate($dtls);
216
        } elseif ($dtls['DATA_TYPE'] == 'time') {
217
            return $this->getFieldOutputTime($dtls, $iar);
218
        } elseif (in_array($dtls['DATA_TYPE'], ['datetime', 'timestamp'])) {
219
            return $this->getFieldOutputTimestamp($dtls, $iar);
220
        }
221
        return $this->getFieldOutputYear($tblName, $dtls, $iar);
222
    }
223
224
    /**
225
     *
226
     * @param string $tblName
227
     * @param array $dtls
228
     * @param array $iar
229
     * @return string
230
     */
231
    private function setNeededFieldTextRelated($tblName, $dtls, $iar)
232
    {
233
        if (in_array($dtls['DATA_TYPE'], ['char', 'tinytext', 'varchar'])) {
234
            return $this->getFieldOutputText($tblName, $dtls['DATA_TYPE'], $dtls, $iar);
235
        } elseif (in_array($dtls['DATA_TYPE'], ['text', 'blob'])) {
236
            return $this->getFieldOutputTextLarge($dtls['DATA_TYPE'], $dtls, $iar);
237
        }
238
        return $this->getFieldOutputEnumSet($tblName, $dtls['DATA_TYPE'], $dtls, $iar);
239
    }
240
241
}
242