]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/TitlesController.php
9988cf3edf50d8676443f3c369d1d52d76566918
[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 * @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\Http\Request;
41 use Slim\Http\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-2020 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 }
75
76 /**
77 * Add action
78 *
79 * @param Request $request PSR Request
80 * @param Response $response PSR Response
81 *
82 * @return Response
83 */
84 public function doAdd(Request $request, Response $response): Response
85 {
86 return $this->store($request, $response, null);
87 }
88
89 // /CRUD - Create
90 // CRUD - Read
91
92 /**
93 * Titles list page
94 *
95 * @param Request $request PSR Request
96 * @param Response $response PSR Response
97 * @param string $option One of 'page' or 'order'
98 * @param string|integer $value Value of the option
99 *
100 * @return Response
101 */
102 public function list(Request $request, Response $response, $option = null, $value = null): Response
103 {
104 $titles = Titles::getList($this->zdb);
105
106 // display page
107 $this->view->render(
108 $response,
109 'gestion_titres.tpl',
110 [
111 'page_title' => _T("Titles management"),
112 'titles_list' => $titles
113 ]
114 );
115 return $response;
116 }
117
118 /**
119 * Titles filtering
120 *
121 * @param Request $request PSR Request
122 * @param Response $response PSR Response
123 *
124 * @return Response
125 */
126 public function filter(Request $request, Response $response): Response
127 {
128 //no filtering
129 }
130
131 // /CRUD - Read
132 // CRUD - Update
133
134 /**
135 * Edit page
136 *
137 * @param Request $request PSR Request
138 * @param Response $response PSR Response
139 * @param integer $id Title id
140 *
141 * @return Response
142 */
143 public function edit(Request $request, Response $response, int $id): Response
144 {
145 $title = new Title($id);
146
147 // display page
148 $this->view->render(
149 $response,
150 'edit_title.tpl',
151 [
152 'page_title' => _T("Edit title"),
153 'title' => $title
154 ]
155 );
156 return $response;
157 }
158
159 /**
160 * Edit action
161 *
162 * @param Request $request PSR Request
163 * @param Response $response PSR Response
164 * @param integer $id Title id
165 *
166 * @return Response
167 */
168 public function doEdit(Request $request, Response $response, int $id): Response
169 {
170 return $this->store($request, $response, $id);
171 }
172
173 /**
174 * Store
175 *
176 * @param Request $request PSR Request
177 * @param Response $response PSR Response
178 * @param integer $id Title id
179 *
180 * @return Response
181 */
182 public function store(Request $request, Response $response, int $id = null): Response
183 {
184 $post = $request->getParsedBody();
185
186 if (isset($post['cancel'])) {
187 return $response
188 ->withStatus(301)
189 ->withHeader('Location', $this->cancelUri($this->getArgs($request)));
190 }
191
192 $title = new Title((int)$id);
193 $title->short = $post['short_label'];
194 $title->long = $post['long_label'];
195 $res = $title->store($this->zdb);
196 $redirect_uri = $this->redirectUri($this->getArgs($request));
197
198 if (!$res) {
199 if ($id === null) {
200 $this->flash->addMessage(
201 'error_detected',
202 preg_replace(
203 '(%s)',
204 $title->short,
205 _T("Title '%s' has not been added!")
206 )
207 );
208 } else {
209 $this->flash->addMessage(
210 'error_detected',
211 preg_replace(
212 '(%s)',
213 $title->short,
214 _T("Title '%s' has not been modified!")
215 )
216 );
217
218 $redirect_uri = $this->router->pathFor('editTitle', ['id' => $id]);
219 }
220 } else {
221 if ($id === null) {
222 $this->flash->addMessage(
223 'success_detected',
224 preg_replace(
225 '(%s)',
226 $title->short,
227 _T("Title '%s' has been successfully added.")
228 )
229 );
230 } else {
231 $this->flash->addMessage(
232 'success_detected',
233 preg_replace(
234 '(%s)',
235 $title->short,
236 _T("Title '%s' has been successfully modified.")
237 )
238 );
239 }
240 }
241 return $response
242 ->withStatus(301)
243 ->withHeader('Location', $redirect_uri);
244 }
245
246 // /CRUD - Update
247 // CRUD - Delete
248
249 /**
250 * Get redirection URI
251 *
252 * @param array $args Route arguments
253 *
254 * @return string
255 */
256 public function redirectUri(array $args)
257 {
258 return $this->router->pathFor('titles');
259 }
260
261 /**
262 * Get form URI
263 *
264 * @param array $args Route arguments
265 *
266 * @return string
267 */
268 public function formUri(array $args)
269 {
270 return $this->router->pathFor(
271 'doRemoveTitle',
272 ['id' => $args['id']]
273 );
274 }
275
276 /**
277 * Get confirmation removal page title
278 *
279 * @param array $args Route arguments
280 *
281 * @return string
282 */
283 public function confirmRemoveTitle(array $args)
284 {
285 $title = new Title((int)$args['id']);
286 return sprintf(
287 _T('Remove title %1$s'),
288 $title->short
289 );
290 }
291
292 /**
293 * Remove object
294 *
295 * @param array $args Route arguments
296 * @param array $post POST values
297 *
298 * @return boolean
299 */
300 protected function doDelete(array $args, array $post)
301 {
302 $title = new Title((int)$args['id']);
303 return $title->remove($this->zdb);
304 }
305
306 // /CRUD - Delete
307 }