3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette history controller
10 * Copyright © 2020 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 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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2020-05-02
38 namespace Galette\Controllers
;
40 use Slim\Http\Request
;
41 use Slim\Http\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 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 array $args Request arguments
70 public function history(Request
$request, Response
$response, array $args = []): Response
73 if (isset($args['option'])) {
74 $option = $args['option'];
77 if (isset($args['value'])) {
78 $value = $args['value'];
81 if (isset($this->session
->filter_history
)) {
82 $filters = $this->session
->filter_history
;
84 $filters = new HistoryList();
87 if (isset($request->getQueryParams()['nbshow'])) {
88 $filters->show
= $request->getQueryParams()['nbshow'];
91 if ($option !== null) {
94 $filters->current_page
= (int)$value;
97 $filters->orderby
= $value;
102 $this->session
->filter_history
= $filters;
104 $this->history
->setFilters($filters);
105 $logs = $this->history
->getHistory();
107 //assign pagination variables to the template and add pagination links
108 $this->history
->filters
->setSmartyPagination($this->router
, $this->view
->getSmarty());
115 'page_title' => _T("Logs"),
117 'history' => $this->history
126 * @param Request $request PSR Request
127 * @param Response $response PSR Response
131 public function historyFilter(Request
$request, Response
$response): Response
133 $post = $request->getParsedBody();
134 $error_detected = [];
136 if ($this->session
->filter_history
!== null) {
137 $filters = $this->session
->filter_history
;
139 $filters = new HistoryList();
142 if (isset($post['clear_filter'])) {
146 (isset($post['nbshow']) && is_numeric($post['nbshow']))
148 $filters->show
= $post['nbshow'];
151 if (isset($post['end_date_filter']) ||
isset($post['start_date_filter'])) {
153 if (isset($post['start_date_filter'])) {
154 $field = _T("start date filter");
155 $filters->start_date_filter
= $post['start_date_filter'];
157 if (isset($post['end_date_filter'])) {
158 $field = _T("end date filter");
159 $filters->end_date_filter
= $post['end_date_filter'];
161 } catch (\Exception
$e) {
162 $error_detected[] = $e->getMessage();
166 if (isset($post['user_filter'])) {
167 $filters->user_filter
= $post['user_filter'];
170 if (isset($post['action_filter'])) {
171 $filters->action_filter
= $post['action_filter'];
175 $this->session
->filter_history
= $filters;
177 if (count($error_detected) > 0) {
179 foreach ($error_detected as $error) {
180 $this->flash
->addMessage(
189 ->withHeader('Location', $this->router
->pathFor('history'));
195 * @param Request $request PSR Request
196 * @param Response $response PSR Response
200 public function flushHistory(Request
$request, Response
$response): Response
202 $post = $request->getParsedBody();
203 $ajax = isset($post['ajax']) && $post['ajax'] === 'true';
206 $uri = isset($post['redirect_uri']) ?
207 $post['redirect_uri'] : $this->router
->pathFor('slash');
209 if (!isset($post['confirm'])) {
210 $this->flash
->addMessage(
212 _T("Removal has not been confirmed!")
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;
222 $this->flash
->addMessage(
224 _T('Logs have been flushed!')
227 } catch (\Exception
$e) {
228 $this->zdb
->connection
->rollBack();
230 'An error occurred flushing logs | ' . $e->getMessage(),
234 $this->flash
->addMessage(
236 _T('An error occurred trying to flush logs :(')
244 ->withHeader('Location', $uri);
246 return $response->withJson(
248 'success' => $success
255 * History flush rconfirmation
257 * @param Request $request PSR Request
258 * @param Response $response PSR Response
262 public function confirmHistoryFlush(Request
$request, Response
$response): Response
265 'redirect_uri' => $this->router
->pathFor('history')
271 'confirm_removal.tpl',
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'],