iFaxity /
anax-layout
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Faxity\Navbar; |
||
| 4 | |||
| 5 | use Anax\Commons\ContainerInjectableTrait; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Helper to create a navbar for sites by reading its configuration from file |
||
| 9 | * and then applying some code while rendering the resultning navbar. |
||
| 10 | * |
||
| 11 | * This is a version of anax navigation, but modified so it fits my use instead. |
||
| 12 | */ |
||
| 13 | class Navbar |
||
| 14 | { |
||
| 15 | use ContainerInjectableTrait; |
||
| 16 | |||
| 17 | |||
| 18 | /** |
||
| 19 | * Callback tracing the current selected menu item base on scriptname. |
||
| 20 | * |
||
| 21 | * @param: string $url to check for. |
||
| 22 | * |
||
| 23 | * @return bool true if item is selected, else false. |
||
| 24 | */ |
||
| 25 | 3 | public function check($url) |
|
| 26 | { |
||
| 27 | 3 | return $url == $this->di->request->getRoute(); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 28 | } |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * Create a navigation bar/menu, with submenus. |
||
| 33 | * |
||
| 34 | * @param array $config with configuration for the menu. |
||
| 35 | * |
||
| 36 | * @return string with html for the menu. |
||
| 37 | */ |
||
| 38 | 1 | public function createMenuWithSubMenus(array $config) |
|
| 39 | { |
||
| 40 | $default = [ |
||
| 41 | 1 | "id" => null, |
|
| 42 | "class" => null, |
||
| 43 | "wrapper" => "nav", |
||
| 44 | ]; |
||
| 45 | 1 | $menu = array_replace_recursive($default, $config); |
|
| 46 | 1 | $class = isset($menu["class"]) ? " class=\"{$menu["class"]}\"" : null; |
|
| 47 | |||
| 48 | // Call the anonomous function to create the menu, and submenues if any. |
||
| 49 | 1 | list($html) = $this->createMenu($menu["items"], $class); |
|
| 50 | |||
| 51 | 1 | return "\n{$html}\n"; |
|
| 52 | } |
||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * Create a navigation bar/menu. |
||
| 57 | * |
||
| 58 | * @param array $items Menu items. |
||
| 59 | * @param string|null $ulClass Optiona class to append on it's ul element. |
||
| 60 | * |
||
| 61 | * @return array with html for the menu. |
||
| 62 | * |
||
| 63 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 64 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
| 65 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
| 66 | */ |
||
| 67 | 2 | public function createMenu(array $items, ?string $ulClass = null) |
|
| 68 | { |
||
| 69 | 2 | $html = null; |
|
| 70 | 2 | $hasItemIsSelected = false; |
|
| 71 | 2 | $subMenuClass = " class=\"submenu\""; |
|
| 72 | |||
| 73 | 2 | foreach ($items as $item) { |
|
| 74 | // has submenu, call recursivly and keep track on if the submenu has a selected item in it. |
||
| 75 | 2 | $subMenu = ""; |
|
| 76 | 2 | $selectedParent = ""; |
|
| 77 | 2 | $subMenuIcon = ""; |
|
| 78 | 2 | $selected = $this->check($item["url"]); |
|
| 79 | 2 | $classes = []; |
|
| 80 | 2 | $class = ""; |
|
| 81 | |||
| 82 | 2 | if (isset($item["submenu"])) { |
|
| 83 | 1 | list($subMenu, $selectedParent) = $this->createMenu($item["submenu"], $subMenuClass); |
|
| 84 | 1 | $subMenuIcon = "<span class=\"submenu-icon\"></span>"; |
|
| 85 | |||
| 86 | 1 | $subMenu && $classes[] = "has-submenu"; |
|
| 87 | 1 | $selectedParent && $classes[] = "selected-parent"; |
|
| 88 | } |
||
| 89 | |||
| 90 | 2 | if ($selected) { |
|
| 91 | // Remember there is selected children when going up the menu hierarchy |
||
| 92 | 2 | $hasItemIsSelected = true; |
|
| 93 | 2 | $classes[] = "selected"; |
|
| 94 | } |
||
| 95 | |||
| 96 | 2 | if (!empty($classes)) { |
|
| 97 | 2 | $class = " class=\"" . implode(" ", $classes) ."\" "; |
|
| 98 | } |
||
| 99 | |||
| 100 | // Add the menu item |
||
| 101 | 2 | $url = $this->di->url->create($item["url"]); |
|
|
0 ignored issues
–
show
|
|||
| 102 | 2 | $html .= "\n<li{$class}><a href=\"{$url}\" title=\"{$item['title']}\">{$item['text']}</a>{$subMenuIcon}{$subMenu}</li>\n"; |
|
| 103 | } |
||
| 104 | |||
| 105 | // Return the menu |
||
| 106 | 2 | return ["\n<ul$ulClass>$html</ul>\n", $hasItemIsSelected]; |
|
| 107 | } |
||
| 108 | } |
||
| 109 |