]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Filters/TransactionsList.php
4f2243e6dc55ce481f68e0720ae4d221b1421dee
[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 date $start_date_filter
56 * @property date $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 retrive
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 try {
142 if ($this->$name !== null) {
143 $d = new \DateTime($this->$name);
144 return $d->format(__("Y-m-d"));
145 }
146 } catch (Throwable $e) {
147 //oops, we've got a bad date :/
148 Analog::log(
149 'Bad date (' . $this->$name . ') | ' .
150 $e->getMessage(),
151 Analog::INFO
152 );
153 return $this->$name;
154 }
155 break;
156 case 'rstart_date_filter':
157 case 'rend_date_filter':
158 //same as above, but raw format
159 $rname = substr($name, 1);
160 return $this->$rname;
161 default:
162 return $this->$name;
163 }
164 } else {
165 Analog::log(
166 '[TransactionsList] Unable to get proprety `' . $name . '`',
167 Analog::WARNING
168 );
169 }
170 }
171 }
172
173 /**
174 * Global isset method
175 * Required for twig to access properties via __get
176 *
177 * @param string $name name of the property we want to retrive
178 *
179 * @return bool
180 */
181 public function __isset($name)
182 {
183 if (in_array($name, $this->pagination_fields)) {
184 return true;
185 } elseif (in_array($name, $this->list_fields) || in_array($name, $this->virtuals_list_fields)) {
186 return true;
187 }
188
189 return false;
190 }
191
192 /**
193 * Global setter method
194 *
195 * @param string $name name of the property we want to assign a value to
196 * @param mixed $value a relevant value for the property
197 *
198 * @return void
199 */
200 public function __set($name, $value)
201 {
202 if (in_array($name, $this->pagination_fields)) {
203 parent::__set($name, $value);
204 } else {
205 Analog::log(
206 '[TransactionsList] Setting property `' . $name . '`',
207 Analog::DEBUG
208 );
209
210 switch ($name) {
211 case 'start_date_filter':
212 case 'end_date_filter':
213 try {
214 if ($value !== '') {
215 $y = \DateTime::createFromFormat(__("Y"), $value);
216 if ($y !== false) {
217 $month = 1;
218 $day = 1;
219 if ($name === 'end_date_filter') {
220 $month = 12;
221 $day = 31;
222 }
223 $y->setDate(
224 $y->format('Y'),
225 $month,
226 $day
227 );
228 $this->$name = $y->format('Y-m-d');
229 }
230
231 $ym = \DateTime::createFromFormat(__("Y-m"), $value);
232 if ($y === false && $ym !== false) {
233 $day = 1;
234 if ($name === 'end_date_filter') {
235 $day = $ym->format('t');
236 }
237 $ym->setDate(
238 $ym->format('Y'),
239 $ym->format('m'),
240 $day
241 );
242 $this->$name = $ym->format('Y-m-d');
243 }
244
245 $d = \DateTime::createFromFormat(__("Y-m-d"), $value);
246 if ($y === false && $ym === false && $d !== false) {
247 $this->$name = $d->format('Y-m-d');
248 }
249
250 if ($y === false && $ym === false && $d === false) {
251 $formats = array(
252 __("Y"),
253 __("Y-m"),
254 __("Y-m-d"),
255 );
256
257 $field = null;
258 if ($name === 'start_date_filter') {
259 $field = _T("start date filter");
260 }
261 if ($name === 'end_date_filter') {
262 $field = _T("end date filter");
263 }
264
265 throw new \Exception(
266 str_replace(
267 array('%field', '%format'),
268 array(
269 $field,
270 implode(', ', $formats)
271 ),
272 _T("Unknown date format for %field.<br/>Know formats are: %formats")
273 )
274 );
275 }
276 } else {
277 $this->$name = null;
278 }
279 } catch (Throwable $e) {
280 Analog::log(
281 'Wrong date format. field: ' . $name .
282 ', value: ' . $value . ', expected fmt: ' .
283 __("Y-m-d") . ' | ' . $e->getMessage(),
284 Analog::INFO
285 );
286 throw $e;
287 }
288 break;
289 default:
290 $this->$name = $value;
291 break;
292 }
293 }
294 }
295 }