| Total Complexity | 55 |
| Total Lines | 494 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like HTMLNavbarController 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 HTMLNavbarController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class HTMLNavbarController extends HTMLController |
||
| 13 | { |
||
| 14 | public $controller_name = 'HTMLNavbarController'; |
||
| 15 | |||
| 16 | private function _getCrumbs($trail) |
||
| 17 | { |
||
| 18 | $crumbs = []; |
||
| 19 | foreach ($trail as $crumb_id => $crumb) { |
||
| 20 | if (isset($crumb['url'])) { |
||
| 21 | $crumbs[$crumb_id]['url'] = str_replace('&', '&', $crumb['url']); |
||
| 22 | } |
||
| 23 | |||
| 24 | if (isset($crumb['title'])) { |
||
| 25 | $crumbs[$crumb_id]['title'] = $crumb['title']; |
||
| 26 | $crumbs[$crumb_id]['iconalt'] = $crumb['title']; |
||
| 27 | } else { |
||
| 28 | $crumbs[$crumb_id]['iconalt'] = 'Database Root'; |
||
| 29 | } |
||
| 30 | |||
| 31 | if (isset($crumb['icon']) && $icon = $this->misc->icon($crumb['icon'])) { |
||
| 32 | $crumbs[$crumb_id]['icon'] = $icon; |
||
| 33 | } |
||
| 34 | |||
| 35 | $crumbs[$crumb_id]['text'] = $crumb['text']; |
||
| 36 | |||
| 37 | if (isset($crumb['help'])) { |
||
| 38 | $crumbs[$crumb_id]['helpurl'] = str_replace('&', '&', $this->misc->getHelpLink($crumb['help'])); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | return $crumbs; |
||
| 42 | } |
||
| 43 | |||
| 44 | private function _getSearchPathsCrumbs($crumbs, $viewVars) |
||
| 45 | { |
||
| 46 | $data = $this->misc->getDatabaseAccessor(); |
||
| 47 | $lang = $this->lang; |
||
| 48 | if (isset($crumbs['database'])) { |
||
| 49 | $search_path_crumbs = []; |
||
| 50 | $dburl = $crumbs['database']['url']; |
||
| 51 | $search_paths = $data->getSearchPath(); |
||
| 52 | foreach ($search_paths as $schema) { |
||
| 53 | $search_path_crumbs[$schema] = [ |
||
| 54 | 'title' => $lang['strschema'], |
||
| 55 | 'text' => $schema, |
||
| 56 | 'icon' => $this->misc->icon('Schema'), |
||
| 57 | 'iconalt' => $lang['strschema'], |
||
| 58 | 'url' => str_replace(['&', 'redirect/database'], ['&', 'redirect/schema'], $dburl . '&schema=' . $schema), |
||
| 59 | ]; |
||
| 60 | } |
||
| 61 | $viewVars['search_paths'] = $search_path_crumbs; |
||
| 62 | } |
||
| 63 | return $viewVars; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Display a bread crumb trail. |
||
| 68 | * |
||
| 69 | * @param array|string $trail an array of breadcrumb items, or a string to identify one of them |
||
| 70 | * @param $do_print true to echo, false to return html |
||
| 71 | * @param null|string $from |
||
| 72 | */ |
||
| 73 | public function printTrail($trail = [], $do_print = true, $from = null) |
||
| 74 | { |
||
| 75 | $plugin_manager = $this->plugin_manager; |
||
| 76 | $from = $from ? $from : __METHOD__; |
||
| 77 | |||
| 78 | $trail_html = $this->printTopbar(false, $from); |
||
| 79 | |||
| 80 | if (is_string($trail)) { |
||
| 81 | $subject = $trail; |
||
| 82 | $trail = $this->_getTrail($subject); |
||
| 83 | // Trail hook's place |
||
| 84 | $plugin_functions_parameters = [ |
||
| 85 | 'trail' => &$trail, |
||
| 86 | 'section' => $subject, |
||
| 87 | ]; |
||
| 88 | |||
| 89 | $plugin_manager->doHook('trail', $plugin_functions_parameters); |
||
| 90 | } |
||
| 91 | |||
| 92 | $crumbs = $this->_getCrumbs($trail); |
||
| 93 | |||
| 94 | $viewVars = [ |
||
| 95 | 'crumbs' => $crumbs, |
||
| 96 | 'controller_name' => $this->controller_name, |
||
| 97 | ]; |
||
| 98 | $viewVars = $this->_getSearchPathsCrumbs($crumbs, $viewVars); |
||
| 99 | |||
| 100 | //\Kint::dump($viewVars); |
||
| 101 | |||
| 102 | $trail_html .= $this->getContainer()->view->fetch('components/trail.twig', $viewVars); |
||
| 103 | |||
| 104 | if ($do_print) { |
||
| 105 | echo $trail_html; |
||
| 106 | } else { |
||
| 107 | return $trail_html; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Display navigation tabs. |
||
| 113 | * |
||
| 114 | * @param string $alltabs The name of current section (Ex: intro, server, ...), |
||
| 115 | * or an array with tabs (Ex: sqledit::doFind function) |
||
| 116 | * @param string $activetab the name of the tab to be highlighted |
||
| 117 | * @param bool $print if false, return html |
||
|
|
|||
| 118 | * @param bool $do_print true to print html, false to return html |
||
| 119 | * @param null|string $from whichi method is calling this one |
||
| 120 | */ |
||
| 121 | public function printTabs($alltabs, $activetab, $do_print = true, $from = null) |
||
| 122 | { |
||
| 123 | $from = $from ? $from : __METHOD__; |
||
| 124 | |||
| 125 | $lang = $this->lang; |
||
| 126 | $this->misc = $this->misc; |
||
| 127 | |||
| 128 | if (is_string($alltabs)) { |
||
|
1 ignored issue
–
show
|
|||
| 129 | $_SESSION['webdbLastTab'][$alltabs] = $activetab; |
||
| 130 | $alltabs = $this->misc->getNavTabs($alltabs); |
||
| 131 | } |
||
| 132 | //$this->prtrace($tabs); |
||
| 133 | $tabs_html = ''; |
||
| 134 | |||
| 135 | //Getting only visible tabs |
||
| 136 | $tabs = []; |
||
| 137 | if (count($alltabs) > 0) { |
||
| 138 | foreach ($alltabs as $tab_id => $tab) { |
||
| 139 | if (!isset($tab['hide']) || true !== $tab['hide']) { |
||
| 140 | $tabs[$tab_id] = $tab; |
||
| 141 | $tabs[$tab_id]['active'] = $active = ($tab_id == $activetab) ? ' active' : ''; |
||
| 142 | $tabs[$tab_id]['tablink'] = str_replace(['&', '.php'], ['&', ''], htmlentities($this->getActionUrl($tab, $_REQUEST, $from))); |
||
| 143 | //$this->prtrace('link for ' . $tab_id, $tabs[$tab_id]['tablink']); |
||
| 144 | if (isset($tab['icon']) && $icon = $this->misc->icon($tab['icon'])) { |
||
| 145 | $tabs[$tab_id]['iconurl'] = $icon; |
||
| 146 | } |
||
| 147 | if (isset($tab['help'])) { |
||
| 148 | $tabs[$tab_id]['helpurl'] = str_replace('&', '&', $this->misc->getHelpLink($tab['help'])); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | //$this->prtrace($tabs); |
||
| 155 | |||
| 156 | if (count($tabs) > 0) { |
||
| 157 | $width = (int) (100 / count($tabs)) . '%'; |
||
| 158 | |||
| 159 | $viewVars = [ |
||
| 160 | 'width' => $width, |
||
| 161 | 'tabs' => $tabs, |
||
| 162 | 'controller_name' => $this->controller_name, |
||
| 163 | ]; |
||
| 164 | |||
| 165 | $tabs_html = $this->getContainer()->view->fetch('components/tabs.twig', $viewVars); |
||
| 166 | } |
||
| 167 | |||
| 168 | if ($do_print) { |
||
| 169 | echo $tabs_html; |
||
| 170 | } else { |
||
| 171 | return $tabs_html; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * [printTopbar description]. |
||
| 177 | * |
||
| 178 | * @param bool $do_print true to print, false to return html |
||
| 179 | * @param null|mixed $from which method is calling this one |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | private function printTopbar($do_print = true, $from = null) |
||
|
1 ignored issue
–
show
|
|||
| 184 | { |
||
| 185 | $from = $from ? $from : __METHOD__; |
||
| 186 | |||
| 187 | $lang = $this->lang; |
||
| 188 | $plugin_manager = $this->plugin_manager; |
||
| 189 | $this->misc = $this->misc; |
||
| 190 | $appName = $this->misc->appName; |
||
| 191 | $appVersion = $this->misc->appVersion; |
||
| 192 | |||
| 193 | $server_info = $this->misc->getServerInfo(); |
||
| 194 | $server_id = $this->misc->getServerId(); |
||
|
1 ignored issue
–
show
|
|||
| 195 | $reqvars = $this->misc->getRequestVars('table'); |
||
| 196 | |||
| 197 | $topbar_html = '<div class="topbar" data-controller="' . $this->controller_name . '"><table style="width: 100%"><tr><td>'; |
||
| 198 | |||
| 199 | if ($server_info && isset($server_info['platform'], $server_info['username'])) { |
||
| 200 | // top left informations when connected |
||
| 201 | $topbar_html .= sprintf( |
||
| 202 | $lang['strtopbar'], |
||
| 203 | '<span class="platform">' . htmlspecialchars($server_info['platform']) . '</span>', |
||
| 204 | '<span class="host">' . htmlspecialchars((empty($server_info['host'])) ? 'localhost' : $server_info['host']) . '</span>', |
||
| 205 | '<span class="port">' . htmlspecialchars($server_info['port']) . '</span>', |
||
| 206 | '<span class="username">' . htmlspecialchars($server_info['username']) . '</span>' |
||
| 207 | ); |
||
| 208 | |||
| 209 | $topbar_html .= '</td>'; |
||
| 210 | |||
| 211 | // top right informations when connected |
||
| 212 | |||
| 213 | $toplinks = [ |
||
| 214 | 'sql' => [ |
||
| 215 | 'attr' => [ |
||
| 216 | 'href' => [ |
||
| 217 | 'url' => SUBFOLDER . '/src/views/sqledit', |
||
| 218 | 'urlvars' => array_merge($reqvars, [ |
||
| 219 | 'action' => 'sql', |
||
| 220 | ]), |
||
| 221 | ], |
||
| 222 | 'target' => 'sqledit', |
||
| 223 | 'id' => 'toplink_sql', |
||
| 224 | ], |
||
| 225 | 'content' => $lang['strsql'], |
||
| 226 | ], |
||
| 227 | 'history' => [ |
||
| 228 | 'attr' => [ |
||
| 229 | 'href' => [ |
||
| 230 | 'url' => SUBFOLDER . '/src/views/history', |
||
| 231 | 'urlvars' => array_merge($reqvars, [ |
||
| 232 | 'action' => 'pophistory', |
||
| 233 | ]), |
||
| 234 | ], |
||
| 235 | 'id' => 'toplink_history', |
||
| 236 | ], |
||
| 237 | 'content' => $lang['strhistory'], |
||
| 238 | ], |
||
| 239 | 'find' => [ |
||
| 240 | 'attr' => [ |
||
| 241 | 'href' => [ |
||
| 242 | 'url' => SUBFOLDER . '/src/views/sqledit', |
||
| 243 | 'urlvars' => array_merge($reqvars, [ |
||
| 244 | 'action' => 'find', |
||
| 245 | ]), |
||
| 246 | ], |
||
| 247 | 'target' => 'sqledit', |
||
| 248 | 'id' => 'toplink_find', |
||
| 249 | ], |
||
| 250 | 'content' => $lang['strfind'], |
||
| 251 | ], |
||
| 252 | 'logout' => [ |
||
| 253 | 'attr' => [ |
||
| 254 | 'href' => [ |
||
| 255 | 'url' => SUBFOLDER . '/src/views/servers', |
||
| 256 | 'urlvars' => [ |
||
| 257 | 'action' => 'logout', |
||
| 258 | 'logoutServer' => sha1("{$server_info['host']}:{$server_info['port']}:{$server_info['sslmode']}"), |
||
| 259 | ], |
||
| 260 | ], |
||
| 261 | 'id' => 'toplink_logout', |
||
| 262 | ], |
||
| 263 | 'content' => $lang['strlogout'], |
||
| 264 | ], |
||
| 265 | ]; |
||
| 266 | |||
| 267 | // Toplink hook's place |
||
| 268 | $plugin_functions_parameters = [ |
||
| 269 | 'toplinks' => &$toplinks, |
||
| 270 | ]; |
||
| 271 | |||
| 272 | $plugin_manager->doHook('toplinks', $plugin_functions_parameters); |
||
| 273 | |||
| 274 | $topbar_html .= '<td style="text-align: right">'; |
||
| 275 | |||
| 276 | $topbar_html .= $this->printLinksList($toplinks, 'toplink', [], false, $from); |
||
| 277 | |||
| 278 | $topbar_html .= '</td>'; |
||
| 279 | } else { |
||
| 280 | $topbar_html .= "<span class=\"appname\">{$appName}</span> <span class=\"version\">{$appVersion}</span>"; |
||
| 281 | } |
||
| 282 | |||
| 283 | $topbar_html .= "</tr></table></div>\n"; |
||
| 284 | |||
| 285 | if ($do_print) { |
||
| 286 | echo $topbar_html; |
||
| 287 | } else { |
||
| 288 | return $topbar_html; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | private function getHREFSubject($subject) |
||
|
1 ignored issue
–
show
|
|||
| 293 | { |
||
| 294 | $vars = $this->misc->getSubjectParams($subject); |
||
| 295 | ksort($vars['params']); |
||
| 296 | |||
| 297 | return "{$vars['url']}?" . http_build_query($vars['params'], '', '&'); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Create a bread crumb trail of the object hierarchy. |
||
| 302 | * |
||
| 303 | * @param null|string $subject sunkect of the trail |
||
| 304 | */ |
||
| 305 | private function _getTrail($subject = null) |
||
| 306 | { |
||
| 307 | $lang = $this->lang; |
||
| 308 | |||
| 309 | $this->misc = $this->misc; |
||
| 310 | $appName = $this->misc->appName; |
||
| 311 | |||
| 312 | $data = $this->misc->getDatabaseAccessor(); |
||
| 313 | |||
| 314 | $trail = []; |
||
| 315 | $vars = ''; |
||
| 316 | $done = false; |
||
| 317 | |||
| 318 | $trail['root'] = [ |
||
| 319 | 'text' => $appName, |
||
| 320 | 'url' => SUBFOLDER . '/src/views/servers', |
||
| 321 | 'icon' => 'Introduction', |
||
| 322 | ]; |
||
| 323 | |||
| 324 | if ('root' == $subject) { |
||
| 325 | return $trail; |
||
| 326 | } |
||
| 327 | |||
| 328 | $server_info = $this->misc->getServerInfo(); |
||
| 329 | $trail['server'] = [ |
||
| 330 | 'title' => $lang['strserver'], |
||
| 331 | 'text' => $server_info['desc'], |
||
| 332 | 'url' => $this->getHREFSubject('server'), |
||
| 333 | 'help' => 'pg.server', |
||
| 334 | 'icon' => 'Server', |
||
| 335 | ]; |
||
| 336 | |||
| 337 | if ('server' == $subject) { |
||
| 338 | return $trail; |
||
| 339 | } |
||
| 340 | |||
| 341 | $database_rolename = [ |
||
| 342 | 'database' => [ |
||
| 343 | 'title' => $lang['strdatabase'], |
||
| 344 | 'subject' => 'database', |
||
| 345 | 'help' => 'pg.database', |
||
| 346 | 'icon' => 'Database', |
||
| 347 | ], |
||
| 348 | 'rolename' => [ |
||
| 349 | 'title' => $lang['strrole'], |
||
| 350 | 'subject' => 'role', |
||
| 351 | 'help' => 'pg.role', |
||
| 352 | 'icon' => 'Roles', |
||
| 353 | ], |
||
| 354 | ]; |
||
| 355 | |||
| 356 | $trail = $this->_getTrailsFromArray($trail, $database_rolename); |
||
| 357 | |||
| 358 | if (in_array($subject, ['database', 'role'])) { |
||
| 359 | return $trail; |
||
| 360 | } |
||
| 361 | |||
| 362 | $schema = [ |
||
| 363 | 'schema' => [ |
||
| 364 | 'title' => $lang['strschema'], |
||
| 365 | 'subject' => 'schema', |
||
| 366 | 'help' => 'pg.schema', |
||
| 367 | 'icon' => 'Schema', |
||
| 368 | ], |
||
| 369 | ]; |
||
| 370 | |||
| 371 | $trail = $this->_getTrailsFromArray($trail, $schema); |
||
| 372 | if ('schema' == $subject) { |
||
| 373 | return $trail; |
||
| 374 | } |
||
| 375 | |||
| 376 | $table_view_matview_fts = [ |
||
| 377 | 'table' => [ |
||
| 378 | 'title' => $lang['strtable'], |
||
| 379 | 'subject' => 'table', |
||
| 380 | 'help' => 'pg.table', |
||
| 381 | 'icon' => 'Table', |
||
| 382 | ], |
||
| 383 | 'view' => [ |
||
| 384 | 'title' => $lang['strview'], |
||
| 385 | 'subject' => 'view', |
||
| 386 | 'help' => 'pg.view', |
||
| 387 | 'icon' => 'View', |
||
| 388 | ], |
||
| 389 | 'matview' => [ |
||
| 390 | 'title' => 'M' . $lang['strview'], |
||
| 391 | 'subject' => 'matview', |
||
| 392 | 'help' => 'pg.matview', |
||
| 393 | 'icon' => 'MViews', |
||
| 394 | ], |
||
| 395 | 'ftscfg' => [ |
||
| 396 | 'title' => $lang['strftsconfig'], |
||
| 397 | 'subject' => 'ftscfg', |
||
| 398 | 'help' => 'pg.ftscfg.example', |
||
| 399 | 'icon' => 'Fts', |
||
| 400 | ], |
||
| 401 | ]; |
||
| 402 | |||
| 403 | $trail = $this->_getTrailsFromArray($trail, $table_view_matview_fts); |
||
| 404 | |||
| 405 | if (in_array($subject, ['table', 'view', 'matview', 'ftscfg'])) { |
||
| 406 | return $trail; |
||
| 407 | } |
||
| 408 | |||
| 409 | if (!is_null($subject)) { |
||
| 410 | $trail = $this->_getLastTrailPart($subject, $trail); |
||
| 411 | } |
||
| 412 | |||
| 413 | //$this->prtrace($trail); |
||
| 414 | |||
| 415 | return $trail; |
||
| 416 | } |
||
| 417 | |||
| 418 | private function _getTrailsFromArray($trail, $the_array) |
||
| 419 | { |
||
| 420 | foreach ($the_array as $key => $value) { |
||
| 421 | if (isset($_REQUEST[$key])) { |
||
| 422 | $trail[$key] = [ |
||
| 423 | 'title' => $value['title'], |
||
| 424 | 'text' => $_REQUEST[$key], |
||
| 425 | 'url' => $this->getHREFSubject($value['subject']), |
||
| 426 | 'help' => $value['help'], |
||
| 427 | 'icon' => $value['icon'], |
||
| 428 | ]; |
||
| 429 | break; |
||
| 430 | } |
||
| 431 | } |
||
| 432 | return $trail; |
||
| 433 | } |
||
| 434 | |||
| 435 | private function _getLastTrailPart($subject, $trail) |
||
| 506 | } |
||
| 507 | } |
||
| 508 |