]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/PaymentTypeController.php
73b5f5831f02f444d240a428291460b818b8da94
[galette.git] / galette / lib / Galette / Controllers / Crud / PaymentTypeController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette payment types controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 2019-2023 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 Controllers
28 * @package Galette
29 *
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
35 */
36
37 namespace Galette\Controllers\Crud;
38
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;
44 use Analog\Analog;
45
46 /**
47 * Galette payment types controller
48 *
49 * @category Controllers
50 * @name PaymentTypeController
51 * @package Galette
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
57 */
58
59 class PaymentTypeController extends CrudController
60 {
61 // CRUD - Create
62
63 /**
64 * Add page
65 *
66 * @param Request $request PSR Request
67 * @param Response $response PSR Response
68 *
69 * @return Response
70 */
71 public function add(Request $request, Response $response): Response
72 {
73 //no new page (included on list), just to satisfy inheritance
74 return $response;
75 }
76
77 /**
78 * Add action
79 *
80 * @param Request $request PSR Request
81 * @param Response $response PSR Response
82 *
83 * @return Response
84 */
85 public function doAdd(Request $request, Response $response): Response
86 {
87 return $this->store($request, $response, null);
88 }
89
90 // /CRUD - Create
91 // CRUD - Read
92
93 /**
94 * List page
95 *
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
100 *
101 * @return Response
102 */
103 public function list(Request $request, Response $response, $option = null, $value = null): Response
104 {
105 $ptypes = new PaymentTypes(
106 $this->zdb,
107 $this->preferences,
108 $this->login
109 );
110 $list = $ptypes->getList();
111
112 // display page
113 $this->view->render(
114 $response,
115 'pages/configuration_payment_types.html.twig',
116 [
117 'page_title' => _T("Payment types management"),
118 'list' => $list
119 ]
120 );
121 return $response;
122 }
123
124 /**
125 * Mailings filtering
126 *
127 * @param Request $request PSR Request
128 * @param Response $response PSR Response
129 *
130 * @return Response
131 */
132 public function filter(Request $request, Response $response): Response
133 {
134 //no filters
135 return $response;
136 }
137
138 // /CRUD - Read
139 // CRUD - Update
140
141 /**
142 * Edit page
143 *
144 * @param Request $request PSR Request
145 * @param Response $response PSR Response
146 * @param integer $id Type id
147 *
148 * @return Response
149 */
150 public function edit(Request $request, Response $response, int $id): Response
151 {
152 $ptype = new PaymentType($this->zdb, $id);
153 $mode = $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest' ? 'ajax' : '';
154
155
156 // display page
157 $this->view->render(
158 $response,
159 'pages/configuration_payment_type_form.html.twig',
160 [
161 'page_title' => _T("Edit payment type"),
162 'ptype' => $ptype,
163 'mode' => $mode
164 ]
165 );
166 return $response;
167 }
168
169 /**
170 * Edit action
171 *
172 * @param Request $request PSR Request
173 * @param Response $response PSR Response
174 * @param integer $id Type id
175 *
176 * @return Response
177 */
178 public function doEdit(Request $request, Response $response, int $id): Response
179 {
180 return $this->store($request, $response, $id);
181 }
182
183 /**
184 * Store
185 *
186 * @param Request $request PSR Request
187 * @param Response $response PSR Response
188 * @param integer $id Type id
189 *
190 * @return Response
191 */
192 public function store(Request $request, Response $response, int $id = null): Response
193 {
194 $post = $request->getParsedBody();
195
196 if (isset($post['cancel'])) {
197 return $response
198 ->withStatus(301)
199 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
200 }
201
202 $error_detected = [];
203 $msg = null;
204
205 $ptype = new PaymentType($this->zdb, $id);
206 $ptype->name = $post['name'];
207 if (isset($post['name']) && $post['name'] != '') {
208 $res = $ptype->store();
209 } else {
210 $res = false;
211 $error_detected[] = _T("Missing required payment type's name!");
212 }
213 $redirect_uri = $this->redirectUri($this->getArgs($request));
214
215 if (!$res) {
216 if ($id === null) {
217 $error_detected[] = preg_replace(
218 '(%s)',
219 $ptype->getName(),
220 _T("Payment type '%s' has not been added!")
221 );
222 } else {
223 $error_detected[] = preg_replace(
224 '(%s)',
225 $ptype->getName(),
226 _T("Payment type '%s' has not been modified!")
227 );
228 //redirect to payment type edition
229 $redirect_uri = $this->routeparser->urlFor('editPaymentType', ['id' => $id]);
230 }
231 } else {
232 if ($id === null) {
233 $error_detected[] = preg_replace(
234 '(%s)',
235 $ptype->getName(),
236 _T("Payment type '%s' has been successfully added.")
237 );
238 } else {
239 $msg = preg_replace(
240 '(%s)',
241 $ptype->getName(),
242 _T("Payment type '%s' has been successfully modified.")
243 );
244 }
245 }
246
247 $warning_detected = $ptype->getWarnings();
248 if (count($warning_detected)) {
249 foreach ($warning_detected as $warning) {
250 $this->flash->addMessage(
251 'warning_detected',
252 $warning
253 );
254 }
255 }
256
257 if (count($error_detected) > 0) {
258 foreach ($error_detected as $error) {
259 $this->flash->addMessage(
260 'error_detected',
261 $error
262 );
263 }
264 } else {
265 $this->flash->addMessage(
266 'success_detected',
267 $msg
268 );
269 }
270
271 return $response
272 ->withStatus(301)
273 ->withHeader('Location', $redirect_uri);
274 }
275
276
277 // /CRUD - Update
278 // CRUD - Delete
279
280 /**
281 * Get redirection URI
282 *
283 * @param array $args Route arguments
284 *
285 * @return string
286 */
287 public function redirectUri(array $args)
288 {
289 return $this->routeparser->urlFor('paymentTypes');
290 }
291
292 /**
293 * Get form URI
294 *
295 * @param array $args Route arguments
296 *
297 * @return string
298 */
299 public function formUri(array $args)
300 {
301 return $this->routeparser->urlFor(
302 'doRemovePaymentType',
303 ['id' => $args['id'] ?? null]
304 );
305 }
306
307 /**
308 * Get confirmation removal page title
309 *
310 * @param array $args Route arguments
311 *
312 * @return string
313 */
314 public function confirmRemoveTitle(array $args)
315 {
316 $ptype = new PaymentType($this->zdb, (int)$args['id']);
317 return sprintf(
318 _T('Remove payment type %1$s'),
319 $ptype->getName()
320 );
321 }
322
323 /**
324 * Remove object
325 *
326 * @param array $args Route arguments
327 * @param array $post POST values
328 *
329 * @return boolean
330 */
331 protected function doDelete(array $args, array $post)
332 {
333 $ptype = new PaymentType($this->zdb, (int)$args['id']);
334 return $ptype->remove();
335 }
336
337 // CRUD - Delete
338 }