1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Http\Controllers\Project; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Exceptions\PageNotFoundException; |
15
|
|
|
use Jitamin\Http\Controllers\Controller; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Category Controller. |
19
|
|
|
*/ |
20
|
|
|
class CategoryController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* List of categories for a given project. |
24
|
|
|
* |
25
|
|
|
* @param array $values |
26
|
|
|
* @param array $errors |
27
|
|
|
* |
28
|
|
|
* @throws PageNotFoundException |
29
|
|
|
*/ |
30
|
|
|
public function index(array $values = [], array $errors = []) |
31
|
|
|
{ |
32
|
|
|
$project = $this->getProject(); |
33
|
|
|
|
34
|
|
|
$this->response->html($this->helper->layout->project('project/category/index', [ |
|
|
|
|
35
|
|
|
'categories' => $this->categoryModel->getList($project['id'], false), |
|
|
|
|
36
|
|
|
'values' => $values + ['project_id' => $project['id']], |
37
|
|
|
'errors' => $errors, |
38
|
|
|
'project' => $project, |
39
|
|
|
'title' => t('Categories'), |
40
|
|
|
])); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a new category. |
45
|
|
|
* |
46
|
|
|
* @param array $values |
47
|
|
|
* @param array $errors |
48
|
|
|
* |
49
|
|
|
* @throws \Jitamin\Foundation\Exceptions\PageNotFoundException |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function create(array $values = [], array $errors = []) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$project = $this->getProject(); |
54
|
|
|
|
55
|
|
|
$this->response->html($this->template->render('project/category/create', [ |
|
|
|
|
56
|
|
|
'values' => $values + ['project_id' => $project['id']], |
57
|
|
|
'errors' => $errors, |
58
|
|
|
'project' => $project, |
59
|
|
|
])); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Validate and save a new category. |
64
|
|
|
*/ |
65
|
|
|
public function store() |
66
|
|
|
{ |
67
|
|
|
$project = $this->getProject(); |
68
|
|
|
|
69
|
|
|
$values = $this->request->getValues(); |
|
|
|
|
70
|
|
|
list($valid, $errors) = $this->categoryValidator->validateCreation($values); |
|
|
|
|
71
|
|
|
|
72
|
|
|
if ($valid) { |
73
|
|
|
if ($this->categoryModel->create($values) !== false) { |
|
|
|
|
74
|
|
|
$this->flash->success(t('Your category have been created successfully.')); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return $this->response->redirect($this->helper->url->to('Project/CategoryController', 'index', ['project_id' => $project['id']])); |
|
|
|
|
77
|
|
|
} else { |
78
|
|
|
$this->flash->failure(t('Unable to create your category.')); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->index($values, $errors); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Edit a category (display the form). |
87
|
|
|
* |
88
|
|
|
* @param array $values |
89
|
|
|
* @param array $errors |
90
|
|
|
* |
91
|
|
|
* @throws PageNotFoundException |
92
|
|
|
*/ |
93
|
|
|
public function edit(array $values = [], array $errors = []) |
94
|
|
|
{ |
95
|
|
|
$project = $this->getProject(); |
96
|
|
|
$category = $this->getCategory(); |
97
|
|
|
|
98
|
|
|
$this->response->html($this->helper->layout->project('project/category/edit', [ |
|
|
|
|
99
|
|
|
'values' => empty($values) ? $category : $values, |
100
|
|
|
'errors' => $errors, |
101
|
|
|
'project' => $project, |
102
|
|
|
'title' => t('Categories'), |
103
|
|
|
])); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Edit a category (validate the form and update the database). |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function update() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$project = $this->getProject(); |
112
|
|
|
|
113
|
|
|
$values = $this->request->getValues(); |
|
|
|
|
114
|
|
|
list($valid, $errors) = $this->categoryValidator->validateModification($values); |
|
|
|
|
115
|
|
|
|
116
|
|
|
if ($valid) { |
117
|
|
|
if ($this->categoryModel->update($values)) { |
|
|
|
|
118
|
|
|
$this->flash->success(t('Your category have been updated successfully.')); |
|
|
|
|
119
|
|
|
|
120
|
|
|
return $this->response->redirect($this->helper->url->to('Project/CategoryController', 'index', ['project_id' => $project['id']])); |
|
|
|
|
121
|
|
|
} else { |
122
|
|
|
$this->flash->failure(t('Unable to update your category.')); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->edit($values, $errors); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Move category position. |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function move() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$project = $this->getProject(); |
135
|
|
|
$values = $this->request->getJson(); |
|
|
|
|
136
|
|
|
|
137
|
|
|
if (!empty($values) && isset($values['category_id']) && isset($values['position'])) { |
138
|
|
|
$result = $this->categoryModel->changePosition($project['id'], $values['category_id'], $values['position']); |
|
|
|
|
139
|
|
|
$this->response->json(['result' => $result]); |
|
|
|
|
140
|
|
|
} else { |
141
|
|
|
throw new AccessForbiddenException(); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Remove a category. |
147
|
|
|
*/ |
148
|
|
|
public function remove() |
149
|
|
|
{ |
150
|
|
|
$project = $this->getProject(); |
151
|
|
|
$category = $this->getCategory(); |
152
|
|
|
|
153
|
|
|
if ($this->request->isPost()) { |
|
|
|
|
154
|
|
|
$this->request->checkCSRFToken(); |
|
|
|
|
155
|
|
|
if ($this->categoryModel->remove($category['id'])) { |
|
|
|
|
156
|
|
|
$this->flash->success(t('Category removed successfully.')); |
|
|
|
|
157
|
|
|
} else { |
158
|
|
|
$this->flash->failure(t('Unable to remove this category.')); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->response->redirect($this->helper->url->to('Project/CategoryController', 'index', ['project_id' => $project['id']])); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->response->html($this->helper->layout->project('project/category/remove', [ |
|
|
|
|
165
|
|
|
'project' => $project, |
166
|
|
|
'category' => $category, |
167
|
|
|
'title' => t('Remove a category'), |
168
|
|
|
])); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get the category (common method between actions). |
173
|
|
|
* |
174
|
|
|
* @throws PageNotFoundException |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
View Code Duplication |
protected function getCategory() |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
$category = $this->categoryModel->getById($this->request->getIntegerParam('category_id')); |
|
|
|
|
181
|
|
|
|
182
|
|
|
if (empty($category)) { |
183
|
|
|
throw new PageNotFoundException(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $category; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.