]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/CrudController.php
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette CRUD controller
10 * Copyright © 2019 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
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
37 namespace Galette\Controllers
;
39 use Slim\Http\Request
;
40 use Slim\Http\Response
;
44 * Galette CRUD controller
46 * @category Controllers
47 * @name CrudController
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
56 abstract class CrudController
extends AbstractController
63 * @param Request $request PSR Request
64 * @param Response $response PSR Response
65 * @param array $args Request arguments
69 abstract public function add(Request
$request, Response
$response, array $args = []): Response
;
74 * @param Request $request PSR Request
75 * @param Response $response PSR Response
76 * @param array $args Request arguments
80 abstract public function doAdd(Request
$request, Response
$response, array $args = []): Response
;
88 * @param Request $request PSR Request
89 * @param Response $response PSR Response
90 * @param array $args Request arguments
94 abstract public function list(Request
$request, Response
$response, array $args = []): Response
;
99 * @param Request $request PSR Request
100 * @param Response $response PSR Response
104 abstract public function filter(Request
$request, Response
$response): Response
;
112 * @param Request $request PSR Request
113 * @param Response $response PSR Response
114 * @param array $args Request arguments
118 abstract public function edit(Request
$request, Response
$response, array $args = []): Response
;
123 * @param Request $request PSR Request
124 * @param Response $response PSR Response
125 * @param array $args Request arguments
129 abstract public function doEdit(Request
$request, Response
$response, array $args = []): Response
;
135 * Removal confirmation
137 * @param Request $request PSR Request
138 * @param Response $response PSR Response
139 * @param array $args Request arguments
143 public function confirmDelete(Request
$request, Response
$response, array $args = []): Response
145 $post = $request->getParsedBody();
147 'id' => $this->getIdsToRemove($args, $post),
148 'redirect_uri' => $this->redirectUri($args)
154 'confirm_removal.tpl',
156 'mode' => $request->isXhr() ?
'ajax' : '',
157 'page_title' => $this->confirmRemoveTitle($args),
158 'form_url' => $this->formUri($args),
159 'cancel_uri' => $this->cancelUri($args),
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)
173 * @param array $args Request arguments
174 * @param array $post POST values
176 * @return null|integer|integer[]
178 protected function getIdsToRemove(&$args, $post)
181 if (isset($post['id'])) {
183 } elseif (isset($args['id'])) {
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.
195 'Several {sthing}_sel variables in same post request should be avoid.',
202 if (is_array($ids)) {
203 $ids = array_map('intval', $ids);
204 } elseif (is_string($ids)) {
208 //add to $args if needed
209 if (is_array($ids)) {
211 } elseif (!isset($args['id'])) {
219 * Get redirection URI
221 * @param array $args Route arguments
225 abstract public function redirectUri(array $args = []);
230 * @param array $args Route arguments
234 public function cancelUri(array $args = [])
236 return $this->redirectUri($args);
242 * @param array $args Route arguments
246 abstract public function formUri(array $args = []);
249 * Get confirmation removal page title
251 * @param array $args Route arguments
255 abstract public function confirmRemoveTitle(array $args = []);
260 * @param Request $request PSR Request
261 * @param Response $response PSR Response
262 * @param array $args Request arguments
266 public function delete(Request
$request, Response
$response, array $args = []): Response
268 $post = $request->getParsedBody();
269 $ajax = isset($post['ajax']) && $post['ajax'] === 'true';
272 $uri = $post['redirect_uri'] ??
$this->redirectUri();
274 if (!isset($post['confirm'])) {
275 $this->flash
->addMessage(
277 _T("Removal has not been confirmed!")
281 $this->zdb
->connection
->beginTransaction();
283 $ids = $this->getIdsToRemove($args, $post);
285 $res = $this->doDelete($args, $post);
287 $this->flash
->addMessage(
289 _T('Successfully deleted!')
293 $this->zdb
->connection
->commit();
294 } catch (\Exception
$e) {
295 $this->zdb
->connection
->rollBack();
297 'An error occurred on delete | ' . $e->getMessage(),
301 $this->flash
->addMessage(
303 _T('An error occurred trying to delete :(')
313 ->withHeader('Location', $uri);
315 return $response->withJson(
317 'success' => $success
326 * @param array $args Route arguments
327 * @param array $post POST values
331 abstract protected function doDelete(array $args, array $post);