766ee0c11f5c9dedb1b445a565325625614f8177
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
;
327 // {{{ class UFC_HasEmailRedirect
328 /** Filters users, keeping only those with a valid email redirection.
330 class UFC_HasEmailRedirect
extends UserFilterCondition
332 public function buildCondition(PlFilter
$uf)
334 $sub_redirect = $uf->addEmailRedirectFilter();
335 $sub_options = $uf->addEmailOptionsFilter();
336 return 'e' . $sub_redirect . '.flags = \'active\' OR FIND_IN_SET(\'googleapps\', ' . $sub_options . '.storage)';
341 /** Filters users based on one of their last IPs
342 * @param $ip IP from which connection are checked
344 class UFC_Ip
extends UserFilterCondition
348 public function __construct($ip)
353 public function buildCondition(PlFilter
$uf)
355 $sub = $uf->addLoggerFilter();
356 $ip = ip_to_uint($this->ip
);
357 return XDB
::format($sub . '.ip = {?} OR ' . $sub . '.forward_ip = {?}', $ip, $ip);
360 public function export()
362 $export = $this->buildExport('host');
363 $export['ip'] = $this->ip
;
368 // {{{ class UFC_Comment
369 class UFC_Comment
extends UserFilterCondition
373 public function __construct($text)
378 public function buildCondition(PlFilter
$uf)
380 $uf->requireProfiles();
381 return $uf->getVisibilityCondition('p.freetext_pub') . ' AND p.freetext ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->text
);
384 public function export()
386 $export = $this->buildExport('comment');
387 $export['comparison'] = self
::OP_CONTAINS
;
388 $export['text'] = $this->text
;
393 // {{{ class UFC_Promo
394 /** Filters users based on promotion
395 * @param $comparison Comparison operator (>, =, ...)
396 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
397 * @param $promo Promotion on which the filter is based
399 class UFC_Promo
extends UserFilterCondition
406 public function __construct($comparison, $grade, $promo)
408 $this->grade
= $grade;
409 $this->comparison
= $comparison;
410 $this->promo
= $promo;
411 if ($this->grade
!= UserFilter
::DISPLAY
) {
412 UserFilter
::assertGrade($this->grade
);
414 if ($this->grade
== UserFilter
::DISPLAY
&& $this->comparison
!= '=') {
415 // XXX: we might try to guess the grade from the first char of the promo and forbid only '<= 2004', but allow '<= X2004'
416 Platal
::page()->killError("Il n'est pas possible d'appliquer la comparaison '" . $this->comparison
. "' aux promotions sans spécifier de formation (X/M/D)");
420 public function buildCondition(PlFilter
$uf)
422 if ($this->grade
== UserFilter
::DISPLAY
) {
423 $sub = $uf->addDisplayFilter();
424 return XDB
::format('pd' . $sub . '.promo ' . $this->comparison
. ' {?}', $this->promo
);
426 $sub = $uf->addEducationFilter(true
, $this->grade
);
427 $field = 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
428 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->promo
);
432 public function export()
434 $export = $this->buildExport('promo');
435 $export['comparison'] = $this->comparison
;
436 if ($this->grade
!= UserFilter
::DISPLAY
) {
437 $export['grade'] = $this->grade
;
439 $export['promo'] = $this->promo
;
444 // {{{ class UFC_SchoolId
445 /** Filters users based on their shoold identifier
446 * @param type Parameter type (Xorg, AX, School)
447 * @param value Array of school ids
449 class UFC_SchoolId
extends UserFilterCondition
453 const School
= 'school';
458 static public function assertType($type)
460 if ($type != self
::AX
&& $type != self
::Xorg
&& $type != self
::School
) {
461 Platal
::page()->killError("Type de matricule invalide: $type");
465 /** Construct a UFC_SchoolId
466 * The first argument is the type, all following arguments can be either ids
467 * or arrays of ids to use:
468 * $ufc = new UFC_SchoolId(UFC_SchoolId::AX, $id1, $id2, array($id3, $id4));
470 public function __construct($type)
473 $ids = func_get_args();
475 $this->ids
= pl_flatten($ids);
476 self
::assertType($type);
479 public function buildCondition(PlFilter
$uf)
481 $uf->requireProfiles();
484 if ($type == self
::School
) {
486 $ids = array_map(array('Profile', 'getXorgId'), $ids);
488 return XDB
::format('p.' . $type . '_id IN {?}', $ids);
492 // {{{ class UFC_EducationSchool
493 /** Filters users by formation
494 * @param $val The formation to search (either ID or array of IDs)
496 class UFC_EducationSchool
extends UserFilterCondition
500 public function __construct()
502 $this->val
= pl_flatten(func_get_args());
505 public function buildCondition(PlFilter
$uf)
507 $sub = $uf->addEducationFilter();
508 return XDB
::format('pe' . $sub . '.eduid IN {?}', $this->val
);
512 // {{{ class UFC_EducationDegree
513 class UFC_EducationDegree
extends UserFilterCondition
517 public function __construct()
519 $this->diploma
= pl_flatten(func_get_args());
522 public function buildCondition(PlFilter
$uf)
524 $sub = $uf->addEducationFilter();
525 return XDB
::format('pe' . $sub . '.degreeid IN {?}', $this->diploma
);
529 // {{{ class UFC_EducationField
530 class UFC_EducationField
extends UserFilterCondition
534 public function __construct()
536 $this->val
= pl_flatten(func_get_args());
539 public function buildCondition(PlFilter
$uf)
541 $sub = $uf->addEducationFilter();
542 return XDB
::format('pe' . $sub . '.fieldid IN {?}', $this->val
);
546 // {{{ class UFC_Name
547 /** Filters users based on name
548 * @param $type Type of name field on which filtering is done (firstname, lastname...)
549 * @param $text Text on which to filter
550 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
552 class UFC_Name
extends UserFilterCondition
554 const EXACT
= XDB
::WILDCARD_EXACT
; // 0x000
555 const PREFIX
= XDB
::WILDCARD_PREFIX
; // 0x001
556 const SUFFIX
= XDB
::WILDCARD_SUFFIX
; // 0x002
557 const CONTAINS
= XDB
::WILDCARD_CONTAINS
; // 0x003
558 const PARTICLE
= 0x004;
559 const VARIANTS
= 0x008;
565 public function __construct($type, $text, $mode)
572 private function buildNameQuery($type, $variant, $where, UserFilter
$uf)
574 $sub = $uf->addNameFilter($type, $variant);
575 return str_replace('$ME', 'pn' . $sub, $where);
578 public function buildCondition(PlFilter
$uf)
581 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
582 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
584 $right = XDB
::formatWildcards($this->mode
& self
::CONTAINS
, $this->text
);
586 $cond = $left . $right;
587 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
588 if (($this->mode
& self
::VARIANTS
) != 0 && isset(Profile
::$name_variants[$this->type
])) {
589 foreach (Profile
::$name_variants[$this->type
] as $var) {
590 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
593 return implode(' OR ', $conds);
596 public function export()
598 $export = $this->buildExport($this->type
);
599 if ($this->mode
& self
::VARIANTS
) {
600 $export['search_in_variants'] = true
;
602 if ($this->mode
& self
::PARTICLE
) {
603 $export['search_in_particle'] = true
;
605 $export['comparison'] = self
::comparisonFromXDBWildcard($this->mode
& 0x3);
606 $export['text'] = $this->text
;
611 // {{{ class UFC_NameTokens
612 /** Selects users based on tokens in their name (for quicksearch)
613 * @param $tokens An array of tokens to search
614 * @param $flags Flags the tokens must have (e.g 'public' for public search)
615 * @param $soundex (bool) Whether those tokens are fulltext or soundex
617 class UFC_NameTokens
extends UserFilterCondition
620 const FLAG_PUBLIC
= 'public';
627 public function __construct($tokens, $flags = array(), $soundex = false
, $exact = false
)
629 if (is_array($tokens)) {
630 $this->tokens
= $tokens;
632 $this->tokens
= array($tokens);
634 if (is_array($flags)) {
635 $this->flags
= $flags;
637 $this->flags
= array($flags);
639 $this->soundex
= $soundex;
640 $this->exact
= $exact;
643 public function buildCondition(PlFilter
$uf)
646 foreach ($this->tokens
as $i => $token) {
647 $sub = $uf->addNameTokensFilter($token);
648 if ($this->soundex
) {
649 $c = XDB
::format($sub . '.soundex = {?}', soundex_fr($token));
650 } else if ($this->exact
) {
651 $c = XDB
::format($sub . '.token = {?}', $token);
653 $c = $sub . '.token ' . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $token);
655 if ($this->flags
!= null
) {
656 $c .= XDB
::format(' AND ' . $sub . '.flags IN {?}', $this->flags
);
661 return implode(' AND ', $conds);
665 // {{{ class UFC_Nationality
666 class UFC_Nationality
extends UserFilterCondition
670 public function __construct()
672 $this->val
= pl_flatten(func_get_args());
675 public function buildCondition(PlFilter
$uf)
677 $uf->requireProfiles();
678 $nat = XDB
::formatArray($this->val
);
680 'p.nationality1 IN ' . $nat,
681 'p.nationality2 IN ' . $nat,
682 'p.nationality3 IN ' . $nat,
684 return implode(' OR ', $conds);
688 // {{{ class UFC_Dead
689 /** Filters users based on death date
690 * @param $comparison Comparison operator
691 * @param $date Date to which death date should be compared (DateTime object, string or timestamp)
693 class UFC_Dead
extends UserFilterCondition
698 public function __construct($comparison = null
, $date = null
)
700 $this->comparison
= $comparison;
701 $this->date
= make_datetime($date);
704 public function buildCondition(PlFilter
$uf)
706 $uf->requireProfiles();
707 $str = 'p.deathdate IS NOT NULL';
708 if (!is_null($this->comparison
)) {
709 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->date
->format('Y-m-d'));
715 // {{{ class UFC_Registered
716 /** Filters users based on registration state
717 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
718 * @param $comparison Comparison operator
719 * @param $date Date to which users registration date should be compared
721 class UFC_Registered
extends UserFilterCondition
727 public function __construct($active = false
, $comparison = null
, $date = null
)
729 $this->active
= $active;
730 $this->comparison
= $comparison;
731 $this->date
= make_datetime($date);
734 public function buildCondition(PlFilter
$uf)
736 $uf->requireAccounts();
738 $date = '$UID IS NOT NULL AND a.state = \'active\'';
740 $date = '$UID IS NOT NULL AND a.state != \'pending\'';
742 if (!is_null($this->comparison
)) {
743 $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'));
749 // {{{ class UFC_ProfileUpdated
750 /** Filters users based on profile update date
751 * @param $comparison Comparison operator
752 * @param $date Date to which profile update date must be compared
754 class UFC_ProfileUpdated
extends UserFilterCondition
759 public function __construct($comparison = null
, $date = null
)
761 $this->comparison
= $comparison;
765 public function buildCondition(PlFilter
$uf)
767 $uf->requireProfiles();
768 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
772 // {{{ class UFC_Birthday
773 /** Filters users based on next birthday date
774 * @param $comparison Comparison operator
775 * @param $date Date to which users next birthday date should be compared
777 class UFC_Birthday
extends UserFilterCondition
782 public function __construct($comparison = null
, $date = null
)
784 $this->comparison
= $comparison;
788 public function buildCondition(PlFilter
$uf)
790 $uf->requireProfiles();
791 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
796 /** Filters users based on sex
797 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
799 class UFC_Sex
extends UserFilterCondition
802 public function __construct($sex)
807 public function buildCondition(PlFilter
$uf)
809 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
810 return self
::COND_FALSE
;
812 $uf->requireProfiles();
813 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
818 // {{{ class UFC_NLSubscribed
819 /** Filters users based on NL subscription
820 * @param $nlid NL whose subscribers we are selecting
821 * @param $issue Select only subscribers who have not yet received that issue
823 class UFC_NLSubscribed
extends UserFilterCondition
827 public function __construct($nlid, $issue_id)
830 $this->issue_id
= $issue_id;
833 public function buildCondition(PlFilter
$uf)
835 $sub = $uf->addNewsLetterFilter($this->nlid
);
836 return XDB
::format($sub . '.last < {?}', $this->issue_id
);
840 // {{{ class UFC_Group
841 /** Filters users based on group membership
842 * @param $group Group whose members we are selecting
843 * @param $anim Whether to restrict selection to animators of that group
845 class UFC_Group
extends UserFilterCondition
849 public function __construct($group, $anim = false
)
851 $this->group
= $group;
855 public function buildCondition(PlFilter
$uf)
857 // Groups have AX visibility.
858 if ($uf->getVisibilityLevel() == ProfileVisibility
::VIS_PUBLIC
) {
859 return self
::COND_TRUE
;
861 $sub = $uf->addGroupFilter($this->group
);
862 $where = 'gpm' . $sub . '.perms IS NOT NULL';
864 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
870 // {{{ class UFC_Binet
871 /** Selects users based on their belonging to a given (list of) binet
872 * @param $binet either a binet_id or an array of binet_ids
874 class UFC_Binet
extends UserFilterCondition
878 public function __construct()
880 $this->val
= pl_flatten(func_get_args());
883 public function buildCondition(PlFilter
$uf)
885 // Binets are private.
886 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
887 return self
::CONF_TRUE
;
889 $sub = $uf->addBinetsFilter();
890 return XDB
::format($sub . '.binet_id IN {?}', $this->val
);
894 // {{{ class UFC_Section
895 /** Selects users based on section
896 * @param $section ID of the section
898 class UFC_Section
extends UserFilterCondition
902 public function __construct()
904 $this->section
= pl_flatten(func_get_args());
907 public function buildCondition(PlFilter
$uf)
909 // Sections are private.
910 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
911 return self
::CONF_TRUE
;
913 $uf->requireProfiles();
914 return XDB
::format('p.section IN {?}', $this->section
);
918 // {{{ class UFC_Email
919 /** Filters users based on an email or a list of emails
920 * @param $emails List of emails whose owner must be selected
922 class UFC_Email
extends UserFilterCondition
925 public function __construct()
927 $this->emails
= pl_flatten(func_get_args());
930 public function buildCondition(PlFilter
$uf)
937 if (count($this->emails
) == 0) {
938 return PlFilterCondition
::COND_TRUE
;
941 foreach ($this->emails
as $entry) {
942 if (User
::isForeignEmailAddress($entry)) {
944 } else if (User
::isVirtualEmailAddress($entry)) {
947 @list
($user, $domain) = explode('@', $entry);
952 if (count($foreign) > 0) {
953 $sub = $uf->addEmailRedirectFilter($foreign);
954 $cond[] = XDB
::format('e' . $sub . '.email IS NOT NULL OR a.email IN {?}', $foreign);
956 if (count($virtual) > 0) {
957 $sub = $uf->addVirtualEmailFilter($virtual);
958 $cond[] = 'vr' . $sub . '.redirect IS NOT NULL';
960 if (count($aliases) > 0) {
961 $sub = $uf->addAliasFilter($aliases);
962 $cond[] = 'al' . $sub . '.alias IS NOT NULL';
964 return '(' . implode(') OR (', $cond) . ')';
968 // {{{ class UFC_Address
969 abstract class UFC_Address
extends UserFilterCondition
971 /** Valid address type ('hq' is reserved for company addresses)
977 /** Text for these types
979 protected static $typetexts = array(
980 self
::TYPE_HOME
=> 'home',
981 self
::TYPE_PRO
=> 'pro',
986 /** Flags for addresses
988 const FLAG_CURRENT
= 0x0001;
989 const FLAG_TEMP
= 0x0002;
990 const FLAG_SECOND
= 0x0004;
991 const FLAG_MAIL
= 0x0008;
992 const FLAG_CEDEX
= 0x0010;
994 // Binary OR of those flags
995 const FLAG_ANY
= 0x001F;
997 /** Text of these flags
999 protected static $flagtexts = array(
1000 self
::FLAG_CURRENT
=> 'current',
1001 self
::FLAG_TEMP
=> 'temporary',
1002 self
::FLAG_SECOND
=> 'secondary',
1003 self
::FLAG_MAIL
=> 'mail',
1004 self
::FLAG_CEDEX
=> 'cedex',
1009 public function __construct($type = null
, $flags = null
)
1011 $this->type
= $type;
1012 $this->flags
= $flags;
1015 protected function initConds($sub, $vis_cond)
1017 $conds = array($vis_cond);
1020 foreach (self
::$typetexts as $flag => $type) {
1021 if ($flag & $this->type
) {
1025 if (count($types)) {
1026 $conds[] = XDB
::format($sub . '.type IN {?}', $types);
1029 if ($this->flags
!= self
::FLAG_ANY
) {
1030 foreach(self
::$flagtexts as $flag => $text) {
1031 if ($flag & $this->flags
) {
1032 $conds[] = 'FIND_IN_SET(' . XDB
::format('{?}', $text) . ', ' . $sub . '.flags)';
1041 // {{{ class UFC_AddressText
1042 /** Select users based on their address, using full text search
1043 * @param $text Text for filter in fulltext search
1044 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
1045 * @param $type Filter on address type
1046 * @param $flags Filter on address flags
1047 * @param $country Filter on address country
1048 * @param $locality Filter on address locality
1050 class UFC_AddressText
extends UFC_Address
1054 private $textSearchMode;
1056 public function __construct($text = null
, $textSearchMode = XDB
::WILDCARD_CONTAINS
,
1057 $type = null
, $flags = self
::FLAG_ANY
, $country = null
, $locality = null
)
1059 parent
::__construct($type, $flags);
1060 $this->text
= $text;
1061 $this->textSearchMode
= $textSearchMode;
1062 $this->country
= $country;
1063 $this->locality
= $locality;
1066 private function mkMatch($txt)
1068 return XDB
::formatWildcards($this->textSearchMode
, $txt);
1071 public function buildCondition(PlFilter
$uf)
1073 $sub = $uf->addAddressFilter();
1074 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1075 if ($this->text
!= null
) {
1076 $conds[] = $sub . '.text' . $this->mkMatch($this->text
);
1079 if ($this->country
!= null
) {
1080 $subc = $uf->addAddressCountryFilter();
1081 $subconds = array();
1082 $subconds[] = $subc . '.country' . $this->mkMatch($this->country
);
1083 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country
);
1084 $conds[] = implode(' OR ', $subconds);
1087 if ($this->locality
!= null
) {
1088 $subl = $uf->addAddressLocalityFilter();
1089 $conds[] = $subl . '.name' . $this->mkMatch($this->locality
);
1092 return implode(' AND ', $conds);
1096 // {{{ class UFC_AddressField
1097 /** Filters users based on their address,
1098 * @param $val Either a code for one of the fields, or an array of such codes
1099 * @param $fieldtype The type of field to look for
1100 * @param $type Filter on address type
1101 * @param $flags Filter on address flags
1103 class UFC_AddressField
extends UFC_Address
1105 const FIELD_COUNTRY
= 1;
1106 const FIELD_ADMAREA
= 2;
1107 const FIELD_SUBADMAREA
= 3;
1108 const FIELD_LOCALITY
= 4;
1109 const FIELD_ZIPCODE
= 5;
1111 /** Data of the filter
1116 public function __construct($val, $fieldtype, $type = null
, $flags = self
::FLAG_ANY
)
1118 parent
::__construct($type, $flags);
1120 if (!is_array($val)) {
1124 $this->fieldtype
= $fieldtype;
1127 public function buildCondition(PlFilter
$uf)
1129 $sub = $uf->addAddressFilter();
1130 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1132 switch ($this->fieldtype
) {
1133 case self
::FIELD_COUNTRY
:
1134 $field = 'countryId';
1136 case self
::FIELD_ADMAREA
:
1137 $field = 'administrativeAreaId';
1139 case self
::FIELD_SUBADMAREA
:
1140 $field = 'subAdministrativeAreaId';
1142 case self
::FIELD_LOCALITY
:
1143 $field = 'localityId';
1145 case self
::FIELD_ZIPCODE
:
1146 $field = 'postalCode';
1149 Platal
::page()->killError('Invalid address field type: ' . $this->fieldtype
);
1151 $conds[] = XDB
::format($sub . '.' . $field . ' IN {?}', $this->val
);
1153 return implode(' AND ', $conds);
1157 // {{{ class UFC_Corps
1158 /** Filters users based on the corps they belong to
1159 * @param $corps Corps we are looking for (abbreviation)
1160 * @param $type Whether we search for original or current corps
1162 class UFC_Corps
extends UserFilterCondition
1170 public function __construct($corps, $type = self
::CURRENT
)
1172 $this->corps
= $corps;
1173 $this->type
= $type;
1176 public function buildCondition(PlFilter
$uf)
1178 /** Tables shortcuts:
1179 * pc for profile_corps,
1180 * pceo for profile_corps_enum - orginal
1181 * pcec for profile_corps_enum - current
1183 $sub = $uf->addCorpsFilter($this->type
);
1184 $cond = $sub . '.abbreviation = ' . $corps;
1185 $cond .= ' AND ' . $uf->getVisibilityCondition($sub . '.corps_pub');
1190 // {{{ class UFC_Corps_Rank
1191 /** Filters users based on their rank in the corps
1192 * @param $rank Rank we are looking for (abbreviation)
1194 class UFC_Corps_Rank
extends UserFilterCondition
1197 public function __construct($rank)
1199 $this->rank
= $rank;
1202 public function buildCondition(PlFilter
$uf)
1204 /** Tables shortcuts:
1205 * pc for profile_corps
1206 * pcr for profile_corps_rank
1208 $sub = $uf->addCorpsRankFilter();
1209 $cond = $sub . '.abbreviation = ' . $rank;
1210 // XXX(x2006barrois): find a way to get rid of that hardcoded
1211 // reference to 'pc'.
1212 $cond .= ' AND ' . $uf->getVisibilityCondition('pc.corps_pub');
1217 // {{{ class UFC_Job_Company
1218 /** Filters users based on the company they belong to
1219 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
1220 * @param $value The searched value
1222 class UFC_Job_Company
extends UserFilterCondition
1225 const JOBNAME
= 'name';
1226 const JOBACRONYM
= 'acronym';
1231 public function __construct($type, $value)
1233 $this->assertType($type);
1234 $this->type
= $type;
1235 $this->value
= $value;
1238 private function assertType($type)
1240 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
1241 Platal
::page()->killError("Type de recherche non valide.");
1245 public function buildCondition(PlFilter
$uf)
1247 $sub = $uf->addJobCompanyFilter();
1248 $cond = $sub . '.' . $this->type
. XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1249 $jsub = $uf->addJobFilter();
1250 $cond .= ' AND ' . $uf->getVisibilityCondition($jsub . '.pub');
1255 // {{{ class UFC_Job_Terms
1256 /** Filters users based on the job terms they assigned to one of their
1258 * @param $val The ID of the job term, or an array of such IDs
1260 class UFC_Job_Terms
extends UserFilterCondition
1264 public function __construct($val)
1266 if (!is_array($val)) {
1272 public function buildCondition(PlFilter
$uf)
1274 $sub = $uf->addJobTermsFilter(count($this->val
));
1275 $conditions = array();
1276 foreach ($this->val
as $i => $jtid) {
1277 $conditions[] = $sub[$i] . '.jtid_1 = ' . XDB
::escape($jtid);
1279 $jsub = $uf->addJobFilter();
1280 $conditions[] = $uf->getVisibilityCondition($jsub . '.pub');
1281 return implode(' AND ', $conditions);
1285 // {{{ class UFC_Job_Description
1286 /** Filters users based on their job description
1287 * @param $description The text being searched for
1288 * @param $fields The fields to search for (CV, user-defined)
1290 class UFC_Job_Description
extends UserFilterCondition
1293 private $description;
1296 public function __construct($description, $fields)
1298 $this->fields
= $fields;
1299 $this->description
= $description;
1302 public function buildCondition(PlFilter
$uf)
1306 $jsub = $uf->addJobFilter();
1307 // CV is private => if only CV requested, and not private,
1308 // don't do anything. Otherwise restrict to standard job visibility.
1309 if ($this->fields
== UserFilter
::JOB_CV
) {
1310 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
1311 return self
::CONF_TRUE
;
1314 $conds[] = $uf->getVisibilityCondition($jsub . '.pub');
1317 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
1318 $conds[] = $jsub . '.description ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1320 if ($this->fields
& UserFilter
::JOB_CV
&& $uf->getVisibilityLevel() == ProfileVisibility
::VIS_PRIVATE
) {
1321 $uf->requireProfiles();
1322 $conds[] = 'p.cv ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1324 return implode(' OR ', $conds);
1328 // {{{ class UFC_Networking
1329 /** Filters users based on network identity (IRC, ...)
1330 * @param $type Type of network (-1 for any)
1331 * @param $value Value to search
1333 class UFC_Networking
extends UserFilterCondition
1338 public function __construct($type, $value)
1340 $this->type
= $type;
1341 $this->value
= $value;
1344 public function buildCondition(PlFilter
$uf)
1346 $sub = $uf->addNetworkingFilter();
1348 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1349 $conds[] = $sub . '.address ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1350 if ($this->type
!= -1) {
1351 $conds[] = $sub . '.nwid = ' . XDB
::format('{?}', $this->type
);
1353 return implode(' AND ', $conds);
1357 // {{{ class UFC_Phone
1358 /** Filters users based on their phone number
1359 * @param $num_type Type of number (pro/user/home)
1360 * @param $phone_type Type of phone (fixed/mobile/fax)
1361 * @param $number Phone number
1363 class UFC_Phone
extends UserFilterCondition
1365 const NUM_PRO
= 'pro';
1366 const NUM_USER
= 'user';
1367 const NUM_HOME
= 'address';
1368 const NUM_ANY
= 'any';
1370 const PHONE_FIXED
= 'fixed';
1371 const PHONE_MOBILE
= 'mobile';
1372 const PHONE_FAX
= 'fax';
1373 const PHONE_ANY
= 'any';
1376 private $phone_type;
1379 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
1381 $phone = new Phone(array('display' => $number));
1383 $this->number
= $phone->search();
1384 $this->num_type
= $num_type;
1385 $this->phone_type
= $phone_type;
1388 public function buildCondition(PlFilter
$uf)
1390 $sub = $uf->addPhoneFilter();
1393 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1395 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
1396 if ($this->num_type
!= self
::NUM_ANY
) {
1397 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
1399 if ($this->phone_type
!= self
::PHONE_ANY
) {
1400 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
1402 return implode(' AND ', $conds);
1406 // {{{ class UFC_Medal
1407 /** Filters users based on their medals
1408 * @param $medal ID of the medal
1409 * @param $grade Grade of the medal (null for 'any')
1411 class UFC_Medal
extends UserFilterCondition
1416 public function __construct($medal, $grade = null
)
1418 $this->medal
= $medal;
1419 $this->grade
= $grade;
1422 public function buildCondition(PlFilter
$uf)
1426 // This will require profiles => table 'p' will be available.
1427 $sub = $uf->addMedalFilter();
1429 $conds[] = $uf->getVisibilityCondition('p.medals_pub');
1431 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
1432 if ($this->grade
!= null
) {
1433 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
1435 return implode(' AND ', $conds);
1439 // {{{ class UFC_Photo
1440 /** Filters profiles with photo
1442 class UFC_Photo
extends UserFilterCondition
1444 public function buildCondition(PlFilter
$uf)
1446 $sub = $uf->addPhotoFilter();
1447 return $sub . '.attach IS NOT NULL AND ' . $uf->getVisibilityCondition($sub . '.pub');
1451 // {{{ class UFC_Mentor
1452 class UFC_Mentor
extends UserFilterCondition
1454 public function buildCondition(PlFilter
$uf)
1456 $sub = $uf->addMentorFilter(UserFilter
::MENTOR
);
1457 return $sub . '.expertise IS NOT NULL';
1461 // {{{ class UFC_Mentor_Expertise
1462 /** Filters users by mentoring expertise
1463 * @param $expertise Domain of expertise
1465 class UFC_Mentor_Expertise
extends UserFilterCondition
1469 public function __construct($expertise)
1471 $this->expertise
= $expertise;
1474 public function buildCondition(PlFilter
$uf)
1476 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_EXPERTISE
);
1477 return $sub . '.expertise ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->expertise
);
1481 // {{{ class UFC_Mentor_Country
1482 /** Filters users by mentoring country
1483 * @param $country Two-letters code of country being searched
1485 class UFC_Mentor_Country
extends UserFilterCondition
1489 public function __construct()
1491 $this->country
= pl_flatten(func_get_args());
1494 public function buildCondition(PlFilter
$uf)
1496 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_COUNTRY
);
1497 return $sub . '.country IN ' . XDB
::format('{?}', $this->country
);
1501 // {{{ class UFC_Mentor_Terms
1502 /** Filters users based on the job terms they used in mentoring.
1503 * @param $val The ID of the job term, or an array of such IDs
1505 class UFC_Mentor_Terms
extends UserFilterCondition
1509 public function __construct($val)
1514 public function buildCondition(PlFilter
$uf)
1516 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_TERM
);
1517 return $sub . '.jtid_1 = ' . XDB
::escape($this->val
);
1521 // {{{ class UFC_UserRelated
1522 /** Filters users based on a relation toward a user
1523 * @param $user User to which searched users are related
1525 abstract class UFC_UserRelated
extends UserFilterCondition
1528 public function __construct(PlUser
&$user)
1530 $this->user
=& $user;
1534 // {{{ class UFC_Contact
1535 /** Filters users who belong to selected user's contacts
1537 class UFC_Contact
extends UFC_UserRelated
1539 public function buildCondition(PlFilter
$uf)
1541 $sub = $uf->addContactFilter($this->user
->id());
1542 return 'c' . $sub . '.contact IS NOT NULL';
1546 // {{{ class UFC_WatchRegistration
1547 /** Filters users being watched by selected user
1549 class UFC_WatchRegistration
extends UFC_UserRelated
1551 public function buildCondition(PlFilter
$uf)
1553 if (!$this->user
->watchType('registration')) {
1554 return PlFilterCondition
::COND_FALSE
;
1556 $uids = $this->user
->watchUsers();
1557 if (count($uids) == 0) {
1558 return PlFilterCondition
::COND_FALSE
;
1560 return XDB
::format('$UID IN {?}', $uids);
1565 // {{{ class UFC_WatchPromo
1566 /** Filters users belonging to a promo watched by selected user
1567 * @param $user Selected user (the one watching promo)
1568 * @param $grade Formation the user is watching
1570 class UFC_WatchPromo
extends UFC_UserRelated
1573 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
1575 parent
::__construct($user);
1576 $this->grade
= $grade;
1579 public function buildCondition(PlFilter
$uf)
1581 $promos = $this->user
->watchPromos();
1582 if (count($promos) == 0) {
1583 return PlFilterCondition
::COND_FALSE
;
1585 $sube = $uf->addEducationFilter(true
, $this->grade
);
1586 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
1587 return XDB
::format($field . ' IN {?}', $promos);
1592 // {{{ class UFC_WatchContact
1593 /** Filters users watched by selected user
1595 class UFC_WatchContact
extends UFC_Contact
1597 public function buildCondition(PlFilter
$uf)
1599 if (!$this->user
->watchContacts()) {
1600 return PlFilterCondition
::COND_FALSE
;
1602 return parent
::buildCondition($uf);
1606 // {{{ class UFC_MarketingHash
1607 /** Filters users using the hash generated
1608 * to send marketing emails to him.
1610 class UFC_MarketingHash
extends UserFilterCondition
1614 public function __construct($hash)
1616 $this->hash
= $hash;
1619 public function buildCondition(PlFilter
$uf)
1621 $table = $uf->addMarketingHash();
1622 return XDB
::format('rm.hash = {?}', $this->hash
);
1627 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: