]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Filters/HistoryList.php
Fix replacement pattern; refs #1714
[galette.git] / galette / lib / Galette / Filters / HistoryList.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * History lists filters and paginator
7 *
8 * PHP version 5
9 *
10 * Copyright © 2016-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 Filters
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2016-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 june, 12th 2016
35 */
36
37 namespace Galette\Filters;
38
39 use Throwable;
40 use Analog\Analog;
41 use Galette\Core\Pagination;
42
43 /**
44 * History lists filters and paginator
45 *
46 * @name ContributionsList
47 * @category Filters
48 * @package Galette
49 *
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2016-2023 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 *
55 * @property ?string $start_date_filter
56 * @property ?string $raw_start_date_filter
57 * @property ?string $end_date_filter
58 * @property ?string $raw_end_date_filter
59 * @property integer $user_filter
60 * @property ?string $action_filter
61 */
62
63 class HistoryList extends Pagination
64 {
65 public const ORDERBY_DATE = 0;
66 public const ORDERBY_IP = 1;
67 public const ORDERBY_USER = 2;
68 public const ORDERBY_ACTION = 3;
69
70 //filters
71 private $start_date_filter = null;
72 private $end_date_filter = null;
73 private $user_filter = 0;
74 private $action_filter = null;
75
76 protected $list_fields = array(
77 'start_date_filter',
78 'raw_start_date_filter',
79 'end_date_filter',
80 'raw_end_date_filter',
81 'user_filter',
82 'action_filter'
83 );
84
85 /**
86 * Default constructor
87 */
88 public function __construct()
89 {
90 $this->reinit();
91 }
92
93 /**
94 * Returns the field we want to default set order to
95 *
96 * @return int|string
97 */
98 protected function getDefaultOrder()
99 {
100 return self::ORDERBY_DATE;
101 }
102
103 /**
104 * Return the default direction for ordering
105 *
106 * @return string ASC or DESC
107 */
108 protected function getDefaultDirection()
109 {
110 return self::ORDER_DESC;
111 }
112
113 /**
114 * Reinit default parameters
115 *
116 * @return void
117 */
118 public function reinit()
119 {
120 parent::reinit();
121 $this->start_date_filter = null;
122 $this->end_date_filter = null;
123 $this->user_filter = 0;
124 $this->action_filter = null;
125 }
126
127 /**
128 * Global getter method
129 *
130 * @param string $name name of the property we want to retrive
131 *
132 * @return mixed the called property
133 */
134 public function __get($name)
135 {
136 Analog::log(
137 '[HistoryList] Getting property `' . $name . '`',
138 Analog::DEBUG
139 );
140
141 if (in_array($name, $this->pagination_fields)) {
142 return parent::__get($name);
143 } else {
144 if (in_array($name, $this->list_fields)) {
145 switch ($name) {
146 case 'raw_start_date_filter':
147 return $this->start_date_filter;
148 break;
149 case 'raw_end_date_filter':
150 return $this->end_date_filter;
151 break;
152 case 'start_date_filter':
153 case 'end_date_filter':
154 try {
155 if ($this->$name !== null) {
156 $d = new \DateTime($this->$name);
157 return $d->format(__("Y-m-d"));
158 }
159 } catch (Throwable $e) {
160 //oops, we've got a bad date :/
161 Analog::log(
162 'Bad date (' . $this->$name . ') | ' .
163 $e->getMessage(),
164 Analog::INFO
165 );
166 return $this->$name;
167 }
168 break;
169 default:
170 return $this->$name;
171 }
172 } else {
173 Analog::log(
174 '[HistoryList] Unable to get proprety `' . $name . '`',
175 Analog::WARNING
176 );
177 }
178 }
179 }
180
181 /**
182 * Global isset method
183 * Required for twig to access properties via __get
184 *
185 * @param string $name name of the property we want to retrive
186 *
187 * @return bool
188 */
189 public function __isset($name)
190 {
191 if (in_array($name, $this->pagination_fields)) {
192 return true;
193 } elseif (in_array($name, $this->list_fields)) {
194 return true;
195 }
196
197 return false;
198 }
199
200 /**
201 * Global setter method
202 *
203 * @param string $name name of the property we want to assign a value to
204 * @param object $value a relevant value for the property
205 *
206 * @return void
207 */
208 public function __set($name, $value)
209 {
210 if (in_array($name, $this->pagination_fields)) {
211 parent::__set($name, $value);
212 } else {
213 Analog::log(
214 '[HistoryList] Setting property `' . $name . '`',
215 Analog::DEBUG
216 );
217
218 switch ($name) {
219 case 'start_date_filter':
220 case 'end_date_filter':
221 try {
222 if ($value !== '') {
223 $y = \DateTime::createFromFormat(__("Y"), $value);
224 if ($y !== false) {
225 $month = 1;
226 $day = 1;
227 if ($name === 'end_date_filter') {
228 $month = 12;
229 $day = 31;
230 }
231 $y->setDate(
232 $y->format('Y'),
233 $month,
234 $day
235 );
236 $this->$name = $y->format('Y-m-d');
237 }
238
239 $ym = \DateTime::createFromFormat(__("Y-m"), $value);
240 if ($y === false && $ym !== false) {
241 $day = 1;
242 if ($name === 'end_date_filter') {
243 $day = $ym->format('t');
244 }
245 $ym->setDate(
246 $ym->format('Y'),
247 $ym->format('m'),
248 $day
249 );
250 $this->$name = $ym->format('Y-m-d');
251 }
252
253 $d = \DateTime::createFromFormat(__("Y-m-d"), $value);
254 if ($y === false && $ym === false && $d !== false) {
255 $this->$name = $d->format('Y-m-d');
256 }
257
258 if ($y === false && $ym === false && $d === false) {
259 $formats = array(
260 __("Y"),
261 __("Y-m"),
262 __("Y-m-d"),
263 );
264
265 $field = null;
266 if ($name === 'start_date_filter') {
267 $field = _T("start date filter");
268 }
269 if ($name === 'end_date_filter') {
270 $field = _T("end date filter");
271 }
272
273 throw new \Exception(
274 str_replace(
275 array('%field', '%formats'),
276 array(
277 $field,
278 implode(', ', $formats)
279 ),
280 _T("Unknown date format for %field.<br/>Know formats are: %formats")
281 )
282 );
283 }
284 } else {
285 $this->$name = null;
286 }
287 } catch (Throwable $e) {
288 Analog::log(
289 'Wrong date format. field: ' . $name .
290 ', value: ' . $value . ', expected fmt: ' .
291 __("Y-m-d") . ' | ' . $e->getMessage(),
292 Analog::INFO
293 );
294 throw $e;
295 }
296 break;
297 default:
298 $this->$name = $value;
299 break;
300 }
301 }
302 }
303 }