1 | 2 | simandl | <?php |
2 | | | |
3 | | | namespace Phem\Controllers; |
4 | | | |
5 | | | use Phem\Controllers\Controller; |
6 | | | use Phem\Core\Collection; |
7 | | | use Phem\Environment\EnvironmentManager; |
8 | | | use Phem\Environment\ResourceType; |
9 | | | use Phem\Libraries\Security\Model\MySQL\User; |
10 | | | use Phem\Views\SimpleView; |
11 | | | |
12 | | | /** |
13 | | | * |
14 | | | * @author kubapet |
15 | | | */ |
16 | | | abstract class SimpleViewController extends Controller |
17 | | | { |
18 | | | |
19 | | | /** @var Collection */ |
20 | | | protected $model; |
21 | | | protected $template; |
22 | | | |
23 | | | /** @var Boolean */ |
24 | | | protected $renderTemplate = true; |
25 | | | private $view; |
26 | | | |
27 | | | function __construct() |
28 | | | { |
29 | | | parent::__construct(); |
30 | | | $this->model = new Collection(); |
31 | | | } |
32 | | | |
33 | | | public function getModel() |
34 | | | { |
35 | | | return $this->model; |
36 | | | } |
37 | | | |
38 | | | public function setModel($model) |
39 | | | { |
40 | | | $this->model = $model; |
41 | | | } |
42 | | | |
43 | | | public function getTemplate() |
44 | | | { |
45 | | | return $this->template; |
46 | | | } |
47 | | | |
48 | | | public function setTemplate($template) |
49 | | | { |
50 | | | $this->template = $template; |
51 | | | } |
52 | | | |
53 | | | public function getRenderTemplate() |
54 | | | { |
55 | | | return $this->renderTemplate; |
56 | | | } |
57 | | | |
58 | | | public function setRenderTemplate($renderTemplate) |
59 | | | { |
60 | | | $this->renderTemplate = $renderTemplate; |
61 | | | } |
62 | | | |
63 | | | function execute($task) |
64 | | | { |
65 | | | parent::execute($task); |
66 | | | if (($this->redirectToController != null) || ($this->redirectToTask != null)) |
67 | | | { |
68 | | | return; |
69 | | | } |
70 | | | |
71 | | | |
72 | | | if ($task == null) |
73 | | | { |
74 | | | $task = $this->defaultTask; |
75 | | | } |
76 | | | |
77 | | | $classPathArray = explode('\\', get_class($this)); |
78 | | | $classShortName = end($classPathArray); |
79 | | | |
80 | | | if ($this->template == null) |
81 | | | { |
82 | | | $this->template = str_replace("Controller", "", $classShortName) . DS . $task . ".twig"; |
83 | | | } |
84 | | | |
85 | | | |
86 | | | if ($this->loggedUser == "guest") |
87 | | | { |
88 | | | $user = new User(); |
89 | | | $user->setUsername($this->loggedUser); |
90 | | | $this->model->put("loggedUser", $user); |
91 | | | } |
92 | | | else if ($this->loggedUser != null) |
93 | | | { |
94 | | | $user = EnvironmentManager::getUserManager()->getUser($this->loggedUser); |
95 | | | $this->model->put("loggedUser", $user); |
96 | | | } |
97 | | | |
98 | | | |
99 | | | $this->model->put("styles", EnvironmentManager::getResourceManager() |
100 | | | ->getResourcePathCollection(ResourceType::CSS)); |
101 | | | $this->model->put("scripts", EnvironmentManager::getResourceManager() |
102 | | | ->getResourcePathCollection(ResourceType::JS)); |
103 | | | $this->model->put("controller", str_replace("Controller", "", $classShortName)); |
104 | | | $this->model->put("task", $task); |
105 | | | $this->model->put("requestUrl", $_SERVER["REQUEST_URI"]); |
106 | | | $this->model->put("lb", EnvironmentManager::getLinkBuilder()); |
107 | | | $this->model->put("httpHost", $_SERVER["HTTP_HOST"]); |
108 | | | $this->model->put("pathPrefix", SITE_SUBDIR); |
109 | | | $this->model->put("sessionId", EnvironmentManager::getSession()->getId()); |
110 | | | |
111 | | | if ($this->renderTemplate) |
112 | | | { |
113 | | | $this->view = new SimpleView($this->template, $this->model); |
114 | | | $this->view->display(); |
115 | | | } |
116 | | | } |
117 | | | |
118 | | | } |