1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Users\Roles;
13
14 use ICanBoogie\I18n;
15 use ICanBoogie\Operation;
16 use ICanBoogie\Route;
17 use ICanBoogie\Routes;
18
19 use Brickrouge\A;
20 use Brickrouge\Button;
21 use Brickrouge\Element;
22 use Brickrouge\Form;
23
24 class ManageBlock extends Form
25 {
26 static protected function add_assets(\Brickrouge\Document $document)
27 {
28 parent::add_assets($document);
29
30 $document->css->add(\Icybee\ASSETS . 'css/manage.css', -170);
31 $document->css->add('manage.css');
32 }
33
34 public function __construct(Module $module, array $attributes=array())
35 {
36 global $core;
37
38 $this->module = $module;
39
40 $actions = null;
41
42 if ($core->user->has_permission(Module::PERMISSION_ADMINISTER, $module))
43 {
44 $actions = new Button
45 (
46 'Save permissions', array
47 (
48 'class' => 'btn-primary',
49 'type' => 'submit',
50 'value' => Module::OPERATION_PERMISSIONS
51 )
52 );
53 }
54
55 parent::__construct
56 (
57 $attributes + array
58 (
59 self::ACTIONS => $actions,
60 self::HIDDENS => array
61 (
62 Operation::DESTINATION => $module->id,
63 Operation::NAME => Module::OPERATION_PERMISSIONS
64 ),
65
66 'class' => 'form-primary',
67 'name' => 'roles/manage'
68 )
69 );
70 }
71
72 public function render()
73 {
74 global $core;
75
76 $packages = array();
77 $modules = array();
78
79 foreach ($core->modules->descriptors as $m_id => $descriptor)
80 {
81 if (!isset($core->modules[$m_id]))
82 {
83 continue;
84 }
85
86 $name = isset($descriptor[Module::T_TITLE]) ? $descriptor[Module::T_TITLE] : $m_id;
87
88 if (isset($descriptor[Module::T_PERMISSION]))
89 {
90 if ($descriptor[Module::T_PERMISSION] != Module::PERMISSION_NONE)
91 {
92 $name .= ' <em>(';
93 $name .= Module::$levels[$descriptor[Module::T_PERMISSION]];
94 $name .= ')</em>';
95 }
96 else if (empty($descriptor[Module::T_PERMISSIONS]))
97 {
98 continue;
99 }
100 }
101
102 if (isset($descriptor[Module::T_CATEGORY]))
103 {
104 $package = $descriptor[Module::T_CATEGORY];
105 }
106 else
107 {
108 list($package) = explode('.', $m_id);
109 }
110
111 $package = I18n\t($package, array(), array('scope' => 'module_category', 'default' => $package));
112
113 $packages[$package][I18n\t($name)] = array_merge
114 (
115 $descriptor, array
116 (
117 Module::T_ID => $m_id
118 )
119 );
120 }
121
122 uksort($packages, 'ICanBoogie\unaccent_compare_ci');
123
124 $packages = array_merge
125 (
126 array
127 (
128 I18n\t('General') => array
129 (
130 I18n\t('All') => array(Module::T_ID => 'all')
131 )
132 ),
133
134 $packages
135 );
136
137
138
139
140
141 $roles = $this->module->model->all;
142
143
144
145
146
147 $rc = '';
148
149
150
151 $rc .= '<div class="listview"><table class="manage" cellpadding="4" cellspacing="0">';
152
153
154
155
156
157 $span = 1;
158 $context = $core->site->path;
159
160 $rc .= '<thead>';
161 $rc .= '<tr>';
162 $rc .= '<th> </th>';
163
164 foreach ($roles as $role)
165 {
166 $span++;
167
168 $rc .= '<th><div>';
169
170 if ($role->rid == 0)
171 {
172 $rc .= $role->title;
173 }
174 else
175 {
176 $rc .= new Element
177 (
178 'a', array
179 (
180 Element::INNER_HTML => $role->name,
181 'href' => $context . '/admin/' . $this->module . '/' . $role->rid . '/edit',
182 'title' => I18n\t('Edit entry')
183 )
184 );
185 }
186
187 $rc .= '</div></th>';
188 }
189
190 $rc .= '</tr>';
191 $rc .= '</thead>';
192
193 if (1)
194 {
195 $n = 0;
196 $actions_rows = '';
197
198 foreach ($roles as $role)
199 {
200 $actions_rows .= '<td>';
201
202 if ($role->rid == 1 || $role->rid == 2)
203 {
204 $actions_rows .= ' ';
205 }
206 else
207 {
208 ++$n;
209
210 $actions_rows .= new A
211 (
212 I18n\t('Delete', array(), array('scope' => 'button')), \ICanBoogie\Routing\contextualize('/admin/users.roles/' . $role->rid . '/delete'), array
213 (
214 'class' => 'btn btn-danger'
215 )
216 );
217 }
218
219 $actions_rows .= '</td>';
220 }
221
222 if ($n)
223 {
224 $rc .= <<<EOT
225 <tfoot>
226 <tr class="footer">
227 <td>
228 <div class="jobs">
229 <a class="operation-delete" href="#" rel="op-delete">Delete the selected entries</a>
230 </div>
231 </td>
232
233 $actions_rows
234
235 </tr>
236 </tfoot>
237 EOT;
238 }
239 }
240
241 $rc .= '<tbody>';
242
243
244
245
246
247
248 $role_options = array();
249
250 foreach (Module::$levels as $i => $level)
251 {
252 $role_options[$i] = I18n\t('permission.' . $level, array(), array('default' => $level));
253 }
254
255 $user_has_access = $core->user->has_permission(Module::PERMISSION_ADMINISTER, $this->module);
256 $routes = \ICanBoogie\Routes::get();
257
258 foreach ($packages as $p_name => $modules)
259 {
260 $rc .= '<tr class="listview-divider">';
261 $rc .= '<td colspan="' . $span . '">';
262 $rc .= $p_name;
263 $rc .= '</td>';
264 $rc .= '</tr>';
265
266 $n = 0;
267
268
269
270
271
272 uksort($modules, 'ICanBoogie\unaccent_compare_ci');
273
274 foreach ($modules as $m_name => $m_desc)
275 {
276 $m_id = $m_desc[Module::T_ID];
277 $flat_id = strtr($m_id, '.', '_');
278
279
280 $rc .= '<tr class="admin">';
281
282 $rc .= '<td>';
283 $rc .= $routes->find('/admin/' . $m_id) ? '<a href="' . $context . '/admin/' . $m_id . '">' . $m_name . '</a>' : $m_name;
284 $rc .= '</td>';
285
286 foreach ($roles as $role)
287 {
288 $rc .= '<td>';
289
290 if (isset($m_desc[Module::T_PERMISSION]))
291 {
292 if ($m_desc[Module::T_PERMISSION] != Module::PERMISSION_NONE)
293 {
294 $level = $m_desc[Module::T_PERMISSION];
295
296 $rc .= new Element
297 (
298 Element::TYPE_CHECKBOX, array
299 (
300 'name' => 'roles[' . $role->rid . '][' . $m_id . ']',
301 'checked' => isset($role->levels[$m_id]) && ($role->levels[$m_id] = $level)
302 )
303 );
304 }
305 else
306 {
307 $rc .= ' ';
308 }
309 }
310 else
311 {
312 if ($user_has_access)
313 {
314 $options = $role_options;
315
316 if ($m_id != 'all')
317 {
318 $options = array('inherit' => '') + $options;
319 }
320
321 $rc .= new Element
322 (
323 'select', array
324 (
325 Element::OPTIONS => $options,
326
327 'name' => 'roles[' . $role->rid . '][' . $m_id . ']',
328 'value' => isset($role->perms[$m_id]) ? $role->perms[$m_id] : null
329 )
330 );
331 }
332 else
333 {
334 $level = isset($role->perms[$m_id]) ? $role->perms[$m_id] : null;
335
336 if ($level)
337 {
338 $rc .= Module::$levels[$level];
339 }
340 else
341 {
342 $rc .= ' ';
343 }
344 }
345 }
346
347 $rc .= '</td>';
348 }
349
350 $rc .= '</tr>';
351
352
353
354
355
356
357
358 if (empty($m_desc[Module::T_PERMISSIONS]))
359 {
360 continue;
361 }
362
363 $perms = $m_desc[Module::T_PERMISSIONS];
364
365 foreach ($perms as $pname)
366 {
367 $columns = '';
368
369 foreach ($roles as $role)
370 {
371 $columns .= '<td>' . new Element
372 (
373 Element::TYPE_CHECKBOX, array
374 (
375 'name' => $user_has_access ? 'roles[' . $role->rid . '][' . $pname . ']' : NULL,
376 'checked' => $role->has_permission($pname)
377 )
378 )
379 . '</td>';
380 }
381
382 $label = I18n\t($pname, array(), array('scope' => array($flat_id, 'permission')));
383
384 $rc .= <<<EOT
385 <tr class="perm">
386 <td><span title="$pname">$label</span></td>
387 $columns
388 </tr>
389 EOT;
390 }
391 }
392 }
393
394 $rc .= '</tbody>';
395 $rc .= '</table></div>';
396
397 $this->inner_html = $rc;
398
399 return parent::render();
400 }
401 }