SelectOptions::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Db\Page\Dql;
4
5
use Lagdo\DbAdmin\Driver\Utils\Utils;
6
use Lagdo\DbAdmin\Driver\DriverInterface;
7
8
use function intval;
9
10
class SelectOptions
11
{
12
    /**
13
     * The constructor
14
     *
15
     * @param DriverInterface $driver
16
     * @param Utils $utils
17
     */
18
    public function __construct(private DriverInterface $driver, private Utils $utils)
19
    {}
20
21
    /**
22
     * @param SelectEntity $selectEntity
23
     *
24
     * @return void
25
     */
26
    public function setDefaultOptions(SelectEntity $selectEntity): void
27
    {
28
        $defaultOptions = [
29
            'columns' => [],
30
            'where' => [],
31
            'order' => [],
32
            'desc' => [],
33
            'fulltext' => [],
34
            'limit' => '50',
35
            'text_length' => '100',
36
            'page' => '1',
37
        ];
38
        foreach ($defaultOptions as $name => $value) {
39
            if (!isset($this->utils->input->values[$name])) {
40
                $this->utils->input->values[$name] = $value;
41
            }
42
            if (!isset($selectEntity->queryOptions[$name])) {
43
                $selectEntity->queryOptions[$name] = $value;
44
            }
45
        }
46
        $page = intval($selectEntity->queryOptions['page']);
47
        if ($page > 0) {
48
            $page -= 1; // Page numbers start at 0 here, instead of 1.
49
        }
50
        $selectEntity->queryOptions['page'] = $page;
51
        $selectEntity->page = $page;
52
    }
53
54
    /**
55
     * Print columns box in select
56
     *
57
     * @param array $select Result of processSelectColumns()[0]
58
     * @param array $columns Selectable columns
59
     * @param array $options
60
     * @return array
61
     */
62
    private function getColumnsOptions(array $select, array $columns, array $options): array
63
    {
64
        return [
65
            'select' => $select,
66
            'values' => (array)$options["columns"],
67
            'columns' => $columns,
68
            'functions' => $this->driver->functions(),
69
            'grouping' => $this->driver->grouping(),
70
        ];
71
    }
72
73
    /**
74
     * Print search box in select
75
     *
76
     * @param array $columns Selectable columns
77
     * @param array $indexes
78
     * @param array $options
79
     *
80
     * @return array
81
     */
82
    private function getFiltersOptions(array $columns, array $indexes, array $options): array
83
    {
84
        $fulltexts = [];
85
        foreach ($indexes as $i => $index) {
86
            $fulltexts[$i] = $index->type == "FULLTEXT" ?
87
                $this->utils->str->html($options["fulltext"][$i] ?? '') : '';
88
        }
89
        return [
90
            // 'where' => $where,
91
            'values' => (array)$options["where"],
92
            'columns' => $columns,
93
            'indexes' => $indexes,
94
            'operators' => $this->driver->operators(),
95
            'fulltexts' => $fulltexts,
96
        ];
97
    }
98
99
    /**
100
     * Print order box in select
101
     *
102
     * @param array $columns Selectable columns
103
     * @param array $options
104
     *
105
     * @return array
106
     */
107
    private function getSortingOptions(array $columns, array $options): array
108
    {
109
        $values = [];
110
        $descs = (array)$options["desc"];
111
        foreach ((array)$options["order"] as $key => $value) {
112
            $values[] = [
113
                'col' => $value,
114
                'desc' => $descs[$key] ?? 0,
115
            ];
116
        }
117
        return [
118
            // 'order' => $order,
119
            'values' => $values,
120
            'columns' => $columns,
121
        ];
122
    }
123
124
    /**
125
     * Print limit box in select
126
     *
127
     * @param string $limit Result of processSelectLimit()
128
     *
129
     * @return array
130
     */
131
    private function getLimitOptions(string $limit): array
132
    {
133
        return ['value' => $this->utils->str->html($limit)];
134
    }
135
136
    /**
137
     * Print text length box in select
138
     *
139
     * @param int $textLength Result of processSelectLength()
140
     *
141
     * @return array
142
     */
143
    private function getLengthOptions(int $textLength): array
144
    {
145
        return ['value' => $textLength === 0 ? 0 : $this->utils->str->html($textLength)];
146
    }
147
148
    /**
149
     * Print action box in select
150
     *
151
     * @param array $indexes
152
     *
153
     * @return array
154
     */
155
    // private function getActionOptions(array $indexes)
156
    // {
157
    //     $columns = [];
158
    //     foreach ($indexes as $index) {
159
    //         $current_key = \reset($index->columns);
160
    //         if ($index->type != "FULLTEXT" && $current_key) {
161
    //             $columns[$current_key] = 1;
162
    //         }
163
    //     }
164
    //     $columns[""] = 1;
165
    //     return ['columns' => $columns];
166
    // }
167
168
    /**
169
     * Print command box in select
170
     *
171
     * @return bool whether to print default commands
172
     */
173
    // private function getCommandOptions()
174
    // {
175
    //     return !$this->driver->isInformationSchema($this->driver->database());
176
    // }
177
178
    /**
179
     * Print import box in select
180
     *
181
     * @return bool whether to print default import
182
     */
183
    // private function getImportOptions()
184
    // {
185
    //     return !$this->driver->isInformationSchema($this->driver->database());
186
    // }
187
188
    /**
189
     * Print extra text in the end of a select form
190
     *
191
     * @param array $emailFields Fields holding e-mails
192
     * @param array $columns Selectable columns
193
     *
194
     * @return array
195
     */
196
    // private function getEmailOptions(array $emailFields, array $columns)
197
    // {
198
    // }
199
200
    /**
201
     * @param SelectEntity $selectEntity
202
     *
203
     * @return void
204
     */
205
    public function setSelectOptions(SelectEntity $selectEntity): void
206
    {
207
        $selectEntity->options = [
208
            'columns' => $this->getColumnsOptions($selectEntity->select,
209
                $selectEntity->columns, $selectEntity->queryOptions),
210
            'filters' => $this->getFiltersOptions($selectEntity->columns,
211
                $selectEntity->indexes, $selectEntity->queryOptions),
212
            'sorting' => $this->getSortingOptions($selectEntity->columns,
213
                $selectEntity->queryOptions),
214
            'limit' => $this->getLimitOptions($selectEntity->limit),
215
            'length' => $this->getLengthOptions($selectEntity->textLength),
216
            // 'action' => $this->getActionOptions($selectEntity->indexes),
217
        ];
218
    }
219
}
220