1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Element;
13
14 use ICanBoogie\I18n\Translator\Proxi;
15 use ICanBoogie\Module;
16 use ICanBoogie\Operation;
17 use ICanBoogie\Route;
18 use ICanBoogie\Routing;
19
20 use Brickrouge\Element;
21 use Brickrouge\ElementIsEmpty;
22
23 use Icybee\Modules\Users\User;
24
25 26 27 28 29
30 class extends Element
31 {
32 const NODES = '#nodes';
33
34 static protected function add_assets(\Brickrouge\Document $document)
35 {
36 parent::add_assets($document);
37
38 $document->css->add(\Icybee\ASSETS . 'css/admin-menu.css');
39 }
40
41 public function __construct(array $attributes=array())
42 {
43 parent::__construct('div', $attributes + [
44
45 'id' => 'icybee-admin-menu'
46
47 ]);
48 }
49
50 protected function lazy_get_translator()
51 {
52 global $core;
53
54 $user = $core->user;
55 $translator = new Proxi();
56
57 if ($user->language)
58 {
59 $translator->language = $user->language;
60 }
61
62 return $translator;
63 }
64
65 66 67
68 public function render()
69 {
70 global $core;
71
72 if (!$core->user_id || $core->user instanceof \Icybee\Modules\Members\Member)
73 {
74 return '';
75 }
76
77 return parent::render();
78 }
79
80 protected function render_inner_html()
81 {
82 global $core;
83
84 $page = $core->request->context->page;
85 $edit_target = $page->node ?: $page;
86
87 if (!$edit_target)
88 {
89
90
91
92
93
94 throw new ElementIsEmpty();
95 }
96
97 $translator = $this->translator;
98 $user = $core->user;
99
100
101
102 $html = $this->render_header($translator, $user, $edit_target);
103
104
105
106 $nodes = $this[self::NODES];
107
108 if ($nodes)
109 {
110 if ($edit_target)
111 {
112 unset($nodes[$edit_target->nid]);
113 }
114
115 $html .= $this->render_panel_nodes($nodes, $translator, $user, $edit_target);
116 }
117
118
119
120 $html .= $this->render_panel_config($translator);
121
122
123
124 if (!$html)
125 {
126 throw new ElementIsEmpty();
127 }
128
129 $admin_path = Routing\contextualize('/admin/');
130
131 return <<<EOT
132 <div class="panel-title"><a href="$admin_path">Icybee</a></div>
133 <div class="contents">$html</div>
134 EOT;
135 }
136
137 protected function (Proxi $translator, User $user, $edit_target)
138 {
139 global $core;
140
141 $html = '<ul style="text-align: center;"><li>';
142
143 if ($user->has_permission(Module::PERMISSION_MAINTAIN, $edit_target->constructor))
144 {
145 $href = Routing\contextualize('/admin/' . $edit_target->constructor . '/' . $edit_target->nid . '/edit');
146 $title = $translator('Edit: !title', [ 'title' => $edit_target->title ]);
147 $label = $translator('Edit');
148
149 $html .= <<<EOT
150 <a href="$href" title="$title">$label</a> –
151 EOT;
152 }
153
154 $html .= ' <a href="' . Operation::encode('users/logout', [ 'location' => $_SERVER['REQUEST_URI'] ]) . '">' . $translator('Disconnect') . '</a> –';
155 $html .= ' <a href="' . Routing\contextualize('/admin/') . '">' . $translator('Admin') . '</a></li>';
156 $html .= '</ul>';
157
158 return $html;
159 }
160
161 protected function render_panel_config(Proxi $translator)
162 {
163 global $core;
164
165 $links = [];
166
167 $routes = $core->routes;
168 $site = $core->site;
169
170 foreach ($core->modules as $module_id => $module)
171 {
172 $id = "admin:$module_id/config";
173
174 if (empty($routes[$id]))
175 {
176 continue;
177 }
178
179 $href = \ICanBoogie\escape(Routing\contextualize($routes[$id]));
180
181 $label = $translator($module->flat_id, [], [
182
183 'scope' => 'module_title',
184 'default' => $module->title
185
186 ]);
187
188 $links[] = <<<EOT
189 <a href="$href">$label</a>
190 EOT;
191 }
192
193 if (!$links)
194 {
195 return;
196 }
197
198 $links = implode('</li><li>', $links);
199
200 return <<<EOT
201 <div class="panel-section-title">Configurer</div>
202 <ul><li>$links</li></ul>
203 EOT;
204 }
205
206 protected function render_panel_nodes(array $nodes, Proxi $translator, User $user)
207 {
208 global $core;
209
210 $editables_by_category = [];
211 $descriptors = $core->modules->descriptors;
212
213 $translator->scope = 'module_category';
214
215 foreach ($nodes as $node)
216 {
217 if (!$user->has_permission(Module::PERMISSION_MAINTAIN, $node->constructor))
218 {
219 continue;
220 }
221
222
223
224
225 $category = isset($descriptors[$node->constructor][Module::T_CATEGORY]) ? $descriptors[$node->constructor][Module::T_CATEGORY] : 'contents';
226 $category = $translator($category);
227
228 $editables_by_category[$category][] = $node;
229 }
230
231 $translator->scope = null;
232 $html = '';
233
234 foreach ($editables_by_category as $category => $nodes)
235 {
236 $html .= '<div class="panel-section-title">' . \ICanBoogie\escape($category) . '</div>';
237 $html .= '<ul>';
238
239 foreach ($nodes as $node)
240 {
241 $url = Routing\contextualize('/admin/' . $node->constructor . '/' . $node->nid . '/edit');
242 $title = $translator->__invoke('Edit: !title', array('!title' => $node->title));
243 $label = \ICanBoogie\escape(\ICanBoogie\shorten($node->title));
244
245 $html .= <<<EOT
246 <li><a href="$url" title="$title">$label</a></li>
247 EOT;
248 }
249
250 $html .= '</ul>';
251 }
252
253 return $html;
254 }
255 }