3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette abstract controller
10 * Copyright © 2019-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
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
37 namespace Galette\Controllers
;
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
;
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
;
61 * Galette abstract controller
63 * @category Controllers
64 * @name AbstractController
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
73 abstract class AbstractController
85 protected Login
$login;
90 protected Preferences
$preferences;
100 protected Logo
$logo;
105 protected PrintLogo
$print_logo;
110 protected Plugins
$plugins;
115 protected RouteParser
$routeparser;
120 protected History
$history;
125 protected I18n
$i18n;
130 protected L10n
$l10n;
135 protected Session
$session;
140 protected Messages
$flash;
145 protected FieldsConfig
$fields_config;
150 protected ListsConfig
$lists_config;
154 #[Inject("members_fields")]
155 protected array $members_fields;
159 #[Inject("members_form_fields")]
160 protected array $members_form_fields;
164 #[Inject("members_fields_cats")]
165 protected array $members_fields_cats;
170 * @param ContainerInterface $container Container instance
172 public function __construct(ContainerInterface
$container)
174 $this->container
= $container;
178 * Galette redirection workflow
179 * Each user have a default homepage depending on it status (logged in or not, its credentials, etc.
181 * @param Request $request PSR Request
182 * @param Response $response PSR Response
186 protected function galetteRedirect(Request
$request, Response
$response)
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);
196 if ($this->login
->isLogged()) {
198 if ($this->session
->urlRedirect
!== null) {
199 $urlRedirect = $this->session
->urlRedirect
;
200 $this->session
->urlRedirect
= null;
203 if ($urlRedirect !== null) {
206 ->withHeader('Location', $urlRedirect);
209 $this->login
->isSuperAdmin()
210 ||
$this->login
->isAdmin()
211 ||
$this->login
->isStaff()
214 !isset($_COOKIE['show_galette_dashboard'])
215 ||
$_COOKIE['show_galette_dashboard'] == 1
219 ->withHeader('Location', $this->routeparser
->urlFor('dashboard'));
223 ->withHeader('Location', $this->routeparser
->urlFor('members'));
228 ->withHeader('Location', $this->routeparser
->urlFor('dashboard'));
234 ->withHeader('Location', $this->routeparser
->urlFor('login'));
239 * Get route arguments
240 * php-di bridge pass each variable, not an array of all arguments
242 * @param Request $request PSR Request
246 protected function getArgs(Request
$request): array
248 $routeContext = RouteContext
::fromRequest($request);
249 $route = $routeContext->getRoute();
250 $args = $route->getArguments();
255 * Get a JSON response
257 * @param Response $response Response instance
258 * @param array $data Data to send
259 * @param int $status HTTP status code
263 protected function withJson(Response
$response, array $data, int $status = 200): Response
265 $response = $response->withStatus($status);
266 $response = $response->withHeader('Content-Type', 'application/json');
267 $response->getBody()->write(json_encode($data));