c54ddac2347bd5a9b48a8070976082e136e8bfbb
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 ***************************************************************************/
27 // {{{ interface UserFilterCondition
28 /** This interface describe objects which filter users based
29 * on various parameters.
30 * The parameters of the filter must be given to the constructor.
31 * The buildCondition function is called by UserFilter when
32 * actually building the query. That function must call
33 * $uf->addWheteverFilter so that the UserFilter makes
34 * adequate joins. It must return the 'WHERE' condition to use
37 interface UserFilterCondition
extends PlFilterCondition
42 // {{{ class UFC_Profile
43 /** Filters users who have a profile
45 class UFC_Profile
implements UserFilterCondition
47 public function buildCondition(PlFilter
&$uf)
49 return '$PID IS NOT NULL';
54 // {{{ class UFC_Hruid
55 /** Filters users based on their hruid
56 * @param $val Either an hruid, or a list of those
58 class UFC_Hruid
implements UserFilterCondition
62 public function __construct($val)
64 if (!is_array($val)) {
70 public function buildCondition(PlFilter
&$uf)
72 $ufc->requireAccounts();
74 return 'a.hruid IN ' . XDB
::formatArray($this->hruids
);
80 /** Filters users based on one of their last IPs
81 * @param $ip IP from which connection are checked
83 class UFC_Ip
implements UserFilterCondition
87 public function __construct($ip)
92 public function buildCondition(PlFilter
&$uf)
94 $sub = $uf->addLoggerFilter();
95 $ip = ip_to_uint($this->ip
);
96 return XDB
::format($sub . '.ip = {?} OR ' . $sub . '.forward_ip = {?}', $ip, $ip);
101 // {{{ class UFC_Comment
102 class UFC_Comment
implements UserFilterCondition
106 public function __construct($text)
111 public function buildCondition(PlFilter
&$uf)
113 $uf->requireProfiles();
114 return 'p.freetext ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->text
);
119 // {{{ class UFC_Promo
120 /** Filters users based on promotion
121 * @param $comparison Comparison operator (>, =, ...)
122 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
123 * @param $promo Promotion on which the filter is based
125 class UFC_Promo
implements UserFilterCondition
132 public function __construct($comparison, $grade, $promo)
134 $this->grade
= $grade;
135 $this->comparison
= $comparison;
136 $this->promo
= $promo;
137 if ($this->grade
!= UserFilter
::DISPLAY
) {
138 UserFilter
::assertGrade($this->grade
);
142 public function buildCondition(PlFilter
&$uf)
144 if ($this->grade
== UserFilter
::DISPLAY
) {
145 $sub = $uf->addDisplayFilter();
146 return XDB
::format('pd' . $sub . '.promo ' . $this->comparison
. ' {?}', $this->promo
);
148 $sub = $uf->addEducationFilter(true
, $this->grade
);
149 $field = 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
150 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->promo
);
156 // {{{ class UFC_EducationSchool
157 /** Filters users by formation
158 * @param $val The formation to search (either ID or array of IDs)
160 class UFC_EducationSchool
implements UserFilterCondition
164 public function __construct($val)
166 if (!is_array($val)) {
172 public function buildCondition(PlFilter
&$uf)
174 $sub = $uf->addEducationFilter();
175 return 'pe' . $sub . '.eduid IN ' . XDB
::formatArray($this->val
);
180 // {{{ class UFC_EducationDegree
181 class UFC_EducationDegree
implements UserFilterCondition
185 public function __construct($diploma)
187 if (! is_array($diploma)) {
188 $diploma = array($diploma);
190 $this->diploma
= $diploma;
193 public function buildCondition(PlFilter
&$uf)
195 $sub = $uf->addEducationFilter();
196 return 'pee' . $sub . '.degreeid IN ' . XDB
::formatArray($this->val
);
201 // {{{ class UFC_EducationField
202 class UFC_EducationField
implements UserFilterCondition
206 public function __construct($val)
208 if (!is_array($val)) {
214 public function buildCondition(PlFilter
&$uf)
216 $sub = $uf->addEducationFilter();
217 return 'pee' . $sub . '.fieldid IN ' . XDB
::formatArray($this->val
);
222 // {{{ class UFC_Name
223 /** Filters users based on name
224 * @param $type Type of name field on which filtering is done (firstname, lastname...)
225 * @param $text Text on which to filter
226 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
228 class UFC_Name
implements UserFilterCondition
230 const PREFIX
= XDB
::WILDCARD_PREFIX
; // 0x001
231 const SUFFIX
= XDB
::WILDCARD_SUFFIX
; // 0x002
232 const CONTAINS
= XDB
::WILDCARD_CONTAINS
; // 0x003
233 const PARTICLE
= 0x007; // self::CONTAINS | 0x004
234 const VARIANTS
= 0x008;
240 public function __construct($type, $text, $mode)
247 private function buildNameQuery($type, $variant, $where, UserFilter
&$uf)
249 $sub = $uf->addNameFilter($type, $variant);
250 return str_replace('$ME', 'pn' . $sub, $where);
253 public function buildCondition(PlFilter
&$uf)
256 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
257 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
259 $right = XDB
::formatWildcards($this->mode
& self
::CONTAINS
, $this->text
);
261 $cond = $left . $right;
262 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
263 if (($this->mode
& self
::VARIANTS
) != 0 && isset(Profile
::$name_variants[$this->type
])) {
264 foreach (Profile
::$name_variants[$this->type
] as $var) {
265 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
268 return implode(' OR ', $conds);
273 // {{{ class UFC_NameTokens
274 /** Selects users based on tokens in their name (for quicksearch)
275 * @param $tokens An array of tokens to search
276 * @param $flags Flags the tokens must have (e.g 'public' for public search)
277 * @param $soundex (bool) Whether those tokens are fulltext or soundex
279 class UFC_NameTokens
implements UserFilterCondition
282 const FLAG_PUBLIC
= 'public';
289 public function __construct($tokens, $flags = array(), $soundex = false
, $exact = false
)
291 $this->tokens
= $tokens;
292 if (is_array($flags)) {
293 $this->flags
= $flags;
295 $this->flags
= array($flags);
297 $this->soundex
= $soundex;
298 $this->exact
= $exact;
301 public function buildCondition(PlFilter
&$uf)
303 $sub = $uf->addNameTokensFilter(!($this->exact ||
$this->soundex
));
305 if ($this->soundex
) {
306 $conds[] = $sub . '.soundex IN ' . XDB
::formatArray($this->tokens
);
307 } else if ($this->exact
) {
308 $conds[] = $sub . '.token IN ' . XDB
::formatArray($this->tokens
);
311 foreach ($this->tokens
as $token) {
312 $tokconds[] = $sub . '.token ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $token);
314 $conds[] = implode(' OR ', $tokconds);
317 if ($this->flags
!= null
) {
318 $conds[] = $sub . '.flags IN ' . XDB
::formatArray($this->flags
);
321 return implode(' AND ', $conds);
326 // {{{ class UFC_Nationality
327 class UFC_Nationality
implements UserFilterCondition
331 public function __construct($val)
333 if (!is_array($val)) {
339 public function buildCondition(PlFilter
&$uf)
341 $uf->requireProfiles();
342 $nat = XDB
::formatArray($this->val
);
344 'p.nationality1 IN ' . $nat,
345 'p.nationality2 IN ' . $nat,
346 'p.nationality3 IN ' . $nat,
348 return implode(' OR ', $conds);
353 // {{{ class UFC_Dead
354 /** Filters users based on death date
355 * @param $comparison Comparison operator
356 * @param $date Date to which death date should be compared
358 class UFC_Dead
implements UserFilterCondition
363 public function __construct($comparison = null
, $date = null
)
365 $this->comparison
= $comparison;
369 public function buildCondition(PlFilter
&$uf)
371 $uf->requireProfiles();
372 $str = 'p.deathdate IS NOT NULL';
373 if (!is_null($this->comparison
)) {
374 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
381 // {{{ class UFC_Registered
382 /** Filters users based on registration state
383 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
384 * @param $comparison Comparison operator
385 * @param $date Date to which users registration date should be compared
387 class UFC_Registered
implements UserFilterCondition
393 public function __construct($active = false
, $comparison = null
, $date = null
)
395 $this->active
= $active;
396 $this->comparison
= $comparison;
400 public function buildCondition(PlFilter
&$uf)
402 $uf->requireAccounts();
404 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
406 $date = 'a.uid IS NOT NULL AND a.state != \'pending\'';
408 if (!is_null($this->comparison
)) {
409 $date .= ' AND a.registration_date ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
416 // {{{ class UFC_ProfileUpdated
417 /** Filters users based on profile update date
418 * @param $comparison Comparison operator
419 * @param $date Date to which profile update date must be compared
421 class UFC_ProfileUpdated
implements UserFilterCondition
426 public function __construct($comparison = null
, $date = null
)
428 $this->comparison
= $comparison;
432 public function buildCondition(PlFilter
&$uf)
434 $uf->requireProfiles();
435 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
440 // {{{ class UFC_Birthday
441 /** Filters users based on next birthday date
442 * @param $comparison Comparison operator
443 * @param $date Date to which users next birthday date should be compared
445 class UFC_Birthday
implements UserFilterCondition
450 public function __construct($comparison = null
, $date = null
)
452 $this->comparison
= $comparison;
456 public function buildCondition(PlFilter
&$uf)
458 $uf->requireProfiles();
459 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
465 /** Filters users based on sex
466 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
468 class UFC_Sex
implements UserFilterCondition
471 public function __construct($sex)
476 public function buildCondition(PlFilter
&$uf)
478 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
479 return self
::COND_FALSE
;
481 $uf->requireProfiles();
482 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
488 // {{{ class UFC_Group
489 /** Filters users based on group membership
490 * @param $group Group whose members we are selecting
491 * @param $anim Whether to restrict selection to animators of that group
493 class UFC_Group
implements UserFilterCondition
497 public function __construct($group, $anim = false
)
499 $this->group
= $group;
503 public function buildCondition(PlFilter
&$uf)
505 $sub = $uf->addGroupFilter($this->group
);
506 $where = 'gpm' . $sub . '.perms IS NOT NULL';
508 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
515 // {{{ class UFC_Binet
516 /** Selects users based on their belonging to a given (list of) binet
517 * @param $binet either a binet_id or an array of binet_ids
519 class UFC_Binet
implements UserFilterCondition
523 public function __construct($val)
525 if (!is_array($val)) {
531 public function buildCondition(PlFilter
&$uf)
533 $sub = $uf->addBinetsFilter();
534 return $sub . '.binet_id IN ' . XDB
::formatArray($this->val
);
539 // {{{ class UFC_Section
540 /** Selects users based on section
541 * @param $section ID of the section
543 class UFC_Section
implements UserFilterCondition
547 public function __construct($section)
549 $this->section
= $section;
552 public function buildCondition(PlFilter
&$uf)
554 $uf->requireProfiles();
555 return 'p.section = ' . XDB
::format('{?}', $this->section
);
560 // {{{ class UFC_Email
561 /** Filters users based on email address
562 * @param $email Email whose owner we are looking for
564 class UFC_Email
implements UserFilterCondition
567 public function __construct($email)
569 $this->email
= $email;
572 public function buildCondition(PlFilter
&$uf)
574 if (User
::isForeignEmailAddress($this->email
)) {
575 $sub = $uf->addEmailRedirectFilter($this->email
);
576 return XDB
::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email
);
577 } else if (User
::isVirtualEmailAddress($this->email
)) {
578 $sub = $uf->addVirtualEmailFilter($this->email
);
579 return 'vr' . $sub . '.redirect IS NOT NULL';
581 @list
($user, $domain) = explode('@', $this->email
);
582 $sub = $uf->addAliasFilter($user);
583 return 'al' . $sub . '.alias IS NOT NULL';
589 // {{{ class UFC_EmailList
590 /** Filters users based on an email list
591 * @param $emails List of emails whose owner must be selected
593 class UFC_EmailList
implements UserFilterCondition
596 public function __construct($emails)
598 $this->emails
= $emails;
601 public function buildCondition(PlFilter
&$uf)
608 if (count($this->emails
) == 0) {
609 return PlFilterCondition
::COND_TRUE
;
612 foreach ($this->emails
as $entry) {
613 if (User
::isForeignEmailAddress($entry)) {
614 if (is_null($email)) {
615 $email = $uf->addEmailRedirectFilter();
617 $cond[] = XDB
::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry);
618 } else if (User
::isVirtualEmailAddress($entry)) {
619 if (is_null($virtual)) {
620 $virtual = $uf->addVirtualEmailFilter();
622 $cond[] = XDB
::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry);
624 if (is_null($alias)) {
625 $alias = $uf->addAliasFilter();
627 @list
($user, $domain) = explode('@', $entry);
628 $cond[] = XDB
::format('al' . $alias . '.alias = {?}', $user);
631 return '(' . implode(') OR (', $cond) . ')';
636 // {{{ class UFC_Address
637 abstract class UFC_Address
implements UserFilterCondition
639 /** Valid address type ('hq' is reserved for company addresses)
645 /** Text for these types
647 protected static $typetexts = array(
648 self
::TYPE_HOME
=> 'home',
649 self
::TYPE_PRO
=> 'pro',
654 /** Flags for addresses
656 const FLAG_CURRENT
= 0x0001;
657 const FLAG_TEMP
= 0x0002;
658 const FLAG_SECOND
= 0x0004;
659 const FLAG_MAIL
= 0x0008;
660 const FLAG_CEDEX
= 0x0010;
662 // Binary OR of those flags
663 const FLAG_ANY
= 0x001F;
665 /** Text of these flags
667 protected static $flagtexts = array(
668 self
::FLAG_CURRENT
=> 'current',
669 self
::FLAG_TEMP
=> 'temporary',
670 self
::FLAG_SECOND
=> 'secondary',
671 self
::FLAG_MAIL
=> 'mail',
672 self
::FLAG_CEDEX
=> 'cedex',
677 public function __construct($type = null
, $flags = null
)
680 $this->flags
= $flags;
683 protected function initConds($sub)
687 foreach (self
::$typetexts as $flag => $type) {
688 if ($flag & $this->type
) {
693 $conds[] = $sub . '.type IN ' . XDB
::formatArray($types);
696 if ($this->flags
!= self
::FLAG_ANY
) {
697 foreach(self
::$flagtexts as $flag => $text) {
698 if ($flag & $this->flags
) {
699 $conds[] = 'FIND_IN_SET(' . XDB
::format('{?}', $text) . ', ' . $sub . '.flags)';
709 // {{{ class UFC_AddressText
710 /** Select users based on their address, using full text search
711 * @param $text Text for filter in fulltext search
712 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
713 * @param $type Filter on address type
714 * @param $flags Filter on address flags
715 * @param $country Filter on address country
716 * @param $locality Filter on address locality
718 class UFC_AddressText
extends UFC_Address
722 private $textSearchMode;
724 public function __construct($text = null
, $textSearchMode = XDB
::WILDCARD_CONTAINS
,
725 $type = null
, $flags = self
::FLAG_ANY
, $country = null
, $locality = null
)
727 parent
::__construct($type, $flags);
729 $this->textSearchMode
= $textSearchMode;
730 $this->country
= $country;
731 $this->locality
= $locality;
734 private function mkMatch($txt)
736 return XDB
::formatWildcards($this->textSearchMode
, $txt);
739 public function buildCondition(PlFilter
&$uf)
741 $sub = $uf->addAddressFilter();
742 $conds = $this->initConds($sub);
743 if ($this->text
!= null
) {
744 $conds[] = $sub . '.text' . $this->mkMatch($this->text
);
747 if ($this->country
!= null
) {
748 $subc = $uf->addAddressCountryFilter();
750 $subconds[] = $subc . '.country' . $this->mkMatch($this->country
);
751 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country
);
752 $conds[] = implode(' OR ', $subconds);
755 if ($this->locality
!= null
) {
756 $subl = $uf->addAddressLocalityFilter();
757 $conds[] = $subl . '.name' . $this->mkMatch($this->locality
);
760 return implode(' AND ', $conds);
765 // {{{ class UFC_AddressField
766 /** Filters users based on their address,
767 * @param $val Either a code for one of the fields, or an array of such codes
768 * @param $fieldtype The type of field to look for
769 * @param $type Filter on address type
770 * @param $flags Filter on address flags
772 class UFC_AddressField
extends UFC_Address
774 const FIELD_COUNTRY
= 1;
775 const FIELD_ADMAREA
= 2;
776 const FIELD_SUBADMAREA
= 3;
777 const FIELD_LOCALITY
= 4;
778 const FIELD_ZIPCODE
= 5;
780 /** Data of the filter
785 public function __construct($val, $fieldtype, $type = null
, $flags = self
::FLAG_ANY
)
787 parent
::__construct($type, $flags);
789 if (!is_array($val)) {
793 $this->fieldtype
= $fieldtype;
796 public function buildCondition(PlFilter
&$uf)
798 $sub = $uf->addAddressFilter();
799 $conds = $this->initConds($sub);
801 switch ($this->fieldtype
) {
802 case self
::FIELD_COUNTRY
:
803 $field = 'countryId';
805 case self
::FIELD_ADMAREA
:
806 $field = 'administrativeAreaId';
808 case self
::FIELD_SUBADMAREA
:
809 $field = 'subAdministrativeAreaId';
811 case self
::FIELD_LOCALITY
:
812 $field = 'localityId';
814 case self
::FIELD_ZIPCODE
:
815 $field = 'postalCode';
818 Platal
::page()->killError('Invalid address field type: ' . $this->fieldtype
);
820 $conds[] = $sub . '.' . $field . ' IN ' . XDB
::formatArray($this->val
);
822 return implode(' AND ', $conds);
827 // {{{ class UFC_Corps
828 /** Filters users based on the corps they belong to
829 * @param $corps Corps we are looking for (abbreviation)
830 * @param $type Whether we search for original or current corps
832 class UFC_Corps
implements UserFilterCondition
840 public function __construct($corps, $type = self
::CURRENT
)
842 $this->corps
= $corps;
846 public function buildCondition(PlFilter
&$uf)
848 /** Tables shortcuts:
849 * pc for profile_corps,
850 * pceo for profile_corps_enum - orginal
851 * pcec for profile_corps_enum - current
853 $sub = $uf->addCorpsFilter($this->type
);
854 $cond = $sub . '.abbreviation = ' . $corps;
860 // {{{ class UFC_Corps_Rank
861 /** Filters users based on their rank in the corps
862 * @param $rank Rank we are looking for (abbreviation)
864 class UFC_Corps_Rank
implements UserFilterCondition
867 public function __construct($rank)
872 public function buildCondition(PlFilter
&$uf)
874 /** Tables shortcuts:
875 * pcr for profile_corps_rank
877 $sub = $uf->addCorpsRankFilter();
878 $cond = $sub . '.abbreviation = ' . $rank;
884 // {{{ class UFC_Job_Company
885 /** Filters users based on the company they belong to
886 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
887 * @param $value The searched value
889 class UFC_Job_Company
implements UserFilterCondition
892 const JOBNAME
= 'name';
893 const JOBACRONYM
= 'acronym';
898 public function __construct($type, $value)
900 $this->assertType($type);
902 $this->value
= $value;
905 private function assertType($type)
907 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
908 Platal
::page()->killError("Type de recherche non valide.");
912 public function buildCondition(PlFilter
&$uf)
914 $sub = $uf->addJobCompanyFilter();
915 $cond = $sub . '.' . $this->type
. ' = ' . XDB
::format('{?}', $this->value
);
921 // {{{ class UFC_Job_Sectorization
922 /** Filters users based on the ((sub)sub)sector they work in
923 * @param $val The ID of the sector, or an array of such IDs
924 * @param $type The kind of search (subsubsector/subsector/sector)
926 class UFC_Job_Sectorization
implements UserFilterCondition
931 public function __construct($val, $type = UserFilter
::JOB_SECTOR
)
933 self
::assertType($type);
934 if (!is_array($val)) {
941 private static function assertType($type)
943 if ($type != UserFilter
::JOB_SECTOR
&& $type != UserFilter
::JOB_SUBSECTOR
&& $type != UserFilter
::JOB_SUBSUBSECTOR
) {
944 Platal
::page()->killError("Type de secteur non valide.");
948 public function buildCondition(PlFilter
&$uf)
950 $sub = $uf->addJobSectorizationFilter($this->type
);
951 return $sub . '.id = ' . XDB
::format('{?}', $this->val
);
956 // {{{ class UFC_Job_Description
957 /** Filters users based on their job description
958 * @param $description The text being searched for
959 * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
961 class UFC_Job_Description
implements UserFilterCondition
964 private $description;
967 public function __construct($description, $fields)
969 $this->fields
= $fields;
970 $this->description
= $description;
973 public function buildCondition(PlFilter
&$uf)
976 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
977 $sub = $uf->addJobFilter();
978 $conds[] = $sub . '.description ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
980 if ($this->fields
& UserFilter
::JOB_CV
) {
981 $uf->requireProfiles();
982 $conds[] = 'p.cv ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
984 if ($this->fields
& UserFilter
::JOB_SECTOR
) {
985 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SECTOR
);
986 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
988 if ($this->fields
& UserFilter
::JOB_SUBSECTOR
) {
989 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSECTOR
);
990 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
992 if ($this->fields
& UserFilter
::JOB_SUBSUBSECTOR
) {
993 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSUBSECTOR
);
994 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
995 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_ALTERNATES
);
996 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
998 return implode(' OR ', $conds);
1003 // {{{ class UFC_Networking
1004 /** Filters users based on network identity (IRC, ...)
1005 * @param $type Type of network (-1 for any)
1006 * @param $value Value to search
1008 class UFC_Networking
implements UserFilterCondition
1013 public function __construct($type, $value)
1015 $this->type
= $type;
1016 $this->value
= $value;
1019 public function buildCondition(PlFilter
&$uf)
1021 $sub = $uf->addNetworkingFilter();
1023 $conds[] = $sub . '.address ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1024 if ($this->type
!= -1) {
1025 $conds[] = $sub . '.network_type = ' . XDB
::format('{?}', $this->type
);
1027 return implode(' AND ', $conds);
1032 // {{{ class UFC_Phone
1033 /** Filters users based on their phone number
1034 * @param $num_type Type of number (pro/user/home)
1035 * @param $phone_type Type of phone (fixed/mobile/fax)
1036 * @param $number Phone number
1038 class UFC_Phone
implements UserFilterCondition
1040 const NUM_PRO
= 'pro';
1041 const NUM_USER
= 'user';
1042 const NUM_HOME
= 'address';
1043 const NUM_ANY
= 'any';
1045 const PHONE_FIXED
= 'fixed';
1046 const PHONE_MOBILE
= 'mobile';
1047 const PHONE_FAX
= 'fax';
1048 const PHONE_ANY
= 'any';
1051 private $phone_type;
1054 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
1056 require_once('profil.func.inc.php');
1057 $this->number
= $number;
1058 $this->num_type
= $num_type;
1059 $this->phone_type
= format_phone_number($phone_type);
1062 public function buildCondition(PlFilter
&$uf)
1064 $sub = $uf->addPhoneFilter();
1066 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
1067 if ($this->num_type
!= self
::NUM_ANY
) {
1068 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
1070 if ($this->phone_type
!= self
::PHONE_ANY
) {
1071 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
1073 return implode(' AND ', $conds);
1078 // {{{ class UFC_Medal
1079 /** Filters users based on their medals
1080 * @param $medal ID of the medal
1081 * @param $grade Grade of the medal (null for 'any')
1083 class UFC_Medal
implements UserFilterCondition
1088 public function __construct($medal, $grade = null
)
1090 $this->medal
= $medal;
1091 $this->grade
= $grade;
1094 public function buildCondition(PlFilter
&$uf)
1097 $sub = $uf->addMedalFilter();
1098 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
1099 if ($this->grade
!= null
) {
1100 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
1102 return implode(' AND ', $conds);
1107 // {{{ class UFC_Mentor_Expertise
1108 /** Filters users by mentoring expertise
1109 * @param $expertise Domain of expertise
1111 class UFC_Mentor_Expertise
implements UserFilterCondition
1115 public function __construct($expertise)
1117 $this->expertise
= $expertise;
1120 public function buildCondition(PlFilter
&$uf)
1122 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_EXPERTISE
);
1123 return $sub . '.expertise ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->expertise
);
1128 // {{{ class UFC_Mentor_Country
1129 /** Filters users by mentoring country
1130 * @param $country Two-letters code of country being searched
1132 class UFC_Mentor_Country
implements UserFilterCondition
1136 public function __construct($country)
1138 $this->country
= $country;
1141 public function buildCondition(PlFilter
&$uf)
1143 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_COUNTRY
);
1144 return $sub . '.country = ' . XDB
::format('{?}', $this->country
);
1149 // {{{ class UFC_Mentor_Sectorization
1150 /** Filters users based on mentoring (sub|)sector
1151 * @param $sector ID of (sub)sector
1152 * @param $type Whether we are looking for a sector or a subsector
1154 class UFC_Mentor_Sectorization
implements UserFilterCondition
1157 const SUBSECTOR
= 2;
1161 public function __construct($sector, $type = self
::SECTOR
)
1163 $this->sector
= $sector;
1164 $this->type
= $type;
1167 public function buildCondition(PlFilter
&$uf)
1169 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_SECTOR
);
1170 if ($this->type
== self
::SECTOR
) {
1171 $field = 'sectorid';
1173 $field = 'subsectorid';
1175 return $sub . '.' . $field . ' = ' . XDB
::format('{?}', $this->sector
);
1180 // {{{ class UFC_UserRelated
1181 /** Filters users based on a relation toward a user
1182 * @param $user User to which searched users are related
1184 abstract class UFC_UserRelated
implements UserFilterCondition
1187 public function __construct(PlUser
&$user)
1189 $this->user
=& $user;
1194 // {{{ class UFC_Contact
1195 /** Filters users who belong to selected user's contacts
1197 class UFC_Contact
extends UFC_UserRelated
1199 public function buildCondition(PlFilter
&$uf)
1201 $sub = $uf->addContactFilter($this->user
->id());
1202 return 'c' . $sub . '.contact IS NOT NULL';
1207 // {{{ class UFC_WatchRegistration
1208 /** Filters users being watched by selected user
1210 class UFC_WatchRegistration
extends UFC_UserRelated
1212 public function buildCondition(PlFilter
&$uf)
1214 if (!$this->user
->watch('registration')) {
1215 return PlFilterCondition
::COND_FALSE
;
1217 $uids = $this->user
->watchUsers();
1218 if (count($uids) == 0) {
1219 return PlFilterCondition
::COND_FALSE
;
1221 return '$UID IN ' . XDB
::formatArray($uids);
1227 // {{{ class UFC_WatchPromo
1228 /** Filters users belonging to a promo watched by selected user
1229 * @param $user Selected user (the one watching promo)
1230 * @param $grade Formation the user is watching
1232 class UFC_WatchPromo
extends UFC_UserRelated
1235 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
1237 parent
::__construct($user);
1238 $this->grade
= $grade;
1241 public function buildCondition(PlFilter
&$uf)
1243 $promos = $this->user
->watchPromos();
1244 if (count($promos) == 0) {
1245 return PlFilterCondition
::COND_FALSE
;
1247 $sube = $uf->addEducationFilter(true
, $this->grade
);
1248 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
1249 return $field . ' IN ' . XDB
::formatArray($promos);
1255 // {{{ class UFC_WatchContact
1256 /** Filters users watched by selected user
1258 class UFC_WatchContact
extends UFC_Contact
1260 public function buildCondition(PlFilter
&$uf)
1262 if (!$this->user
->watchContacts()) {
1263 return PlFilterCondition
::COND_FALSE
;
1265 return parent
::buildCondition($uf);
1275 // {{{ class UserFilterOrder
1276 /** Base class for ordering results of a query.
1277 * Parameters for the ordering must be given to the constructor ($desc for a
1278 * descending order).
1279 * The getSortTokens function is used to get actual ordering part of the query.
1281 abstract class UserFilterOrder
extends PlFilterOrder
1283 /** This function must return the tokens to use for ordering
1284 * @param &$uf The UserFilter whose results must be ordered
1285 * @return The name of the field to use for ordering results
1287 // abstract protected function getSortTokens(UserFilter &$uf);
1291 // {{{ class UFO_Promo
1292 /** Orders users by promotion
1293 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
1294 * @param $desc Whether sort is descending
1296 class UFO_Promo
extends UserFilterOrder
1300 public function __construct($grade = null
, $desc = false
)
1302 parent
::__construct($desc);
1303 $this->grade
= $grade;
1306 protected function getSortTokens(PlFilter
&$uf)
1308 if (UserFilter
::isGrade($this->grade
)) {
1309 $sub = $uf->addEducationFilter($this->grade
);
1310 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
1312 $sub = $uf->addDisplayFilter();
1313 return 'pd' . $sub . '.promo';
1319 // {{{ class UFO_Name
1320 /** Sorts users by name
1321 * @param $type Type of name on which to sort (firstname...)
1322 * @param $variant Variant of that name to use (marital, ordinary...)
1323 * @param $particle Set to true if particles should be included in the sorting order
1324 * @param $desc If sort order should be descending
1326 class UFO_Name
extends UserFilterOrder
1332 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
1334 parent
::__construct($desc);
1335 $this->type
= $type;
1336 $this->variant
= $variant;
1337 $this->particle
= $particle;
1340 protected function getSortTokens(PlFilter
&$uf)
1342 if (Profile
::isDisplayName($this->type
)) {
1343 $sub = $uf->addDisplayFilter();
1344 return 'pd' . $sub . '.' . $this->type
;
1346 $sub = $uf->addNameFilter($this->type
, $this->variant
);
1347 if ($this->particle
) {
1348 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1350 return 'pn' . $sub . '.name';
1357 // {{{ class UFO_Score
1358 class UFO_Score
extends UserFilterOrder
1360 protected function getSortTokens(PlFilter
&$uf)
1362 $sub = $uf->addNameTokensFilter();
1363 return 'SUM(' . $sub . '.score)';
1368 // {{{ class UFO_Registration
1369 /** Sorts users based on registration date
1371 class UFO_Registration
extends UserFilterOrder
1373 protected function getSortTokens(PlFilter
&$uf)
1375 return 'a.registration_date';
1380 // {{{ class UFO_Birthday
1381 /** Sorts users based on next birthday date
1383 class UFO_Birthday
extends UserFilterOrder
1385 protected function getSortTokens(PlFilter
&$uf)
1387 return 'p.next_birthday';
1392 // {{{ class UFO_ProfileUpdate
1393 /** Sorts users based on last profile update
1395 class UFO_ProfileUpdate
extends UserFilterOrder
1397 protected function getSortTokens(PlFilter
&$uf)
1399 return 'p.last_change';
1404 // {{{ class UFO_Death
1405 /** Sorts users based on death date
1407 class UFO_Death
extends UserFilterOrder
1409 protected function getSortTokens(PlFilter
&$uf)
1411 return 'p.deathdate';
1417 /***********************************
1418 *********************************
1420 *********************************
1421 ***********************************/
1423 // {{{ class UserFilter
1424 /** This class provides a convenient and centralized way of filtering users.
1427 * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t));
1429 * Resulting UserFilter can be used to:
1430 * - get a list of User objects matching the filter
1431 * - get a list of UIDs matching the filter
1432 * - get the number of users matching the filter
1433 * - check whether a given User matches the filter
1434 * - filter a list of User objects depending on whether they match the filter
1436 * Usage for UFC and UFO objects:
1437 * A UserFilter will call all private functions named XXXJoins.
1438 * These functions must return an array containing the list of join
1439 * required by the various UFC and UFO associated to the UserFilter.
1440 * Entries in those returned array are of the following form:
1441 * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter')
1442 * which will be translated into :
1443 * join_type JOIN joined_table AS join_tablealias ON (join_criter)
1444 * in the final query.
1446 * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with
1447 * profile.pid, and $UID with accounts.uid.
1449 * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined;
1450 * its parameter will be used to set various private vars of the UserFilter describing
1451 * the required joins ; such a function shall return the "join_tablealias" to use
1452 * when referring to the joined table.
1454 * For example, if data from profile_job must be available to filter results,
1455 * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and
1456 * return 'pj', the short name to use when referring to profile_job; when building
1457 * the query, calling the jobJoins function will return an array containing a single
1459 * 'pj' => array('left', 'profile_job', '$ME.pid = $UID');
1461 * The 'register_optional' function can be used to generate unique table aliases when
1462 * the same table has to be joined several times with different aliases.
1464 class UserFilter
extends PlFilter
1466 protected $joinMethods = array();
1468 protected $joinMetas = array(
1474 private $sort = array();
1475 private $query = null
;
1476 private $orderby = null
;
1478 private $lastcount = null
;
1480 public function __construct($cond = null
, $sort = null
)
1482 if (empty($this->joinMethods
)) {
1483 $class = new ReflectionClass('UserFilter');
1484 foreach ($class->getMethods() as $method) {
1485 $name = $method->getName();
1486 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
1487 $this->joinMethods
[] = $name;
1491 if (!is_null($cond)) {
1492 if ($cond instanceof PlFilterCondition
) {
1493 $this->setCondition($cond);
1496 if (!is_null($sort)) {
1497 if ($sort instanceof UserFilterOrder
) {
1498 $this->addSort($sort);
1499 } else if (is_array($sort)) {
1500 foreach ($sort as $s) {
1507 private function buildQuery()
1509 if (is_null($this->orderby
)) {
1511 foreach ($this->sort
as $sort) {
1512 $orders = array_merge($orders, $sort->buildSort($this));
1514 if (count($orders) == 0) {
1515 $this->orderby
= '';
1517 $this->orderby
= 'ORDER BY ' . implode(', ', $orders);
1520 if (is_null($this->query
)) {
1521 $where = $this->root
->buildCondition($this);
1522 if ($this->with_forced_sn
) {
1523 $this->requireProfiles();
1524 $from = 'search_name AS sn';
1525 } else if ($this->with_accounts
) {
1526 $from = 'accounts AS a';
1528 $this->requireProfiles();
1529 $from = 'profiles AS p';
1531 $joins = $this->buildJoins();
1532 $this->query
= 'FROM ' . $from . '
1534 WHERE (' . $where . ')';
1538 private function getUIDList($uids = null
, PlLimit
&$limit)
1540 $this->requireAccounts();
1541 $this->buildQuery();
1542 $lim = $limit->getSql();
1544 if (!is_null($uids)) {
1545 $cond = ' AND a.uid IN ' . XDB
::formatArray($uids);
1547 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1548 ' . $this->query
. $cond . '
1550 ' . $this->orderby
. '
1552 $this->lastcount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
1556 private function getPIDList($pids = null
, PlLimit
&$limit)
1558 $this->requireProfiles();
1559 $this->buildQuery();
1560 $lim = $limit->getSql();
1562 if (!is_null($pids)) {
1563 $cond = ' AND p.pid IN ' . XDB
::formatArray($pids);
1565 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid
1566 ' . $this->query
. $cond . '
1568 ' . $this->orderby
. '
1570 $this->lastcount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
1574 private static function defaultLimit($limit) {
1575 if ($limit == null
) {
1576 return new PlLimit();
1582 /** Check that the user match the given rule.
1584 public function checkUser(PlUser
&$user)
1586 $this->requireAccounts();
1587 $this->buildQuery();
1588 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
1589 ' . $this->query
. XDB
::format(' AND a.uid = {?}', $user->id()));
1593 /** Check that the profile match the given rule.
1595 public function checkProfile(Profile
&$profile)
1597 $this->requireProfiles();
1598 $this->buildQuery();
1599 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
1600 ' . $this->query
. XDB
::format(' AND p.pid = {?}', $profile->id()));
1604 /** Default filter is on users
1606 public function filter(array $users, $limit = null
)
1608 return $this->filterUsers($users, self
::defaultLimit($limit));
1611 /** Filter a list of users to extract the users matching the rule.
1613 public function filterUsers(array $users, $limit = null
)
1615 $limit = self
::defaultLimit($limit);
1616 $this->requireAccounts();
1617 $this->buildQuery();
1620 foreach ($users as $user) {
1621 if ($user instanceof PlUser
) {
1627 $table[$uid] = $user;
1629 $fetched = $this->getUIDList($uids, $limit);
1631 foreach ($fetched as $uid) {
1632 $output[] = $table[$uid];
1637 /** Filter a list of profiles to extract the users matching the rule.
1639 public function filterProfiles(array $profiles, $limit = null
)
1641 $limit = self
::defaultLimit($limit);
1642 $this->requireProfiles();
1643 $this->buildQuery();
1646 foreach ($profiles as $profile) {
1647 if ($profile instanceof Profile
) {
1648 $pid = $profile->id();
1653 $table[$pid] = $profile;
1655 $fetched = $this->getPIDList($pids, $limit);
1657 foreach ($fetched as $pid) {
1658 $output[] = $table[$pid];
1663 public function getUIDs($limit = null
)
1665 $limit = self
::defaultLimit($limit);
1666 return $this->getUIDList(null
, $limit);
1669 public function getPIDs($limit = null
)
1671 $limit = self
::defaultLimit($limit);
1672 return $this->getPIDList(null
, $limit);
1675 public function getUsers($limit = null
)
1677 return User
::getBulkUsersWithUIDs($this->getUIDs($limit));
1680 public function iterUsers($limit = null
)
1682 return User
::iterOverUIDs($this->getUIDs($limit));
1685 public function getProfiles($limit = null
)
1687 return Profile
::getBulkProfilesWithPIDs($this->getPIDs($limit));
1690 public function iterProfiles($limit = null
)
1692 return Profile
::iterOverPIDs($this->getPIDs($limit));
1695 public function get($limit = null
)
1697 return $this->getUsers($limit);
1700 public function getTotalCount()
1702 if (is_null($this->lastcount
)) {
1703 $this->buildQuery();
1704 if ($this->with_accounts
) {
1709 return (int)XDB
::fetchOneCell('SELECT COUNT(DISTINCT ' . $field . ')
1712 return $this->lastcount
;
1716 public function setCondition(PlFilterCondition
&$cond)
1718 $this->root
=& $cond;
1719 $this->query
= null
;
1722 public function addSort(PlFilterOrder
&$sort)
1724 $this->sort
[] = $sort;
1725 $this->orderby
= null
;
1728 static public function getLegacy($promo_min, $promo_max)
1730 if ($promo_min != 0) {
1731 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
1733 $min = new PFC_True();
1735 if ($promo_max != 0) {
1736 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
1738 $max = new PFC_True();
1740 return new UserFilter(new PFC_And($min, $max));
1743 static public function sortByName()
1745 return array(new UFO_Name(Profile
::LASTNAME
), new UFO_Name(Profile
::FIRSTNAME
));
1748 static public function sortByPromo()
1750 return array(new UFO_Promo(), new UFO_Name(Profile
::LASTNAME
), new UFO_Name(Profile
::FIRSTNAME
));
1753 static private function getDBSuffix($string)
1755 return preg_replace('/[^a-z0-9]/i', '', $string);
1759 /** Stores a new (and unique) table alias in the &$table table
1760 * @param &$table Array in which the table alias must be stored
1761 * @param $val Value which will then be used to build the join
1762 * @return Name of the newly created alias
1764 private $option = 0;
1765 private function register_optional(array &$table, $val)
1767 if (is_null($val)) {
1768 $sub = $this->option++
;
1771 $sub = self
::getDBSuffix($val);
1775 $table[$sub] = $index;
1779 /** PROFILE VS ACCOUNT
1781 private $with_profiles = false
;
1782 private $with_accounts = false
;
1783 private $with_forced_sn = false
;
1784 public function requireAccounts()
1786 $this->with_accounts
= true
;
1789 public function requireProfiles()
1791 $this->with_profiles
= true
;
1794 /** Forces the "FROM" to use search_name instead of accounts or profiles */
1795 public function forceSearchName()
1797 $this->with_forced_sn
= true
;
1800 protected function accountJoins()
1803 /** Quick search is much more efficient with sn first and PID second */
1804 if ($this->with_forced_sn
) {
1805 $joins['p'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profiles', '$PID = sn.uid');
1806 if ($this->with_accounts
) {
1807 $joins['ap'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'account_profiles', '$ME.pid = $PID');
1808 $joins['a'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'accounts', '$UID = ap.uid');
1810 } else if ($this->with_profiles
&& $this->with_accounts
) {
1811 $joins['ap'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)');
1812 $joins['p'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profiles', '$PID = ap.pid');
1819 const DISPLAY
= 'display';
1820 private $pd = false
;
1821 public function addDisplayFilter()
1823 $this->requireProfiles();
1828 protected function displayJoins()
1831 return array('pd' => new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_display', '$ME.pid = $PID'));
1840 private $with_logger = false
;
1841 public function addLoggerFilter()
1843 $this->with_logger
= true
;
1844 $this->requireAccounts();
1847 protected function loggerJoins()
1850 if ($this->with_logger
) {
1851 $joins['ls'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'log_sessions', '$ME.uid = $UID');
1859 static public function assertName($name)
1861 if (!Profile
::getNameTypeId($name)) {
1862 Platal
::page()->kill('Invalid name type: ' . $name);
1866 private $pn = array();
1867 public function addNameFilter($type, $variant = null
)
1869 $this->requireProfiles();
1870 if (!is_null($variant)) {
1871 $ft = $type . '_' . $variant;
1876 self
::assertName($ft);
1878 if (!is_null($variant) && $variant == 'other') {
1879 $sub .= $this->option++
;
1881 $this->pn
[$sub] = Profile
::getNameTypeId($ft);
1885 protected function nameJoins()
1888 foreach ($this->pn
as $sub => $type) {
1889 $joins['pn' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
1896 private $with_sn = false
;
1897 // Set $doingQuickSearch to true if you wish to optimize the query
1898 public function addNameTokensFilter($doingQuickSearch = false
)
1900 $this->requireProfiles();
1901 $this->with_forced_sn
= ($this->with_forced_sn ||
$doingQuickSearch);
1902 $this->with_sn
= true
;
1906 protected function nameTokensJoins()
1908 /* We don't return joins, since with_sn forces the SELECT to run on search_name first */
1909 if ($this->with_sn
&& !$this->with_forced_sn
) {
1911 'sn' => new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'search_name', '$ME.uid = $PID')
1921 private $with_nat = false
;
1922 public function addNationalityFilter()
1924 $this->with_nat
= true
;
1928 protected function nationalityJoins()
1931 if ($this->with_nat
) {
1932 $joins['ngc'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'geoloc_countries', '$ME.iso_3166_1_a2 = p.nationality1 OR $ME.iso_3166_1_a2 = p.nationality2 OR $ME.iso_3166_1_a2 = p.nationality3');
1939 const GRADE_ING
= 'Ing.';
1940 const GRADE_PHD
= 'PhD';
1941 const GRADE_MST
= 'M%';
1942 static public function isGrade($grade)
1944 return $grade == self
::GRADE_ING ||
$grade == self
::GRADE_PHD ||
$grade == self
::GRADE_MST
;
1947 static public function assertGrade($grade)
1949 if (!self
::isGrade($grade)) {
1950 Platal
::page()->killError("Diplôme non valide");
1954 static public function promoYear($grade)
1956 // XXX: Definition of promotion for phds and masters might change in near future.
1957 return ($grade == UserFilter
::GRADE_ING
) ?
'entry_year' : 'grad_year';
1960 private $pepe = array();
1961 private $with_pee = false
;
1962 public function addEducationFilter($x = false
, $grade = null
)
1964 $this->requireProfiles();
1966 $index = $this->option
;
1967 $sub = $this->option++
;
1969 self
::assertGrade($grade);
1972 $this->with_pee
= true
;
1975 $this->pepe
[$index] = $sub;
1979 protected function educationJoins()
1982 if ($this->with_pee
) {
1983 $joins['pee'] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'profile_education_enum', 'pee.abbreviation = \'X\'');
1985 foreach ($this->pepe
as $grade => $sub) {
1986 if ($this->isGrade($grade)) {
1987 $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID');
1988 $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
1989 XDB
::format('{?}', $grade));
1991 $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_education', '$ME.pid = $PID');
1992 $joins['pee' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
1993 $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
2002 private $gpm = array();
2003 public function addGroupFilter($group = null
)
2005 $this->requireAccounts();
2006 if (!is_null($group)) {
2007 if (ctype_digit($group)) {
2008 $index = $sub = $group;
2011 $sub = self
::getDBSuffix($group);
2014 $sub = 'group_' . $this->option++
;
2018 $this->gpm
[$sub] = $index;
2022 protected function groupJoins()
2025 foreach ($this->gpm
as $sub => $key) {
2026 if (is_null($key)) {
2027 $joins['gpa' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'groups');
2028 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
2029 } else if (ctype_digit($key)) {
2030 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
2032 $joins['gpa' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'groups', XDB
::format('$ME.diminutif = {?}', $key));
2033 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
2042 private $with_bi = false
;
2043 private $with_bd = false
;
2044 public function addBinetsFilter($with_enum = false
)
2046 $this->requireProfiles();
2047 $this->with_bi
= true
;
2049 $this->with_bd
= true
;
2056 protected function binetsJoins()
2059 if ($this->with_bi
) {
2060 $joins['bi'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_binets', '$ME.pid = $PID');
2062 if ($this->with_bd
) {
2063 $joins['bd'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_binet_enum', '$ME.id = bi.binet_id');
2070 private $e = array();
2071 public function addEmailRedirectFilter($email = null
)
2073 $this->requireAccounts();
2074 return $this->register_optional($this->e
, $email);
2077 private $ve = array();
2078 public function addVirtualEmailFilter($email = null
)
2080 $this->addAliasFilter(self
::ALIAS_FORLIFE
);
2081 return $this->register_optional($this->ve
, $email);
2084 const ALIAS_BEST
= 'bestalias';
2085 const ALIAS_FORLIFE
= 'forlife';
2086 private $al = array();
2087 public function addAliasFilter($alias = null
)
2089 $this->requireAccounts();
2090 return $this->register_optional($this->al
, $alias);
2093 protected function emailJoins()
2097 foreach ($this->e
as $sub=>$key) {
2098 if (is_null($key)) {
2099 $joins['e' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
2101 $joins['e' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'emails', XDB
::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
2104 foreach ($this->al
as $sub=>$key) {
2105 if (is_null($key)) {
2106 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
2107 } else if ($key == self
::ALIAS_BEST
) {
2108 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
2109 } else if ($key == self
::ALIAS_FORLIFE
) {
2110 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
2112 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'aliases', XDB
::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
2115 foreach ($this->ve
as $sub=>$key) {
2116 if (is_null($key)) {
2117 $joins['v' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'virtual', '$ME.type = \'user\'');
2119 $joins['v' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'virtual', XDB
::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
2121 $joins['vr' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'virtual_redirect', XDB
::format('$ME.vid = v' . $sub . '.vid
2122 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
2123 CONCAT(al_forlife.alias, \'@\', {?}),
2125 $globals->mail
->domain
, $globals->mail
->domain2
));
2133 private $with_pa = false
;
2134 public function addAddressFilter()
2136 $this->requireProfiles();
2137 $this->with_pa
= true
;
2141 private $with_pac = false
;
2142 public function addAddressCountryFilter()
2144 $this->requireProfiles();
2145 $this->addAddressFilter();
2146 $this->with_pac
= true
;
2150 private $with_pal = false
;
2151 public function addAddressLocalityFilter()
2153 $this->requireProfiles();
2154 $this->addAddressFilter();
2155 $this->with_pal
= true
;
2159 protected function addressJoins()
2162 if ($this->with_pa
) {
2163 $joins['pa'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_addresses', '$ME.pid = $PID');
2165 if ($this->with_pac
) {
2166 $joins['gc'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID');
2168 if ($this->with_pal
) {
2169 $joins['gl'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'geoloc_localities', '$ME.id = pa.localityID');
2178 private $pc = false
;
2179 private $pce = array();
2180 private $pcr = false
;
2181 public function addCorpsFilter($type)
2183 $this->requireProfiles();
2185 if ($type == UFC_Corps
::CURRENT
) {
2186 $pce['pcec'] = 'current_corpsid';
2188 } else if ($type == UFC_Corps
::ORIGIN
) {
2189 $pce['pceo'] = 'original_corpsid';
2194 public function addCorpsRankFilter()
2196 $this->requireProfiles();
2202 protected function corpsJoins()
2206 $joins['pc'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_corps', '$ME.pid = $PID');
2209 $joins['pcr'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_corps_rank_enum', '$ME.id = pc.rankid');
2211 foreach($this->pce
as $sub => $field) {
2212 $joins[$sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_corps_enum', '$ME.id = pc.' . $field);
2220 const JOB_SECTOR
= 0x0001;
2221 const JOB_SUBSECTOR
= 0x0002;
2222 const JOB_SUBSUBSECTOR
= 0x0004;
2223 const JOB_ALTERNATES
= 0x0008;
2224 const JOB_USERDEFINED
= 0x0010;
2225 const JOB_CV
= 0x0020;
2227 const JOB_SECTORIZATION
= 0x000F;
2228 const JOB_ANY
= 0x003F;
2232 * pje => profile_job_enum
2233 * pjse => profile_job_sector_enum
2234 * pjsse => profile_job_subsector_enum
2235 * pjssse => profile_job_subsubsector_enum
2236 * pja => profile_job_alternates
2238 private $with_pj = false
;
2239 private $with_pje = false
;
2240 private $with_pjse = false
;
2241 private $with_pjsse = false
;
2242 private $with_pjssse = false
;
2243 private $with_pja = false
;
2245 public function addJobFilter()
2247 $this->requireProfiles();
2248 $this->with_pj
= true
;
2252 public function addJobCompanyFilter()
2254 $this->addJobFilter();
2255 $this->with_pje
= true
;
2259 public function addJobSectorizationFilter($type)
2261 $this->addJobFilter();
2262 if ($type == self
::JOB_SECTOR
) {
2263 $this->with_pjse
= true
;
2265 } else if ($type == self
::JOB_SUBSECTOR
) {
2266 $this->with_pjsse
= true
;
2268 } else if ($type == self
::JOB_SUBSUBSECTOR
) {
2269 $this->with_pjssse
= true
;
2271 } else if ($type == self
::JOB_ALTERNATES
) {
2272 $this->with_pja
= true
;
2277 protected function jobJoins()
2280 if ($this->with_pj
) {
2281 $joins['pj'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job', '$ME.pid = $PID');
2283 if ($this->with_pje
) {
2284 $joins['pje'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job_enum', '$ME.id = pj.jobid');
2286 if ($this->with_pjse
) {
2287 $joins['pjse'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job_sector_enum', '$ME.id = pj.sectorid');
2289 if ($this->with_pjsse
) {
2290 $joins['pjsse'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job_subsector_enum', '$ME.id = pj.subsectorid');
2292 if ($this->with_pjssse
) {
2293 $joins['pjssse'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
2295 if ($this->with_pja
) {
2296 $joins['pja'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
2304 private $with_pnw = false
;
2305 public function addNetworkingFilter()
2307 $this->requireAccounts();
2308 $this->with_pnw
= true
;
2312 protected function networkingJoins()
2315 if ($this->with_pnw
) {
2316 $joins['pnw'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_networking', '$ME.pid = $PID');
2324 private $with_ptel = false
;
2326 public function addPhoneFilter()
2328 $this->requireAccounts();
2329 $this->with_ptel
= true
;
2333 protected function phoneJoins()
2336 if ($this->with_ptel
) {
2337 $joins['ptel'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_phones', '$ME.pid = $PID');
2345 private $with_pmed = false
;
2346 public function addMedalFilter()
2348 $this->requireProfiles();
2349 $this->with_pmed
= true
;
2353 protected function medalJoins()
2356 if ($this->with_pmed
) {
2357 $joins['pmed'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_medals', '$ME.pid = $PID');
2365 private $pms = array();
2366 const MENTOR_EXPERTISE
= 1;
2367 const MENTOR_COUNTRY
= 2;
2368 const MENTOR_SECTOR
= 3;
2370 public function addMentorFilter($type)
2372 $this->requireAccounts();
2374 case self
::MENTOR_EXPERTISE
:
2375 $this->pms
['pme'] = 'profile_mentor';
2377 case self
::MENTOR_COUNTRY
:
2378 $this->pms
['pmc'] = 'profile_mentor_country';
2380 case self
::MENTOR_SECTOR
:
2381 $this->pms
['pms'] = 'profile_mentor_sector';
2384 Platal
::page()->killError("Undefined mentor filter.");
2388 protected function mentorJoins()
2391 foreach ($this->pms
as $sub => $tab) {
2392 $joins[$sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, $tab, '$ME.pid = $PID');
2399 private $cts = array();
2400 public function addContactFilter($uid = null
)
2402 $this->requireAccounts();
2403 return $this->register_optional($this->cts
, is_null($uid) ? null
: 'user_' . $uid);
2406 protected function contactJoins()
2409 foreach ($this->cts
as $sub=>$key) {
2410 if (is_null($key)) {
2411 $joins['c' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'contacts', '$ME.contact = $UID');
2413 $joins['c' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'contacts', XDB
::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
2422 private $wn = array();
2423 public function addWatchRegistrationFilter($uid = null
)
2425 $this->requireAccounts();
2426 return $this->register_optional($this->wn
, is_null($uid) ? null
: 'user_' . $uid);
2429 private $wp = array();
2430 public function addWatchPromoFilter($uid = null
)
2432 $this->requireAccounts();
2433 return $this->register_optional($this->wp
, is_null($uid) ? null
: 'user_' . $uid);
2436 private $w = array();
2437 public function addWatchFilter($uid = null
)
2439 $this->requireAccounts();
2440 return $this->register_optional($this->w
, is_null($uid) ? null
: 'user_' . $uid);
2443 protected function watchJoins()
2446 foreach ($this->w
as $sub=>$key) {
2447 if (is_null($key)) {
2448 $joins['w' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch');
2450 $joins['w' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch', XDB
::format('$ME.uid = {?}', substr($key, 5)));
2453 foreach ($this->wn
as $sub=>$key) {
2454 if (is_null($key)) {
2455 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_nonins', '$ME.ni_id = $UID');
2457 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_nonins', XDB
::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
2460 foreach ($this->wn
as $sub=>$key) {
2461 if (is_null($key)) {
2462 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_nonins', '$ME.ni_id = $UID');
2464 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_nonins', XDB
::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
2467 foreach ($this->wp
as $sub=>$key) {
2468 if (is_null($key)) {
2469 $joins['wp' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_promo');
2471 $joins['wp' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_promo', XDB
::format('$ME.uid = {?}', substr($key, 5)));
2479 // {{{ class ProfileFilter
2480 class ProfileFilter
extends UserFilter
2482 public function get($limit = null
)
2484 return $this->getProfiles($limit);
2489 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: