c7d5bb3e038b02afd57c0bf0b7e57c800711e4e7
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['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 Array of school ids
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 /** Construct a UFC_SchoolId
452 * The first argument is the type, all following arguments can be either ids
453 * or arrays of ids to use:
454 * $ufc = new UFC_SchoolId(UFC_SchoolId::AX, $id1, $id2, array($id3, $id4));
456 public function __construct($type)
459 $ids = func_get_args();
461 $this->ids
= pl_flatten($ids);
462 self
::assertType($type);
465 public function buildCondition(PlFilter
$uf)
467 $uf->requireProfiles();
470 if ($type == self
::School
) {
472 $ids = array_map(array('Profile', 'getXorgId'), $ids);
474 return XDB
::format('p.' . $type . '_id IN {?}', $ids);
478 // {{{ class UFC_EducationSchool
479 /** Filters users by formation
480 * @param $val The formation to search (either ID or array of IDs)
482 class UFC_EducationSchool
extends UserFilterCondition
486 public function __construct()
488 $this->val
= pl_flatten(func_get_args());
491 public function buildCondition(PlFilter
$uf)
493 $sub = $uf->addEducationFilter();
494 return XDB
::format('pe' . $sub . '.eduid IN {?}', $this->val
);
498 // {{{ class UFC_EducationDegree
499 class UFC_EducationDegree
extends UserFilterCondition
503 public function __construct()
505 $this->diploma
= pl_flatten(func_get_args());
508 public function buildCondition(PlFilter
$uf)
510 $sub = $uf->addEducationFilter();
511 return XDB
::format('pe' . $sub . '.degreeid IN {?}', $this->diploma
);
515 // {{{ class UFC_EducationField
516 class UFC_EducationField
extends UserFilterCondition
520 public function __construct()
522 $this->val
= pl_flatten(func_get_args());
525 public function buildCondition(PlFilter
$uf)
527 $sub = $uf->addEducationFilter();
528 return XDB
::format('pe' . $sub . '.fieldid IN {?}', $this->val
);
532 // {{{ class UFC_Name
533 /** Filters users based on name
534 * @param $type Type of name field on which filtering is done (firstname, lastname...)
535 * @param $text Text on which to filter
536 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
538 class UFC_Name
extends UserFilterCondition
540 const EXACT
= XDB
::WILDCARD_EXACT
; // 0x000
541 const PREFIX
= XDB
::WILDCARD_PREFIX
; // 0x001
542 const SUFFIX
= XDB
::WILDCARD_SUFFIX
; // 0x002
543 const CONTAINS
= XDB
::WILDCARD_CONTAINS
; // 0x003
544 const PARTICLE
= 0x004;
545 const VARIANTS
= 0x008;
551 public function __construct($type, $text, $mode)
558 private function buildNameQuery($type, $variant, $where, UserFilter
$uf)
560 $sub = $uf->addNameFilter($type, $variant);
561 return str_replace('$ME', 'pn' . $sub, $where);
564 public function buildCondition(PlFilter
$uf)
567 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
568 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
570 $right = XDB
::formatWildcards($this->mode
& self
::CONTAINS
, $this->text
);
572 $cond = $left . $right;
573 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
574 if (($this->mode
& self
::VARIANTS
) != 0 && isset(Profile
::$name_variants[$this->type
])) {
575 foreach (Profile
::$name_variants[$this->type
] as $var) {
576 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
579 return implode(' OR ', $conds);
582 public function export()
584 $export = $this->buildExport($this->type
);
585 if ($this->mode
& self
::VARIANTS
) {
586 $export['search_in_variants'] = true
;
588 if ($this->mode
& self
::PARTICLE
) {
589 $export['search_in_particle'] = true
;
591 $export['comparison'] = self
::comparisonFromXDBWildcard($this->mode
& 0x3);
592 $export['text'] = $this->text
;
597 // {{{ class UFC_NameTokens
598 /** Selects users based on tokens in their name (for quicksearch)
599 * @param $tokens An array of tokens to search
600 * @param $flags Flags the tokens must have (e.g 'public' for public search)
601 * @param $soundex (bool) Whether those tokens are fulltext or soundex
603 class UFC_NameTokens
extends UserFilterCondition
606 const FLAG_PUBLIC
= 'public';
613 public function __construct($tokens, $flags = array(), $soundex = false
, $exact = false
)
615 if (is_array($tokens)) {
616 $this->tokens
= $tokens;
618 $this->tokens
= array($tokens);
620 if (is_array($flags)) {
621 $this->flags
= $flags;
623 $this->flags
= array($flags);
625 $this->soundex
= $soundex;
626 $this->exact
= $exact;
629 public function buildCondition(PlFilter
$uf)
632 foreach ($this->tokens
as $i => $token) {
633 $sub = $uf->addNameTokensFilter($token);
634 if ($this->soundex
) {
635 $c = XDB
::format($sub . '.soundex = {?}', soundex_fr($token));
636 } else if ($this->exact
) {
637 $c = XDB
::format($sub . '.token = {?}', $token);
639 $c = $sub . '.token ' . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $token);
641 if ($this->flags
!= null
) {
642 $c .= XDB
::format(' AND ' . $sub . '.flags IN {?}', $this->flags
);
647 return implode(' AND ', $conds);
651 // {{{ class UFC_Nationality
652 class UFC_Nationality
extends UserFilterCondition
656 public function __construct()
658 $this->val
= pl_flatten(func_get_args());
661 public function buildCondition(PlFilter
$uf)
663 $uf->requireProfiles();
664 $nat = XDB
::formatArray($this->val
);
666 'p.nationality1 IN ' . $nat,
667 'p.nationality2 IN ' . $nat,
668 'p.nationality3 IN ' . $nat,
670 return implode(' OR ', $conds);
674 // {{{ class UFC_Dead
675 /** Filters users based on death date
676 * @param $comparison Comparison operator
677 * @param $date Date to which death date should be compared (DateTime object, string or timestamp)
679 class UFC_Dead
extends UserFilterCondition
684 public function __construct($comparison = null
, $date = null
)
686 $this->comparison
= $comparison;
687 $this->date
= make_datetime($date);
690 public function buildCondition(PlFilter
$uf)
692 $uf->requireProfiles();
693 $str = 'p.deathdate IS NOT NULL';
694 if (!is_null($this->comparison
)) {
695 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->date
->format('Y-m-d'));
701 // {{{ class UFC_Registered
702 /** Filters users based on registration state
703 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
704 * @param $comparison Comparison operator
705 * @param $date Date to which users registration date should be compared
707 class UFC_Registered
extends UserFilterCondition
713 public function __construct($active = false
, $comparison = null
, $date = null
)
715 $this->active
= $active;
716 $this->comparison
= $comparison;
717 $this->date
= make_datetime($date);
720 public function buildCondition(PlFilter
$uf)
722 $uf->requireAccounts();
724 $date = '$UID IS NOT NULL AND a.state = \'active\'';
726 $date = '$UID IS NOT NULL AND a.state != \'pending\'';
728 if (!is_null($this->comparison
)) {
729 $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'));
735 // {{{ class UFC_ProfileUpdated
736 /** Filters users based on profile update date
737 * @param $comparison Comparison operator
738 * @param $date Date to which profile update date must be compared
740 class UFC_ProfileUpdated
extends UserFilterCondition
745 public function __construct($comparison = null
, $date = null
)
747 $this->comparison
= $comparison;
751 public function buildCondition(PlFilter
$uf)
753 $uf->requireProfiles();
754 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
758 // {{{ class UFC_Birthday
759 /** Filters users based on next birthday date
760 * @param $comparison Comparison operator
761 * @param $date Date to which users next birthday date should be compared
763 class UFC_Birthday
extends UserFilterCondition
768 public function __construct($comparison = null
, $date = null
)
770 $this->comparison
= $comparison;
774 public function buildCondition(PlFilter
$uf)
776 $uf->requireProfiles();
777 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
782 /** Filters users based on sex
783 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
785 class UFC_Sex
extends UserFilterCondition
788 public function __construct($sex)
793 public function buildCondition(PlFilter
$uf)
795 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
796 return self
::COND_FALSE
;
798 $uf->requireProfiles();
799 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
804 // {{{ class UFC_Group
805 /** Filters users based on group membership
806 * @param $group Group whose members we are selecting
807 * @param $anim Whether to restrict selection to animators of that group
809 class UFC_Group
extends UserFilterCondition
813 public function __construct($group, $anim = false
)
815 $this->group
= $group;
819 public function buildCondition(PlFilter
$uf)
821 // Groups have AX visibility.
822 if ($uf->getVisibilityLevel() == ProfileVisibility
::VIS_PUBLIC
) {
823 return self
::COND_TRUE
;
825 $sub = $uf->addGroupFilter($this->group
);
826 $where = 'gpm' . $sub . '.perms IS NOT NULL';
828 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
834 // {{{ class UFC_Binet
835 /** Selects users based on their belonging to a given (list of) binet
836 * @param $binet either a binet_id or an array of binet_ids
838 class UFC_Binet
extends UserFilterCondition
842 public function __construct()
844 $this->val
= pl_flatten(func_get_args());
847 public function buildCondition(PlFilter
$uf)
849 // Binets are private.
850 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
851 return self
::CONF_TRUE
;
853 $sub = $uf->addBinetsFilter();
854 return XDB
::format($sub . '.binet_id IN {?}', $this->val
);
858 // {{{ class UFC_Section
859 /** Selects users based on section
860 * @param $section ID of the section
862 class UFC_Section
extends UserFilterCondition
866 public function __construct()
868 $this->section
= pl_flatten(func_get_args());
871 public function buildCondition(PlFilter
$uf)
873 // Sections are private.
874 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
875 return self
::CONF_TRUE
;
877 $uf->requireProfiles();
878 return XDB
::format('p.section IN {?}', $this->section
);
882 // {{{ class UFC_Email
883 /** Filters users based on an email or a list of emails
884 * @param $emails List of emails whose owner must be selected
886 class UFC_Email
extends UserFilterCondition
889 public function __construct()
891 $this->emails
= pl_flatten(func_get_args());
894 public function buildCondition(PlFilter
$uf)
901 if (count($this->emails
) == 0) {
902 return PlFilterCondition
::COND_TRUE
;
905 foreach ($this->emails
as $entry) {
906 if (User
::isForeignEmailAddress($entry)) {
908 } else if (User
::isVirtualEmailAddress($entry)) {
911 @list
($user, $domain) = explode('@', $entry);
916 if (count($foreign) > 0) {
917 $sub = $uf->addEmailRedirectFilter($foreign);
918 $cond[] = XDB
::format('e' . $sub . '.email IS NOT NULL OR a.email IN {?}', $foreign);
920 if (count($virtual) > 0) {
921 $sub = $uf->addVirtualEmailFilter($virtual);
922 $cond[] = 'vr' . $sub . '.redirect IS NOT NULL';
924 if (count($aliases) > 0) {
925 $sub = $uf->addAliasFilter($aliases);
926 $cond[] = 'al' . $sub . '.alias IS NOT NULL';
928 return '(' . implode(') OR (', $cond) . ')';
932 // {{{ class UFC_Address
933 abstract class UFC_Address
extends UserFilterCondition
935 /** Valid address type ('hq' is reserved for company addresses)
941 /** Text for these types
943 protected static $typetexts = array(
944 self
::TYPE_HOME
=> 'home',
945 self
::TYPE_PRO
=> 'pro',
950 /** Flags for addresses
952 const FLAG_CURRENT
= 0x0001;
953 const FLAG_TEMP
= 0x0002;
954 const FLAG_SECOND
= 0x0004;
955 const FLAG_MAIL
= 0x0008;
956 const FLAG_CEDEX
= 0x0010;
958 // Binary OR of those flags
959 const FLAG_ANY
= 0x001F;
961 /** Text of these flags
963 protected static $flagtexts = array(
964 self
::FLAG_CURRENT
=> 'current',
965 self
::FLAG_TEMP
=> 'temporary',
966 self
::FLAG_SECOND
=> 'secondary',
967 self
::FLAG_MAIL
=> 'mail',
968 self
::FLAG_CEDEX
=> 'cedex',
973 public function __construct($type = null
, $flags = null
)
976 $this->flags
= $flags;
979 protected function initConds($sub, $vis_cond)
981 $conds = array($vis_cond);
984 foreach (self
::$typetexts as $flag => $type) {
985 if ($flag & $this->type
) {
990 $conds[] = XDB
::format($sub . '.type IN {?}', $types);
993 if ($this->flags
!= self
::FLAG_ANY
) {
994 foreach(self
::$flagtexts as $flag => $text) {
995 if ($flag & $this->flags
) {
996 $conds[] = 'FIND_IN_SET(' . XDB
::format('{?}', $text) . ', ' . $sub . '.flags)';
1005 // {{{ class UFC_AddressText
1006 /** Select users based on their address, using full text search
1007 * @param $text Text for filter in fulltext search
1008 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
1009 * @param $type Filter on address type
1010 * @param $flags Filter on address flags
1011 * @param $country Filter on address country
1012 * @param $locality Filter on address locality
1014 class UFC_AddressText
extends UFC_Address
1018 private $textSearchMode;
1020 public function __construct($text = null
, $textSearchMode = XDB
::WILDCARD_CONTAINS
,
1021 $type = null
, $flags = self
::FLAG_ANY
, $country = null
, $locality = null
)
1023 parent
::__construct($type, $flags);
1024 $this->text
= $text;
1025 $this->textSearchMode
= $textSearchMode;
1026 $this->country
= $country;
1027 $this->locality
= $locality;
1030 private function mkMatch($txt)
1032 return XDB
::formatWildcards($this->textSearchMode
, $txt);
1035 public function buildCondition(PlFilter
$uf)
1037 $sub = $uf->addAddressFilter();
1038 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1039 if ($this->text
!= null
) {
1040 $conds[] = $sub . '.text' . $this->mkMatch($this->text
);
1043 if ($this->country
!= null
) {
1044 $subc = $uf->addAddressCountryFilter();
1045 $subconds = array();
1046 $subconds[] = $subc . '.country' . $this->mkMatch($this->country
);
1047 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country
);
1048 $conds[] = implode(' OR ', $subconds);
1051 if ($this->locality
!= null
) {
1052 $subl = $uf->addAddressLocalityFilter();
1053 $conds[] = $subl . '.name' . $this->mkMatch($this->locality
);
1056 return implode(' AND ', $conds);
1060 // {{{ class UFC_AddressField
1061 /** Filters users based on their address,
1062 * @param $val Either a code for one of the fields, or an array of such codes
1063 * @param $fieldtype The type of field to look for
1064 * @param $type Filter on address type
1065 * @param $flags Filter on address flags
1067 class UFC_AddressField
extends UFC_Address
1069 const FIELD_COUNTRY
= 1;
1070 const FIELD_ADMAREA
= 2;
1071 const FIELD_SUBADMAREA
= 3;
1072 const FIELD_LOCALITY
= 4;
1073 const FIELD_ZIPCODE
= 5;
1075 /** Data of the filter
1080 public function __construct($val, $fieldtype, $type = null
, $flags = self
::FLAG_ANY
)
1082 parent
::__construct($type, $flags);
1084 if (!is_array($val)) {
1088 $this->fieldtype
= $fieldtype;
1091 public function buildCondition(PlFilter
$uf)
1093 $sub = $uf->addAddressFilter();
1094 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1096 switch ($this->fieldtype
) {
1097 case self
::FIELD_COUNTRY
:
1098 $field = 'countryId';
1100 case self
::FIELD_ADMAREA
:
1101 $field = 'administrativeAreaId';
1103 case self
::FIELD_SUBADMAREA
:
1104 $field = 'subAdministrativeAreaId';
1106 case self
::FIELD_LOCALITY
:
1107 $field = 'localityId';
1109 case self
::FIELD_ZIPCODE
:
1110 $field = 'postalCode';
1113 Platal
::page()->killError('Invalid address field type: ' . $this->fieldtype
);
1115 $conds[] = XDB
::format($sub . '.' . $field . ' IN {?}', $this->val
);
1117 return implode(' AND ', $conds);
1121 // {{{ class UFC_Corps
1122 /** Filters users based on the corps they belong to
1123 * @param $corps Corps we are looking for (abbreviation)
1124 * @param $type Whether we search for original or current corps
1126 class UFC_Corps
extends UserFilterCondition
1134 public function __construct($corps, $type = self
::CURRENT
)
1136 $this->corps
= $corps;
1137 $this->type
= $type;
1140 public function buildCondition(PlFilter
$uf)
1142 /** Tables shortcuts:
1143 * pc for profile_corps,
1144 * pceo for profile_corps_enum - orginal
1145 * pcec for profile_corps_enum - current
1147 $sub = $uf->addCorpsFilter($this->type
);
1148 $cond = $sub . '.abbreviation = ' . $corps;
1149 $cond .= ' AND ' . $uf->getVisibilityCondition($sub . '.corps_pub');
1154 // {{{ class UFC_Corps_Rank
1155 /** Filters users based on their rank in the corps
1156 * @param $rank Rank we are looking for (abbreviation)
1158 class UFC_Corps_Rank
extends UserFilterCondition
1161 public function __construct($rank)
1163 $this->rank
= $rank;
1166 public function buildCondition(PlFilter
$uf)
1168 /** Tables shortcuts:
1169 * pc for profile_corps
1170 * pcr for profile_corps_rank
1172 $sub = $uf->addCorpsRankFilter();
1173 $cond = $sub . '.abbreviation = ' . $rank;
1174 // XXX(x2006barrois): find a way to get rid of that hardcoded
1175 // reference to 'pc'.
1176 $cond .= ' AND ' . $uf->getVisibilityCondition('pc.corps_pub');
1181 // {{{ class UFC_Job_Company
1182 /** Filters users based on the company they belong to
1183 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
1184 * @param $value The searched value
1186 class UFC_Job_Company
extends UserFilterCondition
1189 const JOBNAME
= 'name';
1190 const JOBACRONYM
= 'acronym';
1195 public function __construct($type, $value)
1197 $this->assertType($type);
1198 $this->type
= $type;
1199 $this->value
= $value;
1202 private function assertType($type)
1204 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
1205 Platal
::page()->killError("Type de recherche non valide.");
1209 public function buildCondition(PlFilter
$uf)
1211 $sub = $uf->addJobCompanyFilter();
1212 $cond = $sub . '.' . $this->type
. XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1213 $jsub = $uf->addJobFilter();
1214 $cond .= ' AND ' . $uf->getVisibilityCondition($jsub . '.pub');
1219 // {{{ class UFC_Job_Terms
1220 /** Filters users based on the job terms they assigned to one of their
1222 * @param $val The ID of the job term, or an array of such IDs
1224 class UFC_Job_Terms
extends UserFilterCondition
1228 public function __construct($val)
1230 if (!is_array($val)) {
1236 public function buildCondition(PlFilter
$uf)
1238 $sub = $uf->addJobTermsFilter(count($this->val
));
1239 $conditions = array();
1240 foreach ($this->val
as $i => $jtid) {
1241 $conditions[] = $sub[$i] . '.jtid_1 = ' . XDB
::escape($jtid);
1243 $jsub = $uf->addJobFilter();
1244 $conditions[] = $uf->getVisibilityCondition($jsub . '.pub');
1245 return implode(' AND ', $conditions);
1249 // {{{ class UFC_Job_Description
1250 /** Filters users based on their job description
1251 * @param $description The text being searched for
1252 * @param $fields The fields to search for (CV, user-defined)
1254 class UFC_Job_Description
extends UserFilterCondition
1257 private $description;
1260 public function __construct($description, $fields)
1262 $this->fields
= $fields;
1263 $this->description
= $description;
1266 public function buildCondition(PlFilter
$uf)
1270 $jsub = $uf->addJobFilter();
1271 // CV is private => if only CV requested, and not private,
1272 // don't do anything. Otherwise restrict to standard job visibility.
1273 if ($this->fields
== UserFilter
::JOB_CV
) {
1274 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
1275 return self
::CONF_TRUE
;
1278 $conds[] = $uf->getVisibilityCondition($jsub . '.pub');
1281 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
1282 $conds[] = $jsub . '.description ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1284 if ($this->fields
& UserFilter
::JOB_CV
&& $uf->getVisibilityLevel() == ProfileVisibility
::VIS_PRIVATE
) {
1285 $uf->requireProfiles();
1286 $conds[] = 'p.cv ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1288 return implode(' OR ', $conds);
1292 // {{{ class UFC_Networking
1293 /** Filters users based on network identity (IRC, ...)
1294 * @param $type Type of network (-1 for any)
1295 * @param $value Value to search
1297 class UFC_Networking
extends UserFilterCondition
1302 public function __construct($type, $value)
1304 $this->type
= $type;
1305 $this->value
= $value;
1308 public function buildCondition(PlFilter
$uf)
1310 $sub = $uf->addNetworkingFilter();
1312 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1313 $conds[] = $sub . '.address ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1314 if ($this->type
!= -1) {
1315 $conds[] = $sub . '.nwid = ' . XDB
::format('{?}', $this->type
);
1317 return implode(' AND ', $conds);
1321 // {{{ class UFC_Phone
1322 /** Filters users based on their phone number
1323 * @param $num_type Type of number (pro/user/home)
1324 * @param $phone_type Type of phone (fixed/mobile/fax)
1325 * @param $number Phone number
1327 class UFC_Phone
extends UserFilterCondition
1329 const NUM_PRO
= 'pro';
1330 const NUM_USER
= 'user';
1331 const NUM_HOME
= 'address';
1332 const NUM_ANY
= 'any';
1334 const PHONE_FIXED
= 'fixed';
1335 const PHONE_MOBILE
= 'mobile';
1336 const PHONE_FAX
= 'fax';
1337 const PHONE_ANY
= 'any';
1340 private $phone_type;
1343 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
1345 $phone = new Phone(array('display' => $number));
1347 $this->number
= $phone->search
;
1348 $this->num_type
= $num_type;
1349 $this->phone_type
= $phone_type;
1352 public function buildCondition(PlFilter
$uf)
1354 $sub = $uf->addPhoneFilter();
1357 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1359 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
1360 if ($this->num_type
!= self
::NUM_ANY
) {
1361 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
1363 if ($this->phone_type
!= self
::PHONE_ANY
) {
1364 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
1366 return implode(' AND ', $conds);
1370 // {{{ class UFC_Medal
1371 /** Filters users based on their medals
1372 * @param $medal ID of the medal
1373 * @param $grade Grade of the medal (null for 'any')
1375 class UFC_Medal
extends UserFilterCondition
1380 public function __construct($medal, $grade = null
)
1382 $this->medal
= $medal;
1383 $this->grade
= $grade;
1386 public function buildCondition(PlFilter
$uf)
1390 // This will require profiles => table 'p' will be available.
1391 $sub = $uf->addMedalFilter();
1393 $conds[] = $uf->getVisibilityCondition('p.medals_pub');
1395 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
1396 if ($this->grade
!= null
) {
1397 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
1399 return implode(' AND ', $conds);
1403 // {{{ class UFC_Photo
1404 /** Filters profiles with photo
1406 class UFC_Photo
extends UserFilterCondition
1408 public function buildCondition(PlFilter
$uf)
1410 $sub = $uf->addPhotoFilter();
1411 return $sub . '.attach IS NOT NULL AND ' . $uf->getVisibilityCondition($sub . '.pub');
1415 // {{{ class UFC_Mentor
1416 class UFC_Mentor
extends UserFilterCondition
1418 public function buildCondition(PlFilter
$uf)
1420 $sub = $uf->addMentorFilter(UserFilter
::MENTOR
);
1421 return $sub . '.expertise IS NOT NULL';
1425 // {{{ class UFC_Mentor_Expertise
1426 /** Filters users by mentoring expertise
1427 * @param $expertise Domain of expertise
1429 class UFC_Mentor_Expertise
extends UserFilterCondition
1433 public function __construct($expertise)
1435 $this->expertise
= $expertise;
1438 public function buildCondition(PlFilter
$uf)
1440 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_EXPERTISE
);
1441 return $sub . '.expertise ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->expertise
);
1445 // {{{ class UFC_Mentor_Country
1446 /** Filters users by mentoring country
1447 * @param $country Two-letters code of country being searched
1449 class UFC_Mentor_Country
extends UserFilterCondition
1453 public function __construct()
1455 $this->country
= pl_flatten(func_get_args());
1458 public function buildCondition(PlFilter
$uf)
1460 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_COUNTRY
);
1461 return $sub . '.country IN ' . XDB
::format('{?}', $this->country
);
1465 // {{{ class UFC_Mentor_Terms
1466 /** Filters users based on the job terms they used in mentoring.
1467 * @param $val The ID of the job term, or an array of such IDs
1469 class UFC_Mentor_Terms
extends UserFilterCondition
1473 public function __construct($val)
1478 public function buildCondition(PlFilter
$uf)
1480 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_TERM
);
1481 return $sub . '.jtid_1 = ' . XDB
::escape($this->val
);
1485 // {{{ class UFC_UserRelated
1486 /** Filters users based on a relation toward a user
1487 * @param $user User to which searched users are related
1489 abstract class UFC_UserRelated
extends UserFilterCondition
1492 public function __construct(PlUser
&$user)
1494 $this->user
=& $user;
1498 // {{{ class UFC_Contact
1499 /** Filters users who belong to selected user's contacts
1501 class UFC_Contact
extends UFC_UserRelated
1503 public function buildCondition(PlFilter
$uf)
1505 $sub = $uf->addContactFilter($this->user
->id());
1506 return 'c' . $sub . '.contact IS NOT NULL';
1510 // {{{ class UFC_WatchRegistration
1511 /** Filters users being watched by selected user
1513 class UFC_WatchRegistration
extends UFC_UserRelated
1515 public function buildCondition(PlFilter
$uf)
1517 if (!$this->user
->watchType('registration')) {
1518 return PlFilterCondition
::COND_FALSE
;
1520 $uids = $this->user
->watchUsers();
1521 if (count($uids) == 0) {
1522 return PlFilterCondition
::COND_FALSE
;
1524 return XDB
::format('$UID IN {?}', $uids);
1529 // {{{ class UFC_WatchPromo
1530 /** Filters users belonging to a promo watched by selected user
1531 * @param $user Selected user (the one watching promo)
1532 * @param $grade Formation the user is watching
1534 class UFC_WatchPromo
extends UFC_UserRelated
1537 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
1539 parent
::__construct($user);
1540 $this->grade
= $grade;
1543 public function buildCondition(PlFilter
$uf)
1545 $promos = $this->user
->watchPromos();
1546 if (count($promos) == 0) {
1547 return PlFilterCondition
::COND_FALSE
;
1549 $sube = $uf->addEducationFilter(true
, $this->grade
);
1550 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
1551 return XDB
::format($field . ' IN {?}', $promos);
1556 // {{{ class UFC_WatchContact
1557 /** Filters users watched by selected user
1559 class UFC_WatchContact
extends UFC_Contact
1561 public function buildCondition(PlFilter
$uf)
1563 if (!$this->user
->watchContacts()) {
1564 return PlFilterCondition
::COND_FALSE
;
1566 return parent
::buildCondition($uf);
1570 // {{{ class UFC_MarketingHash
1571 /** Filters users using the hash generated
1572 * to send marketing emails to him.
1574 class UFC_MarketingHash
extends UserFilterCondition
1578 public function __construct($hash)
1580 $this->hash
= $hash;
1583 public function buildCondition(PlFilter
$uf)
1585 $table = $uf->addMarketingHash();
1586 return XDB
::format('rm.hash = {?}', $this->hash
);
1591 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: