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->getVisibilityCondition('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_Name
587 /** Filters users based on name
588 * @param $type Type of name field on which filtering is done (firstname, lastname...)
589 * @param $text Text on which to filter
590 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
592 class UFC_Name
extends UserFilterCondition
594 const EXACT
= XDB
::WILDCARD_EXACT
; // 0x000
595 const PREFIX
= XDB
::WILDCARD_PREFIX
; // 0x001
596 const SUFFIX
= XDB
::WILDCARD_SUFFIX
; // 0x002
597 const CONTAINS
= XDB
::WILDCARD_CONTAINS
; // 0x003
598 const PARTICLE
= 0x004;
599 const VARIANTS
= 0x008;
605 public function __construct($type, $text, $mode)
612 private function buildNameQuery($type, $variant, $where, UserFilter
$uf)
614 $sub = $uf->addNameFilter($type, $variant);
615 return str_replace('$ME', 'pn' . $sub, $where);
618 public function buildCondition(PlFilter
$uf)
621 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
622 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
624 $right = XDB
::formatWildcards($this->mode
& self
::CONTAINS
, $this->text
);
626 $cond = $left . $right;
627 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
628 if (($this->mode
& self
::VARIANTS
) != 0 && isset(Profile
::$name_variants[$this->type
])) {
629 foreach (Profile
::$name_variants[$this->type
] as $var) {
630 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
633 return implode(' OR ', $conds);
636 public function export()
638 $export = $this->buildExport($this->type
);
639 if ($this->mode
& self
::VARIANTS
) {
640 $export['search_in_variants'] = true
;
642 if ($this->mode
& self
::PARTICLE
) {
643 $export['search_in_particle'] = true
;
645 $export['comparison'] = self
::comparisonFromXDBWildcard($this->mode
& 0x3);
646 $export['text'] = $this->text
;
651 // {{{ class UFC_NameTokens
652 /** Selects users based on tokens in their name (for quicksearch)
653 * @param $tokens An array of tokens to search
654 * @param $flags Flags the tokens must have (e.g 'public' for public search)
655 * @param $soundex (bool) Whether those tokens are fulltext or soundex
657 class UFC_NameTokens
extends UserFilterCondition
660 const FLAG_PUBLIC
= 'public';
666 private $general_type;
668 public function __construct($tokens, $flags = array(), $soundex = false
, $exact = false
, $general_type = '')
670 if (is_array($tokens)) {
671 $this->tokens
= $tokens;
673 $this->tokens
= array($tokens);
675 if (is_array($flags)) {
676 $this->flags
= $flags;
678 $this->flags
= array($flags);
680 $this->soundex
= $soundex;
681 $this->exact
= $exact;
682 $this->general_type
= $general_type;
685 public function buildCondition(PlFilter
$uf)
688 foreach ($this->tokens
as $i => $token) {
689 $sub = $uf->addNameTokensFilter($token);
690 if ($this->soundex
) {
691 $c = XDB
::format($sub . '.soundex = {?}', soundex_fr($token));
692 } else if ($this->exact
) {
693 $c = XDB
::format($sub . '.token = {?}', $token);
695 $c = $sub . '.token ' . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $token);
697 if ($this->flags
!= null
) {
698 $c .= XDB
::format(' AND ' . $sub . '.flags IN {?}', $this->flags
);
700 if ($this->general_type
) {
701 $c .= XDB
::format(' AND ' . $sub . '.general_type = {?}', $this->general_type
);
706 return implode(' AND ', $conds);
710 // {{{ class UFC_Nationality
711 class UFC_Nationality
extends UserFilterCondition
715 public function __construct()
717 $this->val
= pl_flatten(func_get_args());
720 public function buildCondition(PlFilter
$uf)
722 $uf->requireProfiles();
723 $nat = XDB
::formatArray($this->val
);
725 'p.nationality1 IN ' . $nat,
726 'p.nationality2 IN ' . $nat,
727 'p.nationality3 IN ' . $nat,
729 return implode(' OR ', $conds);
733 // {{{ class UFC_Dead
734 /** Filters users based on death date
735 * @param $comparison Comparison operator
736 * @param $date Date to which death date should be compared (DateTime object, string or timestamp)
738 class UFC_Dead
extends UserFilterCondition
743 public function __construct($comparison = null
, $date = null
)
745 $this->comparison
= $comparison;
746 $this->date
= make_datetime($date);
749 public function buildCondition(PlFilter
$uf)
751 $uf->requireProfiles();
752 $str = 'p.deathdate IS NOT NULL';
753 if (!is_null($this->comparison
)) {
754 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->date
->format('Y-m-d'));
760 // {{{ class UFC_Registered
761 /** Filters users based on registration state
762 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
763 * @param $comparison Comparison operator
764 * @param $date Date to which users registration date should be compared
766 class UFC_Registered
extends UserFilterCondition
772 public function __construct($active = false
, $comparison = null
, $date = null
)
774 $this->active
= $active;
775 $this->comparison
= $comparison;
776 $this->date
= make_datetime($date);
779 public function buildCondition(PlFilter
$uf)
781 $uf->requireAccounts();
783 $date = '$UID IS NOT NULL AND a.state = \'active\'';
785 $date = '$UID IS NOT NULL AND a.state != \'pending\'';
787 if (!is_null($this->comparison
)) {
788 $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'));
794 // {{{ class UFC_ProfileUpdated
795 /** Filters users based on profile update date
796 * @param $comparison Comparison operator
797 * @param $date Date to which profile update date must be compared
799 class UFC_ProfileUpdated
extends UserFilterCondition
804 public function __construct($comparison = null
, $date = null
)
806 $this->comparison
= $comparison;
810 public function buildCondition(PlFilter
$uf)
812 $uf->requireProfiles();
813 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
817 // {{{ class UFC_Birthday
818 /** Filters users based on next birthday date
819 * @param $comparison Comparison operator
820 * @param $date Date to which users next birthday date should be compared
822 class UFC_Birthday
extends UserFilterCondition
827 public function __construct($comparison = null
, $date = null
)
829 $this->comparison
= $comparison;
833 public function buildCondition(PlFilter
$uf)
835 $uf->requireProfiles();
836 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
841 /** Filters users based on sex
842 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
844 class UFC_Sex
extends UserFilterCondition
847 public function __construct($sex)
852 public function buildCondition(PlFilter
$uf)
854 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
855 return self
::COND_FALSE
;
857 $uf->requireProfiles();
858 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
863 // {{{ class UFC_NLSubscribed
864 /** Filters users based on NL subscription
865 * @param $nlid NL whose subscribers we are selecting
866 * @param $issue Select only subscribers who have not yet received that issue
868 class UFC_NLSubscribed
extends UserFilterCondition
872 public function __construct($nlid, $issue_id)
875 $this->issue_id
= $issue_id;
878 public function buildCondition(PlFilter
$uf)
880 $sub = $uf->addNewsLetterFilter($this->nlid
);
881 return XDB
::format($sub . '.last < {?}', $this->issue_id
);
885 // {{{ class UFC_Group
886 /** Filters users based on group membership
887 * @param $group Group whose members we are selecting
888 * @param $anim Whether to restrict selection to animators of that group
890 class UFC_Group
extends UserFilterCondition
894 public function __construct($group, $anim = false
)
896 $this->group
= $group;
900 public function buildCondition(PlFilter
$uf)
902 // Groups are only visible for users with perm 'groups'.
903 if (!S
::user()->checkPerms(User
::PERM_GROUPS
)) {
904 return self
::COND_FALSE
;
906 $sub = $uf->addGroupFilter($this->group
);
907 $where = 'gpm' . $sub . '.perms IS NOT NULL';
909 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
915 // {{{ class UFC_Binet
916 /** Selects users based on their belonging to a given (list of) binet
917 * @param $binet either a binet_id or an array of binet_ids
919 class UFC_Binet
extends UserFilterCondition
923 public function __construct()
925 $this->val
= pl_flatten(func_get_args());
928 public function buildCondition(PlFilter
$uf)
930 // Binets are private.
931 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
932 return self
::COND_TRUE
;
934 $sub = $uf->addBinetsFilter();
935 return XDB
::format($sub . '.binet_id IN {?}', $this->val
);
939 // {{{ class UFC_Section
940 /** Selects users based on section
941 * @param $section ID of the section
943 class UFC_Section
extends UserFilterCondition
947 public function __construct()
949 $this->section
= pl_flatten(func_get_args());
952 public function buildCondition(PlFilter
$uf)
954 // Sections are private.
955 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
956 return self
::COND_TRUE
;
958 $uf->requireProfiles();
959 return XDB
::format('p.section IN {?}', $this->section
);
963 // {{{ class UFC_Email
964 /** Filters users based on an email or a list of emails
965 * @param $emails List of emails whose owner must be selected
967 class UFC_Email
extends UserFilterCondition
970 public function __construct()
972 $this->emails
= pl_flatten(func_get_args());
975 public function buildCondition(PlFilter
$uf)
981 if (count($this->emails
) == 0) {
982 return PlFilterCondition
::COND_TRUE
;
985 foreach ($this->emails
as $entry) {
986 if (User
::isForeignEmailAddress($entry)) {
989 list($local_part, ) = explode('@', $entry);
990 $local[] = $local_part;
994 if (count($foreign) > 0) {
995 $sub = $uf->addEmailRedirectFilter($foreign);
996 $cond[] = XDB
::format('ra' . $sub . '.redirect IS NOT NULL OR ra' . $sub . '.redirect IN {?}', $foreign);
998 if (count($local) > 0) {
999 $sub = $uf->addAliasFilter($local);
1000 $cond[] = 'sa' . $sub . '.email IS NOT NULL';
1002 return '(' . implode(') OR (', $cond) . ')';
1006 // {{{ class UFC_Address
1007 abstract class UFC_Address
extends UserFilterCondition
1009 /** Valid address type ('hq' is reserved for company addresses)
1011 const TYPE_HOME
= 1;
1015 /** Text for these types
1017 protected static $typetexts = array(
1018 self
::TYPE_HOME
=> 'home',
1019 self
::TYPE_PRO
=> 'pro',
1024 /** Flags for addresses
1026 const FLAG_CURRENT
= 0x0001;
1027 const FLAG_TEMP
= 0x0002;
1028 const FLAG_SECOND
= 0x0004;
1029 const FLAG_MAIL
= 0x0008;
1030 const FLAG_CEDEX
= 0x0010;
1032 // Binary OR of those flags
1033 const FLAG_ANY
= 0x001F;
1035 /** Text of these flags
1037 protected static $flagtexts = array(
1038 self
::FLAG_CURRENT
=> 'current',
1039 self
::FLAG_TEMP
=> 'temporary',
1040 self
::FLAG_SECOND
=> 'secondary',
1041 self
::FLAG_MAIL
=> 'mail',
1042 self
::FLAG_CEDEX
=> 'cedex',
1047 public function __construct($type = null
, $flags = null
)
1049 $this->type
= $type;
1050 $this->flags
= $flags;
1053 protected function initConds($sub, $vis_cond)
1055 $conds = array($vis_cond);
1058 foreach (self
::$typetexts as $flag => $type) {
1059 if ($flag & $this->type
) {
1063 if (count($types)) {
1064 $conds[] = XDB
::format($sub . '.type IN {?}', $types);
1067 if ($this->flags
!= self
::FLAG_ANY
) {
1068 foreach(self
::$flagtexts as $flag => $text) {
1069 if ($flag & $this->flags
) {
1070 $conds[] = 'FIND_IN_SET(' . XDB
::format('{?}', $text) . ', ' . $sub . '.flags)';
1079 // {{{ class UFC_AddressText
1080 /** Select users based on their address, using full text search
1081 * @param $text Text for filter in fulltext search
1082 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
1083 * @param $type Filter on address type
1084 * @param $flags Filter on address flags
1085 * @param $country Filter on address country
1086 * @param $locality Filter on address locality
1088 class UFC_AddressText
extends UFC_Address
1092 private $textSearchMode;
1094 public function __construct($text = null
, $textSearchMode = XDB
::WILDCARD_CONTAINS
,
1095 $type = null
, $flags = self
::FLAG_ANY
, $country = null
, $locality = null
)
1097 parent
::__construct($type, $flags);
1098 $this->text
= $text;
1099 $this->textSearchMode
= $textSearchMode;
1100 $this->country
= $country;
1101 $this->locality
= $locality;
1104 private function mkMatch($txt)
1106 return XDB
::formatWildcards($this->textSearchMode
, $txt);
1109 public function buildCondition(PlFilter
$uf)
1111 $sub = $uf->addAddressFilter();
1112 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1113 if ($this->text
!= null
) {
1114 $conds[] = $sub . '.text' . $this->mkMatch($this->text
);
1117 if ($this->country
!= null
) {
1118 $subc = $uf->addAddressCountryFilter();
1119 $subconds = array();
1120 $subconds[] = $subc . '.country' . $this->mkMatch($this->country
);
1121 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country
);
1122 $conds[] = implode(' OR ', $subconds);
1125 if ($this->locality
!= null
) {
1126 $subl = $uf->addAddressLocalityFilter();
1127 $conds[] = $subl . '.name' . $this->mkMatch($this->locality
);
1130 return implode(' AND ', $conds);
1134 // {{{ class UFC_AddressField
1135 /** Filters users based on their address,
1136 * @param $val Either a code for one of the fields, or an array of such codes
1137 * @param $fieldtype The type of field to look for
1138 * @param $type Filter on address type
1139 * @param $flags Filter on address flags
1141 class UFC_AddressField
extends UFC_Address
1143 const FIELD_COUNTRY
= 1;
1144 const FIELD_ADMAREA
= 2;
1145 const FIELD_SUBADMAREA
= 3;
1146 const FIELD_LOCALITY
= 4;
1147 const FIELD_ZIPCODE
= 5;
1149 /** Data of the filter
1154 public function __construct($val, $fieldtype, $type = null
, $flags = self
::FLAG_ANY
)
1156 parent
::__construct($type, $flags);
1158 if (!is_array($val)) {
1162 $this->fieldtype
= $fieldtype;
1165 public function buildCondition(PlFilter
$uf)
1167 $sub = $uf->addAddressFilter();
1168 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1170 switch ($this->fieldtype
) {
1171 case self
::FIELD_COUNTRY
:
1172 $field = 'countryId';
1174 case self
::FIELD_ADMAREA
:
1175 $field = 'administrativeAreaId';
1177 case self
::FIELD_SUBADMAREA
:
1178 $field = 'subAdministrativeAreaId';
1180 case self
::FIELD_LOCALITY
:
1181 $field = 'localityId';
1183 case self
::FIELD_ZIPCODE
:
1184 $field = 'postalCode';
1187 Platal
::page()->killError('Invalid address field type: ' . $this->fieldtype
);
1189 $conds[] = XDB
::format($sub . '.' . $field . ' IN {?}', $this->val
);
1191 return implode(' AND ', $conds);
1195 // {{{ class UFC_Corps
1196 /** Filters users based on the corps they belong to
1197 * @param $corps Corps we are looking for (abbreviation)
1198 * @param $type Whether we search for original or current corps
1200 class UFC_Corps
extends UserFilterCondition
1209 public function __construct($corps, $id = null
, $type = self
::CURRENT
)
1211 $this->corps
= $corps;
1213 $this->type
= $type;
1216 public function buildCondition(PlFilter
$uf)
1218 /** Tables shortcuts:
1219 * pc for profile_corps,
1220 * pceo for profile_corps_enum - orginal
1221 * pcec for profile_corps_enum - current
1223 $sub = $uf->addCorpsFilter($this->type
);
1224 if (is_null($this->id
)) {
1225 $cond = $sub . '.abbreviation = ' . $this->corps
;
1227 $cond = $sub . '.id = ' . $this->id
;
1229 // XXX(x2006barrois): find a way to get rid of that hardcoded
1230 // reference to 'pc'.
1231 $cond .= ' AND ' . $uf->getVisibilityCondition('pc.corps_pub');
1236 // {{{ class UFC_Corps_Rank
1237 /** Filters users based on their rank in the corps
1238 * @param $rank Rank we are looking for (abbreviation)
1240 class UFC_Corps_Rank
extends UserFilterCondition
1245 public function __construct($rank, $id = null
)
1247 $this->rank
= $rank;
1251 public function buildCondition(PlFilter
$uf)
1253 /** Tables shortcuts:
1254 * pc for profile_corps
1255 * pcr for profile_corps_rank
1257 $sub = $uf->addCorpsRankFilter();
1258 if (is_null($this->id
)) {
1259 $cond = $sub . '.abbreviation = ' . $this->rank
;
1261 $cond = $sub . '.id = ' . $this->id
;
1263 // XXX(x2006barrois): find a way to get rid of that hardcoded
1264 // reference to 'pc'.
1265 $cond .= ' AND ' . $uf->getVisibilityCondition('pc.corps_pub');
1270 // {{{ class UFC_Job_Company
1271 /** Filters users based on the company they belong to
1272 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
1273 * @param $value The searched value
1275 class UFC_Job_Company
extends UserFilterCondition
1278 const JOBNAME
= 'name';
1279 const JOBACRONYM
= 'acronym';
1284 public function __construct($type, $value)
1286 $this->assertType($type);
1287 $this->type
= $type;
1288 $this->value
= $value;
1291 private function assertType($type)
1293 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
1294 Platal
::page()->killError("Type de recherche non valide.");
1298 public function buildCondition(PlFilter
$uf)
1300 $sub = $uf->addJobCompanyFilter();
1301 $cond = $sub . '.' . $this->type
. XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1302 $jsub = $uf->addJobFilter();
1303 $cond .= ' AND ' . $uf->getVisibilityCondition($jsub . '.pub');
1308 // {{{ class UFC_Job_Terms
1309 /** Filters users based on the job terms they assigned to one of their
1311 * @param $val The ID of the job term, or an array of such IDs
1313 class UFC_Job_Terms
extends UserFilterCondition
1317 public function __construct($val)
1319 if (!is_array($val)) {
1325 public function buildCondition(PlFilter
$uf)
1327 $sub = $uf->addJobTermsFilter(count($this->val
));
1328 $conditions = array();
1329 foreach ($this->val
as $i => $jtid) {
1330 $conditions[] = $sub[$i] . '.jtid_1 = ' . XDB
::escape($jtid);
1332 $jsub = $uf->addJobFilter();
1333 $conditions[] = $uf->getVisibilityCondition($jsub . '.pub');
1334 return implode(' AND ', $conditions);
1338 // {{{ class UFC_Job_Description
1339 /** Filters users based on their job description
1340 * @param $description The text being searched for
1341 * @param $fields The fields to search for (CV, user-defined)
1343 class UFC_Job_Description
extends UserFilterCondition
1346 private $description;
1349 public function __construct($description, $fields)
1351 $this->fields
= $fields;
1352 $this->description
= $description;
1355 public function buildCondition(PlFilter
$uf)
1359 $jsub = $uf->addJobFilter();
1360 // CV is private => if only CV requested, and not private,
1361 // don't do anything. Otherwise restrict to standard job visibility.
1362 if ($this->fields
== UserFilter
::JOB_CV
) {
1363 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
1364 return self
::COND_TRUE
;
1367 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
1368 $conds[] = $jsub . '.description ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1370 if ($this->fields
& UserFilter
::JOB_CV
&& $uf->getVisibilityLevel() == ProfileVisibility
::VIS_PRIVATE
) {
1371 $uf->requireProfiles();
1372 $conds[] = 'p.cv ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1374 if (count($conds) == 0) {
1375 return self
::COND_TRUE
;
1377 return $uf->getVisibilityCondition($jsub . '.pub') . ' AND ( ' . implode(' OR ', $conds) . ' )';
1381 // {{{ class UFC_Networking
1382 /** Filters users based on network identity (IRC, ...)
1383 * @param $type Type of network (-1 for any)
1384 * @param $value Value to search
1386 class UFC_Networking
extends UserFilterCondition
1391 public function __construct($type, $value)
1393 $this->type
= $type;
1394 $this->value
= $value;
1397 public function buildCondition(PlFilter
$uf)
1399 $sub = $uf->addNetworkingFilter();
1401 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1402 $conds[] = $sub . '.address ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1403 if ($this->type
!= -1) {
1404 $conds[] = $sub . '.nwid = ' . XDB
::format('{?}', $this->type
);
1406 return implode(' AND ', $conds);
1410 // {{{ class UFC_Phone
1411 /** Filters users based on their phone number
1412 * @param $num_type Type of number (pro/user/home)
1413 * @param $phone_type Type of phone (fixed/mobile/fax)
1414 * @param $number Phone number
1416 class UFC_Phone
extends UserFilterCondition
1418 const NUM_PRO
= 'pro';
1419 const NUM_USER
= 'user';
1420 const NUM_HOME
= 'address';
1421 const NUM_ANY
= 'any';
1423 const PHONE_FIXED
= 'fixed';
1424 const PHONE_MOBILE
= 'mobile';
1425 const PHONE_FAX
= 'fax';
1426 const PHONE_ANY
= 'any';
1429 private $phone_type;
1432 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
1434 $phone = new Phone(array('display' => $number));
1436 $this->number
= $phone->search
;
1437 $this->num_type
= $num_type;
1438 $this->phone_type
= $phone_type;
1441 public function buildCondition(PlFilter
$uf)
1443 $sub = $uf->addPhoneFilter();
1446 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1448 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
1449 if ($this->num_type
!= self
::NUM_ANY
) {
1450 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
1452 if ($this->phone_type
!= self
::PHONE_ANY
) {
1453 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
1455 return implode(' AND ', $conds);
1459 // {{{ class UFC_Medal
1460 /** Filters users based on their medals
1461 * @param $medal ID of the medal
1462 * @param $grade Grade of the medal (null for 'any')
1464 class UFC_Medal
extends UserFilterCondition
1469 public function __construct($medal, $grade = null
)
1471 $this->medal
= $medal;
1472 $this->grade
= $grade;
1475 public function buildCondition(PlFilter
$uf)
1479 // This will require profiles => table 'p' will be available.
1480 $sub = $uf->addMedalFilter();
1482 $conds[] = $uf->getVisibilityCondition('p.medals_pub');
1484 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
1485 if ($this->grade
!= null
) {
1486 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
1488 return implode(' AND ', $conds);
1492 // {{{ class UFC_Photo
1493 /** Filters profiles with photo
1495 class UFC_Photo
extends UserFilterCondition
1497 public function buildCondition(PlFilter
$uf)
1499 $sub = $uf->addPhotoFilter();
1500 return $sub . '.attach IS NOT NULL AND ' . $uf->getVisibilityCondition($sub . '.pub');
1504 // {{{ class UFC_Mentor
1505 class UFC_Mentor
extends UserFilterCondition
1507 public function buildCondition(PlFilter
$uf)
1509 $sub = $uf->addMentorFilter(UserFilter
::MENTOR
);
1510 return $sub . '.expertise IS NOT NULL';
1514 // {{{ class UFC_Mentor_Expertise
1515 /** Filters users by mentoring expertise
1516 * @param $expertise Domain of expertise
1518 class UFC_Mentor_Expertise
extends UserFilterCondition
1522 public function __construct($expertise)
1524 $this->expertise
= $expertise;
1527 public function buildCondition(PlFilter
$uf)
1529 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_EXPERTISE
);
1530 return $sub . '.expertise ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->expertise
);
1534 // {{{ class UFC_Mentor_Country
1535 /** Filters users by mentoring country
1536 * @param $country Two-letters code of country being searched
1538 class UFC_Mentor_Country
extends UserFilterCondition
1542 public function __construct()
1544 $this->country
= pl_flatten(func_get_args());
1547 public function buildCondition(PlFilter
$uf)
1549 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_COUNTRY
);
1550 return $sub . '.country IN ' . XDB
::format('{?}', $this->country
);
1554 // {{{ class UFC_Mentor_Terms
1555 /** Filters users based on the job terms they used in mentoring.
1556 * @param $val The ID of the job term, or an array of such IDs
1558 class UFC_Mentor_Terms
extends UserFilterCondition
1562 public function __construct($val)
1567 public function buildCondition(PlFilter
$uf)
1569 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_TERM
);
1570 return $sub . '.jtid_1 = ' . XDB
::escape($this->val
);
1574 // {{{ class UFC_UserRelated
1575 /** Filters users based on a relation toward a user
1576 * @param $user User to which searched users are related
1578 abstract class UFC_UserRelated
extends UserFilterCondition
1581 public function __construct(PlUser
$user)
1583 $this->user
=& $user;
1587 // {{{ class UFC_Contact
1588 /** Filters users who belong to selected user's contacts
1590 class UFC_Contact
extends UFC_UserRelated
1592 public function buildCondition(PlFilter
$uf)
1594 $sub = $uf->addContactFilter($this->user
->id());
1595 return 'c' . $sub . '.contact IS NOT NULL';
1599 // {{{ class UFC_WatchRegistration
1600 /** Filters users being watched by selected user
1602 class UFC_WatchRegistration
extends UFC_UserRelated
1604 public function buildCondition(PlFilter
$uf)
1606 if (!$this->user
->watchType('registration')) {
1607 return PlFilterCondition
::COND_FALSE
;
1609 $uids = $this->user
->watchUsers();
1610 if (count($uids) == 0) {
1611 return PlFilterCondition
::COND_FALSE
;
1613 return XDB
::format('$UID IN {?}', $uids);
1618 // {{{ class UFC_WatchPromo
1619 /** Filters users belonging to a promo watched by selected user
1620 * @param $user Selected user (the one watching promo)
1621 * @param $grade Formation the user is watching
1623 class UFC_WatchPromo
extends UFC_UserRelated
1626 public function __construct(PlUser
$user, $grade = UserFilter
::GRADE_ING
)
1628 parent
::__construct($user);
1629 $this->grade
= $grade;
1632 public function buildCondition(PlFilter
$uf)
1634 $promos = $this->user
->watchPromos();
1635 if (count($promos) == 0) {
1636 return PlFilterCondition
::COND_FALSE
;
1638 $sube = $uf->addEducationFilter(true
, $this->grade
);
1639 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
1640 return XDB
::format($field . ' IN {?}', $promos);
1645 // {{{ class UFC_WatchContact
1646 /** Filters users watched by selected user
1648 class UFC_WatchContact
extends UFC_Contact
1650 public function buildCondition(PlFilter
$uf)
1652 if (!$this->user
->watchContacts()) {
1653 return PlFilterCondition
::COND_FALSE
;
1655 return parent
::buildCondition($uf);
1659 // {{{ class UFC_MarketingHash
1660 /** Filters users using the hash generated
1661 * to send marketing emails to him.
1663 class UFC_MarketingHash
extends UserFilterCondition
1667 public function __construct($hash)
1669 $this->hash
= $hash;
1672 public function buildCondition(PlFilter
$uf)
1674 $table = $uf->addMarketingHash();
1675 return XDB
::format('rm.hash = {?}', $this->hash
);
1680 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: