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 $uf->requireProfiles();
50 return '$PID IS NOT NULL';
55 // {{{ class UFC_Hruid
56 /** Filters users based on their hruid
57 * @param $val Either an hruid, or a list of those
59 class UFC_Hruid
implements UserFilterCondition
63 public function __construct()
65 $this->hruids
= pl_flatten(func_get_args());
68 public function buildCondition(PlFilter
&$uf)
70 $uf->requireAccounts();
71 return XDB
::format('a.hruid IN {?}', $this->hruids
);
76 // {{{ class UFC_Hrpid
77 /** Filters users based on the hrpid of their profiles
78 * @param $val Either an hrpid, or a list of those
80 class UFC_Hrpid
implements UserFilterCondition
84 public function __construct()
86 $this->hrpids
= pl_flatten(func_get_args());
89 public function buildCondition(PlFilter
&$uf)
91 $uf->requireProfiles();
92 return XDB
::format('p.hrpid IN {?}', $this->hrpids
);
98 /** Filters users based on one of their last IPs
99 * @param $ip IP from which connection are checked
101 class UFC_Ip
implements UserFilterCondition
105 public function __construct($ip)
110 public function buildCondition(PlFilter
&$uf)
112 $sub = $uf->addLoggerFilter();
113 $ip = ip_to_uint($this->ip
);
114 return XDB
::format($sub . '.ip = {?} OR ' . $sub . '.forward_ip = {?}', $ip, $ip);
119 // {{{ class UFC_Comment
120 class UFC_Comment
implements UserFilterCondition
124 public function __construct($text)
129 public function buildCondition(PlFilter
&$uf)
131 $uf->requireProfiles();
132 return 'p.freetext ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->text
);
137 // {{{ class UFC_Promo
138 /** Filters users based on promotion
139 * @param $comparison Comparison operator (>, =, ...)
140 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
141 * @param $promo Promotion on which the filter is based
143 class UFC_Promo
implements UserFilterCondition
150 public function __construct($comparison, $grade, $promo)
152 $this->grade
= $grade;
153 $this->comparison
= $comparison;
154 $this->promo
= $promo;
155 if ($this->grade
!= UserFilter
::DISPLAY
) {
156 UserFilter
::assertGrade($this->grade
);
158 if ($this->grade
== UserFilter
::DISPLAY
&& $this->comparison
!= '=') {
159 Platal
::page()->killError('Comparison ' . $this->comparison
. ' not allowed on displaid promo');
163 public function buildCondition(PlFilter
&$uf)
165 if ($this->grade
== UserFilter
::DISPLAY
) {
166 $sub = $uf->addDisplayFilter();
167 return XDB
::format('pd' . $sub . '.promo ' . $this->comparison
. ' {?}', $this->promo
);
169 $sub = $uf->addEducationFilter(true
, $this->grade
);
170 $field = 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
171 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->promo
);
177 // {{{ class UFC_SchoolId
178 /** Filters users based on their shoold identifier
179 * @param type Parameter type (Xorg, AX, School)
180 * @param value School id value
182 class UFC_SchoolId
implements UserFilterCondition
186 const School
= 'school';
191 static public function assertType($type)
193 if ($type != self
::AX
&& $type != self
::Xorg
&& $type != self
::School
) {
194 Platal
::page()->killError("Type de matricule invalide: $type");
198 public function __construct($type, $id)
202 self
::assertType($type);
205 public function buildCondition(PlFilter
&$uf)
207 $uf->requireProfiles();
210 if ($type == self
::School
) {
212 $id = Profile
::getXorgId($id);
214 return XDB
::format('p.' . $type . '_id = {?}', $id);
219 // {{{ class UFC_EducationSchool
220 /** Filters users by formation
221 * @param $val The formation to search (either ID or array of IDs)
223 class UFC_EducationSchool
implements UserFilterCondition
227 public function __construct()
229 $this->val
= pl_flatten(func_get_args());
232 public function buildCondition(PlFilter
&$uf)
234 $sub = $uf->addEducationFilter();
235 return XDB
::format('pe' . $sub . '.eduid IN {?}', $this->val
);
240 // {{{ class UFC_EducationDegree
241 class UFC_EducationDegree
implements UserFilterCondition
245 public function __construct()
247 $this->diploma
= pl_flatten(func_get_args());
250 public function buildCondition(PlFilter
&$uf)
252 $sub = $uf->addEducationFilter();
253 return XDB
::format('pe' . $sub . '.degreeid IN {?}', $this->diploma
);
258 // {{{ class UFC_EducationField
259 class UFC_EducationField
implements UserFilterCondition
263 public function __construct()
265 $this->val
= pl_flatten(func_get_args());
268 public function buildCondition(PlFilter
&$uf)
270 $sub = $uf->addEducationFilter();
271 return XDB
::format('pe' . $sub . '.fieldid IN {?}', $this->val
);
276 // {{{ class UFC_Name
277 /** Filters users based on name
278 * @param $type Type of name field on which filtering is done (firstname, lastname...)
279 * @param $text Text on which to filter
280 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
282 class UFC_Name
implements UserFilterCondition
284 const EXACT
= XDB
::WILDCARD_EXACT
; // 0x000
285 const PREFIX
= XDB
::WILDCARD_PREFIX
; // 0x001
286 const SUFFIX
= XDB
::WILDCARD_SUFFIX
; // 0x002
287 const CONTAINS
= XDB
::WILDCARD_CONTAINS
; // 0x003
288 const PARTICLE
= 0x004;
289 const VARIANTS
= 0x008;
295 public function __construct($type, $text, $mode)
302 private function buildNameQuery($type, $variant, $where, UserFilter
&$uf)
304 $sub = $uf->addNameFilter($type, $variant);
305 return str_replace('$ME', 'pn' . $sub, $where);
308 public function buildCondition(PlFilter
&$uf)
311 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
312 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
314 $right = XDB
::formatWildcards($this->mode
& self
::CONTAINS
, $this->text
);
316 $cond = $left . $right;
317 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
318 if (($this->mode
& self
::VARIANTS
) != 0 && isset(Profile
::$name_variants[$this->type
])) {
319 foreach (Profile
::$name_variants[$this->type
] as $var) {
320 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
323 return implode(' OR ', $conds);
328 // {{{ class UFC_NameTokens
329 /** Selects users based on tokens in their name (for quicksearch)
330 * @param $tokens An array of tokens to search
331 * @param $flags Flags the tokens must have (e.g 'public' for public search)
332 * @param $soundex (bool) Whether those tokens are fulltext or soundex
334 class UFC_NameTokens
implements UserFilterCondition
337 const FLAG_PUBLIC
= 'public';
344 public function __construct($tokens, $flags = array(), $soundex = false
, $exact = false
)
346 if (is_array($tokens)) {
347 $this->tokens
= $tokens;
349 $this->tokens
= array($tokens);
351 if (is_array($flags)) {
352 $this->flags
= $flags;
354 $this->flags
= array($flags);
356 $this->soundex
= $soundex;
357 $this->exact
= $exact;
360 public function buildCondition(PlFilter
&$uf)
363 foreach ($this->tokens
as $i => $token) {
364 $sub = $uf->addNameTokensFilter($token);
365 if ($this->soundex
) {
366 $c = XDB
::format($sub . '.soundex = {?}', $token);
367 } else if ($this->exact
) {
368 $c = XDB
::format($sub . '.token = {?}', $token);
370 $c = $sub . '.token ' . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $token);
372 if ($this->flags
!= null
) {
373 $c .= XDB
::format(' AND ' . $sub . '.flags IN {?}', $this->flags
);
378 return implode(' AND ', $conds);
383 // {{{ class UFC_Nationality
384 class UFC_Nationality
implements UserFilterCondition
388 public function __construct()
390 $this->val
= pl_flatten(func_get_args());
393 public function buildCondition(PlFilter
&$uf)
395 $uf->requireProfiles();
396 $nat = XDB
::formatArray($this->val
);
398 'p.nationality1 IN ' . $nat,
399 'p.nationality2 IN ' . $nat,
400 'p.nationality3 IN ' . $nat,
402 return implode(' OR ', $conds);
407 // {{{ class UFC_Dead
408 /** Filters users based on death date
409 * @param $comparison Comparison operator
410 * @param $date Date to which death date should be compared (DateTime object, string or timestamp)
412 class UFC_Dead
implements UserFilterCondition
417 public function __construct($comparison = null
, $date = null
)
419 $this->comparison
= $comparison;
420 $this->date
= make_datetime($date);
423 public function buildCondition(PlFilter
&$uf)
425 $uf->requireProfiles();
426 $str = 'p.deathdate IS NOT NULL';
427 if (!is_null($this->comparison
)) {
428 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->date
->format('Y-m-d'));
435 // {{{ class UFC_Registered
436 /** Filters users based on registration state
437 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
438 * @param $comparison Comparison operator
439 * @param $date Date to which users registration date should be compared
441 class UFC_Registered
implements UserFilterCondition
447 public function __construct($active = false
, $comparison = null
, $date = null
)
449 $this->active
= $active;
450 $this->comparison
= $comparison;
451 $this->date
= make_datetime($date);
454 public function buildCondition(PlFilter
&$uf)
456 $uf->requireAccounts();
458 $date = '$UID IS NOT NULL AND a.state = \'active\'';
460 $date = '$UID IS NOT NULL AND a.state != \'pending\'';
462 if (!is_null($this->comparison
)) {
463 $date .= ' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->date
->format('Y-m-d'));
470 // {{{ class UFC_ProfileUpdated
471 /** Filters users based on profile update date
472 * @param $comparison Comparison operator
473 * @param $date Date to which profile update date must be compared
475 class UFC_ProfileUpdated
implements UserFilterCondition
480 public function __construct($comparison = null
, $date = null
)
482 $this->comparison
= $comparison;
486 public function buildCondition(PlFilter
&$uf)
488 $uf->requireProfiles();
489 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
494 // {{{ class UFC_Birthday
495 /** Filters users based on next birthday date
496 * @param $comparison Comparison operator
497 * @param $date Date to which users next birthday date should be compared
499 class UFC_Birthday
implements UserFilterCondition
504 public function __construct($comparison = null
, $date = null
)
506 $this->comparison
= $comparison;
510 public function buildCondition(PlFilter
&$uf)
512 $uf->requireProfiles();
513 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
519 /** Filters users based on sex
520 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
522 class UFC_Sex
implements UserFilterCondition
525 public function __construct($sex)
530 public function buildCondition(PlFilter
&$uf)
532 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
533 return self
::COND_FALSE
;
535 $uf->requireProfiles();
536 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
542 // {{{ class UFC_Group
543 /** Filters users based on group membership
544 * @param $group Group whose members we are selecting
545 * @param $anim Whether to restrict selection to animators of that group
547 class UFC_Group
implements UserFilterCondition
551 public function __construct($group, $anim = false
)
553 $this->group
= $group;
557 public function buildCondition(PlFilter
&$uf)
559 $sub = $uf->addGroupFilter($this->group
);
560 $where = 'gpm' . $sub . '.perms IS NOT NULL';
562 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
569 // {{{ class UFC_Binet
570 /** Selects users based on their belonging to a given (list of) binet
571 * @param $binet either a binet_id or an array of binet_ids
573 class UFC_Binet
implements UserFilterCondition
577 public function __construct()
579 $this->val
= pl_flatten(func_get_args());
582 public function buildCondition(PlFilter
&$uf)
584 $sub = $uf->addBinetsFilter();
585 return XDB
::format($sub . '.binet_id IN {?}', $this->val
);
590 // {{{ class UFC_Section
591 /** Selects users based on section
592 * @param $section ID of the section
594 class UFC_Section
implements UserFilterCondition
598 public function __construct()
600 $this->section
= pl_flatten(func_get_args());
603 public function buildCondition(PlFilter
&$uf)
605 $uf->requireProfiles();
606 return XDB
::format('p.section IN {?}', $this->section
);
611 // {{{ class UFC_Email
612 /** Filters users based on an email or a list of emails
613 * @param $emails List of emails whose owner must be selected
615 class UFC_Email
implements UserFilterCondition
618 public function __construct()
620 $this->emails
= pl_flatten(func_get_args());
623 public function buildCondition(PlFilter
&$uf)
630 if (count($this->emails
) == 0) {
631 return PlFilterCondition
::COND_TRUE
;
634 foreach ($this->emails
as $entry) {
635 if (User
::isForeignEmailAddress($entry)) {
637 } else if (User
::isVirtualEmailAddress($entry)) {
640 @list
($user, $domain) = explode('@', $entry);
645 if (count($foreign) > 0) {
646 $sub = $uf->addEmailRedirectFilter($foreign);
647 $cond[] = XDB
::format('e' . $sub . '.email IS NOT NULL OR a.email IN {?}', $foreign);
649 if (count($virtual) > 0) {
650 $sub = $uf->addVirtualEmailFilter($virtual);
651 $cond[] = 'vr' . $sub . '.redirect IS NOT NULL';
653 if (count($aliases) > 0) {
654 $sub = $uf->addAliasFilter($aliases);
655 $cond[] = 'al' . $sub . '.alias IS NOT NULL';
657 return '(' . implode(') OR (', $cond) . ')';
662 // {{{ class UFC_Address
663 abstract class UFC_Address
implements UserFilterCondition
665 /** Valid address type ('hq' is reserved for company addresses)
671 /** Text for these types
673 protected static $typetexts = array(
674 self
::TYPE_HOME
=> 'home',
675 self
::TYPE_PRO
=> 'pro',
680 /** Flags for addresses
682 const FLAG_CURRENT
= 0x0001;
683 const FLAG_TEMP
= 0x0002;
684 const FLAG_SECOND
= 0x0004;
685 const FLAG_MAIL
= 0x0008;
686 const FLAG_CEDEX
= 0x0010;
688 // Binary OR of those flags
689 const FLAG_ANY
= 0x001F;
691 /** Text of these flags
693 protected static $flagtexts = array(
694 self
::FLAG_CURRENT
=> 'current',
695 self
::FLAG_TEMP
=> 'temporary',
696 self
::FLAG_SECOND
=> 'secondary',
697 self
::FLAG_MAIL
=> 'mail',
698 self
::FLAG_CEDEX
=> 'cedex',
703 public function __construct($type = null
, $flags = null
)
706 $this->flags
= $flags;
709 protected function initConds($sub)
713 foreach (self
::$typetexts as $flag => $type) {
714 if ($flag & $this->type
) {
719 $conds[] = XDB
::format($sub . '.type IN {?}', $types);
722 if ($this->flags
!= self
::FLAG_ANY
) {
723 foreach(self
::$flagtexts as $flag => $text) {
724 if ($flag & $this->flags
) {
725 $conds[] = 'FIND_IN_SET(' . XDB
::format('{?}', $text) . ', ' . $sub . '.flags)';
735 // {{{ class UFC_AddressText
736 /** Select users based on their address, using full text search
737 * @param $text Text for filter in fulltext search
738 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
739 * @param $type Filter on address type
740 * @param $flags Filter on address flags
741 * @param $country Filter on address country
742 * @param $locality Filter on address locality
744 class UFC_AddressText
extends UFC_Address
748 private $textSearchMode;
750 public function __construct($text = null
, $textSearchMode = XDB
::WILDCARD_CONTAINS
,
751 $type = null
, $flags = self
::FLAG_ANY
, $country = null
, $locality = null
)
753 parent
::__construct($type, $flags);
755 $this->textSearchMode
= $textSearchMode;
756 $this->country
= $country;
757 $this->locality
= $locality;
760 private function mkMatch($txt)
762 return XDB
::formatWildcards($this->textSearchMode
, $txt);
765 public function buildCondition(PlFilter
&$uf)
767 $sub = $uf->addAddressFilter();
768 $conds = $this->initConds($sub);
769 if ($this->text
!= null
) {
770 $conds[] = $sub . '.text' . $this->mkMatch($this->text
);
773 if ($this->country
!= null
) {
774 $subc = $uf->addAddressCountryFilter();
776 $subconds[] = $subc . '.country' . $this->mkMatch($this->country
);
777 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country
);
778 $conds[] = implode(' OR ', $subconds);
781 if ($this->locality
!= null
) {
782 $subl = $uf->addAddressLocalityFilter();
783 $conds[] = $subl . '.name' . $this->mkMatch($this->locality
);
786 return implode(' AND ', $conds);
791 // {{{ class UFC_AddressField
792 /** Filters users based on their address,
793 * @param $val Either a code for one of the fields, or an array of such codes
794 * @param $fieldtype The type of field to look for
795 * @param $type Filter on address type
796 * @param $flags Filter on address flags
798 class UFC_AddressField
extends UFC_Address
800 const FIELD_COUNTRY
= 1;
801 const FIELD_ADMAREA
= 2;
802 const FIELD_SUBADMAREA
= 3;
803 const FIELD_LOCALITY
= 4;
804 const FIELD_ZIPCODE
= 5;
806 /** Data of the filter
811 public function __construct($val, $fieldtype, $type = null
, $flags = self
::FLAG_ANY
)
813 parent
::__construct($type, $flags);
815 if (!is_array($val)) {
819 $this->fieldtype
= $fieldtype;
822 public function buildCondition(PlFilter
&$uf)
824 $sub = $uf->addAddressFilter();
825 $conds = $this->initConds($sub);
827 switch ($this->fieldtype
) {
828 case self
::FIELD_COUNTRY
:
829 $field = 'countryId';
831 case self
::FIELD_ADMAREA
:
832 $field = 'administrativeAreaId';
834 case self
::FIELD_SUBADMAREA
:
835 $field = 'subAdministrativeAreaId';
837 case self
::FIELD_LOCALITY
:
838 $field = 'localityId';
840 case self
::FIELD_ZIPCODE
:
841 $field = 'postalCode';
844 Platal
::page()->killError('Invalid address field type: ' . $this->fieldtype
);
846 $conds[] = XDB
::format($sub . '.' . $field . ' IN {?}', $this->val
);
848 return implode(' AND ', $conds);
853 // {{{ class UFC_Corps
854 /** Filters users based on the corps they belong to
855 * @param $corps Corps we are looking for (abbreviation)
856 * @param $type Whether we search for original or current corps
858 class UFC_Corps
implements UserFilterCondition
866 public function __construct($corps, $type = self
::CURRENT
)
868 $this->corps
= $corps;
872 public function buildCondition(PlFilter
&$uf)
874 /** Tables shortcuts:
875 * pc for profile_corps,
876 * pceo for profile_corps_enum - orginal
877 * pcec for profile_corps_enum - current
879 $sub = $uf->addCorpsFilter($this->type
);
880 $cond = $sub . '.abbreviation = ' . $corps;
886 // {{{ class UFC_Corps_Rank
887 /** Filters users based on their rank in the corps
888 * @param $rank Rank we are looking for (abbreviation)
890 class UFC_Corps_Rank
implements UserFilterCondition
893 public function __construct($rank)
898 public function buildCondition(PlFilter
&$uf)
900 /** Tables shortcuts:
901 * pcr for profile_corps_rank
903 $sub = $uf->addCorpsRankFilter();
904 $cond = $sub . '.abbreviation = ' . $rank;
910 // {{{ class UFC_Job_Company
911 /** Filters users based on the company they belong to
912 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
913 * @param $value The searched value
915 class UFC_Job_Company
implements UserFilterCondition
918 const JOBNAME
= 'name';
919 const JOBACRONYM
= 'acronym';
924 public function __construct($type, $value)
926 $this->assertType($type);
928 $this->value
= $value;
931 private function assertType($type)
933 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
934 Platal
::page()->killError("Type de recherche non valide.");
938 public function buildCondition(PlFilter
&$uf)
940 $sub = $uf->addJobCompanyFilter();
941 $cond = $sub . '.' . $this->type
. ' = ' . XDB
::format('{?}', $this->value
);
947 // {{{ class UFC_Job_Sectorization
948 /** Filters users based on the ((sub)sub)sector they work in
949 * @param $val The ID of the sector, or an array of such IDs
950 * @param $type The kind of search (subsubsector/subsector/sector)
952 class UFC_Job_Sectorization
implements UserFilterCondition
957 public function __construct($val, $type = UserFilter
::JOB_SECTOR
)
959 self
::assertType($type);
960 if (!is_array($val)) {
967 private static function assertType($type)
969 if ($type != UserFilter
::JOB_SECTOR
&& $type != UserFilter
::JOB_SUBSECTOR
&& $type != UserFilter
::JOB_SUBSUBSECTOR
) {
970 Platal
::page()->killError("Type de secteur non valide.");
974 public function buildCondition(PlFilter
&$uf)
976 $sub = $uf->addJobSectorizationFilter($this->type
);
977 return $sub . '.id = ' . XDB
::format('{?}', $this->val
);
982 // {{{ class UFC_Job_Description
983 /** Filters users based on their job description
984 * @param $description The text being searched for
985 * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
987 class UFC_Job_Description
implements UserFilterCondition
990 private $description;
993 public function __construct($description, $fields)
995 $this->fields
= $fields;
996 $this->description
= $description;
999 public function buildCondition(PlFilter
&$uf)
1002 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
1003 $sub = $uf->addJobFilter();
1004 $conds[] = $sub . '.description ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1006 if ($this->fields
& UserFilter
::JOB_CV
) {
1007 $uf->requireProfiles();
1008 $conds[] = 'p.cv ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1010 if ($this->fields
& UserFilter
::JOB_SECTOR
) {
1011 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SECTOR
);
1012 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1014 if ($this->fields
& UserFilter
::JOB_SUBSECTOR
) {
1015 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSECTOR
);
1016 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1018 if ($this->fields
& UserFilter
::JOB_SUBSUBSECTOR
) {
1019 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSUBSECTOR
);
1020 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1021 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_ALTERNATES
);
1022 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1024 return implode(' OR ', $conds);
1029 // {{{ class UFC_Networking
1030 /** Filters users based on network identity (IRC, ...)
1031 * @param $type Type of network (-1 for any)
1032 * @param $value Value to search
1034 class UFC_Networking
implements UserFilterCondition
1039 public function __construct($type, $value)
1041 $this->type
= $type;
1042 $this->value
= $value;
1045 public function buildCondition(PlFilter
&$uf)
1047 $sub = $uf->addNetworkingFilter();
1049 $conds[] = $sub . '.address ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1050 if ($this->type
!= -1) {
1051 $conds[] = $sub . '.nwid = ' . XDB
::format('{?}', $this->type
);
1053 return implode(' AND ', $conds);
1058 // {{{ class UFC_Phone
1059 /** Filters users based on their phone number
1060 * @param $num_type Type of number (pro/user/home)
1061 * @param $phone_type Type of phone (fixed/mobile/fax)
1062 * @param $number Phone number
1064 class UFC_Phone
implements UserFilterCondition
1066 const NUM_PRO
= 'pro';
1067 const NUM_USER
= 'user';
1068 const NUM_HOME
= 'address';
1069 const NUM_ANY
= 'any';
1071 const PHONE_FIXED
= 'fixed';
1072 const PHONE_MOBILE
= 'mobile';
1073 const PHONE_FAX
= 'fax';
1074 const PHONE_ANY
= 'any';
1077 private $phone_type;
1080 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
1082 require_once('profil.func.inc.php');
1083 $this->number
= $number;
1084 $this->num_type
= $num_type;
1085 $this->phone_type
= format_phone_number($phone_type);
1088 public function buildCondition(PlFilter
&$uf)
1090 $sub = $uf->addPhoneFilter();
1092 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
1093 if ($this->num_type
!= self
::NUM_ANY
) {
1094 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
1096 if ($this->phone_type
!= self
::PHONE_ANY
) {
1097 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
1099 return implode(' AND ', $conds);
1104 // {{{ class UFC_Medal
1105 /** Filters users based on their medals
1106 * @param $medal ID of the medal
1107 * @param $grade Grade of the medal (null for 'any')
1109 class UFC_Medal
implements UserFilterCondition
1114 public function __construct($medal, $grade = null
)
1116 $this->medal
= $medal;
1117 $this->grade
= $grade;
1120 public function buildCondition(PlFilter
&$uf)
1123 $sub = $uf->addMedalFilter();
1124 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
1125 if ($this->grade
!= null
) {
1126 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
1128 return implode(' AND ', $conds);
1133 // {{{ class UFC_Photo
1134 /** Filters profiles with photo
1136 class UFC_Photo
implements UserFilterCondition
1138 public function buildCondition(PlFilter
&$uf)
1140 $uf->addPhotoFilter();
1141 return 'photo.attach IS NOT NULL';
1146 // {{{ class UFC_Mentor_Expertise
1147 /** Filters users by mentoring expertise
1148 * @param $expertise Domain of expertise
1150 class UFC_Mentor_Expertise
implements UserFilterCondition
1154 public function __construct($expertise)
1156 $this->expertise
= $expertise;
1159 public function buildCondition(PlFilter
&$uf)
1161 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_EXPERTISE
);
1162 return $sub . '.expertise ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->expertise
);
1167 // {{{ class UFC_Mentor_Country
1168 /** Filters users by mentoring country
1169 * @param $country Two-letters code of country being searched
1171 class UFC_Mentor_Country
implements UserFilterCondition
1175 public function __construct()
1177 $this->country
= pl_flatten(func_get_args());
1180 public function buildCondition(PlFilter
&$uf)
1182 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_COUNTRY
);
1183 return $sub . '.country IN ' . XDB
::format('{?}', $this->country
);
1188 // {{{ class UFC_Mentor_Sectorization
1189 /** Filters users based on mentoring (sub|)sector
1190 * @param $sector ID of (sub)sector
1191 * @param $type Whether we are looking for a sector or a subsector
1193 class UFC_Mentor_Sectorization
implements UserFilterCondition
1196 const SUBSECTOR
= 2;
1200 public function __construct($sector, $type = self
::SECTOR
)
1202 $this->sector
= $sector;
1203 $this->type
= $type;
1206 public function buildCondition(PlFilter
&$uf)
1208 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_SECTOR
);
1209 if ($this->type
== self
::SECTOR
) {
1210 $field = 'sectorid';
1212 $field = 'subsectorid';
1214 return $sub . '.' . $field . ' = ' . XDB
::format('{?}', $this->sector
);
1219 // {{{ class UFC_UserRelated
1220 /** Filters users based on a relation toward a user
1221 * @param $user User to which searched users are related
1223 abstract class UFC_UserRelated
implements UserFilterCondition
1226 public function __construct(PlUser
&$user)
1228 $this->user
=& $user;
1233 // {{{ class UFC_Contact
1234 /** Filters users who belong to selected user's contacts
1236 class UFC_Contact
extends UFC_UserRelated
1238 public function buildCondition(PlFilter
&$uf)
1240 $sub = $uf->addContactFilter($this->user
->id());
1241 return 'c' . $sub . '.contact IS NOT NULL';
1246 // {{{ class UFC_WatchRegistration
1247 /** Filters users being watched by selected user
1249 class UFC_WatchRegistration
extends UFC_UserRelated
1251 public function buildCondition(PlFilter
&$uf)
1253 if (!$this->user
->watchType('registration')) {
1254 return PlFilterCondition
::COND_FALSE
;
1256 $uids = $this->user
->watchUsers();
1257 if (count($uids) == 0) {
1258 return PlFilterCondition
::COND_FALSE
;
1260 return XDB
::format('$UID IN {?}', $uids);
1266 // {{{ class UFC_WatchPromo
1267 /** Filters users belonging to a promo watched by selected user
1268 * @param $user Selected user (the one watching promo)
1269 * @param $grade Formation the user is watching
1271 class UFC_WatchPromo
extends UFC_UserRelated
1274 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
1276 parent
::__construct($user);
1277 $this->grade
= $grade;
1280 public function buildCondition(PlFilter
&$uf)
1282 $promos = $this->user
->watchPromos();
1283 if (count($promos) == 0) {
1284 return PlFilterCondition
::COND_FALSE
;
1286 $sube = $uf->addEducationFilter(true
, $this->grade
);
1287 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
1288 return XDB
::format($field . ' IN {?}', $promos);
1294 // {{{ class UFC_WatchContact
1295 /** Filters users watched by selected user
1297 class UFC_WatchContact
extends UFC_Contact
1299 public function buildCondition(PlFilter
&$uf)
1301 if (!$this->user
->watchContacts()) {
1302 return PlFilterCondition
::COND_FALSE
;
1304 return parent
::buildCondition($uf);
1309 // {{{ class UFC_MarketingHash
1310 /** Filters users using the hash generated
1311 * to send marketing emails to him.
1313 class UFC_MarketingHash
implements UserFilterCondition
1317 public function __construct($hash)
1319 $this->hash
= $hash;
1322 public function buildCondition(PlFilter
&$uf)
1324 $table = $uf->addMarketingHash();
1325 return XDB
::format('rm.hash = {?}', $this->hash
);
1334 // {{{ class UserFilterOrder
1335 /** Base class for ordering results of a query.
1336 * Parameters for the ordering must be given to the constructor ($desc for a
1337 * descending order).
1338 * The getSortTokens function is used to get actual ordering part of the query.
1340 abstract class UserFilterOrder
extends PlFilterOrder
1342 /** This function must return the tokens to use for ordering
1343 * @param &$uf The UserFilter whose results must be ordered
1344 * @return The name of the field to use for ordering results
1346 // abstract protected function getSortTokens(UserFilter &$uf);
1350 // {{{ class UFO_Promo
1351 /** Orders users by promotion
1352 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
1353 * @param $desc Whether sort is descending
1355 class UFO_Promo
extends UserFilterOrder
1359 public function __construct($grade = null
, $desc = false
)
1361 parent
::__construct($desc);
1362 $this->grade
= $grade;
1365 protected function getSortTokens(PlFilter
&$uf)
1367 if (UserFilter
::isGrade($this->grade
)) {
1368 $sub = $uf->addEducationFilter($this->grade
);
1369 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
1371 $sub = $uf->addDisplayFilter();
1372 return 'pd' . $sub . '.promo';
1378 // {{{ class UFO_Name
1379 /** Sorts users by name
1380 * @param $type Type of name on which to sort (firstname...)
1381 * @param $variant Variant of that name to use (marital, ordinary...)
1382 * @param $particle Set to true if particles should be included in the sorting order
1383 * @param $desc If sort order should be descending
1385 class UFO_Name
extends UserFilterOrder
1391 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
1393 parent
::__construct($desc);
1394 $this->type
= $type;
1395 $this->variant
= $variant;
1396 $this->particle
= $particle;
1399 protected function getSortTokens(PlFilter
&$uf)
1401 if (Profile
::isDisplayName($this->type
)) {
1402 $sub = $uf->addDisplayFilter();
1403 return 'pd' . $sub . '.' . $this->type
;
1405 $sub = $uf->addNameFilter($this->type
, $this->variant
);
1406 if ($this->particle
) {
1407 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1409 return 'pn' . $sub . '.name';
1416 // {{{ class UFO_Score
1417 class UFO_Score
extends UserFilterOrder
1419 protected function getSortTokens(PlFilter
&$uf)
1421 $toks = $uf->getNameTokens();
1424 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
1425 if (count($toks) == 0) {
1429 foreach ($toks as $sub => $token) {
1430 $scores[] = XDB
::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
1432 return implode(' + ', $scores);
1437 // {{{ class UFO_Registration
1438 /** Sorts users based on registration date
1440 class UFO_Registration
extends UserFilterOrder
1442 protected function getSortTokens(PlFilter
&$uf)
1444 $uf->requireAccounts();
1445 return 'a.registration_date';
1450 // {{{ class UFO_Birthday
1451 /** Sorts users based on next birthday date
1453 class UFO_Birthday
extends UserFilterOrder
1455 protected function getSortTokens(PlFilter
&$uf)
1457 $uf->requireProfiles();
1458 return 'p.next_birthday';
1463 // {{{ class UFO_ProfileUpdate
1464 /** Sorts users based on last profile update
1466 class UFO_ProfileUpdate
extends UserFilterOrder
1468 protected function getSortTokens(PlFilter
&$uf)
1470 $uf->requireProfiles();
1471 return 'p.last_change';
1476 // {{{ class UFO_Death
1477 /** Sorts users based on death date
1479 class UFO_Death
extends UserFilterOrder
1481 protected function getSortTokens(PlFilter
&$uf)
1483 $uf->requireProfiles();
1484 return 'p.deathdate';
1489 // {{{ class UFO_Uid
1490 /** Sorts users based on their uid
1492 class UFO_Uid
extends UserFilterOrder
1494 protected function getSortTokens(PlFilter
&$uf)
1496 $uf->requireAccounts();
1502 // {{{ class UFO_Hruid
1503 /** Sorts users based on their hruid
1505 class UFO_Hruid
extends UserFilterOrder
1507 protected function getSortTokens(PlFilter
&$uf)
1509 $uf->requireAccounts();
1515 // {{{ class UFO_Pid
1516 /** Sorts users based on their pid
1518 class UFO_Pid
extends UserFilterOrder
1520 protected function getSortTokens(PlFilter
&$uf)
1522 $uf->requireProfiles();
1528 // {{{ class UFO_Hrpid
1529 /** Sorts users based on their hrpid
1531 class UFO_Hrpid
extends UserFilterOrder
1533 protected function getSortTokens(PlFilter
&$uf)
1535 $uf->requireProfiles();
1542 /***********************************
1543 *********************************
1545 *********************************
1546 ***********************************/
1548 // {{{ class UserFilter
1549 /** This class provides a convenient and centralized way of filtering users.
1552 * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t));
1554 * Resulting UserFilter can be used to:
1555 * - get a list of User objects matching the filter
1556 * - get a list of UIDs matching the filter
1557 * - get the number of users matching the filter
1558 * - check whether a given User matches the filter
1559 * - filter a list of User objects depending on whether they match the filter
1561 * Usage for UFC and UFO objects:
1562 * A UserFilter will call all private functions named XXXJoins.
1563 * These functions must return an array containing the list of join
1564 * required by the various UFC and UFO associated to the UserFilter.
1565 * Entries in those returned array are of the following form:
1566 * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter')
1567 * which will be translated into :
1568 * join_type JOIN joined_table AS join_tablealias ON (join_criter)
1569 * in the final query.
1571 * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with
1572 * profile.pid, and $UID with accounts.uid.
1574 * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined;
1575 * its parameter will be used to set various private vars of the UserFilter describing
1576 * the required joins ; such a function shall return the "join_tablealias" to use
1577 * when referring to the joined table.
1579 * For example, if data from profile_job must be available to filter results,
1580 * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and
1581 * return 'pj', the short name to use when referring to profile_job; when building
1582 * the query, calling the jobJoins function will return an array containing a single
1584 * 'pj' => array('left', 'profile_job', '$ME.pid = $UID');
1586 * The 'register_optional' function can be used to generate unique table aliases when
1587 * the same table has to be joined several times with different aliases.
1589 class UserFilter
extends PlFilter
1591 protected $joinMethods = array();
1593 protected $joinMetas = array(
1599 private $sort = array();
1600 private $query = null
;
1601 private $orderby = null
;
1603 private $lastusercount = null
;
1604 private $lastprofilecount = null
;
1606 public function __construct($cond = null
, $sort = null
)
1608 if (empty($this->joinMethods
)) {
1609 $class = new ReflectionClass('UserFilter');
1610 foreach ($class->getMethods() as $method) {
1611 $name = $method->getName();
1612 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
1613 $this->joinMethods
[] = $name;
1617 if (!is_null($cond)) {
1618 if ($cond instanceof PlFilterCondition
) {
1619 $this->setCondition($cond);
1622 if (!is_null($sort)) {
1623 if ($sort instanceof UserFilterOrder
) {
1624 $this->addSort($sort);
1625 } else if (is_array($sort)) {
1626 foreach ($sort as $s) {
1633 private function buildQuery()
1635 // The root condition is built first because some orders need info
1636 // available only once all UFC have set their conditions (UFO_Score)
1637 if (is_null($this->query
)) {
1638 $where = $this->root
->buildCondition($this);
1639 $where = str_replace(array_keys($this->joinMetas
),
1643 if (is_null($this->orderby
)) {
1645 foreach ($this->sort
as $sort) {
1646 $orders = array_merge($orders, $sort->buildSort($this));
1648 if (count($orders) == 0) {
1649 $this->orderby
= '';
1651 $this->orderby
= 'ORDER BY ' . implode(', ', $orders);
1653 $this->orderby
= str_replace(array_keys($this->joinMetas
),
1657 if (is_null($this->query
)) {
1658 if ($this->with_accounts
) {
1659 $from = 'accounts AS a';
1661 $this->requireProfiles();
1662 $from = 'profiles AS p';
1664 $joins = $this->buildJoins();
1665 $this->query
= 'FROM ' . $from . '
1667 WHERE (' . $where . ')';
1671 private function getUIDList($uids = null
, PlLimit
&$limit)
1673 $this->requireAccounts();
1674 $this->buildQuery();
1675 $lim = $limit->getSql();
1677 if (!empty($uids)) {
1678 $cond = XDB
::format(' AND a.uid IN {?}', $uids);
1680 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1681 ' . $this->query
. $cond . '
1683 ' . $this->orderby
. '
1685 $this->lastusercount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
1689 private function getPIDList($pids = null
, PlLimit
&$limit)
1691 $this->requireProfiles();
1692 $this->buildQuery();
1693 $lim = $limit->getSql();
1695 if (!is_null($pids)) {
1696 $cond = XDB
::format(' AND p.pid IN {?}', $pids);
1698 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid
1699 ' . $this->query
. $cond . '
1701 ' . $this->orderby
. '
1703 $this->lastprofilecount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
1707 private static function defaultLimit($limit) {
1708 if ($limit == null
) {
1709 return new PlLimit();
1715 /** Check that the user match the given rule.
1717 public function checkUser(PlUser
&$user)
1719 $this->requireAccounts();
1720 $this->buildQuery();
1721 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
1722 ' . $this->query
. XDB
::format(' AND a.uid = {?}', $user->id()));
1726 /** Check that the profile match the given rule.
1728 public function checkProfile(Profile
&$profile)
1730 $this->requireProfiles();
1731 $this->buildQuery();
1732 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
1733 ' . $this->query
. XDB
::format(' AND p.pid = {?}', $profile->id()));
1737 /** Default filter is on users
1739 public function filter(array $users, $limit = null
)
1741 return $this->filterUsers($users, self
::defaultLimit($limit));
1744 /** Filter a list of users to extract the users matching the rule.
1746 public function filterUsers(array $users, $limit = null
)
1748 $limit = self
::defaultLimit($limit);
1749 $this->requireAccounts();
1750 $this->buildQuery();
1753 foreach ($users as $user) {
1754 if ($user instanceof PlUser
) {
1760 $table[$uid] = $user;
1762 $fetched = $this->getUIDList($uids, $limit);
1764 foreach ($fetched as $uid) {
1765 $output[] = $table[$uid];
1770 /** Filter a list of profiles to extract the users matching the rule.
1772 public function filterProfiles(array $profiles, $limit = null
)
1774 $limit = self
::defaultLimit($limit);
1775 $this->requireProfiles();
1776 $this->buildQuery();
1779 foreach ($profiles as $profile) {
1780 if ($profile instanceof Profile
) {
1781 $pid = $profile->id();
1786 $table[$pid] = $profile;
1788 $fetched = $this->getPIDList($pids, $limit);
1790 foreach ($fetched as $pid) {
1791 $output[] = $table[$pid];
1796 public function getUIDs($limit = null
)
1798 $limit = self
::defaultLimit($limit);
1799 return $this->getUIDList(null
, $limit);
1802 public function getUID($pos = 0)
1804 $uids =$this->getUIDList(null
, new PlLimit(1, $pos));
1805 if (count($uids) == 0) {
1812 public function getPIDs($limit = null
)
1814 $limit = self
::defaultLimit($limit);
1815 return $this->getPIDList(null
, $limit);
1818 public function getPID($pos = 0)
1820 $pids =$this->getPIDList(null
, new PlLimit(1, $pos));
1821 if (count($pids) == 0) {
1828 public function getUsers($limit = null
)
1830 return User
::getBulkUsersWithUIDs($this->getUIDs($limit));
1833 public function getUser($pos = 0)
1835 $uid = $this->getUID($pos);
1839 return User
::getWithUID($uid);
1843 public function iterUsers($limit = null
)
1845 return User
::iterOverUIDs($this->getUIDs($limit));
1848 public function getProfiles($limit = null
, $fields = 0x0000, $visibility = null
)
1850 return Profile
::getBulkProfilesWithPIDs($this->getPIDs($limit), $fields, $visibility);
1853 public function getProfile($pos = 0, $fields = 0x0000, $visibility = null
)
1855 $pid = $this->getPID($pos);
1859 return Profile
::get($pid, $fields, $visibility);
1863 public function iterProfiles($limit = null
, $fields = 0x0000, $visibility = null
)
1865 return Profile
::iterOverPIDs($this->getPIDs($limit), true
, $fields, $visibility);
1868 public function get($limit = null
)
1870 return $this->getUsers($limit);
1874 public function getTotalCount()
1876 return $this->getTotalUserCount();
1879 public function getTotalUserCount()
1881 if (is_null($this->lastusercount
)) {
1882 $this->requireAccounts();
1883 $this->buildQuery();
1884 return (int)XDB
::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
1887 return $this->lastusercount
;
1891 public function getTotalProfileCount()
1893 if (is_null($this->lastprofilecount
)) {
1894 $this->requireProfiles();
1895 $this->buildQuery();
1896 return (int)XDB
::fetchOneCell('SELECT COUNT(DISTINCT p.pid)
1899 return $this->lastprofilecount
;
1903 public function setCondition(PlFilterCondition
&$cond)
1905 $this->root
=& $cond;
1906 $this->query
= null
;
1909 public function addSort(PlFilterOrder
&$sort)
1911 $this->sort
[] = $sort;
1912 $this->orderby
= null
;
1915 static public function getLegacy($promo_min, $promo_max)
1917 if ($promo_min != 0) {
1918 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
1920 $min = new PFC_True();
1922 if ($promo_max != 0) {
1923 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
1925 $max = new PFC_True();
1927 return new UserFilter(new PFC_And($min, $max));
1930 static public function sortByName()
1932 return array(new UFO_Name(Profile
::LASTNAME
), new UFO_Name(Profile
::FIRSTNAME
));
1935 static public function sortByPromo()
1937 return array(new UFO_Promo(), new UFO_Name(Profile
::LASTNAME
), new UFO_Name(Profile
::FIRSTNAME
));
1940 static private function getDBSuffix($string)
1942 if (is_array($string)) {
1943 if (count($string) == 1) {
1944 return self
::getDBSuffix(array_pop($string));
1946 return md5(implode('|', $string));
1948 return preg_replace('/[^a-z0-9]/i', '', $string);
1953 /** Stores a new (and unique) table alias in the &$table table
1954 * @param &$table Array in which the table alias must be stored
1955 * @param $val Value which will then be used to build the join
1956 * @return Name of the newly created alias
1958 private $option = 0;
1959 private function register_optional(array &$table, $val)
1961 if (is_null($val)) {
1962 $sub = $this->option++
;
1965 $sub = self
::getDBSuffix($val);
1969 $table[$sub] = $index;
1973 /** PROFILE VS ACCOUNT
1975 private $with_profiles = false
;
1976 private $with_accounts = false
;
1977 public function requireAccounts()
1979 $this->with_accounts
= true
;
1982 public function requireProfiles()
1984 $this->with_profiles
= true
;
1987 protected function accountJoins()
1990 if ($this->with_profiles
&& $this->with_accounts
) {
1991 $joins['ap'] = PlSqlJoin
::left('account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)');
1992 $joins['p'] = PlSqlJoin
::left('profiles', '$PID = ap.pid');
1999 const DISPLAY
= 'display';
2000 private $pd = false
;
2001 public function addDisplayFilter()
2003 $this->requireProfiles();
2008 protected function displayJoins()
2011 return array('pd' => PlSqlJoin
::left('profile_display', '$ME.pid = $PID'));
2020 private $with_logger = false
;
2021 public function addLoggerFilter()
2023 $this->with_logger
= true
;
2024 $this->requireAccounts();
2027 protected function loggerJoins()
2030 if ($this->with_logger
) {
2031 $joins['ls'] = PlSqlJoin
::left('log_sessions', '$ME.uid = $UID');
2039 static public function assertName($name)
2041 if (!DirEnum
::getID(DirEnum
::NAMETYPES
, $name)) {
2042 Platal
::page()->kill('Invalid name type: ' . $name);
2046 private $pn = array();
2047 public function addNameFilter($type, $variant = null
)
2049 $this->requireProfiles();
2050 if (!is_null($variant)) {
2051 $ft = $type . '_' . $variant;
2056 self
::assertName($ft);
2058 if (!is_null($variant) && $variant == 'other') {
2059 $sub .= $this->option++
;
2061 $this->pn
[$sub] = DirEnum
::getID(DirEnum
::NAMETYPES
, $ft);
2065 protected function nameJoins()
2068 foreach ($this->pn
as $sub => $type) {
2069 $joins['pn' . $sub] = PlSqlJoin
::left('profile_name', '$ME.pid = $PID AND $ME.typeid = {?}', $type);
2076 private $name_tokens = array();
2077 private $nb_tokens = 0;
2079 public function addNameTokensFilter($token)
2081 $this->requireProfiles();
2082 $sub = 'sn' . (1 +
$this->nb_tokens
);
2084 $this->name_tokens
[$sub] = $token;
2088 protected function nameTokensJoins()
2090 /* We don't return joins, since with_sn forces the SELECT to run on search_name first */
2092 foreach ($this->name_tokens
as $sub => $token) {
2093 $joins[$sub] = PlSqlJoin
::left('search_name', '$ME.pid = $PID');
2098 public function getNameTokens()
2100 return $this->name_tokens
;
2106 private $with_nat = false
;
2107 public function addNationalityFilter()
2109 $this->with_nat
= true
;
2113 protected function nationalityJoins()
2116 if ($this->with_nat
) {
2117 $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');
2124 const GRADE_ING
= 'Ing.';
2125 const GRADE_PHD
= 'PhD';
2126 const GRADE_MST
= 'M%';
2127 static public function isGrade($grade)
2129 return ($grade !== 0) && ($grade == self
::GRADE_ING ||
$grade == self
::GRADE_PHD ||
$grade == self
::GRADE_MST
);
2132 static public function assertGrade($grade)
2134 if (!self
::isGrade($grade)) {
2135 Platal
::page()->killError("Diplôme non valide: $grade");
2139 static public function promoYear($grade)
2141 // XXX: Definition of promotion for phds and masters might change in near future.
2142 return ($grade == UserFilter
::GRADE_ING
) ?
'entry_year' : 'grad_year';
2145 private $pepe = array();
2146 private $with_pee = false
;
2147 public function addEducationFilter($x = false
, $grade = null
)
2149 $this->requireProfiles();
2151 $index = $this->option
;
2152 $sub = $this->option++
;
2154 self
::assertGrade($grade);
2157 $this->with_pee
= true
;
2160 $this->pepe
[$index] = $sub;
2164 protected function educationJoins()
2167 if ($this->with_pee
) {
2168 $joins['pee'] = PlSqlJoin
::inner('profile_education_enum', 'pee.abbreviation = \'X\'');
2170 foreach ($this->pepe
as $grade => $sub) {
2171 if ($this->isGrade($grade)) {
2172 $joins['pe' . $sub] = PlSqlJoin
::left('profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID');
2173 $joins['pede' . $sub] = PlSqlJoin
::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE {?}', $grade);
2175 $joins['pe' . $sub] = PlSqlJoin
::left('profile_education', '$ME.pid = $PID');
2176 $joins['pee' . $sub] = PlSqlJoin
::inner('profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
2177 $joins['pede' . $sub] = PlSqlJoin
::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
2186 private $gpm = array();
2187 public function addGroupFilter($group = null
)
2189 $this->requireAccounts();
2190 if (!is_null($group)) {
2191 if (is_int($group) ||
ctype_digit($group)) {
2192 $index = $sub = $group;
2195 $sub = self
::getDBSuffix($group);
2198 $sub = 'group_' . $this->option++
;
2202 $this->gpm
[$sub] = $index;
2206 protected function groupJoins()
2209 foreach ($this->gpm
as $sub => $key) {
2210 if (is_null($key)) {
2211 $joins['gpa' . $sub] = PlSqlJoin
::inner('groups');
2212 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
2213 } else if (is_int($key) ||
ctype_digit($key)) {
2214 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
2216 $joins['gpa' . $sub] = PlSqlJoin
::inner('groups', '$ME.diminutif = {?}', $key);
2217 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
2226 private $with_bi = false
;
2227 private $with_bd = false
;
2228 public function addBinetsFilter($with_enum = false
)
2230 $this->requireProfiles();
2231 $this->with_bi
= true
;
2233 $this->with_bd
= true
;
2240 protected function binetsJoins()
2243 if ($this->with_bi
) {
2244 $joins['bi'] = PlSqlJoin
::left('profile_binets', '$ME.pid = $PID');
2246 if ($this->with_bd
) {
2247 $joins['bd'] = PlSqlJoin
::left('profile_binet_enum', '$ME.id = bi.binet_id');
2254 private $e = array();
2255 public function addEmailRedirectFilter($email = null
)
2257 $this->requireAccounts();
2258 return $this->register_optional($this->e
, $email);
2261 private $ve = array();
2262 public function addVirtualEmailFilter($email = null
)
2264 $this->addAliasFilter(self
::ALIAS_FORLIFE
);
2265 return $this->register_optional($this->ve
, $email);
2268 const ALIAS_BEST
= 'bestalias';
2269 const ALIAS_FORLIFE
= 'forlife';
2270 private $al = array();
2271 public function addAliasFilter($alias = null
)
2273 $this->requireAccounts();
2274 return $this->register_optional($this->al
, $alias);
2277 protected function emailJoins()
2281 foreach ($this->e
as $sub=>$key) {
2282 if (is_null($key)) {
2283 $joins['e' . $sub] = PlSqlJoin
::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
2285 if (!is_array($key)) {
2288 $joins['e' . $sub] = PlSqlJoin
::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'
2289 AND $ME.email IN {?}', $key);
2292 foreach ($this->al
as $sub=>$key) {
2293 if (is_null($key)) {
2294 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
2295 } else if ($key == self
::ALIAS_BEST
) {
2296 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
2297 } else if ($key == self
::ALIAS_FORLIFE
) {
2298 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type = \'a_vie\'');
2300 if (!is_array($key)) {
2303 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')
2304 AND $ME.alias IN {?}', $key);
2307 foreach ($this->ve
as $sub=>$key) {
2308 if (is_null($key)) {
2309 $joins['v' . $sub] = PlSqlJoin
::left('virtual', '$ME.type = \'user\'');
2311 if (!is_array($key)) {
2314 $joins['v' . $sub] = PlSqlJoin
::left('virtual', '$ME.type = \'user\' AND $ME.alias IN {?}', $key);
2316 $joins['vr' . $sub] = PlSqlJoin
::left('virtual_redirect',
2317 '$ME.vid = v' . $sub . '.vid
2318 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
2319 CONCAT(al_forlife.alias, \'@\', {?}),
2321 $globals->mail
->domain
, $globals->mail
->domain2
);
2329 private $with_pa = false
;
2330 public function addAddressFilter()
2332 $this->requireProfiles();
2333 $this->with_pa
= true
;
2337 private $with_pac = false
;
2338 public function addAddressCountryFilter()
2340 $this->requireProfiles();
2341 $this->addAddressFilter();
2342 $this->with_pac
= true
;
2346 private $with_pal = false
;
2347 public function addAddressLocalityFilter()
2349 $this->requireProfiles();
2350 $this->addAddressFilter();
2351 $this->with_pal
= true
;
2355 protected function addressJoins()
2358 if ($this->with_pa
) {
2359 $joins['pa'] = PlSqlJoin
::left('profile_addresses', '$ME.pid = $PID');
2361 if ($this->with_pac
) {
2362 $joins['gc'] = PlSqlJoin
::left('geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID');
2364 if ($this->with_pal
) {
2365 $joins['gl'] = PlSqlJoin
::left('geoloc_localities', '$ME.id = pa.localityID');
2374 private $pc = false
;
2375 private $pce = array();
2376 private $pcr = false
;
2377 public function addCorpsFilter($type)
2379 $this->requireProfiles();
2381 if ($type == UFC_Corps
::CURRENT
) {
2382 $pce['pcec'] = 'current_corpsid';
2384 } else if ($type == UFC_Corps
::ORIGIN
) {
2385 $pce['pceo'] = 'original_corpsid';
2390 public function addCorpsRankFilter()
2392 $this->requireProfiles();
2398 protected function corpsJoins()
2402 $joins['pc'] = PlSqlJoin
::left('profile_corps', '$ME.pid = $PID');
2405 $joins['pcr'] = PlSqlJoin
::left('profile_corps_rank_enum', '$ME.id = pc.rankid');
2407 foreach($this->pce
as $sub => $field) {
2408 $joins[$sub] = PlSqlJoin
::left('profile_corps_enum', '$ME.id = pc.' . $field);
2416 const JOB_SECTOR
= 0x0001;
2417 const JOB_SUBSECTOR
= 0x0002;
2418 const JOB_SUBSUBSECTOR
= 0x0004;
2419 const JOB_ALTERNATES
= 0x0008;
2420 const JOB_USERDEFINED
= 0x0010;
2421 const JOB_CV
= 0x0020;
2423 const JOB_SECTORIZATION
= 0x000F;
2424 const JOB_ANY
= 0x003F;
2428 * pje => profile_job_enum
2429 * pjse => profile_job_sector_enum
2430 * pjsse => profile_job_subsector_enum
2431 * pjssse => profile_job_subsubsector_enum
2432 * pja => profile_job_alternates
2434 private $with_pj = false
;
2435 private $with_pje = false
;
2436 private $with_pjse = false
;
2437 private $with_pjsse = false
;
2438 private $with_pjssse = false
;
2439 private $with_pja = false
;
2441 public function addJobFilter()
2443 $this->requireProfiles();
2444 $this->with_pj
= true
;
2448 public function addJobCompanyFilter()
2450 $this->addJobFilter();
2451 $this->with_pje
= true
;
2455 public function addJobSectorizationFilter($type)
2457 $this->addJobFilter();
2458 if ($type == self
::JOB_SECTOR
) {
2459 $this->with_pjse
= true
;
2461 } else if ($type == self
::JOB_SUBSECTOR
) {
2462 $this->with_pjsse
= true
;
2464 } else if ($type == self
::JOB_SUBSUBSECTOR
) {
2465 $this->with_pjssse
= true
;
2467 } else if ($type == self
::JOB_ALTERNATES
) {
2468 $this->with_pja
= true
;
2473 protected function jobJoins()
2476 if ($this->with_pj
) {
2477 $joins['pj'] = PlSqlJoin
::left('profile_job', '$ME.pid = $PID');
2479 if ($this->with_pje
) {
2480 $joins['pje'] = PlSqlJoin
::left('profile_job_enum', '$ME.id = pj.jobid');
2482 if ($this->with_pjse
) {
2483 $joins['pjse'] = PlSqlJoin
::left('profile_job_sector_enum', '$ME.id = pj.sectorid');
2485 if ($this->with_pjsse
) {
2486 $joins['pjsse'] = PlSqlJoin
::left('profile_job_subsector_enum', '$ME.id = pj.subsectorid');
2488 if ($this->with_pjssse
) {
2489 $joins['pjssse'] = PlSqlJoin
::left('profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
2491 if ($this->with_pja
) {
2492 $joins['pja'] = PlSqlJoin
::left('profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
2500 private $with_pnw = false
;
2501 public function addNetworkingFilter()
2503 $this->requireAccounts();
2504 $this->with_pnw
= true
;
2508 protected function networkingJoins()
2511 if ($this->with_pnw
) {
2512 $joins['pnw'] = PlSqlJoin
::left('profile_networking', '$ME.pid = $PID');
2520 private $with_ptel = false
;
2522 public function addPhoneFilter()
2524 $this->requireAccounts();
2525 $this->with_ptel
= true
;
2529 protected function phoneJoins()
2532 if ($this->with_ptel
) {
2533 $joins['ptel'] = PlSqlJoin
::left('profile_phones', '$ME.pid = $PID');
2541 private $with_pmed = false
;
2542 public function addMedalFilter()
2544 $this->requireProfiles();
2545 $this->with_pmed
= true
;
2549 protected function medalJoins()
2552 if ($this->with_pmed
) {
2553 $joins['pmed'] = PlSqlJoin
::left('profile_medals', '$ME.pid = $PID');
2561 private $pms = array();
2562 const MENTOR_EXPERTISE
= 1;
2563 const MENTOR_COUNTRY
= 2;
2564 const MENTOR_SECTOR
= 3;
2566 public function addMentorFilter($type)
2568 $this->requireAccounts();
2570 case self
::MENTOR_EXPERTISE
:
2571 $this->pms
['pme'] = 'profile_mentor';
2573 case self
::MENTOR_COUNTRY
:
2574 $this->pms
['pmc'] = 'profile_mentor_country';
2576 case self
::MENTOR_SECTOR
:
2577 $this->pms
['pms'] = 'profile_mentor_sector';
2580 Platal
::page()->killError("Undefined mentor filter.");
2584 protected function mentorJoins()
2587 foreach ($this->pms
as $sub => $tab) {
2588 $joins[$sub] = PlSqlJoin
::left($tab, '$ME.pid = $PID');
2595 private $cts = array();
2596 public function addContactFilter($uid = null
)
2598 $this->requireProfiles();
2599 return $this->register_optional($this->cts
, is_null($uid) ? null
: 'user_' . $uid);
2602 protected function contactJoins()
2605 foreach ($this->cts
as $sub=>$key) {
2606 if (is_null($key)) {
2607 $joins['c' . $sub] = PlSqlJoin
::left('contacts', '$ME.contact = $PID');
2609 $joins['c' . $sub] = PlSqlJoin
::left('contacts', '$ME.uid = {?} AND $ME.contact = $PID', substr($key, 5));
2618 private $wn = array();
2619 public function addWatchRegistrationFilter($uid = null
)
2621 $this->requireAccounts();
2622 return $this->register_optional($this->wn
, is_null($uid) ? null
: 'user_' . $uid);
2625 private $wp = array();
2626 public function addWatchPromoFilter($uid = null
)
2628 $this->requireAccounts();
2629 return $this->register_optional($this->wp
, is_null($uid) ? null
: 'user_' . $uid);
2632 private $w = array();
2633 public function addWatchFilter($uid = null
)
2635 $this->requireAccounts();
2636 return $this->register_optional($this->w
, is_null($uid) ? null
: 'user_' . $uid);
2639 protected function watchJoins()
2642 foreach ($this->w
as $sub=>$key) {
2643 if (is_null($key)) {
2644 $joins['w' . $sub] = PlSqlJoin
::left('watch');
2646 $joins['w' . $sub] = PlSqlJoin
::left('watch', '$ME.uid = {?}', substr($key, 5));
2649 foreach ($this->wn
as $sub=>$key) {
2650 if (is_null($key)) {
2651 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.ni_id = $UID');
2653 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
2656 foreach ($this->wn
as $sub=>$key) {
2657 if (is_null($key)) {
2658 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.ni_id = $UID');
2660 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
2663 foreach ($this->wp
as $sub=>$key) {
2664 if (is_null($key)) {
2665 $joins['wp' . $sub] = PlSqlJoin
::left('watch_promo');
2667 $joins['wp' . $sub] = PlSqlJoin
::left('watch_promo', '$ME.uid = {?}', substr($key, 5));
2676 private $with_photo;
2677 public function addPhotoFilter()
2679 $this->requireProfiles();
2680 $this->with_photo
= true
;
2683 protected function photoJoins()
2685 if ($this->with_photo
) {
2686 return array('photo' => PlSqlJoin
::left('profile_photos', '$ME.pid = $PID'));
2696 public function addMarketingHash()
2698 $this->requireAccounts();
2699 $this->with_rm
= true
;
2702 protected function marketingJoins()
2704 if ($this->with_rm
) {
2705 return array('rm' => PlSqlJoin
::left('register_marketing', '$ME.uid = $UID'));
2713 // {{{ class ProfileFilter
2714 class ProfileFilter
extends UserFilter
2716 public function get($limit = null
)
2718 return $this->getProfiles($limit);
2721 public function filter(array $profiles, $limit = null
)
2723 return $this->filterProfiles($profiles, self
::defaultLimit($limit));
2726 public function getTotalCount()
2728 return $this->getTotalProfileCount();
2733 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: