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
,
485 $this->filters
->setLimits($select);
487 $results = $zdb->execute($select);
489 foreach ($results as $row) {
493 'picture' => $with_photos
495 $members[] = new Adherent($zdb, $row, $deps);
498 } catch (\Exception
$e) {
500 'Cannot list members with public information (photos: '
501 . $with_photos . ') | ' . $e->getMessage(),
509 * Get list of members that has been selected
511 * @param array $ids an array of members id that has been selected
512 * @param array $orderby SQL order clause (optionnal)
513 * @param boolean $with_photos Should photos be loaded?
514 * @param boolean $as_members Return Adherent[] or simple ResultSet
515 * @param array $fields Fields to use
516 * @param boolean $export True if we are exporting
517 * @param boolean $dues True if load dues as Adherent dependency
518 * @param boolean $parent True if load parent as Adherent dependency
522 public function getArrayList(
525 $with_photos = false,
534 if (!is_array($ids) ||
count($ids) < 1) {
535 Analog
::log('No member selected for labels.', Analog
::INFO
);
540 $damode = self
::SHOW_ARRAY_LIST
;
541 if ($export === true) {
542 $damode = self
::SHOW_EXPORT
;
544 $select = $this->buildSelect(
550 $select->where
->in('a.' . self
::PK
, $ids);
551 if ($orderby != null && count($orderby) > 0) {
552 if (is_array($orderby)) {
553 foreach ($orderby as $o) {
557 $select->order($orderby);
561 $results = $zdb->execute($select);
564 foreach ($results as $o) {
566 'picture' => $with_photos,
571 if ($as_members === true) {
572 $members[] = new Adherent($zdb, $o, $deps);
578 } catch (\Exception
$e) {
580 'Cannot load members form ids array | ' . $e->getMessage(),
587 * Builds the SELECT statement
589 * @param int $mode the current mode (see self::SHOW_*)
590 * @param array $fields fields list to retrieve
591 * @param bool $photos true if we want to get only members with photos
592 * Default to false, only relevant for SHOW_PUBLIC_LIST
593 * @param bool $count true if we want to count members, defaults to false
595 * @return Select SELECT statement
597 private function buildSelect($mode, $fields, $photos, $count = false): Select
602 if ($fields != null && is_array($fields) && !in_array('id_adh', $fields)) {
603 $fields[] = 'id_adh';
605 $fieldsList = ($fields != null)
606 ?
((!is_array($fields) ||
count($fields) < 1) ?
(array)'*'
607 : $fields) : (array)'*';
609 $select = $zdb->select(self
::TABLE
, 'a');
611 $select->columns($fieldsList);
613 $select->quantifier('DISTINCT');
616 case self
::SHOW_STAFF
:
617 case self
::SHOW_LIST
:
618 case self
::SHOW_ARRAY_LIST
:
620 array('p' => PREFIX_DB
. Status
::TABLE
),
621 'a.' . Status
::PK
. '=p.' . Status
::PK
,
625 case self
::SHOW_EXPORT
:
626 //basically the same as above, but without any fields
628 array('p' => PREFIX_DB
. Status
::TABLE
),
629 'a.' . Status
::PK
. '=p.' . Status
::PK
,
633 case self
::SHOW_MANAGED
:
635 array('p' => PREFIX_DB
. Status
::TABLE
),
636 'a.' . Status
::PK
. '=p.' . Status
::PK
638 array('gr' => PREFIX_DB
. Group
::GROUPSUSERS_TABLE
),
639 'a.' . Adherent
::PK
. '=gr.' . Adherent
::PK
,
642 array('m' => PREFIX_DB
. Group
::GROUPSMANAGERS_TABLE
),
643 'gr.' . Group
::PK
. '=m.' . Group
::PK
,
645 )->where('m.' . Adherent
::PK
. ' = ' . $login->id
);
647 case self
::SHOW_PUBLIC_LIST
:
650 array('p' => PREFIX_DB
. Picture
::TABLE
),
651 'a.' . self
::PK
. '= p.' . self
::PK
,
658 //check for contributions filtering
660 $this->filters
instanceof AdvancedMembersList
661 && $this->filters
->withinContributions()
664 array('ct' => PREFIX_DB
. Contribution
::TABLE
),
665 'ct.' . self
::PK
. '=a.' . self
::PK
,
671 //check if there are dynamic fields in filter
676 $this->filters
instanceof AdvancedMembersList
677 && $this->filters
->free_search
678 && count($this->filters
->free_search
) > 0
679 && !isset($this->filters
->free_search
['empty'])
681 $free_searches = $this->filters
->free_search
;
682 foreach ($free_searches as $fs) {
683 if (strpos($fs['field'], 'dyn_') === 0) {
684 // simple dynamic fields
686 $dfs[] = str_replace('dyn_', '', $fs['field']);
691 //check if there are dynamic fields for contributions in filter
697 $this->filters
instanceof AdvancedMembersList
698 && $this->filters
->withinContributions()
701 $this->filters
->contrib_dynamic
702 && count($this->filters
->contrib_dynamic
) > 0
703 && !isset($this->filters
->contrib_dynamic
['empty'])
707 //check if there are dynamic fields in the filter
708 foreach ($this->filters
->contrib_dynamic
as $k => $cd) {
717 if ($hasDfc === true ||
$hasCdfc === true) {
719 array('dfc' => PREFIX_DB
. DynamicFieldsHandle
::TABLE
),
720 'dfc.item_id=ct.' . Contribution
::PK
,
726 // simple dynamic fields
727 if ($hasDf === true) {
728 foreach ($dfs as $df) {
729 $subselect = $zdb->select(DynamicFieldsHandle
::TABLE
, 'df');
732 'item_id' => 'item_id',
736 $subselect->where('df.field_form = \'adh\'');
737 $subselect->where('df.field_id = ' . $df);
739 array('df' . $df => $subselect),
740 'a.id_adh = df' . $df . '.item_id',
747 // choice dynamic fields
748 if ($hasCdfc === true) {
749 $cdf_field = 'cdf.id';
750 if (TYPE_DB
=== 'pgsql') {
751 $cdf_field .= '::text';
754 $cdf_field = 'cdfc.id';
755 if (TYPE_DB
=== 'pgsql') {
756 $cdf_field .= '::text';
758 foreach ($cdfcs as $cdf) {
759 $rcdf_field = str_replace(
765 array('cdfc' . $cdf => DynamicField
::getFixedValuesTableName($cdf, true)),
766 $rcdf_field . '=dfc.field_val',
773 if ($mode == self
::SHOW_LIST ||
$mode == self
::SHOW_MANAGED
) {
774 if ($this->filters
!== false) {
775 $this->buildWhereClause($select);
777 } elseif ($mode == self
::SHOW_PUBLIC_LIST
) {
790 new Expression('true')
800 new Expression('true')
805 new Expression('true')
814 if ($mode === self
::SHOW_STAFF
) {
815 $select->where
->lessThan(
817 self
::NON_STAFF_MEMBERS
822 $this->proceedCount($select);
825 $this->buildOrderClause($select, $fields);
828 } catch (\Exception
$e) {
830 'Cannot build SELECT clause for members | ' . $e->getMessage(),
838 * Count members from the query
840 * @param Select $select Original select
844 private function proceedCount(Select
$select)
849 $countSelect = clone $select;
850 $countSelect->reset($countSelect::COLUMNS
);
851 $countSelect->reset($countSelect::ORDER
);
852 $countSelect->reset($countSelect::HAVING
);
853 $joins = $countSelect->joins
;
854 $countSelect->reset($countSelect::JOINS
);
855 foreach ($joins as $join) {
862 unset($join['columns']);
864 $countSelect->columns(
866 'count' => new Expression('count(DISTINCT a.' . self
::PK
. ')')
870 $have = $select->having
;
871 if ($have->count() > 0) {
872 foreach ($have->getPredicates() as $h) {
873 $countSelect->where($h);
877 $results = $zdb->execute($countSelect);
879 $this->count
= $results->current()->count
;
880 if (isset($this->filters
) && $this->count
> 0) {
881 $this->filters
->setCounter($this->count
);
883 } catch (\Exception
$e) {
885 'Cannot count members | ' . $e->getMessage(),
893 * Builds the order clause
895 * @param Select $select Original select
896 * @param array $fields Fields list to ensure ORDER clause
897 * references selected fields. Optionnal.
901 private function buildOrderClause(Select
$select, $fields = null): Select
905 switch ($this->filters
->orderby
) {
906 case self
::ORDERBY_NICKNAME
:
907 if ($this->canOrderBy('pseudo_adh', $fields)) {
908 $order[] = 'pseudo_adh ' . $this->filters
->getDirection();
911 case self
::ORDERBY_STATUS
:
912 if ($this->canOrderBy('priorite_statut', $fields)) {
913 $order[] = 'priorite_statut ' . $this->filters
->getDirection();
916 case self
::ORDERBY_MODIFDATE
:
917 if ($this->canOrderBy('date_modif_adh', $fields)) {
918 $order[] = 'date_modif_adh ' . $this->filters
->getDirection();
921 case 'list_adh_contribstatus':
922 case self
::ORDERBY_FEE_STATUS
:
923 if ($this->canOrderBy('bool_exempt_adh', $fields)) {
924 $order[] = 'bool_exempt_adh ' . $this->filters
->getDirection();
927 if ($this->canOrderBy('date_echeance', $fields)) {
928 $order[] = 'date_echeance ' . $this->filters
->getDirection();
931 case self
::ORDERBY_ID
:
932 if ($this->canOrderBy('id_adh', $fields)) {
933 $order[] = 'id_adh ' . $this->filters
->getDirection();
936 case 'list_adh_name':
942 if ($this->canOrderBy($this->filters
->orderby
, $fields)) {
943 $order[] = $this->filters
->orderby
. ' ' . $this->filters
->getDirection();
948 //anyways, we want to order by firstname, lastname
949 if ($this->canOrderBy('nom_adh', $fields)) {
950 $order[] = 'nom_adh ' . $this->filters
->getDirection();
952 if ($this->canOrderBy('prenom_adh', $fields)) {
953 $order[] = 'prenom_adh ' . $this->filters
->getDirection();
956 $select->order($order);
961 * Is field allowed to order? it shoulsd be present in
962 * provided fields list (those that are SELECT'ed).
964 * @param string $field_name Field name to order by
965 * @param array $fields SELECTE'ed fields
969 private function canOrderBy($field_name, $fields)
971 if ($fields === null) {
973 } elseif (!is_array($fields)) {
975 } elseif (in_array($field_name, $fields)) {
979 'Trying to order by ' . $field_name . ' while it is not in ' .
988 * Builds where clause, for filtering on simple list mode
990 * @param Select $select Original select
994 private function buildWhereClause(Select
$select)
999 if ($this->filters
->email_filter
== self
::FILTER_W_EMAIL
) {
1000 $select->where('email_adh != \'\'');
1002 if ($this->filters
->email_filter
== self
::FILTER_WO_EMAIL
) {
1003 $select->where('(email_adh = \'\' OR email_adh IS NULL)');
1006 if ($this->filters
->filter_str
!= '') {
1007 $token = $zdb->platform
->quoteValue(
1008 '%' . strtolower($this->filters
->filter_str
) . '%'
1010 switch ($this->filters
->field_filter
) {
1011 case self
::FILTER_NAME
:
1012 if (TYPE_DB
=== 'pgsql') {
1013 $sep = " || ' ' || ";
1024 $pre . 'LOWER(nom_adh)' . $sep .
1025 'LOWER(prenom_adh)' . $sep .
1026 'LOWER(pseudo_adh)' . $post . ' LIKE ' .
1029 $pre . 'LOWER(prenom_adh)' . $sep .
1030 'LOWER(nom_adh)' . $sep .
1031 'LOWER(pseudo_adh)' . $post . ' LIKE ' .
1036 case self
::FILTER_COMPANY_NAME
:
1038 'LOWER(societe_adh) LIKE ' .
1042 case self
::FILTER_ADDRESS
:
1045 'LOWER(adresse_adh) LIKE ' . $token
1047 'LOWER(adresse2_adh) LIKE ' . $token
1049 'cp_adh LIKE ' . $token
1051 'LOWER(ville_adh) LIKE ' . $token
1053 'LOWER(pays_adh) LIKE ' . $token
1057 case self
::FILTER_MAIL
:
1060 'LOWER(email_adh) LIKE ' . $token
1062 'LOWER(url_adh) LIKE ' . $token
1064 'LOWER(msn_adh) LIKE ' . $token
1066 'LOWER(icq_adh) LIKE ' . $token
1068 'LOWER(jabber_adh) LIKE ' . $token
1072 case self
::FILTER_JOB
:
1074 'LOWER(prof_adh) LIKE ' . $token
1077 case self
::FILTER_INFOS
:
1079 if ($login->isAdmin() ||
$login->isStaff()) {
1080 $more = ' OR LOWER(info_adh) LIKE ' . $token;
1083 '(LOWER(info_public_adh) LIKE ' .
1084 $token . $more . ')'
1087 case self
::FILTER_NUMBER
:
1088 $select->where
->equalTo('a.id_adh', $this->filters
->filter_str
);
1093 if ($this->filters
->membership_filter
) {
1094 switch ($this->filters
->membership_filter
) {
1095 case self
::MEMBERSHIP_NEARLY
:
1096 $now = new \
DateTime();
1097 $duedate = new \
DateTime();
1098 $duedate->modify('+1 month');
1099 $select->where
->greaterThanOrEqualTo(
1101 $now->format('Y-m-d')
1104 $duedate->format('Y-m-d')
1107 case self
::MEMBERSHIP_LATE
:
1111 date('Y-m-d', time())
1112 )->equalTo('bool_exempt_adh', new Expression('false'));
1114 case self
::MEMBERSHIP_UP2DATE
:
1116 '(' . 'date_echeance >= \'' . date('Y-m-d', time())
1117 . '\' OR bool_exempt_adh=true)'
1120 case self
::MEMBERSHIP_NEVER
:
1121 $select->where('date_echeance IS NULL')
1122 ->where('bool_exempt_adh = false');
1124 case self
::MEMBERSHIP_STAFF
:
1125 $select->where
->lessThan(
1126 'p.priorite_statut',
1127 self
::NON_STAFF_MEMBERS
1130 case self
::MEMBERSHIP_ADMIN
:
1131 $select->where
->equalTo('bool_admin_adh', true);
1133 case self
::MEMBERSHIP_NONE
:
1134 $select->where
->equalTo('a.id_statut', Status
::DEFAULT_STATUS
);
1139 if ($this->filters
->filter_account
) {
1140 switch ($this->filters
->filter_account
) {
1141 case self
::ACTIVE_ACCOUNT
:
1142 $select->where('activite_adh=true');
1144 case self
::INACTIVE_ACCOUNT
:
1145 $select->where('activite_adh=false');
1150 if ($this->filters
->group_filter
) {
1152 array('g' => PREFIX_DB
. Group
::GROUPSUSERS_TABLE
),
1153 'a.' . Adherent
::PK
. '=g.' . Adherent
::PK
,
1157 array('gs' => PREFIX_DB
. Group
::TABLE
),
1158 'gs.' . Group
::PK
. '=g.' . Group
::PK
,
1162 '(g.' . Group
::PK
. ' = ' . $this->filters
->group_filter
.
1163 ' OR gs.parent_group = NULL OR gs.parent_group = ' .
1164 $this->filters
->group_filter
. ')'
1168 if ($this->filters
instanceof AdvancedMembersList
) {
1169 $this->buildAdvancedWhereClause($select);
1173 } catch (\Exception
$e) {
1175 __METHOD__
. ' | ' . $e->getMessage(),
1183 * Builds where clause, for advanced filtering on simple list mode
1185 * @param Select $select Original select
1189 private function buildAdvancedWhereClause(Select
$select)
1191 global $zdb, $login;
1193 // Search members who belong to any (OR) or all (AND) listed groups.
1194 // Idea is to build an array of members ID that fits groups selection
1195 // we will use in the final query.
1196 // The OR case is quite simple, AND is a bit more complex; since we must
1197 // check each member do belongs to all listed groups.
1199 count($this->filters
->groups_search
) > 0
1200 && !isset($this->filters
->groups_search
['empty'])
1205 foreach ($this->filters
->groups_search
as $gs) { // then add a row for each group
1206 $wheregroups[] = $gs['group'];
1209 $gselect = $zdb->select(Group
::GROUPSUSERS_TABLE
, 'gu');
1213 array('g' => PREFIX_DB
. Group
::TABLE
),
1214 'gu.id_group=g.' . Group
::PK
,
1219 'g.id_group' => ':group',
1220 'g.parent_group' => ':pgroup'
1224 $gselect->group(['gu.id_adh']);
1226 $stmt = $zdb->sql
->prepareStatementForSqlObject($gselect);
1230 foreach ($this->filters
->groups_search
as $gs) { // then add a row for each ig/searched group pair
1231 /** Why where parameter is named where1 ?? */
1232 $gresults = $stmt->execute(
1234 'where1' => $gs['group'],
1235 'where2' => $gs['group']
1239 switch ($this->filters
->groups_search_log_op
) {
1240 case AdvancedMembersList
::OP_AND
:
1241 foreach ($gresults as $gresult) {
1242 if (!isset($ids[$gresult['id_adh']])) {
1243 $ids[$gresult['id_adh']] = 0;
1245 $ids[$gresult['id_adh']] +
= 1;
1248 case AdvancedMembersList
::OP_OR
:
1249 foreach ($gresults as $gresult) {
1250 $mids[$gresult['id_adh']] = $gresult['id_adh'];
1257 foreach ($ids as $id_adh => $count) {
1258 if ($count == count($wheregroups)) {
1259 $mids[$id_adh] = $id_adh;
1265 //limit on found members
1266 $select->where
->in('a.id_adh', $mids);
1268 //no match in groups, end of game.
1269 $select->where('false = true');
1273 //shoudl be retrieved from members_fields
1275 'ddn_adh' => 'birth_date',
1276 'date_crea_adh' => 'creation_date',
1277 'date_modif_adh' => 'modif_date',
1278 'date_echeance' => 'due_date',
1279 'ct.date_enreg' => 'contrib_creation_date',
1280 'ct.date_debut_cotis' => 'contrib_begin_date',
1281 'ct.date_fin_cotis' => 'contrib_end_date'
1284 foreach ($dates as $field => $property) {
1285 $bprop = "r{$property}_begin";
1286 if ($this->filters
->$bprop) {
1287 $d = new \
DateTime($this->filters
->$bprop);
1288 $select->where
->greaterThanOrEqualTo(
1293 $eprop = "r{$property}_end";
1294 if ($this->filters
->$eprop) {
1295 $d = new \
DateTime($this->filters
->$eprop);
1296 $select->where
->lessThanOrEqualTo(
1303 if ($this->filters
->show_public_infos
) {
1304 switch ($this->filters
->show_public_infos
) {
1305 case self
::FILTER_W_PUBINFOS
:
1306 $select->where('bool_display_info = true');
1308 case self
::FILTER_WO_PUBINFOS
:
1309 $select->where('bool_display_info = false');
1311 case self
::FILTER_DC_PUBINFOS
:
1312 //nothing to do here.
1317 if ($this->filters
->status
) {
1320 $this->filters
->status
1325 $this->filters
->contrib_min_amount
1326 ||
$this->filters
->contrib_max_amount
1328 if ($this->filters
->contrib_min_amount
) {
1329 $select->where
->greaterThanOrEqualTo(
1331 $this->filters
->contrib_min_amount
1334 if ($this->filters
->contrib_max_amount
) {
1335 $select->where
->lessThanOrEqualTo(
1337 $this->filters
->contrib_max_amount
1342 if ($this->filters
->contributions_types
) {
1345 $this->filters
->contributions_types
1349 if ($this->filters
->payments_types
) {
1351 'ct.type_paiement_cotis',
1352 $this->filters
->payments_types
1357 count($this->filters
->contrib_dynamic
) > 0
1358 && !isset($this->filters
->contrib_dynamic
['empty'])
1360 foreach ($this->filters
->contrib_dynamic
as $k => $cd) {
1366 if (is_array($cd)) {
1367 //dynamic choice spotted!
1368 $prefix = 'cdfc' . $k . '.';
1369 $qry = 'dfc.field_form = \'contrib\' AND ' .
1370 'dfc.field_id = ' . $k . ' AND ';
1372 $select->where
->in($prefix . $field, $cd);
1374 //dynamic field spotted!
1376 $qry = 'dfc.field_form = \'contrib\' AND ' .
1377 'dfc.field_id = ' . $k . ' AND ';
1378 $field = 'field_val';
1379 $qry .= 'LOWER(' . $prefix . $field . ') ' .
1381 $select->where($qry . $zdb->platform
->quoteValue('%' . strtolower($cd) . '%'));
1387 count($this->filters
->free_search
) > 0
1388 && !isset($this->filters
->free_search
['empty'])
1390 foreach ($this->filters
->free_search
as $fs) {
1391 $fs['search'] = mb_strtolower($fs['search']);
1393 switch ($fs['qry_op']) {
1394 case AdvancedMembersList
::OP_EQUALS
:
1397 case AdvancedMembersList
::OP_CONTAINS
:
1399 $fs['search'] = '%' . $fs['search'] . '%';
1401 case AdvancedMembersList
::OP_NOT_EQUALS
:
1404 case AdvancedMembersList
::OP_NOT_CONTAINS
:
1406 $fs['search'] = '%' . $fs['search'] . '%';
1408 case AdvancedMembersList
::OP_STARTS_WITH
:
1410 $fs['search'] = $fs['search'] . '%';
1412 case AdvancedMembersList
::OP_ENDS_WITH
:
1414 $fs['search'] = '%' . $fs['search'];
1416 case AdvancedMembersList
::OP_BEFORE
:
1419 case AdvancedMembersList
::OP_AFTER
:
1424 'Unknown query operator: ' . $fs['qry_op'] .
1425 ' (will fallback to equals)',
1435 if (strpos($fs['field'], 'dyn_') === 0) {
1436 // simple dynamic field spotted!
1437 $index = str_replace('dyn_', '', $fs['field']);
1438 $dyn_field = DynamicField
::loadFieldType($zdb, (int)$index);
1439 $prefix = 'df' . $index . '.';
1440 $fs['field'] = 'val';
1443 if ($dyn_field && $dyn_field instanceof \Galette\DynamicFields\Boolean
) {
1444 if ($fs['search'] != 0) {
1445 $qry .= $prefix . $fs['field'] . $qop . ' ' .
1448 $qry .= $prefix . $fs['field'] . ' IS NULL';
1450 } elseif (!strncmp($fs['field'], 'bool_', strlen('bool_'))) {
1451 $qry .= $prefix . $fs['field'] . $qop . ' ' .
1454 $fs['qry_op'] === AdvancedMembersList
::OP_BEFORE
1455 ||
$fs['qry_op'] === AdvancedMembersList
::OP_AFTER
1457 if ($prefix === 'a.') {
1458 //dates are OK in the main fields. no cast, just query!
1459 $qry .= $prefix . $fs['field'] . $qop . ' ' .
1460 $zdb->platform
->quoteValue($fs['search']);
1462 //dynamic dates are stored in their localized format :/
1463 //use current lang format to query for now
1464 if ($zdb->isPostgres()) {
1465 $fs['search'] = "to_date('" . $fs['search'] . "', 'YYYY-MM-DD')";
1466 $store_fmt = __("Y-m-d") === 'Y-m-d' ?
'YYYY-MM-DD' : 'DD/MM/YYYY';
1467 $qry .= "to_date('" . $prefix . $fs['field'] . "', '$store_fmt')";
1469 $fs['search'] = "STR_TO_DATE('" . $fs['search'] . "', '%Y-%m-%d')";
1470 $store_fmt = __("Y-m-d") === 'Y-m-d' ?
'%Y-%m-%d' : '%d/%m/%Y';
1471 $qry .= 'STR_TO_DATE(' . $prefix . $fs['field'] . ', \'' . $store_fmt . '\') ';
1474 $qry .= $qop . ' ' . $fs['search'];
1476 } elseif ($fs['field'] == 'status_label') {
1477 $qry_pattern = '%p%field %op %value';
1478 $qry .= str_replace(
1489 $zdb->platform
->quoteValue($fs['search'])
1494 $qry .= 'LOWER(' . $prefix . $fs['field'] . ') ' .
1495 $qop . ' ' . $zdb->platform
->quoteValue($fs['search']);
1498 if ($fs['log_op'] === AdvancedMembersList
::OP_AND
) {
1499 $select->where($qry);
1500 } elseif ($fs['log_op'] === AdvancedMembersList
::OP_OR
) {
1501 $select->where($qry, PredicateSet
::OP_OR
);
1510 * Login and password field cannot be empty.
1512 * If those ones are not required, or if a file has been imported
1513 * (from a CSV file for example), we fill here random values.
1517 public function emptyLogins()
1522 $zdb->connection
->beginTransaction();
1523 $select = $zdb->select(Adherent
::TABLE
);
1525 array('id_adh', 'login_adh', 'mdp_adh')
1528 'login_adh' => new Expression('NULL'),
1530 'mdp_adh' => new Expression('NULL'),
1536 $results = $zdb->execute($select);
1539 if ($results->count() > 0) {
1540 $update = $zdb->update(Adherent
::TABLE
);
1543 'login_adh' => ':login',
1544 'mdp_adh' => ':pass'
1546 )->where
->equalTo(Adherent
::PK
, ':id');
1548 $stmt = $zdb->sql
->prepareStatementForSqlObject($update);
1550 $p = new \Galette\Core\
Password($zdb);
1552 foreach ($results as $m) {
1556 ||
!isset($m->login_adh
)
1557 ||
$m->login_adh
== 'NULL'
1559 $m->login_adh
= $p->makeRandomPassword(15);
1565 ||
!isset($m->mdp_adh
)
1566 ||
$m->mdp_adh
== 'NULL'
1568 $randomp = $p->makeRandomPassword(15);
1569 $m->mdp_adh
= password_hash(
1576 if ($dirty === true) {
1577 /** Why where parameter is named where1 ?? */
1580 'login_adh' => $m->login_adh
,
1581 'mdp_adh' => $m->mdp_adh
,
1582 'where1' => $m->id_adh
1589 $zdb->connection
->commit();
1590 $this->count
= $processed;
1592 } catch (\Exception
$e) {
1593 $zdb->connection
->rollBack();
1595 'An error occurred trying to retrieve members with ' .
1596 'empty logins/passwords (' . $e->getMessage(),
1604 * Loads data to produce a Pie chart based on members state of dues
1608 public function getRemindersCount()
1612 $reminders = array();
1614 $soon_date = new \
DateTime();
1615 $soon_date->modify('+1 month');
1617 $now = new \
DateTime();
1619 $select = $zdb->select(Adherent
::TABLE
, 'a');
1622 'cnt' => new Expression('count(a.' . Adherent
::PK
. ')')
1627 array('p' => PREFIX_DB
. self
::TABLE
),
1628 'a.parent_id=p.' . self
::PK
,
1634 ->lessThan('a.date_echeance', $soon_date->format('Y-m-d'))
1635 ->greaterThanOrEqualTo('a.date_echeance', $now->format('Y-m-d'));
1637 ->where('a.activite_adh=true')
1638 ->where('a.bool_exempt_adh=false');
1640 $select_wo_mail = clone $select;
1642 $select->where('(a.email_adh != \'\' OR p.email_adh != \'\')');
1643 $select_wo_mail->where('a.email_adh = \'\' AND p.email_adh = \'\'');
1645 $results = $zdb->execute($select);
1646 $res = $results->current();
1647 $reminders['impending'] = $res->cnt
;
1649 $results_wo_mail = $zdb->execute($select_wo_mail);
1650 $res_wo_mail = $results_wo_mail->current();
1651 $reminders['nomail']['impending'] = $res_wo_mail->cnt
;
1653 $select = $zdb->select(Adherent
::TABLE
, 'a');
1656 'cnt' => new Expression('count(a.' . Adherent
::PK
. ')')
1661 array('p' => PREFIX_DB
. self
::TABLE
),
1662 'a.parent_id=p.' . self
::PK
,
1668 ->lessThan('a.date_echeance', $now->format('Y-m-d'));
1670 ->where('a.activite_adh=true')
1671 ->where('a.bool_exempt_adh=false');
1673 $select_wo_mail = clone $select;
1675 $select->where('(a.email_adh != \'\' OR p.email_adh != \'\')');
1676 $select_wo_mail->where('a.email_adh = \'\' AND p.email_adh = \'\'');
1678 $results = $zdb->execute($select);
1679 $res = $results->current();
1680 $reminders['late'] = $res->cnt
;
1682 $results_wo_mail = $zdb->execute($select_wo_mail);
1683 $res_wo_mail = $results_wo_mail->current();
1684 $reminders['nomail']['late'] = $res_wo_mail->cnt
;
1690 * Get count for current query
1694 public function getCount()
1696 return $this->count
;
1700 * Get registered errors
1704 public function getErrors()
1706 return $this->errors
;
1710 * Get all existing emails
1712 * @param Db $zdb Database instance
1714 * @return array ['email' => 'id_adh']
1716 public static function getEmails(Db
$zdb)
1719 $select = $zdb->select(self
::TABLE
);
1724 $select->where('email_adh != \'\' AND email_adh IS NOT NULL');
1725 $rows = $zdb->execute($select);
1726 foreach ($rows as $row) {
1727 $emails[$row->email_adh
] = $row->{self
::PK
};
1733 * Get current filters
1735 * @return MembersList
1737 public function getFilters()
1739 return $this->filters
;
1743 * Get members list to instanciate dropdowns
1745 * @param Db $zdb Database instance
1746 * @param integer $current Current member
1750 public function getSelectizedMembers(Db
$zdb, $current = null)
1753 $required_fields = array(
1759 $list_members = $this->getList(false, $required_fields);
1761 if (count($list_members) > 0) {
1762 foreach ($list_members as $member) {
1765 $members[$member->$pk] = Adherent
::getNameWithCase(
1767 $member->prenom_adh
,
1775 //check if current attached member is part of the list
1776 if ($current !== null && !isset($members[$current])) {
1778 [$current => Adherent
::getSName($zdb, $current, true, true)] +