a66ef5ab3370f8f0a8aab6f01e200b6e5c5ed137
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 ***************************************************************************/
22 // {{{ abstract class UserFilterCondition
23 /** This class describe objects which filter users based
24 * on various parameters.
25 * The parameters of the filter must be given to the constructor.
26 * The buildCondition function is called by UserFilter when
27 * actually building the query. That function must call
28 * $uf->addWheteverFilter so that the UserFilter makes
29 * adequate joins. It must return the 'WHERE' condition to use
32 abstract class UserFilterCondition
implements PlFilterCondition
34 const OP_EQUALS
= '=';
35 const OP_GREATER
= '>';
36 const OP_NOTGREATER
= '<=';
37 const OP_LESSER
= '<';
38 const OP_NOTLESSER
= '>=';
39 const OP_NULL
= 'null';
40 const OP_NOTNULL
= 'not null';
41 const OP_CONTAINS
= 'contains';
42 const OP_PREFIX
= 'prefix';
43 const OP_SUFFIX
= 'suffix';
45 protected function buildExport($type)
47 $export = array('type' => $type);
51 public function export()
53 throw new Exception("This class is not exportable");
56 public static function comparisonFromXDBWildcard($wildcard)
59 case XDB
::WILDCARD_EXACT
:
60 return self
::OP_EQUALS
;
61 case XDB
::WILDCARD_PREFIX
:
62 return self
::OP_PREFIX
;
63 case XDB
::WILDCARD_SUFFIX
:
64 return self
::OP_SUFFIX
;
65 case XDB
::WILDCARD_CONTAINS
:
66 return self
::OP_CONTAINS
;
68 throw new Exception("Unknown wildcard mode: $wildcard");
71 public static function xdbWildcardFromComparison($comparison)
73 if (!self
::isStringComparison($comparison)) {
74 throw new Exception("Unknown string coparison: $comparison");
76 switch ($comparison) {
78 return XDB
::WILDCARD_EXACT
;
80 return XDB
::WILDCARD_PREFIX
;
82 return XDB
::WILDCARD_SUFFIX
;
83 case self
::OP_CONTAINS
:
84 return XDB
::WILDCARD_CONTAINS
;
88 private static function isNumericComparison($comparison)
90 return $comparison == self
::OP_EQUALS
91 ||
$comparison == self
::OP_GREATER
92 ||
$comparison == self
::OP_NOTGREATER
93 ||
$comparison == self
::OP_LESSER
94 ||
$comparison == self
::OP_NOTLESSER
;
97 private static function isStringComparison($comparison)
99 return $comparison == self
::OP_EQUALS
100 ||
$comparison == self
::OP_CONTAINS
101 ||
$comparison == self
::OP_PREFIX
102 ||
$comparison == self
::OP_SUFFIX
;
105 public static function fromExport(array $export)
107 $export = new PlDict($export);
108 if (!$export->has('type')) {
109 throw new Exception("Missing type in export");
111 $type = $export->s('type');
119 $class = 'pfc_' . $type;
120 $cond = new $class();
124 if ($export->has('ip')) {
125 $cond = new UFC_Ip($export->s('ip'));
130 if ($export->has('text') && $export->s('comparison') == self
::OP_CONTAINS
) {
131 $cond = new UFC_Comment($export->s('text'));
136 if ($export->has('promo') && self
::isNumericComparison($export->s('comparison'))) {
137 $cond = new UFC_Promo($export->s('comparison'),
138 $export->s('grade', UserFilter
::DISPLAY
),
139 $export->s('promo'));
148 if ($export->has('text')) {
149 $flag = self
::xdbWildcardFromComparison($export->s('comparison'));
150 if ($export->b('search_in_variants')) {
151 $flag |
= UFC_Name
::VARIANTS
;
153 if ($export->b('search_in_particle')) {
154 $flag |
= UFC_Name
::PARTICLE
;
156 $cond = new UFC_Name($type, $export->s('text'), $flag);
164 $values = $export->v('values', array());
165 $class = 'ufc_' . str_replace('_', '', $type);
166 $cond = new $class($values);
170 $values = $export->v('values', array());
171 $school_type = $export->s('school_type');
172 $cond = new UFC_SchoolId($school_type, $values);
176 case 'has_email_redirect':
177 case 'has_valid_email':
178 $class = 'ufc_' . str_replace('_', '', $type);
179 $cond = new $class();
183 throw new Exception("Unknown condition type: $type");
185 if (is_null($cond)) {
186 throw new Exception("Unsupported $type definition");
188 if ($cond instanceof PFC_NChildren
) {
189 $children = $export->v('children', array());
190 foreach ($children as $child) {
191 $cond->addChild(self
::fromExport($child));
193 } else if ($cond instanceof PFC_OneChild
) {
194 if ($export->has('child')) {
195 $cond->setChild(self
::fromExport($export->v('child')));
202 // {{{ class UFC_HasProfile
203 /** Filters users who have a profile
205 class UFC_HasProfile
extends UserFilterCondition
207 public function buildCondition(PlFilter
$uf)
209 $uf->requireProfiles();
210 return '$PID IS NOT NULL';
213 public function export()
215 return $this->buildExport('has_profile');
219 // {{{ class UFC_AccountType
220 /** Filters users who have one of the given account types
222 class UFC_AccountType
extends UserFilterCondition
226 public function __construct()
228 $this->types
= pl_flatten(func_get_args());
231 public function buildCondition(PlFilter
$uf)
233 $uf->requireAccounts();
234 return XDB
::format('a.type IN {?}', $this->types
);
237 public function export()
239 $export = $this->buildExport('account_type');
240 $export['values'] = $this->types
;
245 // {{{ class UFC_AccountPerm
246 /** Filters users who have one of the given permissions
248 class UFC_AccountPerm
extends UserFilterCondition
252 public function __construct()
254 $this->perms
= pl_flatten(func_get_args());
257 public function buildCondition(PlFilter
$uf)
261 foreach ($this->perms
as $perm) {
262 $conds[] = XDB
::format('FIND_IN_SET({?}, IF(a.user_perms IS NULL, at.perms,
263 CONCAT(at.perms, \',\', a.user_perms)))',
267 return self
::COND_TRUE
;
269 return implode(' OR ', $conds);
273 public function export()
275 $export = $this->buildExport('account_perm');
276 $export['values'] = $this->perms
;
281 // {{{ class UFC_Hruid
282 /** Filters users based on their hruid
283 * @param $val Either an hruid, or a list of those
285 class UFC_Hruid
extends UserFilterCondition
289 public function __construct()
291 $this->hruids
= pl_flatten(func_get_args());
294 public function buildCondition(PlFilter
$uf)
296 $uf->requireAccounts();
297 return XDB
::format('a.hruid IN {?}', $this->hruids
);
300 public function export()
302 $export = $this->buildExport('hruid');
303 $export['values'] = $this->hruids
;
308 // {{{ class UFC_Hrpid
309 /** Filters users based on the hrpid of their profiles
310 * @param $val Either an hrpid, or a list of those
312 class UFC_Hrpid
extends UserFilterCondition
316 public function __construct()
318 $this->hrpids
= pl_flatten(func_get_args());
321 public function buildCondition(PlFilter
$uf)
323 $uf->requireProfiles();
324 return XDB
::format('p.hrpid IN {?}', $this->hrpids
);
327 public function export()
329 $export = $this->buildExport('hrpid');
330 $export['values'] = $this->hrpids
;
335 // {{{ class UFC_HasEmailRedirect
336 /** Filters users, keeping only those with a valid email redirection (only X.org accounts).
338 class UFC_HasEmailRedirect
extends UserFilterCondition
340 public function buildCondition(PlFilter
$uf)
342 $sub_redirect = $uf->addActiveEmailRedirectFilter();
343 return 'rf.redirect IS NOT NULL';
346 public function export()
348 $export = $this->buildExport('has_email_redirect');
353 // {{{ class UFC_HasValidEmail
354 /** Filters users, keeping only those with a valid email address (all accounts).
356 class UFC_HasValidEmail
extends UserFilterCondition
358 public function buildCondition(PlFilter
$uf)
360 $sub_redirect = $uf->addEmailRedirectFilter();
361 $uf->requireAccounts();
362 return 'ra' . $sub_redirect . '.flags = \'active\' OR a.email IS NOT NULL';
365 public function export()
367 $export = $this->buildExport('has_valid_email');
373 /** Filters users based on one of their last IPs
374 * @param $ip IP from which connection are checked
376 class UFC_Ip
extends UserFilterCondition
380 public function __construct($ip)
385 public function buildCondition(PlFilter
$uf)
387 $sub = $uf->addLoggerFilter();
388 $ip = ip_to_uint($this->ip
);
389 return XDB
::format($sub . '.ip = {?} OR ' . $sub . '.forward_ip = {?}', $ip, $ip);
392 public function export()
394 $export = $this->buildExport('host');
395 $export['ip'] = $this->ip
;
400 // {{{ class UFC_Comment
401 class UFC_Comment
extends UserFilterCondition
405 public function __construct($text)
410 public function buildCondition(PlFilter
$uf)
412 $uf->requireProfiles();
413 return $uf->getVisibilityConditionForField('p.freetext_pub') . ' AND p.freetext ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->text
);
416 public function export()
418 $export = $this->buildExport('comment');
419 $export['comparison'] = self
::OP_CONTAINS
;
420 $export['text'] = $this->text
;
425 // {{{ class UFC_Promo
426 /** Filters users based on promotion
427 * @param $comparison Comparison operator (>, =, ...)
428 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
429 * @param $promo Promotion on which the filter is based
431 class UFC_Promo
extends UserFilterCondition
438 public function __construct($comparison, $grade, $promo)
440 $this->grade
= $grade;
441 $this->comparison
= $comparison;
442 $this->promo
= $promo;
443 if ($this->grade
!= UserFilter
::DISPLAY
) {
444 UserFilter
::assertGrade($this->grade
);
446 if ($this->grade
== UserFilter
::DISPLAY
&& $this->comparison
!= '=') {
447 // XXX: we might try to guess the grade from the first char of the promo and forbid only '<= 2004', but allow '<= X2004'
448 Platal
::page()->killError("Il n'est pas possible d'appliquer la comparaison '" . $this->comparison
. "' aux promotions sans spécifier de formation (X/M/D)");
452 public function buildCondition(PlFilter
$uf)
454 if ($this->grade
== UserFilter
::DISPLAY
) {
455 $sub = $uf->addDisplayFilter();
456 return XDB
::format('pd' . $sub . '.promo ' . $this->comparison
. ' {?}', $this->promo
);
458 $sub = $uf->addEducationFilter(true
, $this->grade
);
459 $field = 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
460 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->promo
);
464 public function export()
466 $export = $this->buildExport('promo');
467 $export['comparison'] = $this->comparison
;
468 if ($this->grade
!= UserFilter
::DISPLAY
) {
469 $export['grade'] = $this->grade
;
471 $export['promo'] = $this->promo
;
476 // {{{ class UFC_SchoolId
477 /** Filters users based on their shoold identifier
478 * @param type Parameter type (Xorg, AX, School)
479 * @param value Array of school ids
481 class UFC_SchoolId
extends UserFilterCondition
485 const School
= 'school';
490 static public function assertType($type)
492 if ($type != self
::AX
&& $type != self
::Xorg
&& $type != self
::School
) {
493 Platal
::page()->killError("Type de matricule invalide: $type");
497 /** Construct a UFC_SchoolId
498 * The first argument is the type, all following arguments can be either ids
499 * or arrays of ids to use:
500 * $ufc = new UFC_SchoolId(UFC_SchoolId::AX, $id1, $id2, array($id3, $id4));
502 public function __construct($type)
505 $ids = func_get_args();
507 $this->ids
= pl_flatten($ids);
508 self
::assertType($type);
511 public function buildCondition(PlFilter
$uf)
513 $uf->requireProfiles();
516 if ($type == self
::School
) {
518 $ids = array_map(array('Profile', 'getXorgId'), $ids);
520 return XDB
::format('p.' . $type . '_id IN {?}', $ids);
523 public function export()
525 $export = $this->buildExport('school_id');
526 $export['school_type'] = $this->type
;
527 $export['values'] = $this->ids
;
532 // {{{ class UFC_EducationSchool
533 /** Filters users by formation
534 * @param $val The formation to search (either ID or array of IDs)
536 class UFC_EducationSchool
extends UserFilterCondition
540 public function __construct()
542 $this->val
= pl_flatten(func_get_args());
545 public function buildCondition(PlFilter
$uf)
547 $sub = $uf->addEducationFilter();
548 return XDB
::format('pe' . $sub . '.eduid IN {?}', $this->val
);
552 // {{{ class UFC_EducationDegree
553 class UFC_EducationDegree
extends UserFilterCondition
557 public function __construct()
559 $this->diploma
= pl_flatten(func_get_args());
562 public function buildCondition(PlFilter
$uf)
564 $sub = $uf->addEducationFilter();
565 return XDB
::format('pe' . $sub . '.degreeid IN {?}', $this->diploma
);
569 // {{{ class UFC_EducationField
570 class UFC_EducationField
extends UserFilterCondition
574 public function __construct()
576 $this->val
= pl_flatten(func_get_args());
579 public function buildCondition(PlFilter
$uf)
581 $sub = $uf->addEducationFilter();
582 return XDB
::format('pe' . $sub . '.fieldid IN {?}', $this->val
);
586 // {{{ class UFC_NameInitial
587 /** Filters users based on sort_name
588 * @param $initial Initial on which to filter
590 class UFC_NameInitial
extends UserFilterCondition
594 public function __construct($initial)
596 $this->initial
= $initial;
599 public function buildCondition(PlFilter
$uf)
601 $table = 'sort_name';
602 if ($uf->accountsRequired()) {
603 $table = Profile
::getAccountEquivalentName($table);
606 $uf->addDisplayFilter();
609 return 'SUBSTRING(' . $sub . '.' . $table . ', 1, 1) ' . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $this->initial
);
612 public function export()
614 $export = $this->buildExport($this->initial
);
619 // {{{ class UFC_NameTokens
620 /** Selects users based on tokens in their name (for quicksearch)
621 * @param $tokens An array of tokens to search
622 * @param $flags Flags the tokens must have (e.g 'public' for public search)
623 * @param $soundex (bool) Whether those tokens are fulltext or soundex
625 class UFC_NameTokens
extends UserFilterCondition
628 const FLAG_PUBLIC
= 'public';
634 private $general_type;
636 public function __construct($tokens, $flags = array(), $soundex = false
, $exact = false
, $general_type = '')
638 if (is_array($tokens)) {
639 $this->tokens
= $tokens;
641 $this->tokens
= array($tokens);
643 if (is_array($flags)) {
644 $this->flags
= $flags;
646 $this->flags
= array($flags);
648 $this->soundex
= $soundex;
649 $this->exact
= $exact;
650 $this->general_type
= $general_type;
653 public function buildCondition(PlFilter
$uf)
656 foreach ($this->tokens
as $i => $token) {
657 $sub = $uf->addNameTokensFilter($token);
658 if ($this->soundex
) {
659 $c = XDB
::format($sub . '.soundex = {?}', soundex_fr($token));
660 } else if ($this->exact
) {
661 $c = XDB
::format($sub . '.token = {?}', $token);
663 $c = $sub . '.token ' . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $token);
665 if ($this->flags
!= null
) {
666 $c .= XDB
::format(' AND ' . $sub . '.flags IN {?}', $this->flags
);
668 if ($this->general_type
) {
669 $c .= XDB
::format(' AND ' . $sub . '.general_type = {?}', $this->general_type
);
671 $c .= ' AND (' . $uf->getVisibilityConditionAbsolute(Visibility
::EXPORT_PRIVATE
) . ' OR ' . $sub . '.general_type != \'nickname\')';
675 return implode(' AND ', $conds);
679 // {{{ class UFC_Nationality
680 class UFC_Nationality
extends UserFilterCondition
684 public function __construct()
686 $this->val
= pl_flatten(func_get_args());
689 public function buildCondition(PlFilter
$uf)
691 $uf->requireProfiles();
692 $nat = XDB
::formatArray($this->val
);
694 'p.nationality1 IN ' . $nat,
695 'p.nationality2 IN ' . $nat,
696 'p.nationality3 IN ' . $nat,
698 return implode(' OR ', $conds);
702 // {{{ class UFC_Dead
703 /** Filters users based on death date
704 * @param $comparison Comparison operator
705 * @param $date Date to which death date should be compared (DateTime object, string or timestamp)
707 class UFC_Dead
extends UserFilterCondition
712 public function __construct($comparison = null
, $date = null
)
714 $this->comparison
= $comparison;
715 $this->date
= make_datetime($date);
718 public function buildCondition(PlFilter
$uf)
720 $uf->requireProfiles();
721 $str = 'p.deathdate IS NOT NULL';
722 if (!is_null($this->comparison
)) {
723 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->date
->format('Y-m-d'));
729 // {{{ class UFC_Registered
730 /** Filters users based on registration state
731 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
732 * @param $comparison Comparison operator
733 * @param $date Date to which users registration date should be compared
735 class UFC_Registered
extends UserFilterCondition
741 public function __construct($active = false
, $comparison = null
, $date = null
)
743 $this->active
= $active;
744 $this->comparison
= $comparison;
745 $this->date
= make_datetime($date);
748 public function buildCondition(PlFilter
$uf)
750 $uf->requireAccounts();
752 $date = '$UID IS NOT NULL AND a.state = \'active\'';
754 $date = '$UID IS NOT NULL AND a.state != \'pending\'';
756 if (!is_null($this->comparison
)) {
757 $date .= ' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->date
->format('Y-m-d'));
763 // {{{ class UFC_ProfileUpdated
764 /** Filters users based on profile update date
765 * @param $comparison Comparison operator
766 * @param $date Date to which profile update date must be compared
768 class UFC_ProfileUpdated
extends UserFilterCondition
773 public function __construct($comparison = null
, $date = null
)
775 $this->comparison
= $comparison;
779 public function buildCondition(PlFilter
$uf)
781 $uf->requireProfiles();
782 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
786 // {{{ class UFC_Birthday
787 /** Filters users based on next birthday date
788 * @param $comparison Comparison operator
789 * @param $date Date to which users next birthday date should be compared
791 class UFC_Birthday
extends UserFilterCondition
796 public function __construct($comparison = null
, $date = null
)
798 $this->comparison
= $comparison;
802 public function buildCondition(PlFilter
$uf)
804 $uf->requireProfiles();
805 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
810 /** Filters users based on sex
811 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
813 class UFC_Sex
extends UserFilterCondition
816 public function __construct($sex)
821 public function buildCondition(PlFilter
$uf)
823 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
824 return self
::COND_FALSE
;
826 $uf->requireProfiles();
827 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
832 // {{{ class UFC_NLSubscribed
833 /** Filters users based on NL subscription
834 * @param $nlid NL whose subscribers we are selecting
835 * @param $issue Select only subscribers who have not yet received that issue
837 class UFC_NLSubscribed
extends UserFilterCondition
841 public function __construct($nlid, $issue_id)
844 $this->issue_id
= $issue_id;
847 public function buildCondition(PlFilter
$uf)
849 $sub = $uf->addNewsLetterFilter($this->nlid
);
850 return XDB
::format($sub . '.nlid IS NOT NULL AND ( ' . $sub . '.last IS NULL OR ' . $sub . '.last < {?})', $this->issue_id
);
854 // {{{ class UFC_Group
855 /** Filters users based on group membership
856 * @param $group Group whose members we are selecting
857 * @param $anim Whether to restrict selection to animators of that group
859 class UFC_Group
extends UserFilterCondition
863 public function __construct($group, $anim = false
)
865 $this->group
= $group;
869 public function buildCondition(PlFilter
$uf)
871 // Groups are only visible for users with perm 'groups'.
872 if (!S
::user()->checkPerms(User
::PERM_GROUPS
)) {
873 return self
::COND_FALSE
;
875 $sub = $uf->addGroupFilter($this->group
);
876 $where = 'gpm' . $sub . '.perms IS NOT NULL';
878 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
884 // {{{ class UFC_GroupFormerMember
885 /** Filters users based on group former membership
886 * @param $group Group whose former members we are selecting
888 class UFC_GroupFormerMember
extends UserFilterCondition
892 public function __construct($group)
894 $this->group
= $group;
897 public function buildCondition(PlFilter
$uf)
899 // Groups are only visible for users with perm 'groups'.
900 if (!S
::user()->checkPerms(User
::PERM_GROUPS
)) {
901 return self
::COND_FALSE
;
903 $sub = $uf->addGroupFormerMemberFilter();
904 return XDB
::format('gpfm' . $sub . '.asso_id = {?}', $this->group
);
908 // {{{ class UFC_Binet
909 /** Selects users based on their belonging to a given (list of) binet
910 * @param $binet either a binet_id or an array of binet_ids
912 class UFC_Binet
extends UserFilterCondition
916 public function __construct()
918 $this->val
= pl_flatten(func_get_args());
921 public function buildCondition(PlFilter
$uf)
923 $sub = $uf->addBinetsFilter();
924 // Binets are private.
925 return XDB
::format($uf->getVisibilityConditionAbsolute(Visibility
::EXPORT_PRIVATE
) . ' AND ' . $sub . '.binet_id IN {?}', $this->val
);
929 // {{{ class UFC_Section
930 /** Selects users based on section
931 * @param $section ID of the section
933 class UFC_Section
extends UserFilterCondition
937 public function __construct()
939 $this->section
= pl_flatten(func_get_args());
942 public function buildCondition(PlFilter
$uf)
944 // Sections are private.
945 $uf->requireProfiles();
946 return XDB
::format($uf->getVisibilityConditionAbsolute(Visibility
::EXPORT_PRIVATE
) . ' AND p.section IN {?}', $this->section
);
950 // {{{ class UFC_Email
951 /** Filters users based on an email or a list of emails
952 * @param $emails List of emails whose owner must be selected
954 class UFC_Email
extends UserFilterCondition
957 public function __construct()
959 $this->emails
= pl_flatten(func_get_args());
962 public function buildCondition(PlFilter
$uf)
968 if (count($this->emails
) == 0) {
969 return PlFilterCondition
::COND_TRUE
;
972 foreach ($this->emails
as $entry) {
973 if (User
::isForeignEmailAddress($entry)) {
976 list($local_part, ) = explode('@', $entry);
977 $local[] = $local_part;
981 if (count($foreign) > 0) {
982 $sub = $uf->addEmailRedirectFilter($foreign);
983 $cond[] = XDB
::format('ra' . $sub . '.redirect IS NOT NULL OR ra' . $sub . '.redirect IN {?} OR a.email IN {?}', $foreign, $foreign);
985 if (count($local) > 0) {
986 $sub = $uf->addAliasFilter($local);
987 $cond[] = 'sa' . $sub . '.email IS NOT NULL';
989 return '(' . implode(') OR (', $cond) . ')';
993 // {{{ class UFC_Address
994 abstract class UFC_Address
extends UserFilterCondition
996 /** Valid address type
1000 const TYPE_NON_HQ
= 3;
1004 /** Text for these types
1006 protected static $typetexts = array(
1007 self
::TYPE_HOME
=> 'home',
1008 self
::TYPE_PRO
=> 'pro',
1009 self
::TYPE_HQ
=> 'hq',
1014 /** Flags for addresses
1016 const FLAG_CURRENT
= 0x0001;
1017 const FLAG_TEMP
= 0x0002;
1018 const FLAG_SECOND
= 0x0004;
1019 const FLAG_MAIL
= 0x0008;
1020 const FLAG_CEDEX
= 0x0010;
1022 // Binary OR of those flags
1023 const FLAG_ANY
= 0x001F;
1025 /** Text of these flags
1027 protected static $flagtexts = array(
1028 self
::FLAG_CURRENT
=> 'current',
1029 self
::FLAG_TEMP
=> 'temporary',
1030 self
::FLAG_SECOND
=> 'secondary',
1031 self
::FLAG_MAIL
=> 'mail',
1032 self
::FLAG_CEDEX
=> 'cedex',
1037 public function __construct($type = null
, $flags = null
)
1039 $this->type
= $type;
1040 $this->flags
= $flags;
1043 protected function initConds($sub, $vis_cond)
1045 $conds = array($vis_cond);
1048 foreach (self
::$typetexts as $flag => $type) {
1049 if ($flag & $this->type
) {
1053 if (count($types)) {
1054 $conds[] = XDB
::format('pa' . $sub . '.type IN {?}', $types);
1057 if ($this->flags
!= self
::FLAG_ANY
) {
1058 foreach(self
::$flagtexts as $flag => $text) {
1059 if ($flag & $this->flags
) {
1060 $conds[] = 'FIND_IN_SET(' . XDB
::format('{?}', $text) . ', pa' . $sub . '.flags)';
1069 // {{{ class UFC_AddressField
1070 /** Filters users based on their address,
1071 * @param $val Either a code for one of the fields, or an array of such codes
1072 * @param $fieldtype The type of field to look for
1073 * @param $type Filter on address type
1074 * @param $flags Filter on address flags
1076 class UFC_AddressComponent
extends UFC_Address
1078 static $components = array('postal_code', 'locality', 'administrative_area_level_3', 'administrative_area_level_2', 'administrative_area_level_1', 'country');
1080 /** Data of the filter
1086 public function __construct($val, $fieldtype, $exact = true
, $type = null
, $flags = self
::FLAG_ANY
)
1088 if (!in_array($fieldtype, self
::$components)) {
1089 Platal
::page()->killError('Invalid address field type: ' . $this->fieldtype
);
1092 parent
::__construct($type, $flags);
1093 if (!is_array($val)) {
1097 $this->fieldtype
= $fieldtype;
1098 $this->exact
= $exact;
1101 public function buildCondition(PlFilter
$uf)
1103 $sub = $uf->addAddressFilter($this->fieldtype
);
1104 $conds = $this->initConds($sub, $uf->getVisibilityConditionForField('pa' . $sub . '.pub'));
1105 $conds[] = XDB
::format('pace' . $sub . '.id IN {?}', $this->val
);
1107 return implode(' AND ', $conds);
1111 // {{{ class UFC_Corps
1112 /** Filters users based on the corps they belong to
1113 * @param $corps Corps we are looking for (abbreviation)
1114 * @param $type Whether we search for original or current corps
1116 class UFC_Corps
extends UserFilterCondition
1125 public function __construct($corps, $id = null
, $type = self
::CURRENT
)
1127 $this->corps
= $corps;
1129 $this->type
= $type;
1132 public function buildCondition(PlFilter
$uf)
1134 /** Tables shortcuts:
1135 * pc for profile_corps,
1136 * pceo for profile_corps_enum - orginal
1137 * pcec for profile_corps_enum - current
1139 $sub = $uf->addCorpsFilter($this->type
);
1140 if (is_null($this->id
)) {
1141 $cond = $sub . '.abbreviation = ' . $this->corps
;
1143 $cond = $sub . '.id = ' . $this->id
;
1145 // XXX(x2006barrois): find a way to get rid of that hardcoded
1146 // reference to 'pc'.
1147 $cond .= ' AND ' . $uf->getVisibilityConditionForField('pc.corps_pub');
1152 // {{{ class UFC_Corps_Rank
1153 /** Filters users based on their rank in the corps
1154 * @param $rank Rank we are looking for (abbreviation)
1156 class UFC_Corps_Rank
extends UserFilterCondition
1161 public function __construct($rank, $id = null
)
1163 $this->rank
= $rank;
1167 public function buildCondition(PlFilter
$uf)
1169 /** Tables shortcuts:
1170 * pc for profile_corps
1171 * pcr for profile_corps_rank
1173 $sub = $uf->addCorpsRankFilter();
1174 if (is_null($this->id
)) {
1175 $cond = $sub . '.abbreviation = ' . $this->rank
;
1177 $cond = $sub . '.id = ' . $this->id
;
1179 // XXX(x2006barrois): find a way to get rid of that hardcoded
1180 // reference to 'pc'.
1181 $cond .= ' AND ' . $uf->getVisibilityConditionForField('pc.corps_pub');
1186 // {{{ class UFC_Job_Company
1187 /** Filters users based on the company they belong to
1188 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
1189 * @param $value The searched value
1191 class UFC_Job_Company
extends UserFilterCondition
1194 const JOBNAME
= 'name';
1195 const JOBACRONYM
= 'acronym';
1200 public function __construct($type, $value)
1202 $this->assertType($type);
1203 $this->type
= $type;
1204 $this->value
= $value;
1207 private function assertType($type)
1209 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
1210 Platal
::page()->killError("Type de recherche non valide.");
1214 public function buildCondition(PlFilter
$uf)
1216 $sub = $uf->addJobCompanyFilter();
1217 $cond = $sub . '.' . $this->type
. XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1218 $jsub = $uf->addJobFilter();
1219 $cond .= ' AND ' . $uf->getVisibilityConditionForField($jsub . '.pub');
1224 // {{{ class UFC_Job_Terms
1225 /** Filters users based on the job terms they assigned to one of their
1227 * @param $val The ID of the job term, or an array of such IDs
1229 class UFC_Job_Terms
extends UserFilterCondition
1233 public function __construct($val)
1235 if (!is_array($val)) {
1241 public function buildCondition(PlFilter
$uf)
1243 $sub = $uf->addJobTermsFilter(count($this->val
));
1244 $conditions = array();
1245 foreach ($this->val
as $i => $jtid) {
1246 $conditions[] = $sub[$i] . '.jtid_1 = ' . XDB
::escape($jtid);
1248 $jsub = $uf->addJobFilter();
1249 $conditions[] = $uf->getVisibilityConditionForField($jsub . '.pub');
1250 return implode(' AND ', $conditions);
1254 // {{{ class UFC_Job_Description
1255 /** Filters users based on their job description
1256 * @param $description The text being searched for
1257 * @param $fields The fields to search for (CV, user-defined)
1259 class UFC_Job_Description
extends UserFilterCondition
1262 private $description;
1265 public function __construct($description, $fields)
1267 $this->fields
= $fields;
1268 $this->description
= $description;
1271 public function buildCondition(PlFilter
$uf)
1275 $jsub = $uf->addJobFilter();
1276 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
1277 $conds[] = $jsub . '.description ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1279 if ($this->fields
& UserFilter
::JOB_CV
) {
1280 $uf->requireProfiles();
1282 $conds[] = '( ' . $uf->getVisibilityConditionAbsolute(Visibility
::EXPORT_PRIVATE
) . ' AND p.cv ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
) . ')';
1284 if (count($conds) == 0) {
1285 return self
::COND_TRUE
;
1287 return $uf->getVisibilityConditionForField($jsub . '.pub') . ' AND ( ' . implode(' OR ', $conds) . ' )';
1291 // {{{ class UFC_Networking
1292 /** Filters users based on network identity (IRC, ...)
1293 * @param $type Type of network (-1 for any)
1294 * @param $value Value to search
1296 class UFC_Networking
extends UserFilterCondition
1301 public function __construct($type, $value)
1303 $this->type
= $type;
1304 $this->value
= $value;
1307 public function buildCondition(PlFilter
$uf)
1309 $sub = $uf->addNetworkingFilter();
1311 $conds[] = $uf->getVisibilityConditionForField($sub . '.pub');
1312 $conds[] = $sub . '.address ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1313 if ($this->type
!= -1) {
1314 $conds[] = $sub . '.nwid = ' . XDB
::format('{?}', $this->type
);
1316 return implode(' AND ', $conds);
1320 // {{{ class UFC_Phone
1321 /** Filters users based on their phone number
1322 * @param $num_type Type of number (pro/user/home)
1323 * @param $phone_type Type of phone (fixed/mobile/fax)
1324 * @param $number Phone number
1326 class UFC_Phone
extends UserFilterCondition
1328 const NUM_PRO
= 'pro';
1329 const NUM_USER
= 'user';
1330 const NUM_HOME
= 'address';
1331 const NUM_ANY
= 'any';
1333 const PHONE_FIXED
= 'fixed';
1334 const PHONE_MOBILE
= 'mobile';
1335 const PHONE_FAX
= 'fax';
1336 const PHONE_ANY
= 'any';
1339 private $phone_type;
1342 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
1344 $phone = new Phone(array('display' => $number));
1346 $this->number
= $phone->search
;
1347 $this->num_type
= $num_type;
1348 $this->phone_type
= $phone_type;
1351 public function buildCondition(PlFilter
$uf)
1353 $sub = $uf->addPhoneFilter();
1356 $conds[] = $uf->getVisibilityConditionForField($sub . '.pub');
1358 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
1359 if ($this->num_type
!= self
::NUM_ANY
) {
1360 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
1362 if ($this->phone_type
!= self
::PHONE_ANY
) {
1363 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
1365 return implode(' AND ', $conds);
1369 // {{{ class UFC_Medal
1370 /** Filters users based on their medals
1371 * @param $medal ID of the medal
1372 * @param $grade Grade of the medal (null for 'any')
1374 class UFC_Medal
extends UserFilterCondition
1379 public function __construct($medal, $grade = null
)
1381 $this->medal
= $medal;
1382 $this->grade
= $grade;
1385 public function buildCondition(PlFilter
$uf)
1389 // This will require profiles => table 'p' will be available.
1390 $sub = $uf->addMedalFilter();
1392 $conds[] = $uf->getVisibilityConditionForField('p.medals_pub');
1394 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
1395 if ($this->grade
!= null
) {
1396 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
1398 return implode(' AND ', $conds);
1402 // {{{ class UFC_Photo
1403 /** Filters profiles with photo
1405 class UFC_Photo
extends UserFilterCondition
1407 public function buildCondition(PlFilter
$uf)
1409 $sub = $uf->addPhotoFilter();
1410 return $sub . '.attach IS NOT NULL AND ' . $uf->getVisibilityConditionForField($sub . '.pub');
1414 // {{{ class UFC_Mentor
1415 class UFC_Mentor
extends UserFilterCondition
1417 public function buildCondition(PlFilter
$uf)
1419 $sub = $uf->addMentorFilter(UserFilter
::MENTOR
);
1420 return $sub . '.expertise IS NOT NULL';
1424 // {{{ class UFC_Mentor_Expertise
1425 /** Filters users by mentoring expertise
1426 * @param $expertise Domain of expertise
1428 class UFC_Mentor_Expertise
extends UserFilterCondition
1432 public function __construct($expertise)
1434 $this->expertise
= $expertise;
1437 public function buildCondition(PlFilter
$uf)
1439 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_EXPERTISE
);
1440 return $sub . '.expertise ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->expertise
);
1444 // {{{ class UFC_Mentor_Country
1445 /** Filters users by mentoring country
1446 * @param $country Two-letters code of country being searched
1448 class UFC_Mentor_Country
extends UserFilterCondition
1452 public function __construct()
1454 $this->country
= pl_flatten(func_get_args());
1457 public function buildCondition(PlFilter
$uf)
1459 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_COUNTRY
);
1460 return $sub . '.country IN ' . XDB
::format('{?}', $this->country
);
1464 // {{{ class UFC_Mentor_Terms
1465 /** Filters users based on the job terms they used in mentoring.
1466 * @param $val The ID of the job term, or an array of such IDs
1468 class UFC_Mentor_Terms
extends UserFilterCondition
1472 public function __construct($val)
1477 public function buildCondition(PlFilter
$uf)
1479 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_TERM
);
1480 return $sub . '.jtid_1 = ' . XDB
::escape($this->val
);
1484 // {{{ class UFC_UserRelated
1485 /** Filters users based on a relation toward a user
1486 * @param $user User to which searched users are related
1488 abstract class UFC_UserRelated
extends UserFilterCondition
1491 public function __construct(PlUser
$user)
1493 $this->user
=& $user;
1497 // {{{ class UFC_DeltaTen
1498 class UFC_DeltaTen
extends UserFilterCondition
1500 public function buildCondition(PlFilter
$uf)
1502 $sub = $uf->addDeltaTenFilter(UserFilter
::DELTATEN
);
1503 return $sub . '.message IS NOT NULL';
1507 // {{{ class UFC_DeltaTen_Message
1508 /** Filters users by deltaten message
1509 * @param $message Message for the DeltaTen program
1511 class UFC_DeltaTen_Message
extends UserFilterCondition
1515 public function __construct($message)
1517 $this->message
= $message;
1520 public function buildCondition(PlFilter
$uf)
1522 $sub = $uf->addDeltaTenFilter(UserFilter
::DELTATEN_MESSAGE
);
1523 return $sub . '.message ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->message
);
1527 // {{{ class UFC_Contact
1528 /** Filters users who belong to selected user's contacts
1530 class UFC_Contact
extends UFC_UserRelated
1532 public function buildCondition(PlFilter
$uf)
1534 $sub = $uf->addContactFilter($this->user
->id());
1535 return 'c' . $sub . '.contact IS NOT NULL';
1539 // {{{ class UFC_WatchRegistration
1540 /** Filters users being watched by selected user
1542 class UFC_WatchRegistration
extends UFC_UserRelated
1544 public function buildCondition(PlFilter
$uf)
1546 if (!$this->user
->watchType('registration')) {
1547 return PlFilterCondition
::COND_FALSE
;
1549 $uids = $this->user
->watchUsers();
1550 if (count($uids) == 0) {
1551 return PlFilterCondition
::COND_FALSE
;
1553 return XDB
::format('$UID IN {?}', $uids);
1558 // {{{ class UFC_WatchPromo
1559 /** Filters users belonging to a promo watched by selected user
1560 * @param $user Selected user (the one watching promo)
1561 * @param $grade Formation the user is watching
1563 class UFC_WatchPromo
extends UFC_UserRelated
1566 public function __construct(PlUser
$user, $grade = UserFilter
::GRADE_ING
)
1568 parent
::__construct($user);
1569 $this->grade
= $grade;
1572 public function buildCondition(PlFilter
$uf)
1574 $promos = $this->user
->watchPromos();
1575 if (count($promos) == 0) {
1576 return PlFilterCondition
::COND_FALSE
;
1578 $sube = $uf->addEducationFilter(true
, $this->grade
);
1579 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
1580 return XDB
::format($field . ' IN {?}', $promos);
1585 // {{{ class UFC_WatchGroup
1586 /** Filters users belonging to a group watched by selected user
1587 * @param $user Selected user (the one watching group)
1589 class UFC_WatchGroup
extends UFC_UserRelated
1591 public function buildCondition(PlFilter
$uf)
1593 $groups = $this->user
->watchGroups();
1594 if (count($groups) == 0) {
1595 return PlFilterCondition
::COND_FALSE
;
1597 $conditions = array();
1598 foreach ($groups as $group) {
1599 $sub = $uf->addGroupFilter($group);
1600 $conditions[] = 'gpm' . $sub . '.perms IS NOT NULL';
1602 return implode(' OR ', $conditions);
1606 // {{{ class UFC_WatchContact
1607 /** Filters users watched by selected user
1609 class UFC_WatchContact
extends UFC_Contact
1611 public function buildCondition(PlFilter
$uf)
1613 if (!$this->user
->watchContacts()) {
1614 return PlFilterCondition
::COND_FALSE
;
1616 return parent
::buildCondition($uf);
1620 // {{{ class UFC_MarketingHash
1621 /** Filters users using the hash generated
1622 * to send marketing emails to him.
1624 class UFC_MarketingHash
extends UserFilterCondition
1628 public function __construct($hash)
1630 $this->hash
= $hash;
1633 public function buildCondition(PlFilter
$uf)
1635 $table = $uf->addMarketingHash();
1636 return XDB
::format('rm.hash = {?}', $this->hash
);
1640 // {{{ class UFC_PartnerSharing
1641 /** Filters users, keeping only those sharing data with a given partner.
1643 class UFC_PartnerSharing
extends UserFilterCondition
1647 private $partner_id;
1649 public function __construct($partner_id)
1651 $this->partner_id
= $partner_id;
1654 public function buildCondition(PlFilter
$uf)
1656 $sub = $uf->addPartnerSharingFilter($this->partner_id
);
1657 return XDB
::format("$sub.exposed_uid IS NOT NULL");
1661 // {{{ class UFC_PartnerSharingEmail
1662 /** Filters users, keeping only those allowing emails to be sent by
1665 class UFC_PartnerSharingEmail
extends UserFilterCondition
1667 private $partner_id;
1669 public function __construct($partner_id)
1671 $this->partner_id
= $partner_id;
1674 public function buildCondition(PlFilter
$uf)
1676 $sub = $uf->addPartnerSharingFilter($this->partner_id
);
1677 return XDB
::format("$sub.allow_email IN ('digest', 'direct')");
1681 // {{{ class UFC_PartnerSharingID
1682 /** Filters users according to a list of partner-known IDs
1684 class UFC_PartnerSharingID
extends UserFilterCondition
1686 private $partner_id;
1689 public function __construct($partner_id)
1691 $this->partner_id
= $partner_id;
1692 $ids = func_get_args();
1694 $this->ids
= pl_flatten($ids);
1697 public function buildCondition(PlFilter
$uf)
1699 $uf->requireProfiles();
1701 $sub = $uf->addPartnerSharingFilter($this->partner_id
);
1702 return XDB
::format("$sub.exposed_uid IN {?}", $ids);
1707 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: