]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/TitlesController.php
9aeee14c7febce8d5501390660b15504ad8b6981
[galette.git] / galette / lib / Galette / Controllers / Crud / TitlesController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette Title 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-06
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\Titles;
43 use Galette\Entity\Title;
44 use Analog\Analog;
45
46 /**
47 * Galette Titles controller
48 *
49 * @category Controllers
50 * @name TitlesController
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-08
57 */
58
59 class TitlesController 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 * Titles 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 $titles = Titles::getList($this->zdb);
106
107 // display page
108 $this->view->render(
109 $response,
110 'pages/configuration_titles.html.twig',
111 [
112 'page_title' => _T("Titles management"),
113 'titles_list' => $titles
114 ]
115 );
116 return $response;
117 }
118
119 /**
120 * Titles filtering
121 *
122 * @param Request $request PSR Request
123 * @param Response $response PSR Response
124 *
125 * @return Response
126 */
127 public function filter(Request $request, Response $response): Response
128 {
129 //no filtering
130 return $response;
131 }
132
133 // /CRUD - Read
134 // CRUD - Update
135
136 /**
137 * Edit page
138 *
139 * @param Request $request PSR Request
140 * @param Response $response PSR Response
141 * @param integer $id Title id
142 *
143 * @return Response
144 */
145 public function edit(Request $request, Response $response, int $id): Response
146 {
147 $title = new Title($id);
148 $mode = $request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest' ? 'ajax' : '';
149
150 // display page
151 $this->view->render(
152 $response,
153 'pages/configuration_title_form.html.twig',
154 [
155 'page_title' => _T("Edit title"),
156 'title' => $title,
157 'mode' => $mode
158 ]
159 );
160 return $response;
161 }
162
163 /**
164 * Edit action
165 *
166 * @param Request $request PSR Request
167 * @param Response $response PSR Response
168 * @param integer $id Title id
169 *
170 * @return Response
171 */
172 public function doEdit(Request $request, Response $response, int $id): Response
173 {
174 return $this->store($request, $response, $id);
175 }
176
177 /**
178 * Store
179 *
180 * @param Request $request PSR Request
181 * @param Response $response PSR Response
182 * @param integer $id Title id
183 *
184 * @return Response
185 */
186 public function store(Request $request, Response $response, int $id = null): Response
187 {
188 $post = $request->getParsedBody();
189
190 if (isset($post['cancel'])) {
191 return $response
192 ->withStatus(301)
193 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
194 }
195
196 $error_detected = [];
197 $msg = null;
198
199 $title = new Title($id);
200 $title->short = $post['short_label'];
201 $title->long = $post['long_label'];
202 if ((isset($post['short_label']) && $post['short_label'] != '') && (isset($post['long_label']) && $post['long_label'] != '')) {
203 $res = $title->store($this->zdb);
204 } else {
205 $res = false;
206 $error_detected[] = _T("Missing required title's short or long form!");
207 }
208 $redirect_uri = $this->redirectUri($this->getArgs($request));
209
210 if (!$res) {
211 if ($id === null) {
212 $error_detected[] = preg_replace(
213 '(%s)',
214 $title->short !== null ? $title->short : '',
215 _T("Title '%s' has not been added!")
216 );
217 } else {
218 $error_detected[] = preg_replace(
219 '(%s)',
220 $title->short !== null ? $title->short : '',
221 _T("Title '%s' has not been modified!")
222 );
223
224 $redirect_uri = $this->routeparser->urlFor('editTitle', ['id' => $id]);
225 }
226 } else {
227 if ($id === null) {
228 $error_detected[] = preg_replace(
229 '(%s)',
230 $title->short,
231 _T("Title '%s' has been successfully added.")
232 );
233 } else {
234 $msg = preg_replace(
235 '(%s)',
236 $title->short,
237 _T("Title '%s' has been successfully modified.")
238 );
239 }
240 }
241
242 if (count($error_detected) > 0) {
243 foreach ($error_detected as $error) {
244 $this->flash->addMessage(
245 'error_detected',
246 $error
247 );
248 }
249 } else {
250 $this->flash->addMessage(
251 'success_detected',
252 $msg
253 );
254 }
255
256 return $response
257 ->withStatus(301)
258 ->withHeader('Location', $redirect_uri);
259 }
260
261 // /CRUD - Update
262 // CRUD - Delete
263
264 /**
265 * Get redirection URI
266 *
267 * @param array $args Route arguments
268 *
269 * @return string
270 */
271 public function redirectUri(array $args)
272 {
273 return $this->routeparser->urlFor('titles');
274 }
275
276 /**
277 * Get form URI
278 *
279 * @param array $args Route arguments
280 *
281 * @return string
282 */
283 public function formUri(array $args)
284 {
285 return $this->routeparser->urlFor(
286 'doRemoveTitle',
287 ['id' => $args['id']]
288 );
289 }
290
291 /**
292 * Get confirmation removal page title
293 *
294 * @param array $args Route arguments
295 *
296 * @return string
297 */
298 public function confirmRemoveTitle(array $args)
299 {
300 $title = new Title((int)$args['id']);
301 return sprintf(
302 _T('Remove title %1$s'),
303 $title->short
304 );
305 }
306
307 /**
308 * Remove object
309 *
310 * @param array $args Route arguments
311 * @param array $post POST values
312 *
313 * @return boolean
314 */
315 protected function doDelete(array $args, array $post)
316 {
317 $title = new Title((int)$args['id']);
318 return $title->remove($this->zdb);
319 }
320
321 // /CRUD - Delete
322 }