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\AccessForbiddenException; |
15
|
|
|
use Jitamin\Foundation\Security\Role; |
16
|
|
|
use Jitamin\Http\Controllers\Controller; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Custom Filter Controller. |
20
|
|
|
*/ |
21
|
|
|
class CustomFilterController extends Controller |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Display list of filters. |
25
|
|
|
* |
26
|
|
|
* @param array $values |
27
|
|
|
* @param array $errors |
28
|
|
|
* |
29
|
|
|
* @throws \Jitamin\Foundation\Exceptions\PageNotFoundException |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function index(array $values = [], array $errors = []) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$project = $this->getProject(); |
34
|
|
|
|
35
|
|
|
$this->response->html($this->helper->layout->project('project/custom_filter/index', [ |
|
|
|
|
36
|
|
|
'values' => $values + ['project_id' => $project['id']], |
37
|
|
|
'errors' => $errors, |
38
|
|
|
'project' => $project, |
39
|
|
|
'custom_filters' => $this->customFilterModel->getAll($project['id'], $this->userSession->getId()), |
|
|
|
|
40
|
|
|
'title' => t('Custom filters'), |
41
|
|
|
])); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Display a form to create a new custom filter. |
46
|
|
|
* |
47
|
|
|
* @param array $values |
48
|
|
|
* @param array $errors |
49
|
|
|
* |
50
|
|
|
* @throws PageNotFoundException |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function create(array $values = [], array $errors = []) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$project = $this->getProject(); |
55
|
|
|
|
56
|
|
|
$this->response->html($this->template->render('project/custom_filter/create', [ |
|
|
|
|
57
|
|
|
'project' => $project, |
58
|
|
|
'values' => $values + ['project_id' => $project['id']], |
59
|
|
|
'errors' => $errors, |
60
|
|
|
])); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Save a new custom filter. |
65
|
|
|
*/ |
66
|
|
|
public function store() |
67
|
|
|
{ |
68
|
|
|
$project = $this->getProject(); |
69
|
|
|
|
70
|
|
|
$values = $this->request->getValues(); |
|
|
|
|
71
|
|
|
$values['user_id'] = $this->userSession->getId(); |
|
|
|
|
72
|
|
|
|
73
|
|
|
list($valid, $errors) = $this->customFilterValidator->validateCreation($values); |
|
|
|
|
74
|
|
|
|
75
|
|
|
if ($valid) { |
76
|
|
|
if ($this->customFilterModel->create($values) !== false) { |
|
|
|
|
77
|
|
|
$this->flash->success(t('Your custom filter have been created successfully.')); |
|
|
|
|
78
|
|
|
|
79
|
|
|
return $this->response->redirect($this->helper->url->to('Project/CustomFilterController', 'index', ['project_id' => $project['id']])); |
|
|
|
|
80
|
|
|
} else { |
81
|
|
|
$this->flash->failure(t('Unable to create your custom filter.')); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->index($values, $errors); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Remove a custom filter. |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function remove() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$project = $this->getProject(); |
94
|
|
|
$filter = $this->customFilterModel->getById($this->request->getIntegerParam('filter_id')); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$this->checkPermission($project, $filter); |
97
|
|
|
|
98
|
|
|
if ($this->request->isPost()) { |
|
|
|
|
99
|
|
|
$this->request->checkCSRFToken(); |
|
|
|
|
100
|
|
|
if ($this->customFilterModel->remove($filter['id'])) { |
|
|
|
|
101
|
|
|
$this->flash->success(t('Custom filter removed successfully.')); |
|
|
|
|
102
|
|
|
} else { |
103
|
|
|
$this->flash->failure(t('Unable to remove this custom filter.')); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->response->redirect($this->helper->url->to('Project/CustomFilterController', 'index', ['project_id' => $project['id']])); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->response->html($this->helper->layout->project('project/custom_filter/remove', [ |
|
|
|
|
110
|
|
|
'project' => $project, |
111
|
|
|
'filter' => $filter, |
112
|
|
|
'title' => t('Remove a custom filter'), |
113
|
|
|
])); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Edit a custom filter (display the form). |
118
|
|
|
* |
119
|
|
|
* @param array $values |
120
|
|
|
* @param array $errors |
121
|
|
|
* |
122
|
|
|
* @throws \Jitamin\Foundation\Exceptions\AccessForbiddenException |
123
|
|
|
* @throws \Jitamin\Foundation\Exceptions\PageNotFoundException |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function edit(array $values = [], array $errors = []) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$project = $this->getProject(); |
128
|
|
|
$filter = $this->customFilterModel->getById($this->request->getIntegerParam('filter_id')); |
|
|
|
|
129
|
|
|
|
130
|
|
|
$this->checkPermission($project, $filter); |
131
|
|
|
|
132
|
|
|
$this->response->html($this->helper->layout->project('project/custom_filter/edit', [ |
|
|
|
|
133
|
|
|
'values' => empty($values) ? $filter : $values, |
134
|
|
|
'errors' => $errors, |
135
|
|
|
'project' => $project, |
136
|
|
|
'filter' => $filter, |
137
|
|
|
'title' => t('Edit custom filter'), |
138
|
|
|
])); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Edit a custom filter (validate the form and update the database). |
143
|
|
|
*/ |
144
|
|
|
public function update() |
145
|
|
|
{ |
146
|
|
|
$project = $this->getProject(); |
147
|
|
|
$filter = $this->customFilterModel->getById($this->request->getIntegerParam('filter_id')); |
|
|
|
|
148
|
|
|
|
149
|
|
|
$this->checkPermission($project, $filter); |
150
|
|
|
|
151
|
|
|
$values = $this->request->getValues(); |
|
|
|
|
152
|
|
|
|
153
|
|
|
if (!isset($values['is_shared'])) { |
154
|
|
|
$values += ['is_shared' => 0]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!isset($values['append'])) { |
158
|
|
|
$values += ['append' => 0]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
list($valid, $errors) = $this->customFilterValidator->validateModification($values); |
|
|
|
|
162
|
|
|
|
163
|
|
|
if ($valid) { |
164
|
|
|
if ($this->customFilterModel->update($values)) { |
|
|
|
|
165
|
|
|
$this->flash->success(t('Your custom filter have been updated successfully.')); |
|
|
|
|
166
|
|
|
|
167
|
|
|
return $this->response->redirect($this->helper->url->to('Project/CustomFilterController', 'index', ['project_id' => $project['id']])); |
|
|
|
|
168
|
|
|
} else { |
169
|
|
|
$this->flash->failure(t('Unable to update custom filter.')); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->edit($values, $errors); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Check permission. |
178
|
|
|
* |
179
|
|
|
* @param array $project |
180
|
|
|
* @param array $filter |
181
|
|
|
* |
182
|
|
|
* @throws AccessForbiddenException |
183
|
|
|
*/ |
184
|
|
|
protected function checkPermission(array $project, array $filter) |
185
|
|
|
{ |
186
|
|
|
$user_id = $this->userSession->getId(); |
|
|
|
|
187
|
|
|
|
188
|
|
|
if ($filter['user_id'] != $user_id && ($this->projectUserRoleModel->getUserRole($project['id'], $user_id) === Role::PROJECT_MANAGER || !$this->userSession->isAdmin())) { |
|
|
|
|
189
|
|
|
throw new AccessForbiddenException(); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.