]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Filters/TransactionsList.php
Improve coding standards
[galette.git] / galette / lib / Galette / Filters / TransactionsList.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 * Transactions lists filters and paginator
30 *
31 * @author Johan Cwiklinski <johan@x-tnd.be>
32 *
33 * @property ?string $start_date_filter
34 * @property ?string $end_date_filter
35 * @property ?integer $filtre_cotis_adh
36 * @property integer|false $filtre_cotis_children
37 * @property string $rstart_date_filter
38 * @property string $rend_date_filter
39 * @property ?integer $max_amount
40 */
41
42 class TransactionsList extends Pagination
43 {
44 public const ORDERBY_DATE = 0;
45 public const ORDERBY_MEMBER = 3;
46 public const ORDERBY_AMOUNT = 5;
47 public const ORDERBY_PAYMENT_TYPE = 7;
48 public const ORDERBY_ID = 8;
49
50 //filters
51 private ?string $start_date_filter = null;
52 private ?string $end_date_filter = null;
53 private ?int $filtre_cotis_adh = null;
54 private int|false $filtre_cotis_children = false;
55 private ?int $max_amount = null;
56
57 /** @var array<string> */
58 protected array $list_fields = array(
59 'start_date_filter',
60 'end_date_filter',
61 'filtre_cotis_adh',
62 'filtre_cotis_children'
63 );
64
65 /** @var array<string> */
66 protected array $virtuals_list_fields = array(
67 'rstart_date_filter',
68 'rend_date_filter'
69 );
70
71 /**
72 * Default constructor
73 */
74 public function __construct()
75 {
76 $this->reinit();
77 }
78
79 /**
80 * Returns the field we want to default set order to
81 *
82 * @return int|string
83 */
84 protected function getDefaultOrder(): int|string
85 {
86 return self::ORDERBY_DATE;
87 }
88
89 /**
90 * Reinit default parameters
91 *
92 * @return void
93 */
94 public function reinit(): void
95 {
96 parent::reinit();
97 $this->start_date_filter = null;
98 $this->end_date_filter = null;
99 $this->filtre_cotis_adh = null;
100 $this->filtre_cotis_children = false;
101 }
102
103 /**
104 * Global getter method
105 *
106 * @param string $name name of the property we want to retrieve
107 *
108 * @return mixed the called property
109 */
110 public function __get(string $name): mixed
111 {
112 if (in_array($name, $this->pagination_fields)) {
113 return parent::__get($name);
114 } else {
115 if (in_array($name, $this->list_fields) || in_array($name, $this->virtuals_list_fields)) {
116 switch ($name) {
117 case 'start_date_filter':
118 case 'end_date_filter':
119 if ($this->$name === null) {
120 return $this->$name;
121 }
122 try {
123 $d = \DateTime::createFromFormat(__("Y-m-d"), $this->$name);
124 if ($d === false) {
125 //try with non localized date
126 $d = \DateTime::createFromFormat("Y-m-d", $this->$name);
127 if ($d === false) {
128 throw new \Exception('Incorrect format');
129 }
130 }
131 return $d->format(__("Y-m-d"));
132 } catch (Throwable $e) {
133 //oops, we've got a bad date :/
134 Analog::log(
135 'Bad date (' . $this->$name . ') | ' .
136 $e->getMessage(),
137 Analog::INFO
138 );
139 return $this->$name;
140 }
141 case 'rstart_date_filter':
142 case 'rend_date_filter':
143 //same as above, but raw format
144 $rname = substr($name, 1);
145 return $this->$rname;
146 default:
147 return $this->$name;
148 }
149 }
150 }
151
152 throw new \RuntimeException(
153 sprintf(
154 'Unable to get property "%s::%s"!',
155 __CLASS__,
156 $name
157 )
158 );
159 }
160
161 /**
162 * Global isset method
163 * Required for twig to access properties via __get
164 *
165 * @param string $name name of the property we want to retrieve
166 *
167 * @return bool
168 */
169 public function __isset(string $name): bool
170 {
171 if (in_array($name, $this->pagination_fields)) {
172 return true;
173 } elseif (in_array($name, $this->list_fields) || in_array($name, $this->virtuals_list_fields)) {
174 return true;
175 }
176
177 return false;
178 }
179
180 /**
181 * Global setter method
182 *
183 * @param string $name name of the property we want to assign a value to
184 * @param mixed $value a relevant value for the property
185 *
186 * @return void
187 */
188 public function __set(string $name, mixed $value): void
189 {
190 if (in_array($name, $this->pagination_fields)) {
191 parent::__set($name, $value);
192 } else {
193 Analog::log(
194 '[TransactionsList] Setting property `' . $name . '`',
195 Analog::DEBUG
196 );
197
198 switch ($name) {
199 case 'start_date_filter':
200 case 'end_date_filter':
201 try {
202 if ($value !== '') {
203 $y = \DateTime::createFromFormat(__("Y"), $value);
204 if ($y !== false) {
205 $month = 1;
206 $day = 1;
207 if ($name === 'end_date_filter') {
208 $month = 12;
209 $day = 31;
210 }
211 $y->setDate(
212 (int)$y->format('Y'),
213 $month,
214 $day
215 );
216 $this->$name = $y->format('Y-m-d');
217 }
218
219 $ym = \DateTime::createFromFormat(__("Y-m"), $value);
220 if ($y === false && $ym !== false) {
221 $day = 1;
222 if ($name === 'end_date_filter') {
223 $day = (int)$ym->format('t');
224 }
225 $ym->setDate(
226 (int)$ym->format('Y'),
227 (int)$ym->format('m'),
228 $day
229 );
230 $this->$name = $ym->format('Y-m-d');
231 }
232
233 $d = \DateTime::createFromFormat(__("Y-m-d"), $value);
234 if ($y === false && $ym === false && $d !== false) {
235 $this->$name = $d->format('Y-m-d');
236 }
237
238 if ($y === false && $ym === false && $d === false) {
239 $formats = array(
240 __("Y"),
241 __("Y-m"),
242 __("Y-m-d"),
243 );
244
245 $field = null;
246 if ($name === 'start_date_filter') {
247 $field = _T("start date filter");
248 }
249 if ($name === 'end_date_filter') {
250 $field = _T("end date filter");
251 }
252
253 throw new \Exception(
254 sprintf(
255 //TRANS: %1$s is field name, %2$s is list of known date formats
256 _T('Unknown date format for %1$s.<br/>Know formats are: %2$s'),
257 $field,
258 implode(', ', $formats)
259 )
260 );
261 }
262 } else {
263 $this->$name = null;
264 }
265 } catch (Throwable $e) {
266 Analog::log(
267 'Wrong date format. field: ' . $name .
268 ', value: ' . $value . ', expected fmt: ' .
269 __("Y-m-d") . ' | ' . $e->getMessage(),
270 Analog::INFO
271 );
272 throw $e;
273 }
274 break;
275 default:
276 $this->$name = $value;
277 break;
278 }
279 }
280 }
281 }