|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Webcms2 admin module package. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace AdminModule; |
|
8
|
|
|
|
|
9
|
|
|
use Nette; |
|
10
|
|
|
use Kdyby\BootstrapFormRenderer\BootstrapRenderer; |
|
11
|
|
|
use Nette\Application\UI; |
|
12
|
|
|
use Monolog\Logger; |
|
13
|
|
|
use Monolog\Handler\StreamHandler; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Base class for all application presenters. |
|
17
|
|
|
* |
|
18
|
|
|
* @property-read string $name |
|
19
|
|
|
* @property-read string $action |
|
20
|
|
|
* |
|
21
|
|
|
* @author Tomáš Voslař <tomas.voslar at webcook.cz> |
|
22
|
|
|
* @package WebCMS2 |
|
23
|
|
|
*/ |
|
24
|
|
|
class BasePresenter extends \WebCMS2\Common\BasePresenter |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var Doctrine\ORM\EntityManager */ |
|
27
|
|
|
protected $em; |
|
28
|
|
|
|
|
29
|
|
|
/* @var \WebCMS\Translation\Translation */ |
|
30
|
|
|
public $translation; |
|
31
|
|
|
|
|
32
|
|
|
/* @var \WebCMS\Translation\Translator */ |
|
33
|
|
|
public $translator; |
|
34
|
|
|
|
|
35
|
|
|
/* @var Nette\Http\SessionSection */ |
|
36
|
|
|
public $state; |
|
37
|
|
|
|
|
38
|
|
|
/* @var User */ |
|
39
|
|
|
public $systemUser; |
|
40
|
|
|
|
|
41
|
|
|
/* @var \WebCMS\Settings */ |
|
42
|
|
|
public $settings; |
|
43
|
|
|
|
|
44
|
|
|
/* @var Page */ |
|
45
|
|
|
public $actualPage; |
|
46
|
|
|
|
|
47
|
|
|
/* @var \WebCMS\Helpers\PriceFormatter */ |
|
48
|
|
|
public $priceFormatter; |
|
49
|
|
|
|
|
50
|
|
|
/* Method is executed before render. */ |
|
51
|
35 |
|
protected function beforeRender() |
|
52
|
|
|
{ |
|
53
|
35 |
|
$this->setLayout("layout"); |
|
54
|
|
|
|
|
55
|
35 |
|
$this->setBasePathModule(); |
|
56
|
|
|
|
|
57
|
35 |
|
if ($this->isAjax()) { |
|
58
|
|
|
$this->invalidateControl('flashMessages'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// boxes settings, only if page is module |
|
62
|
35 |
|
if ($this->getParam('id')) { |
|
|
|
|
|
|
63
|
1 |
|
$this->template->boxesSettings = true; |
|
|
|
|
|
|
64
|
1 |
|
} else { |
|
65
|
34 |
|
$this->template->boxesSettings = false; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// check filled info email |
|
69
|
35 |
|
$infoEmail = $this->settings->get('Info email', \WebCMS\Settings::SECTION_BASIC)->getValue(); |
|
70
|
35 |
|
if (!Nette\Utils\Validators::isEmail($infoEmail)) { |
|
71
|
35 |
|
$this->flashMessage('Please fill in correct info email. Settings -> Basic settings.', 'warning'); |
|
72
|
35 |
|
} |
|
73
|
|
|
|
|
74
|
35 |
|
if (!$this->isAjax()) { |
|
75
|
35 |
|
$this->template->structures = $this->getStructures(); // TODO add function for AJAX and normal request it is not necessary to load everything |
|
|
|
|
|
|
76
|
35 |
|
} |
|
77
|
|
|
|
|
78
|
35 |
|
$this->template->registerHelperLoader('\WebCMS\Helpers\SystemHelper::loader'); |
|
|
|
|
|
|
79
|
35 |
|
$this->template->actualPage = $this->actualPage; |
|
|
|
|
|
|
80
|
35 |
|
$this->template->user = $this->getUser(); |
|
|
|
|
|
|
81
|
35 |
|
$this->template->setTranslator($this->translator); |
|
|
|
|
|
|
82
|
35 |
|
$this->template->language = $this->state->language; |
|
|
|
|
|
|
83
|
35 |
|
$this->template->version = \WebCMS\Helpers\SystemHelper::getVersion(); |
|
|
|
|
|
|
84
|
35 |
|
$this->template->activePresenter = $this->getPresenter()->getName(); |
|
|
|
|
|
|
85
|
35 |
|
$this->template->settings = $this->settings; |
|
|
|
|
|
|
86
|
35 |
|
$this->template->languages = $this->em->getRepository('WebCMS\Entity\Language')->findAll(); |
|
|
|
|
|
|
87
|
35 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* |
|
91
|
|
|
*/ |
|
92
|
41 |
|
private function processLanguage() |
|
93
|
|
|
{ |
|
94
|
41 |
|
$this->state = $this->getSession('admin'); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
// changing language |
|
97
|
41 |
|
if ($this->getParameter('language_id_change')) { |
|
98
|
|
|
$this->state->language = $this->em->find('WebCMS\Entity\Language', $this->getParameter('language_id_change')); |
|
99
|
|
|
$this->forward(':Admin:Homepage:default'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
41 |
|
if (!isset($this->state->language)) { |
|
103
|
1 |
|
$this->state->language = $this->em->getRepository('WebCMS\Entity\Language')->findOneBy(array( |
|
104
|
1 |
|
'defaultBackend' => 1, |
|
105
|
1 |
|
)); |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
41 |
|
$language = $this->em->find('WebCMS\Entity\Language', $this->state->language->getId()); |
|
109
|
|
|
// check whether is language still in db |
|
110
|
41 |
|
if (!$language) { |
|
111
|
|
|
unset($this->state->language); |
|
112
|
|
|
$this->forward('Homepage:default'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// reload entity from db |
|
116
|
41 |
|
$this->state->language = $this->em->find('WebCMS\Entity\Language', $this->state->language->getId()); |
|
117
|
|
|
|
|
118
|
41 |
|
\WebCMS\Helpers\PriceFormatter::setLocale($this->state->language->getLocale()); |
|
119
|
|
|
|
|
120
|
|
|
// translations |
|
121
|
41 |
|
$default = $this->em->getRepository('WebCMS\Entity\Language')->findOneBy(array( |
|
122
|
41 |
|
'defaultBackend' => 1, |
|
123
|
41 |
|
)); |
|
124
|
|
|
|
|
125
|
41 |
|
$translation = new \WebCMS\Translation\Translation($this->em, $default, 1); |
|
126
|
41 |
|
$this->translation = $translation->getTranslations(); |
|
127
|
41 |
|
$this->translator = new \WebCMS\Translation\Translator($this->translation); |
|
128
|
41 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/* Startup method. */ |
|
131
|
41 |
|
protected function startup() |
|
132
|
|
|
{ |
|
133
|
41 |
|
parent::startup(); |
|
134
|
|
|
|
|
135
|
41 |
|
if (!$this->getUser()->isLoggedIn() && $this->presenter->getName() !== "Admin:Login") { |
|
136
|
|
|
$this->forward(':Admin:Login:'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
41 |
|
$this->processLanguage(); |
|
140
|
|
|
|
|
141
|
|
|
// system settings |
|
142
|
41 |
|
$this->settings = new \WebCMS\Settings($this->em, $this->state->language); |
|
143
|
41 |
|
$this->settings->setSettings($this->getSettings()); |
|
144
|
|
|
|
|
145
|
|
|
// system helper sets variables |
|
146
|
41 |
|
\WebCMS\Helpers\SystemHelper::setVariables(array( |
|
147
|
41 |
|
'baseUrl' => $this->presenter->getHttpRequest()->url->baseUrl, |
|
148
|
41 |
|
'infoEmail' => $this->settings->get('Info email', 'basic')->getValue(), |
|
149
|
41 |
|
)); |
|
150
|
|
|
|
|
151
|
|
|
// price formatting |
|
152
|
41 |
|
$this->priceFormatter = new \WebCMS\Helpers\PriceFormatter($this->state->language->getLocale()); |
|
153
|
|
|
|
|
154
|
41 |
|
$id = $this->getParam('idPage'); |
|
|
|
|
|
|
155
|
41 |
|
if ($id) { |
|
156
|
|
|
$this->actualPage = $this->em->find('WebCMS\Entity\Page', $id); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
41 |
|
$this->checkPermission(); |
|
160
|
|
|
|
|
161
|
41 |
|
$this->logAction(); |
|
162
|
41 |
|
} |
|
163
|
|
|
|
|
164
|
41 |
|
private function logAction() |
|
165
|
|
|
{ |
|
166
|
|
|
// Create the logger |
|
167
|
41 |
|
$logger = new Logger('History'); |
|
168
|
|
|
// Now add some handlers |
|
169
|
41 |
|
$parameters = $this->getContext()->getParameters(); |
|
170
|
|
|
|
|
171
|
41 |
|
$logger->pushHandler(new StreamHandler($parameters['tempDir'].'/../log/webcms.log', Logger::DEBUG)); |
|
172
|
|
|
|
|
173
|
|
|
$data = array( |
|
174
|
41 |
|
'user' => $this->getUser()->getIdentity() ? $this->getUser()->getIdentity()->getData()['username'] : 'unknown', |
|
175
|
41 |
|
'action' => $this->getAction(), |
|
176
|
41 |
|
'presenter' => $this->getName(), |
|
177
|
41 |
|
'title' => is_object($this->actualPage) ? $this->actualPage->getTitle() : 'System', |
|
178
|
41 |
|
'url' => $this->getHttpRequest()->url->absoluteUrl, |
|
179
|
41 |
|
); |
|
180
|
|
|
|
|
181
|
41 |
|
if ($this->getName() === 'Admin:Homepage' || $this->getName() === 'Admin:Login') { |
|
182
|
5 |
|
$logger->addNotice('Login', $data); |
|
183
|
5 |
|
} else { |
|
184
|
36 |
|
$logger->addInfo('Request catcher', $data); |
|
185
|
|
|
} |
|
186
|
41 |
|
} |
|
187
|
|
|
|
|
188
|
41 |
|
protected function getLanguageId() |
|
189
|
|
|
{ |
|
190
|
41 |
|
return $this->state->language->getId(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
8 |
|
protected function createSettingsForm($settings) |
|
194
|
|
|
{ |
|
195
|
8 |
|
$form = $this->createForm(); |
|
196
|
|
|
|
|
197
|
8 |
|
if (!$settings) { |
|
198
|
1 |
|
return $form; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
7 |
|
foreach ($settings as $s) { |
|
202
|
7 |
|
$form = $this->createFormElement($form, $s); |
|
203
|
7 |
|
} |
|
204
|
|
|
|
|
205
|
7 |
|
$form->addSubmit('submit', 'Save settings'); |
|
206
|
7 |
|
$form->onSuccess[] = callback($this, 'settingsFormSubmitted'); |
|
207
|
|
|
|
|
208
|
7 |
|
return $form; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
7 |
|
private function createFormElement($form, $setting) |
|
212
|
|
|
{ |
|
213
|
7 |
|
if (!is_null($setting->getLanguage())) { |
|
214
|
7 |
|
$ident = $this->em->getRepository('WebCMS\Entity\Setting')->findOneBy(array( |
|
215
|
7 |
|
'key' => $setting->getKey(), |
|
216
|
7 |
|
'section' => $setting->getSection(), |
|
217
|
7 |
|
'language' => $this->state->language->getId() |
|
218
|
7 |
|
)); |
|
219
|
7 |
|
} else { |
|
220
|
5 |
|
$ident = $setting; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
7 |
|
switch ($setting->getType()) { |
|
224
|
7 |
|
case 'textarea': |
|
225
|
1 |
|
$form->addTextArea($ident->getId(), $setting->getKey())->setDefaultValue($ident->getValue())->setAttribute('class', 'editor'); |
|
226
|
1 |
|
break; |
|
227
|
7 |
|
case 'textarea-plain': |
|
228
|
2 |
|
$form->addTextArea($ident->getId(), $setting->getKey())->setDefaultValue($ident->getValue()); |
|
229
|
2 |
|
break; |
|
230
|
7 |
|
case 'radio': |
|
231
|
1 |
|
$form->addRadioList($ident->getId(), $setting->getKey(), $setting->getOptions())->setDefaultValue($ident->getValue()); |
|
232
|
1 |
|
break; |
|
233
|
7 |
|
case 'select': |
|
234
|
2 |
|
$form->addSelect($ident->getId(), $setting->getKey(), $setting->getOptions())->setDefaultValue($ident->getValue()); |
|
235
|
2 |
|
break; |
|
236
|
7 |
|
case 'checkbox': |
|
237
|
2 |
|
$form->addCheckbox($ident->getId(), $setting->getKey())->setDefaultValue($ident->getValue()); |
|
238
|
2 |
|
break; |
|
239
|
7 |
|
case 'checkbox-toggle': |
|
240
|
2 |
|
$form->addCheckbox($ident->getId(), $setting->getKey())->setDefaultValue($ident->getValue())->setAttribute('data-toggle', 'toggle'); |
|
241
|
2 |
|
break; |
|
242
|
5 |
|
default: |
|
243
|
5 |
|
$form->addText($ident->getId(), $setting->getKey())->setDefaultValue($ident->getValue())->setAttribute('class', 'form-control'); |
|
244
|
5 |
|
break; |
|
245
|
7 |
|
} |
|
246
|
|
|
|
|
247
|
7 |
|
return $form; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public function settingsFormSubmitted(\Nette\Application\UI\Form $form) |
|
251
|
|
|
{ |
|
252
|
|
|
$values = $form->getValues(); |
|
253
|
|
|
|
|
254
|
|
|
foreach ($values as $id => $v) { |
|
255
|
|
|
$setting = $this->em->find('WebCMS\Entity\Setting', $id); |
|
256
|
|
|
$setting->setValue($v); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
$this->em->flush(); |
|
260
|
|
|
|
|
261
|
|
|
$this->flashMessage('Settings has been saved.', 'success'); |
|
262
|
|
|
$this->forward('this'); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/* Invalidate ajax content. */ |
|
266
|
|
|
|
|
267
|
26 |
|
protected function reloadContent() |
|
268
|
|
|
{ |
|
269
|
26 |
|
if ($this->isAjax()) { |
|
270
|
|
|
$this->invalidateControl('content'); |
|
271
|
|
|
} |
|
272
|
26 |
|
} |
|
273
|
|
|
|
|
274
|
|
|
/* Invalidate ajax modal content. */ |
|
275
|
|
|
|
|
276
|
6 |
|
protected function reloadModalContent() |
|
277
|
|
|
{ |
|
278
|
6 |
|
if ($this->isAjax()) { |
|
279
|
|
|
$this->invalidateControl('modalContent'); |
|
280
|
|
|
} |
|
281
|
6 |
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Creates default basic grid. |
|
285
|
|
|
* @param Nette\Application\UI\Presenter $presenter |
|
286
|
|
|
* @param String $name |
|
287
|
|
|
* @param String $entity |
|
288
|
|
|
* @param string[] $where |
|
289
|
|
|
* @return \Grido\Grid |
|
290
|
|
|
*/ |
|
291
|
7 |
|
public function createGrid(Nette\Application\UI\Presenter $presenter, $name, $entity, $order = null, $where = null) |
|
292
|
|
|
{ |
|
293
|
5 |
|
$grid = new \Grido\Grid($presenter, $name); |
|
294
|
|
|
|
|
295
|
5 |
|
$qb = $this->em->createQueryBuilder(); |
|
296
|
|
|
|
|
297
|
5 |
|
if ($order) { |
|
298
|
1 |
|
foreach ($order as $o) { |
|
299
|
1 |
|
$qb->addOrderBy('l.'.$o['by'], $o['dir']); |
|
300
|
1 |
|
} |
|
301
|
2 |
|
} |
|
302
|
|
|
|
|
303
|
5 |
|
if ($where) { |
|
304
|
3 |
|
foreach ($where as $w) { |
|
305
|
3 |
|
$qb->andWhere('l.'.$w); |
|
306
|
3 |
|
} |
|
307
|
3 |
|
} |
|
308
|
|
|
|
|
309
|
5 |
|
if (strpos($entity, 'WebCMS') === false) { |
|
310
|
5 |
|
$grid->setModel($qb->select('l')->from("WebCMS\Entity\\$entity", 'l')); |
|
311
|
5 |
|
} else { |
|
312
|
|
|
$grid->setModel($qb->select('l')->from($entity, 'l')); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
7 |
|
$grid->setRememberState(true); |
|
316
|
5 |
|
$grid->setDefaultPerPage(10); |
|
317
|
5 |
|
$grid->setTranslator($this->translator); |
|
318
|
5 |
|
$grid->setFilterRenderType(\Grido\Components\Filters\Filter::RENDER_INNER); |
|
319
|
|
|
|
|
320
|
5 |
|
return $grid; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Creates form and rewrite renderer for bootstrap. |
|
325
|
|
|
* @return UI\Form |
|
326
|
|
|
*/ |
|
327
|
18 |
|
public function createForm() |
|
328
|
|
|
{ |
|
329
|
18 |
|
$form = new Nette\Application\UI\Form(); |
|
330
|
|
|
|
|
331
|
18 |
|
$form->getElementPrototype()->addAttributes(array('class' => 'ajax')); |
|
332
|
18 |
|
$form->setTranslator($this->translator); |
|
333
|
18 |
|
$form->setRenderer(new BootstrapRenderer()); |
|
334
|
|
|
|
|
335
|
18 |
|
return $form; |
|
336
|
1 |
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Injects entity manager. |
|
340
|
|
|
* @param \Doctrine\ORM\EntityManager $em |
|
341
|
|
|
* @return BasePresenter |
|
342
|
|
|
* @throws \Nette\InvalidStateException |
|
343
|
|
|
*/ |
|
344
|
41 |
|
public function injectEntityManager(\Doctrine\ORM\EntityManager $em) |
|
|
|
|
|
|
345
|
|
|
{ |
|
346
|
41 |
|
if ($this->em) { |
|
347
|
|
|
throw new \Nette\InvalidStateException('Entity manager has been already set.'); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
41 |
|
$this->em = $em; |
|
|
|
|
|
|
351
|
|
|
|
|
352
|
41 |
|
return $this; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
41 |
|
private function initAcl() |
|
356
|
|
|
{ |
|
357
|
|
|
// checking permission of user |
|
358
|
41 |
|
$acl = new Nette\Security\Permission(); |
|
359
|
|
|
|
|
360
|
|
|
// roles |
|
361
|
41 |
|
$roles = $this->em->getRepository("WebCMS\Entity\Role")->findAll(); |
|
362
|
|
|
|
|
363
|
41 |
|
$acl->addRole('guest'); |
|
364
|
41 |
|
foreach ($roles as $r) { |
|
365
|
41 |
|
$acl->addRole($r->getName()); |
|
366
|
41 |
|
} |
|
367
|
|
|
|
|
368
|
|
|
// load resources |
|
369
|
41 |
|
$resources = $this->initPagesResources(); |
|
370
|
|
|
|
|
371
|
|
|
// add all resources to acl |
|
372
|
41 |
|
$acl->addResource('admin:Homepage'); |
|
373
|
41 |
|
$acl->addResource('admin:Login'); |
|
374
|
41 |
|
foreach ($resources as $key => $r) { |
|
375
|
41 |
|
$acl->addResource($key); |
|
376
|
41 |
|
} |
|
377
|
|
|
|
|
378
|
41 |
|
return $acl; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
41 |
|
private function initPagesResources() |
|
382
|
|
|
{ |
|
383
|
41 |
|
$resources = \WebCMS\Helpers\SystemHelper::getResources(); |
|
384
|
|
|
|
|
385
|
|
|
// pages resources |
|
386
|
41 |
|
$pages = $this->em->getRepository('WebCMS\Entity\Page')->findAll(); |
|
387
|
|
|
|
|
388
|
41 |
|
foreach ($pages as $page) { |
|
389
|
|
|
if ($page->getParent() != NULL) { |
|
390
|
|
|
$module = $this->createObject($page->getModuleName()); |
|
391
|
|
|
|
|
392
|
|
|
foreach ($module->getPresenters() as $presenter) { |
|
393
|
|
|
$key = 'admin:'.$page->getModuleName().''.$presenter['name'].$page->getId(); |
|
394
|
|
|
$resources[$key] = $page->getTitle(); |
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
41 |
|
} |
|
398
|
|
|
|
|
399
|
41 |
|
return $resources; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
/** |
|
403
|
|
|
* @param Nette\Security\IIdentity|null $identity |
|
404
|
|
|
*/ |
|
405
|
41 |
|
private function initPermissions($identity) |
|
406
|
|
|
{ |
|
407
|
41 |
|
if (is_object($identity)) { |
|
408
|
41 |
|
$permissions = $identity->data['permissions']; |
|
|
|
|
|
|
409
|
41 |
|
} else { |
|
410
|
|
|
$permissions = array(); |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
41 |
|
return $permissions; |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
41 |
|
private function getPageResource() |
|
417
|
|
|
{ |
|
418
|
41 |
|
if (substr_count(lcfirst($this->name), ':') == 2) { |
|
419
|
|
|
$resource = \WebCMS\Helpers\SystemHelper::strlReplace(':', '', lcfirst($this->name).$this->getParameter('idPage')); |
|
420
|
|
|
} else { |
|
421
|
41 |
|
$resource = lcfirst($this->name); |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
41 |
|
return $resource; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
/** |
|
428
|
|
|
* @param Nette\Security\Permission $acl |
|
429
|
|
|
*/ |
|
430
|
41 |
|
private function checkRights($acl, $roles) |
|
431
|
|
|
{ |
|
432
|
41 |
|
$resource = $this->getPageResource(); |
|
433
|
|
|
|
|
434
|
41 |
|
$hasRigths = false; |
|
435
|
41 |
|
foreach ($roles as $role) { |
|
436
|
41 |
|
$check = $acl->isAllowed($role, $resource, $this->action); |
|
437
|
|
|
|
|
438
|
41 |
|
if ($check) { |
|
439
|
41 |
|
$hasRigths = true; |
|
440
|
41 |
|
} |
|
441
|
41 |
|
} |
|
442
|
|
|
|
|
443
|
41 |
|
return $hasRigths; |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* Checks user permission. |
|
448
|
|
|
* |
|
449
|
|
|
* @return void |
|
450
|
|
|
*/ |
|
451
|
41 |
|
private function checkPermission() |
|
452
|
|
|
{ |
|
453
|
|
|
// creates system acl |
|
454
|
41 |
|
$acl = $this->initAcl(); |
|
455
|
|
|
|
|
456
|
41 |
|
$identity = $this->getUser()->getIdentity(); |
|
457
|
41 |
|
$permissions = $this->initPermissions($identity); |
|
458
|
41 |
|
foreach ($permissions as $key => $p) { |
|
459
|
|
|
if ($p && $acl->hasResource($key)) { |
|
460
|
|
|
$acl->allow($identity->roles[0], $key, Nette\Security\Permission::ALL); |
|
|
|
|
|
|
461
|
|
|
} |
|
462
|
41 |
|
} |
|
463
|
|
|
|
|
464
|
|
|
// homepage and login page can access everyone |
|
465
|
41 |
|
$acl->allow(Nette\Security\Permission::ALL, 'admin:Homepage', Nette\Security\Permission::ALL); |
|
466
|
41 |
|
$acl->allow(Nette\Security\Permission::ALL, 'admin:Login', Nette\Security\Permission::ALL); |
|
467
|
|
|
|
|
468
|
|
|
// superadmin has access to everywhere |
|
469
|
41 |
|
$acl->allow('superadmin', Nette\Security\Permission::ALL, Nette\Security\Permission::ALL); |
|
470
|
|
|
|
|
471
|
41 |
|
$roles = $this->getUser()->getRoles(); |
|
472
|
|
|
|
|
473
|
41 |
|
if (!$this->checkRights($acl, $roles)) { |
|
474
|
|
|
$this->presenter->flashMessage($this->translation['You do not have a permission to do this operation!'], 'danger'); |
|
475
|
|
|
$this->redirect(":Admin:Homepage:"); |
|
476
|
|
|
} |
|
477
|
41 |
|
} |
|
478
|
|
|
|
|
479
|
35 |
|
private function getStructures() |
|
480
|
|
|
{ |
|
481
|
35 |
|
$qb = $this->em->createQueryBuilder(); |
|
482
|
|
|
|
|
483
|
35 |
|
$qb->addOrderBy('l.root', 'ASC'); |
|
484
|
35 |
|
$qb->andWhere('l.parent IS NULL'); |
|
485
|
35 |
|
$qb->andWhere('l.language = '.$this->state->language->getId()); |
|
486
|
|
|
|
|
487
|
35 |
|
return $qb->select('l')->from("WebCMS\Entity\Page", 'l')->getQuery()->getResult(); |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
/* SEO SETTINGS */ |
|
491
|
|
|
|
|
492
|
|
|
public function renderSeo($idPage) |
|
493
|
|
|
{ |
|
494
|
|
|
$this->reloadContent(); |
|
495
|
|
|
|
|
496
|
|
|
$this->template->actualPage = $this->actualPage; |
|
|
|
|
|
|
497
|
|
|
$this->template->idPage = $idPage; |
|
|
|
|
|
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
View Code Duplication |
public function createComponentSeoForm() |
|
|
|
|
|
|
501
|
|
|
{ |
|
502
|
|
|
$form = $this->createForm(); |
|
503
|
|
|
|
|
504
|
|
|
$form->addText('slug', 'Seo url')->setAttribute('class', 'form-control'); |
|
|
|
|
|
|
505
|
|
|
$form->addText('metaTitle', 'Seo title')->setAttribute('class', 'form-control'); |
|
|
|
|
|
|
506
|
|
|
$form->addText('metaKeywords', 'Seo keywords')->setAttribute('class', 'form-control'); |
|
|
|
|
|
|
507
|
|
|
$form->addText('metaDescription', 'Seo description')->setAttribute('class', 'form-control'); |
|
|
|
|
|
|
508
|
|
|
$form->addSubmit('send', 'Save')->setAttribute('class', 'btn btn-success'); |
|
|
|
|
|
|
509
|
|
|
$form->onSuccess[] = callback($this, 'seoFormSubmitted'); |
|
510
|
|
|
|
|
511
|
|
|
$form->setDefaults($this->actualPage->toArray()); |
|
512
|
|
|
|
|
513
|
|
|
return $form; |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
|
|
public function seoFormSubmitted($form) |
|
517
|
|
|
{ |
|
518
|
|
|
$values = $form->getValues(); |
|
519
|
|
|
|
|
520
|
|
|
if (empty($values->slug)) { |
|
521
|
|
|
$this->actualPage->setSlug(null); |
|
522
|
|
|
} else { |
|
523
|
|
|
$this->actualPage->setSlug($values->slug); |
|
524
|
|
|
} |
|
525
|
|
|
$this->actualPage->setMetaTitle($values->metaTitle); |
|
526
|
|
|
$this->actualPage->setMetaKeywords($values->metaKeywords); |
|
527
|
|
|
$this->actualPage->setMetaDescription($values->metaDescription); |
|
528
|
|
|
|
|
529
|
|
|
$path = $this->em->getRepository('WebCMS\Entity\Page')->getPath($this->actualPage); |
|
530
|
|
|
$final = array(); |
|
531
|
|
|
foreach ($path as $p) { |
|
532
|
|
|
if ($p->getParent() != NULL) { |
|
533
|
|
|
$final[] = $p->getSlug(); |
|
534
|
|
|
} |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
$this->actualPage->setPath(implode('/', $final)); |
|
538
|
|
|
|
|
539
|
|
|
$this->em->flush(); |
|
540
|
|
|
|
|
541
|
|
|
$this->flashMessage('Seo settings has been saved.', 'success'); |
|
542
|
|
|
$this->forward('this'); |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
/* BOXES SETTINGS */ |
|
546
|
|
|
|
|
547
|
1 |
|
public function renderBoxes($idPage) |
|
548
|
|
|
{ |
|
549
|
|
|
$this->reloadContent(); |
|
550
|
|
|
|
|
551
|
|
|
$parameters = $this->getContext()->container->getParameters(); |
|
552
|
|
|
$boxes = $parameters['boxes']; |
|
553
|
|
|
|
|
554
|
|
|
/* |
|
555
|
|
|
foreach ($boxes as &$box) { |
|
556
|
|
|
//$box['component'] = $id . '-' . $box['presenter'] . '-' . $box['function']; |
|
557
|
|
|
}*/ |
|
558
|
|
|
if (!is_array($boxes)) { |
|
559
|
|
|
$boxes = array(); |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
|
|
$this->template->actualPage = $this->actualPage; |
|
|
|
|
|
|
563
|
|
|
$this->template->boxes = $boxes; |
|
|
|
|
|
|
564
|
|
|
$this->template->idPage = $idPage; |
|
|
|
|
|
|
565
|
1 |
|
} |
|
566
|
|
|
|
|
567
|
|
|
public function createComponentBoxesForm() |
|
568
|
|
|
{ |
|
569
|
|
|
$form = $this->createForm(); |
|
570
|
|
|
|
|
571
|
|
|
$parameters = $this->getContext()->container->getParameters(); |
|
572
|
|
|
$boxes = $parameters['boxes']; |
|
573
|
|
|
|
|
574
|
|
|
$pages = $this->em->getRepository('WebCMS\Entity\Page')->findBy(array( |
|
575
|
|
|
'language' => $this->state->language, |
|
576
|
|
|
)); |
|
577
|
|
|
|
|
578
|
|
|
$boxesAssoc = array(); |
|
579
|
|
View Code Duplication |
foreach ($pages as $page) { |
|
|
|
|
|
|
580
|
|
|
if ($page->getParent() != NULL) { |
|
581
|
|
|
$module = $this->createObject($page->getModuleName()); |
|
582
|
|
|
|
|
583
|
|
|
foreach ($module->getBoxes() as $box) { |
|
584
|
|
|
$boxesAssoc[$page->getId().'-'.$box['module'].'-'.$box['presenter'].'-'.$box['function']] = $page->getTitle().' - '.$this->translation[$box['name']]; |
|
585
|
|
|
} |
|
586
|
|
|
} |
|
587
|
|
|
} |
|
588
|
|
|
|
|
589
|
|
|
$boxesAssoc = array( |
|
590
|
|
|
0 => $this->translation['Box is not linked.'], |
|
591
|
|
|
) + $boxesAssoc; |
|
592
|
|
|
|
|
593
|
|
|
if (!empty($boxes)) { |
|
594
|
|
|
foreach ($boxes as $name => $active) { |
|
595
|
|
|
$form->addSelect($name, $name, $boxesAssoc) |
|
596
|
|
|
->setTranslator(null) |
|
597
|
|
|
->setAttribute('class', 'form-control'); |
|
|
|
|
|
|
598
|
|
|
} |
|
599
|
|
|
} |
|
600
|
|
|
|
|
601
|
|
|
// set defaults |
|
602
|
|
|
$boxes = $this->em->getRepository('WebCMS\Entity\Box')->findBy(array( |
|
603
|
|
|
'pageTo' => $this->actualPage, |
|
604
|
|
|
)); |
|
605
|
|
|
|
|
606
|
|
|
$defaults = array(); |
|
607
|
|
|
if (!empty($boxes)) { |
|
608
|
|
|
foreach ($boxes as $box) { |
|
609
|
|
|
if (is_object($box->getPageFrom())) { |
|
610
|
|
|
$defaults[$box->getBox()] = $box->getPageFrom()->getId().'-'.$box->getModuleName().'-'.$box->getPresenter().'-'.$box->getFunction(); |
|
611
|
|
|
} |
|
612
|
|
|
} |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
$form->setDefaults($defaults); |
|
616
|
|
|
$form->addSubmit('submit', 'Save'); |
|
617
|
|
|
$form->onSuccess[] = callback($this, 'boxesFormSubmitted'); |
|
618
|
|
|
|
|
619
|
|
|
return $form; |
|
620
|
|
|
} |
|
621
|
|
|
|
|
622
|
|
|
public function boxesFormSubmitted(UI\Form $form) |
|
623
|
|
|
{ |
|
624
|
|
|
$values = $form->getValues(); |
|
625
|
|
|
|
|
626
|
|
|
// delete old asscociations |
|
627
|
|
|
$q = $this->em->createQuery('delete from WebCMS\Entity\Box m where m.pageTo = '.$this->actualPage->getId()); |
|
628
|
|
|
$numDeleted = $q->execute(); |
|
|
|
|
|
|
629
|
|
|
|
|
630
|
|
|
// persist new associations |
|
631
|
|
|
foreach ($values as $key => $value) { |
|
632
|
|
|
if ($value) { |
|
633
|
|
|
$params = explode('-', $value); |
|
634
|
|
|
|
|
635
|
|
|
$pageFrom = $this->em->find('WebCMS\Entity\Page', $params[0]); |
|
636
|
|
|
|
|
637
|
|
|
$box = new \WebCMS\Entity\Box(); |
|
638
|
|
|
$box->setPageFrom($pageFrom); |
|
639
|
|
|
$box->setPageTo($this->actualPage); |
|
640
|
|
|
$box->setModuleName($params[1]); |
|
641
|
|
|
$box->setPresenter($params[2]); |
|
642
|
|
|
$box->setFunction($params[3]); |
|
643
|
|
|
$box->setBox($key); |
|
644
|
|
|
|
|
645
|
|
|
$this->em->persist($box); |
|
646
|
|
|
} |
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
$this->em->flush(); |
|
650
|
|
|
|
|
651
|
|
|
$this->flashMessage('Boxes settings has been saved.', 'success'); |
|
652
|
|
|
$this->forward('this'); |
|
653
|
|
|
} |
|
654
|
|
|
|
|
655
|
|
|
public function handleAddToFavourite($link, $title) |
|
656
|
|
|
{ |
|
657
|
|
|
$user = $this->em->getRepository('WebCMS\Entity\User')->find($this->getUser()->getId()); |
|
658
|
|
|
|
|
659
|
|
|
$fav = new \WebCMS\Entity\Favourites(); |
|
660
|
|
|
$fav->setLink($link); |
|
661
|
|
|
$fav->setUser($user); |
|
662
|
|
|
$fav->setTitle($title); |
|
663
|
|
|
|
|
664
|
|
|
$this->em->persist($fav); |
|
665
|
|
|
|
|
666
|
|
|
$this->em->flush(); |
|
667
|
|
|
|
|
668
|
|
|
$this->flashMessage('Link has been added to favourites.', 'success'); |
|
669
|
|
|
} |
|
670
|
|
|
|
|
671
|
|
|
public function handleGetTranslations() |
|
672
|
|
|
{ |
|
673
|
|
|
$payload = array(); |
|
674
|
|
|
foreach ($_GET['keys'] as $key => $value) { |
|
675
|
|
|
$payload[$key] = $this->translator->translate($key); |
|
676
|
|
|
} |
|
677
|
|
|
|
|
678
|
|
|
$this->sendResponse(new \Nette\Application\Responses\JsonResponse($payload)); |
|
679
|
|
|
} |
|
680
|
|
|
|
|
681
|
1 |
|
public function handleRemoveFromFavourite($idFav) |
|
682
|
|
|
{ |
|
683
|
|
|
$fav = $this->em->getRepository('WebCMS\Entity\Favourites')->find($idFav); |
|
684
|
|
|
|
|
685
|
|
|
$this->em->remove($fav); |
|
686
|
|
|
$this->em->flush(); |
|
687
|
|
|
|
|
688
|
|
|
$this->flashMessage('Link has been removed from favourites.', 'success'); |
|
689
|
1 |
|
} |
|
690
|
|
|
|
|
691
|
39 |
|
public function flashMessage($text, $type = 'info') |
|
692
|
|
|
{ |
|
693
|
39 |
|
parent::flashMessage($this->translation[$text], $type); |
|
694
|
39 |
|
} |
|
695
|
|
|
|
|
696
|
|
|
/** |
|
697
|
|
|
* Formats layout template file names. |
|
698
|
|
|
* @return array |
|
699
|
|
|
*/ |
|
700
|
30 |
|
public function formatLayoutTemplateFiles() |
|
701
|
|
|
{ |
|
702
|
30 |
|
$name = $this->getName(); |
|
703
|
30 |
|
$presenter = substr($name, strrpos(':'.$name, ':')); |
|
|
|
|
|
|
704
|
30 |
|
$layout = $this->layout ? $this->layout : 'layout'; |
|
|
|
|
|
|
705
|
30 |
|
$dir = dirname($this->getReflection()->getFileName()); |
|
706
|
30 |
|
$dir = is_dir("$dir/templates") ? $dir : dirname($dir); |
|
707
|
|
|
|
|
708
|
|
|
$list = array( |
|
709
|
30 |
|
APP_DIR."/../libs/webcms2/webcms2/AdminModule/templates/@$layout.latte", |
|
710
|
30 |
|
); |
|
711
|
|
|
|
|
712
|
|
View Code Duplication |
do { |
|
|
|
|
|
|
713
|
30 |
|
$list[] = "$dir/templates/@$layout.latte"; |
|
714
|
30 |
|
$list[] = "$dir/templates/@$layout.phtml"; |
|
715
|
30 |
|
$dir = dirname($dir); |
|
716
|
30 |
|
} while ($dir && ($name = substr($name, 0, strrpos($name, ':')))); |
|
717
|
|
|
|
|
718
|
30 |
|
return $list; |
|
719
|
|
|
} |
|
720
|
|
|
|
|
721
|
|
|
/** |
|
722
|
|
|
* Transfer collection into array. |
|
723
|
|
|
* |
|
724
|
|
|
* @param [type] $collection [description] |
|
|
|
|
|
|
725
|
|
|
* @param string $title [description] |
|
726
|
|
|
* |
|
727
|
|
|
* @return array [description] |
|
728
|
|
|
*/ |
|
729
|
|
|
public function collectionToArray($collection, $title = 'title') |
|
730
|
|
|
{ |
|
731
|
|
|
$array = array(); |
|
732
|
|
|
foreach ($collection as $item) { |
|
733
|
|
|
$getter = 'get'.ucfirst($title); |
|
734
|
|
|
$array[$item->getId()] = $item->$getter(); |
|
735
|
|
|
} |
|
736
|
|
|
|
|
737
|
|
|
return $array; |
|
738
|
|
|
} |
|
739
|
|
|
|
|
740
|
35 |
|
public function setBasePathModule() |
|
741
|
|
|
{ |
|
742
|
35 |
|
$this->template->basePathModule = __DIR__.'/../../'; |
|
|
|
|
|
|
743
|
35 |
|
} |
|
744
|
|
|
} |
|
745
|
|
|
|
This method has been deprecated.