]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Filters/HistoryList.php
eb4dff20c4dfa445b9405cf4f7ed5821dfa68968
[galette.git] / galette / lib / Galette / Filters / HistoryList.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22 namespace Galette\Filters;
23
24 use Throwable;
25 use Analog\Analog;
26 use Galette\Core\Pagination;
27
28 /**
29 * History lists filters and paginator
30 *
31 * @author Johan Cwiklinski <johan@x-tnd.be>
32 *
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
39 */
40
41 class HistoryList extends Pagination
42 {
43 public const ORDERBY_DATE = 0;
44 public const ORDERBY_IP = 1;
45 public const ORDERBY_USER = 2;
46 public const ORDERBY_ACTION = 3;
47
48 //filters
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;
53
54 /** @var array<string> */
55 protected array $list_fields = array(
56 'start_date_filter',
57 'raw_start_date_filter',
58 'end_date_filter',
59 'raw_end_date_filter',
60 'user_filter',
61 'action_filter'
62 );
63
64 /**
65 * Default constructor
66 */
67 public function __construct()
68 {
69 $this->reinit();
70 }
71
72 /**
73 * Returns the field we want to default set order to
74 *
75 * @return int|string
76 */
77 protected function getDefaultOrder(): int|string
78 {
79 return self::ORDERBY_DATE;
80 }
81
82 /**
83 * Return the default direction for ordering
84 *
85 * @return string ASC or DESC
86 */
87 protected function getDefaultDirection(): string
88 {
89 return self::ORDER_DESC;
90 }
91
92 /**
93 * Reinit default parameters
94 *
95 * @return void
96 */
97 public function reinit(): void
98 {
99 parent::reinit();
100 $this->start_date_filter = null;
101 $this->end_date_filter = null;
102 $this->user_filter = '0';
103 $this->action_filter = null;
104 }
105
106 /**
107 * Global getter method
108 *
109 * @param string $name name of the property we want to retrieve
110 *
111 * @return mixed the called property
112 */
113 public function __get(string $name): mixed
114 {
115 if (in_array($name, $this->pagination_fields)) {
116 return parent::__get($name);
117 } else {
118 if (in_array($name, $this->list_fields)) {
119 switch ($name) {
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':
126 try {
127 if ($this->$name !== null) {
128 $d = new \DateTime($this->$name);
129 return $d->format(__("Y-m-d"));
130 }
131 } catch (Throwable $e) {
132 //oops, we've got a bad date :/
133 Analog::log(
134 'Bad date (' . $this->$name . ') | ' .
135 $e->getMessage(),
136 Analog::INFO
137 );
138 }
139 return $this->$name;
140 default:
141 return $this->$name;
142 }
143 }
144 }
145
146 throw new \RuntimeException(
147 sprintf(
148 'Unable to get property "%s::%s"!',
149 __CLASS__,
150 $name
151 )
152 );
153 }
154
155 /**
156 * Global isset method
157 * Required for twig to access properties via __get
158 *
159 * @param string $name name of the property we want to retrieve
160 *
161 * @return bool
162 */
163 public function __isset(string $name): bool
164 {
165 if (in_array($name, $this->pagination_fields)) {
166 return true;
167 } elseif (in_array($name, $this->list_fields)) {
168 return true;
169 }
170
171 return false;
172 }
173
174 /**
175 * Global setter method
176 *
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
179 *
180 * @return void
181 */
182 public function __set(string $name, mixed $value): void
183 {
184 if (in_array($name, $this->pagination_fields)) {
185 parent::__set($name, $value);
186 } else {
187 Analog::log(
188 '[' . static::class . '] Setting property `' . $name . '`',
189 Analog::DEBUG
190 );
191
192 switch ($name) {
193 case 'start_date_filter':
194 case 'end_date_filter':
195 try {
196 if ($value !== '') {
197 $y = \DateTime::createFromFormat(__("Y"), $value);
198 if ($y !== false) {
199 $month = 1;
200 $day = 1;
201 if ($name === 'end_date_filter') {
202 $month = 12;
203 $day = 31;
204 }
205 $y->setDate(
206 (int)$y->format('Y'),
207 $month,
208 $day
209 );
210 $this->$name = $y->format('Y-m-d');
211 }
212
213 $ym = \DateTime::createFromFormat(__("Y-m"), $value);
214 if ($y === false && $ym !== false) {
215 $day = 1;
216 if ($name === 'end_date_filter') {
217 $day = (int)$ym->format('t');
218 }
219 $ym->setDate(
220 (int)$ym->format('Y'),
221 (int)$ym->format('m'),
222 $day
223 );
224 $this->$name = $ym->format('Y-m-d');
225 }
226
227 $d = \DateTime::createFromFormat(__("Y-m-d"), $value);
228 if ($y === false && $ym === false && $d !== false) {
229 $this->$name = $d->format('Y-m-d');
230 }
231
232 if ($y === false && $ym === false && $d === false) {
233 $formats = array(
234 __("Y"),
235 __("Y-m"),
236 __("Y-m-d"),
237 );
238
239 $field = null;
240 if ($name === 'start_date_filter') {
241 $field = _T("start date filter");
242 }
243 if ($name === 'end_date_filter') {
244 $field = _T("end date filter");
245 }
246
247 throw new \Exception(
248 sprintf(
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'),
251 $field,
252 implode(', ', $formats)
253 )
254 );
255 }
256 } else {
257 $this->$name = null;
258 }
259 } catch (Throwable $e) {
260 Analog::log(
261 'Wrong date format. field: ' . $name .
262 ', value: ' . $value . ', expected fmt: ' .
263 __("Y-m-d") . ' | ' . $e->getMessage(),
264 Analog::INFO
265 );
266 throw $e;
267 }
268 break;
269 default:
270 $this->$name = $value;
271 break;
272 }
273 }
274 }
275 }