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_HasProfile
43 /** Filters users who have a profile
45 class UFC_HasProfile
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 $uf->requireAccounts();
73 return XDB
::format('a.hruid IN {?}', $this->hruids
);
78 // {{{ class UFC_Hrpid
79 /** Filters users based on the hrpid of their profiles
80 * @param $val Either an hrpid, or a list of those
82 class UFC_Hrpid
implements UserFilterCondition
86 public function __construct($val)
88 if (!is_array($val)) {
94 public function buildCondition(PlFilter
&$uf)
96 $uf->requireProfiles();
97 return XDB
::format('p.hrpid IN {?}', $this->hrpids
);
103 /** Filters users based on one of their last IPs
104 * @param $ip IP from which connection are checked
106 class UFC_Ip
implements UserFilterCondition
110 public function __construct($ip)
115 public function buildCondition(PlFilter
&$uf)
117 $sub = $uf->addLoggerFilter();
118 $ip = ip_to_uint($this->ip
);
119 return XDB
::format($sub . '.ip = {?} OR ' . $sub . '.forward_ip = {?}', $ip, $ip);
124 // {{{ class UFC_Comment
125 class UFC_Comment
implements UserFilterCondition
129 public function __construct($text)
134 public function buildCondition(PlFilter
&$uf)
136 $uf->requireProfiles();
137 return 'p.freetext ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->text
);
142 // {{{ class UFC_Promo
143 /** Filters users based on promotion
144 * @param $comparison Comparison operator (>, =, ...)
145 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
146 * @param $promo Promotion on which the filter is based
148 class UFC_Promo
implements UserFilterCondition
155 public function __construct($comparison, $grade, $promo)
157 $this->grade
= $grade;
158 $this->comparison
= $comparison;
159 $this->promo
= $promo;
160 if ($this->grade
!= UserFilter
::DISPLAY
) {
161 UserFilter
::assertGrade($this->grade
);
165 public function buildCondition(PlFilter
&$uf)
167 if ($this->grade
== UserFilter
::DISPLAY
) {
168 $sub = $uf->addDisplayFilter();
169 return XDB
::format('pd' . $sub . '.promo ' . $this->comparison
. ' {?}', $this->promo
);
171 $sub = $uf->addEducationFilter(true
, $this->grade
);
172 $field = 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
173 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->promo
);
179 // {{{ class UFC_SchoolId
180 /** Filters users based on their shoold identifier
181 * @param type Parameter type (Xorg, AX, School)
182 * @param value School id value
184 class UFC_SchooldId
implements UserFilterCondition
188 const School
= 'school';
193 static public function assertType($type)
195 if ($type != self
::AX
&& $type != self
::Xorg
&& $type != self
::School
) {
196 Platal
::page()->killError("Type de matricule invalide: $type");
200 public function __construct($type, $id)
204 self
::assertType($type);
207 public function buildCondition(PlFilter
&$uf)
209 $uf->requireProfiles();
212 if ($type == self
::School
) {
214 $id = Profile
::getXorgId($id);
216 return XDB
::format('p.' . $type . '_id = {?}', $id);
221 // {{{ class UFC_EducationSchool
222 /** Filters users by formation
223 * @param $val The formation to search (either ID or array of IDs)
225 class UFC_EducationSchool
implements UserFilterCondition
229 public function __construct($val)
231 if (!is_array($val)) {
237 public function buildCondition(PlFilter
&$uf)
239 $sub = $uf->addEducationFilter();
240 return XDB
::format('pe' . $sub . '.eduid IN {?}', $this->val
);
245 // {{{ class UFC_EducationDegree
246 class UFC_EducationDegree
implements UserFilterCondition
250 public function __construct($diploma)
252 if (! is_array($diploma)) {
253 $diploma = array($diploma);
255 $this->diploma
= $diploma;
258 public function buildCondition(PlFilter
&$uf)
260 $sub = $uf->addEducationFilter();
261 return XDB
::format('pee' . $sub . '.degreeid IN {?}', $this->val
);
266 // {{{ class UFC_EducationField
267 class UFC_EducationField
implements UserFilterCondition
271 public function __construct($val)
273 if (!is_array($val)) {
279 public function buildCondition(PlFilter
&$uf)
281 $sub = $uf->addEducationFilter();
282 return XDB
::format('pee' . $sub . '.fieldid IN {?}', $this->val
);
287 // {{{ class UFC_Name
288 /** Filters users based on name
289 * @param $type Type of name field on which filtering is done (firstname, lastname...)
290 * @param $text Text on which to filter
291 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
293 class UFC_Name
implements UserFilterCondition
295 const PREFIX
= XDB
::WILDCARD_PREFIX
; // 0x001
296 const SUFFIX
= XDB
::WILDCARD_SUFFIX
; // 0x002
297 const CONTAINS
= XDB
::WILDCARD_CONTAINS
; // 0x003
298 const PARTICLE
= 0x007; // self::CONTAINS | 0x004
299 const VARIANTS
= 0x008;
305 public function __construct($type, $text, $mode)
312 private function buildNameQuery($type, $variant, $where, UserFilter
&$uf)
314 $sub = $uf->addNameFilter($type, $variant);
315 return str_replace('$ME', 'pn' . $sub, $where);
318 public function buildCondition(PlFilter
&$uf)
321 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
322 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
324 $right = XDB
::formatWildcards($this->mode
& self
::CONTAINS
, $this->text
);
326 $cond = $left . $right;
327 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
328 if (($this->mode
& self
::VARIANTS
) != 0 && isset(Profile
::$name_variants[$this->type
])) {
329 foreach (Profile
::$name_variants[$this->type
] as $var) {
330 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
333 return implode(' OR ', $conds);
338 // {{{ class UFC_NameTokens
339 /** Selects users based on tokens in their name (for quicksearch)
340 * @param $tokens An array of tokens to search
341 * @param $flags Flags the tokens must have (e.g 'public' for public search)
342 * @param $soundex (bool) Whether those tokens are fulltext or soundex
344 class UFC_NameTokens
implements UserFilterCondition
347 const FLAG_PUBLIC
= 'public';
354 public function __construct($tokens, $flags = array(), $soundex = false
, $exact = false
)
356 $this->tokens
= $tokens;
357 if (is_array($flags)) {
358 $this->flags
= $flags;
360 $this->flags
= array($flags);
362 $this->soundex
= $soundex;
363 $this->exact
= $exact;
366 public function buildCondition(PlFilter
&$uf)
368 $sub = $uf->addNameTokensFilter(!($this->exact ||
$this->soundex
));
370 if ($this->soundex
) {
371 $conds[] = XDB
::format($sub . '.soundex IN {?}', $this->tokens
);
372 } else if ($this->exact
) {
373 $conds[] = XDB
::format($sub . '.token IN {?}', $this->tokens
);
376 foreach ($this->tokens
as $token) {
377 $tokconds[] = $sub . '.token ' . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $token);
379 $conds[] = implode(' OR ', $tokconds);
382 if ($this->flags
!= null
) {
383 $conds[] = XDB
::format($sub . '.flags IN {?}', $this->flags
);
386 return implode(' AND ', $conds);
391 // {{{ class UFC_Nationality
392 class UFC_Nationality
implements UserFilterCondition
396 public function __construct($val)
398 if (!is_array($val)) {
404 public function buildCondition(PlFilter
&$uf)
406 $uf->requireProfiles();
407 $nat = XDB
::formatArray($this->val
);
409 'p.nationality1 IN ' . $nat,
410 'p.nationality2 IN ' . $nat,
411 'p.nationality3 IN ' . $nat,
413 return implode(' OR ', $conds);
418 // {{{ class UFC_Dead
419 /** Filters users based on death date
420 * @param $comparison Comparison operator
421 * @param $date Date to which death date should be compared
423 class UFC_Dead
implements UserFilterCondition
428 public function __construct($comparison = null
, $date = null
)
430 $this->comparison
= $comparison;
434 public function buildCondition(PlFilter
&$uf)
436 $uf->requireProfiles();
437 $str = 'p.deathdate IS NOT NULL';
438 if (!is_null($this->comparison
)) {
439 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
446 // {{{ class UFC_Registered
447 /** Filters users based on registration state
448 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
449 * @param $comparison Comparison operator
450 * @param $date Date to which users registration date should be compared
452 class UFC_Registered
implements UserFilterCondition
458 public function __construct($active = false
, $comparison = null
, $date = null
)
460 $this->active
= $active;
461 $this->comparison
= $comparison;
465 public function buildCondition(PlFilter
&$uf)
467 $uf->requireAccounts();
469 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
471 $date = 'a.uid IS NOT NULL AND a.state != \'pending\'';
473 if (!is_null($this->comparison
)) {
474 $date .= ' AND a.registration_date ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
481 // {{{ class UFC_ProfileUpdated
482 /** Filters users based on profile update date
483 * @param $comparison Comparison operator
484 * @param $date Date to which profile update date must be compared
486 class UFC_ProfileUpdated
implements UserFilterCondition
491 public function __construct($comparison = null
, $date = null
)
493 $this->comparison
= $comparison;
497 public function buildCondition(PlFilter
&$uf)
499 $uf->requireProfiles();
500 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
505 // {{{ class UFC_Birthday
506 /** Filters users based on next birthday date
507 * @param $comparison Comparison operator
508 * @param $date Date to which users next birthday date should be compared
510 class UFC_Birthday
implements UserFilterCondition
515 public function __construct($comparison = null
, $date = null
)
517 $this->comparison
= $comparison;
521 public function buildCondition(PlFilter
&$uf)
523 $uf->requireProfiles();
524 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
530 /** Filters users based on sex
531 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
533 class UFC_Sex
implements UserFilterCondition
536 public function __construct($sex)
541 public function buildCondition(PlFilter
&$uf)
543 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
544 return self
::COND_FALSE
;
546 $uf->requireProfiles();
547 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
553 // {{{ class UFC_Group
554 /** Filters users based on group membership
555 * @param $group Group whose members we are selecting
556 * @param $anim Whether to restrict selection to animators of that group
558 class UFC_Group
implements UserFilterCondition
562 public function __construct($group, $anim = false
)
564 $this->group
= $group;
568 public function buildCondition(PlFilter
&$uf)
570 $sub = $uf->addGroupFilter($this->group
);
571 $where = 'gpm' . $sub . '.perms IS NOT NULL';
573 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
580 // {{{ class UFC_Binet
581 /** Selects users based on their belonging to a given (list of) binet
582 * @param $binet either a binet_id or an array of binet_ids
584 class UFC_Binet
implements UserFilterCondition
588 public function __construct($val)
590 if (!is_array($val)) {
596 public function buildCondition(PlFilter
&$uf)
598 $sub = $uf->addBinetsFilter();
599 return XDB
::format($sub . '.binet_id IN {?}', $this->val
);
604 // {{{ class UFC_Section
605 /** Selects users based on section
606 * @param $section ID of the section
608 class UFC_Section
implements UserFilterCondition
612 public function __construct($section)
614 $this->section
= $section;
617 public function buildCondition(PlFilter
&$uf)
619 $uf->requireProfiles();
620 return 'p.section = ' . XDB
::format('{?}', $this->section
);
625 // {{{ class UFC_Email
626 /** Filters users based on an email or a list of emails
627 * @param $emails List of emails whose owner must be selected
629 class UFC_Email
implements UserFilterCondition
632 public function __construct()
634 $this->emails
= func_get_args();
637 public function buildCondition(PlFilter
&$uf)
644 if (count($this->emails
) == 0) {
645 return PlFilterCondition
::COND_TRUE
;
648 foreach ($this->emails
as $entry) {
649 if (User
::isForeignEmailAddress($entry)) {
651 } else if (User
::isVirtualEmailAddress($entry)) {
654 @list
($user, $domain) = explode('@', $entry);
659 if (count($foreign) > 0) {
660 $sub = $uf->addEmailRedirectFilter($foreign);
661 $cond[] = XDB
::format('e' . $sub . '.email IS NOT NULL OR a.email IN {?}', $foreign);
663 if (count($virtual) > 0) {
664 $sub = $uf->addVirtualEmailFilter($virtual);
665 $cond[] = 'vr' . $sub . '.redirect IS NOT NULL';
667 if (count($aliases) > 0) {
668 $sub = $uf->addAliasFilter($aliases);
669 $cond[] = 'al' . $sub . '.alias IS NOT NULL';
671 return '(' . implode(') OR (', $cond) . ')';
676 // {{{ class UFC_Address
677 abstract class UFC_Address
implements UserFilterCondition
679 /** Valid address type ('hq' is reserved for company addresses)
685 /** Text for these types
687 protected static $typetexts = array(
688 self
::TYPE_HOME
=> 'home',
689 self
::TYPE_PRO
=> 'pro',
694 /** Flags for addresses
696 const FLAG_CURRENT
= 0x0001;
697 const FLAG_TEMP
= 0x0002;
698 const FLAG_SECOND
= 0x0004;
699 const FLAG_MAIL
= 0x0008;
700 const FLAG_CEDEX
= 0x0010;
702 // Binary OR of those flags
703 const FLAG_ANY
= 0x001F;
705 /** Text of these flags
707 protected static $flagtexts = array(
708 self
::FLAG_CURRENT
=> 'current',
709 self
::FLAG_TEMP
=> 'temporary',
710 self
::FLAG_SECOND
=> 'secondary',
711 self
::FLAG_MAIL
=> 'mail',
712 self
::FLAG_CEDEX
=> 'cedex',
717 public function __construct($type = null
, $flags = null
)
720 $this->flags
= $flags;
723 protected function initConds($sub)
727 foreach (self
::$typetexts as $flag => $type) {
728 if ($flag & $this->type
) {
733 $conds[] = XDB
::foramt($sub . '.type IN {?}', $types);
736 if ($this->flags
!= self
::FLAG_ANY
) {
737 foreach(self
::$flagtexts as $flag => $text) {
738 if ($flag & $this->flags
) {
739 $conds[] = 'FIND_IN_SET(' . XDB
::format('{?}', $text) . ', ' . $sub . '.flags)';
749 // {{{ class UFC_AddressText
750 /** Select users based on their address, using full text search
751 * @param $text Text for filter in fulltext search
752 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
753 * @param $type Filter on address type
754 * @param $flags Filter on address flags
755 * @param $country Filter on address country
756 * @param $locality Filter on address locality
758 class UFC_AddressText
extends UFC_Address
762 private $textSearchMode;
764 public function __construct($text = null
, $textSearchMode = XDB
::WILDCARD_CONTAINS
,
765 $type = null
, $flags = self
::FLAG_ANY
, $country = null
, $locality = null
)
767 parent
::__construct($type, $flags);
769 $this->textSearchMode
= $textSearchMode;
770 $this->country
= $country;
771 $this->locality
= $locality;
774 private function mkMatch($txt)
776 return XDB
::formatWildcards($this->textSearchMode
, $txt);
779 public function buildCondition(PlFilter
&$uf)
781 $sub = $uf->addAddressFilter();
782 $conds = $this->initConds($sub);
783 if ($this->text
!= null
) {
784 $conds[] = $sub . '.text' . $this->mkMatch($this->text
);
787 if ($this->country
!= null
) {
788 $subc = $uf->addAddressCountryFilter();
790 $subconds[] = $subc . '.country' . $this->mkMatch($this->country
);
791 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country
);
792 $conds[] = implode(' OR ', $subconds);
795 if ($this->locality
!= null
) {
796 $subl = $uf->addAddressLocalityFilter();
797 $conds[] = $subl . '.name' . $this->mkMatch($this->locality
);
800 return implode(' AND ', $conds);
805 // {{{ class UFC_AddressField
806 /** Filters users based on their address,
807 * @param $val Either a code for one of the fields, or an array of such codes
808 * @param $fieldtype The type of field to look for
809 * @param $type Filter on address type
810 * @param $flags Filter on address flags
812 class UFC_AddressField
extends UFC_Address
814 const FIELD_COUNTRY
= 1;
815 const FIELD_ADMAREA
= 2;
816 const FIELD_SUBADMAREA
= 3;
817 const FIELD_LOCALITY
= 4;
818 const FIELD_ZIPCODE
= 5;
820 /** Data of the filter
825 public function __construct($val, $fieldtype, $type = null
, $flags = self
::FLAG_ANY
)
827 parent
::__construct($type, $flags);
829 if (!is_array($val)) {
833 $this->fieldtype
= $fieldtype;
836 public function buildCondition(PlFilter
&$uf)
838 $sub = $uf->addAddressFilter();
839 $conds = $this->initConds($sub);
841 switch ($this->fieldtype
) {
842 case self
::FIELD_COUNTRY
:
843 $field = 'countryId';
845 case self
::FIELD_ADMAREA
:
846 $field = 'administrativeAreaId';
848 case self
::FIELD_SUBADMAREA
:
849 $field = 'subAdministrativeAreaId';
851 case self
::FIELD_LOCALITY
:
852 $field = 'localityId';
854 case self
::FIELD_ZIPCODE
:
855 $field = 'postalCode';
858 Platal
::page()->killError('Invalid address field type: ' . $this->fieldtype
);
860 $conds[] = XDB
::format($sub . '.' . $field . ' IN {?}', $this->val
);
862 return implode(' AND ', $conds);
867 // {{{ class UFC_Corps
868 /** Filters users based on the corps they belong to
869 * @param $corps Corps we are looking for (abbreviation)
870 * @param $type Whether we search for original or current corps
872 class UFC_Corps
implements UserFilterCondition
880 public function __construct($corps, $type = self
::CURRENT
)
882 $this->corps
= $corps;
886 public function buildCondition(PlFilter
&$uf)
888 /** Tables shortcuts:
889 * pc for profile_corps,
890 * pceo for profile_corps_enum - orginal
891 * pcec for profile_corps_enum - current
893 $sub = $uf->addCorpsFilter($this->type
);
894 $cond = $sub . '.abbreviation = ' . $corps;
900 // {{{ class UFC_Corps_Rank
901 /** Filters users based on their rank in the corps
902 * @param $rank Rank we are looking for (abbreviation)
904 class UFC_Corps_Rank
implements UserFilterCondition
907 public function __construct($rank)
912 public function buildCondition(PlFilter
&$uf)
914 /** Tables shortcuts:
915 * pcr for profile_corps_rank
917 $sub = $uf->addCorpsRankFilter();
918 $cond = $sub . '.abbreviation = ' . $rank;
924 // {{{ class UFC_Job_Company
925 /** Filters users based on the company they belong to
926 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
927 * @param $value The searched value
929 class UFC_Job_Company
implements UserFilterCondition
932 const JOBNAME
= 'name';
933 const JOBACRONYM
= 'acronym';
938 public function __construct($type, $value)
940 $this->assertType($type);
942 $this->value
= $value;
945 private function assertType($type)
947 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
948 Platal
::page()->killError("Type de recherche non valide.");
952 public function buildCondition(PlFilter
&$uf)
954 $sub = $uf->addJobCompanyFilter();
955 $cond = $sub . '.' . $this->type
. ' = ' . XDB
::format('{?}', $this->value
);
961 // {{{ class UFC_Job_Sectorization
962 /** Filters users based on the ((sub)sub)sector they work in
963 * @param $val The ID of the sector, or an array of such IDs
964 * @param $type The kind of search (subsubsector/subsector/sector)
966 class UFC_Job_Sectorization
implements UserFilterCondition
971 public function __construct($val, $type = UserFilter
::JOB_SECTOR
)
973 self
::assertType($type);
974 if (!is_array($val)) {
981 private static function assertType($type)
983 if ($type != UserFilter
::JOB_SECTOR
&& $type != UserFilter
::JOB_SUBSECTOR
&& $type != UserFilter
::JOB_SUBSUBSECTOR
) {
984 Platal
::page()->killError("Type de secteur non valide.");
988 public function buildCondition(PlFilter
&$uf)
990 $sub = $uf->addJobSectorizationFilter($this->type
);
991 return $sub . '.id = ' . XDB
::format('{?}', $this->val
);
996 // {{{ class UFC_Job_Description
997 /** Filters users based on their job description
998 * @param $description The text being searched for
999 * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
1001 class UFC_Job_Description
implements UserFilterCondition
1004 private $description;
1007 public function __construct($description, $fields)
1009 $this->fields
= $fields;
1010 $this->description
= $description;
1013 public function buildCondition(PlFilter
&$uf)
1016 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
1017 $sub = $uf->addJobFilter();
1018 $conds[] = $sub . '.description ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1020 if ($this->fields
& UserFilter
::JOB_CV
) {
1021 $uf->requireProfiles();
1022 $conds[] = 'p.cv ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1024 if ($this->fields
& UserFilter
::JOB_SECTOR
) {
1025 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SECTOR
);
1026 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1028 if ($this->fields
& UserFilter
::JOB_SUBSECTOR
) {
1029 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSECTOR
);
1030 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1032 if ($this->fields
& UserFilter
::JOB_SUBSUBSECTOR
) {
1033 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSUBSECTOR
);
1034 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1035 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_ALTERNATES
);
1036 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1038 return implode(' OR ', $conds);
1043 // {{{ class UFC_Networking
1044 /** Filters users based on network identity (IRC, ...)
1045 * @param $type Type of network (-1 for any)
1046 * @param $value Value to search
1048 class UFC_Networking
implements UserFilterCondition
1053 public function __construct($type, $value)
1055 $this->type
= $type;
1056 $this->value
= $value;
1059 public function buildCondition(PlFilter
&$uf)
1061 $sub = $uf->addNetworkingFilter();
1063 $conds[] = $sub . '.address ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1064 if ($this->type
!= -1) {
1065 $conds[] = $sub . '.network_type = ' . XDB
::format('{?}', $this->type
);
1067 return implode(' AND ', $conds);
1072 // {{{ class UFC_Phone
1073 /** Filters users based on their phone number
1074 * @param $num_type Type of number (pro/user/home)
1075 * @param $phone_type Type of phone (fixed/mobile/fax)
1076 * @param $number Phone number
1078 class UFC_Phone
implements UserFilterCondition
1080 const NUM_PRO
= 'pro';
1081 const NUM_USER
= 'user';
1082 const NUM_HOME
= 'address';
1083 const NUM_ANY
= 'any';
1085 const PHONE_FIXED
= 'fixed';
1086 const PHONE_MOBILE
= 'mobile';
1087 const PHONE_FAX
= 'fax';
1088 const PHONE_ANY
= 'any';
1091 private $phone_type;
1094 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
1096 require_once('profil.func.inc.php');
1097 $this->number
= $number;
1098 $this->num_type
= $num_type;
1099 $this->phone_type
= format_phone_number($phone_type);
1102 public function buildCondition(PlFilter
&$uf)
1104 $sub = $uf->addPhoneFilter();
1106 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
1107 if ($this->num_type
!= self
::NUM_ANY
) {
1108 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
1110 if ($this->phone_type
!= self
::PHONE_ANY
) {
1111 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
1113 return implode(' AND ', $conds);
1118 // {{{ class UFC_Medal
1119 /** Filters users based on their medals
1120 * @param $medal ID of the medal
1121 * @param $grade Grade of the medal (null for 'any')
1123 class UFC_Medal
implements UserFilterCondition
1128 public function __construct($medal, $grade = null
)
1130 $this->medal
= $medal;
1131 $this->grade
= $grade;
1134 public function buildCondition(PlFilter
&$uf)
1137 $sub = $uf->addMedalFilter();
1138 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
1139 if ($this->grade
!= null
) {
1140 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
1142 return implode(' AND ', $conds);
1147 // {{{ class UFC_Photo
1148 /** Filters profiles with photo
1150 class UFC_Photo
implements UserFilterCondition
1152 public function buildCondition(PlFilter
&$uf)
1154 $uf->addPhotoFilter();
1155 return 'photo.attach IS NOT NULL';
1160 // {{{ class UFC_Mentor_Expertise
1161 /** Filters users by mentoring expertise
1162 * @param $expertise Domain of expertise
1164 class UFC_Mentor_Expertise
implements UserFilterCondition
1168 public function __construct($expertise)
1170 $this->expertise
= $expertise;
1173 public function buildCondition(PlFilter
&$uf)
1175 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_EXPERTISE
);
1176 return $sub . '.expertise ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->expertise
);
1181 // {{{ class UFC_Mentor_Country
1182 /** Filters users by mentoring country
1183 * @param $country Two-letters code of country being searched
1185 class UFC_Mentor_Country
implements UserFilterCondition
1189 public function __construct($country)
1191 $this->country
= $country;
1194 public function buildCondition(PlFilter
&$uf)
1196 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_COUNTRY
);
1197 return $sub . '.country = ' . XDB
::format('{?}', $this->country
);
1202 // {{{ class UFC_Mentor_Sectorization
1203 /** Filters users based on mentoring (sub|)sector
1204 * @param $sector ID of (sub)sector
1205 * @param $type Whether we are looking for a sector or a subsector
1207 class UFC_Mentor_Sectorization
implements UserFilterCondition
1210 const SUBSECTOR
= 2;
1214 public function __construct($sector, $type = self
::SECTOR
)
1216 $this->sector
= $sector;
1217 $this->type
= $type;
1220 public function buildCondition(PlFilter
&$uf)
1222 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_SECTOR
);
1223 if ($this->type
== self
::SECTOR
) {
1224 $field = 'sectorid';
1226 $field = 'subsectorid';
1228 return $sub . '.' . $field . ' = ' . XDB
::format('{?}', $this->sector
);
1233 // {{{ class UFC_UserRelated
1234 /** Filters users based on a relation toward a user
1235 * @param $user User to which searched users are related
1237 abstract class UFC_UserRelated
implements UserFilterCondition
1240 public function __construct(PlUser
&$user)
1242 $this->user
=& $user;
1247 // {{{ class UFC_Contact
1248 /** Filters users who belong to selected user's contacts
1250 class UFC_Contact
extends UFC_UserRelated
1252 public function buildCondition(PlFilter
&$uf)
1254 $sub = $uf->addContactFilter($this->user
->id());
1255 return 'c' . $sub . '.contact IS NOT NULL';
1260 // {{{ class UFC_WatchRegistration
1261 /** Filters users being watched by selected user
1263 class UFC_WatchRegistration
extends UFC_UserRelated
1265 public function buildCondition(PlFilter
&$uf)
1267 if (!$this->user
->watchType('registration')) {
1268 return PlFilterCondition
::COND_FALSE
;
1270 $uids = $this->user
->watchUsers();
1271 if (count($uids) == 0) {
1272 return PlFilterCondition
::COND_FALSE
;
1274 return XDB
::format('$UID IN {?}', $uids);
1280 // {{{ class UFC_WatchPromo
1281 /** Filters users belonging to a promo watched by selected user
1282 * @param $user Selected user (the one watching promo)
1283 * @param $grade Formation the user is watching
1285 class UFC_WatchPromo
extends UFC_UserRelated
1288 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
1290 parent
::__construct($user);
1291 $this->grade
= $grade;
1294 public function buildCondition(PlFilter
&$uf)
1296 $promos = $this->user
->watchPromos();
1297 if (count($promos) == 0) {
1298 return PlFilterCondition
::COND_FALSE
;
1300 $sube = $uf->addEducationFilter(true
, $this->grade
);
1301 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
1302 return XDB
::format($field . ' IN {?}', $promos);
1308 // {{{ class UFC_WatchContact
1309 /** Filters users watched by selected user
1311 class UFC_WatchContact
extends UFC_Contact
1313 public function buildCondition(PlFilter
&$uf)
1315 if (!$this->user
->watchContacts()) {
1316 return PlFilterCondition
::COND_FALSE
;
1318 return parent
::buildCondition($uf);
1323 // {{{ class UFC_MarketingHash
1324 /** Filters users using the hash generated
1325 * to send marketing emails to him.
1327 class UFC_MarketingHash
implements UserFilterCondition
1331 public function __construct($hash)
1333 $this->hash
= $hash;
1336 public function buildCondition(PlFilter
&$uf)
1338 $table = $uf->addMarketingHash();
1339 return XDB
::format('rm.hash = {?}', $this->hash
);
1348 // {{{ class UserFilterOrder
1349 /** Base class for ordering results of a query.
1350 * Parameters for the ordering must be given to the constructor ($desc for a
1351 * descending order).
1352 * The getSortTokens function is used to get actual ordering part of the query.
1354 abstract class UserFilterOrder
extends PlFilterOrder
1356 /** This function must return the tokens to use for ordering
1357 * @param &$uf The UserFilter whose results must be ordered
1358 * @return The name of the field to use for ordering results
1360 // abstract protected function getSortTokens(UserFilter &$uf);
1364 // {{{ class UFO_Promo
1365 /** Orders users by promotion
1366 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
1367 * @param $desc Whether sort is descending
1369 class UFO_Promo
extends UserFilterOrder
1373 public function __construct($grade = null
, $desc = false
)
1375 parent
::__construct($desc);
1376 $this->grade
= $grade;
1379 protected function getSortTokens(PlFilter
&$uf)
1381 if (UserFilter
::isGrade($this->grade
)) {
1382 $sub = $uf->addEducationFilter($this->grade
);
1383 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
1385 $sub = $uf->addDisplayFilter();
1386 return 'pd' . $sub . '.promo';
1392 // {{{ class UFO_Name
1393 /** Sorts users by name
1394 * @param $type Type of name on which to sort (firstname...)
1395 * @param $variant Variant of that name to use (marital, ordinary...)
1396 * @param $particle Set to true if particles should be included in the sorting order
1397 * @param $desc If sort order should be descending
1399 class UFO_Name
extends UserFilterOrder
1405 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
1407 parent
::__construct($desc);
1408 $this->type
= $type;
1409 $this->variant
= $variant;
1410 $this->particle
= $particle;
1413 protected function getSortTokens(PlFilter
&$uf)
1415 if (Profile
::isDisplayName($this->type
)) {
1416 $sub = $uf->addDisplayFilter();
1417 return 'pd' . $sub . '.' . $this->type
;
1419 $sub = $uf->addNameFilter($this->type
, $this->variant
);
1420 if ($this->particle
) {
1421 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1423 return 'pn' . $sub . '.name';
1430 // {{{ class UFO_Score
1431 class UFO_Score
extends UserFilterOrder
1433 protected function getSortTokens(PlFilter
&$uf)
1435 $sub = $uf->addNameTokensFilter();
1436 return 'SUM(' . $sub . '.score)';
1441 // {{{ class UFO_Registration
1442 /** Sorts users based on registration date
1444 class UFO_Registration
extends UserFilterOrder
1446 protected function getSortTokens(PlFilter
&$uf)
1448 return 'a.registration_date';
1453 // {{{ class UFO_Birthday
1454 /** Sorts users based on next birthday date
1456 class UFO_Birthday
extends UserFilterOrder
1458 protected function getSortTokens(PlFilter
&$uf)
1460 return 'p.next_birthday';
1465 // {{{ class UFO_ProfileUpdate
1466 /** Sorts users based on last profile update
1468 class UFO_ProfileUpdate
extends UserFilterOrder
1470 protected function getSortTokens(PlFilter
&$uf)
1472 return 'p.last_change';
1477 // {{{ class UFO_Death
1478 /** Sorts users based on death date
1480 class UFO_Death
extends UserFilterOrder
1482 protected function getSortTokens(PlFilter
&$uf)
1484 return 'p.deathdate';
1490 /***********************************
1491 *********************************
1493 *********************************
1494 ***********************************/
1496 // {{{ class UserFilter
1497 /** This class provides a convenient and centralized way of filtering users.
1500 * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t));
1502 * Resulting UserFilter can be used to:
1503 * - get a list of User objects matching the filter
1504 * - get a list of UIDs matching the filter
1505 * - get the number of users matching the filter
1506 * - check whether a given User matches the filter
1507 * - filter a list of User objects depending on whether they match the filter
1509 * Usage for UFC and UFO objects:
1510 * A UserFilter will call all private functions named XXXJoins.
1511 * These functions must return an array containing the list of join
1512 * required by the various UFC and UFO associated to the UserFilter.
1513 * Entries in those returned array are of the following form:
1514 * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter')
1515 * which will be translated into :
1516 * join_type JOIN joined_table AS join_tablealias ON (join_criter)
1517 * in the final query.
1519 * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with
1520 * profile.pid, and $UID with accounts.uid.
1522 * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined;
1523 * its parameter will be used to set various private vars of the UserFilter describing
1524 * the required joins ; such a function shall return the "join_tablealias" to use
1525 * when referring to the joined table.
1527 * For example, if data from profile_job must be available to filter results,
1528 * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and
1529 * return 'pj', the short name to use when referring to profile_job; when building
1530 * the query, calling the jobJoins function will return an array containing a single
1532 * 'pj' => array('left', 'profile_job', '$ME.pid = $UID');
1534 * The 'register_optional' function can be used to generate unique table aliases when
1535 * the same table has to be joined several times with different aliases.
1537 class UserFilter
extends PlFilter
1539 protected $joinMethods = array();
1541 protected $joinMetas = array(
1547 private $sort = array();
1548 private $query = null
;
1549 private $orderby = null
;
1551 private $lastcount = null
;
1553 public function __construct($cond = null
, $sort = null
)
1555 if (empty($this->joinMethods
)) {
1556 $class = new ReflectionClass('UserFilter');
1557 foreach ($class->getMethods() as $method) {
1558 $name = $method->getName();
1559 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
1560 $this->joinMethods
[] = $name;
1564 if (!is_null($cond)) {
1565 if ($cond instanceof PlFilterCondition
) {
1566 $this->setCondition($cond);
1569 if (!is_null($sort)) {
1570 if ($sort instanceof UserFilterOrder
) {
1571 $this->addSort($sort);
1572 } else if (is_array($sort)) {
1573 foreach ($sort as $s) {
1580 private function buildQuery()
1582 if (is_null($this->orderby
)) {
1584 foreach ($this->sort
as $sort) {
1585 $orders = array_merge($orders, $sort->buildSort($this));
1587 if (count($orders) == 0) {
1588 $this->orderby
= '';
1590 $this->orderby
= 'ORDER BY ' . implode(', ', $orders);
1593 if (is_null($this->query
)) {
1594 $where = $this->root
->buildCondition($this);
1595 if ($this->with_forced_sn
) {
1596 $this->requireProfiles();
1597 $from = 'search_name AS sn';
1598 } else if ($this->with_accounts
) {
1599 $from = 'accounts AS a';
1601 $this->requireProfiles();
1602 $from = 'profiles AS p';
1604 $joins = $this->buildJoins();
1605 $this->query
= 'FROM ' . $from . '
1607 WHERE (' . $where . ')';
1611 private function getUIDList($uids = null
, PlLimit
&$limit)
1613 $this->requireAccounts();
1614 $this->buildQuery();
1615 $lim = $limit->getSql();
1617 if (!is_null($uids)) {
1618 $cond = XDB
::format(' AND a.uid IN {?}', $uids);
1620 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1621 ' . $this->query
. $cond . '
1623 ' . $this->orderby
. '
1625 $this->lastcount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
1629 private function getPIDList($pids = null
, PlLimit
&$limit)
1631 $this->requireProfiles();
1632 $this->buildQuery();
1633 $lim = $limit->getSql();
1635 if (!is_null($pids)) {
1636 $cond = XDB
::format(' AND p.pid IN {?}', $pids);
1638 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid
1639 ' . $this->query
. $cond . '
1641 ' . $this->orderby
. '
1643 $this->lastcount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
1647 private static function defaultLimit($limit) {
1648 if ($limit == null
) {
1649 return new PlLimit();
1655 /** Check that the user match the given rule.
1657 public function checkUser(PlUser
&$user)
1659 $this->requireAccounts();
1660 $this->buildQuery();
1661 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
1662 ' . $this->query
. XDB
::format(' AND a.uid = {?}', $user->id()));
1666 /** Check that the profile match the given rule.
1668 public function checkProfile(Profile
&$profile)
1670 $this->requireProfiles();
1671 $this->buildQuery();
1672 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
1673 ' . $this->query
. XDB
::format(' AND p.pid = {?}', $profile->id()));
1677 /** Default filter is on users
1679 public function filter(array $users, $limit = null
)
1681 return $this->filterUsers($users, self
::defaultLimit($limit));
1684 /** Filter a list of users to extract the users matching the rule.
1686 public function filterUsers(array $users, $limit = null
)
1688 $limit = self
::defaultLimit($limit);
1689 $this->requireAccounts();
1690 $this->buildQuery();
1693 foreach ($users as $user) {
1694 if ($user instanceof PlUser
) {
1700 $table[$uid] = $user;
1702 $fetched = $this->getUIDList($uids, $limit);
1704 foreach ($fetched as $uid) {
1705 $output[] = $table[$uid];
1710 /** Filter a list of profiles to extract the users matching the rule.
1712 public function filterProfiles(array $profiles, $limit = null
)
1714 $limit = self
::defaultLimit($limit);
1715 $this->requireProfiles();
1716 $this->buildQuery();
1719 foreach ($profiles as $profile) {
1720 if ($profile instanceof Profile
) {
1721 $pid = $profile->id();
1726 $table[$pid] = $profile;
1728 $fetched = $this->getPIDList($pids, $limit);
1730 foreach ($fetched as $pid) {
1731 $output[] = $table[$pid];
1736 public function getUIDs($limit = null
)
1738 $limit = self
::defaultLimit($limit);
1739 return $this->getUIDList(null
, $limit);
1742 public function getUID($pos = 0)
1744 $uids =$this->getUIDList(null
, new PlFilter(1, $pos));
1745 if (count($uids) == 0) {
1752 public function getPIDs($limit = null
)
1754 $limit = self
::defaultLimit($limit);
1755 return $this->getPIDList(null
, $limit);
1758 public function getPID($pos = 0)
1760 $pids =$this->getPIDList(null
, new PlFilter(1, $pos));
1761 if (count($pids) == 0) {
1768 public function getUsers($limit = null
)
1770 return User
::getBulkUsersWithUIDs($this->getUIDs($limit));
1773 public function getUser($pos = 0)
1775 $uid = $this->getUID($pos);
1779 return User
::getWithUID($uid);
1783 public function iterUsers($limit = null
)
1785 return User
::iterOverUIDs($this->getUIDs($limit));
1788 public function getProfiles($limit = null
, $fields = 0x0000, $visibility = null
)
1790 return Profile
::getBulkProfilesWithPIDs($this->getPIDs($limit), $fields, $visibility);
1793 public function getProfile($pos = 0, $fields = 0x0000, $visibility = null
)
1795 $pid = $this->getPID($pos);
1799 return Profile
::get($pid, $fields, $visibility);
1803 public function iterProfiles($limit = null
, $fields = 0x0000, $visibility = null
)
1805 return Profile
::iterOverPIDs($this->getPIDs($limit), true
, $fields, $visibility);
1808 public function get($limit = null
)
1810 return $this->getUsers($limit);
1813 public function getTotalCount()
1815 if (is_null($this->lastcount
)) {
1816 $this->buildQuery();
1817 if ($this->with_accounts
) {
1822 return (int)XDB
::fetchOneCell('SELECT COUNT(DISTINCT ' . $field . ')
1825 return $this->lastcount
;
1829 public function setCondition(PlFilterCondition
&$cond)
1831 $this->root
=& $cond;
1832 $this->query
= null
;
1835 public function addSort(PlFilterOrder
&$sort)
1837 $this->sort
[] = $sort;
1838 $this->orderby
= null
;
1841 static public function getLegacy($promo_min, $promo_max)
1843 if ($promo_min != 0) {
1844 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
1846 $min = new PFC_True();
1848 if ($promo_max != 0) {
1849 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
1851 $max = new PFC_True();
1853 return new UserFilter(new PFC_And($min, $max));
1856 static public function sortByName()
1858 return array(new UFO_Name(Profile
::LASTNAME
), new UFO_Name(Profile
::FIRSTNAME
));
1861 static public function sortByPromo()
1863 return array(new UFO_Promo(), new UFO_Name(Profile
::LASTNAME
), new UFO_Name(Profile
::FIRSTNAME
));
1866 static private function getDBSuffix($string)
1868 if (is_array($string)) {
1869 if (count($string) == 1) {
1870 return self
::getDBSuffix(array_pop($string));
1872 return md5(implode('|', $string));
1874 return preg_replace('/[^a-z0-9]/i', '', $string);
1879 /** Stores a new (and unique) table alias in the &$table table
1880 * @param &$table Array in which the table alias must be stored
1881 * @param $val Value which will then be used to build the join
1882 * @return Name of the newly created alias
1884 private $option = 0;
1885 private function register_optional(array &$table, $val)
1887 if (is_null($val)) {
1888 $sub = $this->option++
;
1891 $sub = self
::getDBSuffix($val);
1895 $table[$sub] = $index;
1899 /** PROFILE VS ACCOUNT
1901 private $with_profiles = false
;
1902 private $with_accounts = false
;
1903 private $with_forced_sn = false
;
1904 public function requireAccounts()
1906 $this->with_accounts
= true
;
1909 public function requireProfiles()
1911 $this->with_profiles
= true
;
1914 /** Forces the "FROM" to use search_name instead of accounts or profiles */
1915 public function forceSearchName()
1917 $this->with_forced_sn
= true
;
1920 protected function accountJoins()
1923 /** Quick search is much more efficient with sn first and PID second */
1924 if ($this->with_forced_sn
) {
1925 $joins['p'] = PlSqlJoin
::left('profiles', '$PID = sn.pid');
1926 if ($this->with_accounts
) {
1927 $joins['ap'] = PlSqlJoin
::left('account_profiles', '$ME.pid = $PID');
1928 $joins['a'] = PlSqlJoin
::left('accounts', '$UID = ap.uid');
1930 } else if ($this->with_profiles
&& $this->with_accounts
) {
1931 $joins['ap'] = PlSqlJoin
::left('account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)');
1932 $joins['p'] = PlSqlJoin
::left('profiles', '$PID = ap.pid');
1939 const DISPLAY
= 'display';
1940 private $pd = false
;
1941 public function addDisplayFilter()
1943 $this->requireProfiles();
1948 protected function displayJoins()
1951 return array('pd' => PlSqlJoin
::left('profile_display', '$ME.pid = $PID'));
1960 private $with_logger = false
;
1961 public function addLoggerFilter()
1963 $this->with_logger
= true
;
1964 $this->requireAccounts();
1967 protected function loggerJoins()
1970 if ($this->with_logger
) {
1971 $joins['ls'] = PlSqlJoin
::left('log_sessions', '$ME.uid = $UID');
1979 static public function assertName($name)
1981 if (!DirEnum
::getID(DirEnum
::NAMETYPES
, $name)) {
1982 Platal
::page()->kill('Invalid name type: ' . $name);
1986 private $pn = array();
1987 public function addNameFilter($type, $variant = null
)
1989 $this->requireProfiles();
1990 if (!is_null($variant)) {
1991 $ft = $type . '_' . $variant;
1996 self
::assertName($ft);
1998 if (!is_null($variant) && $variant == 'other') {
1999 $sub .= $this->option++
;
2001 $this->pn
[$sub] = DirEnum
::getID(DirEnum
::NAMETYPES
, $ft);
2005 protected function nameJoins()
2008 foreach ($this->pn
as $sub => $type) {
2009 $joins['pn' . $sub] = PlSqlJoin
::left('profile_name', '$ME.pid = $PID AND $ME.typeid = {?}', $type);
2016 private $with_sn = false
;
2017 // Set $doingQuickSearch to true if you wish to optimize the query
2018 public function addNameTokensFilter($doingQuickSearch = false
)
2020 $this->requireProfiles();
2021 $this->with_forced_sn
= ($this->with_forced_sn ||
$doingQuickSearch);
2022 $this->with_sn
= true
;
2026 protected function nameTokensJoins()
2028 /* We don't return joins, since with_sn forces the SELECT to run on search_name first */
2029 if ($this->with_sn
&& !$this->with_forced_sn
) {
2031 'sn' => PlSqlJoin
::left('search_name', '$ME.pid = $PID')
2041 private $with_nat = false
;
2042 public function addNationalityFilter()
2044 $this->with_nat
= true
;
2048 protected function nationalityJoins()
2051 if ($this->with_nat
) {
2052 $joins['ngc'] = PlSqlJoin
::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');
2059 const GRADE_ING
= 'Ing.';
2060 const GRADE_PHD
= 'PhD';
2061 const GRADE_MST
= 'M%';
2062 static public function isGrade($grade)
2064 return $grade == self
::GRADE_ING ||
$grade == self
::GRADE_PHD ||
$grade == self
::GRADE_MST
;
2067 static public function assertGrade($grade)
2069 if (!self
::isGrade($grade)) {
2070 Platal
::page()->killError("Diplôme non valide: $grade");
2074 static public function promoYear($grade)
2076 // XXX: Definition of promotion for phds and masters might change in near future.
2077 return ($grade == UserFilter
::GRADE_ING
) ?
'entry_year' : 'grad_year';
2080 private $pepe = array();
2081 private $with_pee = false
;
2082 public function addEducationFilter($x = false
, $grade = null
)
2084 $this->requireProfiles();
2086 $index = $this->option
;
2087 $sub = $this->option++
;
2089 self
::assertGrade($grade);
2092 $this->with_pee
= true
;
2095 $this->pepe
[$index] = $sub;
2099 protected function educationJoins()
2102 if ($this->with_pee
) {
2103 $joins['pee'] = PlSqlJoin
::inner('profile_education_enum', 'pee.abbreviation = \'X\'');
2105 foreach ($this->pepe
as $grade => $sub) {
2106 if ($this->isGrade($grade)) {
2107 $joins['pe' . $sub] = PlSqlJoin
::left('profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID');
2108 $joins['pede' . $sub] = PlSqlJoin
::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE {?}', $grade);
2110 $joins['pe' . $sub] = PlSqlJoin
::left('profile_education', '$ME.pid = $PID');
2111 $joins['pee' . $sub] = PlSqlJoin
::inner('profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
2112 $joins['pede' . $sub] = PlSqlJoin
::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
2121 private $gpm = array();
2122 public function addGroupFilter($group = null
)
2124 $this->requireAccounts();
2125 if (!is_null($group)) {
2126 if (is_int($group) ||
ctype_digit($group)) {
2127 $index = $sub = $group;
2130 $sub = self
::getDBSuffix($group);
2133 $sub = 'group_' . $this->option++
;
2137 $this->gpm
[$sub] = $index;
2141 protected function groupJoins()
2144 foreach ($this->gpm
as $sub => $key) {
2145 if (is_null($key)) {
2146 $joins['gpa' . $sub] = PlSqlJoin
::inner('groups');
2147 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
2148 } else if (is_int($key) ||
ctype_digit($key)) {
2149 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
2151 $joins['gpa' . $sub] = PlSqlJoin
::inner('groups', '$ME.diminutif = {?}', $key);
2152 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
2161 private $with_bi = false
;
2162 private $with_bd = false
;
2163 public function addBinetsFilter($with_enum = false
)
2165 $this->requireProfiles();
2166 $this->with_bi
= true
;
2168 $this->with_bd
= true
;
2175 protected function binetsJoins()
2178 if ($this->with_bi
) {
2179 $joins['bi'] = PlSqlJoin
::left('profile_binets', '$ME.pid = $PID');
2181 if ($this->with_bd
) {
2182 $joins['bd'] = PlSqlJoin
::left('profile_binet_enum', '$ME.id = bi.binet_id');
2189 private $e = array();
2190 public function addEmailRedirectFilter($email = null
)
2192 $this->requireAccounts();
2193 return $this->register_optional($this->e
, $email);
2196 private $ve = array();
2197 public function addVirtualEmailFilter($email = null
)
2199 $this->addAliasFilter(self
::ALIAS_FORLIFE
);
2200 return $this->register_optional($this->ve
, $email);
2203 const ALIAS_BEST
= 'bestalias';
2204 const ALIAS_FORLIFE
= 'forlife';
2205 private $al = array();
2206 public function addAliasFilter($alias = null
)
2208 $this->requireAccounts();
2209 return $this->register_optional($this->al
, $alias);
2212 protected function emailJoins()
2216 foreach ($this->e
as $sub=>$key) {
2217 if (is_null($key)) {
2218 $joins['e' . $sub] = PlSqlJoin
::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
2220 if (!is_array($key)) {
2223 $joins['e' . $sub] = PlSqlJoin
::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'
2224 AND $ME.email IN {?}' . $key);
2227 foreach ($this->al
as $sub=>$key) {
2228 if (is_null($key)) {
2229 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
2230 } else if ($key == self
::ALIAS_BEST
) {
2231 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
2232 } else if ($key == self
::ALIAS_FORLIFE
) {
2233 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type = \'a_vie\'');
2235 if (!is_array($key)) {
2238 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')
2239 AND $ME.alias IN {?}', $key);
2242 foreach ($this->ve
as $sub=>$key) {
2243 if (is_null($key)) {
2244 $joins['v' . $sub] = PlSqlJoin
::left('virtual', '$ME.type = \'user\'');
2246 if (!is_array($key)) {
2249 $joins['v' . $sub] = PlSqlJoin
::left('virtual', '$ME.type = \'user\' AND $ME.alias IN {?}', $key);
2251 $joins['vr' . $sub] = PlSqlJoin
::left('virtual_redirect',
2252 '$ME.vid = v' . $sub . '.vid
2253 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
2254 CONCAT(al_forlife.alias, \'@\', {?}),
2256 $globals->mail
->domain
, $globals->mail
->domain2
);
2264 private $with_pa = false
;
2265 public function addAddressFilter()
2267 $this->requireProfiles();
2268 $this->with_pa
= true
;
2272 private $with_pac = false
;
2273 public function addAddressCountryFilter()
2275 $this->requireProfiles();
2276 $this->addAddressFilter();
2277 $this->with_pac
= true
;
2281 private $with_pal = false
;
2282 public function addAddressLocalityFilter()
2284 $this->requireProfiles();
2285 $this->addAddressFilter();
2286 $this->with_pal
= true
;
2290 protected function addressJoins()
2293 if ($this->with_pa
) {
2294 $joins['pa'] = PlSqlJoin
::left('profile_addresses', '$ME.pid = $PID');
2296 if ($this->with_pac
) {
2297 $joins['gc'] = PlSqlJoin
::left('geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID');
2299 if ($this->with_pal
) {
2300 $joins['gl'] = PlSqlJoin
::left('geoloc_localities', '$ME.id = pa.localityID');
2309 private $pc = false
;
2310 private $pce = array();
2311 private $pcr = false
;
2312 public function addCorpsFilter($type)
2314 $this->requireProfiles();
2316 if ($type == UFC_Corps
::CURRENT
) {
2317 $pce['pcec'] = 'current_corpsid';
2319 } else if ($type == UFC_Corps
::ORIGIN
) {
2320 $pce['pceo'] = 'original_corpsid';
2325 public function addCorpsRankFilter()
2327 $this->requireProfiles();
2333 protected function corpsJoins()
2337 $joins['pc'] = PlSqlJoin
::left('profile_corps', '$ME.pid = $PID');
2340 $joins['pcr'] = PlSqlJoin
::left('profile_corps_rank_enum', '$ME.id = pc.rankid');
2342 foreach($this->pce
as $sub => $field) {
2343 $joins[$sub] = PlSqlJoin
::left('profile_corps_enum', '$ME.id = pc.' . $field);
2351 const JOB_SECTOR
= 0x0001;
2352 const JOB_SUBSECTOR
= 0x0002;
2353 const JOB_SUBSUBSECTOR
= 0x0004;
2354 const JOB_ALTERNATES
= 0x0008;
2355 const JOB_USERDEFINED
= 0x0010;
2356 const JOB_CV
= 0x0020;
2358 const JOB_SECTORIZATION
= 0x000F;
2359 const JOB_ANY
= 0x003F;
2363 * pje => profile_job_enum
2364 * pjse => profile_job_sector_enum
2365 * pjsse => profile_job_subsector_enum
2366 * pjssse => profile_job_subsubsector_enum
2367 * pja => profile_job_alternates
2369 private $with_pj = false
;
2370 private $with_pje = false
;
2371 private $with_pjse = false
;
2372 private $with_pjsse = false
;
2373 private $with_pjssse = false
;
2374 private $with_pja = false
;
2376 public function addJobFilter()
2378 $this->requireProfiles();
2379 $this->with_pj
= true
;
2383 public function addJobCompanyFilter()
2385 $this->addJobFilter();
2386 $this->with_pje
= true
;
2390 public function addJobSectorizationFilter($type)
2392 $this->addJobFilter();
2393 if ($type == self
::JOB_SECTOR
) {
2394 $this->with_pjse
= true
;
2396 } else if ($type == self
::JOB_SUBSECTOR
) {
2397 $this->with_pjsse
= true
;
2399 } else if ($type == self
::JOB_SUBSUBSECTOR
) {
2400 $this->with_pjssse
= true
;
2402 } else if ($type == self
::JOB_ALTERNATES
) {
2403 $this->with_pja
= true
;
2408 protected function jobJoins()
2411 if ($this->with_pj
) {
2412 $joins['pj'] = PlSqlJoin
::left('profile_job', '$ME.pid = $PID');
2414 if ($this->with_pje
) {
2415 $joins['pje'] = PlSqlJoin
::left('profile_job_enum', '$ME.id = pj.jobid');
2417 if ($this->with_pjse
) {
2418 $joins['pjse'] = PlSqlJoin
::left('profile_job_sector_enum', '$ME.id = pj.sectorid');
2420 if ($this->with_pjsse
) {
2421 $joins['pjsse'] = PlSqlJoin
::left('profile_job_subsector_enum', '$ME.id = pj.subsectorid');
2423 if ($this->with_pjssse
) {
2424 $joins['pjssse'] = PlSqlJoin
::left('profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
2426 if ($this->with_pja
) {
2427 $joins['pja'] = PlSqlJoin
::left('profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
2435 private $with_pnw = false
;
2436 public function addNetworkingFilter()
2438 $this->requireAccounts();
2439 $this->with_pnw
= true
;
2443 protected function networkingJoins()
2446 if ($this->with_pnw
) {
2447 $joins['pnw'] = PlSqlJoin
::left('profile_networking', '$ME.pid = $PID');
2455 private $with_ptel = false
;
2457 public function addPhoneFilter()
2459 $this->requireAccounts();
2460 $this->with_ptel
= true
;
2464 protected function phoneJoins()
2467 if ($this->with_ptel
) {
2468 $joins['ptel'] = PlSqlJoin
::left('profile_phones', '$ME.pid = $PID');
2476 private $with_pmed = false
;
2477 public function addMedalFilter()
2479 $this->requireProfiles();
2480 $this->with_pmed
= true
;
2484 protected function medalJoins()
2487 if ($this->with_pmed
) {
2488 $joins['pmed'] = PlSqlJoin
::left('profile_medals', '$ME.pid = $PID');
2496 private $pms = array();
2497 const MENTOR_EXPERTISE
= 1;
2498 const MENTOR_COUNTRY
= 2;
2499 const MENTOR_SECTOR
= 3;
2501 public function addMentorFilter($type)
2503 $this->requireAccounts();
2505 case self
::MENTOR_EXPERTISE
:
2506 $this->pms
['pme'] = 'profile_mentor';
2508 case self
::MENTOR_COUNTRY
:
2509 $this->pms
['pmc'] = 'profile_mentor_country';
2511 case self
::MENTOR_SECTOR
:
2512 $this->pms
['pms'] = 'profile_mentor_sector';
2515 Platal
::page()->killError("Undefined mentor filter.");
2519 protected function mentorJoins()
2522 foreach ($this->pms
as $sub => $tab) {
2523 $joins[$sub] = PlSqlJoin
::left($tab, '$ME.pid = $PID');
2530 private $cts = array();
2531 public function addContactFilter($uid = null
)
2533 $this->requireProfiles();
2534 return $this->register_optional($this->cts
, is_null($uid) ? null
: 'user_' . $uid);
2537 protected function contactJoins()
2540 foreach ($this->cts
as $sub=>$key) {
2541 if (is_null($key)) {
2542 $joins['c' . $sub] = PlSqlJoin
::left('contacts', '$ME.contact = $PID');
2544 $joins['c' . $sub] = PlSqlJoin
::left('contacts', '$ME.uid = {?} AND $ME.contact = $PID', substr($key, 5));
2553 private $wn = array();
2554 public function addWatchRegistrationFilter($uid = null
)
2556 $this->requireAccounts();
2557 return $this->register_optional($this->wn
, is_null($uid) ? null
: 'user_' . $uid);
2560 private $wp = array();
2561 public function addWatchPromoFilter($uid = null
)
2563 $this->requireAccounts();
2564 return $this->register_optional($this->wp
, is_null($uid) ? null
: 'user_' . $uid);
2567 private $w = array();
2568 public function addWatchFilter($uid = null
)
2570 $this->requireAccounts();
2571 return $this->register_optional($this->w
, is_null($uid) ? null
: 'user_' . $uid);
2574 protected function watchJoins()
2577 foreach ($this->w
as $sub=>$key) {
2578 if (is_null($key)) {
2579 $joins['w' . $sub] = PlSqlJoin
::left('watch');
2581 $joins['w' . $sub] = PlSqlJoin
::left('watch', '$ME.uid = {?}', substr($key, 5));
2584 foreach ($this->wn
as $sub=>$key) {
2585 if (is_null($key)) {
2586 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.ni_id = $UID');
2588 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
2591 foreach ($this->wn
as $sub=>$key) {
2592 if (is_null($key)) {
2593 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.ni_id = $UID');
2595 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
2598 foreach ($this->wp
as $sub=>$key) {
2599 if (is_null($key)) {
2600 $joins['wp' . $sub] = PlSqlJoin
::left('watch_promo');
2602 $joins['wp' . $sub] = PlSqlJoin
::left('watch_promo', '$ME.uid = {?}', substr($key, 5));
2611 private $with_photo;
2612 public function addPhotoFilter()
2614 $this->requireProfiles();
2615 $this->with_photo
= true
;
2618 protected function photoJoins()
2620 if ($this->with_photo
) {
2621 return array('photo' => PlSqlJoin
::left('profile_photos', '$ME.pid = $PID'));
2631 public function addMarketingHash()
2633 $this->requireAccounts();
2634 $this->with_rm
= true
;
2637 protected function marketingJoins()
2639 if ($this->with_rm
) {
2640 return array('rm' => PlSqlJoin
::left('register_marketing', '$ME.uid = $UID'));
2648 // {{{ class ProfileFilter
2649 class ProfileFilter
extends UserFilter
2651 public function get($limit = null
)
2653 return $this->getProfiles($limit);
2658 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: