| Total Complexity | 76 |
| Total Lines | 541 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DashboardManager 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 DashboardManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class DashboardManager |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Constructor. |
||
| 14 | */ |
||
| 15 | public function __construct() |
||
| 17 | } |
||
| 18 | |||
| 19 | /** |
||
| 20 | * This function allows easy activating and inactivating of dashboard plugins. |
||
| 21 | */ |
||
| 22 | public static function handle_dashboard_plugins() |
||
| 23 | { |
||
| 24 | $token = Security::get_existing_token(); |
||
| 25 | $tokenCondition = '&sec_token='.$token; |
||
| 26 | |||
| 27 | /* We scan the plugin directory. Each folder is a potential plugin. */ |
||
| 28 | $dashboard_pluginpath = api_get_path(SYS_PLUGIN_PATH).'dashboard/'; |
||
| 29 | $possiblePlugins = self::getPossibleDashboardPluginsPath(); |
||
| 30 | |||
| 31 | $table_cols = ['name', 'version', 'description']; |
||
| 32 | echo Display::page_subheader(get_lang('DashboardPlugins')); |
||
| 33 | echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).$tokenCondition.'">'; |
||
| 34 | echo '<table class="table table-hover table-striped data_table">'; |
||
| 35 | echo '<tr>'; |
||
| 36 | echo '<th width="50px">'.get_lang('Enabled').'</th>'; |
||
| 37 | echo '<th width="250px">'.get_lang('Name').'</th>'; |
||
| 38 | echo '<th width="100px">'.get_lang('Version').'</th>'; |
||
| 39 | echo '<th>'.get_lang('Description').'</th>'; |
||
| 40 | echo '</tr>'; |
||
| 41 | |||
| 42 | $disabled_blocks_data = self::get_block_data_without_plugin(); |
||
| 43 | |||
| 44 | // We display all the possible enabled or disabled plugins |
||
| 45 | foreach ($possiblePlugins as $testplugin) { |
||
| 46 | $plugin_info_file = $dashboard_pluginpath.$testplugin."/$testplugin.info"; |
||
| 47 | if (file_exists($plugin_info_file) && is_readable($plugin_info_file)) { |
||
| 48 | $plugin_info = api_parse_info_file($plugin_info_file); |
||
| 49 | |||
| 50 | // change index to lower case |
||
| 51 | $plugin_info = array_change_key_case($plugin_info); |
||
| 52 | |||
| 53 | echo '<tr>'; |
||
| 54 | self::display_dashboard_plugin_checkboxes($testplugin); |
||
| 55 | for ($i = 0; $i < count($table_cols); $i++) { |
||
|
|
|||
| 56 | if (isset($plugin_info[strtolower($table_cols[$i])])) { |
||
| 57 | echo '<td>'; |
||
| 58 | echo $plugin_info[$table_cols[$i]]; |
||
| 59 | echo '</td>'; |
||
| 60 | } else { |
||
| 61 | echo '<td></td>'; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | echo '</tr>'; |
||
| 65 | } else { |
||
| 66 | if ($testplugin != 'css') { |
||
| 67 | echo Display::tag( |
||
| 68 | 'tr', |
||
| 69 | Display::tag( |
||
| 70 | 'td', |
||
| 71 | get_lang('CheckFilePermissions').' '.Security::remove_XSS($plugin_info_file), |
||
| 72 | ['colspan' => '3'] |
||
| 73 | ) |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | // display all disabled block data |
||
| 80 | if (count($disabled_blocks_data) > 0) { |
||
| 81 | foreach ($disabled_blocks_data as $disabled_block) { |
||
| 82 | echo '<tr style="background-color:#eee">'; |
||
| 83 | echo '<td><center><input type="checkbox" name="disabled_block" value="true" checked disabled /></center>'; |
||
| 84 | for ($j = 0; $j < count($table_cols); $j++) { |
||
| 85 | if (isset($disabled_block[strtolower($table_cols[$j])])) { |
||
| 86 | if ($j == 2) { |
||
| 87 | echo '<td>'; |
||
| 88 | echo '<font color="#aaa">'.$disabled_block[$table_cols[$j]].'</font><br />'; |
||
| 89 | echo '<font color="red">'.get_lang('ThisPluginHasbeenDeletedFromDashboardPluginDirectory').'</font>'; |
||
| 90 | echo '</td>'; |
||
| 91 | } else { |
||
| 92 | echo '<td>'; |
||
| 93 | echo '<font color="#aaa">'.$disabled_block[$table_cols[$j]].'</font>'; |
||
| 94 | echo '</td>'; |
||
| 95 | } |
||
| 96 | } else { |
||
| 97 | echo '<td> </td>'; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | echo '</tr>'; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | echo '</table>'; |
||
| 105 | echo '<br />'; |
||
| 106 | echo '<button class="btn btn-default" type="submit" name="submit_dashboard_plugins" value="'.get_lang('EnableDashboardPlugins').'">'. |
||
| 107 | get_lang('EnableDashboardPlugins').'</button></form>'; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * display checkboxes for dashboard plugin list. |
||
| 112 | * |
||
| 113 | * @param string $plugin_path |
||
| 114 | */ |
||
| 115 | public static function display_dashboard_plugin_checkboxes($plugin_path) |
||
| 116 | { |
||
| 117 | $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK); |
||
| 118 | |||
| 119 | $sql = "SELECT * FROM $tbl_block |
||
| 120 | WHERE path = '".Database::escape_string($plugin_path)."' AND active = 1"; |
||
| 121 | $rs = Database::query($sql); |
||
| 122 | |||
| 123 | $checked = ''; |
||
| 124 | if (Database::num_rows($rs) > 0) { |
||
| 125 | $checked = "checked"; |
||
| 126 | } |
||
| 127 | |||
| 128 | echo "<td align=\"center\">"; |
||
| 129 | echo '<input type="checkbox" name="'.$plugin_path.'" value="true" '.$checked.'/>'; |
||
| 130 | echo "</td>"; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * This function allows easy activating and inactivating |
||
| 135 | * of plugins and save them inside db. |
||
| 136 | * |
||
| 137 | * @param array $plugin_paths dashboard plugin paths |
||
| 138 | * return int affected rows |
||
| 139 | */ |
||
| 140 | public static function store_dashboard_plugins($plugin_paths) |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get all plugins path inside dashboard directory. |
||
| 256 | * |
||
| 257 | * @return array name plugins directories |
||
| 258 | */ |
||
| 259 | public static function getPossibleDashboardPluginsPath() |
||
| 260 | { |
||
| 261 | // get all plugins path inside plugin directory |
||
| 262 | /* We scan the plugin directory. Each folder is a potential plugin. */ |
||
| 263 | $possiblePlugins = []; |
||
| 264 | $dashboard_pluginpath = api_get_path(SYS_PLUGIN_PATH).'dashboard/'; |
||
| 265 | $handle = @opendir($dashboard_pluginpath); |
||
| 266 | while (false !== ($file = readdir($handle))) { |
||
| 267 | if ($file != '.' && $file != '..' && is_dir($dashboard_pluginpath.$file)) { |
||
| 268 | $possiblePlugins[] = $file; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | @closedir($handle); |
||
| 272 | |||
| 273 | return $possiblePlugins; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get all blocks data without plugin directory. |
||
| 278 | * |
||
| 279 | * @return array Block data |
||
| 280 | */ |
||
| 281 | public static function get_block_data_without_plugin() |
||
| 282 | { |
||
| 283 | $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK); |
||
| 284 | $possiblePlugins = self::getPossibleDashboardPluginsPath(); |
||
| 285 | |||
| 286 | // We check if plugin exists inside directory for updating active field |
||
| 287 | $sql = "SELECT * FROM $tbl_block"; |
||
| 288 | $rs = Database::query($sql); |
||
| 289 | if (Database::num_rows($rs) > 0) { |
||
| 290 | while ($row = Database::fetch_array($rs)) { |
||
| 291 | if (!in_array($row['path'], $possiblePlugins)) { |
||
| 292 | $active = 0; |
||
| 293 | } else { |
||
| 294 | $active = 1; |
||
| 295 | } |
||
| 296 | // update active |
||
| 297 | $upd = "UPDATE $tbl_block SET active = '$active' |
||
| 298 | WHERE path = '".$row['path']."'"; |
||
| 299 | Database::query($upd); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | // get disabled block data |
||
| 304 | $block_data = []; |
||
| 305 | $sql = "SELECT * FROM $tbl_block WHERE active = 0"; |
||
| 306 | $rs_block = Database::query($sql); |
||
| 307 | if (Database::num_rows($rs_block) > 0) { |
||
| 308 | while ($row_block = Database::fetch_array($rs_block)) { |
||
| 309 | $block_data[] = $row_block; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | return $block_data; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * get data about enabled dashboard block (stored insise block table). |
||
| 318 | * |
||
| 319 | * @param string $path plugin path |
||
| 320 | * |
||
| 321 | * @return array data |
||
| 322 | */ |
||
| 323 | public static function get_enabled_dashboard_blocks($path = '') |
||
| 324 | { |
||
| 325 | $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK); |
||
| 326 | $condition_path = ''; |
||
| 327 | if (!empty($path)) { |
||
| 328 | $path = Database::escape_string($path); |
||
| 329 | $condition_path = ' AND path = "'.$path.'" '; |
||
| 330 | } |
||
| 331 | |||
| 332 | $sql = "SELECT * FROM $tbl_block WHERE active = 1 $condition_path "; |
||
| 333 | $rs = Database::query($sql); |
||
| 334 | $block_data = []; |
||
| 335 | if (Database::num_rows($rs) > 0) { |
||
| 336 | while ($row = Database::fetch_array($rs)) { |
||
| 337 | $block_data[$row['path']] = $row; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | return $block_data; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * display user dashboard list. |
||
| 346 | * |
||
| 347 | * @param int User id |
||
| 348 | */ |
||
| 349 | public static function display_user_dashboard_list($user_id) |
||
| 350 | { |
||
| 351 | $enabled_dashboard_plugins = self::get_enabled_dashboard_blocks(); |
||
| 352 | $user_block_data = self::get_user_block_data($user_id); |
||
| 353 | $html = ''; |
||
| 354 | if (count($enabled_dashboard_plugins) > 0) { |
||
| 355 | $html .= '<div style="margin-top:20px">'; |
||
| 356 | $html .= '<div><strong>'.get_lang('SelectBlockForDisplayingInsideBlocksDashboardView').'</strong></div><br />'; |
||
| 357 | $html .= '<form name="dashboard_list" method="post" action="index.php?action=store_user_block">'; |
||
| 358 | $html .= '<table class="table table-hover table-striped data_table">'; |
||
| 359 | $html .= '<tr>'; |
||
| 360 | $html .= '<th width="5%">'; |
||
| 361 | $html .= get_lang('Enabled'); |
||
| 362 | $html .= '</th>'; |
||
| 363 | $html .= '<th width="30%">'; |
||
| 364 | $html .= get_lang('Name'); |
||
| 365 | $html .= '</th>'; |
||
| 366 | $html .= '<th width="40%">'; |
||
| 367 | $html .= get_lang('Description'); |
||
| 368 | $html .= '</th>'; |
||
| 369 | $html .= '<th>'; |
||
| 370 | $html .= get_lang('ColumnPosition'); |
||
| 371 | $html .= '</th>'; |
||
| 372 | $html .= '</tr>'; |
||
| 373 | |||
| 374 | // We display all enabled plugins and the checkboxes |
||
| 375 | foreach ($enabled_dashboard_plugins as $block) { |
||
| 376 | $path = $block['path']; |
||
| 377 | $controller_class = $block['controller']; |
||
| 378 | $filename_controller = $path.'.class.php'; |
||
| 379 | $dashboard_plugin_path = api_get_path(SYS_PLUGIN_PATH).'dashboard/'.$path.'/'; |
||
| 380 | require_once $dashboard_plugin_path.$filename_controller; |
||
| 381 | if (class_exists($controller_class)) { |
||
| 382 | $obj_block = new $controller_class($user_id); |
||
| 383 | |||
| 384 | // check if user is allowed to see the block |
||
| 385 | if (method_exists($obj_block, 'is_block_visible_for_user')) { |
||
| 386 | $is_block_visible_for_user = $obj_block->is_block_visible_for_user($user_id); |
||
| 387 | if (!$is_block_visible_for_user) { |
||
| 388 | continue; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | $html .= '<tr>'; |
||
| 393 | // checkboxes |
||
| 394 | $html .= self::display_user_dashboard_list_checkboxes($user_id, $block['id']); |
||
| 395 | $html .= '<td>'.$block['name'].'</td>'; |
||
| 396 | $html .= '<td>'.$block['description'].'</td>'; |
||
| 397 | $html .= '<td> |
||
| 398 | <select class="selectpicker form-control" name="columns['.$block['id'].']"> |
||
| 399 | <option value="1" '.(isset($user_block_data[$block['id']]) && $user_block_data[$block['id']]['column'] == 1 ? 'selected' : '').' >1</option> |
||
| 400 | <option value="2" '.(isset($user_block_data[$block['id']]) && $user_block_data[$block['id']]['column'] == 2 ? 'selected' : '').' >2</option> |
||
| 401 | </select> |
||
| 402 | </td>'; |
||
| 403 | $html .= '</tr>'; |
||
| 404 | } else { |
||
| 405 | $html .= Display::tag('tr', Display::tag('td', get_lang('Error').' '.$controller_class, ['colspan' => '3'])); |
||
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | $html .= '</table>'; |
||
| 410 | $html .= '<div class="row"><div class="col-md-12">'; |
||
| 411 | $html .= '<button class="btn btn-default" type="submit" name="submit_dashboard_list" value="'.get_lang('EnableDashboardBlock').'"><em class="fa fa-check-square"></em> '. |
||
| 412 | get_lang('EnableDashboardBlock').'</button></form>'; |
||
| 413 | $html .= '</div></div>'; |
||
| 414 | } else { |
||
| 415 | $html .= '<div style="margin-top:20px">'.get_lang('ThereAreNoEnabledDashboardPlugins').'</div>'; |
||
| 416 | if (api_is_platform_admin()) { |
||
| 417 | $html .= '<a class="btn btn-default" href="'.api_get_path(WEB_CODE_PATH).'admin/settings.php?category=Plugins">'. |
||
| 418 | get_lang('ConfigureDashboardPlugin').'</a>'; |
||
| 419 | } |
||
| 420 | } |
||
| 421 | |||
| 422 | return $html; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * display checkboxes for user dashboard list. |
||
| 427 | * |
||
| 428 | * @param int User id |
||
| 429 | * @param int Block id |
||
| 430 | */ |
||
| 431 | public static function display_user_dashboard_list_checkboxes($user_id, $block_id) |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * This function store enabled blocks id with its column position (block_id1:colum;block_id2:colum; ...) |
||
| 451 | * inside extra user fields. |
||
| 452 | * |
||
| 453 | * @param int $user_id User id |
||
| 454 | * @param array $enabled_blocks selected blocks |
||
| 455 | * @param array $columns columns position |
||
| 456 | * |
||
| 457 | * @return bool |
||
| 458 | */ |
||
| 459 | public static function store_user_blocks($user_id, $enabled_blocks, $columns) |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * This function get user block data (block id with its number of column) from extra user data. |
||
| 483 | * |
||
| 484 | * @param int User id |
||
| 485 | * |
||
| 486 | * @return array data (block_id,column) |
||
| 487 | */ |
||
| 488 | public static function get_user_block_data($user_id) |
||
| 489 | { |
||
| 490 | $user_id = intval($user_id); |
||
| 491 | $field_variable = 'dashboard'; |
||
| 492 | $extra_user_data = UserManager::get_extra_user_data_by_field($user_id, $field_variable); |
||
| 493 | |||
| 494 | if (!isset($extra_user_data[$field_variable])) { |
||
| 495 | return []; |
||
| 496 | } |
||
| 497 | |||
| 498 | $extra_user_data = explode(';', $extra_user_data[$field_variable]); |
||
| 499 | $data = []; |
||
| 500 | foreach ($extra_user_data as $extra) { |
||
| 501 | $split_extra = explode(':', $extra); |
||
| 502 | if (!empty($split_extra)) { |
||
| 503 | $block_id = $split_extra[0]; |
||
| 504 | $column = isset($split_extra[1]) ? $split_extra[1] : null; |
||
| 505 | $data[$block_id] = ['block_id' => $block_id, 'column' => $column]; |
||
| 506 | } |
||
| 507 | } |
||
| 508 | |||
| 509 | return $data; |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * This function update extra user blocks data after closing a dashboard block. |
||
| 514 | * |
||
| 515 | * @param int $user_id User id |
||
| 516 | * @param string plugin path |
||
| 517 | * |
||
| 518 | * @return bool |
||
| 519 | */ |
||
| 520 | public static function close_user_block($user_id, $path) |
||
| 521 | { |
||
| 522 | $enabled_dashboard_blocks = self::get_enabled_dashboard_blocks($path); |
||
| 523 | $user_block_data = self::get_user_block_data($user_id); |
||
| 524 | |||
| 525 | foreach ($enabled_dashboard_blocks as $enabled_block) { |
||
| 526 | unset($user_block_data[$enabled_block['id']]); |
||
| 527 | } |
||
| 528 | |||
| 529 | // get columns and blocks id for updating extra user data |
||
| 530 | $columns = []; |
||
| 531 | $user_blocks_id = []; |
||
| 532 | foreach ($user_block_data as $data) { |
||
| 533 | $user_blocks_id[$data['block_id']] = true; |
||
| 534 | $columns[$data['block_id']] = $data['column']; |
||
| 535 | } |
||
| 536 | |||
| 537 | // update extra user blocks data |
||
| 538 | $upd_extra_field = self::store_user_blocks($user_id, $user_blocks_id, $columns); |
||
| 539 | |||
| 540 | return $upd_extra_field; |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * get links for styles from dashboard plugins. |
||
| 545 | * |
||
| 546 | * @return string links |
||
| 547 | */ |
||
| 548 | public static function getStyleSheet() |
||
| 551 | } |
||
| 552 | } |
||
| 553 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: