]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/PaymentTypeController.php
919f8dc6bac515ad4191e4fb14e27832ecb6ee20
[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-2020 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-2020 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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2019-12-09
36 */
37
38 namespace Galette\Controllers\Crud;
39
40 use Galette\Controllers\CrudController;
41 use Slim\Http\Request;
42 use Slim\Http\Response;
43 use Galette\Repository\PaymentTypes;
44 use Galette\Entity\PaymentType;
45 use Analog\Analog;
46
47 /**
48 * Galette payment types controller
49 *
50 * @category Controllers
51 * @name PaymentTypeController
52 * @package Galette
53 * @author Johan Cwiklinski <johan@x-tnd.be>
54 * @copyright 2019-2020 The Galette Team
55 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
56 * @link http://galette.tuxfamily.org
57 * @since Available since 0.9.4dev - 2019-12-09
58 */
59
60 class PaymentTypeController extends CrudController
61 {
62 // CRUD - Create
63
64 /**
65 * Add page
66 *
67 * @param Request $request PSR Request
68 * @param Response $response PSR Response
69 * @param array $args Request arguments
70 *
71 * @return Response
72 */
73 public function add(Request $request, Response $response, array $args = []): Response
74 {
75 //no new page (included on list), just to satisfy inheritance
76 }
77
78 /**
79 * Add action
80 *
81 * @param Request $request PSR Request
82 * @param Response $response PSR Response
83 * @param array $args Request arguments
84 *
85 * @return Response
86 */
87 public function doAdd(Request $request, Response $response, array $args = []): Response
88 {
89 $args['id'] = null;
90 return $this->store($request, $response, $args);
91 }
92
93 // /CRUD - Create
94 // CRUD - Read
95
96 /**
97 * Mailings history page
98 *
99 * @param Request $request PSR Request
100 * @param Response $response PSR Response
101 * @param array $args Request arguments
102 *
103 * @return Response
104 */
105 public function list(Request $request, Response $response, array $args = []): Response
106 {
107 $ptypes = new PaymentTypes(
108 $this->zdb,
109 $this->preferences,
110 $this->login
111 );
112 $list = $ptypes->getList();
113
114 // display page
115 $this->view->render(
116 $response,
117 'gestion_paymentstypes.tpl',
118 [
119 'page_title' => _T("Payment types management"),
120 'list' => $list
121 ]
122 );
123 return $response;
124 }
125
126 /**
127 * Mailings filtering
128 *
129 * @param Request $request PSR Request
130 * @param Response $response PSR Response
131 *
132 * @return Response
133 */
134 public function filter(Request $request, Response $response): Response
135 {
136 //no filters
137 }
138
139 // /CRUD - Read
140 // CRUD - Update
141
142 /**
143 * Edit page
144 *
145 * @param Request $request PSR Request
146 * @param Response $response PSR Response
147 * @param array $args Request arguments
148 *
149 * @return Response
150 */
151 public function edit(Request $request, Response $response, array $args = []): Response
152 {
153 $id = (int)$args['id'];
154 $ptype = new PaymentType($this->zdb, $id);
155
156 // display page
157 $this->view->render(
158 $response,
159 'edit_paymenttype.tpl',
160 [
161 'page_title' => _T("Edit payment type"),
162 'ptype' => $ptype
163 ]
164 );
165 return $response;
166 }
167
168 /**
169 * Edit action
170 *
171 * @param Request $request PSR Request
172 * @param Response $response PSR Response
173 * @param array $args Request arguments
174 *
175 * @return Response
176 */
177 public function doEdit(Request $request, Response $response, array $args = []): Response
178 {
179 return $this->store($request, $response, $args);
180 }
181
182 /**
183 * Store
184 *
185 * @param Request $request PSR Request
186 * @param Response $response PSR Response
187 * @param array $args Request arguments
188 *
189 * @return Response
190 */
191 public function store(Request $request, Response $response, array $args = []): Response
192 {
193 $id = (isset($args['id']) ? (int)$args['id'] : null);
194 $post = $request->getParsedBody();
195
196 if (isset($post['cancel'])) {
197 return $response
198 ->withStatus(301)
199 ->withHeader('Location', $this->cancelUri());
200 }
201
202 $ptype = new PaymentType($this->zdb, $id);
203 $ptype->name = $post['name'];
204 $res = $ptype->store();
205 $redirect_uri = $this->redirectUri();
206
207 if (!$res) {
208 if ($id === null) {
209 $this->flash->addMessage(
210 'error_detected',
211 preg_replace(
212 '(%s)',
213 $ptype->name,
214 _T("Payment type '%s' has not been added!")
215 )
216 );
217 } else {
218 $this->flash->addMessage(
219 'error_detected',
220 preg_replace(
221 '(%s)',
222 $ptype->name,
223 _T("Payment type '%s' has not been modified!")
224 )
225 );
226 //redirect to payment type edition
227 $redirect_uri = $this->router->pathFor('editPaymentType', ['id' => $id]);
228 }
229 } else {
230 if ($id === null) {
231 $this->flash->addMessage(
232 'success_detected',
233 preg_replace(
234 '(%s)',
235 $ptype->name,
236 _T("Payment type '%s' has been successfully added.")
237 )
238 );
239 } else {
240 $this->flash->addMessage(
241 'success_detected',
242 preg_replace(
243 '(%s)',
244 $ptype->name,
245 _T("Payment type '%s' has been successfully modified.")
246 )
247 );
248 }
249 }
250
251 $warning_detected = $ptype->getWarnings();
252 if (count($warning_detected)) {
253 foreach ($warning_detected as $warning) {
254 $this->flash->addMessage(
255 'warning_detected',
256 $warning
257 );
258 }
259 }
260
261 return $response
262 ->withStatus(301)
263 ->withHeader('Location', $redirect_uri);
264 }
265
266
267 // /CRUD - Update
268 // CRUD - Delete
269
270 /**
271 * Get redirection URI
272 *
273 * @param array $args Route arguments
274 *
275 * @return string
276 */
277 public function redirectUri(array $args = [])
278 {
279 return $this->router->pathFor('paymentTypes');
280 }
281
282 /**
283 * Get form URI
284 *
285 * @param array $args Route arguments
286 *
287 * @return string
288 */
289 public function formUri(array $args = [])
290 {
291 return $this->router->pathFor(
292 'doRemovePaymentType',
293 ['id' => $args['id'] ?? null]
294 );
295 }
296
297 /**
298 * Get confirmation removal page title
299 *
300 * @param array $args Route arguments
301 *
302 * @return string
303 */
304 public function confirmRemoveTitle(array $args = [])
305 {
306 $ptype = new PaymentType($this->zdb, (int)$args['id']);
307 return sprintf(
308 _T('Remove payment type %1$s'),
309 $ptype->getName()
310 );
311 }
312
313 /**
314 * Remove object
315 *
316 * @param array $args Route arguments
317 * @param array $post POST values
318 *
319 * @return boolean
320 */
321 protected function doDelete(array $args, array $post)
322 {
323 $ptype = new PaymentType($this->zdb, (int)$args['id']);
324 return $ptype->remove();
325 }
326
327 // CRUD - Delete
328 }