]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/AbstractController.php
Migrate CSV related routes 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 * @Inject
121 * @var array
122 */
123 protected $members_fields;
124 /**
125 * @Inject
126 * @var array
127 */
128 protected $members_fields_cats;
129
130 /**
131 * @Inject
132 * @var Galette\Handlers\NotFound
133 */
134 protected $notFoundHandler;
135
136 /**
137 * Constructor
138 *
139 * @param ContainerInterface $container COntainer instance
140 */
141 public function __construct(ContainerInterface $container)
142 {
143 $this->container = $container;
144 //set variosu services we need
145 $this->zdb = $container->get('zdb');
146 $this->login = $container->get('login');
147 $this->preferences = $container->get('preferences');
148 $this->view = $container->get('view');
149 $this->logo = $container->get('logo');
150 $this->print_logo = $container->get('print_logo');
151 $this->plugins = $container->get('plugins');
152 $this->router = $container->get('router');
153 $this->history = $container->get('history');
154 $this->i18n = $container->get('i18n');
155 $this->session = $container->get('session');
156 $this->flash = $container->get('flash');
157 $this->fields_config = $container->get('fields_config');
158 $this->notFoundHandler = $container->get('notFoundHandler');
159 $this->members_fields = $container->get('members_fields');
160 $this->members_fields_cats = $container->get('members_fields_cats');
161 }
162
163 /**
164 * Galette redirection workflow
165 * Each user have a default homepage depending on it status (logged in or not, its credentials, etc.
166 *
167 * @param Request $request PSR Request
168 * @param Response $response PSR Response
169 * @param array $args Request arguments ['r']
170 *
171 * @return void
172 */
173 protected function galetteRedirect(Request $request, Response $response, array $args = [])
174 {
175 //reinject flash messages so they're not lost
176 $flashes = $this->flash->getMessages();
177 foreach ($flashes as $type => $messages) {
178 foreach ($messages as $message) {
179 $this->container->get('flash')->addMessage($type, $message);
180 }
181 }
182
183 if ($this->login->isLogged()) {
184 $urlRedirect = null;
185 if ($this->session->urlRedirect !== null) {
186 $urlRedirect = $this->getGaletteBaseUrl($request) . $this->session->urlRedirect;
187 $this->session->urlRedirect = null;
188 }
189
190 if ($urlRedirect !== null) {
191 return $response
192 ->withStatus(301)
193 ->withHeader('Location', $urlRedirect);
194 } else {
195 if ($this->login->isSuperAdmin()
196 || $this->login->isAdmin()
197 || $this->login->isStaff()
198 ) {
199 if (!isset($_COOKIE['show_galette_dashboard'])
200 || $_COOKIE['show_galette_dashboard'] == 1
201 ) {
202 return $response
203 ->withStatus(301)
204 ->withHeader('Location', $this->router->pathFor('dashboard'));
205 } else {
206 return $response
207 ->withStatus(301)
208 ->withHeader('Location', $this->router->pathFor('members'));
209 }
210 } else {
211 return $response
212 ->withStatus(301)
213 ->withHeader('Location', $this->router->pathFor('dashboard'));
214 }
215 }
216 } else {
217 return $response
218 ->withStatus(301)
219 ->withHeader('Location', $this->router->pathFor('login'));
220 }
221 }
222
223 /**
224 * Get base URL fixed for proxies
225 *
226 * @param Request $request PSR Request
227 *
228 * @return string
229 */
230 private function getGaletteBaseUrl(Request $request)
231 {
232 $url = preg_replace(
233 [
234 '|index\.php|',
235 '|https?://' . $_SERVER['HTTP_HOST'] . '(:\d+)?' . '|'
236 ],
237 ['', ''],
238 $request->getUri()->getBaseUrl()
239 );
240 if (strlen($url) && substr($url, -1) !== '/') {
241 $url .= '/';
242 }
243 return $url;
244 }
245 }