2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
27 interface UserFilterCondition
29 const COND_TRUE
= 'TRUE';
30 const COND_FALSE
= 'FALSE';
32 /** Check that the given user matches the rule.
34 public function buildCondition(UserFilter
&$uf);
37 abstract class UFC_OneChild
implements UserFilterCondition
41 public function __construct($child = null
)
43 if (!is_null($child) && ($child instanceof UserFilterCondition
)) {
44 $this->setChild($child);
48 public function setChild(UserFilterCondition
&$cond)
50 $this->child
=& $cond;
54 abstract class UFC_NChildren
implements UserFilterCondition
56 protected $children = array();
58 public function __construct()
60 $children = func_get_args();
61 foreach ($children as &$child) {
62 if (!is_null($child) && ($child instanceof UserFilterCondition
)) {
63 $this->addChild($child);
68 public function addChild(UserFilterCondition
&$cond)
70 $this->children
[] =& $cond;
73 protected function catConds(array $cond, $op, $fallback)
75 if (count($cond) == 0) {
77 } else if (count($cond) == 1) {
80 return '(' . implode(') ' . $op . ' (', $cond) . ')';
85 class UFC_True
implements UserFilterCondition
87 public function buildCondition(UserFilter
&$uf)
89 return self
::COND_TRUE
;
93 class UFC_False
implements UserFilterCondition
95 public function buildCondition(UserFilter
&$uf)
97 return self
::COND_FALSE
;
101 class UFC_Not
extends UFC_OneChild
103 public function buildCondition(UserFilter
&$uf)
105 $val = $this->child
->buildCondition($uf);
106 if ($val == self
::COND_TRUE
) {
107 return self
::COND_FALSE
;
108 } else if ($val == self
::COND_FALSE
) {
109 return self
::COND_TRUE
;
111 return 'NOT (' . $val . ')';
116 class UFC_And
extends UFC_NChildren
118 public function buildCondition(UserFilter
&$uf)
120 if (empty($this->children
)) {
121 return self
::COND_FALSE
;
123 $true = self
::COND_FALSE
;
125 foreach ($this->children
as &$child) {
126 $val = $child->buildCondition($uf);
127 if ($val == self
::COND_TRUE
) {
128 $true = self
::COND_TRUE
;
129 } else if ($val == self
::COND_FALSE
) {
130 return self
::COND_FALSE
;
135 return $this->catConds($conds, 'AND', $true);
140 class UFC_Or
extends UFC_NChildren
142 public function buildCondition(UserFilter
&$uf)
144 if (empty($this->children
)) {
145 return self
::COND_TRUE
;
147 $true = self
::COND_TRUE
;
149 foreach ($this->children
as &$child) {
150 $val = $child->buildCondition($uf);
151 if ($val == self
::COND_TRUE
) {
152 return self
::COND_TRUE
;
153 } else if ($val == self
::COND_FALSE
) {
154 $true = self
::COND_FALSE
;
159 return $this->catConds($conds, 'OR', $true);
164 /** Filters users who have a profile
166 class UFC_Profile
implements UserFilterCondition
168 public function buildCondition(UserFilter
&$uf)
170 return '$PID IS NOT NULL';
174 /** Filters users based on promo
175 * @param $comparison Comparison operator (>, =, ...)
176 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
177 * @param $promo Promo on which the filter is based
179 class UFC_Promo
implements UserFilterCondition
186 public function __construct($comparison, $grade, $promo)
188 $this->grade
= $grade;
189 $this->comparison
= $comparison;
190 $this->promo
= $promo;
191 if ($this->grade
!= UserFilter
::DISPLAY
) {
192 UserFilter
::assertGrade($this->grade
);
196 public function buildCondition(UserFilter
&$uf)
198 if ($this->grade
== UserFilter
::DISPLAY
) {
199 $sub = $uf->addDisplayFilter();
200 return XDB
::format('pd' . $sub . '.promo = {?}', $this->promo
);
202 $sub = $uf->addEducationFilter(true
, $this->grade
);
203 $field = 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
204 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->promo
);
209 /** Filters users based on name
210 * @param $type Type of name field on which filtering is done (firstname, lastname, ...)
211 * @param $text Text on which to filter
212 * @param $mode flag indicating search type (prefix, suffix, with particule, ...)
214 class UFC_Name
implements UserFilterCondition
226 public function __construct($type, $text, $mode)
233 private function buildNameQuery($type, $variant, $where, UserFilter
&$uf)
235 $sub = $uf->addNameFilter($type, $variant);
236 return str_replace('$ME', 'pn' . $sub, $where);
239 public function buildCondition(UserFilter
&$uf)
243 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
244 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
246 if (($this->mode
& self
::CONTAINS
) == 0) {
247 $right = XDB
::format('{?}', $this->text
);
249 } else if (($this->mode
& self
::CONTAINS
) == self
::PREFIX
) {
250 $right = XDB
::format('CONCAT({?}, \'%\')', $this->text
);
251 } else if (($this->mode
& self
::CONTAINS
) == self
::SUFFIX
) {
252 $right = XDB
::format('CONCAT(\'%\', {?})', $this->text
);
254 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->text
);
256 $cond = $left . $op . $right;
257 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
258 if (($this->mode
& self
::VARIANTS
) != 0 && isset(UserFilter
::$name_variants[$this->type
])) {
259 foreach (UserFilter
::$name_variants[$this->type
] as $var) {
260 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
263 return implode(' OR ', $conds);
267 /** Filters users based on death date
268 * @param $comparison Comparison operator
269 * @param $date Date to which death date should be compared
271 class UFC_Dead
implements UserFilterCondition
276 public function __construct($comparison = null
, $date = null
)
278 $this->comparison
= $comparison;
282 public function buildCondition(UserFilter
&$uf)
284 $str = 'p.deathdate IS NOT NULL';
285 if (!is_null($this->comparison
)) {
286 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
292 /** Filters users based on registration state
293 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
294 * @param $comparison Comparison operator
295 * @param $date Date to which users registration date should be compared
297 class UFC_Registered
implements UserFilterCondition
303 public function __construct($active = false
, $comparison = null
, $date = null
)
305 $this->only_active
= $active;
306 $this->comparison
= $comparison;
310 public function buildCondition(UserFilter
&$uf)
313 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
315 $date = 'a.uid IS NOT NULL AND a.state != \'pending\'';
317 if (!is_null($this->comparison
)) {
318 $date .= ' AND a.registration_date ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
324 /** Filters users based on profile update date
325 * @param $comparison Comparison operator
326 * @param $date Date to which profile update date must be compared
328 class UFC_ProfileUpdated
implements UserFilterCondition
333 public function __construct($comparison = null
, $date = null
)
335 $this->comparison
= $comparison;
339 public function buildCondition(UserFilter
&$uf)
341 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
345 /** Filters users based on next birthday date
346 * @param $comparison Comparison operator
347 * @param $date Date to which users next birthday date should be compared
349 class UFC_Birthday
implements UserFilterCondition
354 public function __construct($comparison = null
, $date = null
)
356 $this->comparison
= $comparison;
360 public function buildCondition(UserFilter
&$uf)
362 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
366 /** Filters users based on sex
367 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
369 class UFC_Sex
implements UserFilterCondition
372 public function __construct($sex)
377 public function buildCondition(UserFilter
&$uf)
379 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
380 return self
::COND_FALSE
;
382 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
387 /** Filters users based on group membership
388 * @param $group Group whose member we are selecting
389 * @param $admin Whether to restrict selection to admins of that group
391 class UFC_Group
implements UserFilterCondition
395 public function __construct($group, $admin = false
)
397 $this->group
= $group;
398 $this->admin
= $admin;
401 public function buildCondition(UserFilter
&$uf)
403 $sub = $uf->addGroupFilter($this->group
);
404 $where = 'gpm' . $sub . '.perms IS NOT NULL';
406 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
412 /** Filters users based on email address
413 * @param $email Email whose owner we are looking for
415 class UFC_Email
implements UserFilterCondition
418 public function __construct($email)
420 $this->email
= $email;
423 public function buildCondition(UserFilter
&$uf)
425 if (User
::isForeignEmailAddress($this->email
)) {
426 $sub = $uf->addEmailRedirectFilter($this->email
);
427 return XDB
::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email
);
428 } else if (User
::isVirtualEmailAddress($this->email
)) {
429 $sub = $uf->addVirtualEmailFilter($this->email
);
430 return 'vr' . $sub . '.redirect IS NOT NULL';
432 @list
($user, $domain) = explode('@', $this->email
);
433 $sub = $uf->addAliasFilter($user);
434 return 'al' . $sub . '.alias IS NOT NULL';
439 /** Filters users base on an email list
440 * @param $emails List of emails whose owner must be selected
442 class UFC_EmailList
implements UserFilterCondition
445 public function __construct($emails)
447 $this->emails
= $emails;
450 public function buildCondition(UserFilter
&$uf)
457 if (count($this->emails
) == 0) {
458 return UserFilterCondition
::COND_TRUE
;
461 foreach ($this->emails
as $entry) {
462 if (User
::isForeignEmailAddress($entry)) {
463 if (is_null($email)) {
464 $email = $uf->addEmailRedirectFilter();
466 $cond[] = XDB
::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry);
467 } else if (User
::isVirtualEmailAddress($entry)) {
468 if (is_null($virtual)) {
469 $virtual = $uf->addVirtualEmailFilter();
471 $cond[] = XDB
::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry);
473 if (is_null($alias)) {
474 $alias = $uf->addAliasFilter();
476 @list
($user, $domain) = explode('@', $entry);
477 $cond[] = XDB
::format('al' . $alias . '.alias = {?}', $user);
480 return '(' . implode(') OR (', $cond) . ')';
484 /** Filters users based on their address
485 * @param $field Field of the address used for filtering (city, street, ...)
486 * @param $text Text for filter
487 * @param $mode Mode for search (PREFIX, SUFFIX, ...)
489 class UFC_Address
implements UserFilterCondition
499 public function __construct($field, $text, $mode)
501 $this->field
= $field;
506 public function buildCondition(UserFilter
&$uf)
508 $left = 'pa.' . $field;
510 if (($this->mode
& self
::CONTAINS
) == 0) {
511 $right = XDB
::format('{?}', $this->text
);
513 } else if (($this->mode
& self
::CONTAINS
) == self
::PREFIX
) {
514 $right = XDB
::format('CONCAT({?}, \'%\')', $this->text
);
515 } else if (($this->mode
& self
::CONTAINS
) == self
::SUFFIX
) {
516 $right = XDB
::format('CONCAT(\'%\', {?})', $this->text
);
518 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->text
);
520 $cond = $left . $op . $right;
521 $uf->addAddressFilter();
526 /** Filters users based on the corps they belong to
527 * @param $corps Corps we are looking for (abbreviation)
528 * @param $type Whether we search for original or current corps
530 class UFC_Corps
implements UserFilterCondition
538 public function __construct($corps, $type = self
::CURRENT
)
540 $this->corps
= $corps;
544 public function buildCondition(UserFilter
&$uf)
546 /** Tables shortcuts :
547 * pc for profile corps,
548 * pceo for profile_corps_enum - orginal
549 * pcec for profile_corps_enum - current
551 $sub = $uf->addCorpsFilter($this->type
);
552 $cond = $sub . '.abbreviation = ' . $corps;
557 /** Filters users based on their rank in the corps
558 * @param $rank Rank we are looking for (abbreviation)
560 class UFC_Corps_Rank
implements UserFilterCondition
563 public function __construct($rank)
568 public function buildCondition(UserFilter
&$uf)
570 /** Tables shortcuts :
571 * pcr for profile_corps_rank
573 $sub = $uf->addCorpsRankFilter();
574 $cond = $sub . '.abbreviation = ' . $rank;
579 /** Filters users based on a relation toward on user
580 * @param $user User to which searched users are related
582 abstract class UFC_UserRelated
implements UserFilterCondition
585 public function __construct(PlUser
&$user)
587 $this->user
=& $user;
591 /** Filters users who belongs to selected user's contacts
593 class UFC_Contact
extends UFC_UserRelated
595 public function buildCondition(UserFilter
&$uf)
597 $sub = $uf->addContactFilter($this->user
->id());
598 return 'c' . $sub . '.contact IS NOT NULL';
602 /** Filters users being watched by selected user
604 class UFC_WatchRegistration
extends UFC_UserRelated
606 public function buildCondition(UserFilter
&$uf)
608 if (!$this->user
->watch('registration')) {
609 return UserFilterCondition
::COND_FALSE
;
611 $uids = $this->user
->watchUsers();
612 if (count($uids) == 0) {
613 return UserFilterCondition
::COND_FALSE
;
615 return '$UID IN ' . XDB
::formatArray($uids);
620 /** Filters users belonging to a promo watched by selected user
621 * @param $user Selected user (the one watching promo)
622 * @param $grade Formation the user is watching
624 class UFC_WatchPromo
extends UFC_UserRelated
627 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
629 parent
::__construct($user);
630 $this->grade
= $grade;
633 public function buildCondition(UserFilter
&$uf)
635 $promos = $this->user
->watchPromos();
636 if (count($promos) == 0) {
637 return UserFilterCondition
::COND_FALSE
;
639 $sube = $uf->addEducationFilter(true
, $this->grade
);
640 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
641 return $field . ' IN ' . XDB
::formatArray($promos);
646 /** Filters users watched by selected user
648 class UFC_WatchContact
extends UFC_Contact
650 public function buildCondition(UserFilter
&$uf)
652 if (!$this->user
->watchContacts()) {
653 return UserFilterCondition
::COND_FALSE
;
655 return parent
::buildCondition($uf);
664 abstract class UserFilterOrder
666 protected $desc = false
;
667 public function __construct($desc = false
)
672 public function buildSort(UserFilter
&$uf)
674 $sel = $this->getSortTokens($uf);
675 if (!is_array($sel)) {
679 foreach ($sel as $k=>$s) {
680 $sel[$k] = $s . ' DESC';
686 abstract protected function getSortTokens(UserFilter
&$uf);
689 /** Orders users by promo
690 * @param $grade Formation whose promo users should be sorted by (restricts results to users of that formation)
691 * @param $desc Whether sort is descending
693 class UFO_Promo
extends UserFilterOrder
697 public function __construct($grade = null
, $desc = false
)
699 parent
::__construct($desc);
700 $this->grade
= $grade;
703 protected function getSortTokens(UserFilter
&$uf)
705 if (UserFilter
::isGrade($this->grade
)) {
706 $sub = $uf->addEducationFilter($this->grade
);
707 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
709 $sub = $uf->addDisplayFilter();
710 return 'pd' . $sub . '.promo';
715 /** Sorts users by name
716 * @param $type Type of name on which to sort (firstname, ...)
717 * @param $variant Variant of that name to user (marital, ordinary, ...)
718 * @param $particle Set to true if particles should be included in the sorting order
719 * @param $desc If sort order should be descending
721 class UFO_Name
extends UserFilterOrder
727 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
729 parent
::__construct($desc);
731 $this->variant
= $variant;
732 $this->particle
= $particle;
735 protected function getSortTokens(UserFilter
&$uf)
737 if (UserFilter
::isDisplayName($this->type
)) {
738 $sub = $uf->addDisplayFilter();
739 return 'pd' . $sub . '.' . $this->type
;
741 $sub = $uf->addNameFilter($this->type
, $this->variant
);
742 if ($this->particle
) {
743 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
745 return 'pn' . $sub . '.name';
751 /** Sorts users based on registration date
753 class UFO_Registration
extends UserFilterOrder
755 protected function getSortTokens(UserFilter
&$uf)
757 return 'a.registration_date';
761 /** Sorts users based on next birthday date
763 class UFO_Birthday
extends UserFilterOrder
765 protected function getSortTokens(UserFilter
&$uf)
767 return 'p.next_birthday';
771 /** Sorts users based on last profile update
773 class UFO_ProfileUpdate
extends UserFilterOrder
775 protected function getSortTokens(UserFilter
&$uf)
777 return 'p.last_change';
781 /** Sorts users based on death date
783 class UFO_Death
extends UserFilterOrder
785 protected function getSortTokens(UserFilter
&$uf)
787 return 'p.deathdate';
792 /***********************************
793 *********************************
795 *********************************
796 ***********************************/
800 static private $joinMethods = array();
803 private $sort = array();
804 private $query = null
;
805 private $orderby = null
;
807 private $lastcount = null
;
809 public function __construct($cond = null
, $sort = null
)
811 if (empty(self
::$joinMethods)) {
812 $class = new ReflectionClass('UserFilter');
813 foreach ($class->getMethods() as $method) {
814 $name = $method->getName();
815 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
816 self
::$joinMethods[] = $name;
820 if (!is_null($cond)) {
821 if ($cond instanceof UserFilterCondition
) {
822 $this->setCondition($cond);
825 if (!is_null($sort)) {
826 if ($sort instanceof UserFilterOrder
) {
827 $this->addSort($sort);
828 } else if (is_array($sort)) {
829 foreach ($sort as $s) {
836 private function buildQuery()
838 if (is_null($this->orderby
)) {
840 foreach ($this->sort
as $sort) {
841 $orders = array_merge($orders, $sort->buildSort($this));
843 if (count($orders) == 0) {
846 $this->orderby
= 'ORDER BY ' . implode(', ', $orders);
849 if (is_null($this->query
)) {
850 $where = $this->root
->buildCondition($this);
851 $joins = $this->buildJoins();
852 $this->query
= 'FROM accounts AS a
853 LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
854 LEFT JOIN profiles AS p ON (p.pid = ap.pid)
856 WHERE (' . $where . ')';
860 private function formatJoin(array $joins)
863 foreach ($joins as $key => $infos) {
866 if ($mode == 'inner') {
867 $str .= 'INNER JOIN ';
868 } else if ($mode == 'left') {
869 $str .= 'LEFT JOIN ';
871 Platal
::page()->kill("Join mode error");
873 $str .= $table . ' AS ' . $key;
874 if (isset($infos[2])) {
875 $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')';
882 private function buildJoins()
885 foreach (self
::$joinMethods as $method) {
886 $joins = array_merge($joins, $this->$method());
888 return $this->formatJoin($joins);
891 private function getUIDList($uids = null
, $count = null
, $offset = null
)
895 if (!is_null($count)) {
896 if (!is_null($offset)) {
897 $limit = XDB
::format('LIMIT {?}, {?}', (int)$offset, (int)$count);
899 $limit = XDB
::format('LIMIT {?}', (int)$count);
903 if (!is_null($uids)) {
904 $cond = ' AND a.uid IN ' . XDB
::formatArray($uids);
906 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
907 ' . $this->query
. $cond . '
909 ' . $this->orderby
. '
911 $this->lastcount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
915 /** Check that the user match the given rule.
917 public function checkUser(PlUser
&$user)
920 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
921 ' . $this->query
. XDB
::format(' AND a.uid = {?}', $user->id()));
925 /** Filter a list of user to extract the users matching the rule.
927 public function filter(array $users, $count = null
, $offset = null
)
932 foreach ($users as $user) {
933 if ($user instanceof PlUser
) {
939 $table[$uid] = $user;
941 $fetched = $this->getUIDList($uids, $count, $offset);
943 foreach ($fetched as $uid) {
944 $output[] = $table[$uid];
949 public function getUIDs($count = null
, $offset = null
)
951 return $this->getUIDList(null
, $count, $offset);
954 public function getUsers($count = null
, $offset = null
)
956 return User
::getBulkUsersWithUIDs($this->getUIDs($count, $offset));
959 public function getTotalCount()
961 if (is_null($this->lastcount
)) {
963 return (int)XDB
::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
966 return $this->lastcount
;
970 public function setCondition(UserFilterCondition
&$cond)
972 $this->root
=& $cond;
976 public function addSort(UserFilterOrder
&$sort)
978 $this->sort
[] = $sort;
979 $this->orderby
= null
;
982 static public function getLegacy($promo_min, $promo_max)
984 if ($promo_min != 0) {
985 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
987 $min = new UFC_True();
989 if ($promo_max != 0) {
990 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
992 $max = new UFC_True();
994 return new UserFilter(new UFC_And($min, $max));
997 static public function sortByName()
999 return array(new UFO_Name(self
::LASTNAME
), new UFO_Name(self
::FIRSTNAME
));
1002 static public function sortByPromo()
1004 return array(new UFO_Promo(), new UFO_Name(self
::LASTNAME
), new UFO_Name(self
::FIRSTNAME
));
1007 static private function getDBSuffix($string)
1009 return preg_replace('/[^a-z0-9]/i', '', $string);
1013 private $option = 0;
1014 private function register_optional(array &$table, $val)
1016 if (is_null($val)) {
1017 $sub = $this->option++
;
1020 $sub = self
::getDBSuffix($val);
1024 $table[$sub] = $index;
1030 const DISPLAY
= 'display';
1031 private $pd = false
;
1032 public function addDisplayFilter()
1038 private function displayJoins()
1041 return array('pd' => array('left', 'profile_display', '$ME.pid = $PID'));
1050 const LASTNAME
= 'lastname';
1051 const FIRSTNAME
= 'firstname';
1052 const NICKNAME
= 'nickname';
1053 const PSEUDONYM
= 'pseudonym';
1054 const NAME
= 'name';
1056 const VN_MARITAL
= 'marital';
1057 const VN_ORDINARY
= 'ordinary';
1058 const VN_OTHER
= 'other';
1059 const VN_INI
= 'ini';
1061 const DN_FULL
= 'directory_name';
1062 const DN_DISPLAY
= 'yourself';
1063 const DN_YOURSELF
= 'yourself';
1064 const DN_DIRECTORY
= 'directory_name';
1065 const DN_PRIVATE
= 'private_name';
1066 const DN_PUBLIC
= 'public_name';
1067 const DN_SHORT
= 'short_name';
1068 const DN_SORT
= 'sort_name';
1070 static public $name_variants = array(
1071 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
1072 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
1075 static public function assertName($name)
1077 if (!Profile
::getNameTypeId($name)) {
1078 Platal
::page()->kill('Invalid name type');
1082 static public function isDisplayName($name)
1084 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
1085 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
1086 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1087 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1090 private $pn = array();
1091 public function addNameFilter($type, $variant = null
)
1093 if (!is_null($variant)) {
1094 $ft = $type . '_' . $variant;
1099 self
::assertName($ft);
1101 if (!is_null($variant) && $variant == 'other') {
1102 $sub .= $this->option++
;
1104 $this->pn
[$sub] = Profile
::getNameTypeId($ft);
1108 private function nameJoins()
1111 foreach ($this->pn
as $sub => $type) {
1112 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
1120 const GRADE_ING
= 'Ing.';
1121 const GRADE_PHD
= 'PhD';
1122 const GRADE_MST
= 'M%';
1123 static public function isGrade($grade)
1125 return $grade == self
::GRADE_ING ||
$grade == self
::GRADE_PHD ||
$grade == self
::GRADE_MST
;
1128 static public function assertGrade($grade)
1130 if (!self
::isGrade($grade)) {
1131 Platal
::page()->killError("Diplôme non valide");
1135 static public function promoYear($grade)
1137 // XXX: Definition of promotion for phds and masters might change in near future.
1138 return ($grade == UserFilter
::GRADE_ING
) ?
'entry_year' : 'grad_year';
1141 private $pepe = array();
1142 private $with_pee = false
;
1143 public function addEducationFilter($x = false
, $grade = null
)
1146 $index = $this->option
;
1147 $sub = $this->option++
;
1149 self
::assertGrade($grade);
1152 $this->with_pee
= true
;
1155 $this->pepe
[$index] = $sub;
1159 private function educationJoins()
1162 if ($this->with_pee
) {
1163 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
1165 foreach ($this->pepe
as $grade => $sub) {
1166 if ($this->isGrade($grade)) {
1167 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
1168 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
1169 XDB
::format('{?}', $grade));
1171 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID');
1172 $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
1173 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
1182 private $gpm = array();
1183 public function addGroupFilter($group = null
)
1185 if (!is_null($group)) {
1186 if (ctype_digit($group)) {
1187 $index = $sub = $group;
1190 $sub = self
::getDBSuffix($group);
1193 $sub = 'group_' . $this->option++
;
1197 $this->gpm
[$sub] = $index;
1201 private function groupJoins()
1204 foreach ($this->gpm
as $sub => $key) {
1205 if (is_null($key)) {
1206 $joins['gpa' . $sub] = array('inner', 'groupex.asso');
1207 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1208 } else if (ctype_digit($key)) {
1209 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
1211 $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB
::format('$ME.diminutif = {?}', $key));
1212 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1220 private $e = array();
1221 public function addEmailRedirectFilter($email = null
)
1223 return $this->register_optional($this->e
, $email);
1226 private $ve = array();
1227 public function addVirtualEmailFilter($email = null
)
1229 $this->addAliasFilter(self
::ALIAS_FORLIFE
);
1230 return $this->register_optional($this->ve
, $email);
1233 const ALIAS_BEST
= 'bestalias';
1234 const ALIAS_FORLIFE
= 'forlife';
1235 private $al = array();
1236 public function addAliasFilter($alias = null
)
1238 return $this->register_optional($this->al
, $alias);
1241 private function emailJoins()
1245 foreach ($this->e
as $sub=>$key) {
1246 if (is_null($key)) {
1247 $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
1249 $joins['e' . $sub] = array('left', 'emails', XDB
::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
1252 foreach ($this->al
as $sub=>$key) {
1253 if (is_null($key)) {
1254 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
1255 } else if ($key == self
::ALIAS_BEST
) {
1256 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
1257 } else if ($key == self
::ALIAS_FORLIFE
) {
1258 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
1260 $joins['al' . $sub] = array('left', 'aliases', XDB
::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
1263 foreach ($this->ve
as $sub=>$key) {
1264 if (is_null($key)) {
1265 $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\'');
1267 $joins['v' . $sub] = array('left', 'virtual', XDB
::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
1269 $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB
::format('$ME.vid = v' . $sub . '.vid
1270 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
1271 CONCAT(al_forlife.alias, \'@\', {?}),
1273 $globals->mail
->domain
, $globals->mail
->domain2
));
1281 private $pa = false
;
1282 public function addAddressFilter()
1287 private function addressJoins()
1291 $joins['pa'] = array('left', 'profile_address', '$ME.PID = $PID');
1300 private $pc = false
;
1301 private $pce = array();
1302 private $pcr = false
;
1303 public function addCorpsFilter($type)
1306 if ($type == UFC_Corps
::CURRENT
) {
1307 $pce['pcec'] = 'current_corpsid';
1309 } else if ($type == UFC_Corps
::ORIGIN
) {
1310 $pce['pceo'] = 'original_corpsid';
1315 public function addCorpsRankFilter()
1322 private function corpsJoins()
1326 $joins['pc'] = array('left', 'profile_corps', '$ME.uid = $UID');
1329 $joins['pcr'] = array('left', 'profile_corps_rank_enum', '$ME.id = pc.rankid');
1331 foreach($this->pce
as $sub => $field) {
1332 $joins[$sub] = array('left', 'profile_corps_enum', '$ME.id = pc.' . $field);
1339 private $cts = array();
1340 public function addContactFilter($uid = null
)
1342 return $this->register_optional($this->cts
, is_null($uid) ? null
: 'user_' . $uid);
1345 private function contactJoins()
1348 foreach ($this->cts
as $sub=>$key) {
1349 if (is_null($key)) {
1350 $joins['c' . $sub] = array('left', 'contacts', '$ME.contact = $UID');
1352 $joins['c' . $sub] = array('left', 'contacts', XDB
::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
1361 private $wn = array();
1362 public function addWatchRegistrationFilter($uid = null
)
1364 return $this->register_optional($this->wn
, is_null($uid) ? null
: 'user_' . $uid);
1367 private $wp = array();
1368 public function addWatchPromoFilter($uid = null
)
1370 return $this->register_optional($this->wp
, is_null($uid) ? null
: 'user_' . $uid);
1373 private $w = array();
1374 public function addWatchFilter($uid = null
)
1376 return $this->register_optional($this->w
, is_null($uid) ? null
: 'user_' . $uid);
1379 private function watchJoins()
1382 foreach ($this->w
as $sub=>$key) {
1383 if (is_null($key)) {
1384 $joins['w' . $sub] = array('left', 'watch');
1386 $joins['w' . $sub] = array('left', 'watch', XDB
::format('$ME.uid = {?}', substr($key, 5)));
1389 foreach ($this->wn
as $sub=>$key) {
1390 if (is_null($key)) {
1391 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1393 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB
::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1396 foreach ($this->wn
as $sub=>$key) {
1397 if (is_null($key)) {
1398 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1400 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB
::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1403 foreach ($this->wp
as $sub=>$key) {
1404 if (is_null($key)) {
1405 $joins['wp' . $sub] = array('left', 'watch_promo');
1407 $joins['wp' . $sub] = array('left', 'watch_promo', XDB
::format('$ME.uid = {?}', substr($key, 5)));
1415 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: