3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Members list advanced filters
10 * Copyright © 2012-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2012-2014 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
34 * @link http://galette.tuxfamily.org
35 * @since 0.73dev 2012-10-16
38 namespace Galette\Filters
;
41 use Galette\Entity\Status
;
42 use Galette\Entity\ContributionsTypes
;
43 use Galette\Entity\Contribution
;
44 use Galette\Repository\Members
;
45 use Galette\DynamicFields\DynamicField
;
46 use Galette\Repository\PaymentTypes
;
49 * Members list filters and paginator
51 * @name AdvancedMembersList
55 * @author Johan Cwiklinski <johan@x-tnd.be>
56 * @copyright 2012-2014 The Galette Team
57 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
58 * @link http://galette.tuxfamily.org
61 class AdvancedMembersList
extends MembersList
68 const OP_CONTAINS
= 1;
69 const OP_NOT_EQUALS
= 2;
70 const OP_NOT_CONTAINS
= 3;
71 const OP_STARTS_WITH
= 4;
72 const OP_ENDS_WITH
= 5;
76 private $_creation_date_begin;
77 private $_creation_date_end;
78 private $_modif_date_begin;
79 private $_modif_date_end;
80 private $_due_date_begin;
81 private $_due_date_end;
82 private $_birth_date_begin;
83 private $_birth_date_end;
84 private $_show_public_infos = Members
::FILTER_DC_PUBINFOS
;
85 private $_status = array();
86 private $_contrib_creation_date_begin;
87 private $_contrib_creation_date_end;
88 private $_contrib_begin_date_begin;
89 private $_contrib_begin_date_end;
90 private $_contrib_end_date_begin;
91 private $_contrib_end_date_end;
92 private $_contributions_types;
93 private $_payments_types;
94 private $_contrib_min_amount;
95 private $_contrib_max_amount;
97 protected $advancedmemberslist_fields = array(
98 'creation_date_begin',
108 'contrib_creation_date_begin',
109 'contrib_creation_date_end',
110 'contrib_begin_date_begin',
111 'contrib_begin_date_end',
112 'contrib_end_date_begin',
113 'contrib_end_date_end',
114 'contributions_types',
116 'contrib_min_amount',
117 'contrib_max_amount',
122 protected $virtuals_advancedmemberslist_fields = array(
123 'rcreation_date_begin',
124 'rcreation_date_end',
131 'rcontrib_creation_date_begin',
132 'rcontrib_creation_date_end',
133 'rcontrib_begin_date_begin',
134 'rcontrib_begin_date_end',
135 'rcontrib_end_date_begin',
136 'rcontrib_end_date_end',
140 //an empty free search criteria to begin
141 private $_free_search = array(
145 'log_op' => self
::OP_AND
,
146 'qry_op' => self
::OP_EQUALS
150 //an empty contributions dynamic field criteria to begin
151 private $_contrib_dynamic = array(
155 'log_op' => self
::OP_AND
,
156 'qry_op' => self
::OP_EQUALS
161 * Default constructor
163 * @param MembersList $simple A simple filter search to keep
165 public function __construct($simple = null)
167 parent
::__construct();
168 if ($simple instanceof MembersList
) {
169 foreach ($this->pagination_fields
as $pf) {
170 $this->$pf = $simple->$pf;
172 foreach ($this->memberslist_fields
as $mlf) {
173 $this->$mlf = $simple->$mlf;
179 * Do we want to filter within contributions?
183 public function withinContributions()
185 if ($this->_contrib_creation_date_begin
!= null
186 ||
$this->_contrib_creation_date_end
!= null
187 ||
$this->_contrib_begin_date_begin
!= null
188 ||
$this->_contrib_begin_date_end
!= null
189 ||
$this->_contrib_end_date_begin
!= null
190 ||
$this->_contrib_begin_date_end
!= null
191 ||
$this->_contrib_min_amount
!= null
192 ||
$this->_contrib_max_amount
!= null
193 ||
count($this->_contrib_dynamic
) > 0
194 ||
count($this->_contributions_types
) > 0
195 ||
count($this->_payments_types
) > 0
204 * Reinit default parameters
208 public function reinit()
212 $this->_creation_date_begin
= null;
213 $this->_creation_date_end
= null;
214 $this->_modif_date_begin
= null;
215 $this->_modif_date_end
= null;
216 $this->_due_date_begin
= null;
217 $this->_due_date_end
= null;
218 $this->_birth_date_begin
= null;
219 $this->_birth_date_end
= null;
220 $this->_show_public_infos
= Members
::FILTER_DC_PUBINFOS
;
221 $this->_status
= array();
223 $this->_contrib_creation_date_begin
= null;
224 $this->_contrib_creation_date_end
= null;
225 $this->_contrib_begin_date_begin
= null;
226 $this->_contrib_begin_date_end
= null;
227 $this->_contrib_end_date_begin
= null;
228 $this->_contrib_begin_date_end
= null;
229 $this->_contributions_types
= array();
230 $this->_payments_types
= array();
232 $this->_free_search
= array(
236 'log_op' => self
::OP_AND
,
237 'qry_op' => self
::OP_EQUALS
241 $this->_contrib_dynamic
= array(
245 'log_op' => self
::OP_AND
,
246 'qry_op' => self
::OP_EQUALS
252 * Global getter method
254 * @param string $name name of the property we want to retrive
256 * @return object the called property
258 public function __get($name)
262 '[AdvancedMembersList] Getting property `' . $name . '`',
266 if (in_array($name, $this->pagination_fields
)
267 ||
in_array($name, $this->memberslist_fields
)
269 return parent
::__get($name);
271 if (in_array($name, $this->advancedmemberslist_fields
)
272 ||
in_array($name, $this->virtuals_advancedmemberslist_fields
)
274 $rname = '_' . $name;
276 case 'creation_date_begin':
277 case 'creation_date_end':
278 case 'modif_date_begin':
279 case 'modif_date_end':
280 case 'due_date_begin':
282 case 'birth_date_begin':
283 case 'birth_date_end':
284 case 'contrib_creation_date_begin':
285 case 'contrib_creation_date_end':
286 case 'contrib_begin_date_begin':
287 case 'contrib_begin_date_end':
288 case 'contrib_end_date_begin':
289 case 'contrib_end_date_end':
291 if ($this->$rname !== null) {
292 $d = new \
DateTime($this->$rname);
293 return $d->format(__("Y-m-d"));
295 } catch (\Exception
$e) {
296 //oops, we've got a bad date :/
298 'Bad date (' . $this->$rname . ') | ' .
302 return $this->$rname;
305 case 'rcreation_date_begin':
306 case 'rcreation_date_end':
307 case 'rmodif_date_begin':
308 case 'rmodif_date_end':
309 case 'rdue_date_begin':
310 case 'rdue_date_end':
311 case 'rbirth_date_begin':
312 case 'rbirth_date_end':
313 case 'rcontrib_creation_date_begin':
314 case 'rcontrib_creation_date_end':
315 case 'rcontrib_begin_date_begin':
316 case 'rcontrib_begin_date_end':
317 case 'rcontrib_end_date_begin':
318 case 'rcontrib_end_date_end':
319 //same as above, but raw format
320 $rname = '_' . substr($name, 1);
321 return $this->$rname;
322 case 'search_fields':
323 $search_fields = array_merge($this->memberslist_fields
, $this->advancedmemberslist_fields
);
324 $key = array_search('selected', $search_fields);
325 unset($search_fields[$key]);
326 $key = array_search('unreachable', $search_fields);
327 unset($search_fields[$key]);
328 $key = array_search('query', $search_fields);
329 unset($search_fields[$key]);
330 return $search_fields;
332 return $this->$rname;
335 '[AdvancedMembersList] Unable to get proprety `' .$name . '`',
343 * Global setter method
345 * @param string $name name of the property we want to assign a value to
346 * @param object $value a relevant value for the property
350 public function __set($name, $value)
352 global $zdb, $preferences, $login;
354 if (in_array($name, $this->pagination_fields
)
355 ||
in_array($name, $this->memberslist_fields
)
357 parent
::__set($name, $value);
360 '[AdvancedMembersList] Setting property `' . $name . '`',
367 case 'creation_date_begin':
368 case 'creation_date_end':
369 case 'modif_date_begin':
370 case 'modif_date_end':
371 case 'due_date_begin':
373 case 'birth_date_begin':
374 case 'birth_date_end':
375 case 'contrib_creation_date_begin':
376 case 'contrib_creation_date_end':
377 case 'contrib_begin_date_begin':
378 case 'contrib_begin_date_end':
379 case 'contrib_end_date_begin':
380 case 'contrib_end_date_end':
381 if ($value !== null && trim($value) !== '') {
383 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
385 throw new \
Exception('Incorrect format');
387 $this->$prop = $d->format('Y-m-d');
388 } catch (\Exception
$e) {
390 'Incorrect date format for ' . $name .
397 case 'contrib_min_amount':
398 case 'contrib_max_amount':
399 if (is_float($value)) {
400 $this->$prop = $value;
402 if ($value !== null) {
404 'Incorrect amount for ' . $name . '! ' .
405 'Should be a float (' . gettype($value) . ' given)',
411 case 'show_public_infos':
412 if (is_numeric($value)) {
413 $this->$prop = $value;
416 '[AdvancedMembersList] Value for property `' . $name .
417 '` should be an integer (' . gettype($value) . ' given)',
423 if (!is_array($value)) {
424 $value = array($value);
426 $this->_status
= array();
427 foreach ($value as $v) {
428 if (is_numeric($v)) {
429 //check status existence
430 $s = new Status($zdb);
432 if ($res !== false) {
433 $this->_status
[] = $v;
436 'Status #' . $v . ' does not exists!',
442 '[AdvancedMembersList] Value for status filter should be an '
443 .'integer (' . gettype($v) . ' given',
449 case 'contributions_types':
450 if (!is_array($value)) {
451 $value = array($value);
453 $this->_contributions_types
= array();
454 foreach ($value as $v) {
455 if (is_numeric($v)) {
456 //check type existence
457 $s = new ContributionsTypes($zdb);
459 if ($res !== false) {
460 $this->_contributions_types
[] = $v;
463 'Contribution type #' . $v . ' does not exists!',
469 '[AdvancedMembersList] Value for contribution type '
470 . 'filter should be an integer (' . gettype($v) .
477 case 'payments_types':
478 if (!is_array($value)) {
479 $value = array($value);
481 $this->_payments_types
= array();
482 $ptypes = new PaymentTypes(
487 $ptlist = $ptypes->getList();
489 foreach ($value as $v) {
490 if (is_numeric($v)) {
491 if (isset($ptlist[$v])) {
492 $this->_payments_types
[] = $v;
495 'Payment type #' . $v . ' does not exists!',
501 '[AdvancedMembersList] Value for payment type filter should be an '
502 .'integer (' . gettype($v) . ' given',
509 if (isset($this->_free_search
['empty'])) {
510 unset($this->_free_search
['empty']);
512 if (is_array($value)) {
513 if (isset($value['field'])
514 && isset($value['search'])
515 && isset($value['log_op'])
516 && isset($value['qry_op'])
517 && isset($value['idx'])
518 && isset($value['type'])
521 unset($value['idx']);
523 //handle value according to type
524 switch ($value['type']) {
525 case DynamicField
::DATE
:
526 if ($value['search'] !== null && trim($value['search']) !== '') {
528 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value['search']);
530 throw new \
Exception('Incorrect format');
532 $value['search'] = $d->format('Y-m-d');
533 } catch (\Exception
$e) {
535 'Incorrect date format for ' . $value['field'] .
536 '! was: ' . $value['search'],
544 $this->_free_search
[$id] = $value;
547 '[AdvancedMembersList] bad construct for free filter',
553 '[AdvancedMembersList] Value for free filter should be an '
554 .'array (' . gettype($value) . ' given',
560 if (substr($name, 0, 4) === 'cds_'
561 ||
substr($name, 0, 5) === 'cdsc_'
563 if (is_array($value) ||
trim($value) !== '') {
564 if (isset($this->_contrib_dynamic
['empty'])) {
565 unset($this->_contrib_dynamic
['empty']);
569 if (substr($name, 0, 5) === 'cdsc_') {
570 $id = substr($name, 5, strlen($name));
572 $id = substr($name, 4, strlen($name));
574 $this->_contrib_dynamic
[$id] = $value;
578 '[AdvancedMembersList] Unable to set proprety `' .