]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Filters/ContributionsList.php
Fix contributions date filters; closes #1469
[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 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 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 Analog\Analog;
40 use Galette\Core\Pagination;
41
42 /**
43 * Contributions lists filters and paginator
44 *
45 * @name ContributionsList
46 * @category Filters
47 * @package Galette
48 *
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2016 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 */
54
55 class ContributionsList extends Pagination
56 {
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
67 public const DATE_BEGIN = 0;
68 public const DATE_END = 1;
69 public const DATE_RECORD = 2;
70
71 //filters
72 private $date_field = null;
73 private $start_date_filter = null;
74 private $end_date_filter = null;
75 private $payment_type_filter = null;
76 private $filtre_cotis_adh = null;
77 private $filtre_transactions = null;
78
79 private $from_transaction = false;
80 private $max_amount = null;
81
82 protected $list_fields = array(
83 'start_date_filter',
84 'end_date_filter',
85 'filtre_cotis_adh',
86 'date_field',
87 'payment_type_filter',
88 'filtre_transactions',
89 'from_transaction',
90 'max_amount'
91 );
92
93 protected $virtuals_list_fields = array(
94 'rstart_date_filter',
95 'rend_date_filter'
96 );
97
98 /**
99 * Default constructor
100 */
101 public function __construct()
102 {
103 $this->reinit();
104 }
105
106 /**
107 * Returns the field we want to default set order to
108 *
109 * @return string field name
110 */
111 protected function getDefaultOrder()
112 {
113 return self::ORDERBY_BEGIN_DATE;
114 }
115
116 /**
117 * Reinit default parameters
118 *
119 * @return void
120 */
121 public function reinit()
122 {
123 parent::reinit();
124 $this->date_field = self::DATE_BEGIN;
125 $this->start_date_filter = null;
126 $this->end_date_filter = null;
127 $this->payment_type_filter = null;
128 $this->filtre_transactions = null;
129 $this->filtre_cotis_adh = null;
130 $this->from_transaction = false;
131 $this->max_amount = null;
132 }
133
134 /**
135 * Global getter method
136 *
137 * @param string $name name of the property we want to retrive
138 *
139 * @return object the called property
140 */
141 public function __get($name)
142 {
143 Analog::log(
144 '[ContributionsList] Getting property `' . $name . '`',
145 Analog::DEBUG
146 );
147
148 if (in_array($name, $this->pagination_fields)) {
149 return parent::__get($name);
150 } else {
151 if (in_array($name, $this->list_fields) || in_array($name, $this->virtuals_list_fields)) {
152 switch ($name) {
153 case 'start_date_filter':
154 case 'end_date_filter':
155 try {
156 $d = \DateTime::createFromFormat(__("Y-m-d"), $this->$name);
157 if ($d === false) {
158 //try with non localized date
159 $d = \DateTime::createFromFormat("Y-m-d", $this->$name);
160 if ($d === false) {
161 throw new \Exception('Incorrect format');
162 }
163 }
164 return $d->format(__("Y-m-d"));
165 } catch (\Exception $e) {
166 //oops, we've got a bad date :/
167 Analog::log(
168 'Bad date (' . $this->$name . ') | ' .
169 $e->getMessage(),
170 Analog::INFO
171 );
172 return $this->$name;
173 }
174 break;
175 case 'rstart_date_filter':
176 case 'rend_date_filter':
177 //same as above, but raw format
178 $rname = substr($name, 1);
179 return $this->$rname;
180 default:
181 return $this->$name;
182 }
183 } else {
184 Analog::log(
185 '[ContributionsList] Unable to get proprety `' . $name . '`',
186 Analog::WARNING
187 );
188 }
189 }
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 object $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 '[ContributionsList] 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 (\Exception $e) {
280 Analog::log(
281 'Wrong date format. field: ' . $key .
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 }