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