1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.net) |
4
|
|
|
* @author Aleksandr Torosh <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Tree\Controller; |
8
|
|
|
|
9
|
|
|
use Application\Localization\Transliterator; |
10
|
|
|
use Application\Mvc\Controller; |
11
|
|
|
use Tree\Form\CategoryForm; |
12
|
|
|
use Tree\Model\Category; |
13
|
|
|
|
14
|
|
|
class AdminController extends Controller |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
public function initialize() |
18
|
|
|
{ |
19
|
|
|
$this->helper->activeMenu()->setActive('tree'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function indexAction() |
23
|
|
|
{ |
24
|
|
|
$this->setAdminEnvironment(); |
25
|
|
|
$this->view->roots = Category::$roots; |
|
|
|
|
26
|
|
|
|
27
|
|
|
$assets = $this->getDI()->get('assets'); |
|
|
|
|
28
|
|
|
$assets->collection('modules-admin-less')->addCss(__DIR__ . '/../assets/tree.less'); |
29
|
|
|
$assets->collection('modules-admin-js')->addJs(__DIR__ . '/../assets/tree.js'); |
30
|
|
|
|
31
|
|
|
$this->helper->title($this->helper->at('Tree Categories'), true); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function addAction() |
35
|
|
|
{ |
36
|
|
|
if (!$this->request->getPost() || !$this->request->isAjax()) { |
37
|
|
|
return $this->flash->error('post ajax required'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$root = $this->request->getPost('root'); |
41
|
|
|
$title = $this->request->getPost('title', 'string'); |
42
|
|
|
|
43
|
|
|
$model = new Category(); |
44
|
|
|
$model->setRoot($root); |
45
|
|
|
if ($model->create()) { |
46
|
|
|
$model->setTitle($title); |
47
|
|
|
$model->setSlug(Transliterator::slugify($title)); |
48
|
|
|
if ($model->update()) { |
49
|
|
|
$this->returnJSON([ |
50
|
|
|
'success' => true, |
51
|
|
|
'id' => $model->getId(), |
52
|
|
|
'slug' => $model->getSlug(), |
53
|
|
|
'title' => $title, |
54
|
|
|
]); |
55
|
|
|
} else { |
56
|
|
|
$this->returnJSON(['error' => implode(' | ', $model->getMessages())]); |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
$this->returnJSON(['error' => implode(' | ', $model->getMessages())]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function editAction($id) |
64
|
|
|
{ |
65
|
|
|
$this->setAdminEnvironment(); |
66
|
|
|
|
67
|
|
|
$form = new CategoryForm(); |
68
|
|
|
$model = Category::findFirst($id); |
|
|
|
|
69
|
|
|
if (!$model) { |
70
|
|
|
$this->redirect($this->url->get() . 'tree/admin?lang=' . LANG); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($this->request->isPost()) { |
74
|
|
|
$form->bind($this->request->getPost(), $model); |
75
|
|
|
if ($form->isValid()) { |
76
|
|
|
if ($model->save()) { |
|
|
|
|
77
|
|
|
$this->flash->success($this->helper->at('Updated has been successful')); |
78
|
|
|
$this->redirect($this->url->get() . 'tree/admin?lang=' . LANG); |
79
|
|
|
} else { |
80
|
|
|
$this->flashErrors($model); |
81
|
|
|
} |
82
|
|
|
} else { |
83
|
|
|
$this->flashErrors($form); |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
|
|
$form->setEntity($model); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->helper->title($this->helper->at('Edit Category'), true); |
90
|
|
|
|
91
|
|
|
$this->view->form = $form; |
|
|
|
|
92
|
|
|
$this->view->model = $model; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function deleteAction() |
96
|
|
|
{ |
97
|
|
|
if (!$this->request->getPost() || !$this->request->isAjax()) { |
98
|
|
|
return $this->flash->error('post ajax required'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$category_id = $this->request->getPost('category_id'); |
102
|
|
|
|
103
|
|
|
$model = Category::findFirst($category_id); |
|
|
|
|
104
|
|
|
if ($model) { |
105
|
|
|
if ($model->delete()) { |
106
|
|
|
$this->returnJSON([ |
107
|
|
|
'success' => true, |
108
|
|
|
'root' => $model->getRoot(), |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function saveTreeAction() |
115
|
|
|
{ |
116
|
|
|
if (!$this->request->getPost() || !$this->request->isAjax()) { |
117
|
|
|
return $this->flash->error('post ajax required'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$data = $this->request->getPost('data'); |
121
|
|
|
|
122
|
|
|
foreach ($data as $el) { |
123
|
|
|
if ($el['item_id']) { |
124
|
|
|
$model = Category::findFirst($el['item_id']); |
|
|
|
|
125
|
|
|
if ($model) { |
126
|
|
|
if ($el['parent_id']) { |
127
|
|
|
$model->setParentId($el['parent_id']); |
128
|
|
|
} else { |
129
|
|
|
$model->setParentId(null); |
130
|
|
|
} |
131
|
|
|
$model->setDepth($el['depth']); |
132
|
|
|
$model->setLeftKey($el['left']); |
133
|
|
|
$model->setRightKey($el['right']); |
134
|
|
|
$model->update(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->returnJSON(['success' => true]); |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: