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
28 interface UserFilterCondition
30 const COND_TRUE
= 'TRUE';
31 const COND_FALSE
= 'FALSE';
33 /** Check that the given user matches the rule.
35 public function buildCondition(UserFilter
&$uf);
39 // {{{ class UFC_OneChild
40 abstract class UFC_OneChild
implements UserFilterCondition
44 public function __construct($child = null
)
46 if (!is_null($child) && ($child instanceof UserFilterCondition
)) {
47 $this->setChild($child);
51 public function setChild(UserFilterCondition
&$cond)
53 $this->child
=& $cond;
58 // {{{ class UFC_NChildren
59 abstract class UFC_NChildren
implements UserFilterCondition
61 protected $children = array();
63 public function __construct()
65 $children = func_get_args();
66 foreach ($children as &$child) {
67 if (!is_null($child) && ($child instanceof UserFilterCondition
)) {
68 $this->addChild($child);
73 public function addChild(UserFilterCondition
&$cond)
75 $this->children
[] =& $cond;
78 protected function catConds(array $cond, $op, $fallback)
80 if (count($cond) == 0) {
82 } else if (count($cond) == 1) {
85 return '(' . implode(') ' . $op . ' (', $cond) . ')';
92 class UFC_True
implements UserFilterCondition
94 public function buildCondition(UserFilter
&$uf)
96 return self
::COND_TRUE
;
101 // {{{ class UFC_False
102 class UFC_False
implements UserFilterCondition
104 public function buildCondition(UserFilter
&$uf)
106 return self
::COND_FALSE
;
112 class UFC_Not
extends UFC_OneChild
114 public function buildCondition(UserFilter
&$uf)
116 $val = $this->child
->buildCondition($uf);
117 if ($val == self
::COND_TRUE
) {
118 return self
::COND_FALSE
;
119 } else if ($val == self
::COND_FALSE
) {
120 return self
::COND_TRUE
;
122 return 'NOT (' . $val . ')';
129 class UFC_And
extends UFC_NChildren
131 public function buildCondition(UserFilter
&$uf)
133 if (empty($this->children
)) {
134 return self
::COND_FALSE
;
136 $true = self
::COND_FALSE
;
138 foreach ($this->children
as &$child) {
139 $val = $child->buildCondition($uf);
140 if ($val == self
::COND_TRUE
) {
141 $true = self
::COND_TRUE
;
142 } else if ($val == self
::COND_FALSE
) {
143 return self
::COND_FALSE
;
148 return $this->catConds($conds, 'AND', $true);
155 class UFC_Or
extends UFC_NChildren
157 public function buildCondition(UserFilter
&$uf)
159 if (empty($this->children
)) {
160 return self
::COND_TRUE
;
162 $true = self
::COND_TRUE
;
164 foreach ($this->children
as &$child) {
165 $val = $child->buildCondition($uf);
166 if ($val == self
::COND_TRUE
) {
167 return self
::COND_TRUE
;
168 } else if ($val == self
::COND_FALSE
) {
169 $true = self
::COND_FALSE
;
174 return $this->catConds($conds, 'OR', $true);
180 // {{{ class UFC_Profile
181 /** Filters users who have a profile
183 class UFC_Profile
implements UserFilterCondition
185 public function buildCondition(UserFilter
&$uf)
187 return '$PID IS NOT NULL';
192 // {{{ class UFC_Promo
193 /** Filters users based on promo
194 * @param $comparison Comparison operator (>, =, ...)
195 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
196 * @param $promo Promo on which the filter is based
198 class UFC_Promo
implements UserFilterCondition
205 public function __construct($comparison, $grade, $promo)
207 $this->grade
= $grade;
208 $this->comparison
= $comparison;
209 $this->promo
= $promo;
210 if ($this->grade
!= UserFilter
::DISPLAY
) {
211 UserFilter
::assertGrade($this->grade
);
215 public function buildCondition(UserFilter
&$uf)
217 if ($this->grade
== UserFilter
::DISPLAY
) {
218 $sub = $uf->addDisplayFilter();
219 return XDB
::format('pd' . $sub . '.promo = {?}', $this->promo
);
221 $sub = $uf->addEducationFilter(true
, $this->grade
);
222 $field = 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
223 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->promo
);
229 // {{{ class UFC_Name
230 /** Filters users based on name
231 * @param $type Type of name field on which filtering is done (firstname, lastname, ...)
232 * @param $text Text on which to filter
233 * @param $mode flag indicating search type (prefix, suffix, with particule, ...)
235 class UFC_Name
implements UserFilterCondition
247 public function __construct($type, $text, $mode)
254 private function buildNameQuery($type, $variant, $where, UserFilter
&$uf)
256 $sub = $uf->addNameFilter($type, $variant);
257 return str_replace('$ME', 'pn' . $sub, $where);
260 public function buildCondition(UserFilter
&$uf)
264 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
265 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
267 if (($this->mode
& self
::CONTAINS
) == 0) {
268 $right = XDB
::format('{?}', $this->text
);
270 } else if (($this->mode
& self
::CONTAINS
) == self
::PREFIX
) {
271 $right = XDB
::format('CONCAT({?}, \'%\')', $this->text
);
272 } else if (($this->mode
& self
::CONTAINS
) == self
::SUFFIX
) {
273 $right = XDB
::format('CONCAT(\'%\', {?})', $this->text
);
275 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->text
);
277 $cond = $left . $op . $right;
278 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
279 if (($this->mode
& self
::VARIANTS
) != 0 && isset(UserFilter
::$name_variants[$this->type
])) {
280 foreach (UserFilter
::$name_variants[$this->type
] as $var) {
281 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
284 return implode(' OR ', $conds);
289 // {{{ class UFC_Dead
290 /** Filters users based on death date
291 * @param $comparison Comparison operator
292 * @param $date Date to which death date should be compared
294 class UFC_Dead
implements UserFilterCondition
299 public function __construct($comparison = null
, $date = null
)
301 $this->comparison
= $comparison;
305 public function buildCondition(UserFilter
&$uf)
307 $str = 'p.deathdate IS NOT NULL';
308 if (!is_null($this->comparison
)) {
309 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
316 // {{{ class UFC_Registered
317 /** Filters users based on registration state
318 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
319 * @param $comparison Comparison operator
320 * @param $date Date to which users registration date should be compared
322 class UFC_Registered
implements UserFilterCondition
328 public function __construct($active = false
, $comparison = null
, $date = null
)
330 $this->only_active
= $active;
331 $this->comparison
= $comparison;
335 public function buildCondition(UserFilter
&$uf)
338 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
340 $date = 'a.uid IS NOT NULL AND a.state != \'pending\'';
342 if (!is_null($this->comparison
)) {
343 $date .= ' AND a.registration_date ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
350 // {{{ class UFC_ProfileUpdated
351 /** Filters users based on profile update date
352 * @param $comparison Comparison operator
353 * @param $date Date to which profile update date must be compared
355 class UFC_ProfileUpdated
implements UserFilterCondition
360 public function __construct($comparison = null
, $date = null
)
362 $this->comparison
= $comparison;
366 public function buildCondition(UserFilter
&$uf)
368 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
373 // {{{ class UFC_Birthday
374 /** Filters users based on next birthday date
375 * @param $comparison Comparison operator
376 * @param $date Date to which users next birthday date should be compared
378 class UFC_Birthday
implements UserFilterCondition
383 public function __construct($comparison = null
, $date = null
)
385 $this->comparison
= $comparison;
389 public function buildCondition(UserFilter
&$uf)
391 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
397 /** Filters users based on sex
398 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
400 class UFC_Sex
implements UserFilterCondition
403 public function __construct($sex)
408 public function buildCondition(UserFilter
&$uf)
410 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
411 return self
::COND_FALSE
;
413 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
419 // {{{ class UFC_Group
420 /** Filters users based on group membership
421 * @param $group Group whose member we are selecting
422 * @param $admin Whether to restrict selection to admins of that group
424 class UFC_Group
implements UserFilterCondition
428 public function __construct($group, $admin = false
)
430 $this->group
= $group;
431 $this->admin
= $admin;
434 public function buildCondition(UserFilter
&$uf)
436 $sub = $uf->addGroupFilter($this->group
);
437 $where = 'gpm' . $sub . '.perms IS NOT NULL';
439 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
446 // {{{ class UFC_Email
447 /** Filters users based on email address
448 * @param $email Email whose owner we are looking for
450 class UFC_Email
implements UserFilterCondition
453 public function __construct($email)
455 $this->email
= $email;
458 public function buildCondition(UserFilter
&$uf)
460 if (User
::isForeignEmailAddress($this->email
)) {
461 $sub = $uf->addEmailRedirectFilter($this->email
);
462 return XDB
::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email
);
463 } else if (User
::isVirtualEmailAddress($this->email
)) {
464 $sub = $uf->addVirtualEmailFilter($this->email
);
465 return 'vr' . $sub . '.redirect IS NOT NULL';
467 @list
($user, $domain) = explode('@', $this->email
);
468 $sub = $uf->addAliasFilter($user);
469 return 'al' . $sub . '.alias IS NOT NULL';
475 // {{{ class UFC_EmailList
476 /** Filters users base on an email list
477 * @param $emails List of emails whose owner must be selected
479 class UFC_EmailList
implements UserFilterCondition
482 public function __construct($emails)
484 $this->emails
= $emails;
487 public function buildCondition(UserFilter
&$uf)
494 if (count($this->emails
) == 0) {
495 return UserFilterCondition
::COND_TRUE
;
498 foreach ($this->emails
as $entry) {
499 if (User
::isForeignEmailAddress($entry)) {
500 if (is_null($email)) {
501 $email = $uf->addEmailRedirectFilter();
503 $cond[] = XDB
::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry);
504 } else if (User
::isVirtualEmailAddress($entry)) {
505 if (is_null($virtual)) {
506 $virtual = $uf->addVirtualEmailFilter();
508 $cond[] = XDB
::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry);
510 if (is_null($alias)) {
511 $alias = $uf->addAliasFilter();
513 @list
($user, $domain) = explode('@', $entry);
514 $cond[] = XDB
::format('al' . $alias . '.alias = {?}', $user);
517 return '(' . implode(') OR (', $cond) . ')';
522 // {{{ class UFC_Address
523 /** Filters users based on their address
524 * @param $field Field of the address used for filtering (city, street, ...)
525 * @param $text Text for filter
526 * @param $mode Mode for search (PREFIX, SUFFIX, ...)
528 class UFC_Address
implements UserFilterCondition
538 public function __construct($field, $text, $mode)
540 $this->field
= $field;
545 public function buildCondition(UserFilter
&$uf)
547 $left = 'pa.' . $field;
549 if (($this->mode
& self
::CONTAINS
) == 0) {
550 $right = XDB
::format('{?}', $this->text
);
552 } else if (($this->mode
& self
::CONTAINS
) == self
::PREFIX
) {
553 $right = XDB
::format('CONCAT({?}, \'%\')', $this->text
);
554 } else if (($this->mode
& self
::CONTAINS
) == self
::SUFFIX
) {
555 $right = XDB
::format('CONCAT(\'%\', {?})', $this->text
);
557 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->text
);
559 $cond = $left . $op . $right;
560 $uf->addAddressFilter();
566 // {{{ class UFC_Corps
567 /** Filters users based on the corps they belong to
568 * @param $corps Corps we are looking for (abbreviation)
569 * @param $type Whether we search for original or current corps
571 class UFC_Corps
implements UserFilterCondition
579 public function __construct($corps, $type = self
::CURRENT
)
581 $this->corps
= $corps;
585 public function buildCondition(UserFilter
&$uf)
587 /** Tables shortcuts :
588 * pc for profile corps,
589 * pceo for profile_corps_enum - orginal
590 * pcec for profile_corps_enum - current
592 $sub = $uf->addCorpsFilter($this->type
);
593 $cond = $sub . '.abbreviation = ' . $corps;
599 // {{{ class UFC_Corps_Rank
600 /** Filters users based on their rank in the corps
601 * @param $rank Rank we are looking for (abbreviation)
603 class UFC_Corps_Rank
implements UserFilterCondition
606 public function __construct($rank)
611 public function buildCondition(UserFilter
&$uf)
613 /** Tables shortcuts :
614 * pcr for profile_corps_rank
616 $sub = $uf->addCorpsRankFilter();
617 $cond = $sub . '.abbreviation = ' . $rank;
623 // {{{ class UFC_Job_Company
624 /** Filters users based on the company they belong to
625 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
626 * @param $value The searched value
628 class UFC_Job_Company
extends UserFilterCondition
631 const JOBNAME
= 'name';
632 const JOBACRONYM
= 'acronym';
637 public function __construct($type, $value)
639 $this->assertType($type);
641 $this->value
= $value;
644 private function assertType($type)
646 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
647 Platal
::page()->killError("Type de recherche non valide");
651 public function buildCondition(UserFilter
&$uf)
653 $sub = $uf->addJobCompanyFilter();
654 $cond = $sub . '.' . $this->type
. ' = ' . XDB
::format('{?}', $this->value
);
660 // {{{ class UFC_Job_Sectorization
661 /** Filters users based on the ((sub)sub)sector they work in
662 * @param $sector The sector searched
663 * @param $subsector The subsector
664 * @param $subsubsector The subsubsector
666 class UFC_Job_Sectorization
extends UserFilterCondition
671 private $subsubsector;
673 public function __construct($sector = null
, $subsector = null
, $subsubsector = null
)
675 $this->sector
= $sector;
676 $this->subsector
= $subsector;
677 $this->subsubsector
= $subsubsector;
680 public function buildCondition(UserFilter
&$uf)
682 // No need to add the JobFilter, it will be done by addJobSectorizationFilter
684 if ($this->sector
!== null
) {
685 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SECTOR
);
686 $conds[] = $sub . '.id = ' . XDB
::format('{?}', $this->sector
);
688 if ($this->subsector
!== null
) {
689 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSECTOR
);
690 $conds[] = $sub . '.id = ' . XDB
::format('{?}', $this->subsector
);
692 if ($this->subsubsector
!== null
) {
693 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSUBSECTOR
);
694 $conds[] = $sub . '.id = ' . XDB
::format('{?}', $this->subsubsector
);
696 return implode(' AND ', $conds);
701 // {{{ class UFC_Job_Description
702 /** Filters users based on their job description
703 * @param $description The text being searched for
704 * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
706 class UFC_Job_Description
extends UserFilterCondition
710 * Built with binary OR on UserFilter::JOB_*
713 const SECTORIZATION
= 15;
715 private $description;
718 public function __construct($description)
720 $this->fields
= $fields;
721 $this->description
= $description;
724 public function buildCondition(UserFilter
&$uf)
727 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
728 $sub = $uf->addJobFilter();
729 $conds[] = $sub . '.description LIKE ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->description
);
731 if ($this->fields
& UserFilter
::JOB_SECTOR
) {
732 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SECTOR
);
733 $conds[] = $sub . '.name LIKE ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->description
);
735 if ($this->fields
& UserFilter
::JOB_SUBSECTOR
) {
736 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSECTOR
);
737 $conds[] = $sub . '.name LIKE ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->description
);
739 if ($this->fields
& UserFilter
::JOB_SUBSUBSECTOR
) {
740 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSUBSECTOR
);
741 $conds[] = $sub . '.name LIKE ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->description
);
742 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_ALTERNATES
);
743 $conds[] = $sub . '.name LIKE ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->description
);
745 return implode(' OR ', $conds);
750 // {{{ class UFC_Networking
751 /** Filters users based on network identity (IRC, ...)
752 * @param $type Type of network (-1 for any)
753 * @param $value Value to search
755 class UFC_Networking
extends UserFilterCondition
760 public function __construct($type, $value)
763 $this->value
= $value;
766 public function buildCondition(UserFilter
&$uf)
768 $sub = $uf->addNetworkingFilter();
770 $conds[] = $sub . '.address = ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->value
);
771 if ($this->type
!= -1) {
772 $conds[] = $sub . '.network_type = ' . XDB
::format('{?}', $this->type
);
774 return implode(' AND ', $conds);
779 // {{{ class UFC_Phone
780 /** Filters users based on their phone number
781 * @param $num_type Type of number (pro/user/home)
782 * @param $phone_type Type of phone (fixed/mobile/fax)
783 * @param $number Phone number
785 class UFC_Phone
extends UserFilterCondition
787 const NUM_PRO
= 'pro';
788 const NUM_USER
= 'user';
789 const NUM_HOME
= 'address';
790 const NUM_ANY
= 'any';
792 const PHONE_FIXED
= 'fixed';
793 const PHONE_MOBILE
= 'mobile';
794 const PHONE_FAX
= 'fax';
795 const PHONE_ANY
= 'any';
801 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
803 require_once('profil.inc.php');
804 $this->number
= $number;
805 $this->num_type
= $num_type;
806 $this->phone_type
= format_phone_number($phone_type);
809 public function buildCondition(UserFilter
&$uf)
811 $sub = $uf->addPhoneFilter();
813 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
814 if ($this->num_type
!= self
::NUM_ANY
) {
815 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
817 if ($this->phone_type
!= self
::PHONE_ANY
) {
818 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
820 return implode(' AND ', $conds);
825 // {{{ class UFC_Medal
826 /** Filters users based on their medals
827 * @param $medal ID of the medal
828 * @param $grade Grade of the medal (null for 'any')
830 class UFC_Medal
extends UserFilterCondition
835 public function __construct($medal, $grade = null
)
837 $this->medal
= $medal;
838 $this->grade
= $grade;
841 public function buildCondition(UserFilter
&$uf)
844 $sub = $uf->addMedalFilter();
845 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
846 if ($this->grade
!= null
) {
847 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
849 return implode(' AND ', $conds);
854 // {{{ class UFC_UserRelated
855 /** Filters users based on a relation toward on user
856 * @param $user User to which searched users are related
858 abstract class UFC_UserRelated
implements UserFilterCondition
861 public function __construct(PlUser
&$user)
863 $this->user
=& $user;
868 // {{{ class UFC_Contact
869 /** Filters users who belongs to selected user's contacts
871 class UFC_Contact
extends UFC_UserRelated
873 public function buildCondition(UserFilter
&$uf)
875 $sub = $uf->addContactFilter($this->user
->id());
876 return 'c' . $sub . '.contact IS NOT NULL';
881 // {{{ class UFC_WatchRegistration
882 /** Filters users being watched by selected user
884 class UFC_WatchRegistration
extends UFC_UserRelated
886 public function buildCondition(UserFilter
&$uf)
888 if (!$this->user
->watch('registration')) {
889 return UserFilterCondition
::COND_FALSE
;
891 $uids = $this->user
->watchUsers();
892 if (count($uids) == 0) {
893 return UserFilterCondition
::COND_FALSE
;
895 return '$UID IN ' . XDB
::formatArray($uids);
901 // {{{ class UFC_WatchPromo
902 /** Filters users belonging to a promo watched by selected user
903 * @param $user Selected user (the one watching promo)
904 * @param $grade Formation the user is watching
906 class UFC_WatchPromo
extends UFC_UserRelated
909 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
911 parent
::__construct($user);
912 $this->grade
= $grade;
915 public function buildCondition(UserFilter
&$uf)
917 $promos = $this->user
->watchPromos();
918 if (count($promos) == 0) {
919 return UserFilterCondition
::COND_FALSE
;
921 $sube = $uf->addEducationFilter(true
, $this->grade
);
922 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
923 return $field . ' IN ' . XDB
::formatArray($promos);
929 // {{{ class UFC_WatchContact
930 /** Filters users watched by selected user
932 class UFC_WatchContact
extends UFC_Contact
934 public function buildCondition(UserFilter
&$uf)
936 if (!$this->user
->watchContacts()) {
937 return UserFilterCondition
::COND_FALSE
;
939 return parent
::buildCondition($uf);
949 // {{{ class UserFilterOrder
950 abstract class UserFilterOrder
952 protected $desc = false
;
953 public function __construct($desc = false
)
958 public function buildSort(UserFilter
&$uf)
960 $sel = $this->getSortTokens($uf);
961 if (!is_array($sel)) {
965 foreach ($sel as $k=>$s) {
966 $sel[$k] = $s . ' DESC';
972 abstract protected function getSortTokens(UserFilter
&$uf);
976 // {{{ class UFO_Promo
977 /** Orders users by promo
978 * @param $grade Formation whose promo users should be sorted by (restricts results to users of that formation)
979 * @param $desc Whether sort is descending
981 class UFO_Promo
extends UserFilterOrder
985 public function __construct($grade = null
, $desc = false
)
987 parent
::__construct($desc);
988 $this->grade
= $grade;
991 protected function getSortTokens(UserFilter
&$uf)
993 if (UserFilter
::isGrade($this->grade
)) {
994 $sub = $uf->addEducationFilter($this->grade
);
995 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
997 $sub = $uf->addDisplayFilter();
998 return 'pd' . $sub . '.promo';
1004 // {{{ class UFO_Name
1005 /** Sorts users by name
1006 * @param $type Type of name on which to sort (firstname, ...)
1007 * @param $variant Variant of that name to user (marital, ordinary, ...)
1008 * @param $particle Set to true if particles should be included in the sorting order
1009 * @param $desc If sort order should be descending
1011 class UFO_Name
extends UserFilterOrder
1017 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
1019 parent
::__construct($desc);
1020 $this->type
= $type;
1021 $this->variant
= $variant;
1022 $this->particle
= $particle;
1025 protected function getSortTokens(UserFilter
&$uf)
1027 if (UserFilter
::isDisplayName($this->type
)) {
1028 $sub = $uf->addDisplayFilter();
1029 return 'pd' . $sub . '.' . $this->type
;
1031 $sub = $uf->addNameFilter($this->type
, $this->variant
);
1032 if ($this->particle
) {
1033 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1035 return 'pn' . $sub . '.name';
1042 // {{{ class UFO_Registration
1043 /** Sorts users based on registration date
1045 class UFO_Registration
extends UserFilterOrder
1047 protected function getSortTokens(UserFilter
&$uf)
1049 return 'a.registration_date';
1054 // {{{ class UFO_Birthday
1055 /** Sorts users based on next birthday date
1057 class UFO_Birthday
extends UserFilterOrder
1059 protected function getSortTokens(UserFilter
&$uf)
1061 return 'p.next_birthday';
1066 // {{{ class UFO_ProfileUpdate
1067 /** Sorts users based on last profile update
1069 class UFO_ProfileUpdate
extends UserFilterOrder
1071 protected function getSortTokens(UserFilter
&$uf)
1073 return 'p.last_change';
1078 // {{{ class UFO_Death
1079 /** Sorts users based on death date
1081 class UFO_Death
extends UserFilterOrder
1083 protected function getSortTokens(UserFilter
&$uf)
1085 return 'p.deathdate';
1091 /***********************************
1092 *********************************
1094 *********************************
1095 ***********************************/
1097 // {{{ class UserFilter
1100 static private $joinMethods = array();
1103 private $sort = array();
1104 private $query = null
;
1105 private $orderby = null
;
1107 private $lastcount = null
;
1109 public function __construct($cond = null
, $sort = null
)
1111 if (empty(self
::$joinMethods)) {
1112 $class = new ReflectionClass('UserFilter');
1113 foreach ($class->getMethods() as $method) {
1114 $name = $method->getName();
1115 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
1116 self
::$joinMethods[] = $name;
1120 if (!is_null($cond)) {
1121 if ($cond instanceof UserFilterCondition
) {
1122 $this->setCondition($cond);
1125 if (!is_null($sort)) {
1126 if ($sort instanceof UserFilterOrder
) {
1127 $this->addSort($sort);
1128 } else if (is_array($sort)) {
1129 foreach ($sort as $s) {
1136 private function buildQuery()
1138 if (is_null($this->orderby
)) {
1140 foreach ($this->sort
as $sort) {
1141 $orders = array_merge($orders, $sort->buildSort($this));
1143 if (count($orders) == 0) {
1144 $this->orderby
= '';
1146 $this->orderby
= 'ORDER BY ' . implode(', ', $orders);
1149 if (is_null($this->query
)) {
1150 $where = $this->root
->buildCondition($this);
1151 $joins = $this->buildJoins();
1152 $this->query
= 'FROM accounts AS a
1153 LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
1154 LEFT JOIN profiles AS p ON (p.pid = ap.pid)
1156 WHERE (' . $where . ')';
1160 private function formatJoin(array $joins)
1163 foreach ($joins as $key => $infos) {
1166 if ($mode == 'inner') {
1167 $str .= 'INNER JOIN ';
1168 } else if ($mode == 'left') {
1169 $str .= 'LEFT JOIN ';
1171 Platal
::page()->kill("Join mode error");
1173 $str .= $table . ' AS ' . $key;
1174 if (isset($infos[2])) {
1175 $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')';
1182 private function buildJoins()
1185 foreach (self
::$joinMethods as $method) {
1186 $joins = array_merge($joins, $this->$method());
1188 return $this->formatJoin($joins);
1191 private function getUIDList($uids = null
, $count = null
, $offset = null
)
1193 $this->buildQuery();
1195 if (!is_null($count)) {
1196 if (!is_null($offset)) {
1197 $limit = XDB
::format('LIMIT {?}, {?}', (int)$offset, (int)$count);
1199 $limit = XDB
::format('LIMIT {?}', (int)$count);
1203 if (!is_null($uids)) {
1204 $cond = ' AND a.uid IN ' . XDB
::formatArray($uids);
1206 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1207 ' . $this->query
. $cond . '
1209 ' . $this->orderby
. '
1211 $this->lastcount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
1215 /** Check that the user match the given rule.
1217 public function checkUser(PlUser
&$user)
1219 $this->buildQuery();
1220 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
1221 ' . $this->query
. XDB
::format(' AND a.uid = {?}', $user->id()));
1225 /** Filter a list of user to extract the users matching the rule.
1227 public function filter(array $users, $count = null
, $offset = null
)
1229 $this->buildQuery();
1232 foreach ($users as $user) {
1233 if ($user instanceof PlUser
) {
1239 $table[$uid] = $user;
1241 $fetched = $this->getUIDList($uids, $count, $offset);
1243 foreach ($fetched as $uid) {
1244 $output[] = $table[$uid];
1249 public function getUIDs($count = null
, $offset = null
)
1251 return $this->getUIDList(null
, $count, $offset);
1254 public function getUsers($count = null
, $offset = null
)
1256 return User
::getBulkUsersWithUIDs($this->getUIDs($count, $offset));
1259 public function getTotalCount()
1261 if (is_null($this->lastcount
)) {
1262 $this->buildQuery();
1263 return (int)XDB
::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
1266 return $this->lastcount
;
1270 public function setCondition(UserFilterCondition
&$cond)
1272 $this->root
=& $cond;
1273 $this->query
= null
;
1276 public function addSort(UserFilterOrder
&$sort)
1278 $this->sort
[] = $sort;
1279 $this->orderby
= null
;
1282 static public function getLegacy($promo_min, $promo_max)
1284 if ($promo_min != 0) {
1285 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
1287 $min = new UFC_True();
1289 if ($promo_max != 0) {
1290 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
1292 $max = new UFC_True();
1294 return new UserFilter(new UFC_And($min, $max));
1297 static public function sortByName()
1299 return array(new UFO_Name(self
::LASTNAME
), new UFO_Name(self
::FIRSTNAME
));
1302 static public function sortByPromo()
1304 return array(new UFO_Promo(), new UFO_Name(self
::LASTNAME
), new UFO_Name(self
::FIRSTNAME
));
1307 static private function getDBSuffix($string)
1309 return preg_replace('/[^a-z0-9]/i', '', $string);
1313 private $option = 0;
1314 private function register_optional(array &$table, $val)
1316 if (is_null($val)) {
1317 $sub = $this->option++
;
1320 $sub = self
::getDBSuffix($val);
1324 $table[$sub] = $index;
1330 const DISPLAY
= 'display';
1331 private $pd = false
;
1332 public function addDisplayFilter()
1338 private function displayJoins()
1341 return array('pd' => array('left', 'profile_display', '$ME.pid = $PID'));
1350 const LASTNAME
= 'lastname';
1351 const FIRSTNAME
= 'firstname';
1352 const NICKNAME
= 'nickname';
1353 const PSEUDONYM
= 'pseudonym';
1354 const NAME
= 'name';
1356 const VN_MARITAL
= 'marital';
1357 const VN_ORDINARY
= 'ordinary';
1358 const VN_OTHER
= 'other';
1359 const VN_INI
= 'ini';
1361 const DN_FULL
= 'directory_name';
1362 const DN_DISPLAY
= 'yourself';
1363 const DN_YOURSELF
= 'yourself';
1364 const DN_DIRECTORY
= 'directory_name';
1365 const DN_PRIVATE
= 'private_name';
1366 const DN_PUBLIC
= 'public_name';
1367 const DN_SHORT
= 'short_name';
1368 const DN_SORT
= 'sort_name';
1370 static public $name_variants = array(
1371 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
1372 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
1375 static public function assertName($name)
1377 if (!Profile
::getNameTypeId($name)) {
1378 Platal
::page()->kill('Invalid name type');
1382 static public function isDisplayName($name)
1384 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
1385 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
1386 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1387 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1390 private $pn = array();
1391 public function addNameFilter($type, $variant = null
)
1393 if (!is_null($variant)) {
1394 $ft = $type . '_' . $variant;
1399 self
::assertName($ft);
1401 if (!is_null($variant) && $variant == 'other') {
1402 $sub .= $this->option++
;
1404 $this->pn
[$sub] = Profile
::getNameTypeId($ft);
1408 private function nameJoins()
1411 foreach ($this->pn
as $sub => $type) {
1412 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
1420 const GRADE_ING
= 'Ing.';
1421 const GRADE_PHD
= 'PhD';
1422 const GRADE_MST
= 'M%';
1423 static public function isGrade($grade)
1425 return $grade == self
::GRADE_ING ||
$grade == self
::GRADE_PHD ||
$grade == self
::GRADE_MST
;
1428 static public function assertGrade($grade)
1430 if (!self
::isGrade($grade)) {
1431 Platal
::page()->killError("Diplôme non valide");
1435 static public function promoYear($grade)
1437 // XXX: Definition of promotion for phds and masters might change in near future.
1438 return ($grade == UserFilter
::GRADE_ING
) ?
'entry_year' : 'grad_year';
1441 private $pepe = array();
1442 private $with_pee = false
;
1443 public function addEducationFilter($x = false
, $grade = null
)
1446 $index = $this->option
;
1447 $sub = $this->option++
;
1449 self
::assertGrade($grade);
1452 $this->with_pee
= true
;
1455 $this->pepe
[$index] = $sub;
1459 private function educationJoins()
1462 if ($this->with_pee
) {
1463 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
1465 foreach ($this->pepe
as $grade => $sub) {
1466 if ($this->isGrade($grade)) {
1467 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
1468 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
1469 XDB
::format('{?}', $grade));
1471 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID');
1472 $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
1473 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
1482 private $gpm = array();
1483 public function addGroupFilter($group = null
)
1485 if (!is_null($group)) {
1486 if (ctype_digit($group)) {
1487 $index = $sub = $group;
1490 $sub = self
::getDBSuffix($group);
1493 $sub = 'group_' . $this->option++
;
1497 $this->gpm
[$sub] = $index;
1501 private function groupJoins()
1504 foreach ($this->gpm
as $sub => $key) {
1505 if (is_null($key)) {
1506 $joins['gpa' . $sub] = array('inner', 'groupex.asso');
1507 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1508 } else if (ctype_digit($key)) {
1509 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
1511 $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB
::format('$ME.diminutif = {?}', $key));
1512 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1520 private $e = array();
1521 public function addEmailRedirectFilter($email = null
)
1523 return $this->register_optional($this->e
, $email);
1526 private $ve = array();
1527 public function addVirtualEmailFilter($email = null
)
1529 $this->addAliasFilter(self
::ALIAS_FORLIFE
);
1530 return $this->register_optional($this->ve
, $email);
1533 const ALIAS_BEST
= 'bestalias';
1534 const ALIAS_FORLIFE
= 'forlife';
1535 private $al = array();
1536 public function addAliasFilter($alias = null
)
1538 return $this->register_optional($this->al
, $alias);
1541 private function emailJoins()
1545 foreach ($this->e
as $sub=>$key) {
1546 if (is_null($key)) {
1547 $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
1549 $joins['e' . $sub] = array('left', 'emails', XDB
::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
1552 foreach ($this->al
as $sub=>$key) {
1553 if (is_null($key)) {
1554 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
1555 } else if ($key == self
::ALIAS_BEST
) {
1556 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
1557 } else if ($key == self
::ALIAS_FORLIFE
) {
1558 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
1560 $joins['al' . $sub] = array('left', 'aliases', XDB
::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
1563 foreach ($this->ve
as $sub=>$key) {
1564 if (is_null($key)) {
1565 $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\'');
1567 $joins['v' . $sub] = array('left', 'virtual', XDB
::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
1569 $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB
::format('$ME.vid = v' . $sub . '.vid
1570 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
1571 CONCAT(al_forlife.alias, \'@\', {?}),
1573 $globals->mail
->domain
, $globals->mail
->domain2
));
1581 private $pa = false
;
1582 public function addAddressFilter()
1587 private function addressJoins()
1591 $joins['pa'] = array('left', 'profile_address', '$ME.PID = $PID');
1600 private $pc = false
;
1601 private $pce = array();
1602 private $pcr = false
;
1603 public function addCorpsFilter($type)
1606 if ($type == UFC_Corps
::CURRENT
) {
1607 $pce['pcec'] = 'current_corpsid';
1609 } else if ($type == UFC_Corps
::ORIGIN
) {
1610 $pce['pceo'] = 'original_corpsid';
1615 public function addCorpsRankFilter()
1622 private function corpsJoins()
1626 $joins['pc'] = array('left', 'profile_corps', '$ME.uid = $UID');
1629 $joins['pcr'] = array('left', 'profile_corps_rank_enum', '$ME.id = pc.rankid');
1631 foreach($this->pce
as $sub => $field) {
1632 $joins[$sub] = array('left', 'profile_corps_enum', '$ME.id = pc.' . $field);
1640 const JOB_SECTOR
= 1;
1641 const JOB_SUBSECTOR
= 2;
1642 const JOB_SUBSUBSECTOR
= 4;
1643 const JOB_ALTERNATES
= 8;
1644 const JOB_USERDEFINED
= 16;
1648 * pje => profile_job_enum
1649 * pjse => profile_job_sector_enum
1650 * pjsse => profile_job_subsector_enum
1651 * pjssse => profile_job_subsubsector_enum
1652 * pja => profile_job_alternates
1654 private $with_pj = false
;
1655 private $with_pje = false
;
1656 private $with_pjse = false
;
1657 private $with_pjsse = false
;
1658 private $with_pjssse = false
;
1659 private $with_pja = false
;
1661 public function addJobFilter()
1663 $this->with_pj
= true
;
1667 public function addJobCompanyFilter()
1669 $this->addJobFilter();
1670 $this->with_pje
= true
;
1674 public function addJobSectorizationFilter($type)
1676 $this->addJobFilter();
1677 if ($type == self
::JOB_SECTOR
) {
1678 $this->with_pjse
= true
;
1680 } else if ($type == self
::JOB_SUBSECTOR
) {
1681 $this->with_pjsse
= true
;
1683 } else if ($type == self
::JOB_SUBSUBSECTOR
) {
1684 $this->with_pjssse
= true
;
1686 } else if ($type == self
::JOB_ALTERNATES
) {
1687 $this->with_pja
= true
;
1692 private function jobJoins()
1695 if ($this->with_pj
) {
1696 $joins['pj'] = array('left', 'profile_job', '$ME.uid = $UID');
1698 if ($this->with_pje
) {
1699 $joins['pje'] = array('left', 'profile_job_enum', '$ME.id = pj.jobid');
1701 if ($this->with_pjse
) {
1702 $joins['pjse'] = array('left', 'profile_job_sector_enum', '$ME.id = pj.sectorid');
1704 if ($this->with_pjsse
) {
1705 $joins['pjsse'] = array('left', 'profile_job_subsector_enum', '$ME.id = pj.subsectorid');
1707 if ($this->with_pjssse
) {
1708 $joins['pjssse'] = array('left', 'profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
1710 if ($this->with_pja
) {
1711 $joins['pja'] = array('left', 'profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
1719 private $with_pnw = false
;
1720 public function addNetworkingFilter()
1722 $this->with_pnw
= true
;
1726 private function networkingJoins()
1729 if ($this->with_pnw
) {
1730 $joins['pnw'] = array('left', 'profile_networking', '$ME.uid = $UID');
1738 private $with_phone = false
;
1740 public function addPhoneFilter()
1742 $this->with_phone
= true
;
1746 private function phoneJoins()
1749 if ($this->with_phone
) {
1750 $joins['ptel'] = array('left', 'profile_phone', '$ME.uid = $UID');
1758 private $with_medals = false
;
1759 public function addMedalFilter()
1761 $this->with_medals
= true
;
1765 private function medalJoins()
1768 if ($this->with_medals
) {
1769 $joins['pmed'] = array('left', 'profile_medals_sub', '$ME.uid = $UID');
1776 private $cts = array();
1777 public function addContactFilter($uid = null
)
1779 return $this->register_optional($this->cts
, is_null($uid) ? null
: 'user_' . $uid);
1782 private function contactJoins()
1785 foreach ($this->cts
as $sub=>$key) {
1786 if (is_null($key)) {
1787 $joins['c' . $sub] = array('left', 'contacts', '$ME.contact = $UID');
1789 $joins['c' . $sub] = array('left', 'contacts', XDB
::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
1798 private $wn = array();
1799 public function addWatchRegistrationFilter($uid = null
)
1801 return $this->register_optional($this->wn
, is_null($uid) ? null
: 'user_' . $uid);
1804 private $wp = array();
1805 public function addWatchPromoFilter($uid = null
)
1807 return $this->register_optional($this->wp
, is_null($uid) ? null
: 'user_' . $uid);
1810 private $w = array();
1811 public function addWatchFilter($uid = null
)
1813 return $this->register_optional($this->w
, is_null($uid) ? null
: 'user_' . $uid);
1816 private function watchJoins()
1819 foreach ($this->w
as $sub=>$key) {
1820 if (is_null($key)) {
1821 $joins['w' . $sub] = array('left', 'watch');
1823 $joins['w' . $sub] = array('left', 'watch', XDB
::format('$ME.uid = {?}', substr($key, 5)));
1826 foreach ($this->wn
as $sub=>$key) {
1827 if (is_null($key)) {
1828 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1830 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB
::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1833 foreach ($this->wn
as $sub=>$key) {
1834 if (is_null($key)) {
1835 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1837 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB
::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1840 foreach ($this->wp
as $sub=>$key) {
1841 if (is_null($key)) {
1842 $joins['wp' . $sub] = array('left', 'watch_promo');
1844 $joins['wp' . $sub] = array('left', 'watch_promo', XDB
::format('$ME.uid = {?}', substr($key, 5)));
1852 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: