5bc066886383f553c0bcdb515ed869d1b389a46f
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 $class = 'ufc_' . str_replace('_', '', $type);
171 $cond = new $class();
175 throw new Exception("Unknown condition type: $type");
177 if (is_null($cond)) {
178 throw new Exception("Unsupported $type definition");
180 if ($cond instanceof PFC_NChildren
) {
181 $children = $export->v('children', array());
182 foreach ($children as $child) {
183 $cond->addChild(self
::fromExport($child));
185 } else if ($cond instanceof PFC_OneChild
) {
186 if ($export->has('child')) {
187 $cond->setChild(self
::fromExport($export->v('child')));
194 // {{{ class UFC_HasProfile
195 /** Filters users who have a profile
197 class UFC_HasProfile
extends UserFilterCondition
199 public function buildCondition(PlFilter
$uf)
201 $uf->requireProfiles();
202 return '$PID IS NOT NULL';
205 public function export()
207 return $this->buildExport('has_profile');
211 // {{{ class UFC_AccountType
212 /** Filters users who have one of the given account types
214 class UFC_AccountType
extends UserFilterCondition
218 public function __construct()
220 $this->types
= pl_flatten(func_get_args());
223 public function buildCondition(PlFilter
$uf)
225 $uf->requireAccounts();
226 return XDB
::format('a.type IN {?}', $this->types
);
229 public function export()
231 $export = $this->buildExport('account_type');
232 $export['values'] = $this->types
;
237 // {{{ class UFC_AccountPerm
238 /** Filters users who have one of the given permissions
240 class UFC_AccountPerm
extends UserFilterCondition
244 public function __construct()
246 $this->perms
= pl_flatten(func_get_args());
249 public function buildCondition(PlFilter
$uf)
253 foreach ($this->perms
as $perm) {
254 $conds[] = XDB
::format('FIND_IN_SET({?}, IF(a.user_perms IS NULL, at.perms,
255 CONCAT(at.perms, \',\', a.user_perms)))',
259 return self
::COND_TRUE
;
261 return implode(' OR ', $conds);
265 public function export()
267 $export = $this->buildExport('account_perm');
268 $export['values'] = $this->perms
;
273 // {{{ class UFC_Hruid
274 /** Filters users based on their hruid
275 * @param $val Either an hruid, or a list of those
277 class UFC_Hruid
extends UserFilterCondition
281 public function __construct()
283 $this->hruids
= pl_flatten(func_get_args());
286 public function buildCondition(PlFilter
$uf)
288 $uf->requireAccounts();
289 return XDB
::format('a.hruid IN {?}', $this->hruids
);
292 public function export()
294 $export = $this->buildExport('hruid');
295 $export['values'] = $this->hruids
;
300 // {{{ class UFC_Hrpid
301 /** Filters users based on the hrpid of their profiles
302 * @param $val Either an hrpid, or a list of those
304 class UFC_Hrpid
extends UserFilterCondition
308 public function __construct()
310 $this->hrpids
= pl_flatten(func_get_args());
313 public function buildCondition(PlFilter
$uf)
315 $uf->requireProfiles();
316 return XDB
::format('p.hrpid IN {?}', $this->hrpids
);
319 public function export()
321 $export = $this->buildExport('hrpid');
322 $export['values'] = $this->hrpids
;
328 /** Filters users based on one of their last IPs
329 * @param $ip IP from which connection are checked
331 class UFC_Ip
extends UserFilterCondition
335 public function __construct($ip)
340 public function buildCondition(PlFilter
$uf)
342 $sub = $uf->addLoggerFilter();
343 $ip = ip_to_uint($this->ip
);
344 return XDB
::format($sub . '.ip = {?} OR ' . $sub . '.forward_ip = {?}', $ip, $ip);
347 public function export()
349 $export = $this->buildExport('host');
350 $export['ip'] = $this->ip
;
355 // {{{ class UFC_Comment
356 class UFC_Comment
extends UserFilterCondition
360 public function __construct($text)
365 public function buildCondition(PlFilter
$uf)
367 $uf->requireProfiles();
368 return $uf->getVisibilityCondition('p.freetext_pub') . ' AND p.freetext ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->text
);
371 public function export()
373 $export = $this->buildExport('comment');
374 $export['comparison'] = self
::OP_CONTAINS
;
375 $export['text'] = $this->text
;
380 // {{{ class UFC_Promo
381 /** Filters users based on promotion
382 * @param $comparison Comparison operator (>, =, ...)
383 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
384 * @param $promo Promotion on which the filter is based
386 class UFC_Promo
extends UserFilterCondition
393 public function __construct($comparison, $grade, $promo)
395 $this->grade
= $grade;
396 $this->comparison
= $comparison;
397 $this->promo
= $promo;
398 if ($this->grade
!= UserFilter
::DISPLAY
) {
399 UserFilter
::assertGrade($this->grade
);
401 if ($this->grade
== UserFilter
::DISPLAY
&& $this->comparison
!= '=') {
402 // XXX: we might try to guess the grade from the first char of the promo and forbid only '<= 2004', but allow '<= X2004'
403 Platal
::page()->killError("Il n'est pas possible d'appliquer la comparaison '" . $this->comparison
. "' aux promotions sans spécifier de formation (X/M/D)");
407 public function buildCondition(PlFilter
$uf)
409 if ($this->grade
== UserFilter
::DISPLAY
) {
410 $sub = $uf->addDisplayFilter();
411 return XDB
::format('pd' . $sub . '.promo ' . $this->comparison
. ' {?}', $this->promo
);
413 $sub = $uf->addEducationFilter(true
, $this->grade
);
414 $field = 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
415 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->promo
);
419 public function export()
421 $export = $this->buildExport('promo');
422 $export['comparison'] = $this->comparison
;
423 if ($this->grade
!= UserFilter
::DISPLAY
) {
424 $export['grade'] = $this->grade
;
426 $export['promo'] = $this->promo
;
431 // {{{ class UFC_SchoolId
432 /** Filters users based on their shoold identifier
433 * @param type Parameter type (Xorg, AX, School)
434 * @param value Array of school ids
436 class UFC_SchoolId
extends UserFilterCondition
440 const School
= 'school';
445 static public function assertType($type)
447 if ($type != self
::AX
&& $type != self
::Xorg
&& $type != self
::School
) {
448 Platal
::page()->killError("Type de matricule invalide: $type");
452 /** Construct a UFC_SchoolId
453 * The first argument is the type, all following arguments can be either ids
454 * or arrays of ids to use:
455 * $ufc = new UFC_SchoolId(UFC_SchoolId::AX, $id1, $id2, array($id3, $id4));
457 public function __construct($type)
460 $ids = func_get_args();
462 $this->ids
= pl_flatten($ids);
463 self
::assertType($type);
466 public function buildCondition(PlFilter
$uf)
468 $uf->requireProfiles();
471 if ($type == self
::School
) {
473 $ids = array_map(array('Profile', 'getXorgId'), $ids);
475 return XDB
::format('p.' . $type . '_id IN {?}', $ids);
479 // {{{ class UFC_EducationSchool
480 /** Filters users by formation
481 * @param $val The formation to search (either ID or array of IDs)
483 class UFC_EducationSchool
extends UserFilterCondition
487 public function __construct()
489 $this->val
= pl_flatten(func_get_args());
492 public function buildCondition(PlFilter
$uf)
494 $sub = $uf->addEducationFilter();
495 return XDB
::format('pe' . $sub . '.eduid IN {?}', $this->val
);
499 // {{{ class UFC_EducationDegree
500 class UFC_EducationDegree
extends UserFilterCondition
504 public function __construct()
506 $this->diploma
= pl_flatten(func_get_args());
509 public function buildCondition(PlFilter
$uf)
511 $sub = $uf->addEducationFilter();
512 return XDB
::format('pe' . $sub . '.degreeid IN {?}', $this->diploma
);
516 // {{{ class UFC_EducationField
517 class UFC_EducationField
extends UserFilterCondition
521 public function __construct()
523 $this->val
= pl_flatten(func_get_args());
526 public function buildCondition(PlFilter
$uf)
528 $sub = $uf->addEducationFilter();
529 return XDB
::format('pe' . $sub . '.fieldid IN {?}', $this->val
);
533 // {{{ class UFC_Name
534 /** Filters users based on name
535 * @param $type Type of name field on which filtering is done (firstname, lastname...)
536 * @param $text Text on which to filter
537 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
539 class UFC_Name
extends UserFilterCondition
541 const EXACT
= XDB
::WILDCARD_EXACT
; // 0x000
542 const PREFIX
= XDB
::WILDCARD_PREFIX
; // 0x001
543 const SUFFIX
= XDB
::WILDCARD_SUFFIX
; // 0x002
544 const CONTAINS
= XDB
::WILDCARD_CONTAINS
; // 0x003
545 const PARTICLE
= 0x004;
546 const VARIANTS
= 0x008;
552 public function __construct($type, $text, $mode)
559 private function buildNameQuery($type, $variant, $where, UserFilter
$uf)
561 $sub = $uf->addNameFilter($type, $variant);
562 return str_replace('$ME', 'pn' . $sub, $where);
565 public function buildCondition(PlFilter
$uf)
568 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
569 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
571 $right = XDB
::formatWildcards($this->mode
& self
::CONTAINS
, $this->text
);
573 $cond = $left . $right;
574 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
575 if (($this->mode
& self
::VARIANTS
) != 0 && isset(Profile
::$name_variants[$this->type
])) {
576 foreach (Profile
::$name_variants[$this->type
] as $var) {
577 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
580 return implode(' OR ', $conds);
583 public function export()
585 $export = $this->buildExport($this->type
);
586 if ($this->mode
& self
::VARIANTS
) {
587 $export['search_in_variants'] = true
;
589 if ($this->mode
& self
::PARTICLE
) {
590 $export['search_in_particle'] = true
;
592 $export['comparison'] = self
::comparisonFromXDBWildcard($this->mode
& 0x3);
593 $export['text'] = $this->text
;
598 // {{{ class UFC_NameTokens
599 /** Selects users based on tokens in their name (for quicksearch)
600 * @param $tokens An array of tokens to search
601 * @param $flags Flags the tokens must have (e.g 'public' for public search)
602 * @param $soundex (bool) Whether those tokens are fulltext or soundex
604 class UFC_NameTokens
extends UserFilterCondition
607 const FLAG_PUBLIC
= 'public';
614 public function __construct($tokens, $flags = array(), $soundex = false
, $exact = false
)
616 if (is_array($tokens)) {
617 $this->tokens
= $tokens;
619 $this->tokens
= array($tokens);
621 if (is_array($flags)) {
622 $this->flags
= $flags;
624 $this->flags
= array($flags);
626 $this->soundex
= $soundex;
627 $this->exact
= $exact;
630 public function buildCondition(PlFilter
$uf)
633 foreach ($this->tokens
as $i => $token) {
634 $sub = $uf->addNameTokensFilter($token);
635 if ($this->soundex
) {
636 $c = XDB
::format($sub . '.soundex = {?}', soundex_fr($token));
637 } else if ($this->exact
) {
638 $c = XDB
::format($sub . '.token = {?}', $token);
640 $c = $sub . '.token ' . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $token);
642 if ($this->flags
!= null
) {
643 $c .= XDB
::format(' AND ' . $sub . '.flags IN {?}', $this->flags
);
648 return implode(' AND ', $conds);
652 // {{{ class UFC_Nationality
653 class UFC_Nationality
extends UserFilterCondition
657 public function __construct()
659 $this->val
= pl_flatten(func_get_args());
662 public function buildCondition(PlFilter
$uf)
664 $uf->requireProfiles();
665 $nat = XDB
::formatArray($this->val
);
667 'p.nationality1 IN ' . $nat,
668 'p.nationality2 IN ' . $nat,
669 'p.nationality3 IN ' . $nat,
671 return implode(' OR ', $conds);
675 // {{{ class UFC_Dead
676 /** Filters users based on death date
677 * @param $comparison Comparison operator
678 * @param $date Date to which death date should be compared (DateTime object, string or timestamp)
680 class UFC_Dead
extends UserFilterCondition
685 public function __construct($comparison = null
, $date = null
)
687 $this->comparison
= $comparison;
688 $this->date
= make_datetime($date);
691 public function buildCondition(PlFilter
$uf)
693 $uf->requireProfiles();
694 $str = 'p.deathdate IS NOT NULL';
695 if (!is_null($this->comparison
)) {
696 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->date
->format('Y-m-d'));
702 // {{{ class UFC_Registered
703 /** Filters users based on registration state
704 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
705 * @param $comparison Comparison operator
706 * @param $date Date to which users registration date should be compared
708 class UFC_Registered
extends UserFilterCondition
714 public function __construct($active = false
, $comparison = null
, $date = null
)
716 $this->active
= $active;
717 $this->comparison
= $comparison;
718 $this->date
= make_datetime($date);
721 public function buildCondition(PlFilter
$uf)
723 $uf->requireAccounts();
725 $date = '$UID IS NOT NULL AND a.state = \'active\'';
727 $date = '$UID IS NOT NULL AND a.state != \'pending\'';
729 if (!is_null($this->comparison
)) {
730 $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'));
736 // {{{ class UFC_ProfileUpdated
737 /** Filters users based on profile update date
738 * @param $comparison Comparison operator
739 * @param $date Date to which profile update date must be compared
741 class UFC_ProfileUpdated
extends UserFilterCondition
746 public function __construct($comparison = null
, $date = null
)
748 $this->comparison
= $comparison;
752 public function buildCondition(PlFilter
$uf)
754 $uf->requireProfiles();
755 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
759 // {{{ class UFC_Birthday
760 /** Filters users based on next birthday date
761 * @param $comparison Comparison operator
762 * @param $date Date to which users next birthday date should be compared
764 class UFC_Birthday
extends UserFilterCondition
769 public function __construct($comparison = null
, $date = null
)
771 $this->comparison
= $comparison;
775 public function buildCondition(PlFilter
$uf)
777 $uf->requireProfiles();
778 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
783 /** Filters users based on sex
784 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
786 class UFC_Sex
extends UserFilterCondition
789 public function __construct($sex)
794 public function buildCondition(PlFilter
$uf)
796 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
797 return self
::COND_FALSE
;
799 $uf->requireProfiles();
800 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
805 // {{{ class UFC_Group
806 /** Filters users based on group membership
807 * @param $group Group whose members we are selecting
808 * @param $anim Whether to restrict selection to animators of that group
810 class UFC_Group
extends UserFilterCondition
814 public function __construct($group, $anim = false
)
816 $this->group
= $group;
820 public function buildCondition(PlFilter
$uf)
822 // Groups have AX visibility.
823 if ($uf->getVisibilityLevel() == ProfileVisibility
::VIS_PUBLIC
) {
824 return self
::COND_TRUE
;
826 $sub = $uf->addGroupFilter($this->group
);
827 $where = 'gpm' . $sub . '.perms IS NOT NULL';
829 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
835 // {{{ class UFC_Binet
836 /** Selects users based on their belonging to a given (list of) binet
837 * @param $binet either a binet_id or an array of binet_ids
839 class UFC_Binet
extends UserFilterCondition
843 public function __construct()
845 $this->val
= pl_flatten(func_get_args());
848 public function buildCondition(PlFilter
$uf)
850 // Binets are private.
851 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
852 return self
::CONF_TRUE
;
854 $sub = $uf->addBinetsFilter();
855 return XDB
::format($sub . '.binet_id IN {?}', $this->val
);
859 // {{{ class UFC_Section
860 /** Selects users based on section
861 * @param $section ID of the section
863 class UFC_Section
extends UserFilterCondition
867 public function __construct()
869 $this->section
= pl_flatten(func_get_args());
872 public function buildCondition(PlFilter
$uf)
874 // Sections are private.
875 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
876 return self
::CONF_TRUE
;
878 $uf->requireProfiles();
879 return XDB
::format('p.section IN {?}', $this->section
);
883 // {{{ class UFC_Email
884 /** Filters users based on an email or a list of emails
885 * @param $emails List of emails whose owner must be selected
887 class UFC_Email
extends UserFilterCondition
890 public function __construct()
892 $this->emails
= pl_flatten(func_get_args());
895 public function buildCondition(PlFilter
$uf)
902 if (count($this->emails
) == 0) {
903 return PlFilterCondition
::COND_TRUE
;
906 foreach ($this->emails
as $entry) {
907 if (User
::isForeignEmailAddress($entry)) {
909 } else if (User
::isVirtualEmailAddress($entry)) {
912 @list
($user, $domain) = explode('@', $entry);
917 if (count($foreign) > 0) {
918 $sub = $uf->addEmailRedirectFilter($foreign);
919 $cond[] = XDB
::format('e' . $sub . '.email IS NOT NULL OR a.email IN {?}', $foreign);
921 if (count($virtual) > 0) {
922 $sub = $uf->addVirtualEmailFilter($virtual);
923 $cond[] = 'vr' . $sub . '.redirect IS NOT NULL';
925 if (count($aliases) > 0) {
926 $sub = $uf->addAliasFilter($aliases);
927 $cond[] = 'al' . $sub . '.alias IS NOT NULL';
929 return '(' . implode(') OR (', $cond) . ')';
933 // {{{ class UFC_Address
934 abstract class UFC_Address
extends UserFilterCondition
936 /** Valid address type ('hq' is reserved for company addresses)
942 /** Text for these types
944 protected static $typetexts = array(
945 self
::TYPE_HOME
=> 'home',
946 self
::TYPE_PRO
=> 'pro',
951 /** Flags for addresses
953 const FLAG_CURRENT
= 0x0001;
954 const FLAG_TEMP
= 0x0002;
955 const FLAG_SECOND
= 0x0004;
956 const FLAG_MAIL
= 0x0008;
957 const FLAG_CEDEX
= 0x0010;
959 // Binary OR of those flags
960 const FLAG_ANY
= 0x001F;
962 /** Text of these flags
964 protected static $flagtexts = array(
965 self
::FLAG_CURRENT
=> 'current',
966 self
::FLAG_TEMP
=> 'temporary',
967 self
::FLAG_SECOND
=> 'secondary',
968 self
::FLAG_MAIL
=> 'mail',
969 self
::FLAG_CEDEX
=> 'cedex',
974 public function __construct($type = null
, $flags = null
)
977 $this->flags
= $flags;
980 protected function initConds($sub, $vis_cond)
982 $conds = array($vis_cond);
985 foreach (self
::$typetexts as $flag => $type) {
986 if ($flag & $this->type
) {
991 $conds[] = XDB
::format($sub . '.type IN {?}', $types);
994 if ($this->flags
!= self
::FLAG_ANY
) {
995 foreach(self
::$flagtexts as $flag => $text) {
996 if ($flag & $this->flags
) {
997 $conds[] = 'FIND_IN_SET(' . XDB
::format('{?}', $text) . ', ' . $sub . '.flags)';
1006 // {{{ class UFC_AddressText
1007 /** Select users based on their address, using full text search
1008 * @param $text Text for filter in fulltext search
1009 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
1010 * @param $type Filter on address type
1011 * @param $flags Filter on address flags
1012 * @param $country Filter on address country
1013 * @param $locality Filter on address locality
1015 class UFC_AddressText
extends UFC_Address
1019 private $textSearchMode;
1021 public function __construct($text = null
, $textSearchMode = XDB
::WILDCARD_CONTAINS
,
1022 $type = null
, $flags = self
::FLAG_ANY
, $country = null
, $locality = null
)
1024 parent
::__construct($type, $flags);
1025 $this->text
= $text;
1026 $this->textSearchMode
= $textSearchMode;
1027 $this->country
= $country;
1028 $this->locality
= $locality;
1031 private function mkMatch($txt)
1033 return XDB
::formatWildcards($this->textSearchMode
, $txt);
1036 public function buildCondition(PlFilter
$uf)
1038 $sub = $uf->addAddressFilter();
1039 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1040 if ($this->text
!= null
) {
1041 $conds[] = $sub . '.text' . $this->mkMatch($this->text
);
1044 if ($this->country
!= null
) {
1045 $subc = $uf->addAddressCountryFilter();
1046 $subconds = array();
1047 $subconds[] = $subc . '.country' . $this->mkMatch($this->country
);
1048 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country
);
1049 $conds[] = implode(' OR ', $subconds);
1052 if ($this->locality
!= null
) {
1053 $subl = $uf->addAddressLocalityFilter();
1054 $conds[] = $subl . '.name' . $this->mkMatch($this->locality
);
1057 return implode(' AND ', $conds);
1061 // {{{ class UFC_AddressField
1062 /** Filters users based on their address,
1063 * @param $val Either a code for one of the fields, or an array of such codes
1064 * @param $fieldtype The type of field to look for
1065 * @param $type Filter on address type
1066 * @param $flags Filter on address flags
1068 class UFC_AddressField
extends UFC_Address
1070 const FIELD_COUNTRY
= 1;
1071 const FIELD_ADMAREA
= 2;
1072 const FIELD_SUBADMAREA
= 3;
1073 const FIELD_LOCALITY
= 4;
1074 const FIELD_ZIPCODE
= 5;
1076 /** Data of the filter
1081 public function __construct($val, $fieldtype, $type = null
, $flags = self
::FLAG_ANY
)
1083 parent
::__construct($type, $flags);
1085 if (!is_array($val)) {
1089 $this->fieldtype
= $fieldtype;
1092 public function buildCondition(PlFilter
$uf)
1094 $sub = $uf->addAddressFilter();
1095 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1097 switch ($this->fieldtype
) {
1098 case self
::FIELD_COUNTRY
:
1099 $field = 'countryId';
1101 case self
::FIELD_ADMAREA
:
1102 $field = 'administrativeAreaId';
1104 case self
::FIELD_SUBADMAREA
:
1105 $field = 'subAdministrativeAreaId';
1107 case self
::FIELD_LOCALITY
:
1108 $field = 'localityId';
1110 case self
::FIELD_ZIPCODE
:
1111 $field = 'postalCode';
1114 Platal
::page()->killError('Invalid address field type: ' . $this->fieldtype
);
1116 $conds[] = XDB
::format($sub . '.' . $field . ' IN {?}', $this->val
);
1118 return implode(' AND ', $conds);
1122 // {{{ class UFC_Corps
1123 /** Filters users based on the corps they belong to
1124 * @param $corps Corps we are looking for (abbreviation)
1125 * @param $type Whether we search for original or current corps
1127 class UFC_Corps
extends UserFilterCondition
1135 public function __construct($corps, $type = self
::CURRENT
)
1137 $this->corps
= $corps;
1138 $this->type
= $type;
1141 public function buildCondition(PlFilter
$uf)
1143 /** Tables shortcuts:
1144 * pc for profile_corps,
1145 * pceo for profile_corps_enum - orginal
1146 * pcec for profile_corps_enum - current
1148 $sub = $uf->addCorpsFilter($this->type
);
1149 $cond = $sub . '.abbreviation = ' . $corps;
1150 $cond .= ' AND ' . $uf->getVisibilityCondition($sub . '.corps_pub');
1155 // {{{ class UFC_Corps_Rank
1156 /** Filters users based on their rank in the corps
1157 * @param $rank Rank we are looking for (abbreviation)
1159 class UFC_Corps_Rank
extends UserFilterCondition
1162 public function __construct($rank)
1164 $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 $cond = $sub . '.abbreviation = ' . $rank;
1175 // XXX(x2006barrois): find a way to get rid of that hardcoded
1176 // reference to 'pc'.
1177 $cond .= ' AND ' . $uf->getVisibilityCondition('pc.corps_pub');
1182 // {{{ class UFC_Job_Company
1183 /** Filters users based on the company they belong to
1184 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
1185 * @param $value The searched value
1187 class UFC_Job_Company
extends UserFilterCondition
1190 const JOBNAME
= 'name';
1191 const JOBACRONYM
= 'acronym';
1196 public function __construct($type, $value)
1198 $this->assertType($type);
1199 $this->type
= $type;
1200 $this->value
= $value;
1203 private function assertType($type)
1205 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
1206 Platal
::page()->killError("Type de recherche non valide.");
1210 public function buildCondition(PlFilter
$uf)
1212 $sub = $uf->addJobCompanyFilter();
1213 $cond = $sub . '.' . $this->type
. XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1214 $jsub = $uf->addJobFilter();
1215 $cond .= ' AND ' . $uf->getVisibilityCondition($jsub . '.pub');
1220 // {{{ class UFC_Job_Terms
1221 /** Filters users based on the job terms they assigned to one of their
1223 * @param $val The ID of the job term, or an array of such IDs
1225 class UFC_Job_Terms
extends UserFilterCondition
1229 public function __construct($val)
1231 if (!is_array($val)) {
1237 public function buildCondition(PlFilter
$uf)
1239 $sub = $uf->addJobTermsFilter(count($this->val
));
1240 $conditions = array();
1241 foreach ($this->val
as $i => $jtid) {
1242 $conditions[] = $sub[$i] . '.jtid_1 = ' . XDB
::escape($jtid);
1244 $jsub = $uf->addJobFilter();
1245 $conditions[] = $uf->getVisibilityCondition($jsub . '.pub');
1246 return implode(' AND ', $conditions);
1250 // {{{ class UFC_Job_Description
1251 /** Filters users based on their job description
1252 * @param $description The text being searched for
1253 * @param $fields The fields to search for (CV, user-defined)
1255 class UFC_Job_Description
extends UserFilterCondition
1258 private $description;
1261 public function __construct($description, $fields)
1263 $this->fields
= $fields;
1264 $this->description
= $description;
1267 public function buildCondition(PlFilter
$uf)
1271 $jsub = $uf->addJobFilter();
1272 // CV is private => if only CV requested, and not private,
1273 // don't do anything. Otherwise restrict to standard job visibility.
1274 if ($this->fields
== UserFilter
::JOB_CV
) {
1275 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
1276 return self
::CONF_TRUE
;
1279 $conds[] = $uf->getVisibilityCondition($jsub . '.pub');
1282 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
1283 $conds[] = $jsub . '.description ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1285 if ($this->fields
& UserFilter
::JOB_CV
&& $uf->getVisibilityLevel() == ProfileVisibility
::VIS_PRIVATE
) {
1286 $uf->requireProfiles();
1287 $conds[] = 'p.cv ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1289 return implode(' OR ', $conds);
1293 // {{{ class UFC_Networking
1294 /** Filters users based on network identity (IRC, ...)
1295 * @param $type Type of network (-1 for any)
1296 * @param $value Value to search
1298 class UFC_Networking
extends UserFilterCondition
1303 public function __construct($type, $value)
1305 $this->type
= $type;
1306 $this->value
= $value;
1309 public function buildCondition(PlFilter
$uf)
1311 $sub = $uf->addNetworkingFilter();
1313 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1314 $conds[] = $sub . '.address ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1315 if ($this->type
!= -1) {
1316 $conds[] = $sub . '.nwid = ' . XDB
::format('{?}', $this->type
);
1318 return implode(' AND ', $conds);
1322 // {{{ class UFC_Phone
1323 /** Filters users based on their phone number
1324 * @param $num_type Type of number (pro/user/home)
1325 * @param $phone_type Type of phone (fixed/mobile/fax)
1326 * @param $number Phone number
1328 class UFC_Phone
extends UserFilterCondition
1330 const NUM_PRO
= 'pro';
1331 const NUM_USER
= 'user';
1332 const NUM_HOME
= 'address';
1333 const NUM_ANY
= 'any';
1335 const PHONE_FIXED
= 'fixed';
1336 const PHONE_MOBILE
= 'mobile';
1337 const PHONE_FAX
= 'fax';
1338 const PHONE_ANY
= 'any';
1341 private $phone_type;
1344 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
1346 $phone = new Phone(array('display' => $number));
1348 $this->number
= $phone->search();
1349 $this->num_type
= $num_type;
1350 $this->phone_type
= $phone_type;
1353 public function buildCondition(PlFilter
$uf)
1355 $sub = $uf->addPhoneFilter();
1358 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1360 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
1361 if ($this->num_type
!= self
::NUM_ANY
) {
1362 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
1364 if ($this->phone_type
!= self
::PHONE_ANY
) {
1365 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
1367 return implode(' AND ', $conds);
1371 // {{{ class UFC_Medal
1372 /** Filters users based on their medals
1373 * @param $medal ID of the medal
1374 * @param $grade Grade of the medal (null for 'any')
1376 class UFC_Medal
extends UserFilterCondition
1381 public function __construct($medal, $grade = null
)
1383 $this->medal
= $medal;
1384 $this->grade
= $grade;
1387 public function buildCondition(PlFilter
$uf)
1391 // This will require profiles => table 'p' will be available.
1392 $sub = $uf->addMedalFilter();
1394 $conds[] = $uf->getVisibilityCondition('p.medals_pub');
1396 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
1397 if ($this->grade
!= null
) {
1398 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
1400 return implode(' AND ', $conds);
1404 // {{{ class UFC_Photo
1405 /** Filters profiles with photo
1407 class UFC_Photo
extends UserFilterCondition
1409 public function buildCondition(PlFilter
$uf)
1411 $sub = $uf->addPhotoFilter();
1412 return $sub . '.attach IS NOT NULL AND ' . $uf->getVisibilityCondition($sub . '.pub');
1416 // {{{ class UFC_Mentor
1417 class UFC_Mentor
extends UserFilterCondition
1419 public function buildCondition(PlFilter
$uf)
1421 $sub = $uf->addMentorFilter(UserFilter
::MENTOR
);
1422 return $sub . '.expertise IS NOT NULL';
1426 // {{{ class UFC_Mentor_Expertise
1427 /** Filters users by mentoring expertise
1428 * @param $expertise Domain of expertise
1430 class UFC_Mentor_Expertise
extends UserFilterCondition
1434 public function __construct($expertise)
1436 $this->expertise
= $expertise;
1439 public function buildCondition(PlFilter
$uf)
1441 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_EXPERTISE
);
1442 return $sub . '.expertise ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->expertise
);
1446 // {{{ class UFC_Mentor_Country
1447 /** Filters users by mentoring country
1448 * @param $country Two-letters code of country being searched
1450 class UFC_Mentor_Country
extends UserFilterCondition
1454 public function __construct()
1456 $this->country
= pl_flatten(func_get_args());
1459 public function buildCondition(PlFilter
$uf)
1461 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_COUNTRY
);
1462 return $sub . '.country IN ' . XDB
::format('{?}', $this->country
);
1466 // {{{ class UFC_Mentor_Terms
1467 /** Filters users based on the job terms they used in mentoring.
1468 * @param $val The ID of the job term, or an array of such IDs
1470 class UFC_Mentor_Terms
extends UserFilterCondition
1474 public function __construct($val)
1479 public function buildCondition(PlFilter
$uf)
1481 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_TERM
);
1482 return $sub . '.jtid_1 = ' . XDB
::escape($this->val
);
1486 // {{{ class UFC_UserRelated
1487 /** Filters users based on a relation toward a user
1488 * @param $user User to which searched users are related
1490 abstract class UFC_UserRelated
extends UserFilterCondition
1493 public function __construct(PlUser
&$user)
1495 $this->user
=& $user;
1499 // {{{ class UFC_Contact
1500 /** Filters users who belong to selected user's contacts
1502 class UFC_Contact
extends UFC_UserRelated
1504 public function buildCondition(PlFilter
$uf)
1506 $sub = $uf->addContactFilter($this->user
->id());
1507 return 'c' . $sub . '.contact IS NOT NULL';
1511 // {{{ class UFC_WatchRegistration
1512 /** Filters users being watched by selected user
1514 class UFC_WatchRegistration
extends UFC_UserRelated
1516 public function buildCondition(PlFilter
$uf)
1518 if (!$this->user
->watchType('registration')) {
1519 return PlFilterCondition
::COND_FALSE
;
1521 $uids = $this->user
->watchUsers();
1522 if (count($uids) == 0) {
1523 return PlFilterCondition
::COND_FALSE
;
1525 return XDB
::format('$UID IN {?}', $uids);
1530 // {{{ class UFC_WatchPromo
1531 /** Filters users belonging to a promo watched by selected user
1532 * @param $user Selected user (the one watching promo)
1533 * @param $grade Formation the user is watching
1535 class UFC_WatchPromo
extends UFC_UserRelated
1538 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
1540 parent
::__construct($user);
1541 $this->grade
= $grade;
1544 public function buildCondition(PlFilter
$uf)
1546 $promos = $this->user
->watchPromos();
1547 if (count($promos) == 0) {
1548 return PlFilterCondition
::COND_FALSE
;
1550 $sube = $uf->addEducationFilter(true
, $this->grade
);
1551 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
1552 return XDB
::format($field . ' IN {?}', $promos);
1557 // {{{ class UFC_WatchContact
1558 /** Filters users watched by selected user
1560 class UFC_WatchContact
extends UFC_Contact
1562 public function buildCondition(PlFilter
$uf)
1564 if (!$this->user
->watchContacts()) {
1565 return PlFilterCondition
::COND_FALSE
;
1567 return parent
::buildCondition($uf);
1571 // {{{ class UFC_MarketingHash
1572 /** Filters users using the hash generated
1573 * to send marketing emails to him.
1575 class UFC_MarketingHash
extends UserFilterCondition
1579 public function __construct($hash)
1581 $this->hash
= $hash;
1584 public function buildCondition(PlFilter
$uf)
1586 $table = $uf->addMarketingHash();
1587 return XDB
::format('rm.hash = {?}', $this->hash
);
1592 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: