1 | 2 | simandl | <?php |
2 | | | |
3 | | | namespace Phem\Controllers; |
4 | | | |
5 | | | use Doctrine\Common\Annotations\AnnotationReader; |
6 | | | use Phem\Core\Collection; |
7 | | | use Phem\Environment\Application; |
8 | | | use Phem\Environment\EnvironmentManager; |
9 | | | use Phem\Environment\ResourceType; |
10 | | | use Phem\Libraries\Security\Model\MySQL\User; |
11 | | | use Phem\Libraries\UI\Annotations\ContentAttr; |
12 | | | use Phem\Libraries\UI\Annotations\DomAttr; |
13 | | | use Phem\Libraries\UI\Annotations\DomPath; |
14 | | | use Phem\Libraries\UI\Annotations\StyleAttr; |
15 | | | use Phem\Libraries\UI\Model\EventResult; |
16 | | | use Phem\Libraries\UI\Model\ScopeType; |
17 | | | use Phem\Libraries\UI\Model\Workspace; |
18 | | | use Phem\Views\SimpleView; |
19 | | | use ReflectionClass; |
20 | | | use ReflectionProperty; |
21 | | | use stdClass; |
22 | | | |
23 | | | abstract class WorkspaceViewController extends Controller |
24 | | | { |
25 | | | |
26 | | | protected $renderTemplate = true; |
27 | | | private $workspace; |
28 | | | private $connectionId; |
29 | | | |
30 | | | public function getWorkspace() |
31 | | | { |
32 | | | return $this->workspace; |
33 | | | } |
34 | | | |
35 | | | public function setWorkspace($workspace) |
36 | | | { |
37 | | | $this->workspace = $workspace; |
38 | | | } |
39 | | | |
40 | | | public function execute($task) |
41 | | | { |
42 | | | parent::execute($task); |
43 | | | |
44 | | | $this->connectionId = (int) EnvironmentManager::getRequestVar("connectionId", 0); |
45 | | | |
46 | | | $template = "@Phem/UI/Workspace.twig"; |
47 | | | |
48 | | | $classPathArray = explode('\\', get_class($this)); |
49 | | | $classShortName = end($classPathArray); |
50 | | | |
51 | | | $controllerName = str_replace("Controller", "", $classShortName); |
52 | | | |
53 | | | $model = new Collection(); |
54 | | | $model->put("styles", EnvironmentManager::getResourceManager() |
55 | | | ->getResourcePathCollection(ResourceType::CSS)); |
56 | | | $model->put("scripts", EnvironmentManager::getResourceManager() |
57 | | | ->getResourcePathCollection(ResourceType::JS)); |
58 | | | $model->put("controller",$controllerName); |
59 | | | $model->put("task",$task); |
60 | | | $model->put("requestUrl", $_SERVER["REQUEST_URI"]); |
61 | | | $model->put("httpHost", $_SERVER["HTTP_HOST"]); |
62 | | | $model->put("workspace", $this->workspace); |
63 | | | |
64 | | | if ($this->loggedUser == "guest") |
65 | | | { |
66 | | | $user = new User(); |
67 | | | $user->setUsername($this->loggedUser); |
68 | | | $model->put("loggedUser", $user); |
69 | | | } |
70 | | | else if ($this->loggedUser != null) |
71 | | | { |
72 | | | $user = EnvironmentManager::getUserManager()->getUser($this->loggedUser); |
73 | | | $model->put("loggedUser", $user); |
74 | | | } |
75 | | | |
76 | | | $model->put("sessionId", EnvironmentManager::getSession()->getId()); |
77 | | | |
78 | | | $lb = EnvironmentManager::getLinkBuilder(); |
79 | | | $onEventLink = $lb->navigate($controllerName, "onEvent"); |
80 | | | $onStrongEventLink = $lb->navigate($controllerName, "onStrongEvent"); |
81 | | | |
82 | | | $model->put("onEventLink", $onEventLink); |
83 | | | $model->put("onStrongEventLink", $onStrongEventLink); |
84 | | | |
85 | | | |
86 | | | if ($this->renderTemplate) |
87 | | | { |
88 | | | $view = new SimpleView($template, $model); |
89 | | | $view->display(); |
90 | | | } |
91 | | | } |
92 | | | |
93 | | | private function findInnerElement($elem, $name) |
94 | | | { |
95 | | | if (isset($elem->id) && $elem->id == $name) |
96 | | | { |
97 | | | return $elem; |
98 | | | } |
99 | | | else if (isset($elem->_children)) |
100 | | | { |
101 | | | foreach ($elem->_children as $key => $child) |
102 | | | { |
103 | | | return $this->findInnerElement($elem->_children[$key], $name); |
104 | | | } |
105 | | | } |
106 | | | } |
107 | | | |
108 | | | public function onStrongEvent($name,$value,$event,$invokedBy) |
109 | | | { |
110 | | | $this->renderTemplate = false; |
111 | | | |
112 | | | //wkspc = EnvironmentManager::getSession()->getVar("phem.workspace"); |
113 | | | |
114 | | | $wkspc = $this->loadWorkspace("myWkspc", "\PhemDemo\Models\UI\MainWorkspace", $invokedBy); |
115 | | | |
116 | | | if (!($wkspc instanceof Workspace)) |
117 | | | { |
118 | | | //TODO: if wkspc is null |
119 | | | return; |
120 | | | } |
121 | | | |
122 | | | $wkspc->changes = new Collection(); |
123 | | | |
124 | | | $control = $wkspc->findControl($name); |
125 | | | |
126 | | | $src = json_decode(html_entity_decode($value)); |
127 | | | |
128 | | | $reflectionClass = new ReflectionClass($control); |
129 | | | $props = $reflectionClass->getProperties(); |
130 | | | |
131 | | | |
132 | | | foreach ($props as $prop) |
133 | | | { |
134 | | | $reflectionProperty = new ReflectionProperty(get_class($control),$prop->getName()); |
135 | | | $annotationReader = new AnnotationReader(); |
136 | | | $propAnnotations = $annotationReader |
137 | | | ->getPropertyAnnotations($reflectionProperty); |
138 | | | |
139 | | | |
140 | | | if (count($propAnnotations)<1) continue; |
141 | | | |
142 | | | $path = ""; |
143 | | | $srcElem = $src; |
144 | | | foreach ($propAnnotations as $annotation) |
145 | | | { |
146 | | | if ($annotation instanceof DomPath) |
147 | | | { |
148 | | | $path.=$annotation->path; |
149 | | | } |
150 | | | } |
151 | | | |
152 | | | if ($path != "") |
153 | | | { |
154 | | | $srcElem = $this->findInnerElement($src, $name. "_".$path); |
155 | | | } |
156 | | | |
157 | | | foreach ($propAnnotations as $annotation) |
158 | | | { |
159 | | | if ($annotation instanceof DomAttr) |
160 | | | { |
161 | | | $setterName = "set" . ucfirst($prop->getName()); |
162 | | | $propName = $annotation->name; |
163 | | | $control->$setterName($srcElem->$propName); |
164 | | | } |
165 | | | else if ($annotation instanceof ContentAttr) |
166 | | | { |
167 | | | $setterName = "set" . ucfirst($prop->getName()); |
168 | | | $control->$setterName($srcElem->_content); |
169 | | | } |
170 | | | else if ($annotation instanceof StyleAttr) |
171 | | | { |
172 | | | $styles = new Collection(); |
173 | | | $stylePairs = str_getcsv($srcElem->style,";"); |
174 | | | foreach ($stylePairs as $pair) |
175 | | | { |
176 | | | if ($pair != "") |
177 | | | { |
178 | | | $pairArr = explode(":", $pair); |
179 | | | $styles->put(trim($pairArr[0]), ($pairArr[1])); |
180 | | | } |
181 | | | } |
182 | | | |
183 | | | $setterName = "set" . ucfirst($prop->getName()); |
184 | | | $control->$setterName($styles->get($annotation->name)); |
185 | | | |
186 | | | } |
187 | | | } |
188 | | | |
189 | | | } |
190 | | | |
191 | | | $callbackCollection = ucfirst($event); |
192 | | | |
193 | | | foreach ($control->$callbackCollection() as $callback) |
194 | | | { |
195 | | | call_user_func($callback,$control); |
196 | | | } |
197 | | | |
198 | | | $wkspc->setCurrentVersion($wkspc->getCurrentVersion()+1); |
199 | | | |
200 | | | //EnvironmentManager::getSession() |
201 | | | // ->setVar("pehm.workspace", $wkspc); |
202 | | | |
203 | | | $this->persistWorkspace($wkspc); |
204 | | | |
205 | | | $result = new EventResult($wkspc->getChanges(), |
206 | | | $wkspc->getCurrentVersion()); |
207 | | | |
208 | | | $result->setInvokedBy($invokedBy); |
209 | | | |
210 | | | //echo json_encode($result); |
211 | | | Application::notifyRaw($result); |
212 | | | |
213 | | | |
214 | | | } |
215 | | | |
216 | | | public function onEvent($name,$value,$event,$invokedBy) |
217 | | | { |
218 | | | $this->renderTemplate = false; |
219 | | | |
220 | | | //$wkspc = EnvironmentManager::getSession()->getVar("phem.workspace"); |
221 | | | |
222 | | | $wkspc = $this->loadWorkspace("myWkspc", "\PhemDemo\Models\UI\MainWorkspace", $invokedBy); |
223 | | | |
224 | | | if (!($wkspc instanceof Workspace)) |
225 | | | { |
226 | | | //TODO: if wkspc is null |
227 | | | return; |
228 | | | } |
229 | | | |
230 | | | $wkspc->changes = new Collection(); |
231 | | | |
232 | | | $control = $wkspc->findControl($name); |
233 | | | $control->setValue($value); |
234 | | | |
235 | | | $callbackCollection = ucfirst($event); |
236 | | | |
237 | | | foreach ($control->$callbackCollection() as $callback) |
238 | | | { |
239 | | | call_user_func($callback,$control); |
240 | | | } |
241 | | | |
242 | | | $wkspc->setCurrentVersion($wkspc->getCurrentVersion()+1); |
243 | | | |
244 | | | //EnvironmentManager::getSession() |
245 | | | // ->setVar("pehm.workspace", $wkspc); |
246 | | | |
247 | | | $this->persistWorkspace($wkspc); |
248 | | | |
249 | | | $result = new EventResult($wkspc->getChanges(), |
250 | | | $wkspc->getCurrentVersion()); |
251 | | | |
252 | | | $result->setInvokedBy($invokedBy); |
253 | | | |
254 | | | //echo json_encode($result); |
255 | | | Application::notifyRaw($result); |
256 | | | } |
257 | | | |
258 | | | protected function loadWorkspace($name, $className, $connectionId = null) |
259 | | | { |
260 | | | $applicationWorkspaces = Application::getVar("workspaces"); |
261 | | | |
262 | | | if ($applicationWorkspaces != null) |
263 | | | { |
264 | | | foreach ($applicationWorkspaces as $wkspc) |
265 | | | { |
266 | | | /* @var $wkspc Workspace */ |
267 | | | if (($wkspc->getName() == $name)&&("\\".get_class($wkspc) == $className)) |
268 | | | { |
269 | | | return $wkspc; |
270 | | | } |
271 | | | } |
272 | | | } |
273 | | | |
274 | | | $users = Application::getVar("users"); |
275 | | | |
276 | | | $thisUser = null; |
277 | | | |
278 | | | if ($users != null) |
279 | | | { |
280 | | | foreach ($users as $userName => $user) |
281 | | | { |
282 | | | if ($userName == $this->loggedUser) |
283 | | | { |
284 | | | $thisUser = $user; |
285 | | | } |
286 | | | } |
287 | | | } |
288 | | | |
289 | | | if ($thisUser != null) |
290 | | | { |
291 | | | if ($user->workspaces != null) |
292 | | | { |
293 | | | foreach ($user->workspaces as $wkspc) |
294 | | | { |
295 | | | /* @var $wkspc Workspace */ |
296 | | | if (($wkspc->getName() == $name)&&("\\".get_class($wkspc) == $className)) |
297 | | | { |
298 | | | return $wkspc; |
299 | | | } |
300 | | | } |
301 | | | } |
302 | | | } |
303 | | | |
304 | | | $sessionWorkspaces = EnvironmentManager::getSession()->getVar("workspaces"); |
305 | | | |
306 | | | if ($sessionWorkspaces != null) |
307 | | | { |
308 | | | foreach ($sessionWorkspaces as $wkspc) |
309 | | | { |
310 | | | /* @var $wkspc Workspace */ |
311 | | | if (($wkspc->getName() == $name)&&("\\".get_class($wkspc) == $className)) |
312 | | | { |
313 | | | return $wkspc; |
314 | | | } |
315 | | | } |
316 | | | } |
317 | | | |
318 | | | $thisConnection = null; |
319 | | | if ($connectionId != null) |
320 | | | { |
321 | | | $connections = EnvironmentManager::getSession()->getVar("connections"); |
322 | | | |
323 | | | if ($connections != null) |
324 | | | { |
325 | | | foreach ($connections as $id => $connection) |
326 | | | { |
327 | | | if ($id == $connectionId) |
328 | | | { |
329 | | | $thisConnection = $connection; |
330 | | | } |
331 | | | } |
332 | | | } |
333 | | | } |
334 | | | |
335 | | | if ($thisConnection != null) |
336 | | | { |
337 | | | foreach ($thisConnection->workspaces as $wkspc) |
338 | | | { |
339 | | | /* @var $wkspc Workspace */ |
340 | | | if (($wkspc->getName() == $name)&&("\\".get_class($wkspc) == $className)) |
341 | | | { |
342 | | | return $wkspc; |
343 | | | } |
344 | | | } |
345 | | | } |
346 | | | |
347 | | | $wkspc = new $className(); |
348 | | | $wkspc->setName($name); |
349 | | | $this->persistWorkspace($wkspc); |
350 | | | return $wkspc; |
351 | | | |
352 | | | } |
353 | | | |
354 | | | private function persistWorkspace(Workspace $wkspc) |
355 | | | { |
356 | | | switch ($wkspc->getScope()) |
357 | | | { |
358 | | | case ScopeType::APPLICATION: |
359 | | | $lock = Application::lockVarsWait(); |
360 | | | $workspaces = Application::getVar("workspaces"); |
361 | | | if ($workspaces == null) { $workspaces = new Collection(); } |
362 | | | $workspaces->set($wkspc->getName(),$wkspc); |
363 | | | Application::setVar("workspaces",$workspaces); |
364 | | | $lock->unlock(); |
365 | | | break; |
366 | | | case ScopeType::USER: |
367 | | | $lock = Application::lockVarsWait(); |
368 | | | $users = Application::getVar("users"); |
369 | | | if ($users == null) |
370 | | | { |
371 | | | $users = new Collection(); |
372 | | | } |
373 | | | |
374 | | | $foundUser = null; |
375 | | | foreach ($users as $userName => $user) |
376 | | | { |
377 | | | if ($userName == $this->loggedUser) |
378 | | | { |
379 | | | $foundUser = $user; |
380 | | | } |
381 | | | } |
382 | | | |
383 | | | if ($foundUser == null) |
384 | | | { |
385 | | | $foundUser = new stdClass(); |
386 | | | $users->set($this->loggedUser,$foundUser); |
387 | | | } |
388 | | | |
389 | | | if ($foundUser->workspaces == null) |
390 | | | { |
391 | | | $foundUser->workspaces = new Collection(); |
392 | | | } |
393 | | | |
394 | | | $foundUser->workspaces->set($wkspc->getName(),$wkspc); |
395 | | | |
396 | | | Application::setVar("users",$foundUser); |
397 | | | $lock->unlock(); |
398 | | | break; |
399 | | | default: |
400 | | | case ScopeType::SESSION: |
401 | | | $workspaces = EnvironmentManager::getSession()->getVar("workspaces"); |
402 | | | |
403 | | | if ($workspaces == null) |
404 | | | { |
405 | | | $workspaces = new Collection(); |
406 | | | } |
407 | | | $workspaces->set($wkspc->getName(),$wkspc); |
408 | | | |
409 | | | EnvironmentManager::getSession()->setVar("workspaces",$workspaces); |
410 | | | |
411 | | | break; |
412 | | | case ScopeType::CONNECTION: |
413 | | | $connections = EnvironmentManager::getSession()->getVar("connections"); |
414 | | | if ($connections == null) |
415 | | | { |
416 | | | $connections = new Collection(); |
417 | | | } |
418 | | | |
419 | | | $foundConn = null; |
420 | | | foreach ($connections as $connId => $connection) |
421 | | | { |
422 | | | if ($connId == $this->connectionId) |
423 | | | { |
424 | | | $foundConn = $connection; |
425 | | | } |
426 | | | } |
427 | | | |
428 | | | if ($foundConn == null) |
429 | | | { |
430 | | | $foundConn = new stdClass(); |
431 | | | $connections->set($this->connectionId,$connection); |
432 | | | } |
433 | | | |
434 | | | if ($foundConn->workspaces == null) |
435 | | | { |
436 | | | $foundConn->workspaces = new Collection(); |
437 | | | } |
438 | | | |
439 | | | $foundConn->workspaces->set($wkspc->getName(),$wkspc); |
440 | | | |
441 | | | EnvironmentManager::getSession()->setVar("users",$foundUser); |
442 | | | break; |
443 | | | } |
444 | | | } |
445 | | | |
446 | | | } |