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