]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Filters/ContributionsList.php
Fix copyright year
[galette.git] / galette / lib / Galette / Filters / ContributionsList.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Contributions lists filters and paginator
7 *
8 * PHP version 5
9 *
10 * Copyright © 2016-2021 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-2021 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 * Contributions 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-2021 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
56 class ContributionsList extends Pagination
57 {
58 public const ORDERBY_DATE = 0;
59 public const ORDERBY_BEGIN_DATE = 1;
60 public const ORDERBY_END_DATE = 2;
61 public const ORDERBY_MEMBER = 3;
62 public const ORDERBY_TYPE = 4;
63 public const ORDERBY_AMOUNT = 5;
64 public const ORDERBY_DURATION = 6;
65 public const ORDERBY_PAYMENT_TYPE = 7;
66 public const ORDERBY_ID = 8;
67
68 public const DATE_BEGIN = 0;
69 public const DATE_END = 1;
70 public const DATE_RECORD = 2;
71
72 //filters
73 private $date_field = null;
74 private $start_date_filter = null;
75 private $end_date_filter = null;
76 private $payment_type_filter = null;
77 private $filtre_cotis_adh = null;
78 private $filtre_transactions = null;
79
80 private $from_transaction = false;
81 private $max_amount = null;
82
83 protected $list_fields = array(
84 'start_date_filter',
85 'end_date_filter',
86 'filtre_cotis_adh',
87 'date_field',
88 'payment_type_filter',
89 'filtre_transactions',
90 'from_transaction',
91 'max_amount'
92 );
93
94 protected $virtuals_list_fields = array(
95 'rstart_date_filter',
96 'rend_date_filter'
97 );
98
99 /**
100 * Default constructor
101 */
102 public function __construct()
103 {
104 $this->reinit();
105 }
106
107 /**
108 * Returns the field we want to default set order to
109 *
110 * @return string field name
111 */
112 protected function getDefaultOrder()
113 {
114 return self::ORDERBY_BEGIN_DATE;
115 }
116
117 /**
118 * Reinit default parameters
119 *
120 * @return void
121 */
122 public function reinit()
123 {
124 parent::reinit();
125 $this->date_field = self::DATE_BEGIN;
126 $this->start_date_filter = null;
127 $this->end_date_filter = null;
128 $this->payment_type_filter = null;
129 $this->filtre_transactions = null;
130 $this->filtre_cotis_adh = null;
131 $this->from_transaction = false;
132 $this->max_amount = null;
133 }
134
135 /**
136 * Global getter method
137 *
138 * @param string $name name of the property we want to retrive
139 *
140 * @return object the called property
141 */
142 public function __get($name)
143 {
144 Analog::log(
145 '[ContributionsList] Getting property `' . $name . '`',
146 Analog::DEBUG
147 );
148
149 if (in_array($name, $this->pagination_fields)) {
150 return parent::__get($name);
151 } else {
152 if (in_array($name, $this->list_fields) || in_array($name, $this->virtuals_list_fields)) {
153 switch ($name) {
154 case 'start_date_filter':
155 case 'end_date_filter':
156 try {
157 $d = \DateTime::createFromFormat(__("Y-m-d"), $this->$name);
158 if ($d === false) {
159 //try with non localized date
160 $d = \DateTime::createFromFormat("Y-m-d", $this->$name);
161 if ($d === false) {
162 throw new \Exception('Incorrect format');
163 }
164 }
165 return $d->format(__("Y-m-d"));
166 } catch (Throwable $e) {
167 //oops, we've got a bad date :/
168 Analog::log(
169 'Bad date (' . $this->$name . ') | ' .
170 $e->getMessage(),
171 Analog::INFO
172 );
173 return $this->$name;
174 }
175 break;
176 case 'rstart_date_filter':
177 case 'rend_date_filter':
178 //same as above, but raw format
179 $rname = substr($name, 1);
180 return $this->$rname;
181 default:
182 return $this->$name;
183 }
184 } else {
185 Analog::log(
186 '[ContributionsList] Unable to get proprety `' . $name . '`',
187 Analog::WARNING
188 );
189 }
190 }
191 }
192
193 /**
194 * Global setter method
195 *
196 * @param string $name name of the property we want to assign a value to
197 * @param object $value a relevant value for the property
198 *
199 * @return void
200 */
201 public function __set($name, $value)
202 {
203 if (in_array($name, $this->pagination_fields)) {
204 parent::__set($name, $value);
205 } else {
206 Analog::log(
207 '[ContributionsList] Setting property `' . $name . '`',
208 Analog::DEBUG
209 );
210
211 switch ($name) {
212 case 'start_date_filter':
213 case 'end_date_filter':
214 try {
215 if ($value !== '') {
216 $y = \DateTime::createFromFormat(__("Y"), $value);
217 if ($y !== false) {
218 $month = 1;
219 $day = 1;
220 if ($name === 'end_date_filter') {
221 $month = 12;
222 $day = 31;
223 }
224 $y->setDate(
225 $y->format('Y'),
226 $month,
227 $day
228 );
229 $this->$name = $y->format('Y-m-d');
230 }
231
232 $ym = \DateTime::createFromFormat(__("Y-m"), $value);
233 if ($y === false && $ym !== false) {
234 $day = 1;
235 if ($name === 'end_date_filter') {
236 $day = $ym->format('t');
237 }
238 $ym->setDate(
239 $ym->format('Y'),
240 $ym->format('m'),
241 $day
242 );
243 $this->$name = $ym->format('Y-m-d');
244 }
245
246 $d = \DateTime::createFromFormat(__("Y-m-d"), $value);
247 if ($y === false && $ym === false && $d !== false) {
248 $this->$name = $d->format('Y-m-d');
249 }
250
251 if ($y === false && $ym === false && $d === false) {
252 $formats = array(
253 __("Y"),
254 __("Y-m"),
255 __("Y-m-d"),
256 );
257
258 $field = null;
259 if ($name === 'start_date_filter') {
260 $field = _T("start date filter");
261 }
262 if ($name === 'end_date_filter') {
263 $field = _T("end date filter");
264 }
265
266 throw new \Exception(
267 str_replace(
268 array('%field', '%format'),
269 array(
270 $field,
271 implode(', ', $formats)
272 ),
273 _T("Unknown date format for %field.<br/>Know formats are: %formats")
274 )
275 );
276 }
277 } else {
278 $this->$name = null;
279 }
280 } catch (Throwable $e) {
281 Analog::log(
282 'Wrong date format. field: ' . $key .
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 }