| Total Complexity | 58 |
| Total Lines | 358 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like HTMLTableController 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 HTMLTableController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class HTMLTableController extends HTMLController |
||
| 15 | { |
||
| 16 | public $controller_name = 'HTMLTableController'; |
||
| 17 | protected $ma = []; |
||
| 18 | protected $plugin_functions_parameters = []; |
||
| 19 | protected $has_ma = false; |
||
| 20 | |||
| 21 | protected $tabledata; |
||
| 22 | protected $columns; |
||
| 23 | protected $actions; |
||
| 24 | protected $place; |
||
| 25 | protected $nodata; |
||
| 26 | protected $pre_fn; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Display a table of data. |
||
| 30 | * |
||
| 31 | * @param \PHPPgAdmin\ADORecordSet|\PHPPgAdmin\ArrayRecordSet $tabledata a set of data to be formatted, as returned by $data->getDatabases() etc |
||
| 32 | * @param array $columns An associative array of columns to be displayed: |
||
| 33 | * $columns = array( |
||
| 34 | * column_id => array( |
||
| 35 | * 'title' => Column heading, |
||
| 36 | * 'class' => The class to apply on the column cells, |
||
| 37 | * 'field' => Field name for $tabledata->fields[...], |
||
| 38 | * 'help' => Help page for this column, |
||
| 39 | * ), ... |
||
| 40 | * ); |
||
| 41 | * @param array $actions Actions that can be performed on each object: |
||
| 42 | * $actions = array( |
||
| 43 | * * multi action support |
||
| 44 | * * parameters are serialized for each entries and given in $_REQUEST['ma'] |
||
| 45 | * 'multiactions' => array( |
||
| 46 | * 'keycols' => Associative array of (URL variable => field name), // fields included in the form |
||
| 47 | * 'url' => URL submission, |
||
| 48 | * 'default' => Default selected action in the form. If null, an empty action is added & selected |
||
| 49 | * ), |
||
| 50 | * * actions * |
||
| 51 | * action_id => array( |
||
| 52 | * 'title' => Action heading, |
||
| 53 | * 'url' => Static part of URL. Often we rely |
||
| 54 | * relative urls, usually the page itself (not '' !), or just a query string, |
||
| 55 | * 'vars' => Associative array of (URL variable => field name), |
||
| 56 | * 'multiaction' => Name of the action to execute. |
||
| 57 | * Add this action to the multi action form |
||
| 58 | * ), ... |
||
| 59 | * ); |
||
| 60 | * @param string $place Place where the $actions are displayed. Like 'display-browse', where 'display' |
||
| 61 | * is the entrypoint (/src/views/display) and 'browse' is the action used inside its controller (in this case, doBrowse). |
||
| 62 | * @param string $nodata (optional) Message to display if data set is empty |
||
| 63 | * @param callable $pre_fn (optional) callback closure for each row. It will be passed two params: $rowdata and $actions, |
||
| 64 | * it may be used to derive new fields or modify actions. |
||
| 65 | * It can return an array of actions specific to the row, or if nothing is returned then the standard actions are used. |
||
| 66 | * (see TblpropertiesController and ConstraintsController for examples) |
||
| 67 | * The function must not must not store urls because they are relative and won't work out of context. |
||
| 68 | * |
||
| 69 | * @return string the html of the table |
||
| 70 | */ |
||
| 71 | public function initialize(&$tabledata, &$columns, &$actions, $place, $nodata = '', $pre_fn = null) |
||
| 72 | { |
||
| 73 | // Action buttons hook's place |
||
| 74 | $this->plugin_functions_parameters = [ |
||
| 75 | 'actionbuttons' => &$actions, |
||
| 76 | 'place' => $place, |
||
| 77 | ]; |
||
| 78 | |||
| 79 | if ($this->has_ma = isset($actions['multiactions'])) { |
||
| 80 | $this->ma = $actions['multiactions']; |
||
| 81 | } |
||
| 82 | unset($actions['multiactions']); |
||
| 83 | |||
| 84 | $this->tabledata = $tabledata; |
||
| 85 | $this->columns = $columns; |
||
| 86 | $this->actions = $actions; |
||
| 87 | $this->place = $place; |
||
| 88 | $this->nodata = $nodata; |
||
| 89 | $this->pre_fn = $pre_fn; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function printTable($turn_into_datatable = true, $with_body = true) |
||
| 134 | } |
||
| 135 | |||
| 136 | private function _getMaHtml() |
||
| 191 | } |
||
| 192 | |||
| 193 | public function getThead() |
||
| 233 | } |
||
| 234 | |||
| 235 | private function getTbody() |
||
| 236 | { |
||
| 237 | $columns = $this->columns; |
||
| 238 | $actions = $this->actions; |
||
| 239 | $tabledata = $this->tabledata; |
||
| 240 | $pre_fn = $this->pre_fn; |
||
| 241 | |||
| 242 | // Display table rows |
||
| 243 | $i = 0; |
||
| 244 | $tbody_html = '<tbody>'; |
||
| 245 | |||
| 246 | while (!$tabledata->EOF) { |
||
| 247 | $id = ($i % 2) + 1; |
||
| 248 | |||
| 249 | unset($alt_actions); |
||
| 250 | if (!is_null($pre_fn)) { |
||
| 251 | $alt_actions = $pre_fn($tabledata, $actions); |
||
| 252 | } |
||
| 253 | |||
| 254 | if (!isset($alt_actions)) { |
||
| 255 | $alt_actions = &$actions; |
||
| 256 | } |
||
| 257 | |||
| 258 | $tbody_html .= sprintf('<tr class="data%s">', $id).PHP_EOL; |
||
| 259 | if ($this->has_ma) { |
||
| 260 | $a = []; |
||
| 261 | foreach ($this->ma['keycols'] as $k => $v) { |
||
| 262 | $a[$k] = $tabledata->fields[$v]; |
||
| 263 | } |
||
| 264 | //\Kint::dump($a); |
||
| 265 | $tbody_html .= sprintf('<td><input type="checkbox" name="ma[]" value="%s"/></td>', htmlentities(serialize($a), ENT_COMPAT, 'UTF-8')).PHP_EOL; |
||
| 266 | } |
||
| 267 | |||
| 268 | foreach ($columns as $column_id => $column) { |
||
| 269 | // Apply default values for missing parameters |
||
| 270 | if (isset($column['url']) && !isset($column['vars'])) { |
||
| 271 | $column['vars'] = []; |
||
| 272 | } |
||
| 273 | $class = (isset($column['class']) && '' !== $column['class']) ? $column['class'] : ''; |
||
| 274 | |||
| 275 | switch ($column_id) { |
||
| 276 | case 'actions': |
||
| 277 | $tbody_html .= "<td class=\"opbutton{$id} {$class}\">"; |
||
| 278 | foreach ($alt_actions as $action) { |
||
| 279 | if (isset($action['disable']) && true === $action['disable']) { |
||
| 280 | continue; |
||
| 281 | } |
||
| 282 | $action['fields'] = $tabledata->fields; |
||
| 283 | $tbody_html .= $this->printLink($action, false, __METHOD__); |
||
| 284 | } |
||
| 285 | $tbody_html .= '</td>'.PHP_EOL; |
||
| 286 | |||
| 287 | break; |
||
| 288 | case 'comment': |
||
| 289 | $tbody_html .= "<td class='comment_cell'>"; |
||
| 290 | $tbody_html .= htmlentities(Decorator::get_sanitized_value($column['field'], $tabledata->fields)); |
||
| 291 | $tbody_html .= '</td>'; |
||
| 292 | |||
| 293 | break; |
||
| 294 | default: |
||
| 295 | $tbody_html .= '<td class="'.$class.'">'; |
||
| 296 | $val = Decorator::get_sanitized_value($column['field'], $tabledata->fields); |
||
| 297 | if (!is_null($val)) { |
||
| 298 | if (isset($column['url'])) { |
||
| 299 | $tbody_html .= "<a href=\"{$column['url']}"; |
||
| 300 | $tbody_html .= $this->printUrlVars($column['vars'], $tabledata->fields, false); |
||
| 301 | $tbody_html .= '">'; |
||
| 302 | } |
||
| 303 | $type = isset($column['type']) ? $column['type'] : null; |
||
| 304 | $params = isset($column['params']) ? $column['params'] : []; |
||
| 305 | $tbody_html .= $this->misc->printVal($val, $type, $params); |
||
| 306 | if (isset($column['url'])) { |
||
| 307 | $tbody_html .= '</a>'; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | $tbody_html .= '</td>'.PHP_EOL; |
||
| 312 | |||
| 313 | break; |
||
| 314 | } |
||
| 315 | } |
||
| 316 | $tbody_html .= '</tr>'.PHP_EOL; |
||
| 317 | |||
| 318 | $tabledata->moveNext(); |
||
| 319 | ++$i; |
||
| 320 | } |
||
| 321 | |||
| 322 | $tbody_html .= '</tbody>'; |
||
| 323 | |||
| 324 | return $tbody_html; |
||
| 325 | } |
||
| 326 | |||
| 327 | public function getTfooter() |
||
| 328 | { |
||
| 329 | $columns = $this->columns; |
||
| 330 | $actions = $this->actions; |
||
| 331 | |||
| 332 | $tfoot_html = '<tfoot><tr>'.PHP_EOL; |
||
| 333 | |||
| 334 | // Display column headings |
||
| 335 | if ($this->has_ma) { |
||
| 336 | $tfoot_html .= '<td></td>'; |
||
| 337 | } |
||
| 338 | |||
| 339 | foreach ($columns as $column_id => $column) { |
||
| 340 | // Handle cases where no class has been passed |
||
| 341 | |||
| 342 | $class = (isset($column['class']) && '' !== $column['class']) ? $column['class'] : ''; |
||
| 343 | |||
| 344 | if ($column_id !== 'actions' || sizeof($actions) > 0) { |
||
| 345 | $tfoot_html .= "<td class=\"data{$class}\"></td>".PHP_EOL; |
||
| 346 | } |
||
| 347 | } |
||
| 348 | $tfoot_html .= '</tr></tfoot>'.PHP_EOL; |
||
| 349 | |||
| 350 | return $tfoot_html; |
||
| 351 | } |
||
| 352 | |||
| 353 | private function getForm() |
||
| 354 | { |
||
| 355 | if (!$this->form) { |
||
| 356 | $this->form = $this->misc->setForm(); |
||
| 357 | } |
||
| 358 | |||
| 359 | return $this->form; |
||
| 360 | } |
||
| 361 | |||
| 362 | private function printUrlVars(&$vars, &$fields, $do_print = true) |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 |