]>
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-2023 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-2023 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
;
40 use Slim\Psr7\Request
;
41 use Slim\Psr7\Response
;
45 * Galette CRUD controller
47 * @category Controllers
48 * @name CrudController
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2019-2023 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
69 abstract public function add(Request
$request, Response
$response): Response
;
74 * @param Request $request PSR Request
75 * @param Response $response PSR Response
79 abstract public function doAdd(Request
$request, Response
$response): Response
;
87 * @param Request $request PSR Request
88 * @param Response $response PSR Response
89 * @param string $option One of 'page' or 'order'
90 * @param string|integer $value Value of the option
94 abstract public function list(Request
$request, Response
$response, $option = null, $value = null): 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 integer $id Record id
118 abstract public function edit(Request
$request, Response
$response, int $id): Response
;
123 * @param Request $request PSR Request
124 * @param Response $response PSR Response
125 * @param integer $id Record id
129 abstract public function doEdit(Request
$request, Response
$response, int $id): Response
;
135 * Removal confirmation
137 * @param Request $request PSR Request
138 * @param Response $response PSR Response
142 public function confirmDelete(Request
$request, Response
$response): Response
147 'modals/confirm_removal.html.twig',
148 $this->getconfirmDeleteParams($request)
154 * Removal confirmation parameters, can be overriden
156 * @param Request $request PSR Request
160 protected function getconfirmDeleteParams(Request
$request): array
162 $args = $this->getArgs($request);
163 $post = $request->getParsedBody();
165 'id' => $this->getIdsToRemove($args, $post),
166 'redirect_uri' => $this->redirectUri($args)
170 'mode' => ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ?
'ajax' : '',
171 'page_title' => $this->confirmRemoveTitle($args),
172 'form_url' => $this->formUri($args),
173 'cancel_uri' => $this->cancelUri($args),
181 * In simple cases, we get the ID in the route arguments; but for
182 * batchs, it should be found elsewhere.
183 * In post values, we look for id key, as well as all entries_sel keys
185 * @param array $args Request arguments
186 * @param array $post POST values
188 * @return null|integer|integer[]
190 protected function getIdsToRemove(&$args, $post)
193 if (isset($post['id'])) {
195 } elseif (isset($args['id'])) {
199 if ($ids === null && method_exists($this, 'getFilterName')) {
200 $filter_name = $this->getFilterName($args);
201 $filters = $this->session
->$filter_name;
202 $ids = $filters->selected
;
206 if (is_array($ids)) {
207 $ids = array_map('intval', $ids);
208 } elseif (is_string($ids)) {
212 //add to $args if needed
213 if (is_array($ids)) {
215 } elseif (!isset($args['id']) && $ids) {
223 * Get redirection URI
225 * @param array $args Route arguments
229 abstract public function redirectUri(array $args);
234 * @param array $args Route arguments
238 public function cancelUri(array $args)
240 return $this->redirectUri($args);
246 * @param array $args Route arguments
250 abstract public function formUri(array $args);
253 * Get confirmation removal page title
255 * @param array $args Route arguments
259 abstract public function confirmRemoveTitle(array $args);
264 * @param Request $request PSR Request
265 * @param Response $response PSR Response
269 public function delete(Request
$request, Response
$response): Response
271 $post = $request->getParsedBody();
272 $args = $this->getArgs($request);
273 $ajax = isset($post['ajax']) && $post['ajax'] === 'true';
276 $uri = $post['redirect_uri'] ??
$this->redirectUri($args);
278 if (!isset($post['confirm'])) {
279 $this->flash
->addMessage(
281 _T("Removal has not been confirmed!")
285 $res = $this->doDelete($args, $post);
287 $this->flash
->addMessage(
289 _T('Successfully deleted!')
293 } catch (Throwable
$e) {
295 'An error occurred on delete | ' . $e->getMessage(),
299 $this->flash
->addMessage(
301 _T('An error occurred trying to delete :(')
311 ->withHeader('Location', $uri);
313 return $this->withJson($response, ['success' => $success]);
320 * @param array $args Route arguments
321 * @param array $post POST values
325 abstract protected function doDelete(array $args, array $post);