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 a relation toward on user
485 * @param $user User to which searched users are related
487 abstract class UFC_UserRelated
implements UserFilterCondition
490 public function __construct(PlUser
&$user)
492 $this->user
=& $user;
496 /** Filters users who belongs to selected user's contacts
498 class UFC_Contact
extends UFC_UserRelated
500 public function buildCondition(UserFilter
&$uf)
502 $sub = $uf->addContactFilter($this->user
->id());
503 return 'c' . $sub . '.contact IS NOT NULL';
507 /** Filters users being watched by selected user
509 class UFC_WatchRegistration
extends UFC_UserRelated
511 public function buildCondition(UserFilter
&$uf)
513 if (!$this->user
->watch('registration')) {
514 return UserFilterCondition
::COND_FALSE
;
516 $uids = $this->user
->watchUsers();
517 if (count($uids) == 0) {
518 return UserFilterCondition
::COND_FALSE
;
520 return '$UID IN ' . XDB
::formatArray($uids);
525 /** Filters users belonging to a promo watched by selected user
526 * @param $user Selected user (the one watching promo)
527 * @param $grade Formation the user is watching
529 class UFC_WatchPromo
extends UFC_UserRelated
532 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
534 parent
::__construct($user);
535 $this->grade
= $grade;
538 public function buildCondition(UserFilter
&$uf)
540 $promos = $this->user
->watchPromos();
541 if (count($promos) == 0) {
542 return UserFilterCondition
::COND_FALSE
;
544 $sube = $uf->addEducationFilter(true
, $this->grade
);
545 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
546 return $field . ' IN ' . XDB
::formatArray($promos);
551 /** Filters users watched by selected user
553 class UFC_WatchContact
extends UFC_Contact
555 public function buildCondition(UserFilter
&$uf)
557 if (!$this->user
->watchContacts()) {
558 return UserFilterCondition
::COND_FALSE
;
560 return parent
::buildCondition($uf);
569 abstract class UserFilterOrder
571 protected $desc = false
;
572 public function __construct($desc = false
)
577 public function buildSort(UserFilter
&$uf)
579 $sel = $this->getSortTokens($uf);
580 if (!is_array($sel)) {
584 foreach ($sel as $k=>$s) {
585 $sel[$k] = $s . ' DESC';
591 abstract protected function getSortTokens(UserFilter
&$uf);
594 /** Orders users by promo
595 * @param $grade Formation whose promo users should be sorted by (restricts results to users of that formation)
596 * @param $desc Whether sort is descending
598 class UFO_Promo
extends UserFilterOrder
602 public function __construct($grade = null
, $desc = false
)
604 parent
::__construct($desc);
605 $this->grade
= $grade;
608 protected function getSortTokens(UserFilter
&$uf)
610 if (UserFilter
::isGrade($this->grade
)) {
611 $sub = $uf->addEducationFilter($this->grade
);
612 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
614 $sub = $uf->addDisplayFilter();
615 return 'pd' . $sub . '.promo';
620 /** Sorts users by name
621 * @param $type Type of name on which to sort (firstname, ...)
622 * @param $variant Variant of that name to user (marital, ordinary, ...)
623 * @param $particle Set to true if particles should be included in the sorting order
624 * @param $desc If sort order should be descending
626 class UFO_Name
extends UserFilterOrder
632 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
634 parent
::__construct($desc);
636 $this->variant
= $variant;
637 $this->particle
= $particle;
640 protected function getSortTokens(UserFilter
&$uf)
642 if (UserFilter
::isDisplayName($this->type
)) {
643 $sub = $uf->addDisplayFilter();
644 return 'pd' . $sub . '.' . $this->type
;
646 $sub = $uf->addNameFilter($this->type
, $this->variant
);
647 if ($this->particle
) {
648 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
650 return 'pn' . $sub . '.name';
656 /** Sorts users based on registration date
658 class UFO_Registration
extends UserFilterOrder
660 protected function getSortTokens(UserFilter
&$uf)
662 return 'a.registration_date';
666 /** Sorts users based on next birthday date
668 class UFO_Birthday
extends UserFilterOrder
670 protected function getSortTokens(UserFilter
&$uf)
672 return 'p.next_birthday';
676 /** Sorts users based on last profile update
678 class UFO_ProfileUpdate
extends UserFilterOrder
680 protected function getSortTokens(UserFilter
&$uf)
682 return 'p.last_change';
686 /** Sorts users based on death date
688 class UFO_Death
extends UserFilterOrder
690 protected function getSortTokens(UserFilter
&$uf)
692 return 'p.deathdate';
697 /***********************************
698 *********************************
700 *********************************
701 ***********************************/
705 static private $joinMethods = array();
708 private $sort = array();
709 private $query = null
;
710 private $orderby = null
;
712 private $lastcount = null
;
714 public function __construct($cond = null
, $sort = null
)
716 if (empty(self
::$joinMethods)) {
717 $class = new ReflectionClass('UserFilter');
718 foreach ($class->getMethods() as $method) {
719 $name = $method->getName();
720 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
721 self
::$joinMethods[] = $name;
725 if (!is_null($cond)) {
726 if ($cond instanceof UserFilterCondition
) {
727 $this->setCondition($cond);
730 if (!is_null($sort)) {
731 if ($sort instanceof UserFilterOrder
) {
732 $this->addSort($sort);
733 } else if (is_array($sort)) {
734 foreach ($sort as $s) {
741 private function buildQuery()
743 if (is_null($this->orderby
)) {
745 foreach ($this->sort
as $sort) {
746 $orders = array_merge($orders, $sort->buildSort($this));
748 if (count($orders) == 0) {
751 $this->orderby
= 'ORDER BY ' . implode(', ', $orders);
754 if (is_null($this->query
)) {
755 $where = $this->root
->buildCondition($this);
756 $joins = $this->buildJoins();
757 $this->query
= 'FROM accounts AS a
758 LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
759 LEFT JOIN profiles AS p ON (p.pid = ap.pid)
761 WHERE (' . $where . ')';
765 private function formatJoin(array $joins)
768 foreach ($joins as $key => $infos) {
771 if ($mode == 'inner') {
772 $str .= 'INNER JOIN ';
773 } else if ($mode == 'left') {
774 $str .= 'LEFT JOIN ';
776 Platal
::page()->kill("Join mode error");
778 $str .= $table . ' AS ' . $key;
779 if (isset($infos[2])) {
780 $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')';
787 private function buildJoins()
790 foreach (self
::$joinMethods as $method) {
791 $joins = array_merge($joins, $this->$method());
793 return $this->formatJoin($joins);
796 private function getUIDList($uids = null
, $count = null
, $offset = null
)
800 if (!is_null($count)) {
801 if (!is_null($offset)) {
802 $limit = XDB
::format('LIMIT {?}, {?}', (int)$offset, (int)$count);
804 $limit = XDB
::format('LIMIT {?}', (int)$count);
808 if (!is_null($uids)) {
809 $cond = ' AND a.uid IN ' . XDB
::formatArray($uids);
811 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
812 ' . $this->query
. $cond . '
814 ' . $this->orderby
. '
816 $this->lastcount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
820 /** Check that the user match the given rule.
822 public function checkUser(PlUser
&$user)
825 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
826 ' . $this->query
. XDB
::format(' AND a.uid = {?}', $user->id()));
830 /** Filter a list of user to extract the users matching the rule.
832 public function filter(array $users, $count = null
, $offset = null
)
837 foreach ($users as $user) {
838 if ($user instanceof PlUser
) {
844 $table[$uid] = $user;
846 $fetched = $this->getUIDList($uids, $count, $offset);
848 foreach ($fetched as $uid) {
849 $output[] = $table[$uid];
854 public function getUIDs($count = null
, $offset = null
)
856 return $this->getUIDList(null
, $count, $offset);
859 public function getUsers($count = null
, $offset = null
)
861 return User
::getBulkUsersWithUIDs($this->getUIDs($count, $offset));
864 public function getTotalCount()
866 if (is_null($this->lastcount
)) {
868 return (int)XDB
::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
871 return $this->lastcount
;
875 public function setCondition(UserFilterCondition
&$cond)
877 $this->root
=& $cond;
881 public function addSort(UserFilterOrder
&$sort)
883 $this->sort
[] = $sort;
884 $this->orderby
= null
;
887 static public function getLegacy($promo_min, $promo_max)
889 if ($promo_min != 0) {
890 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
892 $min = new UFC_True();
894 if ($promo_max != 0) {
895 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
897 $max = new UFC_True();
899 return new UserFilter(new UFC_And($min, $max));
902 static public function sortByName()
904 return array(new UFO_Name(self
::LASTNAME
), new UFO_Name(self
::FIRSTNAME
));
907 static public function sortByPromo()
909 return array(new UFO_Promo(), new UFO_Name(self
::LASTNAME
), new UFO_Name(self
::FIRSTNAME
));
912 static private function getDBSuffix($string)
914 return preg_replace('/[^a-z0-9]/i', '', $string);
919 private function register_optional(array &$table, $val)
922 $sub = $this->option++
;
925 $sub = self
::getDBSuffix($val);
929 $table[$sub] = $index;
935 const DISPLAY
= 'display';
937 public function addDisplayFilter()
943 private function displayJoins()
946 return array('pd' => array('left', 'profile_display', '$ME.pid = $PID'));
955 const LASTNAME
= 'lastname';
956 const FIRSTNAME
= 'firstname';
957 const NICKNAME
= 'nickname';
958 const PSEUDONYM
= 'pseudonym';
961 const VN_MARITAL
= 'marital';
962 const VN_ORDINARY
= 'ordinary';
963 const VN_OTHER
= 'other';
964 const VN_INI
= 'ini';
966 const DN_FULL
= 'directory_name';
967 const DN_DISPLAY
= 'yourself';
968 const DN_YOURSELF
= 'yourself';
969 const DN_DIRECTORY
= 'directory_name';
970 const DN_PRIVATE
= 'private_name';
971 const DN_PUBLIC
= 'public_name';
972 const DN_SHORT
= 'short_name';
973 const DN_SORT
= 'sort_name';
975 static public $name_variants = array(
976 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
977 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
980 static public function assertName($name)
982 if (!Profile
::getNameTypeId($name)) {
983 Platal
::page()->kill('Invalid name type');
987 static public function isDisplayName($name)
989 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
990 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
991 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
992 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
995 private $pn = array();
996 public function addNameFilter($type, $variant = null
)
998 if (!is_null($variant)) {
999 $ft = $type . '_' . $variant;
1004 self
::assertName($ft);
1006 if (!is_null($variant) && $variant == 'other') {
1007 $sub .= $this->option++
;
1009 $this->pn
[$sub] = Profile
::getNameTypeId($ft);
1013 private function nameJoins()
1016 foreach ($this->pn
as $sub => $type) {
1017 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
1025 const GRADE_ING
= 'Ing.';
1026 const GRADE_PHD
= 'PhD';
1027 const GRADE_MST
= 'M%';
1028 static public function isGrade($grade)
1030 return $grade == self
::GRADE_ING ||
$grade == self
::GRADE_PHD ||
$grade == self
::GRADE_MST
;
1033 static public function assertGrade($grade)
1035 if (!self
::isGrade($grade)) {
1036 Platal
::page()->killError("Diplôme non valide");
1040 static public function promoYear($grade)
1042 // XXX: Definition of promotion for phds and masters might change in near future.
1043 return ($grade == UserFilter
::GRADE_ING
) ?
'entry_year' : 'grad_year';
1046 private $pepe = array();
1047 private $with_pee = false
;
1048 public function addEducationFilter($x = false
, $grade = null
)
1051 $index = $this->option
;
1052 $sub = $this->option++
;
1054 self
::assertGrade($grade);
1057 $this->with_pee
= true
;
1060 $this->pepe
[$index] = $sub;
1064 private function educationJoins()
1067 if ($this->with_pee
) {
1068 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
1070 foreach ($this->pepe
as $grade => $sub) {
1071 if ($this->isGrade($grade)) {
1072 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
1073 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
1074 XDB
::format('{?}', $grade));
1076 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID');
1077 $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
1078 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
1087 private $gpm = array();
1088 public function addGroupFilter($group = null
)
1090 if (!is_null($group)) {
1091 if (ctype_digit($group)) {
1092 $index = $sub = $group;
1095 $sub = self
::getDBSuffix($group);
1098 $sub = 'group_' . $this->option++
;
1102 $this->gpm
[$sub] = $index;
1106 private function groupJoins()
1109 foreach ($this->gpm
as $sub => $key) {
1110 if (is_null($key)) {
1111 $joins['gpa' . $sub] = array('inner', 'groupex.asso');
1112 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1113 } else if (ctype_digit($key)) {
1114 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
1116 $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB
::format('$ME.diminutif = {?}', $key));
1117 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1125 private $e = array();
1126 public function addEmailRedirectFilter($email = null
)
1128 return $this->register_optional($this->e
, $email);
1131 private $ve = array();
1132 public function addVirtualEmailFilter($email = null
)
1134 $this->addAliasFilter(self
::ALIAS_FORLIFE
);
1135 return $this->register_optional($this->ve
, $email);
1138 const ALIAS_BEST
= 'bestalias';
1139 const ALIAS_FORLIFE
= 'forlife';
1140 private $al = array();
1141 public function addAliasFilter($alias = null
)
1143 return $this->register_optional($this->al
, $alias);
1146 private function emailJoins()
1150 foreach ($this->e
as $sub=>$key) {
1151 if (is_null($key)) {
1152 $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
1154 $joins['e' . $sub] = array('left', 'emails', XDB
::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
1157 foreach ($this->al
as $sub=>$key) {
1158 if (is_null($key)) {
1159 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
1160 } else if ($key == self
::ALIAS_BEST
) {
1161 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
1162 } else if ($key == self
::ALIAS_FORLIFE
) {
1163 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
1165 $joins['al' . $sub] = array('left', 'aliases', XDB
::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
1168 foreach ($this->ve
as $sub=>$key) {
1169 if (is_null($key)) {
1170 $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\'');
1172 $joins['v' . $sub] = array('left', 'virtual', XDB
::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
1174 $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB
::format('$ME.vid = v' . $sub . '.vid
1175 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
1176 CONCAT(al_forlife.alias, \'@\', {?}),
1178 $globals->mail
->domain
, $globals->mail
->domain2
));
1186 private $cts = array();
1187 public function addContactFilter($uid = null
)
1189 return $this->register_optional($this->cts
, is_null($uid) ? null
: 'user_' . $uid);
1192 private function contactJoins()
1195 foreach ($this->cts
as $sub=>$key) {
1196 if (is_null($key)) {
1197 $joins['c' . $sub] = array('left', 'contacts', '$ME.contact = $UID');
1199 $joins['c' . $sub] = array('left', 'contacts', XDB
::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
1208 private $wn = array();
1209 public function addWatchRegistrationFilter($uid = null
)
1211 return $this->register_optional($this->wn
, is_null($uid) ? null
: 'user_' . $uid);
1214 private $wp = array();
1215 public function addWatchPromoFilter($uid = null
)
1217 return $this->register_optional($this->wp
, is_null($uid) ? null
: 'user_' . $uid);
1220 private $w = array();
1221 public function addWatchFilter($uid = null
)
1223 return $this->register_optional($this->w
, is_null($uid) ? null
: 'user_' . $uid);
1226 private function watchJoins()
1229 foreach ($this->w
as $sub=>$key) {
1230 if (is_null($key)) {
1231 $joins['w' . $sub] = array('left', 'watch');
1233 $joins['w' . $sub] = array('left', 'watch', XDB
::format('$ME.uid = {?}', substr($key, 5)));
1236 foreach ($this->wn
as $sub=>$key) {
1237 if (is_null($key)) {
1238 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1240 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB
::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1243 foreach ($this->wn
as $sub=>$key) {
1244 if (is_null($key)) {
1245 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1247 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB
::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1250 foreach ($this->wp
as $sub=>$key) {
1251 if (is_null($key)) {
1252 $joins['wp' . $sub] = array('left', 'watch_promo');
1254 $joins['wp' . $sub] = array('left', 'watch_promo', XDB
::format('$ME.uid = {?}', substr($key, 5)));
1262 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: