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