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;
152 * @var \Galette\Handlers\NotFound
155 protected $notFoundHandler;*/
160 * @param ContainerInterface $container Container instance
162 public function __construct(ContainerInterface
$container)
164 $this->container
= $container;
165 //set various services we need
166 $this->zdb
= $container->get('zdb');
167 $this->login
= $container->get('login');
168 $this->preferences
= $container->get('preferences');
169 $this->view
= $container->get(\Slim\Views\Twig
::class);
170 $this->logo
= $container->get('logo');
171 $this->print_logo
= $container->get('print_logo');
172 $this->routeparser
= $container->get(RouteParser
::class);
173 $this->history
= $container->get('history');
174 $this->i18n
= $container->get('i18n');
175 $this->l10n
= $container->get('l10n');
176 $this->session
= $container->get('session');
177 $this->flash
= $container->get('flash');
178 $this->fields_config
= $container->get('fields_config');
179 $this->lists_config
= $container->get('lists_config');
180 /*$this->notFoundHandler = $container->get('notFoundHandler');*/
181 $this->members_fields
= $container->get('members_fields');
182 $this->members_form_fields
= $container->get('members_form_fields');
183 $this->members_fields_cats
= $container->get('members_fields_cats');
184 $this->plugins
= $container->get('plugins');
188 * Galette redirection workflow
189 * Each user have a default homepage depending on it status (logged in or not, its credentials, etc.
191 * @param Request $request PSR Request
192 * @param Response $response PSR Response
196 protected function galetteRedirect(Request
$request, Response
$response)
198 //reinject flash messages so they're not lost
199 $flashes = $this->flash
->getMessages();
200 foreach ($flashes as $type => $messages) {
201 foreach ($messages as $message) {
202 $this->container
->get('flash')->addMessage($type, $message);
206 if ($this->login
->isLogged()) {
208 if ($this->session
->urlRedirect
!== null) {
209 $urlRedirect = $this->getGaletteBaseUrl($request) . $this->session
->urlRedirect
;
210 $this->session
->urlRedirect
= null;
213 if ($urlRedirect !== null) {
216 ->withHeader('Location', $urlRedirect);
219 $this->login
->isSuperAdmin()
220 ||
$this->login
->isAdmin()
221 ||
$this->login
->isStaff()
224 !isset($_COOKIE['show_galette_dashboard'])
225 ||
$_COOKIE['show_galette_dashboard'] == 1
229 ->withHeader('Location', $this->routeparser
->urlFor('dashboard'));
233 ->withHeader('Location', $this->routeparser
->urlFor('members'));
238 ->withHeader('Location', $this->routeparser
->urlFor('dashboard'));
244 ->withHeader('Location', $this->routeparser
->urlFor('login'));
249 * Get base URL fixed for proxies
251 * @param Request $request PSR Request
255 private function getGaletteBaseUrl(Request
$request)
260 '|https?://' . $_SERVER['HTTP_HOST'] . '(:\d+)?' . '|'
263 $request->getUri()->getBaseUrl()
265 if (strlen($url) && substr($url, -1) !== '/') {
272 * Get route arguments
273 * php-di bridge pass each variable, not an array of all arguments
275 * @param Request $request PSR Request
279 protected function getArgs(Request
$request): array
281 $routeContext = RouteContext
::fromRequest($request);
282 $route = $routeContext->getRoute();
283 $args = $route->getArguments();
288 * Get a JSON response
290 * @param Response $response Response instance
291 * @param array $data Data to send
292 * @param int $status HTTP status code
296 protected function withJson(Response
$response, array $data, int $status = 200): Response
298 $response = $response->withStatus($status);
299 $response = $response->withHeader('Content-Type', 'application/json');
300 $response->getBody()->write(json_encode($data));