Passed
Push — main ( 364dc0...5d7b01 )
by Thierry
01:43
created

TableSelectTrait   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 278
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 88
c 1
b 0
f 0
dl 0
loc 278
rs 9.52
wmc 36

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getFiltersOptions() 0 13 3
A getSortingOptions() 0 14 2
A getColumnsOptions() 0 8 1
A setDefaultOptions() 0 23 4
A getLengthOptions() 0 4 2
A getFieldsOptions() 0 16 5
B getSelectEntity() 0 30 10
A getLimitOptions() 0 3 1
B setPrimaryKey() 0 24 8
1
<?php
2
3
namespace Lagdo\DbAdmin\DbAdmin\Traits;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableSelectEntity;
6
7
use function intval;
8
use function count;
9
use function html_entity_decode;
10
use function strip_tags;
11
use function array_flip;
12
use function in_array;
13
use function substr;
14
15
trait TableSelectTrait
16
{
17
    /**
18
     * Print columns box in select
19
     * @param array $select Result of processSelectColumns()[0]
20
     * @param array $columns Selectable columns
21
     * @param array $options
22
     * @return array
23
     */
24
    private function getColumnsOptions(array $select, array $columns, array $options): array
25
    {
26
        return [
27
            'select' => $select,
28
            'values' => (array)$options["columns"],
29
            'columns' => $columns,
30
            'functions' => $this->driver->functions(),
31
            'grouping' => $this->driver->grouping(),
32
        ];
33
    }
34
35
    /**
36
     * Print search box in select
37
     *
38
     * @param array $columns Selectable columns
39
     * @param array $indexes
40
     * @param array $options
41
     *
42
     * @return array
43
     */
44
    private function getFiltersOptions(array $columns, array $indexes, array $options): array
45
    {
46
        $fulltexts = [];
47
        foreach ($indexes as $i => $index) {
48
            $fulltexts[$i] = $index->type == "FULLTEXT" ? $this->util->html($options["fulltext"][$i]) : '';
49
        }
50
        return [
51
            // 'where' => $where,
52
            'values' => (array)$options["where"],
53
            'columns' => $columns,
54
            'indexes' => $indexes,
55
            'operators' => $this->driver->operators(),
56
            'fulltexts' => $fulltexts,
57
        ];
58
    }
59
60
    /**
61
     * Print order box in select
62
     *
63
     * @param array $columns Selectable columns
64
     * @param array $options
65
     *
66
     * @return array
67
     */
68
    private function getSortingOptions(array $columns, array $options): array
69
    {
70
        $values = [];
71
        $descs = (array)$options["desc"];
72
        foreach ((array)$options["order"] as $key => $value) {
73
            $values[] = [
74
                'col' => $value,
75
                'desc' => $descs[$key] ?? 0,
76
            ];
77
        }
78
        return [
79
            // 'order' => $order,
80
            'values' => $values,
81
            'columns' => $columns,
82
        ];
83
    }
84
85
    /**
86
     * Print limit box in select
87
     *
88
     * @param string $limit Result of processSelectLimit()
89
     *
90
     * @return array
91
     */
92
    private function getLimitOptions(string $limit): array
93
    {
94
        return ['value' => $this->util->html($limit)];
95
    }
96
97
    /**
98
     * Print text length box in select
99
     *
100
     * @param int $textLength Result of processSelectLength()
101
     *
102
     * @return array
103
     */
104
    private function getLengthOptions(int $textLength): array
105
    {
106
        return [
107
            'value' => $textLength === 0 ? 0 : $this->util->html($textLength),
108
        ];
109
    }
110
111
    /**
112
     * Print action box in select
113
     *
114
     * @param array $indexes
115
     *
116
     * @return array
117
     */
118
    // private function getActionOptions(array $indexes)
119
    // {
120
    //     $columns = [];
121
    //     foreach ($indexes as $index) {
122
    //         $current_key = \reset($index->columns);
123
    //         if ($index->type != "FULLTEXT" && $current_key) {
124
    //             $columns[$current_key] = 1;
125
    //         }
126
    //     }
127
    //     $columns[""] = 1;
128
    //     return ['columns' => $columns];
129
    // }
130
131
    /**
132
     * Print command box in select
133
     *
134
     * @return bool whether to print default commands
135
     */
136
    // private function getCommandOptions()
137
    // {
138
    //     return !$this->driver->isInformationSchema($this->driver->database());
139
    // }
140
141
    /**
142
     * Print import box in select
143
     *
144
     * @return bool whether to print default import
145
     */
146
    // private function getImportOptions()
147
    // {
148
    //     return !$this->driver->isInformationSchema($this->driver->database());
149
    // }
150
151
    /**
152
     * Print extra text in the end of a select form
153
     *
154
     * @param array $emailFields Fields holding e-mails
155
     * @param array $columns Selectable columns
156
     *
157
     * @return array
158
     */
159
    // private function getEmailOptions(array $emailFields, array $columns)
160
    // {
161
    // }
162
163
    /**
164
     * @param array $queryOptions
165
     *
166
     * @return int
167
     */
168
    private function setDefaultOptions(array &$queryOptions): int
169
    {
170
        $defaultOptions = [
171
            'columns' => [],
172
            'where' => [],
173
            'order' => [],
174
            'desc' => [],
175
            'fulltext' => [],
176
            'limit' => '50',
177
            'text_length' => '100',
178
            'page' => '1',
179
        ];
180
        foreach ($defaultOptions as $name => $value) {
181
            if (!isset($queryOptions[$name])) {
182
                $queryOptions[$name] = $value;
183
            }
184
        }
185
        $page = intval($queryOptions['page']);
186
        if ($page > 0) {
187
            $page -= 1; // Page numbers start at 0 here, instead of 1.
188
        }
189
        $queryOptions['page'] = $page;
190
        return $page;
191
    }
192
193
    /**
194
     * @param array $fields
195
     *
196
     * @return array
197
     */
198
    private function getFieldsOptions(array $fields): array
199
    {
200
        $rights = []; // privilege => 0
201
        $columns = []; // selectable columns
202
        $textLength = 0;
203
        foreach ($fields as $key => $field) {
204
            $name = $this->util->fieldName($field);
205
            if (isset($field->privileges["select"]) && $name != "") {
206
                $columns[$key] = html_entity_decode(strip_tags($name), ENT_QUOTES);
207
                if ($this->util->isShortable($field)) {
208
                    $textLength = $this->util->processSelectLength();
209
                }
210
            }
211
            $rights[] = $field->privileges;
212
        }
213
        return [$rights, $columns, $textLength];
214
    }
215
216
    /**
217
     * @param array $indexes
218
     * @param array $select
219
     * @param mixed $tableStatus
220
     *
221
     * @return array
222
     */
223
    private function setPrimaryKey(array &$indexes, array $select, $tableStatus): array
224
    {
225
        $primary = null;
226
        $unselected = [];
227
        foreach ($indexes as $index) {
228
            if ($index->type == "PRIMARY") {
229
                $primary = array_flip($index->columns);
230
                $unselected = ($select ? $primary : []);
231
                foreach ($unselected as $key => $val) {
232
                    if (in_array($this->driver->escapeId($key), $select)) {
233
                        unset($unselected[$key]);
234
                    }
235
                }
236
                break;
237
            }
238
        }
239
240
        $oid = $tableStatus->oid;
241
        if ($oid && !$primary) {
242
            /*$primary = */$unselected = [$oid => 0];
243
            $indexes[] = ["type" => "PRIMARY", "columns" => [$oid]];
244
        }
245
246
        return $unselected;
247
    }
248
249
    /**
250
     * @param string $table
251
     * @param array $columns
252
     * @param array $fields
253
     * @param array $select
254
     * @param array $group
255
     * @param array $where
256
     * @param array $order
257
     * @param array $unselected
258
     * @param int $limit
259
     * @param int $page
260
     *
261
     * @return TableSelectEntity
262
     */
263
    private function getSelectEntity(string $table, array $columns, array $fields, array $select,
264
                                     array $group, array $where, array $order, array $unselected, int $limit, int $page): TableSelectEntity
265
    {
266
        $select2 = $select;
267
        $group2 = $group;
268
        if (empty($select2)) {
269
            $select2[] = "*";
270
            $convert_fields = $this->driver->convertFields($columns, $fields, $select);
271
            if ($convert_fields) {
272
                $select2[] = substr($convert_fields, 2);
273
            }
274
        }
275
        foreach ($select as $key => $val) {
276
            $field = $fields[$this->driver->unescapeId($val)] ?? null;
277
            if ($field && ($as = $this->driver->convertField($field))) {
278
                $select2[$key] = "$as AS $val";
279
            }
280
        }
281
        $isGroup = count($group) < count($select);
282
        if (!$isGroup && !empty($unselected)) {
283
            foreach ($unselected as $key => $val) {
284
                $select2[] = $this->driver->escapeId($key);
285
                if (!empty($group2)) {
286
                    $group2[] = $this->driver->escapeId($key);
287
                }
288
            }
289
        }
290
291
        // From driver.inc.php
292
        return new TableSelectEntity($table, $select2, $where, $group2, $order, $limit, $page);
293
    }
294
}
295