]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Filters/ContributionsList.php
8068bf59047d6a7b29bca5a27596e248e6e2a116
[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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since june, 12th 2016
36 */
37
38 namespace Galette\Filters;
39
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 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
59 const ORDERBY_DATE = 0;
60 const ORDERBY_BEGIN_DATE = 1;
61 const ORDERBY_END_DATE = 2;
62 const ORDERBY_MEMBER = 3;
63 const ORDERBY_TYPE = 4;
64 const ORDERBY_AMOUNT = 5;
65 const ORDERBY_DURATION = 6;
66 const ORDERBY_PAYMENT_TYPE = 7;
67
68 const DATE_BEGIN = 0;
69 const DATE_END = 1;
70 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 if ($this->$name !== null) {
158 $d = new \DateTime($this->$name);
159 return $d->format(__("Y-m-d"));
160 }
161 } catch (\Exception $e) {
162 //oops, we've got a bad date :/
163 Analog::log(
164 'Bad date (' . $this->$name . ') | ' .
165 $e->getMessage(),
166 Analog::INFO
167 );
168 return $this->$name;
169 }
170 break;
171 case 'rstart_date_filter':
172 case 'rend_date_filter':
173 //same as above, but raw format
174 $rname = substr($name, 1);
175 return $this->$rname;
176 default:
177 return $this->$name;
178 }
179 } else {
180 Analog::log(
181 '[ContributionsList] Unable to get proprety `' .$name . '`',
182 Analog::WARNING
183 );
184 }
185 }
186 }
187
188 /**
189 * Global setter method
190 *
191 * @param string $name name of the property we want to assign a value to
192 * @param object $value a relevant value for the property
193 *
194 * @return void
195 */
196 public function __set($name, $value)
197 {
198 if (in_array($name, $this->pagination_fields)) {
199 parent::__set($name, $value);
200 } else {
201 Analog::log(
202 '[ContributionsList] Setting property `' . $name . '`',
203 Analog::DEBUG
204 );
205
206 switch ($name) {
207 case 'start_date_filter':
208 case 'end_date_filter':
209 try {
210 if ($value !== '') {
211 $y = \DateTime::createFromFormat(__("Y"), $value);
212 if ($y !== false) {
213 $month = 1;
214 $day = 1;
215 if ($name === 'end_date_filter') {
216 $month = 12;
217 $day = 31;
218 }
219 $y->setDate(
220 $y->format('Y'),
221 $month,
222 $day
223 );
224 $this->$name = $y->format('Y-m-d');
225 }
226
227 $ym = \DateTime::createFromFormat(__("Y-m"), $value);
228 if ($y === false && $ym !== false) {
229 $day = 1;
230 if ($name === 'end_date_filter') {
231 $day = $ym->format('t');
232 }
233 $ym->setDate(
234 $ym->format('Y'),
235 $ym->format('m'),
236 $day
237 );
238 $this->$name = $ym->format('Y-m-d');
239 }
240
241 $d = \DateTime::createFromFormat(__("Y-m-d"), $value);
242 if ($y === false && $ym === false && $d !== false) {
243 $this->$name = $d->format('Y-m-d');
244 }
245
246 if ($y === false && $ym === false && $d === false) {
247 $formats = array(
248 __("Y"),
249 __("Y-m"),
250 __("Y-m-d"),
251 );
252
253 $field = null;
254 if ($name === 'start_date_filter') {
255 $field = _T("start date filter");
256 }
257 if ($name === 'end_date_filter') {
258 $field = _T("end date filter");
259 }
260
261 throw new \Exception(
262 str_replace(
263 array('%field', '%format'),
264 array(
265 $field,
266 implode(', ', $formats)
267 ),
268 _T("Unknown date format for %field.<br/>Know formats are: %formats")
269 )
270 );
271 }
272 } else {
273 $this->$name = null;
274 }
275 } catch (\Exception $e) {
276 Analog::log(
277 'Wrong date format. field: ' . $key .
278 ', value: ' . $value . ', expected fmt: ' .
279 __("Y-m-d") . ' | ' . $e->getMessage(),
280 Analog::INFO
281 );
282 throw $e;
283 }
284 break;
285 default:
286 $this->$name = $value;
287 break;
288 }
289 }
290 }
291 }