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\Button;
22 use Brickrouge\DropdownMenu;
23 use Brickrouge\Element;
24 use Brickrouge\SplitButton;
25
26 class ActionbarTitle extends Element
27 {
28 public function __construct(array $attributes=array())
29 {
30 parent::__construct('div', $attributes + array('class' => 'actionbar-title'));
31 }
32
33 protected function render_inner_html()
34 {
35 global $core;
36
37 if ($core->user->is_guest || $core->user instanceof Member)
38 {
39 return '<h1>Icybee</h1>';
40 }
41
42 $request = $core->request;
43
44 try
45 {
46 $route = $request->route;
47 }
48 catch (PropertyNotDefined $e)
49 {
50 $route = null;
51 }
52
53 if (!$route || !$route->module)
54 {
55 throw new \Brickrouge\ElementIsEmpty;
56 }
57
58 $label = $core->modules[$route->module]->title;
59
60 $btn_group = null;
61 $options = $this->collect_options($route);
62
63 if ($options)
64 {
65 $menu = (string) new DropdownMenu
66 (
67 array
68 (
69 DropdownMenu::OPTIONS => $options,
70
71 'value' => $request->path
72 )
73 );
74
75 $btn_group = <<<EOT
76 <div class="btn-group">
77 <div class="dropdown-toggle" data-toggle="dropdown">
78 <span class="caret"></span>
79 </div>
80 $menu
81 </div>
82 EOT;
83 }
84
85 $label = \Brickrouge\escape($label);
86 $url = \Brickrouge\escape(\ICanBoogie\Routing\contextualize('/admin/' . $route->module));
87
88 return <<<EOT
89 <h1><a href="$url">$label</a></h1>
90 $btn_group
91 EOT;
92 }
93
94 protected function collect_options(Route $route)
95 {
96 global $core;
97
98 $options = array();
99
100 $user = $core->user;
101 $module_id = $route->module;
102 $routes = Routes::get();
103
104
105
106
107
108 $list_url = $core->site->resolve_view_url("$module_id/list");
109
110 if ($list_url{0} != '#')
111 {
112 $options[$list_url] = new A(I18n\t("List page on the website"), $list_url);
113 }
114
115
116
117
118
119 if ($user->has_permission(Module::PERMISSION_ADMINISTER, $module_id))
120 {
121 $config_pattern = "/admin/$module_id/config";
122
123 if ($route->pattern != $config_pattern)
124 {
125 $r = $routes->find($config_pattern);
126
127 if ($r)
128 {
129 $url = \ICanBoogie\Routing\contextualize((string) $r->pattern);
130
131 if ($options)
132 {
133 $options[] = false;
134 }
135
136 $options[$url] = new A
137 (
138 I18n\t('Configuration', array(), array('scope' => 'block_title')), $url
139 );
140 }
141 }
142 }
143
144
145
146
147
148 $modules = $core->modules;
149 $descriptors = $modules->descriptors;
150 $category = $descriptors[$module_id][Module::T_CATEGORY];
151
152 $options_routes = array();
153
154 foreach ($routes as $r_id => $r)
155 {
156 if (empty($r['index']) || empty($r['module']))
157 {
158 continue;
159 }
160
161 $r_module_id = $r['module'];
162
163 if (!isset($modules[$r_module_id]) || $descriptors[$r_module_id][Module::T_CATEGORY] != $category)
164 {
165 continue;
166 }
167
168 $permission = isset($r['permission']) ? $r['permission'] : Module::PERMISSION_ACCESS;
169
170 if (!$user->has_permission($permission, $r_module_id))
171 {
172 continue;
173 }
174
175 $url = \ICanBoogie\Routing\contextualize($r['pattern']);
176 $module_flat_id = strtr($r_module_id, '.', '_');
177
178 $options_routes[$url] = new A
179 (
180 I18n\t($module_flat_id, array(), array('scope' => 'module_title', 'default' => $descriptors[$r_module_id][Module::T_TITLE])), $url
181 );
182 }
183
184 if ($options_routes)
185 {
186 if ($options)
187 {
188 $options[] = false;
189 }
190
191 $options = array_merge($options, $options_routes);
192 }
193
194 return $options;
195 }
196 }