24a285756bc99e5c87b5180a1fc89ad516673912
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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['comparison'] = $this->comparison
;
422 if ($this->grade
!= UserFilter
::DISPLAY
) {
423 $export['grade'] = $this->grade
;
425 $export['promo'] = $this->promo
;
430 // {{{ class UFC_SchoolId
431 /** Filters users based on their shoold identifier
432 * @param type Parameter type (Xorg, AX, School)
433 * @param value School id value
435 class UFC_SchoolId
extends UserFilterCondition
439 const School
= 'school';
444 static public function assertType($type)
446 if ($type != self
::AX
&& $type != self
::Xorg
&& $type != self
::School
) {
447 Platal
::page()->killError("Type de matricule invalide: $type");
451 public function __construct($type, $id)
455 self
::assertType($type);
458 public function buildCondition(PlFilter
$uf)
460 $uf->requireProfiles();
463 if ($type == self
::School
) {
465 $id = Profile
::getXorgId($id);
467 return XDB
::format('p.' . $type . '_id = {?}', $id);
471 // {{{ class UFC_EducationSchool
472 /** Filters users by formation
473 * @param $val The formation to search (either ID or array of IDs)
475 class UFC_EducationSchool
extends UserFilterCondition
479 public function __construct()
481 $this->val
= pl_flatten(func_get_args());
484 public function buildCondition(PlFilter
$uf)
486 $sub = $uf->addEducationFilter();
487 return XDB
::format('pe' . $sub . '.eduid IN {?}', $this->val
);
491 // {{{ class UFC_EducationDegree
492 class UFC_EducationDegree
extends UserFilterCondition
496 public function __construct()
498 $this->diploma
= pl_flatten(func_get_args());
501 public function buildCondition(PlFilter
$uf)
503 $sub = $uf->addEducationFilter();
504 return XDB
::format('pe' . $sub . '.degreeid IN {?}', $this->diploma
);
508 // {{{ class UFC_EducationField
509 class UFC_EducationField
extends UserFilterCondition
513 public function __construct()
515 $this->val
= pl_flatten(func_get_args());
518 public function buildCondition(PlFilter
$uf)
520 $sub = $uf->addEducationFilter();
521 return XDB
::format('pe' . $sub . '.fieldid IN {?}', $this->val
);
525 // {{{ class UFC_Name
526 /** Filters users based on name
527 * @param $type Type of name field on which filtering is done (firstname, lastname...)
528 * @param $text Text on which to filter
529 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
531 class UFC_Name
extends UserFilterCondition
533 const EXACT
= XDB
::WILDCARD_EXACT
; // 0x000
534 const PREFIX
= XDB
::WILDCARD_PREFIX
; // 0x001
535 const SUFFIX
= XDB
::WILDCARD_SUFFIX
; // 0x002
536 const CONTAINS
= XDB
::WILDCARD_CONTAINS
; // 0x003
537 const PARTICLE
= 0x004;
538 const VARIANTS
= 0x008;
544 public function __construct($type, $text, $mode)
551 private function buildNameQuery($type, $variant, $where, UserFilter
$uf)
553 $sub = $uf->addNameFilter($type, $variant);
554 return str_replace('$ME', 'pn' . $sub, $where);
557 public function buildCondition(PlFilter
$uf)
560 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
561 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
563 $right = XDB
::formatWildcards($this->mode
& self
::CONTAINS
, $this->text
);
565 $cond = $left . $right;
566 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
567 if (($this->mode
& self
::VARIANTS
) != 0 && isset(Profile
::$name_variants[$this->type
])) {
568 foreach (Profile
::$name_variants[$this->type
] as $var) {
569 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
572 return implode(' OR ', $conds);
575 public function export()
577 $export = $this->buildExport($this->type
);
578 if ($this->mode
& self
::VARIANTS
) {
579 $export['search_in_variants'] = true
;
581 if ($this->mode
& self
::PARTICLE
) {
582 $export['search_in_particle'] = true
;
584 $export['comparison'] = self
::comparisonFromXDBWildcard($this->mode
& 0x3);
585 $export['text'] = $this->text
;
590 // {{{ class UFC_NameTokens
591 /** Selects users based on tokens in their name (for quicksearch)
592 * @param $tokens An array of tokens to search
593 * @param $flags Flags the tokens must have (e.g 'public' for public search)
594 * @param $soundex (bool) Whether those tokens are fulltext or soundex
596 class UFC_NameTokens
extends UserFilterCondition
599 const FLAG_PUBLIC
= 'public';
606 public function __construct($tokens, $flags = array(), $soundex = false
, $exact = false
)
608 if (is_array($tokens)) {
609 $this->tokens
= $tokens;
611 $this->tokens
= array($tokens);
613 if (is_array($flags)) {
614 $this->flags
= $flags;
616 $this->flags
= array($flags);
618 $this->soundex
= $soundex;
619 $this->exact
= $exact;
622 public function buildCondition(PlFilter
$uf)
625 foreach ($this->tokens
as $i => $token) {
626 $sub = $uf->addNameTokensFilter($token);
627 if ($this->soundex
) {
628 $c = XDB
::format($sub . '.soundex = {?}', soundex_fr($token));
629 } else if ($this->exact
) {
630 $c = XDB
::format($sub . '.token = {?}', $token);
632 $c = $sub . '.token ' . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $token);
634 if ($this->flags
!= null
) {
635 $c .= XDB
::format(' AND ' . $sub . '.flags IN {?}', $this->flags
);
640 return implode(' AND ', $conds);
644 // {{{ class UFC_Nationality
645 class UFC_Nationality
extends UserFilterCondition
649 public function __construct()
651 $this->val
= pl_flatten(func_get_args());
654 public function buildCondition(PlFilter
$uf)
656 $uf->requireProfiles();
657 $nat = XDB
::formatArray($this->val
);
659 'p.nationality1 IN ' . $nat,
660 'p.nationality2 IN ' . $nat,
661 'p.nationality3 IN ' . $nat,
663 return implode(' OR ', $conds);
667 // {{{ class UFC_Dead
668 /** Filters users based on death date
669 * @param $comparison Comparison operator
670 * @param $date Date to which death date should be compared (DateTime object, string or timestamp)
672 class UFC_Dead
extends UserFilterCondition
677 public function __construct($comparison = null
, $date = null
)
679 $this->comparison
= $comparison;
680 $this->date
= make_datetime($date);
683 public function buildCondition(PlFilter
$uf)
685 $uf->requireProfiles();
686 $str = 'p.deathdate IS NOT NULL';
687 if (!is_null($this->comparison
)) {
688 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->date
->format('Y-m-d'));
694 // {{{ class UFC_Registered
695 /** Filters users based on registration state
696 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
697 * @param $comparison Comparison operator
698 * @param $date Date to which users registration date should be compared
700 class UFC_Registered
extends UserFilterCondition
706 public function __construct($active = false
, $comparison = null
, $date = null
)
708 $this->active
= $active;
709 $this->comparison
= $comparison;
710 $this->date
= make_datetime($date);
713 public function buildCondition(PlFilter
$uf)
715 $uf->requireAccounts();
717 $date = '$UID IS NOT NULL AND a.state = \'active\'';
719 $date = '$UID IS NOT NULL AND a.state != \'pending\'';
721 if (!is_null($this->comparison
)) {
722 $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'));
728 // {{{ class UFC_ProfileUpdated
729 /** Filters users based on profile update date
730 * @param $comparison Comparison operator
731 * @param $date Date to which profile update date must be compared
733 class UFC_ProfileUpdated
extends UserFilterCondition
738 public function __construct($comparison = null
, $date = null
)
740 $this->comparison
= $comparison;
744 public function buildCondition(PlFilter
$uf)
746 $uf->requireProfiles();
747 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
751 // {{{ class UFC_Birthday
752 /** Filters users based on next birthday date
753 * @param $comparison Comparison operator
754 * @param $date Date to which users next birthday date should be compared
756 class UFC_Birthday
extends UserFilterCondition
761 public function __construct($comparison = null
, $date = null
)
763 $this->comparison
= $comparison;
767 public function buildCondition(PlFilter
$uf)
769 $uf->requireProfiles();
770 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
775 /** Filters users based on sex
776 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
778 class UFC_Sex
extends UserFilterCondition
781 public function __construct($sex)
786 public function buildCondition(PlFilter
$uf)
788 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
789 return self
::COND_FALSE
;
791 $uf->requireProfiles();
792 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
797 // {{{ class UFC_Group
798 /** Filters users based on group membership
799 * @param $group Group whose members we are selecting
800 * @param $anim Whether to restrict selection to animators of that group
802 class UFC_Group
extends UserFilterCondition
806 public function __construct($group, $anim = false
)
808 $this->group
= $group;
812 public function buildCondition(PlFilter
$uf)
814 // Groups have AX visibility.
815 if ($uf->getVisibilityLevel() == ProfileVisibility
::VIS_PUBLIC
) {
816 return self
::COND_TRUE
;
818 $sub = $uf->addGroupFilter($this->group
);
819 $where = 'gpm' . $sub . '.perms IS NOT NULL';
821 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
827 // {{{ class UFC_Binet
828 /** Selects users based on their belonging to a given (list of) binet
829 * @param $binet either a binet_id or an array of binet_ids
831 class UFC_Binet
extends UserFilterCondition
835 public function __construct()
837 $this->val
= pl_flatten(func_get_args());
840 public function buildCondition(PlFilter
$uf)
842 // Binets are private.
843 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
844 return self
::CONF_TRUE
;
846 $sub = $uf->addBinetsFilter();
847 return XDB
::format($sub . '.binet_id IN {?}', $this->val
);
851 // {{{ class UFC_Section
852 /** Selects users based on section
853 * @param $section ID of the section
855 class UFC_Section
extends UserFilterCondition
859 public function __construct()
861 $this->section
= pl_flatten(func_get_args());
864 public function buildCondition(PlFilter
$uf)
866 // Sections are private.
867 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
868 return self
::CONF_TRUE
;
870 $uf->requireProfiles();
871 return XDB
::format('p.section IN {?}', $this->section
);
875 // {{{ class UFC_Email
876 /** Filters users based on an email or a list of emails
877 * @param $emails List of emails whose owner must be selected
879 class UFC_Email
extends UserFilterCondition
882 public function __construct()
884 $this->emails
= pl_flatten(func_get_args());
887 public function buildCondition(PlFilter
$uf)
894 if (count($this->emails
) == 0) {
895 return PlFilterCondition
::COND_TRUE
;
898 foreach ($this->emails
as $entry) {
899 if (User
::isForeignEmailAddress($entry)) {
901 } else if (User
::isVirtualEmailAddress($entry)) {
904 @list
($user, $domain) = explode('@', $entry);
909 if (count($foreign) > 0) {
910 $sub = $uf->addEmailRedirectFilter($foreign);
911 $cond[] = XDB
::format('e' . $sub . '.email IS NOT NULL OR a.email IN {?}', $foreign);
913 if (count($virtual) > 0) {
914 $sub = $uf->addVirtualEmailFilter($virtual);
915 $cond[] = 'vr' . $sub . '.redirect IS NOT NULL';
917 if (count($aliases) > 0) {
918 $sub = $uf->addAliasFilter($aliases);
919 $cond[] = 'al' . $sub . '.alias IS NOT NULL';
921 return '(' . implode(') OR (', $cond) . ')';
925 // {{{ class UFC_Address
926 abstract class UFC_Address
extends UserFilterCondition
928 /** Valid address type ('hq' is reserved for company addresses)
934 /** Text for these types
936 protected static $typetexts = array(
937 self
::TYPE_HOME
=> 'home',
938 self
::TYPE_PRO
=> 'pro',
943 /** Flags for addresses
945 const FLAG_CURRENT
= 0x0001;
946 const FLAG_TEMP
= 0x0002;
947 const FLAG_SECOND
= 0x0004;
948 const FLAG_MAIL
= 0x0008;
949 const FLAG_CEDEX
= 0x0010;
951 // Binary OR of those flags
952 const FLAG_ANY
= 0x001F;
954 /** Text of these flags
956 protected static $flagtexts = array(
957 self
::FLAG_CURRENT
=> 'current',
958 self
::FLAG_TEMP
=> 'temporary',
959 self
::FLAG_SECOND
=> 'secondary',
960 self
::FLAG_MAIL
=> 'mail',
961 self
::FLAG_CEDEX
=> 'cedex',
966 public function __construct($type = null
, $flags = null
)
969 $this->flags
= $flags;
972 protected function initConds($sub, $vis_cond)
974 $conds = array($vis_cond);
977 foreach (self
::$typetexts as $flag => $type) {
978 if ($flag & $this->type
) {
983 $conds[] = XDB
::format($sub . '.type IN {?}', $types);
986 if ($this->flags
!= self
::FLAG_ANY
) {
987 foreach(self
::$flagtexts as $flag => $text) {
988 if ($flag & $this->flags
) {
989 $conds[] = 'FIND_IN_SET(' . XDB
::format('{?}', $text) . ', ' . $sub . '.flags)';
998 // {{{ class UFC_AddressText
999 /** Select users based on their address, using full text search
1000 * @param $text Text for filter in fulltext search
1001 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
1002 * @param $type Filter on address type
1003 * @param $flags Filter on address flags
1004 * @param $country Filter on address country
1005 * @param $locality Filter on address locality
1007 class UFC_AddressText
extends UFC_Address
1011 private $textSearchMode;
1013 public function __construct($text = null
, $textSearchMode = XDB
::WILDCARD_CONTAINS
,
1014 $type = null
, $flags = self
::FLAG_ANY
, $country = null
, $locality = null
)
1016 parent
::__construct($type, $flags);
1017 $this->text
= $text;
1018 $this->textSearchMode
= $textSearchMode;
1019 $this->country
= $country;
1020 $this->locality
= $locality;
1023 private function mkMatch($txt)
1025 return XDB
::formatWildcards($this->textSearchMode
, $txt);
1028 public function buildCondition(PlFilter
$uf)
1030 $sub = $uf->addAddressFilter();
1031 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1032 if ($this->text
!= null
) {
1033 $conds[] = $sub . '.text' . $this->mkMatch($this->text
);
1036 if ($this->country
!= null
) {
1037 $subc = $uf->addAddressCountryFilter();
1038 $subconds = array();
1039 $subconds[] = $subc . '.country' . $this->mkMatch($this->country
);
1040 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country
);
1041 $conds[] = implode(' OR ', $subconds);
1044 if ($this->locality
!= null
) {
1045 $subl = $uf->addAddressLocalityFilter();
1046 $conds[] = $subl . '.name' . $this->mkMatch($this->locality
);
1049 return implode(' AND ', $conds);
1053 // {{{ class UFC_AddressField
1054 /** Filters users based on their address,
1055 * @param $val Either a code for one of the fields, or an array of such codes
1056 * @param $fieldtype The type of field to look for
1057 * @param $type Filter on address type
1058 * @param $flags Filter on address flags
1060 class UFC_AddressField
extends UFC_Address
1062 const FIELD_COUNTRY
= 1;
1063 const FIELD_ADMAREA
= 2;
1064 const FIELD_SUBADMAREA
= 3;
1065 const FIELD_LOCALITY
= 4;
1066 const FIELD_ZIPCODE
= 5;
1068 /** Data of the filter
1073 public function __construct($val, $fieldtype, $type = null
, $flags = self
::FLAG_ANY
)
1075 parent
::__construct($type, $flags);
1077 if (!is_array($val)) {
1081 $this->fieldtype
= $fieldtype;
1084 public function buildCondition(PlFilter
$uf)
1086 $sub = $uf->addAddressFilter();
1087 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1089 switch ($this->fieldtype
) {
1090 case self
::FIELD_COUNTRY
:
1091 $field = 'countryId';
1093 case self
::FIELD_ADMAREA
:
1094 $field = 'administrativeAreaId';
1096 case self
::FIELD_SUBADMAREA
:
1097 $field = 'subAdministrativeAreaId';
1099 case self
::FIELD_LOCALITY
:
1100 $field = 'localityId';
1102 case self
::FIELD_ZIPCODE
:
1103 $field = 'postalCode';
1106 Platal
::page()->killError('Invalid address field type: ' . $this->fieldtype
);
1108 $conds[] = XDB
::format($sub . '.' . $field . ' IN {?}', $this->val
);
1110 return implode(' AND ', $conds);
1114 // {{{ class UFC_Corps
1115 /** Filters users based on the corps they belong to
1116 * @param $corps Corps we are looking for (abbreviation)
1117 * @param $type Whether we search for original or current corps
1119 class UFC_Corps
extends UserFilterCondition
1127 public function __construct($corps, $type = self
::CURRENT
)
1129 $this->corps
= $corps;
1130 $this->type
= $type;
1133 public function buildCondition(PlFilter
$uf)
1135 /** Tables shortcuts:
1136 * pc for profile_corps,
1137 * pceo for profile_corps_enum - orginal
1138 * pcec for profile_corps_enum - current
1140 $sub = $uf->addCorpsFilter($this->type
);
1141 $cond = $sub . '.abbreviation = ' . $corps;
1142 $cond .= ' AND ' . $uf->getVisibilityCondition($sub . '.corps_pub');
1147 // {{{ class UFC_Corps_Rank
1148 /** Filters users based on their rank in the corps
1149 * @param $rank Rank we are looking for (abbreviation)
1151 class UFC_Corps_Rank
extends UserFilterCondition
1154 public function __construct($rank)
1156 $this->rank
= $rank;
1159 public function buildCondition(PlFilter
$uf)
1161 /** Tables shortcuts:
1162 * pc for profile_corps
1163 * pcr for profile_corps_rank
1165 $sub = $uf->addCorpsRankFilter();
1166 $cond = $sub . '.abbreviation = ' . $rank;
1167 // XXX(x2006barrois): find a way to get rid of that hardcoded
1168 // reference to 'pc'.
1169 $cond .= ' AND ' . $uf->getVisibilityCondition('pc.corps_pub');
1174 // {{{ class UFC_Job_Company
1175 /** Filters users based on the company they belong to
1176 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
1177 * @param $value The searched value
1179 class UFC_Job_Company
extends UserFilterCondition
1182 const JOBNAME
= 'name';
1183 const JOBACRONYM
= 'acronym';
1188 public function __construct($type, $value)
1190 $this->assertType($type);
1191 $this->type
= $type;
1192 $this->value
= $value;
1195 private function assertType($type)
1197 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
1198 Platal
::page()->killError("Type de recherche non valide.");
1202 public function buildCondition(PlFilter
$uf)
1204 $sub = $uf->addJobCompanyFilter();
1205 $cond = $sub . '.' . $this->type
. XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1206 $jsub = $uf->addJobFilter();
1207 $cond .= ' AND ' . $uf->getVisibilityCondition($jsub . '.pub');
1212 // {{{ class UFC_Job_Terms
1213 /** Filters users based on the job terms they assigned to one of their
1215 * @param $val The ID of the job term, or an array of such IDs
1217 class UFC_Job_Terms
extends UserFilterCondition
1221 public function __construct($val)
1223 if (!is_array($val)) {
1229 public function buildCondition(PlFilter
$uf)
1231 $sub = $uf->addJobTermsFilter(count($this->val
));
1232 $conditions = array();
1233 foreach ($this->val
as $i => $jtid) {
1234 $conditions[] = $sub[$i] . '.jtid_1 = ' . XDB
::escape($jtid);
1236 $jsub = $uf->addJobFilter();
1237 $conditions[] = $uf->getVisibilityCondition($jsub . '.pub');
1238 return implode(' AND ', $conditions);
1242 // {{{ class UFC_Job_Description
1243 /** Filters users based on their job description
1244 * @param $description The text being searched for
1245 * @param $fields The fields to search for (CV, user-defined)
1247 class UFC_Job_Description
extends UserFilterCondition
1250 private $description;
1253 public function __construct($description, $fields)
1255 $this->fields
= $fields;
1256 $this->description
= $description;
1259 public function buildCondition(PlFilter
$uf)
1263 $jsub = $uf->addJobFilter();
1264 // CV is private => if only CV requested, and not private,
1265 // don't do anything. Otherwise restrict to standard job visibility.
1266 if ($this->fields
== UserFilter
::JOB_CV
) {
1267 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
1268 return self
::CONF_TRUE
;
1271 $conds[] = $uf->getVisibilityCondition($jsub . '.pub');
1274 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
1275 $conds[] = $jsub . '.description ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1277 if ($this->fields
& UserFilter
::JOB_CV
&& $uf->getVisibilityLevel() == ProfileVisibility
::VIS_PRIVATE
) {
1278 $uf->requireProfiles();
1279 $conds[] = 'p.cv ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1281 return implode(' OR ', $conds);
1285 // {{{ class UFC_Networking
1286 /** Filters users based on network identity (IRC, ...)
1287 * @param $type Type of network (-1 for any)
1288 * @param $value Value to search
1290 class UFC_Networking
extends UserFilterCondition
1295 public function __construct($type, $value)
1297 $this->type
= $type;
1298 $this->value
= $value;
1301 public function buildCondition(PlFilter
$uf)
1303 $sub = $uf->addNetworkingFilter();
1305 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1306 $conds[] = $sub . '.address ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1307 if ($this->type
!= -1) {
1308 $conds[] = $sub . '.nwid = ' . XDB
::format('{?}', $this->type
);
1310 return implode(' AND ', $conds);
1314 // {{{ class UFC_Phone
1315 /** Filters users based on their phone number
1316 * @param $num_type Type of number (pro/user/home)
1317 * @param $phone_type Type of phone (fixed/mobile/fax)
1318 * @param $number Phone number
1320 class UFC_Phone
extends UserFilterCondition
1322 const NUM_PRO
= 'pro';
1323 const NUM_USER
= 'user';
1324 const NUM_HOME
= 'address';
1325 const NUM_ANY
= 'any';
1327 const PHONE_FIXED
= 'fixed';
1328 const PHONE_MOBILE
= 'mobile';
1329 const PHONE_FAX
= 'fax';
1330 const PHONE_ANY
= 'any';
1333 private $phone_type;
1336 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
1338 $phone = new Phone(array('display' => $number));
1340 $this->number
= $phone->search();
1341 $this->num_type
= $num_type;
1342 $this->phone_type
= $phone_type;
1345 public function buildCondition(PlFilter
$uf)
1347 $sub = $uf->addPhoneFilter();
1350 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1352 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
1353 if ($this->num_type
!= self
::NUM_ANY
) {
1354 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
1356 if ($this->phone_type
!= self
::PHONE_ANY
) {
1357 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
1359 return implode(' AND ', $conds);
1363 // {{{ class UFC_Medal
1364 /** Filters users based on their medals
1365 * @param $medal ID of the medal
1366 * @param $grade Grade of the medal (null for 'any')
1368 class UFC_Medal
extends UserFilterCondition
1373 public function __construct($medal, $grade = null
)
1375 $this->medal
= $medal;
1376 $this->grade
= $grade;
1379 public function buildCondition(PlFilter
$uf)
1383 // This will require profiles => table 'p' will be available.
1384 $sub = $uf->addMedalFilter();
1386 $conds[] = $uf->getVisibilityCondition('p.medals_pub');
1388 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
1389 if ($this->grade
!= null
) {
1390 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
1392 return implode(' AND ', $conds);
1396 // {{{ class UFC_Photo
1397 /** Filters profiles with photo
1399 class UFC_Photo
extends UserFilterCondition
1401 public function buildCondition(PlFilter
$uf)
1403 $sub = $uf->addPhotoFilter();
1404 return $sub . '.attach IS NOT NULL AND ' . $uf->getVisibilityCondition($sub . '.pub');
1408 // {{{ class UFC_Mentor
1409 class UFC_Mentor
extends UserFilterCondition
1411 public function buildCondition(PlFilter
$uf)
1413 $sub = $uf->addMentorFilter(UserFilter
::MENTOR
);
1414 return $sub . '.expertise IS NOT NULL';
1418 // {{{ class UFC_Mentor_Expertise
1419 /** Filters users by mentoring expertise
1420 * @param $expertise Domain of expertise
1422 class UFC_Mentor_Expertise
extends UserFilterCondition
1426 public function __construct($expertise)
1428 $this->expertise
= $expertise;
1431 public function buildCondition(PlFilter
$uf)
1433 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_EXPERTISE
);
1434 return $sub . '.expertise ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->expertise
);
1438 // {{{ class UFC_Mentor_Country
1439 /** Filters users by mentoring country
1440 * @param $country Two-letters code of country being searched
1442 class UFC_Mentor_Country
extends UserFilterCondition
1446 public function __construct()
1448 $this->country
= pl_flatten(func_get_args());
1451 public function buildCondition(PlFilter
$uf)
1453 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_COUNTRY
);
1454 return $sub . '.country IN ' . XDB
::format('{?}', $this->country
);
1458 // {{{ class UFC_Mentor_Terms
1459 /** Filters users based on the job terms they used in mentoring.
1460 * @param $val The ID of the job term, or an array of such IDs
1462 class UFC_Mentor_Terms
extends UserFilterCondition
1466 public function __construct($val)
1471 public function buildCondition(PlFilter
$uf)
1473 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_TERM
);
1474 return $sub . '.jtid_1 = ' . XDB
::escape($this->val
);
1478 // {{{ class UFC_UserRelated
1479 /** Filters users based on a relation toward a user
1480 * @param $user User to which searched users are related
1482 abstract class UFC_UserRelated
extends UserFilterCondition
1485 public function __construct(PlUser
&$user)
1487 $this->user
=& $user;
1491 // {{{ class UFC_Contact
1492 /** Filters users who belong to selected user's contacts
1494 class UFC_Contact
extends UFC_UserRelated
1496 public function buildCondition(PlFilter
$uf)
1498 $sub = $uf->addContactFilter($this->user
->id());
1499 return 'c' . $sub . '.contact IS NOT NULL';
1503 // {{{ class UFC_WatchRegistration
1504 /** Filters users being watched by selected user
1506 class UFC_WatchRegistration
extends UFC_UserRelated
1508 public function buildCondition(PlFilter
$uf)
1510 if (!$this->user
->watchType('registration')) {
1511 return PlFilterCondition
::COND_FALSE
;
1513 $uids = $this->user
->watchUsers();
1514 if (count($uids) == 0) {
1515 return PlFilterCondition
::COND_FALSE
;
1517 return XDB
::format('$UID IN {?}', $uids);
1522 // {{{ class UFC_WatchPromo
1523 /** Filters users belonging to a promo watched by selected user
1524 * @param $user Selected user (the one watching promo)
1525 * @param $grade Formation the user is watching
1527 class UFC_WatchPromo
extends UFC_UserRelated
1530 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
1532 parent
::__construct($user);
1533 $this->grade
= $grade;
1536 public function buildCondition(PlFilter
$uf)
1538 $promos = $this->user
->watchPromos();
1539 if (count($promos) == 0) {
1540 return PlFilterCondition
::COND_FALSE
;
1542 $sube = $uf->addEducationFilter(true
, $this->grade
);
1543 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
1544 return XDB
::format($field . ' IN {?}', $promos);
1549 // {{{ class UFC_WatchContact
1550 /** Filters users watched by selected user
1552 class UFC_WatchContact
extends UFC_Contact
1554 public function buildCondition(PlFilter
$uf)
1556 if (!$this->user
->watchContacts()) {
1557 return PlFilterCondition
::COND_FALSE
;
1559 return parent
::buildCondition($uf);
1563 // {{{ class UFC_MarketingHash
1564 /** Filters users using the hash generated
1565 * to send marketing emails to him.
1567 class UFC_MarketingHash
extends UserFilterCondition
1571 public function __construct($hash)
1573 $this->hash
= $hash;
1576 public function buildCondition(PlFilter
$uf)
1578 $table = $uf->addMarketingHash();
1579 return XDB
::format('rm.hash = {?}', $this->hash
);
1584 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: