]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Filters/HistoryList.php
eb4dff20c4dfa445b9405cf4f7ed5821dfa68968
4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
22 namespace Galette\Filters
;
26 use Galette\Core\Pagination
;
29 * History lists filters and paginator
31 * @author Johan Cwiklinski <johan@x-tnd.be>
33 * @property ?string $start_date_filter
34 * @property ?string $raw_start_date_filter
35 * @property ?string $end_date_filter
36 * @property ?string $raw_end_date_filter
37 * @property ?string $user_filter
38 * @property ?string $action_filter
41 class HistoryList
extends Pagination
43 public const ORDERBY_DATE
= 0;
44 public const ORDERBY_IP
= 1;
45 public const ORDERBY_USER
= 2;
46 public const ORDERBY_ACTION
= 3;
49 private ?
string $start_date_filter = null;
50 private ?
string $end_date_filter = null;
51 private ?
string $user_filter = null;
52 private ?
string $action_filter = null;
54 /** @var array<string> */
55 protected array $list_fields = array(
57 'raw_start_date_filter',
59 'raw_end_date_filter',
67 public function __construct()
73 * Returns the field we want to default set order to
77 protected function getDefaultOrder(): int|
string
79 return self
::ORDERBY_DATE
;
83 * Return the default direction for ordering
85 * @return string ASC or DESC
87 protected function getDefaultDirection(): string
89 return self
::ORDER_DESC
;
93 * Reinit default parameters
97 public function reinit(): void
100 $this->start_date_filter
= null;
101 $this->end_date_filter
= null;
102 $this->user_filter
= '0';
103 $this->action_filter
= null;
107 * Global getter method
109 * @param string $name name of the property we want to retrieve
111 * @return mixed the called property
113 public function __get(string $name): mixed
115 if (in_array($name, $this->pagination_fields
)) {
116 return parent
::__get($name);
118 if (in_array($name, $this->list_fields
)) {
120 case 'raw_start_date_filter':
121 return $this->start_date_filter
;
122 case 'raw_end_date_filter':
123 return $this->end_date_filter
;
124 case 'start_date_filter':
125 case 'end_date_filter':
127 if ($this->$name !== null) {
128 $d = new \
DateTime($this->$name);
129 return $d->format(__("Y-m-d"));
131 } catch (Throwable
$e) {
132 //oops, we've got a bad date :/
134 'Bad date (' . $this->$name . ') | ' .
146 throw new \
RuntimeException(
148 'Unable to get property "%s::%s"!',
156 * Global isset method
157 * Required for twig to access properties via __get
159 * @param string $name name of the property we want to retrieve
163 public function __isset(string $name): bool
165 if (in_array($name, $this->pagination_fields
)) {
167 } elseif (in_array($name, $this->list_fields
)) {
175 * Global setter method
177 * @param string $name name of the property we want to assign a value to
178 * @param mixed $value a relevant value for the property
182 public function __set(string $name, mixed $value): void
184 if (in_array($name, $this->pagination_fields
)) {
185 parent
::__set($name, $value);
188 '[' . static::class . '] Setting property `' . $name . '`',
193 case 'start_date_filter':
194 case 'end_date_filter':
197 $y = \DateTime
::createFromFormat(__("Y"), $value);
201 if ($name === 'end_date_filter') {
206 (int)$y->format('Y'),
210 $this->$name = $y->format('Y-m-d');
213 $ym = \DateTime
::createFromFormat(__("Y-m"), $value);
214 if ($y === false && $ym !== false) {
216 if ($name === 'end_date_filter') {
217 $day = (int)$ym->format('t');
220 (int)$ym->format('Y'),
221 (int)$ym->format('m'),
224 $this->$name = $ym->format('Y-m-d');
227 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
228 if ($y === false && $ym === false && $d !== false) {
229 $this->$name = $d->format('Y-m-d');
232 if ($y === false && $ym === false && $d === false) {
240 if ($name === 'start_date_filter') {
241 $field = _T("start date filter");
243 if ($name === 'end_date_filter') {
244 $field = _T("end date filter");
247 throw new \
Exception(
249 //TRANS: %1$s is field name, %2$s is list of known date formats
250 _T('Unknown date format for %1$s.<br/>Know formats are: %2$s'),
252 implode(', ', $formats)
259 } catch (Throwable
$e) {
261 'Wrong date format. field: ' . $name .
262 ', value: ' . $value . ', expected fmt: ' .
263 __("Y-m-d") . ' | ' . $e->getMessage(),
270 $this->$name = $value;