]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/EntitledsController.php
Scrutinizer Auto-Fixes (#59)
[galette.git] / galette / lib / Galette / Controllers / Crud / EntitledsController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Entitleds (contributions types and status) controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Controllers
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2019-2020 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2019-12-09
36 */
37
38 namespace Galette\Controllers\Crud;
39
40 use Galette\Controllers\CrudController;
41
42 use Slim\Http\Request;
43 use Slim\Http\Response;
44
45 use Galette\Entity\ContributionsTypes;
46 use Galette\Entity\Status;
47 use Galette\Repository\Members;
48
49 use Analog\Analog;
50
51 /**
52 * Galette Entitleds (contributions types and status) controller
53 *
54 * @category Controllers
55 * @name EntitledsController
56 * @package Galette
57 * @author Johan Cwiklinski <johan@x-tnd.be>
58 * @copyright 2020 The Galette Team
59 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
60 * @link http://galette.tuxfamily.org
61 * @since Available since 0.9.4dev - 2020-06-07
62 */
63
64 class EntitledsController extends CrudController
65 {
66 // CRUD - Create
67
68 /**
69 * Add page
70 *
71 * @param Request $request PSR Request
72 * @param Response $response PSR Response
73 * @param array $args Request arguments
74 *
75 * @return Response
76 */
77 public function add(Request $request, Response $response, array $args = []) :Response
78 {
79 //no new page (included on list), just to satisfy inheritance
80 }
81
82 /**
83 * Add action
84 *
85 * @param Request $request PSR Request
86 * @param Response $response PSR Response
87 * @param array $args Request arguments
88 *
89 * @return Response
90 */
91 public function doAdd(Request $request, Response $response, array $args = []) :Response
92 {
93 $args['id'] = null;
94 return $this->store($request, $response, $args);
95 }
96
97 // /CRUD - Create
98 // CRUD - Read
99
100 /**
101 * Mailings history page
102 *
103 * @param Request $request PSR Request
104 * @param Response $response PSR Response
105 * @param array $args Request arguments
106 *
107 * @return Response
108 */
109 public function list(Request $request, Response $response, array $args = []) :Response
110 {
111 $className = null;
112 $class = null;
113
114 $params = [];
115 switch ($args['class']) {
116 case 'status':
117 $className = 'Status';
118 $class = new Status($this->zdb);
119 $params['page_title'] = _T("User statuses");
120 $params['non_staff_priority'] = Members::NON_STAFF_MEMBERS;
121 break;
122 case 'contributions-types':
123 $className = 'ContributionsTypes';
124 $class = new ContributionsTypes($this->zdb);
125 $params['page_title'] = _T("Contribution types");
126 break;
127 }
128
129 $params['class'] = $className;
130 $params['url_class'] = $args['class'];
131 $params['fields'] = $class::$fields;
132
133 $list = $class->getCompleteList();
134 $params['entries'] = $list;
135
136 if (count($class->errors) > 0) {
137 $error_detected = array_merge($error_detected, $class->errors);
138 }
139
140 // display page
141 $this->view->render(
142 $response,
143 'gestion_intitules.tpl',
144 $params
145 );
146 return $response;
147 }
148
149 /**
150 * Mailings filtering
151 *
152 * @param Request $request PSR Request
153 * @param Response $response PSR Response
154 *
155 * @return Response
156 */
157 public function filter(Request $request, Response $response) :Response
158 {
159 //no filters
160 }
161
162 // /CRUD - Read
163 // CRUD - Update
164
165 /**
166 * Edit page
167 *
168 * @param Request $request PSR Request
169 * @param Response $response PSR Response
170 * @param array $args Request arguments
171 *
172 * @return Response
173 */
174 public function edit(Request $request, Response $response, array $args = []) :Response
175 {
176 $className = null;
177 $class = null;
178
179 $params = [];
180 switch ($args['class']) {
181 case 'status':
182 $className = 'Status';
183 $class = new Status($this->zdb);
184 $params['page_title'] = _T("Edit status");
185 $params['non_staff_priority'] = Members::NON_STAFF_MEMBERS;
186 break;
187 case 'contributions-types':
188 $className = 'ContributionsTypes';
189 $class = new ContributionsTypes($this->zdb);
190 $params['page_title'] = _T("Edit contribution type");
191 break;
192 }
193
194 $params['class'] = $className;
195 $params['url_class'] = $args['class'];
196 $params['fields'] = $class::$fields;
197
198 $entry = $class->get($args['id']);
199 $params['entry'] = $entry;
200
201 // display page
202 $this->view->render(
203 $response,
204 'editer_intitule.tpl',
205 $params
206 );
207 return $response;
208 }
209
210 /**
211 * Edit action
212 *
213 * @param Request $request PSR Request
214 * @param Response $response PSR Response
215 * @param array $args Request arguments
216 *
217 * @return Response
218 */
219 public function doEdit(Request $request, Response $response, array $args = []) :Response
220 {
221 return $this->store($request, $response, $args);
222 }
223
224 /**
225 * Store
226 *
227 * @param Request $request PSR Request
228 * @param Response $response PSR Response
229 * @param array $args Request arguments
230 *
231 * @return Response
232 */
233 public function store(Request $request, Response $response, array $args = []) :Response
234 {
235 $id = (isset($args['id']) ? (int)$args['id'] : null);
236
237 $post = $request->getParsedBody();
238 $class = null;
239
240 switch ($args['class']) {
241 case 'status':
242 $class = new Status($this->zdb);
243 break;
244 case 'contributions-types':
245 $class = new ContributionsTypes($this->zdb);
246 break;
247 }
248
249 $label = trim($post[$class::$fields['libelle']]);
250 $field = trim($post[$class::$fields['third']]);
251
252 $ret = null;
253 if ($args['action'] === 'add') {
254 $ret = $class->add($label, $field);
255 } else {
256 $oldlabel = $class->getLabel($id, false);
257 $ret = $class->update($id, $label, $field);
258 }
259
260 if ($ret !== true) {
261 $msg_type = 'error_detected';
262 $msg = $args['action'] === 'add' ?
263 _T("%type has not been added :(") : _T("%type #%id has not been updated");
264 } else {
265 $msg_type = 'success_detected';
266 $msg = $args['action'] === 'add' ?
267 _T("%type has been successfully added!") : _T("%type #%id has been successfully updated!");
268 }
269
270 $this->flash->addMessage(
271 $msg_type,
272 str_replace(
273 ['%type', '%id'],
274 [$class->getI18nType(), $id],
275 $msg
276 )
277 );
278
279 return $response
280 ->withStatus(301)
281 ->withHeader(
282 'Location',
283 $this->router->pathFor(
284 'entitleds',
285 ['class' => $args['class']]
286 )
287 );
288 }
289
290
291 // /CRUD - Update
292 // CRUD - Delete
293
294 /**
295 * Get redirection URI
296 *
297 * @param array $args Route arguments
298 *
299 * @return string
300 */
301 public function redirectUri(array $args = [])
302 {
303 return $this->router->pathFor('entitleds', ['class' => $args['class']]);
304 }
305
306 /**
307 * Get form URI
308 *
309 * @param array $args Route arguments
310 *
311 * @return string
312 */
313 public function formUri(array $args = [])
314 {
315 return $this->router->pathFor(
316 'doRemoveEntitled',
317 [
318 'class' => $args['class'],
319 'id' => $args['id']
320 ]
321 );
322 }
323
324 /**
325 * Get confirmation removal page title
326 *
327 * @param array $args Route arguments
328 *
329 * @return string
330 */
331 public function confirmRemoveTitle(array $args = [])
332 {
333 $class = null;
334 switch ($args['class']) {
335 case 'status':
336 $class = new Status($this->zdb);
337 break;
338 case 'contributions-types':
339 $class = new ContributionsTypes($this->zdb);
340 break;
341 }
342 $label = $class->getLabel((int)$args['id']);
343
344 return str_replace(
345 ['%type', '%label'],
346 [$class->getI18nType(), $label],
347 _T("Remove %type '%label'")
348 );
349 }
350
351 /**
352 * Remove object
353 *
354 * @param array $args Route arguments
355 * @param array $post POST values
356 *
357 * @return boolean
358 */
359 protected function doDelete(array $args, array $post)
360 {
361 $class = null;
362 switch ($args['class']) {
363 case 'status':
364 $class = new Status($this->zdb);
365 break;
366 case 'contributions-types':
367 $class = new ContributionsTypes($this->zdb);
368 break;
369 }
370
371 $label = $class->getLabel((int)$args['id']);
372 $ret = false;
373 if ($label !== $class::ID_NOT_EXITS) {
374 $ret = $class->delete((int)$args['id']);
375
376 if (!$ret) {
377 foreach ($class->getErrors() as $error) {
378 $this->flash->addMessage(
379 'error_detected',
380 $error
381 );
382 }
383 }
384 }
385
386 return $ret;
387 }
388
389 // CRUD - Delete
390 }