1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Element;
13
14 use ICanBoogie\I18n;
15 use ICanBoogie\Module;
16 use ICanBoogie\PropertyNotDefined;
17 use ICanBoogie\Route;
18 use ICanBoogie\Routes;
19
20 use Brickrouge\A;
21 use Brickrouge\DropdownMenu;
22
23 use Icybee\Modules\Users\Users;
24
25 26 27
28 class Navigation extends \Brickrouge\Element
29 {
30 public function __construct(array $attributes=array())
31 {
32 parent::__construct
33 (
34 'div', $attributes + array
35 (
36 'class' => 'navbar'
37 )
38 );
39 }
40
41 protected function render_inner_html()
42 {
43 global $core;
44
45 $rc = parent::render_inner_html();
46
47 $links = array();
48 $routes = Routes::get();
49 $user = $core->user;
50 $menus = array();
51
52 $modules = $core->modules;
53 $descriptors = $modules->descriptors;
54
55 foreach ($routes as $route)
56 {
57 if (empty($route['index']) || empty($route['module']))
58 {
59 continue;
60 }
61
62 $module_id = $route['module'];
63
64 if (!isset($modules[$module_id]))
65 {
66 continue;
67 }
68
69 $category = $descriptors[$module_id][Module::T_CATEGORY];
70
71 $permission = isset($route['permission']) ? $route['permission'] : Module::PERMISSION_ACCESS;
72
73 if (!$user->has_permission($permission, $module_id))
74 {
75 continue;
76 }
77
78 $menus[$category][$route['pattern']] = $route;
79
80 $links[$category] = I18n\t($category, array(), array('scope' => 'module_category'));
81 }
82
83 uasort($links, 'ICanBoogie\unaccent_compare_ci');
84
85 $links = array_merge
86 (
87 array
88 (
89 'dashboard' => 'Dashboard',
90 'features' => 'Features'
91 ),
92
93 $links
94 );
95
96 if (empty($menus['features']))
97 {
98 unset($links['features']);
99 }
100
101 $matching_route = null;
102
103 try
104 {
105 $matching_route = $core->request->route;
106 }
107 catch (PropertyNotDefined $e) {}
108
109 $active = $matching_route ? $descriptors[$matching_route->module][Module::T_CATEGORY] : 'dashboard';
110
111 $rc .= '<ul class="nav">';
112
113 foreach ($links as $path => $label)
114 {
115 if (strpos($active, $path) === 0)
116 {
117 $rc .= '<li class="active">';
118 }
119 else
120 {
121 $rc .= '<li>';
122 }
123
124 $url = \ICanBoogie\Routing\contextualize('/admin/'. $path);
125
126 $rc .= '<a href="' . \ICanBoogie\escape($url) . '">' . $label . '</a>';
127
128 if (isset($menus[$path]))
129 {
130 $rc .= $this->render_dropdown_menu($menus[$path]);
131 }
132
133 $rc .= '</li>';
134 }
135
136 $rc .= '</ul>';
137
138 return $rc;
139 }
140
141 protected function (array $routes)
142 {
143 global $core;
144
145 $options = array();
146 $descriptors = $core->modules->descriptors;
147
148 foreach ($routes as $route)
149 {
150 $title = $route['title'];
151 $module_id = $route['module'];
152 $module_flat_id = strtr($module_id, '.', '_');
153 $title = I18n\t($module_flat_id, array(), array('scope' => 'module_title', 'default' => $descriptors[$module_id][Module::T_TITLE]));
154 $url = \ICanBoogie\Routing\contextualize($route['pattern']);
155 $options[$url] = array($title, $url);
156 }
157
158 uasort($options, function($a, $b) {
159
160 return \ICanBoogie\unaccent_compare_ci($a[0], $b[0]);
161
162 });
163
164 array_walk($options, function(&$v) {
165
166 list($title, $url) = $v;
167
168 $v = new A($title, $url);
169
170 });
171
172 return new DropdownMenu
173 (
174 array
175 (
176 DropdownMenu::OPTIONS => $options,
177
178 'value' => $core->request->path
179 )
180 );
181 }
182 }