]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/CrudController.php
Fix CS
[galette.git] / galette / lib / Galette / Controllers / CrudController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette CRUD controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 2019 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 Entity
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2019 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 * @link http://galette.tuxfamily.org
34 * @since Available since 0.9.4dev - 2019-12-08
35 */
36
37 namespace Galette\Controllers;
38
39 use Slim\Http\Request;
40 use Slim\Http\Response;
41 use Analog\Analog;
42
43 /**
44 * Galette CRUD controller
45 *
46 * @category Controllers
47 * @name CrudController
48 * @package Galette
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2019 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since Available since 0.9.4dev - 2019-12-08
54 */
55
56 abstract class CrudController extends AbstractController
57 {
58 // CRUD - Create
59
60 /**
61 * Add page
62 *
63 * @param Request $request PSR Request
64 * @param Response $response PSR Response
65 * @param array $args Request arguments
66 *
67 * @return Response
68 */
69 abstract public function add(Request $request, Response $response, array $args = []): Response;
70
71 /**
72 * Add action
73 *
74 * @param Request $request PSR Request
75 * @param Response $response PSR Response
76 * @param array $args Request arguments
77 *
78 * @return Response
79 */
80 abstract public function doAdd(Request $request, Response $response, array $args = []): Response;
81
82 // /CRUD - Create
83 // CRUD - Read
84
85 /**
86 * List page
87 *
88 * @param Request $request PSR Request
89 * @param Response $response PSR Response
90 * @param array $args Request arguments
91 *
92 * @return Response
93 */
94 abstract public function list(Request $request, Response $response, array $args = []): Response;
95
96 /**
97 * List filtering
98 *
99 * @param Request $request PSR Request
100 * @param Response $response PSR Response
101 *
102 * @return Response
103 */
104 abstract public function filter(Request $request, Response $response): Response;
105
106 // /CRUD - Read
107 // CRUD - Update
108
109 /**
110 * Edit page
111 *
112 * @param Request $request PSR Request
113 * @param Response $response PSR Response
114 * @param array $args Request arguments
115 *
116 * @return Response
117 */
118 abstract public function edit(Request $request, Response $response, array $args = []): Response;
119
120 /**
121 * Edit action
122 *
123 * @param Request $request PSR Request
124 * @param Response $response PSR Response
125 * @param array $args Request arguments
126 *
127 * @return Response
128 */
129 abstract public function doEdit(Request $request, Response $response, array $args = []): Response;
130
131 // /CRUD - Update
132 // CRUD - Delete
133
134 /**
135 * Removal confirmation
136 *
137 * @param Request $request PSR Request
138 * @param Response $response PSR Response
139 * @param array $args Request arguments
140 *
141 * @return Response
142 */
143 public function confirmDelete(Request $request, Response $response, array $args = []): Response
144 {
145 $post = $request->getParsedBody();
146 $data = [
147 'id' => $this->getIdsToRemove($args, $post),
148 'redirect_uri' => $this->redirectUri($args)
149 ];
150
151 // display page
152 $this->view->render(
153 $response,
154 'confirm_removal.tpl',
155 array(
156 'mode' => $request->isXhr() ? 'ajax' : '',
157 'page_title' => $this->confirmRemoveTitle($args),
158 'form_url' => $this->formUri($args),
159 'cancel_uri' => $this->cancelUri($args),
160 'data' => $data
161 )
162 );
163 return $response;
164 }
165
166 /**
167 * Get ID to remove
168 *
169 * In simple cases, we get the ID in the route arguments; but for
170 * batchs, it should be found elsewhere.
171 * In post values, we look for id key, as well as all {sthing}_sel keys (like members_sel or contrib_sel)
172 *
173 * @param array $args Request arguments
174 * @param array $post POST values
175 *
176 * @return null|integer|integer[]
177 */
178 protected function getIdsToRemove(&$args, $post)
179 {
180 $ids = null;
181 if (isset($post['id'])) {
182 $ids = $post['id'];
183 } elseif (isset($args['id'])) {
184 $ids = $args['id'];
185 }
186
187 //look for {sthing}_sel as multiple ids selection (members_sel, contrib_sel, and so on)
188 if (is_array($post) && count($post)) {
189 $selecteds = preg_grep('/.+_sel$/', array_keys($post));
190 if (count($selecteds) == 1 && !isset($args['id'])) {
191 $ids = $post[array_shift($selecteds)];
192 } elseif (count($selecteds) > 1) {
193 //maybe an error to have multiple {type}_sel in same post request.
194 Analog::log(
195 'Several {sthing}_sel variables in same post request should be avoid.',
196 ANalog::WARNING
197 );
198 }
199 }
200
201 //type
202 if (is_array($ids)) {
203 $ids = array_map('intval', $ids);
204 } elseif (is_string($ids)) {
205 $ids = (int)$ids;
206 }
207
208 //add to $args if needed
209 if (is_array($ids)) {
210 $args['ids'] = $ids;
211 } elseif (!isset($args['id'])) {
212 $args['id'] = $ids;
213 }
214
215 return $ids;
216 }
217
218 /**
219 * Get redirection URI
220 *
221 * @param array $args Route arguments
222 *
223 * @return string
224 */
225 abstract public function redirectUri(array $args = []);
226
227 /**
228 * Get cancel URI
229 *
230 * @param array $args Route arguments
231 *
232 * @return string
233 */
234 public function cancelUri(array $args = [])
235 {
236 return $this->redirectUri($args);
237 }
238
239 /**
240 * Get form URI
241 *
242 * @param array $args Route arguments
243 *
244 * @return string
245 */
246 abstract public function formUri(array $args = []);
247
248 /**
249 * Get confirmation removal page title
250 *
251 * @param array $args Route arguments
252 *
253 * @return string
254 */
255 abstract public function confirmRemoveTitle(array $args = []);
256
257 /**
258 * Removal
259 *
260 * @param Request $request PSR Request
261 * @param Response $response PSR Response
262 * @param array $args Request arguments
263 *
264 * @return Response
265 */
266 public function delete(Request $request, Response $response, array $args = []): Response
267 {
268 $post = $request->getParsedBody();
269 $ajax = isset($post['ajax']) && $post['ajax'] === 'true';
270 $success = false;
271
272 $uri = $post['redirect_uri'] ?? $this->redirectUri();
273
274 if (!isset($post['confirm'])) {
275 $this->flash->addMessage(
276 'error_detected',
277 _T("Removal has not been confirmed!")
278 );
279 } else {
280 try {
281 $ids = $this->getIdsToRemove($args, $post);
282
283 $res = $this->doDelete($args, $post);
284 if ($res === true) {
285 $this->flash->addMessage(
286 'success_detected',
287 _T('Successfully deleted!')
288 );
289 $success = true;
290 }
291 } catch (\Exception $e) {
292 Analog::log(
293 'An error occurred on delete | ' . $e->getMessage(),
294 Analog::ERROR
295 );
296
297 $this->flash->addMessage(
298 'error_detected',
299 _T('An error occurred trying to delete :(')
300 );
301
302 $success = false;
303 }
304 }
305
306 if (!$ajax) {
307 return $response
308 ->withStatus(301)
309 ->withHeader('Location', $uri);
310 } else {
311 return $response->withJson(
312 [
313 'success' => $success
314 ]
315 );
316 }
317 }
318
319 /**
320 * Remove object
321 *
322 * @param array $args Route arguments
323 * @param array $post POST values
324 *
325 * @return boolean
326 */
327 abstract protected function doDelete(array $args, array $post);
328 // /CRUD - Delete
329 }