3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette payment types 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/>.
27 * @category Controllers
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-09
37 namespace Galette\Controllers\Crud
;
39 use Galette\Controllers\CrudController
;
40 use Slim\Psr7\Request
;
41 use Slim\Psr7\Response
;
42 use Galette\Repository\PaymentTypes
;
43 use Galette\Entity\PaymentType
;
47 * Galette payment types controller
49 * @category Controllers
50 * @name PaymentTypeController
52 * @author Johan Cwiklinski <johan@x-tnd.be>
53 * @copyright 2019-2023 The Galette Team
54 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
55 * @link http://galette.tuxfamily.org
56 * @since Available since 0.9.4dev - 2019-12-09
59 class PaymentTypeController
extends CrudController
66 * @param Request $request PSR Request
67 * @param Response $response PSR Response
71 public function add(Request
$request, Response
$response): Response
73 //no new page (included on list), just to satisfy inheritance
80 * @param Request $request PSR Request
81 * @param Response $response PSR Response
85 public function doAdd(Request
$request, Response
$response): Response
87 return $this->store($request, $response, null);
96 * @param Request $request PSR Request
97 * @param Response $response PSR Response
98 * @param string $option One of 'page' or 'order'
99 * @param string|integer $value Value of the option
103 public function list(Request
$request, Response
$response, $option = null, $value = null): Response
105 $ptypes = new PaymentTypes(
110 $list = $ptypes->getList();
115 'pages/configuration_payment_types.html.twig',
117 'page_title' => _T("Payment types management"),
127 * @param Request $request PSR Request
128 * @param Response $response PSR Response
132 public function filter(Request
$request, Response
$response): Response
144 * @param Request $request PSR Request
145 * @param Response $response PSR Response
146 * @param integer $id Type id
150 public function edit(Request
$request, Response
$response, int $id): Response
152 $ptype = new PaymentType($this->zdb
, $id);
153 $mode = $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest' ?
'ajax' : '';
159 'pages/configuration_payment_type_form.html.twig',
161 'page_title' => _T("Edit payment type"),
172 * @param Request $request PSR Request
173 * @param Response $response PSR Response
174 * @param integer $id Type id
178 public function doEdit(Request
$request, Response
$response, int $id): Response
180 return $this->store($request, $response, $id);
186 * @param Request $request PSR Request
187 * @param Response $response PSR Response
188 * @param integer $id Type id
192 public function store(Request
$request, Response
$response, int $id = null): Response
194 $post = $request->getParsedBody();
196 if (isset($post['cancel'])) {
199 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
202 $error_detected = [];
205 $ptype = new PaymentType($this->zdb
, $id);
206 $ptype->name
= $post['name'];
207 if (isset($post['name']) && $post['name'] != '') {
208 $res = $ptype->store();
211 $error_detected[] = _T("Missing required payment type's name!");
213 $redirect_uri = $this->redirectUri($this->getArgs($request));
217 $error_detected[] = preg_replace(
220 _T("Payment type '%s' has not been added!")
223 $error_detected[] = preg_replace(
226 _T("Payment type '%s' has not been modified!")
228 //redirect to payment type edition
229 $redirect_uri = $this->routeparser
->urlFor('editPaymentType', ['id' => $id]);
233 $error_detected[] = preg_replace(
236 _T("Payment type '%s' has been successfully added.")
242 _T("Payment type '%s' has been successfully modified.")
247 $warning_detected = $ptype->getWarnings();
248 if (count($warning_detected)) {
249 foreach ($warning_detected as $warning) {
250 $this->flash
->addMessage(
257 if (count($error_detected) > 0) {
258 foreach ($error_detected as $error) {
259 $this->flash
->addMessage(
265 $this->flash
->addMessage(
273 ->withHeader('Location', $redirect_uri);
281 * Get redirection URI
283 * @param array $args Route arguments
287 public function redirectUri(array $args)
289 return $this->routeparser
->urlFor('paymentTypes');
295 * @param array $args Route arguments
299 public function formUri(array $args)
301 return $this->routeparser
->urlFor(
302 'doRemovePaymentType',
303 ['id' => $args['id'] ??
null]
308 * Get confirmation removal page title
310 * @param array $args Route arguments
314 public function confirmRemoveTitle(array $args)
316 $ptype = new PaymentType($this->zdb
, (int)$args['id']);
318 _T('Remove payment type %1$s'),
326 * @param array $args Route arguments
327 * @param array $post POST values
331 protected function doDelete(array $args, array $post)
333 $ptype = new PaymentType($this->zdb
, (int)$args['id']);
334 return $ptype->remove();