3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2009-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/>.
27 * @category Repository
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2009-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
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7dev - 2009-02-28
37 namespace Galette\Repository
;
39 use Galette\DynamicFields\DynamicField
;
40 use Galette\Entity\DynamicFieldsHandle
;
42 use Laminas\Db\Adapter\Adapter
;
43 use Laminas\Db\Sql\Expression
;
44 use Laminas\Db\Sql\Select
;
45 use Laminas\Db\Sql\Predicate\PredicateSet
;
46 use Laminas\Db\Sql\Predicate\Operator
;
47 use Galette\Entity\Adherent
;
48 use Galette\Entity\Contribution
;
49 use Galette\Entity\Transaction
;
50 use Galette\Entity\Reminder
;
51 use Galette\Filters\MembersList
;
52 use Galette\Filters\AdvancedMembersList
;
53 use Galette\Core\Picture
;
54 use Galette\Entity\Group
;
55 use Galette\Repository\Groups
;
56 use Galette\Entity\Status
;
60 * Members class for galette
63 * @category Repository
66 * @author Johan Cwiklinski <johan@x-tnd.be>
67 * @copyright 2009-2014 The Galette Team
68 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
69 * @link http://galette.tuxfamily.org
73 public const TABLE
= Adherent
::TABLE
;
74 public const PK
= Adherent
::PK
;
76 public const ALL_ACCOUNTS
= 0;
77 public const ACTIVE_ACCOUNT
= 1;
78 public const INACTIVE_ACCOUNT
= 2;
80 public const SHOW_LIST
= 0;
81 public const SHOW_PUBLIC_LIST
= 1;
82 public const SHOW_ARRAY_LIST
= 2;
83 public const SHOW_STAFF
= 3;
84 public const SHOW_MANAGED
= 4;
85 public const SHOW_EXPORT
= 5;
87 public const FILTER_NAME
= 0;
88 public const FILTER_ADDRESS
= 1;
89 public const FILTER_MAIL
= 2;
90 public const FILTER_JOB
= 3;
91 public const FILTER_INFOS
= 4;
92 public const FILTER_DC_EMAIL
= 5;
93 public const FILTER_W_EMAIL
= 6;
94 public const FILTER_WO_EMAIL
= 7;
95 public const FILTER_COMPANY_NAME
= 8;
96 public const FILTER_DC_PUBINFOS
= 9;
97 public const FILTER_W_PUBINFOS
= 10;
98 public const FILTER_WO_PUBINFOS
= 11;
99 public const FILTER_NUMBER
= 12;
101 public const MEMBERSHIP_ALL
= 0;
102 public const MEMBERSHIP_UP2DATE
= 3;
103 public const MEMBERSHIP_NEARLY
= 1;
104 public const MEMBERSHIP_LATE
= 2;
105 public const MEMBERSHIP_NEVER
= 4;
106 public const MEMBERSHIP_STAFF
= 5;
107 public const MEMBERSHIP_ADMIN
= 6;
108 public const MEMBERSHIP_NONE
= 7;
110 public const ORDERBY_NAME
= 0;
111 public const ORDERBY_NICKNAME
= 1;
112 public const ORDERBY_STATUS
= 2;
113 public const ORDERBY_FEE_STATUS
= 3;
114 public const ORDERBY_MODIFDATE
= 4;
115 public const ORDERBY_ID
= 5;
117 public const NON_STAFF_MEMBERS
= 30;
119 private $filters = false;
120 private $count = null;
121 private $errors = array();
124 * Default constructor
126 * @param MembersList $filters Filtering
128 public function __construct($filters = null)
130 if ($filters === null) {
131 $this->filters
= new MembersList();
133 $this->filters
= $filters;
138 * Get staff members list
140 * @param bool $as_members return the results as an array of
142 * @param array $fields field(s) name(s) to get. Should be a string or
143 * an array. If null, all fields will be
145 * @param boolean $count true if we want to count members
146 * @param boolean $limit true to LIMIT query
148 * @return Adherent[]|ResultSet
150 public function getStaffMembersList(
156 return $this->getMembersList(
167 * Get managed members list (for groups managers)
169 * @param bool $as_members return the results as an array of
171 * @param array $fields field(s) name(s) to get. Should be a string or
172 * an array. If null, all fields will be
174 * @param boolean $count true if we want to count members
175 * @param boolean $limit true to LIMIT query
177 * @return Adherent[]|ResultSet
179 public function getManagedMembersList(
185 return $this->getMembersList(
198 * @param bool $as_members return the results as an array of
200 * @param array $fields field(s) name(s) to get. Should be a string or
201 * an array. If null, all fields will be
203 * @param boolean $count true if we want to count members
204 * @param boolean $staff true if we want only staff members
205 * @param boolean $managed true if we want only managed groups
206 * @param boolean $limit true if we want records pagination
207 * @param boolean $export true if we are exporting
209 * @return Adherent[]|Laminas\Db\ResultSet
211 public function getMembersList(
222 if ($limit === true) {
223 //force count if limit is active
228 $_mode = self
::SHOW_LIST
;
229 if ($staff !== false) {
230 $_mode = self
::SHOW_STAFF
;
232 if ($managed !== false) {
233 $_mode = self
::SHOW_MANAGED
;
235 if ($export !== false) {
236 $_mode = self
::SHOW_EXPORT
;
239 $select = $this->buildSelect(
246 //add limits to retrieve only relavant rows
247 if ($limit === true) {
248 $this->filters
->setLimits($select);
251 $rows = $zdb->execute($select);
252 $this->filters
->query
= $zdb->query_string
;
260 foreach ($rows as $row) {
261 $members[] = new Adherent($zdb, $row, $deps);
267 } catch (\Exception
$e) {
269 'Cannot list members | ' . $e->getMessage(),
277 * Remove specified members
279 * @param integer|array $ids Members identifiers to delete
283 public function removeMembers($ids)
285 global $zdb, $hist, $emitter;
288 if (is_numeric($ids)) {
289 //we've got only one identifier
295 if (is_array($list)) {
297 $zdb->connection
->beginTransaction();
299 //Retrieve some information
300 $select = $zdb->select(self
::TABLE
);
302 array(self
::PK
, 'nom_adh', 'prenom_adh', 'email_adh')
303 )->where
->in(self
::PK
, $list);
305 $results = $zdb->execute($select);
308 foreach ($results as $member) {
309 $str_adh = $member->id_adh
. ' (' . $member->nom_adh
. ' ' .
310 $member->prenom_adh
. ')';
311 $infos .= $str_adh . "\n";
313 $p = new Picture($member->id_adh
);
314 if ($p->hasPicture()) {
315 if (!$p->delete(false)) {
317 'Unable to delete picture for member ' . $str_adh,
320 throw new \
Exception(
321 'Unable to delete picture for member ' .
326 _T("Member Picture deleted"),
332 $emitter->emit('member.remove', [
333 'id_adh' => $member->id_adh
,
334 'nom_adh' => $member->nom_adh
,
335 'prenom_adh' => $member->prenom_adh
,
336 'email_adh' => $member->email_adh
340 //delete contributions
341 $del_qry = $zdb->delete(Contribution
::TABLE
);
346 $zdb->execute($del_qry);
349 $select = $zdb->select(Transaction
::TABLE
);
350 $select->where
->in(self
::PK
, $list);
351 $results = $zdb->execute($select);
353 //if members has transactions;
354 //reset link with other contributions
356 if ($results->count() > 0) {
358 foreach ($results as $transaction) {
359 $transactions[] = $transaction[Transaction
::PK
];
362 $update = $zdb->update(Contribution
::TABLE
);
364 Transaction
::PK
=> new Expression('NULL')
369 $zdb->execute($update);
372 //delete transactions
373 $del_qry = $zdb->delete(Transaction
::TABLE
);
374 $del_qry->where
->in(self
::PK
, $list);
375 $zdb->execute($del_qry);
377 //delete groups membership/mamagmentship
378 Groups
::removeMembersFromGroups($list);
381 $del_qry = $zdb->delete(Reminder
::TABLE
);
386 $zdb->execute($del_qry);
388 //delete dynamic fields values
389 $del_qry = $zdb->delete(DynamicFieldsHandle
::TABLE
);
390 $del_qry->where(['field_form' => 'adh']);
391 $del_qry->where
->in('item_id', $list);
392 $zdb->execute($del_qry);
395 $del_qry = $zdb->delete(self
::TABLE
);
400 $zdb->execute($del_qry);
403 $zdb->connection
->commit();
405 //add an history entry
407 _T("Delete members cards, transactions and dues"),
412 } catch (\Exception
$e) {
413 $zdb->connection
->rollBack();
414 if ($e->getCode() == 23000) {
416 'Member still have existing dependencies in the ' .
417 'database, maybe a mailing or some content from a ' .
418 'plugin. Please remove dependencies before trying ' .
422 $this->errors
[] = _T("Cannot remove a member who still have dependencies (mailings, ...)");
425 'Unable to delete selected member(s) |' .
433 //not numeric and not an array: incorrect.
435 'Asking to remove members, but without providing an array or a single numeric value.',
445 * @param boolean $as_members return the results as an array of
447 * @param array $fields field(s) name(s) to get. Should be a string or
448 * an array. If null, all fields will be
451 * @return Adherent[]|ResultSet
453 public function getList($as_members = false, $fields = null)
455 return $this->getMembersList(
467 * Get members list with public information available
469 * @param boolean $with_photos get only members which have uploaded a
470 * photo (for trombinoscope)
474 public function getPublicList($with_photos)
479 $select = $this->buildSelect(
480 self
::SHOW_PUBLIC_LIST
,
486 $this->filters
->setLimits($select);
488 $results = $zdb->execute($select);
490 foreach ($results as $row) {
494 'picture' => $with_photos
496 $members[] = new Adherent($zdb, $row, $deps);
499 } catch (\Exception
$e) {
501 'Cannot list members with public information (photos: '
502 . $with_photos . ') | ' . $e->getMessage(),
510 * Get list of members that has been selected
512 * @param array $ids an array of members id that has been selected
513 * @param array $orderby SQL order clause (optionnal)
514 * @param boolean $with_photos Should photos be loaded?
515 * @param boolean $as_members Return Adherent[] or simple ResultSet
516 * @param array $fields Fields to use
517 * @param boolean $export True if we are exporting
518 * @param boolean $dues True if load dues as Adherent dependency
519 * @param boolean $parent True if load parent as Adherent dependency
523 public function getArrayList(
526 $with_photos = false,
535 if (!is_array($ids) ||
count($ids) < 1) {
536 Analog
::log('No member selected for labels.', Analog
::INFO
);
541 $damode = self
::SHOW_ARRAY_LIST
;
542 if ($export === true) {
543 $damode = self
::SHOW_EXPORT
;
545 $select = $this->buildSelect(
551 $select->where
->in('a.' . self
::PK
, $ids);
552 if (is_array($orderby) && count($orderby) > 0) {
553 foreach ($orderby as $o) {
558 $results = $zdb->execute($select);
561 foreach ($results as $o) {
563 'picture' => $with_photos,
568 if ($as_members === true) {
569 $members[] = new Adherent($zdb, $o, $deps);
575 } catch (\Exception
$e) {
577 'Cannot load members form ids array | ' . $e->getMessage(),
584 * Builds the SELECT statement
586 * @param int $mode the current mode (see self::SHOW_*)
587 * @param array $fields fields list to retrieve
588 * @param bool $photos true if we want to get only members with photos
589 * Default to false, only relevant for SHOW_PUBLIC_LIST
590 * @param bool $count true if we want to count members, defaults to false
592 * @return Select SELECT statement
594 private function buildSelect($mode, $fields, $photos, $count = false): Select
599 if ($fields != null && is_array($fields) && !in_array('id_adh', $fields)) {
600 $fields[] = 'id_adh';
602 $fieldsList = ($fields != null)
603 ?
((!is_array($fields) ||
count($fields) < 1) ?
(array)'*'
604 : $fields) : (array)'*';
606 $select = $zdb->select(self
::TABLE
, 'a');
608 $select->columns($fieldsList);
610 $select->quantifier('DISTINCT');
613 case self
::SHOW_STAFF
:
614 case self
::SHOW_LIST
:
615 case self
::SHOW_ARRAY_LIST
:
617 array('p' => PREFIX_DB
. Status
::TABLE
),
618 'a.' . Status
::PK
. '=p.' . Status
::PK
,
622 case self
::SHOW_EXPORT
:
623 //basically the same as above, but without any fields
625 array('p' => PREFIX_DB
. Status
::TABLE
),
626 'a.' . Status
::PK
. '=p.' . Status
::PK
,
630 case self
::SHOW_MANAGED
:
632 array('p' => PREFIX_DB
. Status
::TABLE
),
633 'a.' . Status
::PK
. '=p.' . Status
::PK
635 array('gr' => PREFIX_DB
. Group
::GROUPSUSERS_TABLE
),
636 'a.' . Adherent
::PK
. '=gr.' . Adherent
::PK
,
639 array('m' => PREFIX_DB
. Group
::GROUPSMANAGERS_TABLE
),
640 'gr.' . Group
::PK
. '=m.' . Group
::PK
,
642 )->where('m.' . Adherent
::PK
. ' = ' . $login->id
);
644 case self
::SHOW_PUBLIC_LIST
:
647 array('p' => PREFIX_DB
. Picture
::TABLE
),
648 'a.' . self
::PK
. '= p.' . self
::PK
,
655 //check for contributions filtering
657 $this->filters
instanceof AdvancedMembersList
658 && $this->filters
->withinContributions()
661 array('ct' => PREFIX_DB
. Contribution
::TABLE
),
662 'ct.' . self
::PK
. '=a.' . self
::PK
,
668 //check if there are dynamic fields in filter
673 $this->filters
instanceof AdvancedMembersList
674 && $this->filters
->free_search
675 && count($this->filters
->free_search
) > 0
676 && !isset($this->filters
->free_search
['empty'])
678 $free_searches = $this->filters
->free_search
;
679 foreach ($free_searches as $fs) {
680 if (strpos($fs['field'], 'dyn_') === 0) {
681 // simple dynamic fields
683 $dfs[] = str_replace('dyn_', '', $fs['field']);
688 //check if there are dynamic fields for contributions in filter
694 $this->filters
instanceof AdvancedMembersList
695 && $this->filters
->withinContributions()
698 $this->filters
->contrib_dynamic
699 && count($this->filters
->contrib_dynamic
) > 0
700 && !isset($this->filters
->contrib_dynamic
['empty'])
704 //check if there are dynamic fields in the filter
705 foreach ($this->filters
->contrib_dynamic
as $k => $cd) {
714 if ($hasDfc === true ||
$hasCdfc === true) {
716 array('dfc' => PREFIX_DB
. DynamicFieldsHandle
::TABLE
),
717 'dfc.item_id=ct.' . Contribution
::PK
,
723 // simple dynamic fields
724 if ($hasDf === true) {
725 foreach ($dfs as $df) {
726 $subselect = $zdb->select(DynamicFieldsHandle
::TABLE
, 'df');
729 'item_id' => 'item_id',
733 $subselect->where('df.field_form = \'adh\'');
734 $subselect->where('df.field_id = ' . $df);
736 array('df' . $df => $subselect),
737 'a.id_adh = df' . $df . '.item_id',
744 // choice dynamic fields
745 if ($hasCdfc === true) {
746 $cdf_field = 'cdf.id';
747 if (TYPE_DB
=== 'pgsql') {
748 $cdf_field .= '::text';
751 $cdf_field = 'cdfc.id';
752 if (TYPE_DB
=== 'pgsql') {
753 $cdf_field .= '::text';
755 foreach ($cdfcs as $cdf) {
756 $rcdf_field = str_replace(
762 array('cdfc' . $cdf => DynamicField
::getFixedValuesTableName($cdf, true)),
763 $rcdf_field . '=dfc.field_val',
770 if ($mode == self
::SHOW_LIST ||
$mode == self
::SHOW_MANAGED
) {
771 if ($this->filters
!== false) {
772 $this->buildWhereClause($select);
774 } elseif ($mode == self
::SHOW_PUBLIC_LIST
) {
787 new Expression('true')
797 new Expression('true')
802 new Expression('true')
811 if ($mode === self
::SHOW_STAFF
) {
812 $select->where
->lessThan(
814 self
::NON_STAFF_MEMBERS
819 $this->proceedCount($select);
822 $this->buildOrderClause($select, $fields);
825 } catch (\Exception
$e) {
827 'Cannot build SELECT clause for members | ' . $e->getMessage(),
835 * Count members from the query
837 * @param Select $select Original select
841 private function proceedCount(Select
$select)
846 $countSelect = clone $select;
847 $countSelect->reset($countSelect::COLUMNS
);
848 $countSelect->reset($countSelect::ORDER
);
849 $countSelect->reset($countSelect::HAVING
);
850 $joins = $countSelect->joins
;
851 $countSelect->reset($countSelect::JOINS
);
852 foreach ($joins as $join) {
859 unset($join['columns']);
861 $countSelect->columns(
863 'count' => new Expression('count(DISTINCT a.' . self
::PK
. ')')
867 $have = $select->having
;
868 if ($have->count() > 0) {
869 foreach ($have->getPredicates() as $h) {
870 $countSelect->where($h);
874 $results = $zdb->execute($countSelect);
876 $this->count
= (int)$results->current()->count
;
877 if (isset($this->filters
) && $this->count
> 0) {
878 $this->filters
->setCounter($this->count
);
880 } catch (\Exception
$e) {
882 'Cannot count members | ' . $e->getMessage(),
890 * Builds the order clause
892 * @param Select $select Original select
893 * @param array $fields Fields list to ensure ORDER clause
894 * references selected fields. Optionnal.
898 private function buildOrderClause(Select
$select, $fields = null): Select
902 switch ($this->filters
->orderby
) {
903 case self
::ORDERBY_NICKNAME
:
904 if ($this->canOrderBy('pseudo_adh', $fields)) {
905 $order[] = 'pseudo_adh ' . $this->filters
->getDirection();
908 case self
::ORDERBY_STATUS
:
909 if ($this->canOrderBy('priorite_statut', $fields)) {
910 $order[] = 'priorite_statut ' . $this->filters
->getDirection();
913 case self
::ORDERBY_MODIFDATE
:
914 if ($this->canOrderBy('date_modif_adh', $fields)) {
915 $order[] = 'date_modif_adh ' . $this->filters
->getDirection();
918 case 'list_adh_contribstatus':
919 case self
::ORDERBY_FEE_STATUS
:
920 if ($this->canOrderBy('bool_exempt_adh', $fields)) {
921 $order[] = 'bool_exempt_adh ' . $this->filters
->getDirection();
924 if ($this->canOrderBy('date_echeance', $fields)) {
925 $order[] = 'date_echeance ' . $this->filters
->getDirection();
928 case self
::ORDERBY_ID
:
929 if ($this->canOrderBy('id_adh', $fields)) {
930 $order[] = 'id_adh ' . $this->filters
->getDirection();
933 case 'list_adh_name':
939 if ($this->canOrderBy($this->filters
->orderby
, $fields)) {
940 $order[] = $this->filters
->orderby
. ' ' . $this->filters
->getDirection();
945 //anyways, we want to order by firstname, lastname
946 if ($this->canOrderBy('nom_adh', $fields)) {
947 $order[] = 'nom_adh ' . $this->filters
->getDirection();
949 if ($this->canOrderBy('prenom_adh', $fields)) {
950 $order[] = 'prenom_adh ' . $this->filters
->getDirection();
953 $select->order($order);
958 * Is field allowed to order? it shoulsd be present in
959 * provided fields list (those that are SELECT'ed).
961 * @param string $field_name Field name to order by
962 * @param array $fields SELECTE'ed fields
966 private function canOrderBy($field_name, $fields)
968 if ($fields === null) {
970 } elseif (!is_array($fields)) {
972 } elseif (in_array($field_name, $fields)) {
976 'Trying to order by ' . $field_name . ' while it is not in ' .
985 * Builds where clause, for filtering on simple list mode
987 * @param Select $select Original select
991 private function buildWhereClause(Select
$select)
996 if ($this->filters
->email_filter
== self
::FILTER_W_EMAIL
) {
997 $select->where('email_adh != \'\'');
999 if ($this->filters
->email_filter
== self
::FILTER_WO_EMAIL
) {
1000 $select->where('(email_adh = \'\' OR email_adh IS NULL)');
1003 if ($this->filters
->filter_str
!= '') {
1004 $token = $zdb->platform
->quoteValue(
1005 '%' . strtolower($this->filters
->filter_str
) . '%'
1007 switch ($this->filters
->field_filter
) {
1008 case self
::FILTER_NAME
:
1009 if (TYPE_DB
=== 'pgsql') {
1010 $sep = " || ' ' || ";
1021 $pre . 'LOWER(nom_adh)' . $sep .
1022 'LOWER(prenom_adh)' . $sep .
1023 'LOWER(pseudo_adh)' . $post . ' LIKE ' .
1026 $pre . 'LOWER(prenom_adh)' . $sep .
1027 'LOWER(nom_adh)' . $sep .
1028 'LOWER(pseudo_adh)' . $post . ' LIKE ' .
1033 case self
::FILTER_COMPANY_NAME
:
1035 'LOWER(societe_adh) LIKE ' .
1039 case self
::FILTER_ADDRESS
:
1042 'LOWER(adresse_adh) LIKE ' . $token
1044 'LOWER(adresse2_adh) LIKE ' . $token
1046 'cp_adh LIKE ' . $token
1048 'LOWER(ville_adh) LIKE ' . $token
1050 'LOWER(pays_adh) LIKE ' . $token
1054 case self
::FILTER_MAIL
:
1057 'LOWER(email_adh) LIKE ' . $token
1059 'LOWER(url_adh) LIKE ' . $token
1061 'LOWER(msn_adh) LIKE ' . $token
1063 'LOWER(icq_adh) LIKE ' . $token
1065 'LOWER(jabber_adh) LIKE ' . $token
1069 case self
::FILTER_JOB
:
1071 'LOWER(prof_adh) LIKE ' . $token
1074 case self
::FILTER_INFOS
:
1076 if ($login->isAdmin() ||
$login->isStaff()) {
1077 $more = ' OR LOWER(info_adh) LIKE ' . $token;
1080 '(LOWER(info_public_adh) LIKE ' .
1081 $token . $more . ')'
1084 case self
::FILTER_NUMBER
:
1085 $select->where
->equalTo('a.id_adh', $this->filters
->filter_str
);
1090 if ($this->filters
->membership_filter
) {
1091 switch ($this->filters
->membership_filter
) {
1092 case self
::MEMBERSHIP_NEARLY
:
1093 $now = new \
DateTime();
1094 $duedate = new \
DateTime();
1095 $duedate->modify('+1 month');
1096 $select->where
->greaterThanOrEqualTo(
1098 $now->format('Y-m-d')
1101 $duedate->format('Y-m-d')
1104 case self
::MEMBERSHIP_LATE
:
1108 date('Y-m-d', time())
1109 )->equalTo('bool_exempt_adh', new Expression('false'));
1111 case self
::MEMBERSHIP_UP2DATE
:
1113 '(' . 'date_echeance >= \'' . date('Y-m-d', time())
1114 . '\' OR bool_exempt_adh=true)'
1117 case self
::MEMBERSHIP_NEVER
:
1118 $select->where('date_echeance IS NULL')
1119 ->where('bool_exempt_adh = false');
1121 case self
::MEMBERSHIP_STAFF
:
1122 $select->where
->lessThan(
1123 'p.priorite_statut',
1124 self
::NON_STAFF_MEMBERS
1127 case self
::MEMBERSHIP_ADMIN
:
1128 $select->where
->equalTo('bool_admin_adh', true);
1130 case self
::MEMBERSHIP_NONE
:
1131 $select->where
->equalTo('a.id_statut', Status
::DEFAULT_STATUS
);
1136 if ($this->filters
->filter_account
) {
1137 switch ($this->filters
->filter_account
) {
1138 case self
::ACTIVE_ACCOUNT
:
1139 $select->where('activite_adh=true');
1141 case self
::INACTIVE_ACCOUNT
:
1142 $select->where('activite_adh=false');
1147 if ($this->filters
->group_filter
) {
1149 array('g' => PREFIX_DB
. Group
::GROUPSUSERS_TABLE
),
1150 'a.' . Adherent
::PK
. '=g.' . Adherent
::PK
,
1154 array('gs' => PREFIX_DB
. Group
::TABLE
),
1155 'gs.' . Group
::PK
. '=g.' . Group
::PK
,
1159 '(g.' . Group
::PK
. ' = ' . $this->filters
->group_filter
.
1160 ' OR gs.parent_group = NULL OR gs.parent_group = ' .
1161 $this->filters
->group_filter
. ')'
1165 if ($this->filters
instanceof AdvancedMembersList
) {
1166 $this->buildAdvancedWhereClause($select);
1170 } catch (\Exception
$e) {
1172 __METHOD__
. ' | ' . $e->getMessage(),
1180 * Builds where clause, for advanced filtering on simple list mode
1182 * @param Select $select Original select
1186 private function buildAdvancedWhereClause(Select
$select)
1188 global $zdb, $login;
1190 // Search members who belong to any (OR) or all (AND) listed groups.
1191 // Idea is to build an array of members ID that fits groups selection
1192 // we will use in the final query.
1193 // The OR case is quite simple, AND is a bit more complex; since we must
1194 // check each member do belongs to all listed groups.
1196 count($this->filters
->groups_search
) > 0
1197 && !isset($this->filters
->groups_search
['empty'])
1202 foreach ($this->filters
->groups_search
as $gs) { // then add a row for each group
1203 $wheregroups[] = $gs['group'];
1206 $gselect = $zdb->select(Group
::GROUPSUSERS_TABLE
, 'gu');
1210 array('g' => PREFIX_DB
. Group
::TABLE
),
1211 'gu.id_group=g.' . Group
::PK
,
1216 'g.id_group' => ':group',
1217 'g.parent_group' => ':pgroup'
1221 $gselect->group(['gu.id_adh']);
1223 $stmt = $zdb->sql
->prepareStatementForSqlObject($gselect);
1227 foreach ($this->filters
->groups_search
as $gs) { // then add a row for each ig/searched group pair
1228 /** Why where parameter is named where1 ?? */
1229 $gresults = $stmt->execute(
1231 'where1' => $gs['group'],
1232 'where2' => $gs['group']
1236 switch ($this->filters
->groups_search_log_op
) {
1237 case AdvancedMembersList
::OP_AND
:
1238 foreach ($gresults as $gresult) {
1239 if (!isset($ids[$gresult['id_adh']])) {
1240 $ids[$gresult['id_adh']] = 0;
1242 $ids[$gresult['id_adh']] +
= 1;
1245 case AdvancedMembersList
::OP_OR
:
1246 foreach ($gresults as $gresult) {
1247 $mids[$gresult['id_adh']] = $gresult['id_adh'];
1254 foreach ($ids as $id_adh => $count) {
1255 if ($count == count($wheregroups)) {
1256 $mids[$id_adh] = $id_adh;
1262 //limit on found members
1263 $select->where
->in('a.id_adh', $mids);
1265 //no match in groups, end of game.
1266 $select->where('false = true');
1270 //FIXME: should be retrieved from members_fields
1272 'ddn_adh' => 'birth_date',
1273 'date_crea_adh' => 'creation_date',
1274 'date_modif_adh' => 'modif_date',
1275 'date_echeance' => 'due_date',
1276 'ct.date_enreg' => 'contrib_creation_date',
1277 'ct.date_debut_cotis' => 'contrib_begin_date',
1278 'ct.date_fin_cotis' => 'contrib_end_date'
1281 foreach ($dates as $field => $property) {
1282 $bprop = "r{$property}_begin";
1283 if ($this->filters
->$bprop) {
1284 $d = new \
DateTime($this->filters
->$bprop);
1285 $select->where
->greaterThanOrEqualTo(
1290 $eprop = "r{$property}_end";
1291 if ($this->filters
->$eprop) {
1292 $d = new \
DateTime($this->filters
->$eprop);
1293 $select->where
->lessThanOrEqualTo(
1300 if ($this->filters
->show_public_infos
) {
1301 switch ($this->filters
->show_public_infos
) {
1302 case self
::FILTER_W_PUBINFOS
:
1303 $select->where('bool_display_info = true');
1305 case self
::FILTER_WO_PUBINFOS
:
1306 $select->where('bool_display_info = false');
1308 case self
::FILTER_DC_PUBINFOS
:
1309 //nothing to do here.
1314 if ($this->filters
->status
) {
1317 $this->filters
->status
1322 $this->filters
->contrib_min_amount
1323 ||
$this->filters
->contrib_max_amount
1325 if ($this->filters
->contrib_min_amount
) {
1326 $select->where
->greaterThanOrEqualTo(
1328 $this->filters
->contrib_min_amount
1331 if ($this->filters
->contrib_max_amount
) {
1332 $select->where
->lessThanOrEqualTo(
1334 $this->filters
->contrib_max_amount
1339 if ($this->filters
->contributions_types
) {
1342 $this->filters
->contributions_types
1346 if ($this->filters
->payments_types
) {
1348 'ct.type_paiement_cotis',
1349 $this->filters
->payments_types
1354 count($this->filters
->contrib_dynamic
) > 0
1355 && !isset($this->filters
->contrib_dynamic
['empty'])
1357 foreach ($this->filters
->contrib_dynamic
as $k => $cd) {
1363 if (is_array($cd)) {
1364 //dynamic choice spotted!
1365 $prefix = 'cdfc' . $k . '.';
1366 $qry = 'dfc.field_form = \'contrib\' AND ' .
1367 'dfc.field_id = ' . $k . ' AND ';
1369 $select->where
->in($prefix . $field, $cd);
1371 //dynamic field spotted!
1373 $qry = 'dfc.field_form = \'contrib\' AND ' .
1374 'dfc.field_id = ' . $k . ' AND ';
1375 $field = 'field_val';
1376 $qry .= 'LOWER(' . $prefix . $field . ') ' .
1378 $select->where($qry . $zdb->platform
->quoteValue('%' . strtolower($cd) . '%'));
1384 count($this->filters
->free_search
) > 0
1385 && !isset($this->filters
->free_search
['empty'])
1387 foreach ($this->filters
->free_search
as $fs) {
1388 $fs['search'] = mb_strtolower($fs['search']);
1390 switch ($fs['qry_op']) {
1391 case AdvancedMembersList
::OP_EQUALS
:
1394 case AdvancedMembersList
::OP_CONTAINS
:
1396 $fs['search'] = '%' . $fs['search'] . '%';
1398 case AdvancedMembersList
::OP_NOT_EQUALS
:
1401 case AdvancedMembersList
::OP_NOT_CONTAINS
:
1403 $fs['search'] = '%' . $fs['search'] . '%';
1405 case AdvancedMembersList
::OP_STARTS_WITH
:
1407 $fs['search'] = $fs['search'] . '%';
1409 case AdvancedMembersList
::OP_ENDS_WITH
:
1411 $fs['search'] = '%' . $fs['search'];
1413 case AdvancedMembersList
::OP_BEFORE
:
1416 case AdvancedMembersList
::OP_AFTER
:
1421 'Unknown query operator: ' . $fs['qry_op'] .
1422 ' (will fallback to equals)',
1432 if (strpos($fs['field'], 'dyn_') === 0) {
1433 // simple dynamic field spotted!
1434 $index = str_replace('dyn_', '', $fs['field']);
1435 $dyn_field = DynamicField
::loadFieldType($zdb, (int)$index);
1436 $prefix = 'df' . $index . '.';
1437 $fs['field'] = 'val';
1440 if ($dyn_field && $dyn_field instanceof \Galette\DynamicFields\Boolean
) {
1441 if ($fs['search'] != 0) {
1442 $qry .= $prefix . $fs['field'] . $qop . ' ' .
1445 $qry .= $prefix . $fs['field'] . ' IS NULL';
1447 } elseif (!strncmp($fs['field'], 'bool_', strlen('bool_'))) {
1448 $qry .= $prefix . $fs['field'] . $qop . ' ' .
1451 $fs['qry_op'] === AdvancedMembersList
::OP_BEFORE
1452 ||
$fs['qry_op'] === AdvancedMembersList
::OP_AFTER
1454 if ($prefix === 'a.') {
1455 //dates are OK in the main fields. no cast, just query!
1456 $qry .= $prefix . $fs['field'] . $qop . ' ' .
1457 $zdb->platform
->quoteValue($fs['search']);
1459 //dynamic dates are stored in their localized format :/
1460 //use current lang format to query for now
1461 if ($zdb->isPostgres()) {
1462 $fs['search'] = "to_date('" . $fs['search'] . "', 'YYYY-MM-DD')";
1463 $store_fmt = __("Y-m-d") === 'Y-m-d' ?
'YYYY-MM-DD' : 'DD/MM/YYYY';
1464 $qry .= "to_date('" . $prefix . $fs['field'] . "', '$store_fmt')";
1466 $fs['search'] = "STR_TO_DATE('" . $fs['search'] . "', '%Y-%m-%d')";
1467 $store_fmt = __("Y-m-d") === 'Y-m-d' ?
'%Y-%m-%d' : '%d/%m/%Y';
1468 $qry .= 'STR_TO_DATE(' . $prefix . $fs['field'] . ', \'' . $store_fmt . '\') ';
1471 $qry .= $qop . ' ' . $fs['search'];
1473 } elseif ($fs['field'] == 'status_label') {
1474 $qry_pattern = '%p%field %op %value';
1475 $qry .= str_replace(
1486 $zdb->platform
->quoteValue($fs['search'])
1491 $qry .= 'LOWER(' . $prefix . $fs['field'] . ') ' .
1492 $qop . ' ' . $zdb->platform
->quoteValue($fs['search']);
1495 if ($fs['log_op'] === AdvancedMembersList
::OP_AND
) {
1496 $select->where($qry);
1497 } elseif ($fs['log_op'] === AdvancedMembersList
::OP_OR
) {
1498 $select->where($qry, PredicateSet
::OP_OR
);
1507 * Login and password field cannot be empty.
1509 * If those ones are not required, or if a file has been imported
1510 * (from a CSV file for example), we fill here random values.
1514 public function emptyLogins()
1519 $zdb->connection
->beginTransaction();
1520 $select = $zdb->select(Adherent
::TABLE
);
1522 array('id_adh', 'login_adh', 'mdp_adh')
1525 'login_adh' => new Expression('NULL'),
1527 'mdp_adh' => new Expression('NULL'),
1533 $results = $zdb->execute($select);
1536 if ($results->count() > 0) {
1537 $update = $zdb->update(Adherent
::TABLE
);
1540 'login_adh' => ':login',
1541 'mdp_adh' => ':pass'
1543 )->where
->equalTo(Adherent
::PK
, ':id');
1545 $stmt = $zdb->sql
->prepareStatementForSqlObject($update);
1547 $p = new \Galette\Core\
Password($zdb);
1549 foreach ($results as $m) {
1553 ||
!isset($m->login_adh
)
1554 ||
$m->login_adh
== 'NULL'
1556 $m->login_adh
= $p->makeRandomPassword(15);
1562 ||
!isset($m->mdp_adh
)
1563 ||
$m->mdp_adh
== 'NULL'
1565 $randomp = $p->makeRandomPassword(15);
1566 $m->mdp_adh
= password_hash(
1573 if ($dirty === true) {
1574 /** Why where parameter is named where1 ?? */
1577 'login_adh' => $m->login_adh
,
1578 'mdp_adh' => $m->mdp_adh
,
1579 'where1' => $m->id_adh
1586 $zdb->connection
->commit();
1587 $this->count
= $processed;
1589 } catch (\Exception
$e) {
1590 $zdb->connection
->rollBack();
1592 'An error occurred trying to retrieve members with ' .
1593 'empty logins/passwords (' . $e->getMessage(),
1601 * Loads data to produce a Pie chart based on members state of dues
1605 public function getRemindersCount()
1609 $reminders = array();
1611 $soon_date = new \
DateTime();
1612 $soon_date->modify('+1 month');
1614 $now = new \
DateTime();
1616 $select = $zdb->select(Adherent
::TABLE
, 'a');
1619 'cnt' => new Expression('count(a.' . Adherent
::PK
. ')')
1624 array('p' => PREFIX_DB
. self
::TABLE
),
1625 'a.parent_id=p.' . self
::PK
,
1631 ->lessThan('a.date_echeance', $soon_date->format('Y-m-d'))
1632 ->greaterThanOrEqualTo('a.date_echeance', $now->format('Y-m-d'));
1634 ->where('a.activite_adh=true')
1635 ->where('a.bool_exempt_adh=false');
1637 $select_wo_mail = clone $select;
1640 '(a.email_adh != \'\' OR a.parent_id IS NOT NULL AND p.email_adh != \'\')'
1642 $select_wo_mail->where(
1643 '(a.email_adh = \'\' OR a.email_adh IS NULL) AND (p.email_adh = \'\' OR p.email_adh IS NULL)'
1646 $results = $zdb->execute($select);
1647 $res = $results->current();
1648 $reminders['impending'] = $res->cnt
;
1650 $results_wo_mail = $zdb->execute($select_wo_mail);
1651 $res_wo_mail = $results_wo_mail->current();
1652 $reminders['nomail']['impending'] = $res_wo_mail->cnt
;
1654 $select = $zdb->select(Adherent
::TABLE
, 'a');
1657 'cnt' => new Expression('count(a.' . Adherent
::PK
. ')')
1662 array('p' => PREFIX_DB
. self
::TABLE
),
1663 'a.parent_id=p.' . self
::PK
,
1669 ->lessThan('a.date_echeance', $now->format('Y-m-d'));
1671 ->where('a.activite_adh=true')
1672 ->where('a.bool_exempt_adh=false');
1674 $select_wo_mail = clone $select;
1677 '(a.email_adh != \'\' OR a.parent_id IS NOT NULL AND p.email_adh != \'\')'
1680 $select_wo_mail->where(
1681 '(a.email_adh = \'\' OR a.email_adh IS NULL) AND (p.email_adh = \'\' OR p.email_adh IS NULL)'
1684 $results = $zdb->execute($select);
1685 $res = $results->current();
1686 $reminders['late'] = $res->cnt
;
1688 $results_wo_mail = $zdb->execute($select_wo_mail);
1689 $res_wo_mail = $results_wo_mail->current();
1690 $reminders['nomail']['late'] = $res_wo_mail->cnt
;
1696 * Get count for current query
1700 public function getCount()
1702 return $this->count
;
1706 * Get registered errors
1710 public function getErrors()
1712 return $this->errors
;
1716 * Get all existing emails
1718 * @param Db $zdb Database instance
1720 * @return array ['email' => 'id_adh']
1722 public static function getEmails(Db
$zdb)
1725 $select = $zdb->select(self
::TABLE
);
1730 $select->where('email_adh != \'\' AND email_adh IS NOT NULL');
1731 $rows = $zdb->execute($select);
1732 foreach ($rows as $row) {
1733 $emails[$row->email_adh
] = $row->{self
::PK
};
1739 * Get current filters
1741 * @return MembersList
1743 public function getFilters()
1745 return $this->filters
;
1749 * Get members list to instanciate dropdowns
1751 * @param Db $zdb Database instance
1752 * @param integer $current Current member
1756 public function getSelectizedMembers(Db
$zdb, $current = null)
1759 $required_fields = array(
1765 $list_members = $this->getList(false, $required_fields);
1767 if (count($list_members) > 0) {
1768 foreach ($list_members as $member) {
1771 $members[$member->$pk] = Adherent
::getNameWithCase(
1773 $member->prenom_adh
,
1781 //check if current attached member is part of the list
1782 if ($current !== null && !isset($members[$current])) {
1784 [$current => Adherent
::getSName($zdb, $current, true, true)] +