]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/CrudController.php
ff56005959351bf28f675dae9199b65fca728cf3
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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2019-12-08
38 namespace Galette\Controllers
;
40 use Slim\Http\Request
;
41 use Slim\Http\Response
;
45 * Galette CRUD controller
47 * @category Controllers
48 * @name CrudController
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2019 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.9.4dev - 2019-12-08
57 abstract class CrudController
extends AbstractController
64 * @param Request $request PSR Request
65 * @param Response $response PSR Response
66 * @param array $args Request arguments
70 abstract public function add(Request
$request, Response
$response, array $args = []) :Response
;
75 * @param Request $request PSR Request
76 * @param Response $response PSR Response
77 * @param array $args Request arguments
81 abstract public function doAdd(Request
$request, Response
$response, array $args = []) :Response
;
89 * @param Request $request PSR Request
90 * @param Response $response PSR Response
91 * @param array $args Request arguments
95 abstract public function list(Request
$request, Response
$response, array $args = []) :Response
;
100 * @param Request $request PSR Request
101 * @param Response $response PSR Response
105 abstract public function filter(Request
$request, Response
$response) :Response
;
113 * @param Request $request PSR Request
114 * @param Response $response PSR Response
115 * @param array $args Request arguments
119 abstract public function edit(Request
$request, Response
$response, array $args = []) :Response
;
124 * @param Request $request PSR Request
125 * @param Response $response PSR Response
126 * @param array $args Request arguments
130 abstract public function doEdit(Request
$request, Response
$response, array $args = []) :Response
;
136 * Removal confirmation
138 * @param Request $request PSR Request
139 * @param Response $response PSR Response
140 * @param array $args Request arguments
144 public function confirmDelete(Request
$request, Response
$response, array $args = []) :Response
146 $post = $request->getParsedBody();
148 'id' => $this->getIdsToRemove($args, $post),
149 'redirect_uri' => $this->redirectUri($args)
155 'confirm_removal.tpl',
157 'mode' => $request->isXhr() ?
'ajax' : '',
158 'page_title' => $this->confirmRemoveTitle($args),
159 'form_url' => $this->formUri($args),
160 'cancel_uri' => $this->cancelUri($args),
170 * In simple cases, we get the ID in the route arguments; but for
171 * batchs, it should be found elsewhere.
172 * In post values, we look for id key, as well as all {sthing}_sel keys (like members_sel or contrib_sel)
174 * @param array $args Request arguments
175 * @param array $post POST values
177 * @return null|integer|integer[]
179 protected function getIdsToRemove(&$args, $post)
182 if (isset($post['id'])) {
184 } elseif (isset($args['id'])) {
188 //look for {sthing}_sel as multiple ids selection (members_sel, contrib_sel, and so on)
189 if (is_array($post) && count($post)) {
190 $selecteds = preg_grep('/.+_sel$/', array_keys($post));
191 if (count($selecteds) == 1 && !isset($args['id'])) {
192 $ids = $post[array_shift($selecteds)];
193 } elseif (count($selecteds) > 1) {
194 //maybe an error to have multiple {type}_sel in same post request.
196 'Several {sthing}_sel variables in same post request should be avoid.',
203 if (is_array($ids)) {
204 $ids = array_map('intval', $ids);
205 } elseif (is_string($ids)) {
209 //add to $args if needed
210 if (is_array($ids)) {
212 } elseif (!isset($args['id'])) {
220 * Get redirection URI
222 * @param array $args Route arguments
226 abstract public function redirectUri(array $args = []);
231 * @param array $args Route arguments
235 public function cancelUri(array $args = [])
237 return $this->redirectUri($args);
243 * @param array $args Route arguments
247 abstract public function formUri(array $args = []);
250 * Get confirmation removal page title
252 * @param array $args Route arguments
256 abstract public function confirmRemoveTitle(array $args = []);
261 * @param Request $request PSR Request
262 * @param Response $response PSR Response
263 * @param array $args Request arguments
267 public function delete(Request
$request, Response
$response, array $args = []) :Response
269 $post = $request->getParsedBody();
270 $ajax = isset($post['ajax']) && $post['ajax'] === 'true';
273 $uri = $post['redirect_uri'] ??
$this->redirectUri();
275 if (!isset($post['confirm'])) {
276 $this->flash
->addMessage(
278 _T("Removal has not been confirmed!")
282 $this->zdb
->connection
->beginTransaction();
284 $ids = $this->getIdsToRemove($args, $post);
286 $res = $this->doDelete($args, $post);
288 $this->flash
->addMessage(
290 _T('Successfully deleted!')
294 $this->zdb
->connection
->commit();
295 } catch (\Exception
$e) {
296 $this->zdb
->connection
->rollBack();
298 'An error occurred on delete | ' . $e->getMessage(),
302 $this->flash
->addMessage(
304 _T('An error occurred trying to delete :(')
314 ->withHeader('Location', $uri);
316 return $response->withJson(
318 'success' => $success
327 * @param array $args Route arguments
328 * @param array $post POST values
332 abstract protected function doDelete(array $args, array $post);