]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/AbstractController.php
Migrate dashboard, preferences and charts to a controller class
[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-2020 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-2020 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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2019-12-02
36 */
37
38 namespace Galette\Controllers;
39
40 use Psr\Container\ContainerInterface;
41 use Slim\Http\Request;
42 use Slim\Http\Response;
43
44 /**
45 * Galette abstract controller
46 *
47 * @category Controllers
48 * @name AbstractController
49 * @package Galette
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2019-2020 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.9.4dev - 2019-12-02
55 */
56
57 abstract class AbstractController
58 {
59 private $container;
60 /**
61 * @Inject
62 * @var Galette\Core\Db
63 */
64 protected $zdb;
65 /**
66 * @Inject
67 * @var Galette\Core\Login
68 */
69 protected $login;
70 /**
71 * @Inject
72 * @var Galette\Core\Preferences
73 */
74 protected $preferences;
75 /**
76 * @Inject
77 * @var Slim\Views\Smarty
78 */
79 protected $view;
80 /**
81 * @Inject
82 * @var Galette\Core\Logo
83 */
84 protected $logo;
85 /**
86 * @Inject
87 * @var Galette\Core\Plugins
88 */
89 protected $plugins;
90 /**
91 * @Inject
92 * @var Slim\Router
93 */
94 protected $router;
95 /**
96 * @Inject
97 * @var Galette\Core\History
98 */
99 protected $history;
100 /**
101 * @Inject
102 * @var Galette\Core\I18n
103 */
104 protected $i18n;
105 /**
106 * @Inject("session")
107 */
108 protected $session;
109 /**
110 * @Inject
111 * @var Slim\Flash\Messages
112 */
113 protected $flash;
114 /**
115 * @Inject
116 * @var Galette\Entity\FieldsConfig
117 */
118 protected $fields_config;
119
120 /**
121 * @Inject
122 * @var Galette\Handlers\NotFound
123 */
124 protected $notFoundHandler;
125
126 /**
127 * Constructor
128 *
129 * @param ContainerInterface $container COntainer instance
130 */
131 public function __construct(ContainerInterface $container)
132 {
133 $this->container = $container;
134 //set variosu services we need
135 $this->zdb = $container->get('zdb');
136 $this->login = $container->get('login');
137 $this->preferences = $container->get('preferences');
138 $this->view = $container->get('view');
139 $this->logo = $container->get('logo');
140 $this->print_logo = $container->get('print_logo');
141 $this->plugins = $container->get('plugins');
142 $this->router = $container->get('router');
143 $this->history = $container->get('history');
144 $this->i18n = $container->get('i18n');
145 $this->session = $container->get('session');
146 $this->flash = $container->get('flash');
147 $this->fields_config = $container->get('fields_config');
148 $this->notFoundHandler = $container->get('notFoundHandler');
149 }
150
151 /**
152 * Galette redirection workflow
153 * Each user have a default homepage depending on it status (logged in or not, its credentials, etc.
154 *
155 * @param Request $request PSR Request
156 * @param Response $response PSR Response
157 * @param array $args Request arguments ['r']
158 *
159 * @return void
160 */
161 protected function galetteRedirect(Request $request, Response $response, array $args = [])
162 {
163 //reinject flash messages so they're not lost
164 $flashes = $this->flash->getMessages();
165 foreach ($flashes as $type => $messages) {
166 foreach ($messages as $message) {
167 $this->container->get('flash')->addMessage($type, $message);
168 }
169 }
170
171 if ($this->login->isLogged()) {
172 $urlRedirect = null;
173 if ($this->session->urlRedirect !== null) {
174 $urlRedirect = $this->getGaletteBaseUrl($request) . $this->session->urlRedirect;
175 $this->session->urlRedirect = null;
176 }
177
178 if ($urlRedirect !== null) {
179 return $response
180 ->withStatus(301)
181 ->withHeader('Location', $urlRedirect);
182 } else {
183 if ($this->login->isSuperAdmin()
184 || $this->login->isAdmin()
185 || $this->login->isStaff()
186 ) {
187 if (!isset($_COOKIE['show_galette_dashboard'])
188 || $_COOKIE['show_galette_dashboard'] == 1
189 ) {
190 return $response
191 ->withStatus(301)
192 ->withHeader('Location', $this->router->pathFor('dashboard'));
193 } else {
194 return $response
195 ->withStatus(301)
196 ->withHeader('Location', $this->router->pathFor('members'));
197 }
198 } else {
199 return $response
200 ->withStatus(301)
201 ->withHeader('Location', $this->router->pathFor('dashboard'));
202 }
203 }
204 } else {
205 return $response
206 ->withStatus(301)
207 ->withHeader('Location', $this->router->pathFor('login'));
208 }
209 }
210
211 /**
212 * Get base URL fixed for proxies
213 *
214 * @param Request $request PSR Request
215 *
216 * @return string
217 */
218 private function getGaletteBaseUrl(Request $request)
219 {
220 $url = preg_replace(
221 [
222 '|index\.php|',
223 '|https?://' . $_SERVER['HTTP_HOST'] . '(:\d+)?' . '|'
224 ],
225 ['', ''],
226 $request->getUri()->getBaseUrl()
227 );
228 if (strlen($url) && substr($url, -1) !== '/') {
229 $url .= '/';
230 }
231 return $url;
232 }
233 }