]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Filters/TransactionsList.php
2c24384cc9233e2ab9452295fd5ea44092a6881d
[galette.git] / galette / lib / Galette / Filters / TransactionsList.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Transactions 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 * Transactions lists filters and paginator
45 *
46 * @name TransactionsList
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 $end_date_filter
57 * @property ?integer $filtre_cotis_adh
58 * @property boolean $filtre_cotis_children
59 * @property string $rstart_date_filter
60 * @property string $rend_date_filter
61 */
62
63 class TransactionsList extends Pagination
64 {
65 public const ORDERBY_DATE = 0;
66 public const ORDERBY_MEMBER = 3;
67 public const ORDERBY_AMOUNT = 5;
68 public const ORDERBY_ID = 8;
69
70 //filters
71 private $start_date_filter;
72 private $end_date_filter;
73 private $filtre_cotis_adh;
74 private $filtre_cotis_children = false;
75
76 protected $list_fields = array(
77 'start_date_filter',
78 'end_date_filter',
79 'filtre_cotis_adh',
80 'filtre_cotis_children'
81 );
82
83 protected $virtuals_list_fields = array(
84 'rstart_date_filter',
85 'rend_date_filter'
86 );
87
88 /**
89 * Default constructor
90 */
91 public function __construct()
92 {
93 $this->reinit();
94 }
95
96 /**
97 * Returns the field we want to default set order to
98 *
99 * @return int|string
100 */
101 protected function getDefaultOrder()
102 {
103 return self::ORDERBY_DATE;
104 }
105
106 /**
107 * Reinit default parameters
108 *
109 * @return void
110 */
111 public function reinit()
112 {
113 parent::reinit();
114 $this->start_date_filter = null;
115 $this->end_date_filter = null;
116 $this->filtre_cotis_adh = null;
117 $this->filtre_cotis_children = false;
118 }
119
120 /**
121 * Global getter method
122 *
123 * @param string $name name of the property we want to retrieve
124 *
125 * @return mixed the called property
126 */
127 public function __get($name)
128 {
129 Analog::log(
130 '[TransactionsList] Getting property `' . $name . '`',
131 Analog::DEBUG
132 );
133
134 if (in_array($name, $this->pagination_fields)) {
135 return parent::__get($name);
136 } else {
137 if (in_array($name, $this->list_fields) || in_array($name, $this->virtuals_list_fields)) {
138 switch ($name) {
139 case 'start_date_filter':
140 case 'end_date_filter':
141 if ($this->$name === null) {
142 return $this->$name;
143 }
144 try {
145 $d = \DateTime::createFromFormat(__("Y-m-d"), $this->$name);
146 if ($d === false) {
147 //try with non localized date
148 $d = \DateTime::createFromFormat("Y-m-d", $this->$name);
149 if ($d === false) {
150 throw new \Exception('Incorrect format');
151 }
152 }
153 return $d->format(__("Y-m-d"));
154 } catch (Throwable $e) {
155 //oops, we've got a bad date :/
156 Analog::log(
157 'Bad date (' . $this->$name . ') | ' .
158 $e->getMessage(),
159 Analog::INFO
160 );
161 return $this->$name;
162 }
163 break;
164 case 'rstart_date_filter':
165 case 'rend_date_filter':
166 //same as above, but raw format
167 $rname = substr($name, 1);
168 return $this->$rname;
169 default:
170 return $this->$name;
171 }
172 } else {
173 Analog::log(
174 '[TransactionsList] Unable to get property `' . $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 retrieve
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) || in_array($name, $this->virtuals_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 mixed $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 '[TransactionsList] 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 }