Total Complexity | 89 |
Total Lines | 802 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like TypesController 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 TypesController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class TypesController extends BaseController |
||
15 | { |
||
16 | public $controller_title = 'strtypes'; |
||
17 | |||
18 | /** |
||
19 | * Default method to render the controller according to the action parameter. |
||
20 | */ |
||
21 | public function render() |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Show default list of types in the database. |
||
87 | * |
||
88 | * @param mixed $msg |
||
89 | */ |
||
90 | public function doDefault($msg = ''): void |
||
91 | { |
||
92 | $data = $this->misc->getDatabaseAccessor(); |
||
93 | |||
94 | $this->printTrail('schema'); |
||
95 | $this->printTabs('schema', 'types'); |
||
96 | $this->printMsg($msg); |
||
97 | |||
98 | $types = $data->getTypes(); |
||
99 | |||
100 | $columns = [ |
||
101 | 'type' => [ |
||
102 | 'title' => $this->lang['strtype'], |
||
103 | 'field' => Decorator::field('typname'), |
||
104 | 'url' => "types?action=properties&{$this->misc->href}&", |
||
105 | 'vars' => ['type' => 'basename'], |
||
106 | ], |
||
107 | 'owner' => [ |
||
108 | 'title' => $this->lang['strowner'], |
||
109 | 'field' => Decorator::field('typowner'), |
||
110 | ], |
||
111 | 'flavour' => [ |
||
112 | 'title' => $this->lang['strflavor'], |
||
113 | 'field' => Decorator::field('typtype'), |
||
114 | 'type' => 'verbatim', |
||
115 | 'params' => [ |
||
116 | 'map' => [ |
||
117 | 'b' => $this->lang['strbasetype'], |
||
118 | 'c' => $this->lang['strcompositetype'], |
||
119 | 'd' => $this->lang['strdomain'], |
||
120 | 'p' => $this->lang['strpseudotype'], |
||
121 | 'e' => $this->lang['strenum'], |
||
122 | ], |
||
123 | 'align' => 'center', |
||
124 | ], |
||
125 | ], |
||
126 | 'actions' => [ |
||
127 | 'title' => $this->lang['stractions'], |
||
128 | ], |
||
129 | 'comment' => [ |
||
130 | 'title' => $this->lang['strcomment'], |
||
131 | 'field' => Decorator::field('typcomment'), |
||
132 | ], |
||
133 | ]; |
||
134 | |||
135 | if (!isset($types->fields['typtype'])) { |
||
136 | unset($columns['flavour']); |
||
137 | } |
||
138 | |||
139 | $actions = [ |
||
140 | 'drop' => [ |
||
141 | 'content' => $this->lang['strdrop'], |
||
142 | 'attr' => [ |
||
143 | 'href' => [ |
||
144 | 'url' => 'types', |
||
145 | 'urlvars' => [ |
||
146 | 'action' => 'confirm_drop', |
||
147 | 'type' => Decorator::field('basename'), |
||
148 | ], |
||
149 | ], |
||
150 | ], |
||
151 | ], |
||
152 | ]; |
||
153 | |||
154 | echo $this->printTable($types, $columns, $actions, 'types-types', $this->lang['strnotypes']); |
||
|
|||
155 | |||
156 | $navlinks = [ |
||
157 | 'create' => [ |
||
158 | 'attr' => [ |
||
159 | 'href' => [ |
||
160 | 'url' => 'types', |
||
161 | 'urlvars' => [ |
||
162 | 'action' => 'create', |
||
163 | 'server' => $_REQUEST['server'], |
||
164 | 'database' => $_REQUEST['database'], |
||
165 | 'schema' => $_REQUEST['schema'], |
||
166 | ], |
||
167 | ], |
||
168 | ], |
||
169 | 'content' => $this->lang['strcreatetype'], |
||
170 | ], |
||
171 | 'createcomp' => [ |
||
172 | 'attr' => [ |
||
173 | 'href' => [ |
||
174 | 'url' => 'types', |
||
175 | 'urlvars' => [ |
||
176 | 'action' => 'create_comp', |
||
177 | 'server' => $_REQUEST['server'], |
||
178 | 'database' => $_REQUEST['database'], |
||
179 | 'schema' => $_REQUEST['schema'], |
||
180 | ], |
||
181 | ], |
||
182 | ], |
||
183 | 'content' => $this->lang['strcreatecomptype'], |
||
184 | ], |
||
185 | 'createenum' => [ |
||
186 | 'attr' => [ |
||
187 | 'href' => [ |
||
188 | 'url' => 'types', |
||
189 | 'urlvars' => [ |
||
190 | 'action' => 'create_enum', |
||
191 | 'server' => $_REQUEST['server'], |
||
192 | 'database' => $_REQUEST['database'], |
||
193 | 'schema' => $_REQUEST['schema'], |
||
194 | ], |
||
195 | ], |
||
196 | ], |
||
197 | 'content' => $this->lang['strcreateenumtype'], |
||
198 | ], |
||
199 | ]; |
||
200 | |||
201 | //if (!$data->hasEnumTypes()) { unset($navlinks['enum']); } |
||
202 | |||
203 | $this->printNavLinks($navlinks, 'types-types', \get_defined_vars()); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Generate XML for the browser tree. |
||
208 | */ |
||
209 | public function doTree() |
||
210 | { |
||
211 | $data = $this->misc->getDatabaseAccessor(); |
||
212 | |||
213 | $types = $data->getTypes(); |
||
214 | |||
215 | $reqvars = $this->misc->getRequestVars('type'); |
||
216 | |||
217 | $attrs = [ |
||
218 | 'text' => Decorator::field('typname'), |
||
219 | 'icon' => 'Type', |
||
220 | 'toolTip' => Decorator::field('typcomment'), |
||
221 | 'action' => Decorator::actionurl( |
||
222 | 'types', |
||
223 | $reqvars, |
||
224 | [ |
||
225 | 'action' => 'properties', |
||
226 | 'type' => Decorator::field('basename'), |
||
227 | ] |
||
228 | ), |
||
229 | ]; |
||
230 | |||
231 | return $this->printTree($types, $attrs, 'types'); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Show read only properties for a type. |
||
236 | * |
||
237 | * @param mixed $msg |
||
238 | */ |
||
239 | public function doProperties($msg = ''): void |
||
240 | { |
||
241 | $data = $this->misc->getDatabaseAccessor(); |
||
242 | // Get type (using base name) |
||
243 | $typedata = $data->getType($_REQUEST['type']); |
||
244 | |||
245 | $this->printTrail('type'); |
||
246 | $this->printTitle($this->lang['strproperties'], 'pg.type'); |
||
247 | $this->printMsg($msg); |
||
248 | |||
249 | $attPre = static function (&$rowdata) use ($data): void { |
||
250 | $rowdata->fields['+type'] = $data->formatType($rowdata->fields['type'], $rowdata->fields['atttypmod']); |
||
251 | }; |
||
252 | |||
253 | if (0 < $typedata->recordCount()) { |
||
254 | $vals = false; |
||
255 | |||
256 | switch ($typedata->fields['typtype']) { |
||
257 | case 'c': |
||
258 | $attrs = $data->getTableAttributes($_REQUEST['type']); |
||
259 | |||
260 | $columns = [ |
||
261 | 'field' => [ |
||
262 | 'title' => $this->lang['strfield'], |
||
263 | 'field' => Decorator::field('attname'), |
||
264 | ], |
||
265 | 'type' => [ |
||
266 | 'title' => $this->lang['strtype'], |
||
267 | 'field' => Decorator::field('+type'), |
||
268 | ], |
||
269 | 'comment' => [ |
||
270 | 'title' => $this->lang['strcomment'], |
||
271 | 'field' => Decorator::field('comment'), |
||
272 | ], |
||
273 | ]; |
||
274 | |||
275 | $actions = []; |
||
276 | |||
277 | echo $this->printTable($attrs, $columns, $actions, 'types-properties', $this->lang['strnodata'], $attPre); |
||
278 | |||
279 | break; |
||
280 | case 'e': |
||
281 | $vals = $data->getEnumValues($typedata->fields['typname']); |
||
282 | // no break |
||
283 | default: |
||
284 | $byval = $data->phpBool($typedata->fields['typbyval']); |
||
285 | echo '<table>' . \PHP_EOL; |
||
286 | echo "<tr><th class=\"data left\">{$this->lang['strname']}</th>" . \PHP_EOL; |
||
287 | echo '<td class="data1">', $this->misc->printVal($typedata->fields['typname']), '</td></tr>' . \PHP_EOL; |
||
288 | echo "<tr><th class=\"data left\">{$this->lang['strinputfn']}</th>" . \PHP_EOL; |
||
289 | echo '<td class="data1">', $this->misc->printVal($typedata->fields['typin']), '</td></tr>' . \PHP_EOL; |
||
290 | echo "<tr><th class=\"data left\">{$this->lang['stroutputfn']}</th>" . \PHP_EOL; |
||
291 | echo '<td class="data1">', $this->misc->printVal($typedata->fields['typout']), '</td></tr>' . \PHP_EOL; |
||
292 | echo "<tr><th class=\"data left\">{$this->lang['strlength']}</th>" . \PHP_EOL; |
||
293 | echo '<td class="data1">', $this->misc->printVal($typedata->fields['typlen']), '</td></tr>' . \PHP_EOL; |
||
294 | echo "<tr><th class=\"data left\">{$this->lang['strpassbyval']}</th>" . \PHP_EOL; |
||
295 | echo '<td class="data1">', ($byval) ? $this->lang['stryes'] : $this->lang['strno'], '</td></tr>' . \PHP_EOL; |
||
296 | echo "<tr><th class=\"data left\">{$this->lang['stralignment']}</th>" . \PHP_EOL; |
||
297 | echo '<td class="data1">', $this->misc->printVal($typedata->fields['typalign']), '</td></tr>' . \PHP_EOL; |
||
298 | |||
299 | if ($data->hasEnumTypes() && $vals) { |
||
300 | $vals = $vals->getArray(); |
||
301 | $nbVals = \count($vals); |
||
302 | echo "<tr>\n\t<th class=\"data left\" rowspan=\"{$nbVals}\">{$this->lang['strenumvalues']}</th>" . \PHP_EOL; |
||
303 | echo "<td class=\"data2\">{$vals[0]['enumval']}</td></tr>" . \PHP_EOL; |
||
304 | |||
305 | for ($i = 1; $i < $nbVals; ++$i) { |
||
306 | echo '<td class="data', 2 - ($i % 2), "\">{$vals[$i]['enumval']}</td></tr>" . \PHP_EOL; |
||
307 | } |
||
308 | } |
||
309 | echo '</table>' . \PHP_EOL; |
||
310 | } |
||
311 | |||
312 | $this->printNavLinks(['showall' => [ |
||
313 | 'attr' => [ |
||
314 | 'href' => [ |
||
315 | 'url' => 'types', |
||
316 | 'urlvars' => [ |
||
317 | 'server' => $_REQUEST['server'], |
||
318 | 'database' => $_REQUEST['database'], |
||
319 | 'schema' => $_REQUEST['schema'], |
||
320 | ], |
||
321 | ], |
||
322 | ], |
||
323 | 'content' => $this->lang['strshowalltypes'], |
||
324 | ]], 'types-properties', \get_defined_vars()); |
||
325 | } else { |
||
326 | $this->doDefault($this->lang['strinvalidparam']); |
||
327 | } |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Show confirmation of drop and perform actual drop. |
||
332 | * |
||
333 | * @param mixed $confirm |
||
334 | */ |
||
335 | public function doDrop($confirm): void |
||
336 | { |
||
337 | $data = $this->misc->getDatabaseAccessor(); |
||
338 | |||
339 | if ($confirm) { |
||
340 | $this->printTrail('type'); |
||
341 | $this->printTitle($this->lang['strdrop'], 'pg.type.drop'); |
||
342 | |||
343 | echo '<p>', \sprintf($this->lang['strconfdroptype'], $this->misc->printVal($_REQUEST['type'])), '</p>' . \PHP_EOL; |
||
344 | |||
345 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/types" method="post">' . \PHP_EOL; |
||
346 | echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label></p>" . \PHP_EOL; |
||
347 | echo '<p><input type="hidden" name="action" value="drop" />' . \PHP_EOL; |
||
348 | echo '<input type="hidden" name="type" value="', \htmlspecialchars($_REQUEST['type']), '" />' . \PHP_EOL; |
||
349 | echo $this->view->form; |
||
350 | echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />" . \PHP_EOL; |
||
351 | echo \sprintf('<input type="submit" name="cancel" value="%s" /></p>%s', $this->lang['strcancel'], \PHP_EOL); |
||
352 | echo '</form>' . \PHP_EOL; |
||
353 | } else { |
||
354 | $status = $data->dropType($_POST['type'], isset($_POST['cascade'])); |
||
355 | |||
356 | if (0 === $status) { |
||
357 | $this->doDefault($this->lang['strtypedropped']); |
||
358 | } else { |
||
359 | $this->doDefault($this->lang['strtypedroppedbad']); |
||
360 | } |
||
361 | } |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Displays a screen where they can enter a new composite type. |
||
366 | * |
||
367 | * @param mixed $msg |
||
368 | */ |
||
369 | public function doCreateComposite($msg = ''): void |
||
370 | { |
||
371 | $data = $this->misc->getDatabaseAccessor(); |
||
372 | |||
373 | $this->coalesceArr($_REQUEST, 'stage', 1); |
||
374 | |||
375 | $this->coalesceArr($_REQUEST, 'name', ''); |
||
376 | |||
377 | $this->coalesceArr($_REQUEST, 'fields', ''); |
||
378 | |||
379 | $this->coalesceArr($_REQUEST, 'typcomment', ''); |
||
380 | |||
381 | switch ($_REQUEST['stage']) { |
||
382 | case 1: |
||
383 | $this->printTrail('type'); |
||
384 | $this->printTitle($this->lang['strcreatecomptype'], 'pg.type.create'); |
||
385 | $this->printMsg($msg); |
||
386 | |||
387 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/types" method="post">' . \PHP_EOL; |
||
388 | echo '<table>' . \PHP_EOL; |
||
389 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL; |
||
390 | echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
391 | \htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
392 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strnumfields']}</th>" . \PHP_EOL; |
||
393 | echo "\t\t<td class=\"data\"><input name=\"fields\" size=\"5\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
394 | \htmlspecialchars($_REQUEST['fields']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
395 | |||
396 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>" . \PHP_EOL; |
||
397 | echo "\t\t<td><textarea name=\"typcomment\" rows=\"3\" cols=\"32\">", |
||
398 | \htmlspecialchars($_REQUEST['typcomment']), "</textarea></td>\n\t</tr>" . \PHP_EOL; |
||
399 | |||
400 | echo '</table>' . \PHP_EOL; |
||
401 | echo '<p><input type="hidden" name="action" value="create_comp" />' . \PHP_EOL; |
||
402 | echo '<input type="hidden" name="stage" value="2" />' . \PHP_EOL; |
||
403 | echo $this->view->form; |
||
404 | echo "<input type=\"submit\" value=\"{$this->lang['strnext']}\" />" . \PHP_EOL; |
||
405 | echo \sprintf('<input type="submit" name="cancel" value="%s" /></p>%s', $this->lang['strcancel'], \PHP_EOL); |
||
406 | echo '</form>' . \PHP_EOL; |
||
407 | |||
408 | break; |
||
409 | case 2: |
||
410 | // Check inputs |
||
411 | $fields = \trim($_REQUEST['fields']); |
||
412 | |||
413 | if ('' === \trim($_REQUEST['name'])) { |
||
414 | $_REQUEST['stage'] = 1; |
||
415 | $this->doCreateComposite($this->lang['strtypeneedsname']); |
||
416 | |||
417 | return; |
||
418 | } |
||
419 | |||
420 | if ('' === $fields || !\is_numeric($fields) || (int) $fields !== $fields || 1 > $fields) { |
||
421 | $_REQUEST['stage'] = 1; |
||
422 | $this->doCreateComposite($this->lang['strtypeneedscols']); |
||
423 | |||
424 | return; |
||
425 | } |
||
426 | |||
427 | $types = $data->getTypes(true, false, true); |
||
428 | |||
429 | $this->printTrail('schema'); |
||
430 | $this->printTitle($this->lang['strcreatecomptype'], 'pg.type.create'); |
||
431 | $this->printMsg($msg); |
||
432 | |||
433 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/types" method="post">' . \PHP_EOL; |
||
434 | |||
435 | // Output table header |
||
436 | echo '<table>' . \PHP_EOL; |
||
437 | echo "\t<tr><th colspan=\"2\" class=\"data required\">{$this->lang['strfield']}</th><th colspan=\"2\" class=\"data required\">{$this->lang['strtype']}</th>"; |
||
438 | echo "<th class=\"data\">{$this->lang['strlength']}</th><th class=\"data\">{$this->lang['strcomment']}</th></tr>" . \PHP_EOL; |
||
439 | |||
440 | for ($i = 0; $i < $_REQUEST['fields']; ++$i) { |
||
441 | if (!isset($_REQUEST['field'][$i])) { |
||
442 | $_REQUEST['field'][$i] = ''; |
||
443 | } |
||
444 | |||
445 | if (!isset($_REQUEST['length'][$i])) { |
||
446 | $_REQUEST['length'][$i] = ''; |
||
447 | } |
||
448 | |||
449 | if (!isset($_REQUEST['colcomment'][$i])) { |
||
450 | $_REQUEST['colcomment'][$i] = ''; |
||
451 | } |
||
452 | |||
453 | echo "\t<tr>\n\t\t<td>", $i + 1, '. </td>' . \PHP_EOL; |
||
454 | echo "\t\t<td><input name=\"field[{$i}]\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
455 | \htmlspecialchars($_REQUEST['field'][$i]), '" /></td>' . \PHP_EOL; |
||
456 | echo "\t\t<td>\n\t\t\t<select name=\"type[{$i}]\">" . \PHP_EOL; |
||
457 | $types->moveFirst(); |
||
458 | |||
459 | while (!$types->EOF) { |
||
460 | $typname = $types->fields['typname']; |
||
461 | echo "\t\t\t\t<option value=\"", \htmlspecialchars($typname), '"', |
||
462 | (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] === $typname) ? ' selected="selected"' : '', '>', |
||
463 | $this->misc->printVal($typname), '</option>' . \PHP_EOL; |
||
464 | $types->moveNext(); |
||
465 | } |
||
466 | echo "\t\t\t</select>\n\t\t</td>" . \PHP_EOL; |
||
467 | |||
468 | // Output array type selector |
||
469 | echo "\t\t<td>\n\t\t\t<select name=\"array[{$i}]\">" . \PHP_EOL; |
||
470 | echo "\t\t\t\t<option value=\"\"", (isset($_REQUEST['array'][$i]) && '' === $_REQUEST['array'][$i]) ? ' selected="selected"' : '', '></option>' . \PHP_EOL; |
||
471 | echo "\t\t\t\t<option value=\"[]\"", (isset($_REQUEST['array'][$i]) && '[]' === $_REQUEST['array'][$i]) ? ' selected="selected"' : '', '>[ ]</option>' . \PHP_EOL; |
||
472 | echo "\t\t\t</select>\n\t\t</td>" . \PHP_EOL; |
||
473 | |||
474 | echo "\t\t<td><input name=\"length[{$i}]\" size=\"10\" value=\"", |
||
475 | \htmlspecialchars($_REQUEST['length'][$i]), '" /></td>' . \PHP_EOL; |
||
476 | echo "\t\t<td><input name=\"colcomment[{$i}]\" size=\"40\" value=\"", |
||
477 | \htmlspecialchars($_REQUEST['colcomment'][$i]), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
478 | } |
||
479 | echo '</table>' . \PHP_EOL; |
||
480 | echo '<p><input type="hidden" name="action" value="create_comp" />' . \PHP_EOL; |
||
481 | echo '<input type="hidden" name="stage" value="3" />' . \PHP_EOL; |
||
482 | echo $this->view->form; |
||
483 | echo '<input type="hidden" name="name" value="', \htmlspecialchars($_REQUEST['name']), '" />' . \PHP_EOL; |
||
484 | echo '<input type="hidden" name="fields" value="', \htmlspecialchars($_REQUEST['fields']), '" />' . \PHP_EOL; |
||
485 | echo '<input type="hidden" name="typcomment" value="', \htmlspecialchars($_REQUEST['typcomment']), '" />' . \PHP_EOL; |
||
486 | echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />" . \PHP_EOL; |
||
487 | echo \sprintf('<input type="submit" name="cancel" value="%s" /></p>%s', $this->lang['strcancel'], \PHP_EOL); |
||
488 | echo '</form>' . \PHP_EOL; |
||
489 | |||
490 | break; |
||
491 | case 3: |
||
492 | // Check inputs |
||
493 | $fields = \trim($_REQUEST['fields']); |
||
494 | |||
495 | if ('' === \trim($_REQUEST['name'])) { |
||
496 | $_REQUEST['stage'] = 1; |
||
497 | $this->doCreateComposite($this->lang['strtypeneedsname']); |
||
498 | |||
499 | return; |
||
500 | } |
||
501 | |||
502 | if ('' === $fields || !\is_numeric($fields) || (int) $fields !== $fields || 0 >= $fields) { |
||
503 | $_REQUEST['stage'] = 1; |
||
504 | $this->doCreateComposite($this->lang['strtypeneedscols']); |
||
505 | |||
506 | return; |
||
507 | } |
||
508 | |||
509 | $status = $data->createCompositeType( |
||
510 | $_REQUEST['name'], |
||
511 | $_REQUEST['fields'], |
||
512 | $_REQUEST['field'], |
||
513 | $_REQUEST['type'], |
||
514 | $_REQUEST['array'], |
||
515 | $_REQUEST['length'], |
||
516 | $_REQUEST['colcomment'], |
||
517 | $_REQUEST['typcomment'] |
||
518 | ); |
||
519 | |||
520 | if (0 === $status) { |
||
521 | $this->doDefault($this->lang['strtypecreated']); |
||
522 | } elseif (-1 === $status) { |
||
523 | $_REQUEST['stage'] = 2; |
||
524 | $this->doCreateComposite($this->lang['strtypeneedsfield']); |
||
525 | |||
526 | return; |
||
527 | } else { |
||
528 | $_REQUEST['stage'] = 2; |
||
529 | $this->doCreateComposite($this->lang['strtypecreatedbad']); |
||
530 | |||
531 | return; |
||
532 | } |
||
533 | |||
534 | break; |
||
535 | |||
536 | default: |
||
537 | echo "<p>{$this->lang['strinvalidparam']}</p>" . \PHP_EOL; |
||
538 | } |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * Displays a screen where they can enter a new enum type. |
||
543 | * |
||
544 | * @param mixed $msg |
||
545 | */ |
||
546 | public function doCreateEnum($msg = ''): void |
||
547 | { |
||
548 | $data = $this->misc->getDatabaseAccessor(); |
||
549 | |||
550 | $this->coalesceArr($_REQUEST, 'stage', 1); |
||
551 | |||
552 | $this->coalesceArr($_REQUEST, 'name', ''); |
||
553 | |||
554 | $this->coalesceArr($_REQUEST, 'values', ''); |
||
555 | |||
556 | $this->coalesceArr($_REQUEST, 'typcomment', ''); |
||
557 | |||
558 | switch ($_REQUEST['stage']) { |
||
559 | case 1: |
||
560 | $this->printTrail('type'); |
||
561 | $this->printTitle($this->lang['strcreateenumtype'], 'pg.type.create'); |
||
562 | $this->printMsg($msg); |
||
563 | |||
564 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/types" method="post">' . \PHP_EOL; |
||
565 | echo '<table>' . \PHP_EOL; |
||
566 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL; |
||
567 | echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
568 | \htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
569 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strnumvalues']}</th>" . \PHP_EOL; |
||
570 | echo "\t\t<td class=\"data\"><input name=\"values\" size=\"5\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
571 | \htmlspecialchars($_REQUEST['values']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
572 | |||
573 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>" . \PHP_EOL; |
||
574 | echo "\t\t<td><textarea name=\"typcomment\" rows=\"3\" cols=\"32\">", |
||
575 | \htmlspecialchars($_REQUEST['typcomment']), "</textarea></td>\n\t</tr>" . \PHP_EOL; |
||
576 | |||
577 | echo '</table>' . \PHP_EOL; |
||
578 | echo '<p><input type="hidden" name="action" value="create_enum" />' . \PHP_EOL; |
||
579 | echo '<input type="hidden" name="stage" value="2" />' . \PHP_EOL; |
||
580 | echo $this->view->form; |
||
581 | echo "<input type=\"submit\" value=\"{$this->lang['strnext']}\" />" . \PHP_EOL; |
||
582 | echo \sprintf('<input type="submit" name="cancel" value="%s" /></p>%s', $this->lang['strcancel'], \PHP_EOL); |
||
583 | echo '</form>' . \PHP_EOL; |
||
584 | |||
585 | break; |
||
586 | case 2: |
||
587 | // Check inputs |
||
588 | $values = \trim($_REQUEST['values']); |
||
589 | |||
590 | if ('' === \trim($_REQUEST['name'])) { |
||
591 | $_REQUEST['stage'] = 1; |
||
592 | $this->doCreateEnum($this->lang['strtypeneedsname']); |
||
593 | |||
594 | return; |
||
595 | } |
||
596 | |||
597 | if ('' === $values || !\is_numeric($values) || (int) $values !== $values || 1 > $values) { |
||
598 | $_REQUEST['stage'] = 1; |
||
599 | $this->doCreateEnum($this->lang['strtypeneedsvals']); |
||
600 | |||
601 | return; |
||
602 | } |
||
603 | |||
604 | $this->printTrail('schema'); |
||
605 | $this->printTitle($this->lang['strcreateenumtype'], 'pg.type.create'); |
||
606 | $this->printMsg($msg); |
||
607 | |||
608 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/types" method="post">' . \PHP_EOL; |
||
609 | |||
610 | // Output table header |
||
611 | echo '<table>' . \PHP_EOL; |
||
612 | echo "\t<tr><th colspan=\"2\" class=\"data required\">{$this->lang['strvalue']}</th></tr>" . \PHP_EOL; |
||
613 | |||
614 | for ($i = 0; $i < $_REQUEST['values']; ++$i) { |
||
615 | if (!isset($_REQUEST['value'][$i])) { |
||
616 | $_REQUEST['value'][$i] = ''; |
||
617 | } |
||
618 | |||
619 | echo "\t<tr>\n\t\t<td>", $i + 1, '. </td>' . \PHP_EOL; |
||
620 | echo "\t\t<td><input name=\"value[{$i}]\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
621 | \htmlspecialchars($_REQUEST['value'][$i]), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
622 | } |
||
623 | echo '</table>' . \PHP_EOL; |
||
624 | echo '<p><input type="hidden" name="action" value="create_enum" />' . \PHP_EOL; |
||
625 | echo '<input type="hidden" name="stage" value="3" />' . \PHP_EOL; |
||
626 | echo $this->view->form; |
||
627 | echo '<input type="hidden" name="name" value="', \htmlspecialchars($_REQUEST['name']), '" />' . \PHP_EOL; |
||
628 | echo '<input type="hidden" name="values" value="', \htmlspecialchars($_REQUEST['values']), '" />' . \PHP_EOL; |
||
629 | echo '<input type="hidden" name="typcomment" value="', \htmlspecialchars($_REQUEST['typcomment']), '" />' . \PHP_EOL; |
||
630 | echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />" . \PHP_EOL; |
||
631 | echo \sprintf('<input type="submit" name="cancel" value="%s" /></p>%s', $this->lang['strcancel'], \PHP_EOL); |
||
632 | echo '</form>' . \PHP_EOL; |
||
633 | |||
634 | break; |
||
635 | case 3: |
||
636 | // Check inputs |
||
637 | $values = \trim($_REQUEST['values']); |
||
638 | |||
639 | if ('' === \trim($_REQUEST['name'])) { |
||
640 | $_REQUEST['stage'] = 1; |
||
641 | $this->doCreateEnum($this->lang['strtypeneedsname']); |
||
642 | |||
643 | return; |
||
644 | } |
||
645 | |||
646 | if ('' === $values || !\is_numeric($values) || (int) $values !== $values || 0 >= $values) { |
||
647 | $_REQUEST['stage'] = 1; |
||
648 | $this->doCreateEnum($this->lang['strtypeneedsvals']); |
||
649 | |||
650 | return; |
||
651 | } |
||
652 | |||
653 | $status = $data->createEnumType($_REQUEST['name'], $_REQUEST['value'], $_REQUEST['typcomment']); |
||
654 | |||
655 | if (0 === $status) { |
||
656 | $this->doDefault($this->lang['strtypecreated']); |
||
657 | } elseif (-1 === $status) { |
||
658 | $_REQUEST['stage'] = 2; |
||
659 | $this->doCreateEnum($this->lang['strtypeneedsvalue']); |
||
660 | |||
661 | return; |
||
662 | } else { |
||
663 | $_REQUEST['stage'] = 2; |
||
664 | $this->doCreateEnum($this->lang['strtypecreatedbad']); |
||
665 | |||
666 | return; |
||
667 | } |
||
668 | |||
669 | break; |
||
670 | |||
671 | default: |
||
672 | echo "<p>{$this->lang['strinvalidparam']}</p>" . \PHP_EOL; |
||
673 | } |
||
674 | } |
||
675 | |||
676 | /** |
||
677 | * Displays a screen where they can enter a new type. |
||
678 | * |
||
679 | * @param mixed $msg |
||
680 | */ |
||
681 | public function doCreate($msg = ''): void |
||
782 | } |
||
783 | |||
784 | /** |
||
785 | * Actually creates the new type in the database. |
||
786 | */ |
||
787 | public function doSaveCreate(): void |
||
816 | } |
||
817 | } |
||
818 | } |
||
820 |