]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/AbstractController.php
Properly use injection
[galette.git] / galette / lib / Galette / Controllers / AbstractController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette abstract 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 Entity
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-02
35 */
36
37 namespace Galette\Controllers;
38
39 use Galette\Core\Db;
40 use Galette\Core\History;
41 use Galette\Core\I18n;
42 use Galette\Core\L10n;
43 use Galette\Core\Login;
44 use Galette\Core\Logo;
45 use Galette\Core\Plugins;
46 use Galette\Core\Preferences;
47 use Galette\Core\PrintLogo;
48 use Galette\Entity\FieldsConfig;
49 use Galette\Entity\ListsConfig;
50 use Psr\Container\ContainerInterface;
51 use RKA\Session;
52 use Slim\Flash\Messages;
53 use Slim\Psr7\Request;
54 use Slim\Psr7\Response;
55 use Slim\Routing\RouteContext;
56 use Slim\Routing\RouteParser;
57 use DI\Attribute\Inject;
58 use Slim\Views\Twig;
59
60 /**
61 * Galette abstract controller
62 *
63 * @category Controllers
64 * @name AbstractController
65 * @package Galette
66 * @author Johan Cwiklinski <johan@x-tnd.be>
67 * @copyright 2019-2023 The Galette Team
68 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
69 * @link http://galette.tuxfamily.org
70 * @since Available since 0.9.4dev - 2019-12-02
71 */
72
73 abstract class AbstractController
74 {
75 private $container;
76 /**
77 * @var Db
78 */
79 #[Inject]
80 protected Db $zdb;
81 /**
82 * @var Login
83 */
84 #[Inject]
85 protected Login $login;
86 /**
87 * @var Preferences
88 */
89 #[Inject]
90 protected Preferences $preferences;
91 /**
92 * @var Twig
93 */
94 #[Inject]
95 protected Twig $view;
96 /**
97 * @var Logo
98 */
99 #[Inject]
100 protected Logo $logo;
101 /**
102 * @var PrintLogo
103 */
104 #[Inject]
105 protected PrintLogo $print_logo;
106 /**
107 * @var Plugins
108 */
109 #[Inject]
110 protected Plugins $plugins;
111 /**
112 * @var RouteParser
113 */
114 #[Inject]
115 protected RouteParser $routeparser;
116 /**
117 * @var History
118 */
119 #[Inject]
120 protected History $history;
121 /**
122 * @var I18n
123 */
124 #[Inject]
125 protected I18n $i18n;
126 /**
127 * @var L10n
128 */
129 #[Inject]
130 protected L10n $l10n;
131 /**
132 * @var Session
133 */
134 #[Inject("session")]
135 protected Session $session;
136 /**
137 * @var Messages
138 */
139 #[Inject]
140 protected Messages $flash;
141 /**
142 * @var FieldsConfig
143 */
144 #[Inject]
145 protected FieldsConfig $fields_config;
146 /**
147 * @var ListsConfig
148 */
149 #[Inject]
150 protected ListsConfig $lists_config;
151 /**
152 * @var array
153 */
154 #[Inject("members_fields")]
155 protected array $members_fields;
156 /**
157 * @var array
158 */
159 #[Inject("members_form_fields")]
160 protected array $members_form_fields;
161 /**
162 * @var array
163 */
164 #[Inject("members_fields_cats")]
165 protected array $members_fields_cats;
166
167 /**
168 * Constructor
169 *
170 * @param ContainerInterface $container Container instance
171 */
172 public function __construct(ContainerInterface $container)
173 {
174 $this->container = $container;
175 }
176
177 /**
178 * Galette redirection workflow
179 * Each user have a default homepage depending on it status (logged in or not, its credentials, etc.
180 *
181 * @param Request $request PSR Request
182 * @param Response $response PSR Response
183 *
184 * @return Response
185 */
186 protected function galetteRedirect(Request $request, Response $response)
187 {
188 //reinject flash messages so they're not lost
189 $flashes = $this->flash->getMessages();
190 foreach ($flashes as $type => $messages) {
191 foreach ($messages as $message) {
192 $this->container->get('flash')->addMessage($type, $message);
193 }
194 }
195
196 if ($this->login->isLogged()) {
197 $urlRedirect = null;
198 if ($this->session->urlRedirect !== null) {
199 $urlRedirect = $this->session->urlRedirect;
200 $this->session->urlRedirect = null;
201 }
202
203 if ($urlRedirect !== null) {
204 return $response
205 ->withStatus(301)
206 ->withHeader('Location', $urlRedirect);
207 } else {
208 if (
209 $this->login->isSuperAdmin()
210 || $this->login->isAdmin()
211 || $this->login->isStaff()
212 ) {
213 if (
214 !isset($_COOKIE['show_galette_dashboard'])
215 || $_COOKIE['show_galette_dashboard'] == 1
216 ) {
217 return $response
218 ->withStatus(301)
219 ->withHeader('Location', $this->routeparser->urlFor('dashboard'));
220 } else {
221 return $response
222 ->withStatus(301)
223 ->withHeader('Location', $this->routeparser->urlFor('members'));
224 }
225 } else {
226 return $response
227 ->withStatus(301)
228 ->withHeader('Location', $this->routeparser->urlFor('dashboard'));
229 }
230 }
231 } else {
232 return $response
233 ->withStatus(301)
234 ->withHeader('Location', $this->routeparser->urlFor('login'));
235 }
236 }
237
238 /**
239 * Get route arguments
240 * php-di bridge pass each variable, not an array of all arguments
241 *
242 * @param Request $request PSR Request
243 *
244 * @return array
245 */
246 protected function getArgs(Request $request): array
247 {
248 $routeContext = RouteContext::fromRequest($request);
249 $route = $routeContext->getRoute();
250 $args = $route->getArguments();
251 return $args;
252 }
253
254 /**
255 * Get a JSON response
256 *
257 * @param Response $response Response instance
258 * @param array $data Data to send
259 * @param int $status HTTP status code
260 *
261 * @return Response
262 */
263 protected function withJson(Response $response, array $data, int $status = 200): Response
264 {
265 $response = $response->withStatus($status);
266 $response = $response->withHeader('Content-Type', 'application/json');
267 $response->getBody()->write(json_encode($data));
268 return $response;
269 }
270 }