3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette history controller
10 * Copyright © 2020-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/>.
27 * @category Controllers
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
37 namespace Galette\Controllers
;
40 use Slim\Psr7\Request
;
41 use Slim\Psr7\Response
;
42 use Galette\Core\History
;
43 use Galette\Filters\HistoryList
;
47 * Galette history controller
49 * @category Controllers
50 * @name HistoryController
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
59 class HistoryController
extends AbstractController
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
77 if (isset($this->session
->filter_history
)) {
78 $filters = $this->session
->filter_history
;
80 $filters = new HistoryList();
83 if (isset($request->getQueryParams()['nbshow'])) {
84 $filters->show
= $request->getQueryParams()['nbshow'];
87 if ($option !== null) {
90 $filters->current_page
= (int)$value;
93 $filters->orderby
= $value;
98 $this->session
->filter_history
= $filters;
100 $this->history
->setFilters($filters);
101 $logs = $this->history
->getHistory();
103 //assign pagination variables to the template and add pagination links
104 $this->history
->filters
->setViewPagination($this->routeparser
, $this->view
);
109 'pages/history.html.twig',
111 'page_title' => _T("Logs"),
113 'history' => $this->history
122 * @param Request $request PSR Request
123 * @param Response $response PSR Response
127 public function historyFilter(Request
$request, Response
$response): Response
129 $post = $request->getParsedBody();
130 $error_detected = [];
132 if ($this->session
->filter_history
!== null) {
133 $filters = $this->session
->filter_history
;
135 $filters = new HistoryList();
138 if (isset($post['clear_filter'])) {
142 (isset($post['nbshow']) && is_numeric($post['nbshow']))
144 $filters->show
= (int)$post['nbshow'];
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'];
151 if (isset($post['end_date_filter'])) {
152 $filters->end_date_filter
= $post['end_date_filter'];
156 if (isset($post['user_filter'])) {
157 $filters->user_filter
= $post['user_filter'];
160 if (isset($post['action_filter'])) {
161 $filters->action_filter
= $post['action_filter'];
165 $this->session
->filter_history
= $filters;
167 if (count($error_detected) > 0) {
169 foreach ($error_detected as $error) {
170 $this->flash
->addMessage(
179 ->withHeader('Location', $this->routeparser
->urlFor('history'));
185 * @param Request $request PSR Request
186 * @param Response $response PSR Response
190 public function flushHistory(Request
$request, Response
$response): Response
192 $post = $request->getParsedBody();
193 $ajax = isset($post['ajax']) && $post['ajax'] === 'true';
196 $uri = isset($post['redirect_uri']) ?
197 $post['redirect_uri'] : $this->routeparser
->urlFor('slash');
199 if (!isset($post['confirm'])) {
200 $this->flash
->addMessage(
202 _T("Removal has not been confirmed!")
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;
212 $this->flash
->addMessage(
214 _T('Logs have been flushed!')
217 } catch (Throwable
$e) {
218 $this->zdb
->connection
->rollBack();
220 'An error occurred flushing logs | ' . $e->getMessage(),
224 $this->flash
->addMessage(
226 _T('An error occurred trying to flush logs :(')
234 ->withHeader('Location', $uri);
236 return $this->withJson(
239 'success' => $success
246 * History flush confirmation
248 * @param Request $request PSR Request
249 * @param Response $response PSR Response
253 public function confirmHistoryFlush(Request
$request, Response
$response): Response
256 'redirect_uri' => $this->routeparser
->urlFor('history')
262 'modals/confirm_removal.html.twig',
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'],