]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/HistoryController.php
1faddc231d303341f0d70ca2c8af1e1f2b3bd86b
[galette.git] / galette / lib / Galette / Controllers / HistoryController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette history controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 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 Controllers
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 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 - 2020-05-02
36 */
37
38 namespace Galette\Controllers;
39
40 use Slim\Http\Request;
41 use Slim\Http\Response;
42 use Galette\Core\History;
43 use Galette\Filters\HistoryList;
44 use Analog\Analog;
45
46 /**
47 * Galette history controller
48 *
49 * @category Controllers
50 * @name HistoryController
51 * @package Galette
52 * @author Johan Cwiklinski <johan@x-tnd.be>
53 * @copyright 2020 The Galette Team
54 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
55 * @link http://galette.tuxfamily.org
56 * @since Available since 0.9.4dev - 2020-05-02
57 */
58
59 class HistoryController extends AbstractController
60 {
61 /**
62 * History page
63 *
64 * @param Request $request PSR Request
65 * @param Response $response PSR Response
66 * @param array $args Request arguments
67 *
68 * @return Response
69 */
70 public function history(Request $request, Response $response, array $args = []): Response
71 {
72 $option = null;
73 if (isset($args['option'])) {
74 $option = $args['option'];
75 }
76 $value = null;
77 if (isset($args['value'])) {
78 $value = $args['value'];
79 }
80
81 if (isset($this->session->filter_history)) {
82 $filters = $this->session->filter_history;
83 } else {
84 $filters = new HistoryList();
85 }
86
87 if (isset($request->getQueryParams()['nbshow'])) {
88 $filters->show = $request->getQueryParams()['nbshow'];
89 }
90
91 if ($option !== null) {
92 switch ($option) {
93 case 'page':
94 $filters->current_page = (int)$value;
95 break;
96 case 'order':
97 $filters->orderby = $value;
98 break;
99 }
100 }
101
102 $this->session->filter_history = $filters;
103
104 $this->history->setFilters($filters);
105 $logs = $this->history->getHistory();
106
107 //assign pagination variables to the template and add pagination links
108 $this->history->filters->setSmartyPagination($this->router, $this->view->getSmarty());
109
110 // display page
111 $this->view->render(
112 $response,
113 'history.tpl',
114 array(
115 'page_title' => _T("Logs"),
116 'logs' => $logs,
117 'history' => $this->history
118 )
119 );
120 return $response;
121 }
122
123 /**
124 * History filtering
125 *
126 * @param Request $request PSR Request
127 * @param Response $response PSR Response
128 *
129 * @return Response
130 */
131 public function historyFilter(Request $request, Response $response): Response
132 {
133 $post = $request->getParsedBody();
134 $error_detected = [];
135
136 if ($this->session->filter_history !== null) {
137 $filters = $this->session->filter_history;
138 } else {
139 $filters = new HistoryList();
140 }
141
142 if (isset($post['clear_filter'])) {
143 $filters->reinit();
144 } else {
145 if (
146 (isset($post['nbshow']) && is_numeric($post['nbshow']))
147 ) {
148 $filters->show = $post['nbshow'];
149 }
150
151 if (isset($post['end_date_filter']) || isset($post['start_date_filter'])) {
152 try {
153 if (isset($post['start_date_filter'])) {
154 $field = _T("start date filter");
155 $filters->start_date_filter = $post['start_date_filter'];
156 }
157 if (isset($post['end_date_filter'])) {
158 $field = _T("end date filter");
159 $filters->end_date_filter = $post['end_date_filter'];
160 }
161 } catch (\Exception $e) {
162 $error_detected[] = $e->getMessage();
163 }
164 }
165
166 if (isset($post['user_filter'])) {
167 $filters->user_filter = $post['user_filter'];
168 }
169
170 if (isset($post['action_filter'])) {
171 $filters->action_filter = $post['action_filter'];
172 }
173 }
174
175 $this->session->filter_history = $filters;
176
177 if (count($error_detected) > 0) {
178 //report errors
179 foreach ($error_detected as $error) {
180 $this->flash->addMessage(
181 'error_detected',
182 $error
183 );
184 }
185 }
186
187 return $response
188 ->withStatus(301)
189 ->withHeader('Location', $this->router->pathFor('history'));
190 }
191
192 /**
193 * History flush
194 *
195 * @param Request $request PSR Request
196 * @param Response $response PSR Response
197 *
198 * @return Response
199 */
200 public function flushHistory(Request $request, Response $response): Response
201 {
202 $post = $request->getParsedBody();
203 $ajax = isset($post['ajax']) && $post['ajax'] === 'true';
204 $success = false;
205
206 $uri = isset($post['redirect_uri']) ?
207 $post['redirect_uri'] : $this->router->pathFor('slash');
208
209 if (!isset($post['confirm'])) {
210 $this->flash->addMessage(
211 'error_detected',
212 _T("Removal has not been confirmed!")
213 );
214 } else {
215 try {
216 $this->history->clean();
217 //reinitialize object after flush
218 $this->history = new History($this->zdb, $this->login);
219 $filters = new HistoryList();
220 $this->session->filter_history = $filters;
221
222 $this->flash->addMessage(
223 'success_detected',
224 _T('Logs have been flushed!')
225 );
226 $success = true;
227 } catch (\Exception $e) {
228 $this->zdb->connection->rollBack();
229 Analog::log(
230 'An error occurred flushing logs | ' . $e->getMessage(),
231 Analog::ERROR
232 );
233
234 $this->flash->addMessage(
235 'error_detected',
236 _T('An error occurred trying to flush logs :(')
237 );
238 }
239 }
240
241 if (!$ajax) {
242 return $response
243 ->withStatus(301)
244 ->withHeader('Location', $uri);
245 } else {
246 return $response->withJson(
247 [
248 'success' => $success
249 ]
250 );
251 }
252 }
253
254 /**
255 * History flush rconfirmation
256 *
257 * @param Request $request PSR Request
258 * @param Response $response PSR Response
259 *
260 * @return Response
261 */
262 public function confirmHistoryFlush(Request $request, Response $response): Response
263 {
264 $data = [
265 'redirect_uri' => $this->router->pathFor('history')
266 ];
267
268 // display page
269 $this->view->render(
270 $response,
271 'confirm_removal.tpl',
272 array(
273 'mode' => $request->isXhr() ? 'ajax' : '',
274 'page_title' => _T('Flush the logs'),
275 'form_url' => $this->router->pathFor('doFlushHistory'),
276 'cancel_uri' => $data['redirect_uri'],
277 'data' => $data
278 )
279 );
280 return $response;
281 }
282 }