3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Member class for galette
10 * Copyright © 2009-2021 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 2009-2021 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-06-02
37 namespace Galette\Entity
;
39 use Galette\Features\Socials
;
42 use Laminas\Db\Sql\Expression
;
44 use Galette\Core\Picture
;
45 use Galette\Core\GaletteMail
;
46 use Galette\Core\Password
;
47 use Galette\Core\Preferences
;
48 use Galette\Core\History
;
49 use Galette\Repository\Groups
;
50 use Galette\Core\Login
;
51 use Galette\Repository\Members
;
52 use Galette\Features\Dynamics
;
55 * Member class for galette
60 * @author Johan Cwiklinski <johan@x-tnd.be>
61 * @copyright 2009-2021 The Galette Team
62 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
63 * @link http://galette.tuxfamily.org
64 * @since Available since 0.7dev - 02-06-2009
66 * @property integer $id
67 * @property integer|Title $title Either a title id or an instance of Title
68 * @property string $stitle Title label
69 * @property string company_name
70 * @property string $name
71 * @property string $surname
72 * @property string $nickname
73 * @property string $birthdate Localized birth date
74 * @property string $rbirthdate Raw birth date
75 * @property string $birth_place
76 * @property integer $gender
77 * @property string $sgender Gender label
78 * @property string $job
79 * @property string $language
80 * @property integer $status
81 * @property string $sstatus Status label
82 * @property string $address
83 * @property string $address_continuation
84 * @property string $zipcode
85 * @property string $town
86 * @property string $country
87 * @property string $phone
88 * @property string $gsm
89 * @property string $email
90 * @property string $gnupgid
91 * @property string $fingerprint
92 * @property string $login
93 * @property string $creation_date Localized creation date
94 * @property string $modification_date Localized modification date
95 * @property string $due_date Localized due date
96 * @property string $others_infos
97 * @property string $others_infos_admin
98 * @property Picture $picture
99 * @property array $groups
100 * @property array $managed_groups
101 * @property integer|Adherent $parent Parent id if parent dep is not loaded, Adherent instance otherwise
102 * @property array $children
103 * @property boolean $admin better to rely on isAdmin()
104 * @property boolean $staff better to rely on isStaff()
105 * @property boolean $due_free better to rely on isDueFree()
106 * @property boolean $appears_in_list better to rely on appearsInMembersList()
107 * @property boolean $active better to rely on isActive()
108 * @property boolean $duplicate better to rely on isDuplicate()
109 * @property string $sadmin yes/no
110 * @property string $sstaff yes/no
111 * @property string $sdue_free yes/no
112 * @property string $sappears_in_list yes/no
113 * @property string $sactive yes/no
114 * @property string $sfullname
115 * @property string $sname
116 * @property string $saddress Concatened address and continuation
117 * @property string $contribstatus State of member contributions
118 * @property string $days_remaining
119 * @property-read integer $parent_id
120 * @property Social $social Social networks/Contact
121 * @property string $number Member number
129 public const TABLE
= 'adherents';
130 public const PK
= 'id_adh';
133 public const MAN
= 1;
134 public const WOMAN
= 2;
136 public const AFTER_ADD_DEFAULT
= 0;
137 public const AFTER_ADD_TRANS
= 1;
138 public const AFTER_ADD_NEW
= 2;
139 public const AFTER_ADD_SHOW
= 3;
140 public const AFTER_ADD_LIST
= 4;
141 public const AFTER_ADD_HOME
= 5;
146 private $_company_name;
151 private $_birth_place;
157 //Contact information
159 private $_address_continuation; /** TODO: remove */
167 private $_fingerprint;
168 //Galette relative information
169 private $_appears_in_list;
171 private $_staff = false;
175 private $_creation_date;
176 private $_modification_date;
178 private $_others_infos;
179 private $_others_infos_admin;
182 private $_days_remaining;
183 private $_groups = [];
184 private $_managed_groups = [];
187 private $_duplicate = false;
191 private $_row_classes;
193 private $_self_adh = false;
194 private $_deps = array(
205 private $preferences;
209 private $parent_fields = [
217 private $errors = [];
219 private $sendmail = false;
222 * Default constructor
224 * @param Db $zdb Database instance
225 * @param mixed $args Either a ResultSet row, its id or its
226 * login or its email for to load s specific
227 * member, or null to just instantiate object
228 * @param false|array|null $deps Dependencies configuration, see Adherent::$_deps
230 public function __construct(Db
$zdb, $args = null, $deps = null)
236 if ($deps !== null) {
237 if (is_array($deps)) {
238 $this->_deps
= array_merge(
242 } elseif ($deps === false) {
244 $this->_deps
= array_fill_keys(
245 array_keys($this->_deps
),
250 '$deps should be an array, ' . gettype($deps) . ' given!',
256 if ($args == null ||
is_int($args)) {
257 if (is_int($args) && $args > 0) {
260 $this->_active
= true;
261 $this->_language
= $i18n->getID();
262 $this->_creation_date
= date("Y-m-d");
263 $this->_status
= $this->getDefaultStatus();
264 $this->_title
= null;
265 $this->_gender
= self
::NC
;
266 $gp = new Password($this->zdb
);
267 $this->_password
= $gp->makeRandomPassword();
268 $this->_picture
= new Picture();
269 $this->_admin
= false;
270 $this->_staff
= false;
271 $this->_due_free
= false;
272 $this->_appears_in_list
= false;
273 $this->_parent
= null;
275 if ($this->_deps
['dynamics'] === true) {
276 $this->loadDynamicFields();
279 } elseif (is_object($args)) {
280 $this->loadFromRS($args);
281 } elseif (is_string($args)) {
282 $this->loadFromLoginOrMail($args);
287 * Loads a member from its id
289 * @param int $id the identifier for the member to load
291 * @return bool true if query succeed, false otherwise
293 public function load(int $id): bool
296 $select = $this->zdb
->select(self
::TABLE
, 'a');
299 array('b' => PREFIX_DB
. Status
::TABLE
),
300 'a.' . Status
::PK
. '=b.' . Status
::PK
,
301 array('priorite_statut')
302 )->where(array(self
::PK
=> $id));
304 $results = $this->zdb
->execute($select);
306 if ($results->count() === 0) {
310 $this->loadFromRS($results->current());
312 } catch (Throwable
$e) {
314 'Cannot load member form id `' . $id . '` | ' . $e->getMessage(),
322 * Loads a member from its login
324 * @param string $login login for the member to load
328 public function loadFromLoginOrMail(string $login): bool
331 $select = $this->zdb
->select(self
::TABLE
);
332 if (GaletteMail
::isValidEmail($login)) {
333 //we got a valid email address, use it
334 $select->where(array('email_adh' => $login));
336 ///we did not get an email address, consider using login
337 $select->where(array('login_adh' => $login));
340 $results = $this->zdb
->execute($select);
341 $result = $results->current();
343 $this->loadFromRS($result);
346 } catch (Throwable
$e) {
348 'Cannot load member form login `' . $login . '` | ' .
357 * Populate object from a resultset row
359 * @param ResultSet $r the resultset row
363 private function loadFromRS($r): void
365 $this->_self_adh
= false;
366 $this->_id
= $r->id_adh
;
368 if ($r->titre_adh
!== null) {
369 $this->_title
= new Title((int)$r->titre_adh
);
371 $this->_company_name
= $r->societe_adh
;
372 $this->_name
= $r->nom_adh
;
373 $this->_surname
= $r->prenom_adh
;
374 $this->_nickname
= $r->pseudo_adh
;
375 if ($r->ddn_adh
!= '1901-01-01') {
376 $this->_birthdate
= $r->ddn_adh
;
378 $this->_birth_place
= $r->lieu_naissance
;
379 $this->_gender
= (int)$r->sexe_adh
;
380 $this->_job
= $r->prof_adh
;
381 $this->_language
= $r->pref_lang
;
382 $this->_active
= $r->activite_adh
== 1;
383 $this->_status
= (int)$r->id_statut
;
384 //Contact information
385 $this->_address
= $r->adresse_adh
;
386 /** TODO: remove and merge with address */
387 $this->_address_continuation
= $r->adresse2_adh
;
388 $this->_zipcode
= $r->cp_adh
;
389 $this->_town
= $r->ville_adh
;
390 $this->_country
= $r->pays_adh
;
391 $this->_phone
= $r->tel_adh
;
392 $this->_gsm
= $r->gsm_adh
;
393 $this->_email
= $r->email_adh
;
394 $this->_gnupgid
= $r->gpgid
;
395 $this->_fingerprint
= $r->fingerprint
;
396 //Galette relative information
397 $this->_appears_in_list
= $r->bool_display_info
== 1;
398 $this->_admin
= $r->bool_admin_adh
== 1;
400 isset($r->priorite_statut
)
401 && $r->priorite_statut
< Members
::NON_STAFF_MEMBERS
403 $this->_staff
= true;
405 $this->_due_free
= $r->bool_exempt_adh
== 1;
406 $this->_login
= $r->login_adh
;
407 $this->_password
= $r->mdp_adh
;
408 $this->_creation_date
= $r->date_crea_adh
;
409 if ($r->date_modif_adh
!= '1901-01-01') {
410 $this->_modification_date
= $r->date_modif_adh
;
412 $this->_modification_date
= $this->_creation_date
;
414 $this->_due_date
= $r->date_echeance
;
415 $this->_others_infos
= $r->info_public_adh
;
416 $this->_others_infos_admin
= $r->info_adh
;
417 $this->_number
= $r->num_adh
;
419 if ($r->parent_id
!== null) {
420 $this->_parent
= (int)$r->parent_id
;
421 if ($this->_deps
['parent'] === true) {
426 if ($this->_deps
['children'] === true) {
427 $this->loadChildren();
430 if ($this->_deps
['picture'] === true) {
431 $this->_picture
= new Picture($this->_id
);
434 if ($this->_deps
['groups'] === true) {
438 if ($this->_deps
['dues'] === true) {
442 if ($this->_deps
['dynamics'] === true) {
443 $this->loadDynamicFields();
446 if ($this->_deps
['socials'] === true) {
447 $this->loadSocials();
456 private function loadParent(): void
458 if (!$this->_parent
instanceof Adherent
) {
459 $deps = array_fill_keys(array_keys($this->_deps
), false);
460 $this->_parent
= new Adherent($this->zdb
, (int)$this->_parent
, $deps);
465 * Load member children
469 private function loadChildren(): void
471 $this->_children
= array();
474 $select = $this->zdb
->select(self
::TABLE
);
477 )->where(['parent_id' => $this->_id
]);
479 $results = $this->zdb
->execute($select);
481 if ($results->count() > 0) {
482 foreach ($results as $row) {
483 $deps = $this->_deps
;
484 $deps['children'] = false;
485 $deps['parent'] = false;
486 $this->_children
[] = new Adherent($this->zdb
, (int)$row->$id, $deps);
489 } catch (Throwable
$e) {
491 'Cannot load children for member #' . $this->_id
. ' | ' .
504 public function loadGroups(): void
506 $this->_groups
= Groups
::loadGroups($this->_id
);
507 $this->_managed_groups
= Groups
::loadManagedGroups($this->_id
);
511 * Load member social network/contact information
515 public function loadSocials(): void
517 $this->_socials
= Social
::getListForMember($this->_id
);
521 * Retrieve status from preferences
526 private function getDefaultStatus(): int
529 if ($preferences->pref_statut
!= '') {
530 return $preferences->pref_statut
;
533 'Unable to get pref_statut; is it defined in preferences?',
536 return Status
::DEFAULT_STATUS
;
541 * Check for dues status
545 private function checkDues(): void
547 //how many days since our beloved member has been created
548 $date_now = new \
DateTime();
549 $this->_oldness
= $date_now->diff(
550 new \
DateTime($this->_creation_date
)
553 if ($this->isDueFree()) {
554 //no fee required, we don't care about dates
555 $this->_row_classes
.= ' cotis-exempt';
557 //ok, fee is required. Let's check the dates
558 if ($this->_due_date
== '') {
559 $this->_row_classes
.= ' cotis-never';
561 $date_end = new \
DateTime($this->_due_date
);
562 $date_diff = $date_now->diff($date_end);
563 $this->_days_remaining
= ($date_diff->invert
== 1)
564 ?
$date_diff->days
* -1
567 if ($this->_days_remaining
== 0) {
568 $this->_row_classes
.= ' cotis-lastday';
569 } elseif ($this->_days_remaining
< 0) {
570 //check if member is still active
571 $this->_row_classes
.= $this->isActive() ?
' cotis-late' : ' cotis-old';
572 } elseif ($this->_days_remaining
< 30) {
573 $this->_row_classes
.= ' cotis-soon';
575 $this->_row_classes
.= ' cotis-ok';
586 public function isAdmin(): bool
588 return $this->_admin
;
592 * Is user member of staff?
596 public function isStaff(): bool
598 return $this->_staff
;
602 * Is member freed of dues?
606 public function isDueFree(): bool
608 return $this->_due_free
;
612 * Is member in specified group?
614 * @param string $group_name Group name
618 public function isGroupMember(string $group_name): bool
620 if (!$this->isDepEnabled('groups')) {
624 foreach ($this->_groups
as $g) {
625 if ($g->getName() == $group_name) {
633 * Is member manager of specified group?
635 * @param string $group_name Group name
639 public function isGroupManager(string $group_name): bool
641 if (!$this->isDepEnabled('groups')) {
645 foreach ($this->_managed_groups
as $mg) {
646 if ($mg->getName() == $group_name) {
654 * Does current member represents a company?
658 public function isCompany(): bool
660 return trim($this->_company_name ??
'') != '';
664 * Is current member a man?
668 public function isMan(): bool
670 return (int)$this->_gender
=== self
::MAN
;
674 * Is current member a woman?
678 public function isWoman(): bool
680 return (int)$this->_gender
=== self
::WOMAN
;
685 * Can member appears in public members list?
689 public function appearsInMembersList(): bool
691 return $this->_appears_in_list
;
699 public function isActive(): bool
701 return $this->_active
;
705 * Does member have uploaded a picture?
709 public function hasPicture(): bool
711 return $this->_picture
->hasPicture();
715 * Does member have a parent?
719 public function hasParent(): bool
721 return !empty($this->_parent
);
725 * Does member have children?
729 public function hasChildren(): bool
731 if ($this->_children
=== null) {
734 'Children has not been loaded!',
740 return count($this->_children
) > 0;
745 * Get row class related to current fee status
747 * @param boolean $public we want the class for public pages
749 * @return string the class to apply
751 public function getRowClass(bool $public = false): string
753 $strclass = ($this->isActive()) ?
'active' : 'inactive';
754 if ($public === false) {
755 $strclass .= $this->_row_classes
;
761 * Get current member due status
763 * @return string i18n string representing state of due
765 public function getDues(): string
768 $date_now = new \
DateTime();
769 if ($this->isDueFree()) {
770 $ret = _T("Freed of dues");
771 } elseif ($this->_due_date
== '') {
772 $patterns = array('/%days/', '/%date/');
773 $cdate = new \
DateTime($this->_creation_date
);
776 $cdate->format(__("Y-m-d"))
778 if ($this->_active
) {
782 _T("Never contributed: Registered %days days ago (since %date)")
785 $ret = _T("Never contributed");
787 } elseif ($this->_days_remaining
== 0) {
788 $ddate = new \
DateTime($this->_due_date
);
789 $date_diff = $date_now->diff($ddate);
790 if ($date_diff->invert
== 0) {
791 $ret = _T("Last day!");
793 $ret = _T("Late since today!");
795 } elseif ($this->_days_remaining
< 0) {
796 $ddate = new \
DateTime($this->_due_date
);
797 $patterns = array('/%days/', '/%date/');
799 $this->_days_remaining
* -1,
800 $ddate->format(__("Y-m-d"))
802 if ($this->_active
) {
806 _T("Late of %days days (since %date)")
809 $ret = _T("No longer member");
812 $ddate = new \
DateTime($this->_due_date
);
813 $patterns = array('/%days/', '/%date/');
815 $this->_days_remaining
,
816 $ddate->format(__("Y-m-d"))
821 _T("%days days remaining (ending on %date)")
828 * Retrieve Full name and surname for the specified member id
830 * @param Db $zdb Database instance
831 * @param integer $id Member id
832 * @param boolean $wid Add member id
833 * @param boolean $wnick Add member nickname
835 * @return string formatted Name and Surname
837 public static function getSName(Db
$zdb, int $id, bool $wid = false, bool $wnick = false): string
840 $select = $zdb->select(self
::TABLE
);
841 $select->where([self
::PK
=> $id]);
843 $results = $zdb->execute($select);
844 $row = $results->current();
845 return self
::getNameWithCase(
849 ($wid === true ?
$row->id_adh
: false),
850 ($wnick === true ?
$row->pseudo_adh
: false)
852 } catch (Throwable
$e) {
854 'Cannot get formatted name for member form id `' . $id . '` | ' .
863 * Get member name with correct case
865 * @param string $name Member name
866 * @param string $surname Member surname
867 * @param false|Title $title Member title to show or false
868 * @param false|integer $id Member id to display or false
869 * @param false|string $nick Member nickname to display or false
873 public static function getNameWithCase(
882 if ($title instanceof Title
) {
883 $str .= $title->tshort
. ' ';
886 $str .= mb_strtoupper($name ??
'', 'UTF-8') . ' ' .
887 ucwords(mb_strtolower($surname ??
'', 'UTF-8'), " \t\r\n\f\v-_|");
889 if ($id !== false ||
!empty($nick)) {
901 if ($id !== false ||
!empty($nick)) {
904 return strip_tags($str);
908 * Change password for a given user
910 * @param Db $zdb Database instance
911 * @param integer $id_adh Member identifier
912 * @param string $pass New password
916 public static function updatePassword(Db
$zdb, int $id_adh, string $pass): bool
919 $cpass = password_hash($pass, PASSWORD_BCRYPT
);
921 $update = $zdb->update(self
::TABLE
);
923 array('mdp_adh' => $cpass)
924 )->where([self
::PK
=> $id_adh]);
925 $zdb->execute($update);
927 'Password for `' . $id_adh . '` has been updated.',
931 } catch (Throwable
$e) {
933 'An error occurred while updating password for `' . $id_adh .
934 '` | ' . $e->getMessage(),
944 * @param string $field Field name
948 private function getFieldLabel(string $field): string
950 $label = $this->fields
[$field]['label'];
952 $label = str_replace(' ', ' ', $label);
953 //remove trailing ':' and then trim
954 $label = trim(trim($label ??
'', ':'));
959 * Retrieve fields from database
961 * @param Db $zdb Database instance
965 public static function getDbFields(Db
$zdb): array
967 $columns = $zdb->getColumns(self
::TABLE
);
969 foreach ($columns as $col) {
970 $fields[] = $col->getName();
976 * Mark as self membership
980 public function setSelfMembership(): void
982 $this->_self_adh
= true;
986 * Is member up to date?
990 public function isUp2Date(): bool
992 if (!$this->isDepEnabled('dues')) {
996 if ($this->isDueFree()) {
997 //member is due free, he's up to date.
1000 //let's check from end date, if present
1001 if ($this->_due_date
== null) {
1004 $ech = new \
DateTime($this->_due_date
);
1005 $now = new \
DateTime();
1006 $now->setTime(0, 0, 0);
1007 return $ech >= $now;
1015 * @param Preferences $preferences Preferences instance
1016 * @param array $fields Members fields configuration
1017 * @param History $history History instance
1021 public function setDependencies(
1022 Preferences
$preferences,
1026 $this->preferences
= $preferences;
1027 $this->fields
= $fields;
1028 $this->history
= $history;
1032 * Check posted values validity
1034 * @param array $values All values to check, basically the $_POST array
1035 * after sending the form
1036 * @param array $required Array of required fields
1037 * @param array $disabled Array of disabled fields
1039 * @return true|array
1041 public function check(array $values, array $required, array $disabled)
1045 $this->errors
= array();
1048 foreach ($values as &$rawvalue) {
1049 if (is_string($rawvalue)) {
1050 $rawvalue = strip_tags($rawvalue);
1054 $fields = self
::getDbFields($this->zdb
);
1056 //reset company name if needed
1057 if (!isset($values['is_company'])) {
1058 unset($values['is_company']);
1059 $values['societe_adh'] = '';
1062 //no parent if checkbox was unchecked
1064 !isset($values['attach'])
1065 && empty($this->_id
)
1066 && isset($values['parent_id'])
1068 unset($values['parent_id']);
1071 if (isset($values['duplicate'])) {
1072 //if we're duplicating, keep a trace (if an error occurs)
1073 $this->_duplicate
= true;
1076 foreach ($fields as $key) {
1077 //first, let's sanitize values
1078 $key = strtolower($key);
1079 $prop = '_' . $this->fields
[$key]['propname'];
1081 if (isset($values[$key])) {
1082 $value = $values[$key];
1083 if ($value !== true && $value !== false) {
1084 $value = trim($value ??
'');
1086 } elseif (empty($this->_id
)) {
1088 case 'bool_admin_adh':
1089 case 'bool_exempt_adh':
1090 case 'bool_display_info':
1093 case 'activite_adh':
1094 //values that are set at object instantiation
1097 case 'date_crea_adh':
1103 //values that are set at object instantiation
1104 $value = $this->$prop;
1114 //keep stored value on update
1115 if ($prop != '_password' ||
isset($values['mdp_adh']) && isset($values['mdp_adh2'])) {
1116 $value = $this->$prop;
1122 // if the field is enabled, check it
1123 if (!isset($disabled[$key])) {
1124 // fill up the adherent structure
1125 if ($value !== null && $value !== true && $value !== false && !is_object($value)) {
1126 $value = stripslashes($value);
1128 $this->$prop = $value;
1130 // now, check validity
1131 if ($value !== null && $value != '') {
1132 if ($key !== 'mdp_adh') {
1133 $this->validate($key, $value, $values);
1135 } elseif (empty($this->_id
)) {
1136 //ensure login and password are not empty
1137 if (($key == 'login_adh' ||
$key == 'mdp_adh') && !isset($required[$key])) {
1138 $p = new Password($this->zdb
);
1139 $generated_value = $p->makeRandomPassword(15);
1140 if ($key == 'login_adh') {
1141 //'@' is not permitted in logins
1142 $this->$prop = str_replace('@', 'a', $generated_value);
1144 $this->$prop = $generated_value;
1151 //password checks need data to be previously set
1152 if (isset($values['mdp_adh'])) {
1153 $this->validate('mdp_adh', $values['mdp_adh'], $values);
1156 // missing required fields?
1157 foreach ($required as $key => $val) {
1158 $prop = '_' . $this->fields
[$key]['propname'];
1160 if (!isset($disabled[$key])) {
1161 $mandatory_missing = false;
1162 if (!isset($this->$prop) ||
$this->$prop == '') {
1163 $mandatory_missing = true;
1164 } elseif ($key === 'titre_adh' && $this->$prop == '-1') {
1165 $mandatory_missing = true;
1168 if ($mandatory_missing === true) {
1169 $this->errors
[] = str_replace(
1171 '<a href="#' . $key . '">' . $this->getFieldLabel($key) . '</a>',
1172 _T("- Mandatory field %field empty.")
1178 //attach to/detach from parent
1179 if (isset($values['detach_parent'])) {
1180 $this->_parent
= null;
1183 if ($login->isGroupManager() && !$login->isAdmin() && !$login->isStaff()) {
1184 if (!isset($values['groups_adh'])) {
1185 $this->errors
[] = _T('You have to select a group you own!');
1187 foreach ($values['groups_adh'] as $group) {
1188 list($gid) = explode('|', $group);
1189 if (!$login->isGroupManager($gid)) {
1190 $this->errors
[] = _T('You have to select a group you own!');
1196 $this->dynamicsCheck($values, $required, $disabled);
1197 $this->checkSocials($values);
1199 if (count($this->errors
) > 0) {
1201 'Some errors has been thew attempting to edit/store a member' . "\n" .
1202 print_r($this->errors
, true),
1205 return $this->errors
;
1210 'Member checked successfully.',
1218 * Validate data for given key
1219 * Set valid data in current object, also resets errors list
1221 * @param string $field Field name
1222 * @param mixed $value Value we want to set
1223 * @param array $values All values, for some references
1227 public function validate(string $field, $value, array $values): void
1229 global $preferences;
1231 $prop = '_' . $this->fields
[$field]['propname'];
1233 if ($value === null ||
(is_string($value) && trim($value) == '')) {
1234 //empty values are OK
1235 $this->$prop = $value;
1241 case 'date_crea_adh':
1242 case 'date_modif_adh_':
1244 case 'date_echeance':
1246 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
1248 //try with non localized date
1249 $d = \DateTime
::createFromFormat("Y-m-d", $value);
1251 throw new \
Exception('Incorrect format');
1255 if ($field === 'ddn_adh') {
1256 $now = new \
DateTime();
1257 $now->setTime(0, 0, 0);
1258 $d->setTime(0, 0, 0);
1260 $diff = $now->diff($d);
1261 $days = (int)$diff->format('%R%a');
1263 $this->errors
[] = _T('- Birthdate must be set in the past!');
1266 $years = (int)$diff->format('%R%Y');
1267 if ($years <= -200) {
1268 $this->errors
[] = str_replace(
1271 _T('- Members must be less than 200 years old (currently %years)!')
1275 $this->$prop = $d->format('Y-m-d');
1276 } catch (Throwable
$e) {
1278 'Wrong date format. field: ' . $field .
1279 ', value: ' . $value . ', expected fmt: ' .
1280 __("Y-m-d") . ' | ' . $e->getMessage(),
1283 $this->errors
[] = str_replace(
1290 $this->getFieldLabel($field)
1292 _T("- Wrong date format (%date_format) for %field!")
1297 if ($value !== null && $value !== '') {
1298 if ($value == '-1') {
1299 $this->$prop = null;
1300 } elseif (!$value instanceof Title
) {
1301 $this->$prop = new Title((int)$value);
1304 $this->$prop = null;
1308 if (!GaletteMail
::isValidEmail($value)) {
1309 $this->errors
[] = _T("- Non-valid E-Mail address!") .
1310 ' (' . $this->getFieldLabel($field) . ')';
1312 if ($field == 'email_adh') {
1314 $select = $this->zdb
->select(self
::TABLE
);
1317 )->where(array('email_adh' => $value));
1318 if (!empty($this->_id
)) {
1319 $select->where
->notEqualTo(
1325 $results = $this->zdb
->execute($select);
1326 if ($results->count() !== 0) {
1327 $this->errors
[] = _T("- This E-Mail address is already used by another member!");
1329 } catch (Throwable
$e) {
1331 'An error occurred checking member email uniqueness.',
1334 $this->errors
[] = _T("An error has occurred while looking if login already exists.");
1339 /** FIXME: add a preference for login length */
1340 if (strlen($value) < 2) {
1341 $this->errors
[] = str_replace(
1344 _T("- The username must be composed of at least %i characters!")
1347 //check if login does not contain the @ character
1348 if (strpos($value, '@') != false) {
1349 $this->errors
[] = _T("- The username cannot contain the @ character");
1351 //check if login is already taken
1353 $select = $this->zdb
->select(self
::TABLE
);
1356 )->where(array('login_adh' => $value));
1357 if (!empty($this->_id
)) {
1358 $select->where
->notEqualTo(
1364 $results = $this->zdb
->execute($select);
1366 $results->count() !== 0
1367 ||
$value == $preferences->pref_admin_login
1369 $this->errors
[] = _T("- This username is already in use, please choose another one!");
1371 } catch (Throwable
$e) {
1373 'An error occurred checking member login uniqueness.',
1376 $this->errors
[] = _T("An error has occurred while looking if login already exists.");
1383 $this->_self_adh
!== true
1384 && (!isset($values['mdp_adh2'])
1385 ||
$values['mdp_adh2'] != $value)
1387 $this->errors
[] = _T("- The passwords don't match!");
1389 $this->_self_adh
=== true
1390 && !crypt($value, $values['mdp_crypt']) == $values['mdp_crypt']
1392 $this->errors
[] = _T("Password misrepeated: ");
1394 $pinfos = password_get_info($value);
1395 //check if value is already a hash
1396 if ($pinfos['algo'] == 0) {
1397 $this->$prop = password_hash(
1402 $pwcheck = new \Galette\Util\
Password($preferences);
1403 $pwcheck->setAdherent($this);
1404 if (!$pwcheck->isValid($value)) {
1405 $this->errors
= array_merge(
1407 $pwcheck->getErrors()
1415 $this->$prop = (int)$value;
1416 //check if status exists
1417 $select = $this->zdb
->select(Status
::TABLE
);
1418 $select->where([Status
::PK
=> $value]);
1420 $results = $this->zdb
->execute($select);
1421 $result = $results->current();
1423 $this->errors
[] = str_replace(
1426 _T("Status #%id does not exists in database.")
1430 } catch (Throwable
$e) {
1432 'An error occurred checking status existence: ' . $e->getMessage(),
1435 $this->errors
[] = _T("An error has occurred while looking if status does exists.");
1439 if (in_array($value, [self
::NC
, self
::MAN
, self
::WOMAN
])) {
1440 $this->$prop = (int)$value;
1442 $this->errors
[] = _T("Gender %gender does not exists!");
1446 $this->$prop = ($value instanceof Adherent
) ?
(int)$value->id
: (int)$value;
1447 $this->loadParent();
1457 public function store(): bool
1459 global $hist, $emitter, $login;
1462 if (!$login->isAdmin() && !$login->isStaff() && !$login->isGroupManager() && $this->id
== '') {
1463 if ($this->preferences
->pref_bool_create_member
) {
1464 $this->_parent
= $login->id
;
1470 $fields = self
::getDbFields($this->zdb
);
1472 foreach ($fields as $field) {
1474 $field !== 'date_modif_adh'
1475 ||
empty($this->_id
)
1477 $prop = '_' . $this->fields
[$field]['propname'];
1479 ($field === 'bool_admin_adh'
1480 ||
$field === 'bool_exempt_adh'
1481 ||
$field === 'bool_display_info'
1482 ||
$field === 'activite_adh')
1483 && $this->$prop === false
1485 //Handle booleans for postgres ; bugs #18899 and #19354
1486 $values[$field] = $this->zdb
->isPostgres() ?
'false' : 0;
1487 } elseif ($field === 'parent_id') {
1489 if ($this->_parent
=== null) {
1490 $values['parent_id'] = new Expression('NULL');
1491 } elseif ($this->parent
instanceof Adherent
) {
1492 $values['parent_id'] = $this->_parent
->id
;
1494 $values['parent_id'] = $this->_parent
;
1497 $values[$field] = $this->$prop;
1502 //an empty value will cause date to be set to 1901-01-01, a null
1503 //will result in 0000-00-00. We want a database NULL value here.
1504 if (!$this->_birthdate
) {
1505 $values['ddn_adh'] = new Expression('NULL');
1507 if (!$this->_due_date
) {
1508 $values['date_echeance'] = new Expression('NULL');
1511 if ($this->_title
instanceof Title
) {
1512 $values['titre_adh'] = $this->_title
->id
;
1514 $values['titre_adh'] = new Expression('NULL');
1517 if (!$this->_parent
) {
1518 $values['parent_id'] = new Expression('NULL');
1521 if (!$this->_number
) {
1522 $values['num_adh'] = new Expression('NULL');
1525 //fields that cannot be null
1527 '_surname' => 'prenom_adh',
1528 '_nickname' => 'pseudo_adh',
1529 '_address' => 'adresse_adh',
1530 '_zipcode' => 'cp_adh',
1531 '_town' => 'ville_adh'
1533 foreach ($notnull as $prop => $field) {
1534 if ($this->$prop === null) {
1535 $values[$field] = '';
1540 if (empty($this->_id
)) {
1541 //we're inserting a new member
1542 unset($values[self
::PK
]);
1543 //set modification date
1544 $this->_modification_date
= date('Y-m-d');
1545 $values['date_modif_adh'] = $this->_modification_date
;
1547 $insert = $this->zdb
->insert(self
::TABLE
);
1548 $insert->values($values);
1549 $add = $this->zdb
->execute($insert);
1550 if ($add->count() > 0) {
1551 $this->_id
= $this->zdb
->getLastGeneratedValue($this);
1552 $this->_picture
= new Picture($this->_id
);
1554 if ($this->_self_adh
) {
1556 _T("Self_subscription as a member: ") .
1557 $this->getNameWithCase($this->_name
, $this->_surname
),
1562 _T("Member card added"),
1568 $event = 'member.add';
1570 $hist->add(_T("Fail to add new member."));
1571 throw new \
Exception(
1572 'An error occurred inserting new member!'
1576 //we're editing an existing member
1577 if (!$this->isDueFree()) {
1579 $due_date = Contribution
::getDueDate($this->zdb
, $this->_id
);
1581 $values['date_echeance'] = $due_date;
1585 if (!$this->_password
) {
1586 unset($values['mdp_adh']);
1589 $update = $this->zdb
->update(self
::TABLE
);
1590 $update->set($values);
1591 $update->where([self
::PK
=> $this->_id
]);
1593 $edit = $this->zdb
->execute($update);
1595 //edit == 0 does not mean there were an error, but that there
1596 //were nothing to change
1597 if ($edit->count() > 0) {
1598 $this->updateModificationDate();
1600 _T("Member card updated"),
1605 $event = 'member.edit';
1610 $success = $this->dynamicsStore();
1611 $this->storeSocials($this->id
);
1614 //send event at the end of process, once all has been stored
1615 if ($event !== null) {
1616 $emitter->emit($event, $this);
1619 } catch (Throwable
$e) {
1621 'Something went wrong :\'( | ' . $e->getMessage() . "\n" .
1622 $e->getTraceAsString(),
1630 * Update member modification date
1634 private function updateModificationDate(): void
1637 $modif_date = date('Y-m-d');
1638 $update = $this->zdb
->update(self
::TABLE
);
1640 array('date_modif_adh' => $modif_date)
1641 )->where([self
::PK
=> $this->_id
]);
1643 $edit = $this->zdb
->execute($update);
1644 $this->_modification_date
= $modif_date;
1645 } catch (Throwable
$e) {
1647 'Something went wrong updating modif date :\'( | ' .
1648 $e->getMessage() . "\n" . $e->getTraceAsString(),
1656 * Global getter method
1658 * @param string $name name of the property we want to retrieve
1662 public function __get(string $name)
1665 'admin', 'staff', 'due_free', 'appears_in_list', 'active',
1666 'row_classes', 'oldness', 'duplicate', 'groups', 'managed_groups'
1668 if (!defined('GALETTE_TESTS')) {
1669 $forbidden[] = 'password'; //keep that for tests only
1673 'sadmin', 'sstaff', 'sdue_free', 'sappears_in_list', 'sactive',
1674 'stitle', 'sstatus', 'sfullname', 'sname', 'saddress',
1675 'rbirthdate', 'sgender', 'contribstatus',
1678 $socials = array('website', 'msn', 'jabber', 'icq');
1680 if (in_array($name, $forbidden)) {
1682 'Calling property "' . $name . '" directly is discouraged.',
1687 return $this->isAdmin();
1689 return $this->isStaff();
1691 return $this->isDueFree();
1692 case 'appears_in_list':
1693 return $this->appearsInMembersList();
1695 return $this->isActive();
1697 return $this->isDuplicate();
1699 return $this->getGroups();
1700 case 'managed_groups':
1701 return $this->getManagedGroups();
1703 throw new \
RuntimeException("Call to __get for '$name' is forbidden!");
1707 if (in_array($name, $virtuals)) {
1708 if (substr($name, 0, 1) !== '_') {
1709 $real = '_' . substr($name, 1);
1716 case 'sappears_in_list':
1718 return (($this->$real) ?
_T("Yes") : _T("No"));
1721 return (($this->$real) ?
_T("Active") : _T("Inactive"));
1724 if (isset($this->_title
) && $this->_title
instanceof Title
) {
1725 return $this->_title
->tshort
;
1731 $status = new Status($this->zdb
);
1732 return $status->getLabel($this->_status
);
1735 return $this->getNameWithCase(
1738 (isset($this->_title
) ?
$this->title
: false)
1742 $address = $this->_address
;
1743 if ($this->_address_continuation
!== '' && $this->_address_continuation
!== null) {
1744 $address .= "\n" . $this->_address_continuation
;
1749 return $this->getNameWithCase($this->_name
, $this->_surname
);
1752 return $this->_birthdate
;
1755 switch ($this->gender
) {
1761 return _T('Unspecified');
1764 case 'contribstatus':
1765 return $this->getDues();
1770 //for backward compatibility
1771 if (in_array($name, $socials)) {
1772 $values = Social
::getListForMember($this->_id
, $name);
1773 return $values[0] ??
null;
1776 if (substr($name, 0, 1) !== '_') {
1777 $rname = '_' . $name;
1785 if ($this->$rname !== null) {
1786 return (int)$this->$rname;
1792 case 'address_continuation':
1793 return $this->$rname ??
'';
1796 case 'creation_date':
1797 case 'modification_date':
1799 if ($this->$rname != '') {
1801 $d = new \
DateTime($this->$rname);
1802 return $d->format(__("Y-m-d"));
1803 } catch (Throwable
$e) {
1804 //oops, we've got a bad date :/
1806 'Bad date (' . $this->$rname . ') | ' .
1810 return $this->$rname;
1815 return ($this->_parent
instanceof Adherent
) ?
(int)$this->_parent
->id
: (int)$this->_parent
;
1818 if (!property_exists($this, $rname)) {
1820 "Unknown property '$rname'",
1825 return $this->$rname;
1833 * If member does not have an email address, but is attached to
1834 * another member, we'll take information from its parent.
1838 public function getEmail(): string
1840 $email = $this->_email
;
1841 if (empty($email)) {
1842 $this->loadParent();
1843 $email = $this->parent
->email
;
1846 return $email ??
'';
1850 * Get member address.
1851 * If member does not have an address, but is attached to another member, we'll take information from its parent.
1855 public function getAddress(): string
1857 $address = $this->_address
;
1858 if (empty($address) && $this->hasParent()) {
1859 $this->loadParent();
1860 $address = $this->parent
->address
;
1863 return $address ??
'';
1867 * Get member address continuation.
1868 * If member does not have an address, but is attached to another member, we'll take information from its parent.
1872 public function getAddressContinuation(): string
1874 $address = $this->_address
;
1875 $address_continuation = $this->_address_continuation
;
1876 if (empty($address) && $this->hasParent()) {
1877 $this->loadParent();
1878 $address_continuation = $this->parent
->address_continuation
;
1881 return $address_continuation ??
'';
1885 * Get member zipcode.
1886 * If member does not have an address, but is attached to another member, we'll take information from its parent.
1890 public function getZipcode(): string
1892 $address = $this->_address
;
1893 $zip = $this->_zipcode
;
1894 if (empty($address) && $this->hasParent()) {
1895 $this->loadParent();
1896 $zip = $this->parent
->zipcode
;
1904 * If member does not have an address, but is attached to another member, we'll take information from its parent.
1908 public function getTown(): string
1910 $address = $this->_address
;
1911 $town = $this->_town
;
1912 if (empty($address) && $this->hasParent()) {
1913 $this->loadParent();
1914 $town = $this->parent
->town
;
1921 * Get member country.
1922 * If member does not have an address, but is attached to another member, we'll take information from its parent.
1926 public function getCountry(): string
1928 $address = $this->_address
;
1929 $country = $this->_country
;
1930 if (empty($address) && $this->hasParent()) {
1931 $this->loadParent();
1932 $country = $this->parent
->country
;
1935 return $country ??
'';
1943 public function getAge(): string
1945 if ($this->_birthdate
== null) {
1949 $d = \DateTime
::createFromFormat('Y-m-d', $this->_birthdate
);
1952 'Invalid birthdate: ' . $this->_birthdate
,
1960 $d->diff(new \
DateTime())->y
,
1961 _T(' (%age years old)')
1966 * Get parent inherited fields
1970 public function getParentFields(): array
1972 return $this->parent_fields
;
1976 * Handle files (photo and dynamics files)
1978 * @param array $files Files sent
1980 * @return array|true
1982 public function handleFiles(array $files)
1986 if (isset($files['photo'])) {
1987 if ($files['photo']['error'] === UPLOAD_ERR_OK
) {
1988 if ($files['photo']['tmp_name'] != '') {
1989 if (is_uploaded_file($files['photo']['tmp_name'])) {
1990 $res = $this->picture
->store($files['photo']);
1993 = $this->picture
->getErrorMessage($res);
1997 } elseif ($files['photo']['error'] !== UPLOAD_ERR_NO_FILE
) {
1999 $this->picture
->getPhpErrorMessage($files['photo']['error']),
2002 $this->errors
[] = $this->picture
->getPhpErrorMessage(
2003 $files['photo']['error']
2007 $this->dynamicsFiles($files);
2009 if (count($this->errors
) > 0) {
2011 'Some errors has been thew attempting to edit/store a member files' . "\n" .
2012 print_r($this->errors
, true),
2015 return $this->errors
;
2022 * Set member as duplicate
2026 public function setDuplicate(): void
2028 //mark as duplicated
2029 $this->_duplicate
= true;
2030 $infos = $this->_others_infos_admin
;
2031 $this->_others_infos_admin
= str_replace(
2033 [$this->sname
, $this->_id
],
2034 _T('Duplicated from %name (%id)')
2036 if (!empty($infos)) {
2037 $this->_others_infos_admin
.= "\n" . $infos;
2041 //drop email, must be unique
2042 $this->_email
= null;
2043 //drop creation date
2044 $this->_creation_date
= date("Y-m-d");
2046 $this->_login
= null;
2048 $this->_picture
= new Picture();
2050 $this->_birthdate
= null;
2052 $this->_surname
= null;
2054 $this->_admin
= false;
2056 $this->_due_free
= false;
2060 * Get current errors
2064 public function getErrors(): array
2066 return $this->errors
;
2074 public function getGroups(): array
2076 if (!$this->isDepEnabled('groups')) {
2077 $this->loadGroups();
2079 return $this->_groups
;
2083 * Get user managed groups
2087 public function getManagedGroups(): array
2089 if (!$this->isDepEnabled('groups')) {
2090 $this->loadGroups();
2092 return $this->_managed_groups
;
2096 * Can current logged-in user create member
2098 * @param Login $login Login instance
2102 public function canCreate(Login
$login): bool
2104 global $preferences;
2106 if ($this->id
&& $login->id
== $this->id ||
$login->isAdmin() ||
$login->isStaff()) {
2110 if ($preferences->pref_bool_groupsmanagers_create_member
&& $login->isGroupManager()) {
2114 if ($preferences->pref_bool_create_member
&& $login->isLogged()) {
2122 * Can current logged-in user edit member
2124 * @param Login $login Login instance
2128 public function canEdit(Login
$login): bool
2130 global $preferences;
2132 //admin and staff users can edit, as well as member itself
2133 if ($this->id
&& $login->id
== $this->id ||
$login->isAdmin() ||
$login->isStaff()) {
2137 //parent can edit their child cards
2138 if ($this->hasParent() && $this->parent_id
=== $login->id
) {
2142 //group managers can edit members of groups they manage when pref is on
2143 if ($preferences->pref_bool_groupsmanagers_edit_member
&& $login->isGroupManager()) {
2144 foreach ($this->getGroups() as $g) {
2145 if ($login->isGroupManager($g->getId())) {
2155 * Can current logged-in user display member
2157 * @param Login $login Login instance
2161 public function canShow(Login
$login): bool
2163 //group managers can show members of groups they manage
2164 if ($login->isGroupManager()) {
2165 foreach ($this->getGroups() as $g) {
2166 if ($login->isGroupManager($g->getId())) {
2172 return $this->canEdit($login);
2176 * Are we currently duplicated a member?
2180 public function isDuplicate(): bool
2182 return $this->_duplicate
;
2186 * Flag creation mail sending
2188 * @param boolean $send True (default) to send creation email
2192 public function setSendmail(bool $send = true): self
2194 $this->sendmail
= $send;
2199 * Should we send administrative emails to member?
2203 public function sendEMail(): bool
2205 return $this->sendmail
;
2211 * @param integer $id Parent identifier
2215 public function setParent(int $id): self
2217 $this->_parent
= $id;
2218 $this->loadParent();
2223 * Reset dependencies to load
2227 public function disableAllDeps(): self
2229 foreach ($this->_deps
as &$dep) {
2236 * Enable all dependencies to load
2240 public function enableAllDeps(): self
2242 foreach ($this->_deps
as &$dep) {
2249 * Enable a load dependency
2251 * @param string $name Dependency name
2255 public function enableDep(string $name): self
2257 if (!isset($this->_deps
[$name])) {
2259 'dependency ' . $name . ' does not exists!',
2263 $this->_deps
[$name] = true;
2270 * Enable a load dependency
2272 * @param string $name Dependency name
2276 public function disableDep(string $name): self
2278 if (!isset($this->_deps
[$name])) {
2280 'dependency ' . $name . ' does not exists!',
2284 $this->_deps
[$name] = false;
2291 * Is load dependency enabled?
2293 * @param string $name Dependency name
2297 protected function isDepEnabled(string $name): bool
2299 return $this->_deps
[$name];