| Total Complexity | 149 |
| Total Lines | 1217 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TablesController 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 TablesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class TablesController extends BaseController |
||
| 17 | { |
||
| 18 | use \PHPPgAdmin\Traits\AdminTrait; |
||
| 19 | public $table_place = 'tables-tables'; |
||
| 20 | public $controller_title = 'strtables'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Default method to render the controller according to the action parameter. |
||
| 24 | */ |
||
| 25 | public function render() |
||
| 26 | { |
||
| 27 | if ('tree' == $this->action) { |
||
| 28 | return $this->doTree(); |
||
| 29 | } |
||
| 30 | if ('subtree' == $this->action) { |
||
| 31 | return $this->doSubTree(); |
||
| 32 | } |
||
| 33 | if ('json' == $this->action) { |
||
| 34 | return $this->displayJson(); |
||
| 35 | } |
||
| 36 | |||
| 37 | $header_template = 'header.twig'; |
||
| 38 | $footer_template = 'footer.twig'; |
||
|
|
|||
| 39 | |||
| 40 | ob_start(); |
||
| 41 | |||
| 42 | switch ($this->action) { |
||
| 43 | case 'create': |
||
| 44 | |||
| 45 | if (isset($_POST['cancel'])) { |
||
| 46 | $this->doDefault(); |
||
| 47 | } else { |
||
| 48 | $header_template = 'header_select2.twig'; |
||
| 49 | $this->doCreate(); |
||
| 50 | } |
||
| 51 | |||
| 52 | break; |
||
| 53 | case 'createlike': |
||
| 54 | $header_template = 'header_select2.twig'; |
||
| 55 | $this->doCreateLike(false); |
||
| 56 | |||
| 57 | break; |
||
| 58 | case 'confcreatelike': |
||
| 59 | if (isset($_POST['cancel'])) { |
||
| 60 | $header_template = 'header_datatables.twig'; |
||
| 61 | $this->doDefault(); |
||
| 62 | } else { |
||
| 63 | //$header_template = 'header_select2.twig'; |
||
| 64 | $this->doCreateLike(true); |
||
| 65 | } |
||
| 66 | |||
| 67 | break; |
||
| 68 | case 'selectrows': |
||
| 69 | if (!isset($_POST['cancel'])) { |
||
| 70 | $this->doSelectRows(false); |
||
| 71 | } else { |
||
| 72 | $header_template = 'header_datatables.twig'; |
||
| 73 | $this->doDefault(); |
||
| 74 | } |
||
| 75 | |||
| 76 | break; |
||
| 77 | case 'confselectrows': |
||
| 78 | $this->doSelectRows(true); |
||
| 79 | |||
| 80 | break; |
||
| 81 | case 'insertrow': |
||
| 82 | if (!isset($_POST['cancel'])) { |
||
| 83 | $this->doInsertRow(false); |
||
| 84 | } else { |
||
| 85 | $header_template = 'header_datatables.twig'; |
||
| 86 | $this->doDefault(); |
||
| 87 | } |
||
| 88 | |||
| 89 | break; |
||
| 90 | case 'confinsertrow': |
||
| 91 | $this->doInsertRow(true); |
||
| 92 | |||
| 93 | break; |
||
| 94 | case 'empty': |
||
| 95 | if (isset($_POST['empty'])) { |
||
| 96 | $this->doEmpty(false); |
||
| 97 | } else { |
||
| 98 | $header_template = 'header_datatables.twig'; |
||
| 99 | $this->doDefault(); |
||
| 100 | } |
||
| 101 | |||
| 102 | break; |
||
| 103 | case 'confirm_empty': |
||
| 104 | $this->doEmpty(true); |
||
| 105 | |||
| 106 | break; |
||
| 107 | case 'drop': |
||
| 108 | if (isset($_POST['drop'])) { |
||
| 109 | $this->doDrop(false); |
||
| 110 | } else { |
||
| 111 | $header_template = 'header_datatables.twig'; |
||
| 112 | $this->doDefault(); |
||
| 113 | } |
||
| 114 | |||
| 115 | break; |
||
| 116 | case 'confirm_drop': |
||
| 117 | $this->doDrop(true); |
||
| 118 | |||
| 119 | break; |
||
| 120 | default: |
||
| 121 | if (false === $this->adminActions($this->action, 'table')) { |
||
| 122 | $header_template = 'header_datatables.twig'; |
||
| 123 | $this->doDefault(); |
||
| 124 | } |
||
| 125 | |||
| 126 | break; |
||
| 127 | } |
||
| 128 | |||
| 129 | $output = ob_get_clean(); |
||
| 130 | |||
| 131 | $this->printHeader($this->headerTitle(), null, true, $header_template); |
||
| 132 | $this->printBody(); |
||
| 133 | |||
| 134 | echo $output; |
||
| 135 | |||
| 136 | return $this->printFooter(); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Show default list of tables in the database. |
||
| 141 | * |
||
| 142 | * @param mixed $msg |
||
| 143 | */ |
||
| 144 | public function doDefault($msg = '') |
||
| 145 | { |
||
| 146 | $data = $this->misc->getDatabaseAccessor(); |
||
| 147 | |||
| 148 | $this->printTrail('schema'); |
||
| 149 | $this->printTabs('schema', 'tables'); |
||
| 150 | $this->printMsg($msg); |
||
| 151 | |||
| 152 | $tables = $data->getTables(); |
||
| 153 | |||
| 154 | $columns = [ |
||
| 155 | 'table' => [ |
||
| 156 | 'title' => $this->lang['strtable'], |
||
| 157 | 'field' => Decorator::field('relname'), |
||
| 158 | 'url' => \SUBFOLDER . "/redirect/table?{$this->misc->href}&", |
||
| 159 | 'vars' => ['table' => 'relname'], |
||
| 160 | ], |
||
| 161 | 'owner' => [ |
||
| 162 | 'title' => $this->lang['strowner'], |
||
| 163 | 'field' => Decorator::field('relowner'), |
||
| 164 | ], |
||
| 165 | 'tablespace' => [ |
||
| 166 | 'title' => $this->lang['strtablespace'], |
||
| 167 | 'field' => Decorator::field('tablespace'), |
||
| 168 | ], |
||
| 169 | 'tuples' => [ |
||
| 170 | 'title' => $this->lang['strestimatedrowcount'], |
||
| 171 | 'field' => Decorator::field('reltuples'), |
||
| 172 | 'type' => 'numeric', |
||
| 173 | ], |
||
| 174 | 'table_size' => [ |
||
| 175 | 'title' => $this->lang['strsize'], |
||
| 176 | 'field' => Decorator::field('table_size'), |
||
| 177 | ], |
||
| 178 | 'actions' => [ |
||
| 179 | 'title' => $this->lang['stractions'], |
||
| 180 | ], |
||
| 181 | 'comment' => [ |
||
| 182 | 'title' => $this->lang['strcomment'], |
||
| 183 | 'field' => Decorator::field('relcomment'), |
||
| 184 | ], |
||
| 185 | ]; |
||
| 186 | |||
| 187 | $actions = [ |
||
| 188 | 'multiactions' => [ |
||
| 189 | 'keycols' => ['table' => 'relname'], |
||
| 190 | 'url' => 'tables', |
||
| 191 | 'default' => 'analyze', |
||
| 192 | ], |
||
| 193 | 'browse' => [ |
||
| 194 | 'content' => $this->lang['strbrowse'], |
||
| 195 | 'attr' => [ |
||
| 196 | 'href' => [ |
||
| 197 | 'url' => 'display', |
||
| 198 | 'urlvars' => [ |
||
| 199 | 'subject' => 'table', |
||
| 200 | 'return' => 'table', |
||
| 201 | 'table' => Decorator::field('relname'), |
||
| 202 | ], |
||
| 203 | ], |
||
| 204 | ], |
||
| 205 | ], |
||
| 206 | 'select' => [ |
||
| 207 | 'content' => $this->lang['strselect'], |
||
| 208 | 'attr' => [ |
||
| 209 | 'href' => [ |
||
| 210 | 'url' => 'tables', |
||
| 211 | 'urlvars' => [ |
||
| 212 | 'action' => 'confselectrows', |
||
| 213 | 'table' => Decorator::field('relname'), |
||
| 214 | ], |
||
| 215 | ], |
||
| 216 | ], |
||
| 217 | ], |
||
| 218 | 'insert' => [ |
||
| 219 | 'content' => $this->lang['strinsert'], |
||
| 220 | 'attr' => [ |
||
| 221 | 'href' => [ |
||
| 222 | 'url' => 'tables', |
||
| 223 | 'urlvars' => [ |
||
| 224 | 'action' => 'confinsertrow', |
||
| 225 | 'table' => Decorator::field('relname'), |
||
| 226 | ], |
||
| 227 | ], |
||
| 228 | ], |
||
| 229 | ], |
||
| 230 | 'empty' => [ |
||
| 231 | 'multiaction' => 'confirm_empty', |
||
| 232 | 'content' => $this->lang['strempty'], |
||
| 233 | 'attr' => [ |
||
| 234 | 'href' => [ |
||
| 235 | 'url' => 'tables', |
||
| 236 | 'urlvars' => [ |
||
| 237 | 'action' => 'confirm_empty', |
||
| 238 | 'table' => Decorator::field('relname'), |
||
| 239 | ], |
||
| 240 | ], |
||
| 241 | ], |
||
| 242 | ], |
||
| 243 | 'alter' => [ |
||
| 244 | 'content' => $this->lang['stralter'], |
||
| 245 | 'attr' => [ |
||
| 246 | 'href' => [ |
||
| 247 | 'url' => 'tblproperties', |
||
| 248 | 'urlvars' => [ |
||
| 249 | 'action' => 'confirm_alter', |
||
| 250 | 'table' => Decorator::field('relname'), |
||
| 251 | ], |
||
| 252 | ], |
||
| 253 | ], |
||
| 254 | ], |
||
| 255 | 'drop' => [ |
||
| 256 | 'multiaction' => 'confirm_drop', |
||
| 257 | 'content' => $this->lang['strdrop'], |
||
| 258 | 'attr' => [ |
||
| 259 | 'href' => [ |
||
| 260 | 'url' => 'tables', |
||
| 261 | 'urlvars' => [ |
||
| 262 | 'action' => 'confirm_drop', |
||
| 263 | 'table' => Decorator::field('relname'), |
||
| 264 | ], |
||
| 265 | ], |
||
| 266 | ], |
||
| 267 | ], |
||
| 268 | 'vacuum' => [ |
||
| 269 | 'multiaction' => 'confirm_vacuum', |
||
| 270 | 'content' => $this->lang['strvacuum'], |
||
| 271 | 'attr' => [ |
||
| 272 | 'href' => [ |
||
| 273 | 'url' => 'tables', |
||
| 274 | 'urlvars' => [ |
||
| 275 | 'action' => 'confirm_vacuum', |
||
| 276 | 'table' => Decorator::field('relname'), |
||
| 277 | ], |
||
| 278 | ], |
||
| 279 | ], |
||
| 280 | ], |
||
| 281 | 'analyze' => [ |
||
| 282 | 'multiaction' => 'confirm_analyze', |
||
| 283 | 'content' => $this->lang['stranalyze'], |
||
| 284 | 'attr' => [ |
||
| 285 | 'href' => [ |
||
| 286 | 'url' => 'tables', |
||
| 287 | 'urlvars' => [ |
||
| 288 | 'action' => 'confirm_analyze', |
||
| 289 | 'table' => Decorator::field('relname'), |
||
| 290 | ], |
||
| 291 | ], |
||
| 292 | ], |
||
| 293 | ], |
||
| 294 | 'reindex' => [ |
||
| 295 | 'multiaction' => 'confirm_reindex', |
||
| 296 | 'content' => $this->lang['strreindex'], |
||
| 297 | 'attr' => [ |
||
| 298 | 'href' => [ |
||
| 299 | 'url' => 'tables', |
||
| 300 | 'urlvars' => [ |
||
| 301 | 'action' => 'confirm_reindex', |
||
| 302 | 'table' => Decorator::field('relname'), |
||
| 303 | ], |
||
| 304 | ], |
||
| 305 | ], |
||
| 306 | ], |
||
| 307 | //'cluster' TODO ? |
||
| 308 | ]; |
||
| 309 | |||
| 310 | if (!$data->hasTablespaces()) { |
||
| 311 | unset($columns['tablespace']); |
||
| 312 | } |
||
| 313 | |||
| 314 | //\Kint::dump($tables); |
||
| 315 | |||
| 316 | echo $this->printTable($tables, $columns, $actions, $this->table_place, $this->lang['strnotables']); |
||
| 317 | |||
| 318 | $navlinks = [ |
||
| 319 | 'create' => [ |
||
| 320 | 'attr' => [ |
||
| 321 | 'href' => [ |
||
| 322 | 'url' => 'tables', |
||
| 323 | 'urlvars' => [ |
||
| 324 | 'action' => 'create', |
||
| 325 | 'server' => $_REQUEST['server'], |
||
| 326 | 'database' => $_REQUEST['database'], |
||
| 327 | 'schema' => $_REQUEST['schema'], |
||
| 328 | ], |
||
| 329 | ], |
||
| 330 | ], |
||
| 331 | 'content' => $this->lang['strcreatetable'], |
||
| 332 | ], |
||
| 333 | ]; |
||
| 334 | |||
| 335 | if (($tables->recordCount() > 0) && $data->hasCreateTableLike()) { |
||
| 336 | $navlinks['createlike'] = [ |
||
| 337 | 'attr' => [ |
||
| 338 | 'href' => [ |
||
| 339 | 'url' => 'tables', |
||
| 340 | 'urlvars' => [ |
||
| 341 | 'action' => 'createlike', |
||
| 342 | 'server' => $_REQUEST['server'], |
||
| 343 | 'database' => $_REQUEST['database'], |
||
| 344 | 'schema' => $_REQUEST['schema'], |
||
| 345 | ], |
||
| 346 | ], |
||
| 347 | ], |
||
| 348 | 'content' => $this->lang['strcreatetablelike'], |
||
| 349 | ]; |
||
| 350 | } |
||
| 351 | $this->printNavLinks($navlinks, 'tables-tables', get_defined_vars()); |
||
| 352 | } |
||
| 353 | |||
| 354 | public function displayJson() |
||
| 355 | { |
||
| 356 | $data = $this->misc->getDatabaseAccessor(); |
||
| 357 | |||
| 358 | $tables = $data->getTables(); |
||
| 359 | |||
| 360 | $all_tables = $tables->getAll(); |
||
| 361 | |||
| 362 | return $this |
||
| 363 | ->container |
||
| 364 | ->responseobj |
||
| 365 | ->withStatus(200) |
||
| 366 | ->withJson($all_tables); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Generate XML for the browser tree. |
||
| 371 | */ |
||
| 372 | public function doTree() |
||
| 373 | { |
||
| 374 | $data = $this->misc->getDatabaseAccessor(); |
||
| 375 | |||
| 376 | //\PC::debug($this->misc->getDatabase(), 'getDatabase'); |
||
| 377 | |||
| 378 | $tables = $data->getTables(); |
||
| 379 | |||
| 380 | $reqvars = $this->misc->getRequestVars('table'); |
||
| 381 | |||
| 382 | $attrs = [ |
||
| 383 | 'text' => Decorator::field('relname'), |
||
| 384 | 'icon' => 'Table', |
||
| 385 | 'iconAction' => Decorator::url('display', $reqvars, ['table' => Decorator::field('relname')]), |
||
| 386 | 'toolTip' => Decorator::field('relcomment'), |
||
| 387 | 'action' => Decorator::redirecturl('redirect', $reqvars, ['table' => Decorator::field('relname')]), |
||
| 388 | 'branch' => Decorator::url('tables', $reqvars, ['action' => 'subtree', 'table' => Decorator::field('relname')]), |
||
| 389 | ]; |
||
| 390 | |||
| 391 | return $this->printTree($tables, $attrs, 'tables'); |
||
| 392 | } |
||
| 393 | |||
| 394 | public function doSubTree() |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Displays a screen where they can enter a new table. |
||
| 430 | * |
||
| 431 | * @param mixed $msg |
||
| 432 | */ |
||
| 433 | public function doCreate($msg = '') |
||
| 434 | { |
||
| 435 | $data = $this->misc->getDatabaseAccessor(); |
||
| 436 | |||
| 437 | if (!isset($_REQUEST['stage'])) { |
||
| 438 | $_REQUEST['stage'] = 1; |
||
| 439 | $default_with_oids = $data->getDefaultWithOid(); |
||
| 440 | if ('off' == $default_with_oids) { |
||
| 441 | $_REQUEST['withoutoids'] = 'on'; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | $this->coalesceArr($_REQUEST, 'name', ''); |
||
| 446 | |||
| 447 | $this->coalesceArr($_REQUEST, 'fields', ''); |
||
| 448 | |||
| 449 | $this->coalesceArr($_REQUEST, 'tblcomment', ''); |
||
| 450 | |||
| 451 | $this->coalesceArr($_REQUEST, 'spcname', ''); |
||
| 452 | |||
| 453 | switch ($_REQUEST['stage']) { |
||
| 454 | case 1: |
||
| 455 | // You are presented with a form in which you describe the table, pick the tablespace and state how many fields it will have |
||
| 456 | // Fetch all tablespaces from the database |
||
| 457 | if ($data->hasTablespaces()) { |
||
| 458 | $tablespaces = $data->getTablespaces(); |
||
| 459 | } |
||
| 460 | |||
| 461 | $this->printTrail('schema'); |
||
| 462 | $this->printTitle($this->lang['strcreatetable'], 'pg.table.create'); |
||
| 463 | $this->printMsg($msg); |
||
| 464 | |||
| 465 | echo '<form action="' . \SUBFOLDER . '/src/views/' . $this->script . '" method="post">'; |
||
| 466 | echo "\n"; |
||
| 467 | echo "<table>\n"; |
||
| 468 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>\n"; |
||
| 469 | echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 470 | htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n"; |
||
| 471 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strnumcols']}</th>\n"; |
||
| 472 | echo "\t\t<td class=\"data\"><input name=\"fields\" size=\"5\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 473 | htmlspecialchars($_REQUEST['fields']), "\" /></td>\n\t</tr>\n"; |
||
| 474 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['stroptions']}</th>\n"; |
||
| 475 | echo "\t\t<td class=\"data\"><label for=\"withoutoids\"><input type=\"checkbox\" id=\"withoutoids\" name=\"withoutoids\"", isset($_REQUEST['withoutoids']) ? ' checked="checked"' : '', " />WITHOUT OIDS</label></td>\n\t</tr>\n"; |
||
| 476 | |||
| 477 | // Tablespace (if there are any) |
||
| 478 | if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) { |
||
| 479 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strtablespace']}</th>\n"; |
||
| 480 | echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"spcname\">\n"; |
||
| 481 | // Always offer the default (empty) option |
||
| 482 | echo "\t\t\t\t<option value=\"\"", |
||
| 483 | ('' == $_REQUEST['spcname']) ? ' selected="selected"' : '', "></option>\n"; |
||
| 484 | // Display all other tablespaces |
||
| 485 | while (!$tablespaces->EOF) { |
||
| 486 | $spcname = htmlspecialchars($tablespaces->fields['spcname']); |
||
| 487 | echo "\t\t\t\t<option value=\"{$spcname}\"", |
||
| 488 | ($tablespaces->fields['spcname'] == $_REQUEST['spcname']) ? ' selected="selected"' : '', ">{$spcname}</option>\n"; |
||
| 489 | $tablespaces->moveNext(); |
||
| 490 | } |
||
| 491 | echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n"; |
||
| 492 | } |
||
| 493 | |||
| 494 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n"; |
||
| 495 | echo "\t\t<td><textarea name=\"tblcomment\" rows=\"3\" cols=\"32\">", |
||
| 496 | htmlspecialchars($_REQUEST['tblcomment']), "</textarea></td>\n\t</tr>\n"; |
||
| 497 | |||
| 498 | echo "</table>\n"; |
||
| 499 | echo "<p><input type=\"hidden\" name=\"action\" value=\"create\" />\n"; |
||
| 500 | echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n"; |
||
| 501 | echo $this->misc->form; |
||
| 502 | echo "<input type=\"submit\" value=\"{$this->lang['strnext']}\" />\n"; |
||
| 503 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
| 504 | echo "</form>\n"; |
||
| 505 | |||
| 506 | break; |
||
| 507 | case 2: |
||
| 508 | |||
| 509 | // Check inputs |
||
| 510 | $fields = trim($_REQUEST['fields']); |
||
| 511 | if ('' == trim($_REQUEST['name'])) { |
||
| 512 | $_REQUEST['stage'] = 1; |
||
| 513 | $this->doCreate($this->lang['strtableneedsname']); |
||
| 514 | |||
| 515 | return; |
||
| 516 | } |
||
| 517 | if ('' == $fields || !is_numeric($fields) || $fields != (int) $fields || $fields < 1) { |
||
| 518 | $_REQUEST['stage'] = 1; |
||
| 519 | $this->doCreate($this->lang['strtableneedscols']); |
||
| 520 | |||
| 521 | return; |
||
| 522 | } |
||
| 523 | |||
| 524 | $types = $data->getTypes(true, false, true); |
||
| 525 | $types_for_js = []; |
||
| 526 | |||
| 527 | $this->printTrail('schema'); |
||
| 528 | $this->printTitle($this->lang['strcreatetable'], 'pg.table.create'); |
||
| 529 | $this->printMsg($msg); |
||
| 530 | |||
| 531 | echo '<script src="' . \SUBFOLDER . '/assets/js/tables.js" type="text/javascript"></script>'; |
||
| 532 | echo '<form action="' . \SUBFOLDER . "/src/views/tables\" method=\"post\">\n"; |
||
| 533 | |||
| 534 | // Output table header |
||
| 535 | echo "<table>\n"; |
||
| 536 | echo "\t<tr><th colspan=\"2\" class=\"data required\">{$this->lang['strcolumn']}</th><th colspan=\"2\" class=\"data required\">{$this->lang['strtype']}</th>"; |
||
| 537 | echo "<th class=\"data\">{$this->lang['strlength']}</th><th class=\"data\">{$this->lang['strnotnull']}</th>"; |
||
| 538 | echo "<th class=\"data\">{$this->lang['struniquekey']}</th><th class=\"data\">{$this->lang['strprimarykey']}</th>"; |
||
| 539 | echo "<th class=\"data\">{$this->lang['strdefault']}</th><th class=\"data\">{$this->lang['strcomment']}</th></tr>\n"; |
||
| 540 | |||
| 541 | for ($i = 0; $i < $_REQUEST['fields']; ++$i) { |
||
| 542 | if (!isset($_REQUEST['field'][$i])) { |
||
| 543 | $_REQUEST['field'][$i] = ''; |
||
| 544 | } |
||
| 545 | |||
| 546 | if (!isset($_REQUEST['length'][$i])) { |
||
| 547 | $_REQUEST['length'][$i] = ''; |
||
| 548 | } |
||
| 549 | |||
| 550 | if (!isset($_REQUEST['default'][$i])) { |
||
| 551 | $_REQUEST['default'][$i] = ''; |
||
| 552 | } |
||
| 553 | |||
| 554 | if (!isset($_REQUEST['colcomment'][$i])) { |
||
| 555 | $_REQUEST['colcomment'][$i] = ''; |
||
| 556 | } |
||
| 557 | |||
| 558 | echo "\t<tr>\n\t\t<td>", $i + 1, ". </td>\n"; |
||
| 559 | echo "\t\t<td><input name=\"field[{$i}]\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 560 | htmlspecialchars($_REQUEST['field'][$i]), "\" /></td>\n"; |
||
| 561 | echo "\t\t<td>\n\t\t\t<select name=\"type[{$i}]\" class=\"select2\" id=\"types{$i}\" onchange=\"checkLengths(this.options[this.selectedIndex].value,{$i});\">\n"; |
||
| 562 | // Output any "magic" types |
||
| 563 | foreach ($data->extraTypes as $v) { |
||
| 564 | $types_for_js[strtolower($v)] = 1; |
||
| 565 | echo "\t\t\t\t<option value=\"", htmlspecialchars($v), '"', |
||
| 566 | (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] == $v) ? ' selected="selected"' : '', '>', |
||
| 567 | $this->misc->printVal($v), "</option>\n"; |
||
| 568 | } |
||
| 569 | $types->moveFirst(); |
||
| 570 | while (!$types->EOF) { |
||
| 571 | $typname = $types->fields['typname']; |
||
| 572 | $types_for_js[$typname] = 1; |
||
| 573 | echo "\t\t\t\t<option value=\"", htmlspecialchars($typname), '"', |
||
| 574 | (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] == $typname) ? ' selected="selected"' : '', '>', |
||
| 575 | $this->misc->printVal($typname), "</option>\n"; |
||
| 576 | $types->moveNext(); |
||
| 577 | } |
||
| 578 | echo "\t\t\t</select>\n\t\t\n"; |
||
| 579 | if (0 == $i) { |
||
| 580 | // only define js types array once |
||
| 581 | $predefined_size_types = array_intersect($data->predefined_size_types, array_keys($types_for_js)); |
||
| 582 | $escaped_predef_types = []; // the JS escaped array elements |
||
| 583 | foreach ($predefined_size_types as $value) { |
||
| 584 | $escaped_predef_types[] = "'{$value}'"; |
||
| 585 | } |
||
| 586 | echo '<script type="text/javascript">predefined_lengths = new Array(' . implode(',', $escaped_predef_types) . ");</script>\n\t</td>"; |
||
| 587 | } |
||
| 588 | |||
| 589 | // Output array type selector |
||
| 590 | echo "\t\t<td>\n\t\t\t<select name=\"array[{$i}]\">\n"; |
||
| 591 | echo "\t\t\t\t<option value=\"\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '') ? ' selected="selected"' : '', "></option>\n"; |
||
| 592 | echo "\t\t\t\t<option value=\"[]\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '[]') ? ' selected="selected"' : '', ">[ ]</option>\n"; |
||
| 593 | echo "\t\t\t</select>\n\t\t</td>\n"; |
||
| 594 | |||
| 595 | echo "\t\t<td><input name=\"length[{$i}]\" id=\"lengths{$i}\" size=\"10\" value=\"", |
||
| 596 | htmlspecialchars($_REQUEST['length'][$i]), "\" /></td>\n"; |
||
| 597 | echo "\t\t<td><input type=\"checkbox\" name=\"notnull[{$i}]\"", (isset($_REQUEST['notnull'][$i])) ? ' checked="checked"' : '', " /></td>\n"; |
||
| 598 | echo "\t\t<td style=\"text-align: center\"><input type=\"checkbox\" name=\"uniquekey[{$i}]\"" |
||
| 599 | . (isset($_REQUEST['uniquekey'][$i]) ? ' checked="checked"' : '') . " /></td>\n"; |
||
| 600 | echo "\t\t<td style=\"text-align: center\"><input type=\"checkbox\" name=\"primarykey[{$i}]\" " |
||
| 601 | . (isset($_REQUEST['primarykey'][$i]) ? ' checked="checked"' : '') |
||
| 602 | . " /></td>\n"; |
||
| 603 | echo "\t\t<td><input name=\"default[{$i}]\" size=\"20\" value=\"", |
||
| 604 | htmlspecialchars($_REQUEST['default'][$i]), "\" /></td>\n"; |
||
| 605 | echo "\t\t<td><input name=\"colcomment[{$i}]\" size=\"40\" value=\"", |
||
| 606 | htmlspecialchars($_REQUEST['colcomment'][$i]), "\" /> |
||
| 607 | <script type=\"text/javascript\">checkLengths(document.getElementById('types{$i}').value,{$i});</script> |
||
| 608 | </td>\n\t</tr>\n"; |
||
| 609 | } |
||
| 610 | echo "</table>\n"; |
||
| 611 | echo "<p><input type=\"hidden\" name=\"action\" value=\"create\" />\n"; |
||
| 612 | echo "<input type=\"hidden\" name=\"stage\" value=\"3\" />\n"; |
||
| 613 | echo $this->misc->form; |
||
| 614 | echo '<input type="hidden" name="name" value="', htmlspecialchars($_REQUEST['name']), "\" />\n"; |
||
| 615 | echo '<input type="hidden" name="fields" value="', htmlspecialchars($_REQUEST['fields']), "\" />\n"; |
||
| 616 | if (isset($_REQUEST['withoutoids'])) { |
||
| 617 | echo "<input type=\"hidden\" name=\"withoutoids\" value=\"true\" />\n"; |
||
| 618 | } |
||
| 619 | echo '<input type="hidden" name="tblcomment" value="', htmlspecialchars($_REQUEST['tblcomment']), "\" />\n"; |
||
| 620 | if (isset($_REQUEST['spcname'])) { |
||
| 621 | echo '<input type="hidden" name="spcname" value="', htmlspecialchars($_REQUEST['spcname']), "\" />\n"; |
||
| 622 | } |
||
| 623 | echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n"; |
||
| 624 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
| 625 | echo "</form>\n"; |
||
| 626 | |||
| 627 | break; |
||
| 628 | case 3: |
||
| 629 | |||
| 630 | $this->coalesceArr($_REQUEST, 'notnull', []); |
||
| 631 | |||
| 632 | $this->coalesceArr($_REQUEST, 'uniquekey', []); |
||
| 633 | |||
| 634 | $this->coalesceArr($_REQUEST, 'primarykey', []); |
||
| 635 | |||
| 636 | $this->coalesceArr($_REQUEST, 'length', []); |
||
| 637 | |||
| 638 | // Default tablespace to null if it isn't set |
||
| 639 | $this->coalesceArr($_REQUEST, 'spcname', null); |
||
| 640 | |||
| 641 | // Check inputs |
||
| 642 | $fields = trim($_REQUEST['fields']); |
||
| 643 | if ('' == trim($_REQUEST['name'])) { |
||
| 644 | $_REQUEST['stage'] = 1; |
||
| 645 | $this->doCreate($this->lang['strtableneedsname']); |
||
| 646 | |||
| 647 | return; |
||
| 648 | } |
||
| 649 | if ('' == $fields || !is_numeric($fields) || $fields != (int) $fields || $fields <= 0) { |
||
| 650 | $_REQUEST['stage'] = 1; |
||
| 651 | $this->doCreate($this->lang['strtableneedscols']); |
||
| 652 | |||
| 653 | return; |
||
| 654 | } |
||
| 655 | |||
| 656 | $status = $data->createTable( |
||
| 657 | $_REQUEST['name'], |
||
| 658 | $_REQUEST['fields'], |
||
| 659 | $_REQUEST['field'], |
||
| 660 | $_REQUEST['type'], |
||
| 661 | $_REQUEST['array'], |
||
| 662 | $_REQUEST['length'], |
||
| 663 | $_REQUEST['notnull'], |
||
| 664 | $_REQUEST['default'], |
||
| 665 | isset($_REQUEST['withoutoids']), |
||
| 666 | $_REQUEST['colcomment'], |
||
| 667 | $_REQUEST['tblcomment'], |
||
| 668 | $_REQUEST['spcname'], |
||
| 669 | $_REQUEST['uniquekey'], |
||
| 670 | $_REQUEST['primarykey'] |
||
| 671 | ); |
||
| 672 | |||
| 673 | if (0 == $status) { |
||
| 674 | $this->misc->setReloadBrowser(true); |
||
| 675 | |||
| 676 | return $this->doDefault($this->lang['strtablecreated']); |
||
| 677 | } |
||
| 678 | if ($status == -1) { |
||
| 679 | $_REQUEST['stage'] = 2; |
||
| 680 | $this->doCreate($this->lang['strtableneedsfield']); |
||
| 681 | |||
| 682 | return; |
||
| 683 | } |
||
| 684 | $_REQUEST['stage'] = 2; |
||
| 685 | $this->doCreate($this->lang['strtablecreatedbad']); |
||
| 686 | |||
| 687 | return; |
||
| 688 | break; |
||
| 689 | default: |
||
| 690 | echo "<p>{$this->lang['strinvalidparam']}</p>\n"; |
||
| 691 | } |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Dsiplay a screen where user can create a table from an existing one. |
||
| 696 | * We don't have to check if pg supports schema cause create table like |
||
| 697 | * is available under pg 7.4+ which has schema. |
||
| 698 | * |
||
| 699 | * @param mixed $confirm |
||
| 700 | * @param mixed $msg |
||
| 701 | */ |
||
| 702 | public function doCreateLike($confirm, $msg = '') |
||
| 703 | { |
||
| 704 | $data = $this->misc->getDatabaseAccessor(); |
||
| 705 | |||
| 706 | if (!$confirm) { |
||
| 707 | $this->coalesceArr($_REQUEST, 'name', ''); |
||
| 708 | |||
| 709 | $this->coalesceArr($_REQUEST, 'like', ''); |
||
| 710 | |||
| 711 | $this->coalesceArr($_REQUEST, 'tablespace', ''); |
||
| 712 | |||
| 713 | $this->printTrail('schema'); |
||
| 714 | $this->printTitle($this->lang['strcreatetable'], 'pg.table.create'); |
||
| 715 | $this->printMsg($msg); |
||
| 716 | |||
| 717 | $tbltmp = $data->getTables(true); |
||
| 718 | $tbltmp = $tbltmp->getArray(); |
||
| 719 | |||
| 720 | $tables = []; |
||
| 721 | $tblsel = ''; |
||
| 722 | foreach ($tbltmp as $a) { |
||
| 723 | $data->fieldClean($a['nspname']); |
||
| 724 | $data->fieldClean($a['relname']); |
||
| 725 | $tables["\"{$a['nspname']}\".\"{$a['relname']}\""] = serialize(['schema' => $a['nspname'], 'table' => $a['relname']]); |
||
| 726 | if ($_REQUEST['like'] == $tables["\"{$a['nspname']}\".\"{$a['relname']}\""]) { |
||
| 727 | $tblsel = htmlspecialchars($tables["\"{$a['nspname']}\".\"{$a['relname']}\""]); |
||
| 728 | } |
||
| 729 | } |
||
| 730 | |||
| 731 | unset($tbltmp); |
||
| 732 | |||
| 733 | echo '<form action="' . \SUBFOLDER . "/src/views/tables\" method=\"post\">\n"; |
||
| 734 | echo "<table>\n\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>\n"; |
||
| 735 | echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n"; |
||
| 736 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strcreatetablelikeparent']}</th>\n"; |
||
| 737 | echo "\t\t<td class=\"data\">"; |
||
| 738 | echo \PHPPgAdmin\XHtml\HTMLController::printCombo($tables, 'like', true, $tblsel, false); |
||
| 739 | echo "</td>\n\t</tr>\n"; |
||
| 740 | if ($data->hasTablespaces()) { |
||
| 741 | $tblsp_ = $data->getTablespaces(); |
||
| 742 | if ($tblsp_->recordCount() > 0) { |
||
| 743 | $tblsp_ = $tblsp_->getArray(); |
||
| 744 | $tblsp = []; |
||
| 745 | foreach ($tblsp_ as $a) { |
||
| 746 | $tblsp[$a['spcname']] = $a['spcname']; |
||
| 747 | } |
||
| 748 | |||
| 749 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strtablespace']}</th>\n"; |
||
| 750 | echo "\t\t<td class=\"data\">"; |
||
| 751 | echo \PHPPgAdmin\XHtml\HTMLController::printCombo($tblsp, 'tablespace', true, $_REQUEST['tablespace'], false); |
||
| 752 | echo "</td>\n\t</tr>\n"; |
||
| 753 | } |
||
| 754 | } |
||
| 755 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['stroptions']}</th>\n\t\t<td class=\"data\">"; |
||
| 756 | echo '<label for="withdefaults"><input type="checkbox" id="withdefaults" name="withdefaults"', |
||
| 757 | isset($_REQUEST['withdefaults']) ? ' checked="checked"' : '', |
||
| 758 | "/>{$this->lang['strcreatelikewithdefaults']}</label>"; |
||
| 759 | if ($data->hasCreateTableLikeWithConstraints()) { |
||
| 760 | echo '<br /><label for="withconstraints"><input type="checkbox" id="withconstraints" name="withconstraints"', |
||
| 761 | isset($_REQUEST['withconstraints']) ? ' checked="checked"' : '', |
||
| 762 | "/>{$this->lang['strcreatelikewithconstraints']}</label>"; |
||
| 763 | } |
||
| 764 | if ($data->hasCreateTableLikeWithIndexes()) { |
||
| 765 | echo '<br /><label for="withindexes"><input type="checkbox" id="withindexes" name="withindexes"', |
||
| 766 | isset($_REQUEST['withindexes']) ? ' checked="checked"' : '', |
||
| 767 | "/>{$this->lang['strcreatelikewithindexes']}</label>"; |
||
| 768 | } |
||
| 769 | echo "</td>\n\t</tr>\n"; |
||
| 770 | echo '</table>'; |
||
| 771 | |||
| 772 | echo "<input type=\"hidden\" name=\"action\" value=\"confcreatelike\" />\n"; |
||
| 773 | echo $this->misc->form; |
||
| 774 | echo "<p><input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n"; |
||
| 775 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
| 776 | echo "</form>\n"; |
||
| 777 | } else { |
||
| 778 | if ('' == trim($_REQUEST['name'])) { |
||
| 779 | $this->doCreateLike(false, $this->lang['strtableneedsname']); |
||
| 780 | |||
| 781 | return; |
||
| 782 | } |
||
| 783 | if ('' == trim($_REQUEST['like'])) { |
||
| 784 | $this->doCreateLike(false, $this->lang['strtablelikeneedslike']); |
||
| 785 | |||
| 786 | return; |
||
| 787 | } |
||
| 788 | |||
| 789 | $this->coalesceArr($_REQUEST, 'tablespace', ''); |
||
| 790 | |||
| 791 | $status = $data->createTableLike( |
||
| 792 | $_REQUEST['name'], |
||
| 793 | unserialize($_REQUEST['like']), |
||
| 794 | isset($_REQUEST['withdefaults']), |
||
| 795 | isset($_REQUEST['withconstraints']), |
||
| 796 | isset($_REQUEST['withindexes']), |
||
| 797 | $_REQUEST['tablespace'] |
||
| 798 | ); |
||
| 799 | |||
| 800 | if (0 == $status) { |
||
| 801 | $this->misc->setReloadBrowser(true); |
||
| 802 | |||
| 803 | return $this->doDefault($this->lang['strtablecreated']); |
||
| 804 | } |
||
| 805 | $this->doCreateLike(false, $this->lang['strtablecreatedbad']); |
||
| 806 | |||
| 807 | return; |
||
| 808 | } |
||
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Ask for select parameters and perform select. |
||
| 813 | * |
||
| 814 | * @param mixed $confirm |
||
| 815 | * @param mixed $msg |
||
| 816 | */ |
||
| 817 | public function doSelectRows($confirm, $msg = '') |
||
| 818 | { |
||
| 819 | $data = $this->misc->getDatabaseAccessor(); |
||
| 820 | |||
| 821 | if ($confirm) { |
||
| 822 | $this->printTrail('table'); |
||
| 823 | $this->printTabs('table', 'select'); |
||
| 824 | $this->printMsg($msg); |
||
| 825 | |||
| 826 | $attrs = $data->getTableAttributes($_REQUEST['table']); |
||
| 827 | |||
| 828 | echo '<form action="' . \SUBFOLDER . "/src/views/tables\" method=\"post\" id=\"selectform\">\n"; |
||
| 829 | if ($attrs->recordCount() > 0) { |
||
| 830 | // JavaScript for select all feature |
||
| 831 | echo "<script type=\"text/javascript\">\n"; |
||
| 832 | echo "//<![CDATA[\n"; |
||
| 833 | echo " function selectAll() {\n"; |
||
| 834 | echo " for (var i=0; i<document.getElementById('selectform').elements.length; i++) {\n"; |
||
| 835 | echo " var e = document.getElementById('selectform').elements[i];\n"; |
||
| 836 | echo " if (e.name.indexOf('show') == 0) e.checked = document.getElementById('selectform').selectall.checked;\n"; |
||
| 837 | echo " }\n"; |
||
| 838 | echo " }\n"; |
||
| 839 | echo "//]]>\n"; |
||
| 840 | echo "</script>\n"; |
||
| 841 | |||
| 842 | echo "<table>\n"; |
||
| 843 | |||
| 844 | // Output table header |
||
| 845 | echo "<tr><th class=\"data\">{$this->lang['strshow']}</th><th class=\"data\">{$this->lang['strcolumn']}</th>"; |
||
| 846 | echo "<th class=\"data\">{$this->lang['strtype']}</th><th class=\"data\">{$this->lang['stroperator']}</th>"; |
||
| 847 | echo "<th class=\"data\">{$this->lang['strvalue']}</th></tr>"; |
||
| 848 | |||
| 849 | $i = 0; |
||
| 850 | while (!$attrs->EOF) { |
||
| 851 | $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']); |
||
| 852 | // Set up default value if there isn't one already |
||
| 853 | if (!isset($_REQUEST['values'][$attrs->fields['attname']])) { |
||
| 854 | $_REQUEST['values'][$attrs->fields['attname']] = null; |
||
| 855 | } |
||
| 856 | |||
| 857 | if (!isset($_REQUEST['ops'][$attrs->fields['attname']])) { |
||
| 858 | $_REQUEST['ops'][$attrs->fields['attname']] = null; |
||
| 859 | } |
||
| 860 | |||
| 861 | // Continue drawing row |
||
| 862 | $id = (0 == ($i % 2) ? '1' : '2'); |
||
| 863 | echo "<tr class=\"data{$id}\">\n"; |
||
| 864 | echo '<td style="white-space:nowrap;">'; |
||
| 865 | echo '<input type="checkbox" name="show[', htmlspecialchars($attrs->fields['attname']), ']"', |
||
| 866 | isset($_REQUEST['show'][$attrs->fields['attname']]) ? ' checked="checked"' : '', ' /></td>'; |
||
| 867 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>'; |
||
| 868 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])), '</td>'; |
||
| 869 | echo '<td style="white-space:nowrap;">'; |
||
| 870 | echo "<select name=\"ops[{$attrs->fields['attname']}]\">\n"; |
||
| 871 | foreach (array_keys($data->selectOps) as $v) { |
||
| 872 | echo '<option value="', htmlspecialchars($v), '"', ($_REQUEST['ops'][$attrs->fields['attname']] == $v) ? ' selected="selected"' : '', |
||
| 873 | '>', htmlspecialchars($v), "</option>\n"; |
||
| 874 | } |
||
| 875 | echo "</select>\n</td>\n"; |
||
| 876 | echo '<td style="white-space:nowrap;">', $data->printField( |
||
| 877 | "values[{$attrs->fields['attname']}]", |
||
| 878 | $_REQUEST['values'][$attrs->fields['attname']], |
||
| 879 | $attrs->fields['type'] |
||
| 880 | ), '</td>'; |
||
| 881 | echo "</tr>\n"; |
||
| 882 | ++$i; |
||
| 883 | $attrs->moveNext(); |
||
| 884 | } |
||
| 885 | // Select all checkbox |
||
| 886 | echo "<tr><td colspan=\"5\"><input type=\"checkbox\" id=\"selectall\" name=\"selectall\" accesskey=\"a\" onclick=\"javascript:selectAll()\" /><label for=\"selectall\">{$this->lang['strselectallfields']}</label></td>"; |
||
| 887 | echo "</tr></table>\n"; |
||
| 888 | } else { |
||
| 889 | echo "<p>{$this->lang['strinvalidparam']}</p>\n"; |
||
| 890 | } |
||
| 891 | |||
| 892 | echo "<p><input type=\"hidden\" name=\"action\" value=\"selectrows\" />\n"; |
||
| 893 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 894 | echo "<input type=\"hidden\" name=\"subject\" value=\"table\" />\n"; |
||
| 895 | echo $this->misc->form; |
||
| 896 | echo "<input type=\"submit\" name=\"select\" accesskey=\"r\" value=\"{$this->lang['strselect']}\" />\n"; |
||
| 897 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
| 898 | echo "</form>\n"; |
||
| 899 | |||
| 900 | return; |
||
| 901 | } |
||
| 902 | $this->coalesceArr($_POST, 'show', []); |
||
| 903 | |||
| 904 | $this->coalesceArr($_POST, 'values', []); |
||
| 905 | |||
| 906 | $this->coalesceArr($_POST, 'nulls', []); |
||
| 907 | |||
| 908 | // Verify that they haven't supplied a value for unary operators |
||
| 909 | foreach ($_POST['ops'] as $k => $v) { |
||
| 910 | if ('p' == $data->selectOps[$v] && $_POST['values'][$k] != '') { |
||
| 911 | $this->doSelectRows(true, $this->lang['strselectunary']); |
||
| 912 | |||
| 913 | return; |
||
| 914 | } |
||
| 915 | } |
||
| 916 | |||
| 917 | if (0 == sizeof($_POST['show'])) { |
||
| 918 | $this->doSelectRows(true, $this->lang['strselectneedscol']); |
||
| 919 | } else { |
||
| 920 | // Generate query SQL |
||
| 921 | $query = $data->getSelectSQL( |
||
| 922 | $_REQUEST['table'], |
||
| 923 | array_keys($_POST['show']), |
||
| 924 | $_POST['values'], |
||
| 925 | $_POST['ops'] |
||
| 926 | ); |
||
| 927 | $_REQUEST['query'] = $query; |
||
| 928 | $_REQUEST['return'] = 'selectrows'; |
||
| 929 | |||
| 930 | $this->setNoOutput(true); |
||
| 931 | |||
| 932 | $display_controller = new DisplayController($this->getContainer()); |
||
| 933 | |||
| 934 | return $display_controller->render(); |
||
| 935 | } |
||
| 936 | } |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Ask for insert parameters and then actually insert row. |
||
| 940 | * |
||
| 941 | * @param mixed $confirm |
||
| 942 | * @param mixed $msg |
||
| 943 | */ |
||
| 944 | public function doInsertRow($confirm, $msg = '') |
||
| 945 | { |
||
| 946 | $data = $this->misc->getDatabaseAccessor(); |
||
| 947 | |||
| 948 | if ($confirm) { |
||
| 949 | $this->printTrail('table'); |
||
| 950 | $this->printTabs('table', 'insert'); |
||
| 951 | |||
| 952 | $this->printMsg($msg); |
||
| 953 | |||
| 954 | $attrs = $data->getTableAttributes($_REQUEST['table']); |
||
| 955 | |||
| 956 | if (('disable' != $this->conf['autocomplete'])) { |
||
| 957 | $fksprops = $this->misc->getAutocompleteFKProperties($_REQUEST['table']); |
||
| 958 | if (false !== $fksprops) { |
||
| 959 | echo $fksprops['code']; |
||
| 960 | } |
||
| 961 | } else { |
||
| 962 | $fksprops = false; |
||
| 963 | } |
||
| 964 | |||
| 965 | echo '<form action="' . \SUBFOLDER . "/src/views/tables\" method=\"post\" id=\"ac_form\">\n"; |
||
| 966 | if ($attrs->recordCount() > 0) { |
||
| 967 | echo "<table>\n"; |
||
| 968 | |||
| 969 | // Output table header |
||
| 970 | echo "<tr><th class=\"data\">{$this->lang['strcolumn']}</th><th class=\"data\">{$this->lang['strtype']}</th>"; |
||
| 971 | echo "<th class=\"data\">{$this->lang['strformat']}</th>"; |
||
| 972 | echo "<th class=\"data\">{$this->lang['strnull']}</th><th class=\"data\">{$this->lang['strvalue']}</th></tr>"; |
||
| 973 | |||
| 974 | $i = 0; |
||
| 975 | $fields = []; |
||
| 976 | while (!$attrs->EOF) { |
||
| 977 | $fields[$attrs->fields['attnum']] = $attrs->fields['attname']; |
||
| 978 | $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']); |
||
| 979 | // Set up default value if there isn't one already |
||
| 980 | if (!isset($_REQUEST['values'][$attrs->fields['attnum']])) { |
||
| 981 | $_REQUEST['values'][$attrs->fields['attnum']] = $attrs->fields['adsrc']; |
||
| 982 | } |
||
| 983 | |||
| 984 | // Default format to 'VALUE' if there is no default, |
||
| 985 | // otherwise default to 'EXPRESSION' |
||
| 986 | if (!isset($_REQUEST['format'][$attrs->fields['attnum']])) { |
||
| 987 | $_REQUEST['format'][$attrs->fields['attnum']] = (null === $attrs->fields['adsrc']) ? 'VALUE' : 'EXPRESSION'; |
||
| 988 | } |
||
| 989 | |||
| 990 | // Continue drawing row |
||
| 991 | $id = (0 == ($i % 2) ? '1' : '2'); |
||
| 992 | echo "<tr class=\"data{$id}\">\n"; |
||
| 993 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>'; |
||
| 994 | echo "<td style=\"white-space:nowrap;\">\n"; |
||
| 995 | echo $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])); |
||
| 996 | echo "<input type=\"hidden\" name=\"types[{$attrs->fields['attnum']}]\" value=\"", |
||
| 997 | htmlspecialchars($attrs->fields['type']), '" /></td>'; |
||
| 998 | echo "<td style=\"white-space:nowrap;\">\n"; |
||
| 999 | echo "<select name=\"format[{$attrs->fields['attnum']}]\">\n"; |
||
| 1000 | echo '<option value="VALUE"', ($_REQUEST['format'][$attrs->fields['attnum']] == 'VALUE') ? ' selected="selected"' : '', ">{$this->lang['strvalue']}</option>\n"; |
||
| 1001 | echo '<option value="EXPRESSION"', ($_REQUEST['format'][$attrs->fields['attnum']] == 'EXPRESSION') ? ' selected="selected"' : '', ">{$this->lang['strexpression']}</option>\n"; |
||
| 1002 | echo "</select>\n</td>\n"; |
||
| 1003 | echo '<td style="white-space:nowrap;">'; |
||
| 1004 | // Output null box if the column allows nulls (doesn't look at CHECKs or ASSERTIONS) |
||
| 1005 | if (!$attrs->fields['attnotnull']) { |
||
| 1006 | echo "<label><span><input type=\"checkbox\" name=\"nulls[{$attrs->fields['attnum']}]\"", |
||
| 1007 | isset($_REQUEST['nulls'][$attrs->fields['attnum']]) ? ' checked="checked"' : '', ' /></span></label></td>'; |
||
| 1008 | } else { |
||
| 1009 | echo ' </td>'; |
||
| 1010 | } |
||
| 1011 | echo "<td id=\"row_att_{$attrs->fields['attnum']}\" style=\"white-space:nowrap;\">"; |
||
| 1012 | if ((false !== $fksprops) && isset($fksprops['byfield'][$attrs->fields['attnum']])) { |
||
| 1013 | echo $data->printField( |
||
| 1014 | "values[{$attrs->fields['attnum']}]", |
||
| 1015 | $_REQUEST['values'][$attrs->fields['attnum']], |
||
| 1016 | 'fktype' /*force FK*/, |
||
| 1017 | [ |
||
| 1018 | 'id' => "attr_{$attrs->fields['attnum']}", |
||
| 1019 | 'autocomplete' => 'off', |
||
| 1020 | ] |
||
| 1021 | ); |
||
| 1022 | } else { |
||
| 1023 | echo $data->printField("values[{$attrs->fields['attnum']}]", $_REQUEST['values'][$attrs->fields['attnum']], $attrs->fields['type']); |
||
| 1024 | } |
||
| 1025 | echo "</td>\n"; |
||
| 1026 | echo "</tr>\n"; |
||
| 1027 | ++$i; |
||
| 1028 | $attrs->moveNext(); |
||
| 1029 | } |
||
| 1030 | echo "</table>\n"; |
||
| 1031 | |||
| 1032 | if (!isset($_SESSION['counter'])) { |
||
| 1033 | $_SESSION['counter'] = 0; |
||
| 1034 | } |
||
| 1035 | |||
| 1036 | echo "<input type=\"hidden\" name=\"action\" value=\"insertrow\" />\n"; |
||
| 1037 | echo '<input type="hidden" name="fields" value="', htmlentities(serialize($fields), ENT_QUOTES, 'UTF-8'), "\" />\n"; |
||
| 1038 | echo '<input type="hidden" name="protection_counter" value="' . $_SESSION['counter'] . "\" />\n"; |
||
| 1039 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 1040 | echo "<p><input type=\"submit\" name=\"insert\" value=\"{$this->lang['strinsert']}\" />\n"; |
||
| 1041 | echo "<input type=\"submit\" name=\"insertandrepeat\" accesskey=\"r\" value=\"{$this->lang['strinsertandrepeat']}\" />\n"; |
||
| 1042 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n"; |
||
| 1043 | |||
| 1044 | if (false !== $fksprops) { |
||
| 1045 | if ('default off' != $this->conf['autocomplete']) { |
||
| 1046 | echo "<input type=\"checkbox\" id=\"no_ac\" value=\"1\" checked=\"checked\" /><label for=\"no_ac\">{$this->lang['strac']}</label>\n"; |
||
| 1047 | } else { |
||
| 1048 | echo "<input type=\"checkbox\" id=\"no_ac\" value=\"0\" /><label for=\"no_ac\">{$this->lang['strac']}</label>\n"; |
||
| 1049 | } |
||
| 1050 | } |
||
| 1051 | echo "</p>\n"; |
||
| 1052 | } else { |
||
| 1053 | echo "<p>{$this->lang['strnofieldsforinsert']}</p>\n"; |
||
| 1054 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n"; |
||
| 1055 | } |
||
| 1056 | echo $this->misc->form; |
||
| 1057 | echo "</form>\n"; |
||
| 1058 | } else { |
||
| 1059 | $this->coalesceArr($_POST, 'values', []); |
||
| 1060 | |||
| 1061 | $this->coalesceArr($_POST, 'nulls', []); |
||
| 1062 | |||
| 1063 | $_POST['fields'] = unserialize(htmlspecialchars_decode($_POST['fields'], ENT_QUOTES)); |
||
| 1064 | |||
| 1065 | if ($_SESSION['counter']++ == $_POST['protection_counter']) { |
||
| 1066 | $status = $data->insertRow($_POST['table'], $_POST['fields'], $_POST['values'], $_POST['nulls'], $_POST['format'], $_POST['types']); |
||
| 1067 | if (0 == $status) { |
||
| 1068 | if (isset($_POST['insert'])) { |
||
| 1069 | return $this->doDefault($this->lang['strrowinserted']); |
||
| 1070 | } |
||
| 1071 | $_REQUEST['values'] = []; |
||
| 1072 | $_REQUEST['nulls'] = []; |
||
| 1073 | $this->doInsertRow(true, $this->lang['strrowinserted']); |
||
| 1074 | } else { |
||
| 1075 | $this->doInsertRow(true, $this->lang['strrowinsertedbad']); |
||
| 1076 | } |
||
| 1077 | } else { |
||
| 1078 | $this->doInsertRow(true, $this->lang['strrowduplicate']); |
||
| 1079 | } |
||
| 1080 | } |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Show confirmation of empty and perform actual empty. |
||
| 1085 | * |
||
| 1086 | * @param mixed $confirm |
||
| 1087 | */ |
||
| 1088 | public function doEmpty($confirm) |
||
| 1089 | { |
||
| 1090 | $data = $this->misc->getDatabaseAccessor(); |
||
| 1091 | |||
| 1092 | if (empty($_REQUEST['table']) && empty($_REQUEST['ma'])) { |
||
| 1093 | return $this->doDefault($this->lang['strspecifytabletoempty']); |
||
| 1094 | } |
||
| 1095 | |||
| 1096 | if ($confirm) { |
||
| 1097 | if (isset($_REQUEST['ma'])) { |
||
| 1098 | $this->printTrail('schema'); |
||
| 1099 | $this->printTitle($this->lang['strempty'], 'pg.table.empty'); |
||
| 1100 | |||
| 1101 | echo '<form action="' . \SUBFOLDER . "/src/views/tables\" method=\"post\">\n"; |
||
| 1102 | foreach ($_REQUEST['ma'] as $v) { |
||
| 1103 | $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES)); |
||
| 1104 | echo '<p>' . sprintf($this->lang['strconfemptytable'], $this->misc->printVal($a['table'])); |
||
| 1105 | |||
| 1106 | echo "</p>\n"; |
||
| 1107 | printf('<input type="hidden" name="table[]" value="%s" />', htmlspecialchars($a['table'])); |
||
| 1108 | } // END mutli empty |
||
| 1109 | } else { |
||
| 1110 | $this->printTrail('table'); |
||
| 1111 | $this->printTitle($this->lang['strempty'], 'pg.table.empty'); |
||
| 1112 | |||
| 1113 | echo '<p>', sprintf($this->lang['strconfemptytable'], $this->misc->printVal($_REQUEST['table'])), "</p>\n"; |
||
| 1114 | |||
| 1115 | echo '<form action="' . \SUBFOLDER . "/src/views/tables\" method=\"post\">\n"; |
||
| 1116 | |||
| 1117 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 1118 | // END not mutli empty |
||
| 1119 | } |
||
| 1120 | echo "<input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label>"; |
||
| 1121 | echo "<input type=\"hidden\" name=\"action\" value=\"empty\" />\n"; |
||
| 1122 | echo $this->misc->form; |
||
| 1123 | echo "<input type=\"submit\" name=\"empty\" value=\"{$this->lang['strempty']}\" /> <input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n"; |
||
| 1124 | echo "</form>\n"; // END if confirm |
||
| 1125 | } else { |
||
| 1126 | // Do Empty |
||
| 1127 | $msg = ''; |
||
| 1128 | if (is_array($_REQUEST['table'])) { |
||
| 1129 | foreach ($_REQUEST['table'] as $t) { |
||
| 1130 | list($status, $sql) = $data->emptyTable($t, isset($_POST['cascade'])); |
||
| 1131 | if (0 == $status) { |
||
| 1132 | $msg .= sprintf('%s<br />', $sql); |
||
| 1133 | $msg .= sprintf('%s: %s<br />', htmlentities($t, ENT_QUOTES, 'UTF-8'), $this->lang['strtableemptied']); |
||
| 1134 | } else { |
||
| 1135 | $this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($t, ENT_QUOTES, 'UTF-8'), $this->lang['strtableemptiedbad'])); |
||
| 1136 | |||
| 1137 | return; |
||
| 1138 | } |
||
| 1139 | } |
||
| 1140 | $this->doDefault($msg); // END mutli empty |
||
| 1141 | } else { |
||
| 1142 | list($status, $sql) = $data->emptyTable($_POST['table'], isset($_POST['cascade'])); |
||
| 1143 | if (0 == $status) { |
||
| 1144 | $msg .= sprintf('%s<br />', $sql); |
||
| 1145 | $msg .= sprintf('%s: %s<br />', htmlentities($_POST['table'], ENT_QUOTES, 'UTF-8'), $this->lang['strtableemptied']); |
||
| 1146 | |||
| 1147 | return $this->doDefault($msg); |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | return $this->doDefault($sql . '<br>' . $this->lang['strtableemptiedbad']); |
||
| 1151 | // END not mutli empty |
||
| 1152 | } |
||
| 1153 | // END do Empty |
||
| 1154 | } |
||
| 1155 | } |
||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Show confirmation of drop and perform actual drop. |
||
| 1159 | * |
||
| 1160 | * @param mixed $confirm |
||
| 1161 | */ |
||
| 1162 | public function doDrop($confirm) |
||
| 1233 | // END DROP |
||
| 1234 | } |
||
| 1235 | } |
||
| 1236 | |||
| 1237 | // END Function |
||
| 1238 | } |
||
| 1239 |