]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/HistoryController.php
5bf8b1a3b4a03e04673a36ef591c7ee888f05a0e
[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-2023 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-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 - 2020-05-02
35 */
36
37 namespace Galette\Controllers;
38
39 use Throwable;
40 use Slim\Psr7\Request;
41 use Slim\Psr7\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-2023 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 string $option One of 'page' or 'order'
67 * @param string|integer $value Value of the option
68 *
69 * @return Response
70 */
71 public function list(
72 Request $request,
73 Response $response,
74 $option = null,
75 $value = null
76 ): Response {
77 if (isset($this->session->filter_history)) {
78 $filters = $this->session->filter_history;
79 } else {
80 $filters = new HistoryList();
81 }
82
83 if (isset($request->getQueryParams()['nbshow'])) {
84 $filters->show = $request->getQueryParams()['nbshow'];
85 }
86
87 if ($option !== null) {
88 switch ($option) {
89 case 'page':
90 $filters->current_page = (int)$value;
91 break;
92 case 'order':
93 $filters->orderby = $value;
94 break;
95 }
96 }
97
98 $this->session->filter_history = $filters;
99
100 $this->history->setFilters($filters);
101 $logs = $this->history->getHistory();
102
103 //assign pagination variables to the template and add pagination links
104 $this->history->filters->setViewPagination($this->routeparser, $this->view);
105
106 // display page
107 $this->view->render(
108 $response,
109 'pages/history.html.twig',
110 array(
111 'page_title' => _T("Logs"),
112 'logs' => $logs,
113 'history' => $this->history
114 )
115 );
116 return $response;
117 }
118
119 /**
120 * History filtering
121 *
122 * @param Request $request PSR Request
123 * @param Response $response PSR Response
124 *
125 * @return Response
126 */
127 public function historyFilter(Request $request, Response $response): Response
128 {
129 $post = $request->getParsedBody();
130 $error_detected = [];
131
132 if ($this->session->filter_history !== null) {
133 $filters = $this->session->filter_history;
134 } else {
135 $filters = new HistoryList();
136 }
137
138 if (isset($post['clear_filter'])) {
139 $filters->reinit();
140 } else {
141 if (
142 (isset($post['nbshow']) && is_numeric($post['nbshow']))
143 ) {
144 $filters->show = (int)$post['nbshow'];
145 }
146
147 if (isset($post['end_date_filter']) || isset($post['start_date_filter'])) {
148 if (isset($post['start_date_filter'])) {
149 $filters->start_date_filter = $post['start_date_filter'];
150 }
151 if (isset($post['end_date_filter'])) {
152 $filters->end_date_filter = $post['end_date_filter'];
153 }
154 }
155
156 if (isset($post['user_filter'])) {
157 $filters->user_filter = $post['user_filter'];
158 }
159
160 if (isset($post['action_filter'])) {
161 $filters->action_filter = $post['action_filter'];
162 }
163 }
164
165 $this->session->filter_history = $filters;
166
167 if (count($error_detected) > 0) {
168 //report errors
169 foreach ($error_detected as $error) {
170 $this->flash->addMessage(
171 'error_detected',
172 $error
173 );
174 }
175 }
176
177 return $response
178 ->withStatus(301)
179 ->withHeader('Location', $this->routeparser->urlFor('history'));
180 }
181
182 /**
183 * History flush
184 *
185 * @param Request $request PSR Request
186 * @param Response $response PSR Response
187 *
188 * @return Response
189 */
190 public function flushHistory(Request $request, Response $response): Response
191 {
192 $post = $request->getParsedBody();
193 $ajax = isset($post['ajax']) && $post['ajax'] === 'true';
194 $success = false;
195
196 $uri = isset($post['redirect_uri']) ?
197 $post['redirect_uri'] : $this->routeparser->urlFor('slash');
198
199 if (!isset($post['confirm'])) {
200 $this->flash->addMessage(
201 'error_detected',
202 _T("Removal has not been confirmed!")
203 );
204 } else {
205 try {
206 $this->history->clean();
207 //reinitialize object after flush
208 $this->history = new History($this->zdb, $this->login, $this->preferences);
209 $filters = new HistoryList();
210 $this->session->filter_history = $filters;
211
212 $this->flash->addMessage(
213 'success_detected',
214 _T('Logs have been flushed!')
215 );
216 $success = true;
217 } catch (Throwable $e) {
218 $this->zdb->connection->rollBack();
219 Analog::log(
220 'An error occurred flushing logs | ' . $e->getMessage(),
221 Analog::ERROR
222 );
223
224 $this->flash->addMessage(
225 'error_detected',
226 _T('An error occurred trying to flush logs :(')
227 );
228 }
229 }
230
231 if (!$ajax) {
232 return $response
233 ->withStatus(301)
234 ->withHeader('Location', $uri);
235 } else {
236 return $this->withJson(
237 $response,
238 [
239 'success' => $success
240 ]
241 );
242 }
243 }
244
245 /**
246 * History flush confirmation
247 *
248 * @param Request $request PSR Request
249 * @param Response $response PSR Response
250 *
251 * @return Response
252 */
253 public function confirmHistoryFlush(Request $request, Response $response): Response
254 {
255 $data = [
256 'redirect_uri' => $this->routeparser->urlFor('history')
257 ];
258
259 // display page
260 $this->view->render(
261 $response,
262 'modals/confirm_removal.html.twig',
263 array(
264 'mode' => ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ? 'ajax' : '',
265 'page_title' => _T('Flush the logs'),
266 'form_url' => $this->routeparser->urlFor('doFlushHistory'),
267 'cancel_uri' => $data['redirect_uri'],
268 'data' => $data
269 )
270 );
271 return $response;
272 }
273 }