Total Complexity | 111 |
Total Lines | 424 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Complex classes like TableQueryAdmin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TableQueryAdmin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class TableQueryAdmin extends AbstractAdmin |
||
29 | { |
||
30 | private function getEntryFunction(TableFieldEntity $field, $name, $function, $functions): array |
||
31 | { |
||
32 | // Input for functions |
||
33 | if ($field->type == "enum") { |
||
34 | return [ |
||
35 | 'type' => 'name', |
||
36 | 'name' => $this->util->html($functions[""] ?? ''), |
||
37 | ]; |
||
38 | } |
||
39 | if (count($functions) > 1) { |
||
40 | $hasFunction = (in_array($function, $functions) || isset($functions[$function])); |
||
41 | return [ |
||
42 | 'type' => 'select', |
||
43 | 'name' => "function[$name]", |
||
44 | 'options' => $functions, |
||
45 | 'selected' => $function === null || $hasFunction ? $function : "", |
||
46 | ]; |
||
47 | } |
||
48 | return [ |
||
49 | 'type' => 'name', |
||
50 | 'name' => $this->util->html(reset($functions)), |
||
51 | ]; |
||
52 | } |
||
53 | |||
54 | private function getEntryInput(TableFieldEntity $field, $name, $value, $function, $functions) |
||
55 | { |
||
56 | $attrs = ['name' => "fields[$name]"]; |
||
57 | if ($field->type == "enum") { |
||
58 | return ['type' => 'radio', 'attrs' => $attrs, 'values' => [isset($options["select"]), $field, $attrs, $value]]; |
||
|
|||
59 | } |
||
60 | if (preg_match('~bool~', $field->type)) { |
||
61 | return ['type' => 'checkbox', 'attrs' => $attrs, 'values' => [preg_match('~^(1|t|true|y|yes|on)$~i', $value)]]; |
||
62 | } |
||
63 | if ($field->type == "set") { |
||
64 | $values = []; |
||
65 | preg_match_all("~'((?:[^']|'')*)'~", $field->length, $matches); |
||
66 | foreach ($matches[1] as $i => $val) { |
||
67 | $val = stripcslashes(str_replace("''", "'", $val)); |
||
68 | $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true)); |
||
69 | $values[] = [$this->util->html($val), $checked]; |
||
70 | } |
||
71 | return ['type' => 'checkbox', 'attrs' => $attrs, 'values' => $values]; |
||
72 | } |
||
73 | if (preg_match('~blob|bytea|raw|file~', $field->type) && $this->util->iniBool("file_uploads")) { |
||
74 | return ['type' => 'upload', 'attrs' => $attrs, 'value' => $name]; |
||
75 | } |
||
76 | if (($text = preg_match('~text|lob|memo~i', $field->type)) || preg_match("~\n~", $value)) { |
||
77 | if ($text && $this->driver->jush() != "sqlite") { |
||
78 | $attrs['cols'] = 50; |
||
79 | $attrs['rows'] = 12; |
||
80 | } else { |
||
81 | $rows = min(12, substr_count($value, "\n") + 1); |
||
82 | $attrs['cols'] = 30; |
||
83 | $attrs['rows'] = $rows; |
||
84 | if ($rows == 1) { |
||
85 | $attrs['style'] = 'height: 1.2em;'; |
||
86 | } |
||
87 | } |
||
88 | return ['type' => 'blob', 'attrs' => $attrs, 'value' => $this->util->html($value)]; |
||
89 | } |
||
90 | if ($function == "json" || preg_match('~^jsonb?$~', $field->type)) { |
||
91 | $attrs['cols'] = 50; |
||
92 | $attrs['rows'] = 12; |
||
93 | return ['type' => 'json', 'attrs' => $attrs, 'value' => $this->util->html($value)]; |
||
94 | } |
||
95 | $unsigned = $field->unsigned ?? false; |
||
96 | // int(3) is only a display hint |
||
97 | $maxlength = (!preg_match('~int~', $field->type) && |
||
98 | preg_match('~^(\d+)(,(\d+))?$~', $field->length, $match) ? |
||
99 | ((preg_match("~binary~", $field->type) ? 2 : 1) * $match[1] + (($match[3] ?? null) ? 1 : 0) + |
||
100 | (($match[2] ?? false) && !$unsigned ? 1 : 0)) : |
||
101 | ($this->driver->typeExists($field->type) ? $this->driver->type($field->type) + ($unsigned ? 0 : 1) : 0)); |
||
102 | if ($this->driver->jush() == 'sql' && $this->driver->minVersion(5.6) && preg_match('~time~', $field->type)) { |
||
103 | $maxlength += 7; // microtime |
||
104 | } |
||
105 | if ($maxlength > 0) { |
||
106 | $attrs['data-maxlength'] = $maxlength; |
||
107 | } |
||
108 | // type='date' and type='time' display localized value which may be confusing, |
||
109 | // type='datetime' uses 'T' as date and time separator |
||
110 | $hasFunction = (in_array($function, $functions) || isset($functions[$function])); |
||
111 | if ((!$hasFunction || $function === "") && |
||
112 | preg_match('~(?<!o)int(?!er)~', $field->type) && |
||
113 | !preg_match('~\[\]~', $field->fullType)) { |
||
114 | $attrs['type'] = 'number'; |
||
115 | } |
||
116 | if (preg_match('~char|binary~', $field->type) && $maxlength > 20) { |
||
117 | $attrs['size'] = 40; |
||
118 | } |
||
119 | return ['type' => 'input', 'attrs' => $attrs, 'value' => $this->util->html($value)]; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Get data for an input field |
||
124 | */ |
||
125 | protected function getFieldInput($field, $value, $function, $options) |
||
126 | { |
||
127 | // From functions.inc.php (function input($field, $value, $function)) |
||
128 | $name = $this->util->html($this->util->bracketEscape($field->name)); |
||
129 | $save = $options["save"]; |
||
130 | $reset = ($this->driver->jush() == "mssql" && $field->autoIncrement); |
||
131 | if (is_array($value) && !$function) { |
||
132 | $value = json_encode($value, JSON_PRETTY_PRINT); |
||
133 | $function = "json"; |
||
134 | } |
||
135 | if ($reset && !$save) { |
||
136 | $function = null; |
||
137 | } |
||
138 | $functions = []; |
||
139 | if ($reset) { |
||
140 | $functions["orig"] = $this->trans->lang('original'); |
||
141 | } |
||
142 | $functions += $this->util->editFunctions($field); |
||
143 | return [ |
||
144 | 'type' => $this->util->html($field->fullType), |
||
145 | 'name' => $name, |
||
146 | 'field' => [ |
||
147 | 'type' => $field->type, |
||
148 | ], |
||
149 | 'function' => $this->getEntryFunction($field, $name, $function, $functions), |
||
150 | 'input' => $this->getEntryInput($field, $name, $value, $function, $functions), |
||
151 | ]; |
||
152 | |||
153 | // Input for value |
||
154 | // The HTML code generated by Adminer is kept here. |
||
155 | /*$attrs = " name='fields[$name]'"; |
||
156 | $entry['input'] = ['type' => '']; |
||
157 | if ($field->type == "enum") { |
||
158 | $entry['input']['type'] = 'radio'; |
||
159 | $entry['input']['value'] = $this->util->editInput(isset($options["select"]), $field, $attrs, $value); |
||
160 | } elseif (preg_match('~bool~', $field->type)) { |
||
161 | $entry['input']['type'] = 'checkbox'; |
||
162 | $entry['input']['value'] = ["<input type='hidden'$attrs value='0'>" . "<input type='checkbox'" . |
||
163 | (preg_match('~^(1|t|true|y|yes|on)$~i', $value) ? " checked='checked'" : "") . "$attrs value='1'>"]; |
||
164 | } elseif ($field->type == "set") { |
||
165 | $entry['input']['type'] = 'checkbox'; |
||
166 | $entry['input']['value'] = []; |
||
167 | preg_match_all("~'((?:[^']|'')*)'~", $field->length, $matches); |
||
168 | foreach ($matches[1] as $i => $val) { |
||
169 | $val = \stripcslashes(\str_replace("''", "'", $val)); |
||
170 | $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true)); |
||
171 | $entry['input']['value'][] = "<label><input type='checkbox' name='fields[$name][$i]' value='" . (1 << $i) . "'" . |
||
172 | ($checked ? ' checked' : '') . ">" . $this->util->html($val) . '</label>'; |
||
173 | } |
||
174 | } elseif (preg_match('~blob|bytea|raw|file~', $field->type) && $this->util->iniBool("file_uploads")) { |
||
175 | $entry['input']['value'] = "<input type='file' name='fields-$name'>"; |
||
176 | } elseif (($text = preg_match('~text|lob|memo~i', $field->type)) || preg_match("~\n~", $value)) { |
||
177 | if ($text && $this->driver->jush() != "sqlite") { |
||
178 | $attrs .= " cols='50' rows='12'"; |
||
179 | } else { |
||
180 | $rows = min(12, substr_count($value, "\n") + 1); |
||
181 | $attrs .= " cols='30' rows='$rows'" . ($rows == 1 ? " style='height: 1.2em;'" : ""); // 1.2em - line-height |
||
182 | } |
||
183 | $entry['input']['value'] = "<textarea$attrs>" . $this->util->html($value) . '</textarea>'; |
||
184 | } elseif ($function == "json" || preg_match('~^jsonb?$~', $field->type)) { |
||
185 | $entry['input']['value'] = "<textarea$attrs cols='50' rows='12' class='jush-js'>" . |
||
186 | $this->util->html($value) . '</textarea>'; |
||
187 | } else { |
||
188 | $unsigned = $field->unsigned ?? false; |
||
189 | // int(3) is only a display hint |
||
190 | $maxlength = (!preg_match('~int~', $field->type) && |
||
191 | preg_match('~^(\d+)(,(\d+))?$~', $field->length, $match) ? |
||
192 | ((preg_match("~binary~", $field->type) ? 2 : 1) * $match[1] + (($match[3] ?? null) ? 1 : 0) + |
||
193 | (($match[2] ?? false) && !$unsigned ? 1 : 0)) : |
||
194 | ($this->driver->typeExists($field->type) ? $this->driver->type($field->type) + ($unsigned ? 0 : 1) : 0)); |
||
195 | if ($this->driver->jush() == 'sql' && $this->driver->minVersion(5.6) && preg_match('~time~', $field->type)) { |
||
196 | $maxlength += 7; // microtime |
||
197 | } |
||
198 | // type='date' and type='time' display localized value which may be confusing, |
||
199 | // type='datetime' uses 'T' as date and time separator |
||
200 | $hasFunction = (in_array($function, $functions) || isset($functions[$function])); |
||
201 | $entry['input']['value'] = "<input" . ((!$hasFunction || $function === "") && |
||
202 | preg_match('~(?<!o)int(?!er)~', $field->type) && |
||
203 | !preg_match('~\[\]~', $field->fullType) ? " type='number'" : "") . " value='" . |
||
204 | $this->util->html($value) . "'" . ($maxlength ? " data-maxlength='$maxlength'" : "") . |
||
205 | (preg_match('~char|binary~', $field->type) && $maxlength > 20 ? " size='40'" : "") . "$attrs>"; |
||
206 | } |
||
207 | |||
208 | return $entry;*/ |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Get the table fields |
||
213 | * |
||
214 | * @param string $table The table name |
||
215 | * @param array $queryOptions The query options |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | private function getFields(string $table, array $queryOptions): array |
||
220 | { |
||
221 | // From edit.inc.php |
||
222 | $fields = $this->driver->fields($table); |
||
223 | |||
224 | //!!!! $queryOptions["select"] is never set here !!!!// |
||
225 | |||
226 | $where = $this->admin->where($queryOptions, $fields); |
||
227 | $update = $where; |
||
228 | foreach ($fields as $name => $field) { |
||
229 | $generated = $field->generated ?? false; |
||
230 | if (!isset($field->privileges[$update ? "update" : "insert"]) || |
||
231 | $this->util->fieldName($field) == "" || $generated) { |
||
232 | unset($fields[$name]); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | return [$fields, $where, $update]; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Get data for insert/update on a table |
||
241 | * |
||
242 | * @param string $table The table name |
||
243 | * @param array $queryOptions The query options |
||
244 | * |
||
245 | * @return array |
||
246 | */ |
||
247 | public function getQueryData(string $table, array $queryOptions = []): array |
||
248 | { |
||
249 | $isInsert = (count($queryOptions) === 0); // True only on insert. |
||
250 | // Default options |
||
251 | $queryOptions['clone'] = false; |
||
252 | $queryOptions['save'] = false; |
||
253 | |||
254 | list($fields, $where, $update) = $this->getFields($table, $queryOptions); |
||
255 | |||
256 | // From edit.inc.php |
||
257 | $row = null; |
||
258 | if (($where)) { |
||
259 | $select = []; |
||
260 | foreach ($fields as $name => $field) { |
||
261 | if (isset($field->privileges["select"])) { |
||
262 | $as = $this->driver->convertField($field); |
||
263 | if ($queryOptions["clone"] && $field->autoIncrement) { |
||
264 | $as = "''"; |
||
265 | } |
||
266 | if ($this->driver->jush() == "sql" && preg_match("~enum|set~", $field->type)) { |
||
267 | $as = "1*" . $this->driver->escapeId($name); |
||
268 | } |
||
269 | $select[] = ($as ? "$as AS " : "") . $this->driver->escapeId($name); |
||
270 | } |
||
271 | } |
||
272 | $row = []; |
||
273 | if (!$this->driver->support("table")) { |
||
274 | $select = ["*"]; |
||
275 | } |
||
276 | if ($select) { |
||
277 | $statement = $this->driver->select($table, $select, [$where], $select, [], |
||
278 | (isset($queryOptions["select"]) ? 2 : 1)); |
||
279 | if (($statement)) { |
||
280 | $row = $statement->fetchAssoc(); |
||
281 | }/* else { |
||
282 | $error = $this->driver->error(); |
||
283 | }*/ |
||
284 | // if(isset($queryOptions["select"]) && (!$row || $statement->fetchAssoc())) |
||
285 | // { |
||
286 | // // $statement->rowCount() != 1 isn't available in all drivers |
||
287 | // $row = null; |
||
288 | // } |
||
289 | } |
||
290 | } |
||
291 | |||
292 | if (!$this->driver->support("table") && empty($fields)) { |
||
293 | $primary = ''; // $this->driver->primaryIdName(); |
||
294 | if (!$where) { |
||
295 | // insert |
||
296 | $statement = $this->driver->select($table, ["*"], [$where], ["*"]); |
||
297 | $row = ($statement ? $statement->fetchAssoc() : false); |
||
298 | if (!$row) { |
||
299 | $row = [$primary => ""]; |
||
300 | } |
||
301 | } |
||
302 | if ($row) { |
||
303 | foreach ($row as $key => $val) { |
||
304 | if (!$where) { |
||
305 | $row[$key] = null; |
||
306 | } |
||
307 | $fields[$key] = [ |
||
308 | "name" => $key, |
||
309 | "null" => ($key !== $primary), |
||
310 | "autoIncrement" => ($key === $primary) |
||
311 | ]; |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | |||
316 | // From functions.inc.php (function edit_form($table, $fields, $row, $update)) |
||
317 | $entries = []; |
||
318 | $tableName = $this->util->tableName($this->driver->tableStatusOrName($table, true)); |
||
319 | $error = null; |
||
320 | if (($where) && $row === null) { // No row found to edit. |
||
321 | $error = $this->trans->lang('No rows.'); |
||
322 | } elseif (!$fields) { |
||
323 | $error = $this->trans->lang('You have no privileges to update this table.'); |
||
324 | } else { |
||
325 | foreach ($fields as $name => $field) { |
||
326 | // $default = $queryOptions["set"][$this->util->bracketEscape($name)] ?? null; |
||
327 | // if($default === null) |
||
328 | // { |
||
329 | $default = $field->default; |
||
330 | if ($field->type == "bit" && preg_match("~^b'([01]*)'\$~", $default, $regs)) { |
||
331 | $default = $regs[1]; |
||
332 | } |
||
333 | // } |
||
334 | $value = ($row !== null ? |
||
335 | ($row[$name] != "" && $this->driver->jush() == "sql" && preg_match("~enum|set~", $field->type) ? |
||
336 | (is_array($row[$name]) ? array_sum($row[$name]) : +$row[$name]) : |
||
337 | (is_bool($row[$name]) ? +$row[$name] : $row[$name])) : |
||
338 | (!$update && $field->autoIncrement ? "" : (isset($queryOptions["select"]) ? false : $default))); |
||
339 | $function = ($queryOptions["save"] ? (string)$queryOptions["function"][$name] : |
||
340 | ($update && preg_match('~^CURRENT_TIMESTAMP~i', $field->onUpdate) ? "now" : |
||
341 | ($value === false ? null : ($value !== null ? '' : 'NULL')))); |
||
342 | if (!$update && $value == $field->default && preg_match('~^[\w.]+\(~', $value)) { |
||
343 | $function = "SQL"; |
||
344 | } |
||
345 | if (preg_match("~time~", $field->type) && preg_match('~^CURRENT_TIMESTAMP~i', $value)) { |
||
346 | $value = ""; |
||
347 | $function = "now"; |
||
348 | } |
||
349 | |||
350 | $entries[$name] = $this->getFieldInput($field, $value, $function, $queryOptions); |
||
351 | } |
||
352 | } |
||
353 | |||
354 | $mainActions = [ |
||
355 | 'query-back' => $this->trans->lang('Back'), |
||
356 | 'query-save' => $this->trans->lang('Save'), |
||
357 | ]; |
||
358 | if ($isInsert) { |
||
359 | $mainActions['query-save-select'] = $this->trans->lang('Save and select'); |
||
360 | } |
||
361 | |||
362 | $fields = $entries; |
||
363 | return compact('mainActions', 'tableName', 'error', 'fields'); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Insert a new item in a table |
||
368 | * |
||
369 | * @param string $table The table name |
||
370 | * @param array $queryOptions The query options |
||
371 | * |
||
372 | * @return array |
||
373 | */ |
||
374 | public function insertItem(string $table, array $queryOptions): array |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Update one or more items in a table |
||
398 | * |
||
399 | * @param string $table The table name |
||
400 | * @param array $queryOptions The query options |
||
401 | * |
||
402 | * @return array |
||
403 | */ |
||
404 | public function updateItem(string $table, array $queryOptions): array |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Delete one or more items in a table |
||
431 | * |
||
432 | * @param string $table The table name |
||
433 | * @param array $queryOptions The query options |
||
434 | * |
||
435 | * @return array |
||
436 | */ |
||
437 | public function deleteItem(string $table, array $queryOptions): array |
||
452 | } |
||
453 | } |
||
454 |