1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AdminModule; |
4
|
|
|
|
5
|
|
|
use Nette\Application\UI; |
6
|
|
|
use Nette\Utils\Finder; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Admin presenter. |
10
|
|
|
* @author Tomáš Voslař <tomas.voslar at webcook.cz> |
11
|
|
|
* @package WebCMS2 |
12
|
|
|
*/ |
13
|
|
|
class PagesPresenter extends \AdminModule\BasePresenter |
14
|
|
|
{ |
15
|
|
|
/* @var Page */ |
16
|
|
|
|
17
|
|
|
private $page; |
18
|
|
|
|
19
|
|
|
private $repository; |
20
|
|
|
|
21
|
1 |
|
protected function beforeRender() |
22
|
|
|
{ |
23
|
1 |
|
parent::beforeRender(); |
24
|
1 |
|
} |
25
|
|
|
|
26
|
1 |
|
protected function startup() |
27
|
|
|
{ |
28
|
1 |
|
parent::startup(); |
29
|
|
|
|
30
|
1 |
|
$this->repository = $this->em->getRepository('WebCMS\Entity\Page'); |
31
|
1 |
|
} |
32
|
|
|
|
33
|
1 |
|
protected function createComponentPageForm() |
34
|
|
|
{ |
35
|
|
|
$repository = $this->em->getRepository('WebCMS\Entity\Page'); |
36
|
|
|
$hierarchy = $repository->getTreeForSelect(array( |
37
|
|
|
array('by' => 'root', 'dir' => 'ASC'), |
38
|
|
|
array('by' => 'lft', 'dir' => 'ASC'), |
39
|
|
|
), array( |
40
|
|
|
'language = '.$this->state->language->getId(), |
41
|
|
|
)); |
42
|
|
|
|
43
|
|
|
$hierarchy = array(0 => $this->translation['Pick parent']) + $hierarchy; |
44
|
|
|
|
45
|
|
|
// loads modules |
46
|
|
|
$modules = $this->em->getRepository('WebCMS\Entity\Module')->findAll(); |
47
|
|
|
$modulesToSelect = array(NULL => 'No module'); |
48
|
|
|
foreach ($modules as $module) { |
49
|
|
|
$objectModule = $this->createObject($module->getName()); |
50
|
|
|
|
51
|
|
|
foreach ($objectModule->getPresenters() as $presenter) { |
52
|
|
|
if ($presenter['frontend']) { |
53
|
|
|
$modulesToSelect[$module->getId().'-'.$presenter['name']] = $module->getName().' '.$presenter['name']; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$layouts = array(); |
59
|
|
|
foreach (Finder::findFiles('@*.latte')->in(APP_DIR.'/templates') as $key => $file) { |
60
|
|
|
$layouts[str_replace(array('@', '.latte'), '', $file->getFileName())] = $file->getFileName(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$form = $this->createForm(); |
64
|
|
|
$form->addText('title', 'Name')->setAttribute('class', 'form-control')->setRequired(); |
|
|
|
|
65
|
1 |
|
$form->addText('redirect', 'Redirect')->setAttribute('class', 'form-control'); |
|
|
|
|
66
|
|
|
$form->addText('class', 'Menu item class')->setAttribute('class', 'form-control'); |
|
|
|
|
67
|
|
|
$form->addSelect('module', 'Module')->setTranslator(null)->setItems($modulesToSelect)->setAttribute('class', 'form-control')->setRequired(); |
|
|
|
|
68
|
|
|
$form->addSelect('parent', 'Parent')->setTranslator(null)->setItems($hierarchy)->setAttribute('class', 'form-control'); |
|
|
|
|
69
|
|
|
$form->addSelect('layout', 'Page layout')->setTranslator(null)->setItems($layouts)->setAttribute('class', 'form-control'); |
|
|
|
|
70
|
|
|
$form->addCheckbox('default', 'Default'); |
71
|
|
|
$form->addCheckbox('visible', 'Show'); |
72
|
|
|
$form->addCheckbox('secured', 'Secured'); |
73
|
|
|
|
74
|
|
|
$form->addSubmit('save', 'Save')->setAttribute('class', 'btn btn-success'); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$form->onSuccess[] = callback($this, 'pageFormSubmitted'); |
77
|
|
|
|
78
|
1 |
|
if ($this->page) { |
79
|
1 |
|
$form->setDefaults($this->page->toArray()); |
80
|
1 |
|
if (is_object($this->page->getModule())) { |
81
|
1 |
|
$form['module']->setDefaultValue($this->page->getModule()->getId().'-'.$this->page->getPresenter()); |
82
|
1 |
|
} |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
return $form; |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
public function pageFormSubmitted(UI\Form $form) |
89
|
|
|
{ |
90
|
|
|
$values = $form->getValues(); |
91
|
|
|
|
92
|
|
|
$tmpBoxes = array(); |
93
|
|
|
if ($values->parent) { |
94
|
|
|
$parent = $this->em->find("WebCMS\Entity\Page", $values->parent); |
95
|
|
|
// copy boxes |
96
|
|
|
$tmpBoxes = $parent->getBoxes(); |
97
|
|
|
} else { |
98
|
|
|
$parent = null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->setPageValues($values, $parent); |
102
|
|
|
|
103
|
|
|
if (!$this->page->getId()) { |
104
|
|
|
$this->em->persist($this->page); |
105
|
|
|
|
106
|
|
|
$this->createBoxesFromParent($tmpBoxes); |
107
|
|
|
|
108
|
|
|
$this->em->flush(); |
109
|
|
|
$this->copyPermissions(); |
110
|
|
|
} else { |
111
|
|
|
$children = $this->page->getChildren(); |
112
|
|
|
|
113
|
|
|
foreach ($children as $child) { |
114
|
|
|
$this->generatePath($child); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// persist and generate path |
119
|
|
|
$this->em->flush(); |
120
|
|
|
$this->generatePath($this->page); |
121
|
|
|
$this->em->flush(); |
122
|
|
|
$this->generateSitemap(); |
123
|
|
|
|
124
|
|
|
$this->flashMessage('Page has been added.', 'success'); |
125
|
|
|
$this->forward('Pages:default'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* |
130
|
|
|
* |
131
|
|
|
* @param [type] $values [description] |
|
|
|
|
132
|
|
|
*/ |
133
|
1 |
|
private function setPageValues($values, $parent) |
134
|
|
|
{ |
135
|
|
|
if ($values->module) { |
136
|
|
|
$parse = explode('-', $values->module); |
137
|
|
|
$module = $this->em->find("WebCMS\Entity\Module", $parse[0]); |
138
|
|
|
$presenter = $parse[1]; |
139
|
|
|
} else { |
140
|
|
|
$module = null; |
141
|
|
|
$presenter = ''; |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
$this->page->setTitle($values->title); |
145
|
|
|
|
146
|
|
|
if (!empty($values->redirect)) { |
147
|
1 |
|
$this->page->setRedirect($values->redirect); |
148
|
|
|
} else { |
149
|
|
|
$this->page->setRedirect(null); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($module) { |
153
|
|
|
$this->page->setModuleName($module->getName()); |
154
|
|
|
} else { |
155
|
|
|
$this->page->setModuleName(''); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$this->page->setVisible($values->visible); |
159
|
|
|
$this->page->setDefault($values->default); |
160
|
|
|
$this->page->setSecured($values->secured); |
161
|
1 |
|
$this->page->setParent($parent); |
162
|
|
|
$this->page->setLanguage($this->state->language); |
163
|
1 |
|
$this->page->setModule($module); |
164
|
|
|
$this->page->setPresenter($presenter); |
165
|
|
|
$this->page->setPath('tmp value'); |
166
|
|
|
$this->page->setClass($values->class); |
167
|
|
|
$this->page->setLayout($values->layout); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* |
172
|
|
|
* |
173
|
|
|
* @param [type] $boxes [description] |
|
|
|
|
174
|
|
|
* @return [type] [description] |
|
|
|
|
175
|
|
|
*/ |
176
|
|
|
private function createBoxesFromParent($boxes) |
177
|
|
|
{ |
178
|
|
|
foreach ($boxes as $box) { |
179
|
|
|
$tmp = new \WebCMS\Entity\Box(); |
180
|
|
|
$tmp->setBox($box->getBox()); |
181
|
|
|
$tmp->setFunction($box->getFunction()); |
182
|
|
|
$tmp->setModuleName($box->getModuleName()); |
183
|
|
|
$tmp->setPresenter($box->getPresenter()); |
184
|
|
|
$tmp->setPageFrom($box->getPageFrom()); |
185
|
|
|
$tmp->setPageTo($this->page); |
186
|
|
|
|
187
|
|
|
$this->em->persist($tmp); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* |
193
|
|
|
* |
194
|
|
|
* @return [type] [description] |
|
|
|
|
195
|
|
|
*/ |
196
|
|
View Code Duplication |
private function generatePath($page) |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
$path = $this->em->getRepository('WebCMS\Entity\Page')->getPath($page); |
199
|
|
|
$final = array(); |
200
|
|
|
foreach ($path as $p) { |
201
|
|
|
if ($p->getParent() != NULL) { |
202
|
|
|
$final[] = $p->getSlug(); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$page->setPath(implode('/', $final)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* |
211
|
|
|
* |
212
|
|
|
* @return [type] [description] |
|
|
|
|
213
|
|
|
*/ |
214
|
|
|
private function copyPermissions() |
215
|
|
|
{ |
216
|
|
|
$roles = $this->em->getRepository('WebCMS\Entity\Role')->findBy(array( |
217
|
|
|
'automaticEnable' => true, |
218
|
|
|
)); |
219
|
|
|
|
220
|
|
|
foreach ($roles as $r) { |
221
|
|
|
$module = $this->createObject($this->page->getModuleName()); |
222
|
|
|
foreach ($module->getPresenters() as $presenter) { |
223
|
|
|
$permission = new \WebCMS\Entity\Permission(); |
224
|
|
|
|
225
|
|
|
$resource = 'admin:'.$this->page->getModuleName().$presenter['name'].$this->page->getId(); |
226
|
|
|
$permission->setResource($resource); |
227
|
|
|
$permission->setPage($this->page); |
228
|
|
|
$permission->setRead(true); |
229
|
|
|
|
230
|
|
|
$r->addPermission($permission); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
1 |
|
protected function createComponentPagesGrid($name) |
236
|
|
|
{ |
237
|
1 |
|
$parents = $this->em->getRepository('WebCMS\Entity\Page')->findBy(array( |
238
|
1 |
|
'parent' => NULL, |
239
|
1 |
|
'language' => $this->state->language->getId(), |
240
|
1 |
|
)); |
241
|
|
|
|
242
|
1 |
|
$prnts = array('' => $this->translation['Pick structure']); |
243
|
|
|
|
244
|
1 |
|
foreach ($parents as $p) { |
245
|
|
|
$prnts[$p->getId()] = $p->getTitle(); |
246
|
1 |
|
} |
247
|
|
|
|
248
|
1 |
|
$grid = $this->createGrid($this, $name, 'Page', array( |
249
|
1 |
|
array('by' => 'root', 'dir' => 'ASC'), |
250
|
1 |
|
array('by' => 'lft', 'dir' => 'ASC'), |
251
|
1 |
|
), array( |
252
|
1 |
|
'language = '.$this->state->language->getId(), |
253
|
|
|
) |
254
|
1 |
|
); |
255
|
|
|
|
256
|
|
|
$grid->addColumnText('title', 'Name')->setCustomRender(function ($item) { |
257
|
|
|
return str_repeat("-", $item->getLevel()).$item->getTitle(); |
258
|
1 |
|
}); |
259
|
|
|
|
260
|
1 |
|
$grid->addColumnText('root', 'Structure')->setCustomRender(function ($item) { |
261
|
|
|
return $item->getParent() ? $item->getParent() : '-'; |
262
|
1 |
|
}); |
263
|
|
|
|
264
|
1 |
|
$grid->addFilterSelect('root', 'Structure')->getControl()->setTranslator(null)->setItems($prnts); |
265
|
|
|
|
266
|
1 |
|
$grid->addColumnText('moduleName', 'Module'); |
267
|
|
|
|
268
|
1 |
|
$grid->addColumnText('visible', 'Visible')->setReplacement(array( |
269
|
1 |
|
'1' => 'Yes', |
270
|
1 |
|
NULL => 'No', |
271
|
1 |
|
)); |
272
|
|
|
|
273
|
1 |
|
$grid->addColumnText('default', 'Default')->setReplacement(array( |
274
|
1 |
|
'1' => 'Yes', |
275
|
1 |
|
NULL => 'No', |
276
|
1 |
|
)); |
277
|
|
|
|
278
|
1 |
|
$grid->addColumnText('secured', 'Secured')->setReplacement(array( |
279
|
1 |
|
'1' => 'Yes', |
280
|
1 |
|
NULL => 'No', |
281
|
1 |
|
)); |
282
|
|
|
|
283
|
|
|
//$grid->addActionHref("moveUp", "Move up"); |
284
|
|
|
//$grid->addActionHref("moveDown", "Move down"); |
285
|
1 |
|
$grid->addActionHref("updatePage", 'Edit')->getElementPrototype()->addAttributes(array('class' => array('btn', 'btn-primary', 'ajax'))); |
286
|
1 |
|
$grid->addActionHref("deletePage", 'Delete')->getElementPrototype()->addAttributes(array('class' => array('btn', 'btn-danger'), 'data-confirm' => 'Are you sure you want to delete this item?')); |
287
|
|
|
|
288
|
1 |
|
return $grid; |
289
|
|
|
} |
290
|
|
|
|
291
|
1 |
|
public function actionUpdatePage($id) |
292
|
|
|
{ |
293
|
|
|
if ($id) { |
294
|
|
|
$this->page = $this->em->find("WebCMS\Entity\Page", $id); |
295
|
|
|
} else { |
296
|
|
|
$this->page = new \WebCMS\Entity\Page(); |
|
|
|
|
297
|
1 |
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
View Code Duplication |
public function actionDeletePage($id) |
|
|
|
|
301
|
|
|
{ |
302
|
|
|
$this->page = $this->em->find("WebCMS\Entity\Page", $id); |
303
|
|
|
$this->em->remove($this->page); |
304
|
|
|
$this->em->flush(); |
305
|
|
|
|
306
|
|
|
$this->flashMessage('Page has been removed.', 'success'); |
307
|
|
|
|
308
|
|
|
$this->forward('Pages:default'); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function renderUpdatePage($id) |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
$this->reloadContent(); |
314
|
|
|
|
315
|
|
|
$this->template->page = $this->page; |
|
|
|
|
316
|
|
|
} |
317
|
|
|
|
318
|
1 |
|
public function renderDefault() |
319
|
|
|
{ |
320
|
1 |
|
$this->reloadContent(); |
321
|
1 |
|
} |
322
|
|
|
|
323
|
|
|
// Sorting |
324
|
|
|
public function renderSorting($id) |
|
|
|
|
325
|
|
|
{ |
326
|
|
|
$this->reloadContent(); |
327
|
|
|
|
328
|
|
|
$roots = $this->repository->findBy(array( |
329
|
|
|
'parent' => NULL, |
330
|
|
|
'language' => $this->state->language->getId(), |
331
|
|
|
)); |
332
|
|
|
|
333
|
|
|
$tree = array(); |
334
|
|
|
foreach ($roots as $r) { |
335
|
|
|
$tmp = array(); |
336
|
|
|
$tmp['title'] = $r->getTitle(); |
337
|
|
|
$tmp['__children'] = $this->repository->childrenHierarchy($r); |
338
|
|
|
|
339
|
|
|
$tree[] = $tmp; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$this->template->tree = $tree; |
|
|
|
|
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function actionMove($id, $oldPosition, $newPosition) |
346
|
|
|
{ |
347
|
|
|
$step = $newPosition - $oldPosition; |
348
|
|
|
|
349
|
|
|
if ($step > 0) { |
350
|
|
|
$this->actionMoveDown($id, $step); |
351
|
|
|
} else { |
352
|
|
|
$this->actionMoveUp($id, $step * -1); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
$this->forward('Pages:sorting'); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
View Code Duplication |
public function actionMoveUp($id, $step = 1) |
|
|
|
|
359
|
|
|
{ |
360
|
|
|
$this->page = $this->em->find("WebCMS\Entity\Page", $id); |
361
|
|
|
|
362
|
|
|
if ($this->page->getParent()) { |
363
|
|
|
$repository = $this->em->getRepository('WebCMS\Entity\Page'); |
364
|
|
|
$repository->moveUp($this->page, $step); |
365
|
|
|
|
366
|
|
|
$this->flashMessage('Page has been moved up.', 'success'); |
367
|
|
|
} else { |
368
|
|
|
$this->flashMessage('Page has not been moved up, because it is root page.', 'warning'); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
if (!$this->isAjax()) { |
372
|
|
|
$this->forward('Pages:default'); |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
376
|
|
View Code Duplication |
public function actionMoveDown($id, $step = 1) |
|
|
|
|
377
|
|
|
{ |
378
|
|
|
$this->page = $this->em->find("WebCMS\Entity\Page", $id); |
379
|
|
|
|
380
|
|
|
if ($this->page->getParent()) { |
381
|
|
|
$repository = $this->em->getRepository('WebCMS\Entity\Page'); |
382
|
|
|
$repository->moveDown($this->page, $step); |
383
|
|
|
|
384
|
|
|
$this->flashMessage('Page has been moved down.', 'success'); |
385
|
|
|
} else { |
386
|
|
|
$this->flashMessage('Page has not been moved up, because it is root page.', 'warning'); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
if (!$this->isAjax()) { |
390
|
|
|
$this->forward('Pages:default'); |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: