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
;
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
;
46 * Galette abstract controller
48 * @category Controllers
49 * @name AbstractController
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
58 abstract class AbstractController
62 * @var \Galette\Core\Db
67 * @var \Galette\Core\Login
72 * @var \Galette\Core\Preferences
75 protected $preferences;
77 * @var \Slim\Views\Twig
81 * @var \Galette\Core\Logo
86 * @var \Galette\Core\PrintLogo
89 protected $print_logo;
91 * @var \Galette\Core\Plugins
96 * @var \Slim\Routing\RouteParser
99 protected $routeparser;
101 * @var \Galette\Core\History
106 * @var \Galette\Core\I18n
111 * @var \Galette\Core\L10n
121 * @var \Slim\Flash\Messages
126 * @var \Galette\Entity\FieldsConfig
129 protected $fields_config;
131 * @var \Galette\Entity\ListsConfig
134 protected $lists_config;
138 #[Inject("members_fields")]
139 protected $members_fields;
143 #[Inject("members_form_fields")]
144 protected $members_form_fields;
148 #[Inject("members_fields_cats")]
149 protected $members_fields_cats;
154 * @param ContainerInterface $container Container instance
156 public function __construct(ContainerInterface
$container)
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');
181 * Galette redirection workflow
182 * Each user have a default homepage depending on it status (logged in or not, its credentials, etc.
184 * @param Request $request PSR Request
185 * @param Response $response PSR Response
189 protected function galetteRedirect(Request
$request, Response
$response)
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);
199 if ($this->login
->isLogged()) {
201 if ($this->session
->urlRedirect
!== null) {
202 $urlRedirect = $this->session
->urlRedirect
;
203 $this->session
->urlRedirect
= null;
206 if ($urlRedirect !== null) {
209 ->withHeader('Location', $urlRedirect);
212 $this->login
->isSuperAdmin()
213 ||
$this->login
->isAdmin()
214 ||
$this->login
->isStaff()
217 !isset($_COOKIE['show_galette_dashboard'])
218 ||
$_COOKIE['show_galette_dashboard'] == 1
222 ->withHeader('Location', $this->routeparser
->urlFor('dashboard'));
226 ->withHeader('Location', $this->routeparser
->urlFor('members'));
231 ->withHeader('Location', $this->routeparser
->urlFor('dashboard'));
237 ->withHeader('Location', $this->routeparser
->urlFor('login'));
242 * Get route arguments
243 * php-di bridge pass each variable, not an array of all arguments
245 * @param Request $request PSR Request
249 protected function getArgs(Request
$request): array
251 $routeContext = RouteContext
::fromRequest($request);
252 $route = $routeContext->getRoute();
253 $args = $route->getArguments();
258 * Get a JSON response
260 * @param Response $response Response instance
261 * @param array $data Data to send
262 * @param int $status HTTP status code
266 protected function withJson(Response
$response, array $data, int $status = 200): Response
268 $response = $response->withStatus($status);
269 $response = $response->withHeader('Content-Type', 'application/json');
270 $response->getBody()->write(json_encode($data));