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