1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee;
13
14 use ICanBoogie\HTTP\Request;
15 use ICanBoogie\Route;
16
17 use Brickrouge\Element;
18
19 20 21 22 23 24 25 26 27 28
29 class BlockController extends \ICanBoogie\Routing\Controller
30 {
31 const DECORATE_WITH_BLOCK = 1;
32 const DECORATE_WITH_ADMIN = 2;
33 const DECORATE_WITH_DOCUMENT = 4;
34
35 protected $decorate_flags;
36 protected $request;
37 protected $block_name;
38
39 public function __construct(Route $route)
40 {
41 $this->decorate_flags = self::DECORATE_WITH_BLOCK | self::DECORATE_WITH_ADMIN | self::DECORATE_WITH_DOCUMENT;
42
43 parent::__construct($route);
44 }
45
46 47 48
49 public function __invoke(Request $request)
50 {
51 $this->request = $request;
52 $this->control();
53
54 $flags = $request['decorate_flags'];
55
56 if ($flags === null)
57 {
58 $flags = $this->decorate_flags;
59 }
60
61 return $this->decorate($this->get_component(), $flags);
62 }
63
64 65 66 67 68 69
70 protected function control()
71 {
72 if (!$this->control_permission(Module::PERMISSION_ACCESS))
73 {
74 throw new \ICanBoogie\PermissionRequired();
75 }
76 }
77
78 protected function control_permission($permission)
79 {
80 global $core;
81
82 $route = $this->route;
83 $module = $core->modules[$route->module];
84
85 return $core->user->has_permission(Module::PERMISSION_ACCESS, $module);
86 }
87
88 89 90 91 92 93 94
95 protected function get_component()
96 {
97 global $core;
98
99 $route = $this->route;
100 $module = $core->modules[$route->module];
101 $args = [ $route->block ];
102
103 foreach ($this->request->path_params as $param => $value)
104 {
105 if (is_numeric($param))
106 {
107 $args[] = $value;
108 }
109 }
110
111 return call_user_func_array([ $module, 'getBlock' ], $args);
112 }
113
114 115 116 117 118 119 120 121
122 protected function decorate($component, $flags)
123 {
124 if ($flags & self::DECORATE_WITH_BLOCK)
125 {
126 $route = $this->route;
127 $component = $this->decorate_with_block($component);
128 }
129
130 if ($flags & self::DECORATE_WITH_ADMIN)
131 {
132 $component = $this->decorate_with_admin($component);
133 }
134
135 if ($flags & self::DECORATE_WITH_DOCUMENT)
136 {
137 $component = $this->decorate_with_document($component);
138 }
139
140 return $component;
141 }
142
143 144 145 146 147 148 149
150 protected function decorate_with_block($component)
151 {
152 $route = $this->route;
153
154 return new BlockDecorator($component, $route->block, $route->module);
155 }
156
157 158 159 160 161 162 163
164 protected function decorate_with_admin($component)
165 {
166 return new AdminDecorator($component);
167 }
168
169 170 171 172 173 174 175
176 protected function decorate_with_document($component)
177 {
178 return new DocumentDecorator($component);
179 }
180 }
181
182 183 184 185 186 187 188
189 class BlockDecorator extends \Brickrouge\Decorator
190 {
191 192 193 194 195
196 protected $block_name;
197
198 199 200 201 202
203 protected $module_id;
204
205 206 207 208 209 210 211
212 public function __construct($block, $block_name, $module_id)
213 {
214 $this->block_name = $block_name;
215 $this->module_id = $module_id;
216
217 parent::__construct($block);
218 }
219
220 public function render()
221 {
222 $normalized_block_name = \Brickrouge\normalize($this->block_name);
223 $normalized_module_id = \Brickrouge\normalize($this->module_id);
224
225 return new Element('div', [
226
227 Element::INNER_HTML => $this->component,
228
229 'class' => "block block--{$normalized_block_name} block--{$normalized_module_id}--{$normalized_block_name}"
230
231 ]);
232 }
233 }