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