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