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_AccountType
56 /** Filters users who have the given account types
58 class UFC_AccountType
implements UserFilterCondition
62 public function __construct()
64 $this->types
= pl_flatten(func_get_args());
67 public function buildCondition(PlFilter
&$uf)
69 $uf->requireAccounts();
70 return XDB
::format('a.type IN {?}', $this->types
);
74 // {{{ class UFC_Hruid
75 /** Filters users based on their hruid
76 * @param $val Either an hruid, or a list of those
78 class UFC_Hruid
implements UserFilterCondition
82 public function __construct()
84 $this->hruids
= pl_flatten(func_get_args());
87 public function buildCondition(PlFilter
&$uf)
89 $uf->requireAccounts();
90 return XDB
::format('a.hruid IN {?}', $this->hruids
);
95 // {{{ class UFC_Hrpid
96 /** Filters users based on the hrpid of their profiles
97 * @param $val Either an hrpid, or a list of those
99 class UFC_Hrpid
implements UserFilterCondition
103 public function __construct()
105 $this->hrpids
= pl_flatten(func_get_args());
108 public function buildCondition(PlFilter
&$uf)
110 $uf->requireProfiles();
111 return XDB
::format('p.hrpid IN {?}', $this->hrpids
);
117 /** Filters users based on one of their last IPs
118 * @param $ip IP from which connection are checked
120 class UFC_Ip
implements UserFilterCondition
124 public function __construct($ip)
129 public function buildCondition(PlFilter
&$uf)
131 $sub = $uf->addLoggerFilter();
132 $ip = ip_to_uint($this->ip
);
133 return XDB
::format($sub . '.ip = {?} OR ' . $sub . '.forward_ip = {?}', $ip, $ip);
138 // {{{ class UFC_Comment
139 class UFC_Comment
implements UserFilterCondition
143 public function __construct($text)
148 public function buildCondition(PlFilter
&$uf)
150 $uf->requireProfiles();
151 return $uf->getVisibilityCondition('p.freetext_pub') . ' AND p.freetext ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->text
);
156 // {{{ class UFC_Promo
157 /** Filters users based on promotion
158 * @param $comparison Comparison operator (>, =, ...)
159 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
160 * @param $promo Promotion on which the filter is based
162 class UFC_Promo
implements UserFilterCondition
169 public function __construct($comparison, $grade, $promo)
171 $this->grade
= $grade;
172 $this->comparison
= $comparison;
173 $this->promo
= $promo;
174 if ($this->grade
!= UserFilter
::DISPLAY
) {
175 UserFilter
::assertGrade($this->grade
);
177 if ($this->grade
== UserFilter
::DISPLAY
&& $this->comparison
!= '=') {
178 // XXX: we might try to guess the grade from the first char of the promo and forbid only '<= 2004', but allow '<= X2004'
179 Platal
::page()->killError("Il n'est pas possible d'appliquer la comparaison '" . $this->comparison
. "' aux promotions sans spécifier de formation (X/M/D)");
183 public function buildCondition(PlFilter
&$uf)
185 if ($this->grade
== UserFilter
::DISPLAY
) {
186 $sub = $uf->addDisplayFilter();
187 return XDB
::format('pd' . $sub . '.promo ' . $this->comparison
. ' {?}', $this->promo
);
189 $sub = $uf->addEducationFilter(true
, $this->grade
);
190 $field = 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
191 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->promo
);
197 // {{{ class UFC_SchoolId
198 /** Filters users based on their shoold identifier
199 * @param type Parameter type (Xorg, AX, School)
200 * @param value School id value
202 class UFC_SchoolId
implements UserFilterCondition
206 const School
= 'school';
211 static public function assertType($type)
213 if ($type != self
::AX
&& $type != self
::Xorg
&& $type != self
::School
) {
214 Platal
::page()->killError("Type de matricule invalide: $type");
218 public function __construct($type, $id)
222 self
::assertType($type);
225 public function buildCondition(PlFilter
&$uf)
227 $uf->requireProfiles();
230 if ($type == self
::School
) {
232 $id = Profile
::getXorgId($id);
234 return XDB
::format('p.' . $type . '_id = {?}', $id);
239 // {{{ class UFC_EducationSchool
240 /** Filters users by formation
241 * @param $val The formation to search (either ID or array of IDs)
243 class UFC_EducationSchool
implements UserFilterCondition
247 public function __construct()
249 $this->val
= pl_flatten(func_get_args());
252 public function buildCondition(PlFilter
&$uf)
254 $sub = $uf->addEducationFilter();
255 return XDB
::format('pe' . $sub . '.eduid IN {?}', $this->val
);
260 // {{{ class UFC_EducationDegree
261 class UFC_EducationDegree
implements UserFilterCondition
265 public function __construct()
267 $this->diploma
= pl_flatten(func_get_args());
270 public function buildCondition(PlFilter
&$uf)
272 $sub = $uf->addEducationFilter();
273 return XDB
::format('pe' . $sub . '.degreeid IN {?}', $this->diploma
);
278 // {{{ class UFC_EducationField
279 class UFC_EducationField
implements UserFilterCondition
283 public function __construct()
285 $this->val
= pl_flatten(func_get_args());
288 public function buildCondition(PlFilter
&$uf)
290 $sub = $uf->addEducationFilter();
291 return XDB
::format('pe' . $sub . '.fieldid IN {?}', $this->val
);
296 // {{{ class UFC_Name
297 /** Filters users based on name
298 * @param $type Type of name field on which filtering is done (firstname, lastname...)
299 * @param $text Text on which to filter
300 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
302 class UFC_Name
implements UserFilterCondition
304 const EXACT
= XDB
::WILDCARD_EXACT
; // 0x000
305 const PREFIX
= XDB
::WILDCARD_PREFIX
; // 0x001
306 const SUFFIX
= XDB
::WILDCARD_SUFFIX
; // 0x002
307 const CONTAINS
= XDB
::WILDCARD_CONTAINS
; // 0x003
308 const PARTICLE
= 0x004;
309 const VARIANTS
= 0x008;
315 public function __construct($type, $text, $mode)
322 private function buildNameQuery($type, $variant, $where, UserFilter
&$uf)
324 $sub = $uf->addNameFilter($type, $variant);
325 return str_replace('$ME', 'pn' . $sub, $where);
328 public function buildCondition(PlFilter
&$uf)
331 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
332 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
334 $right = XDB
::formatWildcards($this->mode
& self
::CONTAINS
, $this->text
);
336 $cond = $left . $right;
337 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
338 if (($this->mode
& self
::VARIANTS
) != 0 && isset(Profile
::$name_variants[$this->type
])) {
339 foreach (Profile
::$name_variants[$this->type
] as $var) {
340 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
343 return implode(' OR ', $conds);
348 // {{{ class UFC_NameTokens
349 /** Selects users based on tokens in their name (for quicksearch)
350 * @param $tokens An array of tokens to search
351 * @param $flags Flags the tokens must have (e.g 'public' for public search)
352 * @param $soundex (bool) Whether those tokens are fulltext or soundex
354 class UFC_NameTokens
implements UserFilterCondition
357 const FLAG_PUBLIC
= 'public';
364 public function __construct($tokens, $flags = array(), $soundex = false
, $exact = false
)
366 if (is_array($tokens)) {
367 $this->tokens
= $tokens;
369 $this->tokens
= array($tokens);
371 if (is_array($flags)) {
372 $this->flags
= $flags;
374 $this->flags
= array($flags);
376 $this->soundex
= $soundex;
377 $this->exact
= $exact;
380 public function buildCondition(PlFilter
&$uf)
383 foreach ($this->tokens
as $i => $token) {
384 $sub = $uf->addNameTokensFilter($token);
385 if ($this->soundex
) {
386 $c = XDB
::format($sub . '.soundex = {?}', $token);
387 } else if ($this->exact
) {
388 $c = XDB
::format($sub . '.token = {?}', $token);
390 $c = $sub . '.token ' . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $token);
392 if ($this->flags
!= null
) {
393 $c .= XDB
::format(' AND ' . $sub . '.flags IN {?}', $this->flags
);
398 return implode(' AND ', $conds);
403 // {{{ class UFC_Nationality
404 class UFC_Nationality
implements UserFilterCondition
408 public function __construct()
410 $this->val
= pl_flatten(func_get_args());
413 public function buildCondition(PlFilter
&$uf)
415 $uf->requireProfiles();
416 $nat = XDB
::formatArray($this->val
);
418 'p.nationality1 IN ' . $nat,
419 'p.nationality2 IN ' . $nat,
420 'p.nationality3 IN ' . $nat,
422 return implode(' OR ', $conds);
427 // {{{ class UFC_Dead
428 /** Filters users based on death date
429 * @param $comparison Comparison operator
430 * @param $date Date to which death date should be compared (DateTime object, string or timestamp)
432 class UFC_Dead
implements UserFilterCondition
437 public function __construct($comparison = null
, $date = null
)
439 $this->comparison
= $comparison;
440 $this->date
= make_datetime($date);
443 public function buildCondition(PlFilter
&$uf)
445 $uf->requireProfiles();
446 $str = 'p.deathdate IS NOT NULL';
447 if (!is_null($this->comparison
)) {
448 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->date
->format('Y-m-d'));
455 // {{{ class UFC_Registered
456 /** Filters users based on registration state
457 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
458 * @param $comparison Comparison operator
459 * @param $date Date to which users registration date should be compared
461 class UFC_Registered
implements UserFilterCondition
467 public function __construct($active = false
, $comparison = null
, $date = null
)
469 $this->active
= $active;
470 $this->comparison
= $comparison;
471 $this->date
= make_datetime($date);
474 public function buildCondition(PlFilter
&$uf)
476 $uf->requireAccounts();
478 $date = '$UID IS NOT NULL AND a.state = \'active\'';
480 $date = '$UID IS NOT NULL AND a.state != \'pending\'';
482 if (!is_null($this->comparison
)) {
483 $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'));
490 // {{{ class UFC_ProfileUpdated
491 /** Filters users based on profile update date
492 * @param $comparison Comparison operator
493 * @param $date Date to which profile update date must be compared
495 class UFC_ProfileUpdated
implements UserFilterCondition
500 public function __construct($comparison = null
, $date = null
)
502 $this->comparison
= $comparison;
506 public function buildCondition(PlFilter
&$uf)
508 $uf->requireProfiles();
509 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
514 // {{{ class UFC_Birthday
515 /** Filters users based on next birthday date
516 * @param $comparison Comparison operator
517 * @param $date Date to which users next birthday date should be compared
519 class UFC_Birthday
implements UserFilterCondition
524 public function __construct($comparison = null
, $date = null
)
526 $this->comparison
= $comparison;
530 public function buildCondition(PlFilter
&$uf)
532 $uf->requireProfiles();
533 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
539 /** Filters users based on sex
540 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
542 class UFC_Sex
implements UserFilterCondition
545 public function __construct($sex)
550 public function buildCondition(PlFilter
&$uf)
552 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
553 return self
::COND_FALSE
;
555 $uf->requireProfiles();
556 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
562 // {{{ class UFC_Group
563 /** Filters users based on group membership
564 * @param $group Group whose members we are selecting
565 * @param $anim Whether to restrict selection to animators of that group
567 class UFC_Group
implements UserFilterCondition
571 public function __construct($group, $anim = false
)
573 $this->group
= $group;
577 public function buildCondition(PlFilter
&$uf)
579 // Groups have AX visibility.
580 if ($uf->getVisibilityLevel() == ProfileVisibility
::VIS_PUBLIC
) {
581 return PlFilter
::COND_TRUE
;
583 $sub = $uf->addGroupFilter($this->group
);
584 $where = 'gpm' . $sub . '.perms IS NOT NULL';
586 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
593 // {{{ class UFC_Binet
594 /** Selects users based on their belonging to a given (list of) binet
595 * @param $binet either a binet_id or an array of binet_ids
597 class UFC_Binet
implements UserFilterCondition
601 public function __construct()
603 $this->val
= pl_flatten(func_get_args());
606 public function buildCondition(PlFilter
&$uf)
608 // Binets are private.
609 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
610 return PlFilter
::COND_TRUE
;
612 $sub = $uf->addBinetsFilter();
613 return XDB
::format($sub . '.binet_id IN {?}', $this->val
);
618 // {{{ class UFC_Section
619 /** Selects users based on section
620 * @param $section ID of the section
622 class UFC_Section
implements UserFilterCondition
626 public function __construct()
628 $this->section
= pl_flatten(func_get_args());
631 public function buildCondition(PlFilter
&$uf)
633 // Sections are private.
634 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
635 return PlFilter
::COND_TRUE
;
637 $uf->requireProfiles();
638 return XDB
::format('p.section IN {?}', $this->section
);
643 // {{{ class UFC_Email
644 /** Filters users based on an email or a list of emails
645 * @param $emails List of emails whose owner must be selected
647 class UFC_Email
implements UserFilterCondition
650 public function __construct()
652 $this->emails
= pl_flatten(func_get_args());
655 public function buildCondition(PlFilter
&$uf)
662 if (count($this->emails
) == 0) {
663 return PlFilterCondition
::COND_TRUE
;
666 foreach ($this->emails
as $entry) {
667 if (User
::isForeignEmailAddress($entry)) {
669 } else if (User
::isVirtualEmailAddress($entry)) {
672 @list
($user, $domain) = explode('@', $entry);
677 if (count($foreign) > 0) {
678 $sub = $uf->addEmailRedirectFilter($foreign);
679 $cond[] = XDB
::format('e' . $sub . '.email IS NOT NULL OR a.email IN {?}', $foreign);
681 if (count($virtual) > 0) {
682 $sub = $uf->addVirtualEmailFilter($virtual);
683 $cond[] = 'vr' . $sub . '.redirect IS NOT NULL';
685 if (count($aliases) > 0) {
686 $sub = $uf->addAliasFilter($aliases);
687 $cond[] = 'al' . $sub . '.alias IS NOT NULL';
689 return '(' . implode(') OR (', $cond) . ')';
694 // {{{ class UFC_Address
695 abstract class UFC_Address
implements UserFilterCondition
697 /** Valid address type ('hq' is reserved for company addresses)
703 /** Text for these types
705 protected static $typetexts = array(
706 self
::TYPE_HOME
=> 'home',
707 self
::TYPE_PRO
=> 'pro',
712 /** Flags for addresses
714 const FLAG_CURRENT
= 0x0001;
715 const FLAG_TEMP
= 0x0002;
716 const FLAG_SECOND
= 0x0004;
717 const FLAG_MAIL
= 0x0008;
718 const FLAG_CEDEX
= 0x0010;
720 // Binary OR of those flags
721 const FLAG_ANY
= 0x001F;
723 /** Text of these flags
725 protected static $flagtexts = array(
726 self
::FLAG_CURRENT
=> 'current',
727 self
::FLAG_TEMP
=> 'temporary',
728 self
::FLAG_SECOND
=> 'secondary',
729 self
::FLAG_MAIL
=> 'mail',
730 self
::FLAG_CEDEX
=> 'cedex',
735 public function __construct($type = null
, $flags = null
)
738 $this->flags
= $flags;
741 protected function initConds($sub, $vis_cond)
743 $conds = array($vis_cond);
746 foreach (self
::$typetexts as $flag => $type) {
747 if ($flag & $this->type
) {
752 $conds[] = XDB
::format($sub . '.type IN {?}', $types);
755 if ($this->flags
!= self
::FLAG_ANY
) {
756 foreach(self
::$flagtexts as $flag => $text) {
757 if ($flag & $this->flags
) {
758 $conds[] = 'FIND_IN_SET(' . XDB
::format('{?}', $text) . ', ' . $sub . '.flags)';
768 // {{{ class UFC_AddressText
769 /** Select users based on their address, using full text search
770 * @param $text Text for filter in fulltext search
771 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
772 * @param $type Filter on address type
773 * @param $flags Filter on address flags
774 * @param $country Filter on address country
775 * @param $locality Filter on address locality
777 class UFC_AddressText
extends UFC_Address
781 private $textSearchMode;
783 public function __construct($text = null
, $textSearchMode = XDB
::WILDCARD_CONTAINS
,
784 $type = null
, $flags = self
::FLAG_ANY
, $country = null
, $locality = null
)
786 parent
::__construct($type, $flags);
788 $this->textSearchMode
= $textSearchMode;
789 $this->country
= $country;
790 $this->locality
= $locality;
793 private function mkMatch($txt)
795 return XDB
::formatWildcards($this->textSearchMode
, $txt);
798 public function buildCondition(PlFilter
&$uf)
800 $sub = $uf->addAddressFilter();
801 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
802 if ($this->text
!= null
) {
803 $conds[] = $sub . '.text' . $this->mkMatch($this->text
);
806 if ($this->country
!= null
) {
807 $subc = $uf->addAddressCountryFilter();
809 $subconds[] = $subc . '.country' . $this->mkMatch($this->country
);
810 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country
);
811 $conds[] = implode(' OR ', $subconds);
814 if ($this->locality
!= null
) {
815 $subl = $uf->addAddressLocalityFilter();
816 $conds[] = $subl . '.name' . $this->mkMatch($this->locality
);
819 return implode(' AND ', $conds);
824 // {{{ class UFC_AddressField
825 /** Filters users based on their address,
826 * @param $val Either a code for one of the fields, or an array of such codes
827 * @param $fieldtype The type of field to look for
828 * @param $type Filter on address type
829 * @param $flags Filter on address flags
831 class UFC_AddressField
extends UFC_Address
833 const FIELD_COUNTRY
= 1;
834 const FIELD_ADMAREA
= 2;
835 const FIELD_SUBADMAREA
= 3;
836 const FIELD_LOCALITY
= 4;
837 const FIELD_ZIPCODE
= 5;
839 /** Data of the filter
844 public function __construct($val, $fieldtype, $type = null
, $flags = self
::FLAG_ANY
)
846 parent
::__construct($type, $flags);
848 if (!is_array($val)) {
852 $this->fieldtype
= $fieldtype;
855 public function buildCondition(PlFilter
&$uf)
857 $sub = $uf->addAddressFilter();
858 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
860 switch ($this->fieldtype
) {
861 case self
::FIELD_COUNTRY
:
862 $field = 'countryId';
864 case self
::FIELD_ADMAREA
:
865 $field = 'administrativeAreaId';
867 case self
::FIELD_SUBADMAREA
:
868 $field = 'subAdministrativeAreaId';
870 case self
::FIELD_LOCALITY
:
871 $field = 'localityId';
873 case self
::FIELD_ZIPCODE
:
874 $field = 'postalCode';
877 Platal
::page()->killError('Invalid address field type: ' . $this->fieldtype
);
879 $conds[] = XDB
::format($sub . '.' . $field . ' IN {?}', $this->val
);
881 return implode(' AND ', $conds);
886 // {{{ class UFC_Corps
887 /** Filters users based on the corps they belong to
888 * @param $corps Corps we are looking for (abbreviation)
889 * @param $type Whether we search for original or current corps
891 class UFC_Corps
implements UserFilterCondition
899 public function __construct($corps, $type = self
::CURRENT
)
901 $this->corps
= $corps;
905 public function buildCondition(PlFilter
&$uf)
907 /** Tables shortcuts:
908 * pc for profile_corps,
909 * pceo for profile_corps_enum - orginal
910 * pcec for profile_corps_enum - current
912 $sub = $uf->addCorpsFilter($this->type
);
913 $cond = $sub . '.abbreviation = ' . $corps;
914 $cond .= ' AND ' . $uf->getVisibilityCondition($sub . '.corps_pub');
920 // {{{ class UFC_Corps_Rank
921 /** Filters users based on their rank in the corps
922 * @param $rank Rank we are looking for (abbreviation)
924 class UFC_Corps_Rank
implements UserFilterCondition
927 public function __construct($rank)
932 public function buildCondition(PlFilter
&$uf)
934 /** Tables shortcuts:
935 * pc for profile_corps
936 * pcr for profile_corps_rank
938 $sub = $uf->addCorpsRankFilter();
939 $cond = $sub . '.abbreviation = ' . $rank;
940 // XXX(x2006barrois): find a way to get rid of that hardcoded
941 // reference to 'pc'.
942 $cond .= ' AND ' . $uf->getVisibilityCondition('pc.corps_pub');
948 // {{{ class UFC_Job_Company
949 /** Filters users based on the company they belong to
950 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
951 * @param $value The searched value
953 class UFC_Job_Company
implements UserFilterCondition
956 const JOBNAME
= 'name';
957 const JOBACRONYM
= 'acronym';
962 public function __construct($type, $value)
964 $this->assertType($type);
966 $this->value
= $value;
969 private function assertType($type)
971 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
972 Platal
::page()->killError("Type de recherche non valide.");
976 public function buildCondition(PlFilter
&$uf)
978 $sub = $uf->addJobCompanyFilter();
979 $cond = $sub . '.' . $this->type
. XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
980 $jsub = $uf->addJobFilter();
981 $cond .= ' AND ' . $uf->getVisibilityCondition($jsub . '.pub');
987 // {{{ class UFC_Job_Sectorization
988 /** Filters users based on the ((sub)sub)sector they work in
989 * @param $val The ID of the sector, or an array of such IDs
990 * @param $type The kind of search (subsubsector/subsector/sector)
992 class UFC_Job_Sectorization
implements UserFilterCondition
997 public function __construct($val, $type = UserFilter
::JOB_SECTOR
)
999 self
::assertType($type);
1000 if (!is_array($val)) {
1004 $this->type
= $type;
1007 private static function assertType($type)
1009 if ($type != UserFilter
::JOB_SECTOR
&& $type != UserFilter
::JOB_SUBSECTOR
&& $type != UserFilter
::JOB_SUBSUBSECTOR
) {
1010 Platal
::page()->killError("Type de secteur non valide.");
1014 public function buildCondition(PlFilter
&$uf)
1016 $sub = $uf->addJobSectorizationFilter($this->type
);
1017 $cond = $sub . '.id = ' . XDB
::format('{?}', $this->val
);
1018 $jsub = $uf->addJobFilter();
1019 $cond .= ' AND ' . $uf->getVisibilityCondition($jsub . '.pub');
1025 // {{{ class UFC_Job_Terms
1026 /** Filters users based on the job terms they assigned to one of their
1028 * @param $val The ID of the job term, or an array of such IDs
1030 class UFC_Job_Terms
implements UserFilterCondition
1034 public function __construct($val)
1036 if (!is_array($val)) {
1042 public function buildCondition(PlFilter
&$uf)
1044 $sub = $uf->addJobTermsFilter(count($this->val
));
1045 $conditions = array();
1046 foreach ($this->val
as $i => $jtid) {
1047 $conditions[] = $sub[$i] . '.jtid_1 = ' . XDB
::escape($jtid);
1049 $jsub = $uf->addJobFilter();
1050 $conditions[] = $uf->getVisibilityCondition($jsub . '.pub');
1051 return implode(' AND ', $conditions);
1056 // {{{ class UFC_Job_Description
1057 /** Filters users based on their job description
1058 * @param $description The text being searched for
1059 * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
1061 class UFC_Job_Description
implements UserFilterCondition
1064 private $description;
1067 public function __construct($description, $fields)
1069 $this->fields
= $fields;
1070 $this->description
= $description;
1073 public function buildCondition(PlFilter
&$uf)
1077 $jsub = $uf->addJobFilter();
1078 // CV is private => if only CV requested, and not private,
1079 // don't do anything. Otherwise restrict to standard job visibility.
1080 if ($this->fields
== UserFilter
::JOB_CV
) {
1081 if ($uf->getVisibilityLevel() != ProfileVisibility
::VIS_PRIVATE
) {
1082 return PlFilter
::COND_TRUE
;
1085 $conds[] = $uf->getVisibilityCondition($jsub . '.pub');
1088 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
1089 $conds[] = $jsub . '.description ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1091 if ($this->fields
& UserFilter
::JOB_CV
&& $uf->getVisibilityLevel
== ProfileVisibility
::VIS_PRIVATE
) {
1092 $uf->requireProfiles();
1093 $conds[] = 'p.cv ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1095 if ($this->fields
& UserFilter
::JOB_SECTOR
) {
1096 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SECTOR
);
1097 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1099 if ($this->fields
& UserFilter
::JOB_SUBSECTOR
) {
1100 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSECTOR
);
1101 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1103 if ($this->fields
& UserFilter
::JOB_SUBSUBSECTOR
) {
1104 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSUBSECTOR
);
1105 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1106 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_ALTERNATES
);
1107 $conds[] = $sub . '.name ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->description
);
1109 return implode(' OR ', $conds);
1114 // {{{ class UFC_Networking
1115 /** Filters users based on network identity (IRC, ...)
1116 * @param $type Type of network (-1 for any)
1117 * @param $value Value to search
1119 class UFC_Networking
implements UserFilterCondition
1124 public function __construct($type, $value)
1126 $this->type
= $type;
1127 $this->value
= $value;
1130 public function buildCondition(PlFilter
&$uf)
1132 $sub = $uf->addNetworkingFilter();
1134 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1135 $conds[] = $sub . '.address ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->value
);
1136 if ($this->type
!= -1) {
1137 $conds[] = $sub . '.nwid = ' . XDB
::format('{?}', $this->type
);
1139 return implode(' AND ', $conds);
1144 // {{{ class UFC_Phone
1145 /** Filters users based on their phone number
1146 * @param $num_type Type of number (pro/user/home)
1147 * @param $phone_type Type of phone (fixed/mobile/fax)
1148 * @param $number Phone number
1150 class UFC_Phone
implements UserFilterCondition
1152 const NUM_PRO
= 'pro';
1153 const NUM_USER
= 'user';
1154 const NUM_HOME
= 'address';
1155 const NUM_ANY
= 'any';
1157 const PHONE_FIXED
= 'fixed';
1158 const PHONE_MOBILE
= 'mobile';
1159 const PHONE_FAX
= 'fax';
1160 const PHONE_ANY
= 'any';
1163 private $phone_type;
1166 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
1168 $phone = new Phone(array('display' => $number));
1170 $this->number
= $phone->search();
1171 $this->num_type
= $num_type;
1172 $this->phone_type
= $phone_type;
1175 public function buildCondition(PlFilter
&$uf)
1177 $sub = $uf->addPhoneFilter();
1180 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1182 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
1183 if ($this->num_type
!= self
::NUM_ANY
) {
1184 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
1186 if ($this->phone_type
!= self
::PHONE_ANY
) {
1187 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
1189 return implode(' AND ', $conds);
1194 // {{{ class UFC_Medal
1195 /** Filters users based on their medals
1196 * @param $medal ID of the medal
1197 * @param $grade Grade of the medal (null for 'any')
1199 class UFC_Medal
implements UserFilterCondition
1204 public function __construct($medal, $grade = null
)
1206 $this->medal
= $medal;
1207 $this->grade
= $grade;
1210 public function buildCondition(PlFilter
&$uf)
1214 // This will require profiles => table 'p' will be available.
1215 $sub = $uf->addMedalFilter();
1217 $conds[] = $uf->getVisibilityCondition('p.medals_pub');
1219 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
1220 if ($this->grade
!= null
) {
1221 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
1223 return implode(' AND ', $conds);
1228 // {{{ class UFC_Photo
1229 /** Filters profiles with photo
1231 class UFC_Photo
implements UserFilterCondition
1233 public function buildCondition(PlFilter
&$uf)
1235 $sub = $uf->addPhotoFilter();
1236 return $sub . '.attach IS NOT NULL AND ' . $uf->getVisibilityCondition($sub . '.pub');
1241 // {{{ class UFC_Mentor
1242 class UFC_Mentor
implements UserFilterCondition
1244 public function buildCondition(PlFilter
&$uf)
1246 $sub = $uf->addMentorFilter(UserFilter
::MENTOR
);
1247 return $sub . '.expertise IS NOT NULL';
1253 // {{{ class UFC_Mentor_Expertise
1254 /** Filters users by mentoring expertise
1255 * @param $expertise Domain of expertise
1257 class UFC_Mentor_Expertise
implements UserFilterCondition
1261 public function __construct($expertise)
1263 $this->expertise
= $expertise;
1266 public function buildCondition(PlFilter
&$uf)
1268 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_EXPERTISE
);
1269 return $sub . '.expertise ' . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $this->expertise
);
1274 // {{{ class UFC_Mentor_Country
1275 /** Filters users by mentoring country
1276 * @param $country Two-letters code of country being searched
1278 class UFC_Mentor_Country
implements UserFilterCondition
1282 public function __construct()
1284 $this->country
= pl_flatten(func_get_args());
1287 public function buildCondition(PlFilter
&$uf)
1289 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_COUNTRY
);
1290 return $sub . '.country IN ' . XDB
::format('{?}', $this->country
);
1295 // {{{ class UFC_Mentor_Terms
1296 /** Filters users based on the job terms they used in mentoring.
1297 * @param $val The ID of the job term, or an array of such IDs
1299 class UFC_Mentor_Terms
implements UserFilterCondition
1303 public function __construct($val)
1308 public function buildCondition(PlFilter
&$uf)
1310 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_TERM
);
1311 return $sub . '.jtid_1 = ' . XDB
::escape($this->val
);
1316 // {{{ class UFC_Mentor_Sectorization
1317 /** Filters users based on mentoring (sub|)sector
1318 * @param $sector ID of (sub)sector
1319 * @param $type Whether we are looking for a sector or a subsector
1321 class UFC_Mentor_Sectorization
implements UserFilterCondition
1324 const SUBSECTOR
= 2;
1328 public function __construct($sector, $type = self
::SECTOR
)
1330 $this->sector
= $sector;
1331 $this->type
= $type;
1334 public function buildCondition(PlFilter
&$uf)
1336 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_SECTOR
);
1337 if ($this->type
== self
::SECTOR
) {
1338 $field = 'sectorid';
1340 $field = 'subsectorid';
1342 return $sub . '.' . $field . ' = ' . XDB
::format('{?}', $this->sector
);
1347 // {{{ class UFC_UserRelated
1348 /** Filters users based on a relation toward a user
1349 * @param $user User to which searched users are related
1351 abstract class UFC_UserRelated
implements UserFilterCondition
1354 public function __construct(PlUser
&$user)
1356 $this->user
=& $user;
1361 // {{{ class UFC_Contact
1362 /** Filters users who belong to selected user's contacts
1364 class UFC_Contact
extends UFC_UserRelated
1366 public function buildCondition(PlFilter
&$uf)
1368 $sub = $uf->addContactFilter($this->user
->id());
1369 return 'c' . $sub . '.contact IS NOT NULL';
1374 // {{{ class UFC_WatchRegistration
1375 /** Filters users being watched by selected user
1377 class UFC_WatchRegistration
extends UFC_UserRelated
1379 public function buildCondition(PlFilter
&$uf)
1381 if (!$this->user
->watchType('registration')) {
1382 return PlFilterCondition
::COND_FALSE
;
1384 $uids = $this->user
->watchUsers();
1385 if (count($uids) == 0) {
1386 return PlFilterCondition
::COND_FALSE
;
1388 return XDB
::format('$UID IN {?}', $uids);
1394 // {{{ class UFC_WatchPromo
1395 /** Filters users belonging to a promo watched by selected user
1396 * @param $user Selected user (the one watching promo)
1397 * @param $grade Formation the user is watching
1399 class UFC_WatchPromo
extends UFC_UserRelated
1402 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
1404 parent
::__construct($user);
1405 $this->grade
= $grade;
1408 public function buildCondition(PlFilter
&$uf)
1410 $promos = $this->user
->watchPromos();
1411 if (count($promos) == 0) {
1412 return PlFilterCondition
::COND_FALSE
;
1414 $sube = $uf->addEducationFilter(true
, $this->grade
);
1415 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
1416 return XDB
::format($field . ' IN {?}', $promos);
1422 // {{{ class UFC_WatchContact
1423 /** Filters users watched by selected user
1425 class UFC_WatchContact
extends UFC_Contact
1427 public function buildCondition(PlFilter
&$uf)
1429 if (!$this->user
->watchContacts()) {
1430 return PlFilterCondition
::COND_FALSE
;
1432 return parent
::buildCondition($uf);
1437 // {{{ class UFC_MarketingHash
1438 /** Filters users using the hash generated
1439 * to send marketing emails to him.
1441 class UFC_MarketingHash
implements UserFilterCondition
1445 public function __construct($hash)
1447 $this->hash
= $hash;
1450 public function buildCondition(PlFilter
&$uf)
1452 $table = $uf->addMarketingHash();
1453 return XDB
::format('rm.hash = {?}', $this->hash
);
1462 // {{{ class UFO_Promo
1463 /** Orders users by promotion
1464 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
1465 * @param $desc Whether sort is descending
1467 class UFO_Promo
extends PlFilterGroupableOrder
1471 public function __construct($grade = null
, $desc = false
)
1473 parent
::__construct($desc);
1474 $this->grade
= $grade;
1477 protected function getSortTokens(PlFilter
&$uf)
1479 if (UserFilter
::isGrade($this->grade
)) {
1480 $sub = $uf->addEducationFilter($this->grade
);
1481 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
1483 $sub = $uf->addDisplayFilter();
1484 return 'pd' . $sub . '.promo';
1490 // {{{ class UFO_Name
1491 /** Sorts users by name
1492 * @param $type Type of name on which to sort (firstname...)
1493 * @param $variant Variant of that name to use (marital, ordinary...)
1494 * @param $particle Set to true if particles should be included in the sorting order
1495 * @param $desc If sort order should be descending
1497 class UFO_Name
extends PlFilterOrder
1503 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
1505 parent
::__construct($desc);
1506 $this->type
= $type;
1507 $this->variant
= $variant;
1508 $this->particle
= $particle;
1511 protected function getSortTokens(PlFilter
&$uf)
1513 if (Profile
::isDisplayName($this->type
)) {
1514 $sub = $uf->addDisplayFilter();
1515 $token = 'pd' . $sub . '.' . $this->type
;
1516 if ($uf->accountsRequired()) {
1517 $account_token = Profile
::getAccountEquivalentName($this->type
);
1518 return 'IFNULL(' . $token . ', a.' . $account_token . ')';
1523 $sub = $uf->addNameFilter($this->type
, $this->variant
);
1524 if ($this->particle
) {
1525 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1527 return 'pn' . $sub . '.name';
1534 // {{{ class UFO_Score
1535 class UFO_Score
extends PlFilterOrder
1537 protected function getSortTokens(PlFilter
&$uf)
1539 $toks = $uf->getNameTokens();
1542 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
1543 if (count($toks) == 0) {
1547 foreach ($toks as $sub => $token) {
1548 $scores[] = XDB
::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
1550 return implode(' + ', $scores);
1555 // {{{ class UFO_Registration
1556 /** Sorts users based on registration date
1558 class UFO_Registration
extends PlFilterOrder
1560 protected function getSortTokens(PlFilter
&$uf)
1562 $uf->requireAccounts();
1563 return 'a.registration_date';
1568 // {{{ class UFO_Birthday
1569 /** Sorts users based on next birthday date
1571 class UFO_Birthday
extends PlFilterOrder
1573 protected function getSortTokens(PlFilter
&$uf)
1575 $uf->requireProfiles();
1576 return 'p.next_birthday';
1581 // {{{ class UFO_ProfileUpdate
1582 /** Sorts users based on last profile update
1584 class UFO_ProfileUpdate
extends PlFilterOrder
1586 protected function getSortTokens(PlFilter
&$uf)
1588 $uf->requireProfiles();
1589 return 'p.last_change';
1594 // {{{ class UFO_Death
1595 /** Sorts users based on death date
1597 class UFO_Death
extends PlFilterOrder
1599 protected function getSortTokens(PlFilter
&$uf)
1601 $uf->requireProfiles();
1602 return 'p.deathdate';
1607 // {{{ class UFO_Uid
1608 /** Sorts users based on their uid
1610 class UFO_Uid
extends PlFilterOrder
1612 protected function getSortTokens(PlFilter
&$uf)
1614 $uf->requireAccounts();
1620 // {{{ class UFO_Hruid
1621 /** Sorts users based on their hruid
1623 class UFO_Hruid
extends PlFilterOrder
1625 protected function getSortTokens(PlFilter
&$uf)
1627 $uf->requireAccounts();
1633 // {{{ class UFO_Pid
1634 /** Sorts users based on their pid
1636 class UFO_Pid
extends PlFilterOrder
1638 protected function getSortTokens(PlFilter
&$uf)
1640 $uf->requireProfiles();
1646 // {{{ class UFO_Hrpid
1647 /** Sorts users based on their hrpid
1649 class UFO_Hrpid
extends PlFilterOrder
1651 protected function getSortTokens(PlFilter
&$uf)
1653 $uf->requireProfiles();
1660 /***********************************
1661 *********************************
1663 *********************************
1664 ***********************************/
1666 // {{{ class UserFilter
1667 /** This class provides a convenient and centralized way of filtering users.
1670 * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t));
1672 * Resulting UserFilter can be used to:
1673 * - get a list of User objects matching the filter
1674 * - get a list of UIDs matching the filter
1675 * - get the number of users matching the filter
1676 * - check whether a given User matches the filter
1677 * - filter a list of User objects depending on whether they match the filter
1679 * Usage for UFC and UFO objects:
1680 * A UserFilter will call all private functions named XXXJoins.
1681 * These functions must return an array containing the list of join
1682 * required by the various UFC and UFO associated to the UserFilter.
1683 * Entries in those returned array are of the following form:
1684 * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter')
1685 * which will be translated into :
1686 * join_type JOIN joined_table AS join_tablealias ON (join_criter)
1687 * in the final query.
1689 * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with
1690 * profile.pid, and $UID with accounts.uid.
1692 * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined;
1693 * its parameter will be used to set various private vars of the UserFilter describing
1694 * the required joins ; such a function shall return the "join_tablealias" to use
1695 * when referring to the joined table.
1697 * For example, if data from profile_job must be available to filter results,
1698 * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and
1699 * return 'pj', the short name to use when referring to profile_job; when building
1700 * the query, calling the jobJoins function will return an array containing a single
1702 * 'pj' => array('left', 'profile_job', '$ME.pid = $UID');
1704 * The 'register_optional' function can be used to generate unique table aliases when
1705 * the same table has to be joined several times with different aliases.
1707 class UserFilter
extends PlFilter
1709 protected $joinMethods = array();
1711 protected $joinMetas = array(
1717 private $sort = array();
1718 private $grouper = null
;
1719 private $query = null
;
1720 private $orderby = null
;
1722 // Store the current 'search' visibility.
1723 private $profile_visibility = null
;
1725 private $lastusercount = null
;
1726 private $lastprofilecount = null
;
1728 public function __construct($cond = null
, $sort = null
)
1730 if (empty($this->joinMethods
)) {
1731 $class = new ReflectionClass('UserFilter');
1732 foreach ($class->getMethods() as $method) {
1733 $name = $method->getName();
1734 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
1735 $this->joinMethods
[] = $name;
1739 if (!is_null($cond)) {
1740 if ($cond instanceof PlFilterCondition
) {
1741 $this->setCondition($cond);
1744 if (!is_null($sort)) {
1745 if ($sort instanceof PlFilterOrder
) {
1746 $this->addSort($sort);
1747 } else if (is_array($sort)) {
1748 foreach ($sort as $s) {
1754 // This will set the visibility to the default correct level.
1755 $this->profile_visibility
= new ProfileVisibility();
1758 public function getVisibilityLevels()
1760 return $this->profile_visibility
->levels();
1763 public function getVisibilityLevel()
1765 return $this->profile_visibility
->level();
1768 public function restrictVisibilityTo($level)
1770 $this->profile_visibility
->setLevel($level);
1773 public function getVisibilityCondition($field)
1775 return $field . ' IN ' . XDB
::formatArray($this->getVisibilityLevels());
1778 private function buildQuery()
1780 // The root condition is built first because some orders need info
1781 // available only once all UFC have set their conditions (UFO_Score)
1782 if (is_null($this->query
)) {
1783 $where = $this->root
->buildCondition($this);
1784 $where = str_replace(array_keys($this->joinMetas
),
1788 if (is_null($this->orderby
)) {
1790 foreach ($this->sort
as $sort) {
1791 $orders = array_merge($orders, $sort->buildSort($this));
1793 if (count($orders) == 0) {
1794 $this->orderby
= '';
1796 $this->orderby
= 'ORDER BY ' . implode(', ', $orders);
1798 $this->orderby
= str_replace(array_keys($this->joinMetas
),
1802 if (is_null($this->query
)) {
1803 if ($this->with_accounts
) {
1804 $from = 'accounts AS a';
1806 $this->requireProfiles();
1807 $from = 'profiles AS p';
1809 $joins = $this->buildJoins();
1810 $this->query
= 'FROM ' . $from . '
1812 WHERE (' . $where . ')';
1816 public function hasGroups()
1818 return $this->grouper
!= null
;
1821 public function getGroups()
1823 return $this->getUIDGroups();
1826 public function getUIDGroups()
1828 $this->requireAccounts();
1829 $this->buildQuery();
1830 $token = $this->grouper
->getGroupToken($this);
1832 $groups = XDB
::fetchAllRow('SELECT ' . $token . ', COUNT(a.uid)
1833 ' . $this->query
. '
1834 GROUP BY ' . $token,
1839 public function getPIDGroups()
1841 $this->requireProfiles();
1842 $this->buildQuery();
1843 $token = $this->grouper
->getGroupToken($this);
1845 $groups = XDB
::fetchAllRow('SELECT ' . $token . ', COUNT(p.pid)
1846 ' . $this->query
. '
1847 GROUP BY ' . $token,
1852 private function getUIDList($uids = null
, PlLimit
&$limit)
1854 $this->requireAccounts();
1855 $this->buildQuery();
1856 $lim = $limit->getSql();
1858 if (!empty($uids)) {
1859 $cond = XDB
::format(' AND a.uid IN {?}', $uids);
1861 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1862 ' . $this->query
. $cond . '
1864 ' . $this->orderby
. '
1866 $this->lastusercount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
1870 private function getPIDList($pids = null
, PlLimit
&$limit)
1872 $this->requireProfiles();
1873 $this->buildQuery();
1874 $lim = $limit->getSql();
1876 if (!is_null($pids)) {
1877 $cond = XDB
::format(' AND p.pid IN {?}', $pids);
1879 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid
1880 ' . $this->query
. $cond . '
1882 ' . $this->orderby
. '
1884 $this->lastprofilecount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
1888 private static function defaultLimit($limit) {
1889 if ($limit == null
) {
1890 return new PlLimit();
1896 /** Check that the user match the given rule.
1898 public function checkUser(PlUser
&$user)
1900 $this->requireAccounts();
1901 $this->buildQuery();
1902 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
1903 ' . $this->query
. XDB
::format(' AND a.uid = {?}', $user->id()));
1907 /** Check that the profile match the given rule.
1909 public function checkProfile(Profile
&$profile)
1911 $this->requireProfiles();
1912 $this->buildQuery();
1913 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
1914 ' . $this->query
. XDB
::format(' AND p.pid = {?}', $profile->id()));
1918 /** Default filter is on users
1920 public function filter(array $users, $limit = null
)
1922 return $this->filterUsers($users, self
::defaultLimit($limit));
1925 /** Filter a list of users to extract the users matching the rule.
1927 public function filterUsers(array $users, $limit = null
)
1929 $limit = self
::defaultLimit($limit);
1930 $this->requireAccounts();
1931 $this->buildQuery();
1934 foreach ($users as $user) {
1935 if ($user instanceof PlUser
) {
1941 $table[$uid] = $user;
1943 $fetched = $this->getUIDList($uids, $limit);
1945 foreach ($fetched as $uid) {
1946 $output[] = $table[$uid];
1951 /** Filter a list of profiles to extract the users matching the rule.
1953 public function filterProfiles(array $profiles, $limit = null
)
1955 $limit = self
::defaultLimit($limit);
1956 $this->requireProfiles();
1957 $this->buildQuery();
1960 foreach ($profiles as $profile) {
1961 if ($profile instanceof Profile
) {
1962 $pid = $profile->id();
1967 $table[$pid] = $profile;
1969 $fetched = $this->getPIDList($pids, $limit);
1971 foreach ($fetched as $pid) {
1972 $output[] = $table[$pid];
1977 public function getUIDs($limit = null
)
1979 $limit = self
::defaultLimit($limit);
1980 return $this->getUIDList(null
, $limit);
1983 public function getUID($pos = 0)
1985 $uids =$this->getUIDList(null
, new PlLimit(1, $pos));
1986 if (count($uids) == 0) {
1993 public function getPIDs($limit = null
)
1995 $limit = self
::defaultLimit($limit);
1996 return $this->getPIDList(null
, $limit);
1999 public function getPID($pos = 0)
2001 $pids =$this->getPIDList(null
, new PlLimit(1, $pos));
2002 if (count($pids) == 0) {
2009 public function getUsers($limit = null
)
2011 return User
::getBulkUsersWithUIDs($this->getUIDs($limit));
2014 public function getUser($pos = 0)
2016 $uid = $this->getUID($pos);
2020 return User
::getWithUID($uid);
2024 public function iterUsers($limit = null
)
2026 return User
::iterOverUIDs($this->getUIDs($limit));
2029 public function getProfiles($limit = null
, $fields = 0x0000, $visibility = null
)
2031 return Profile
::getBulkProfilesWithPIDs($this->getPIDs($limit), $fields, $visibility);
2034 public function getProfile($pos = 0, $fields = 0x0000, $visibility = null
)
2036 $pid = $this->getPID($pos);
2040 return Profile
::get($pid, $fields, $visibility);
2044 public function iterProfiles($limit = null
, $fields = 0x0000, $visibility = null
)
2046 return Profile
::iterOverPIDs($this->getPIDs($limit), true
, $fields, $visibility);
2049 public function get($limit = null
)
2051 return $this->getUsers($limit);
2055 public function getTotalCount()
2057 return $this->getTotalUserCount();
2060 public function getTotalUserCount()
2062 if (is_null($this->lastusercount
)) {
2063 $this->requireAccounts();
2064 $this->buildQuery();
2065 return (int)XDB
::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
2068 return $this->lastusercount
;
2072 public function getTotalProfileCount()
2074 if (is_null($this->lastprofilecount
)) {
2075 $this->requireProfiles();
2076 $this->buildQuery();
2077 return (int)XDB
::fetchOneCell('SELECT COUNT(DISTINCT p.pid)
2080 return $this->lastprofilecount
;
2084 public function setCondition(PlFilterCondition
&$cond)
2086 $this->root
=& $cond;
2087 $this->query
= null
;
2090 public function addSort(PlFilterOrder
&$sort)
2092 if (count($this->sort
) == 0 && $sort instanceof PlFilterGroupableOrder
)
2094 $this->grouper
= $sort;
2096 $this->sort
[] = $sort;
2097 $this->orderby
= null
;
2100 static public function getLegacy($promo_min, $promo_max)
2102 if ($promo_min != 0) {
2103 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
2105 $min = new PFC_True();
2107 if ($promo_max != 0) {
2108 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
2110 $max = new PFC_True();
2112 return new UserFilter(new PFC_And($min, $max));
2115 static public function sortByName()
2117 return array(new UFO_Name(Profile
::LASTNAME
), new UFO_Name(Profile
::FIRSTNAME
));
2120 static public function sortByPromo()
2122 return array(new UFO_Promo(), new UFO_Name(Profile
::LASTNAME
), new UFO_Name(Profile
::FIRSTNAME
));
2125 static private function getDBSuffix($string)
2127 if (is_array($string)) {
2128 if (count($string) == 1) {
2129 return self
::getDBSuffix(array_pop($string));
2131 return md5(implode('|', $string));
2133 return preg_replace('/[^a-z0-9]/i', '', $string);
2138 /** Stores a new (and unique) table alias in the &$table table
2139 * @param &$table Array in which the table alias must be stored
2140 * @param $val Value which will then be used to build the join
2141 * @return Name of the newly created alias
2143 private $option = 0;
2144 private function register_optional(array &$table, $val)
2146 if (is_null($val)) {
2147 $sub = $this->option++
;
2150 $sub = self
::getDBSuffix($val);
2154 $table[$sub] = $index;
2158 /** PROFILE VS ACCOUNT
2160 private $with_profiles = false
;
2161 private $with_accounts = false
;
2162 public function requireAccounts()
2164 $this->with_accounts
= true
;
2167 public function accountsRequired()
2169 return $this->with_accounts
;
2172 public function requireProfiles()
2174 $this->with_profiles
= true
;
2177 public function profilesRequired()
2179 return $this->with_profiles
;
2182 protected function accountJoins()
2185 if ($this->with_profiles
&& $this->with_accounts
) {
2186 $joins['ap'] = PlSqlJoin
::left('account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)');
2187 $joins['p'] = PlSqlJoin
::left('profiles', '$PID = ap.pid');
2194 const DISPLAY
= 'display';
2195 private $pd = false
;
2196 public function addDisplayFilter()
2198 $this->requireProfiles();
2203 protected function displayJoins()
2206 return array('pd' => PlSqlJoin
::left('profile_display', '$ME.pid = $PID'));
2215 private $with_logger = false
;
2216 public function addLoggerFilter()
2218 $this->with_logger
= true
;
2219 $this->requireAccounts();
2222 protected function loggerJoins()
2225 if ($this->with_logger
) {
2226 $joins['ls'] = PlSqlJoin
::left('log_sessions', '$ME.uid = $UID');
2234 static public function assertName($name)
2236 if (!DirEnum
::getID(DirEnum
::NAMETYPES
, $name)) {
2237 Platal
::page()->kill('Invalid name type: ' . $name);
2241 private $pn = array();
2242 public function addNameFilter($type, $variant = null
)
2244 $this->requireProfiles();
2245 if (!is_null($variant)) {
2246 $ft = $type . '_' . $variant;
2251 self
::assertName($ft);
2253 if (!is_null($variant) && $variant == 'other') {
2254 $sub .= $this->option++
;
2256 $this->pn
[$sub] = DirEnum
::getID(DirEnum
::NAMETYPES
, $ft);
2260 protected function nameJoins()
2263 foreach ($this->pn
as $sub => $type) {
2264 $joins['pn' . $sub] = PlSqlJoin
::left('profile_name', '$ME.pid = $PID AND $ME.typeid = {?}', $type);
2271 private $name_tokens = array();
2272 private $nb_tokens = 0;
2274 public function addNameTokensFilter($token)
2276 $this->requireProfiles();
2277 $sub = 'sn' . (1 +
$this->nb_tokens
);
2279 $this->name_tokens
[$sub] = $token;
2283 protected function nameTokensJoins()
2285 /* We don't return joins, since with_sn forces the SELECT to run on search_name first */
2287 foreach ($this->name_tokens
as $sub => $token) {
2288 $joins[$sub] = PlSqlJoin
::left('search_name', '$ME.pid = $PID');
2293 public function getNameTokens()
2295 return $this->name_tokens
;
2301 private $with_nat = false
;
2302 public function addNationalityFilter()
2304 $this->with_nat
= true
;
2308 protected function nationalityJoins()
2311 if ($this->with_nat
) {
2312 $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');
2319 const GRADE_ING
= 'Ing.';
2320 const GRADE_PHD
= 'PhD';
2321 const GRADE_MST
= 'M%';
2322 static public function isGrade($grade)
2324 return ($grade !== 0) && ($grade == self
::GRADE_ING ||
$grade == self
::GRADE_PHD ||
$grade == self
::GRADE_MST
);
2327 static public function assertGrade($grade)
2329 if (!self
::isGrade($grade)) {
2330 Platal
::page()->killError("DiplĂ´me non valide: $grade");
2334 static public function promoYear($grade)
2336 // XXX: Definition of promotion for phds and masters might change in near future.
2337 return ($grade == UserFilter
::GRADE_ING
) ?
'entry_year' : 'grad_year';
2340 private $pepe = array();
2341 private $with_pee = false
;
2342 public function addEducationFilter($x = false
, $grade = null
)
2344 $this->requireProfiles();
2346 $index = $this->option
;
2347 $sub = $this->option++
;
2349 self
::assertGrade($grade);
2352 $this->with_pee
= true
;
2355 $this->pepe
[$index] = $sub;
2359 protected function educationJoins()
2362 if ($this->with_pee
) {
2363 $joins['pee'] = PlSqlJoin
::inner('profile_education_enum', 'pee.abbreviation = \'X\'');
2365 foreach ($this->pepe
as $grade => $sub) {
2366 if ($this->isGrade($grade)) {
2367 $joins['pe' . $sub] = PlSqlJoin
::left('profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID');
2368 $joins['pede' . $sub] = PlSqlJoin
::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE {?}', $grade);
2370 $joins['pe' . $sub] = PlSqlJoin
::left('profile_education', '$ME.pid = $PID');
2371 $joins['pee' . $sub] = PlSqlJoin
::inner('profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
2372 $joins['pede' . $sub] = PlSqlJoin
::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
2381 private $gpm = array();
2382 public function addGroupFilter($group = null
)
2384 $this->requireAccounts();
2385 if (!is_null($group)) {
2386 if (is_int($group) ||
ctype_digit($group)) {
2387 $index = $sub = $group;
2390 $sub = self
::getDBSuffix($group);
2393 $sub = 'group_' . $this->option++
;
2397 $this->gpm
[$sub] = $index;
2401 protected function groupJoins()
2404 foreach ($this->gpm
as $sub => $key) {
2405 if (is_null($key)) {
2406 $joins['gpa' . $sub] = PlSqlJoin
::inner('groups');
2407 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
2408 } else if (is_int($key) ||
ctype_digit($key)) {
2409 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
2411 $joins['gpa' . $sub] = PlSqlJoin
::inner('groups', '$ME.diminutif = {?}', $key);
2412 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
2421 private $with_bi = false
;
2422 private $with_bd = false
;
2423 public function addBinetsFilter($with_enum = false
)
2425 $this->requireProfiles();
2426 $this->with_bi
= true
;
2428 $this->with_bd
= true
;
2435 protected function binetsJoins()
2438 if ($this->with_bi
) {
2439 $joins['bi'] = PlSqlJoin
::left('profile_binets', '$ME.pid = $PID');
2441 if ($this->with_bd
) {
2442 $joins['bd'] = PlSqlJoin
::left('profile_binet_enum', '$ME.id = bi.binet_id');
2449 private $e = array();
2450 public function addEmailRedirectFilter($email = null
)
2452 $this->requireAccounts();
2453 return $this->register_optional($this->e
, $email);
2456 private $ve = array();
2457 public function addVirtualEmailFilter($email = null
)
2459 $this->addAliasFilter(self
::ALIAS_FORLIFE
);
2460 return $this->register_optional($this->ve
, $email);
2463 const ALIAS_BEST
= 'bestalias';
2464 const ALIAS_FORLIFE
= 'forlife';
2465 private $al = array();
2466 public function addAliasFilter($alias = null
)
2468 $this->requireAccounts();
2469 return $this->register_optional($this->al
, $alias);
2472 protected function emailJoins()
2476 foreach ($this->e
as $sub=>$key) {
2477 if (is_null($key)) {
2478 $joins['e' . $sub] = PlSqlJoin
::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
2480 if (!is_array($key)) {
2483 $joins['e' . $sub] = PlSqlJoin
::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'
2484 AND $ME.email IN {?}', $key);
2487 foreach ($this->al
as $sub=>$key) {
2488 if (is_null($key)) {
2489 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
2490 } else if ($key == self
::ALIAS_BEST
) {
2491 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
2492 } else if ($key == self
::ALIAS_FORLIFE
) {
2493 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type = \'a_vie\'');
2495 if (!is_array($key)) {
2498 $joins['al' . $sub] = PlSqlJoin
::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')
2499 AND $ME.alias IN {?}', $key);
2502 foreach ($this->ve
as $sub=>$key) {
2503 if (is_null($key)) {
2504 $joins['v' . $sub] = PlSqlJoin
::left('virtual', '$ME.type = \'user\'');
2506 if (!is_array($key)) {
2509 $joins['v' . $sub] = PlSqlJoin
::left('virtual', '$ME.type = \'user\' AND $ME.alias IN {?}', $key);
2511 $joins['vr' . $sub] = PlSqlJoin
::left('virtual_redirect',
2512 '$ME.vid = v' . $sub . '.vid
2513 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
2514 CONCAT(al_forlife.alias, \'@\', {?}),
2516 $globals->mail
->domain
, $globals->mail
->domain2
);
2524 private $with_pa = false
;
2525 public function addAddressFilter()
2527 $this->requireProfiles();
2528 $this->with_pa
= true
;
2532 private $with_pac = false
;
2533 public function addAddressCountryFilter()
2535 $this->requireProfiles();
2536 $this->addAddressFilter();
2537 $this->with_pac
= true
;
2541 private $with_pal = false
;
2542 public function addAddressLocalityFilter()
2544 $this->requireProfiles();
2545 $this->addAddressFilter();
2546 $this->with_pal
= true
;
2550 protected function addressJoins()
2553 if ($this->with_pa
) {
2554 $joins['pa'] = PlSqlJoin
::left('profile_addresses', '$ME.pid = $PID');
2556 if ($this->with_pac
) {
2557 $joins['gc'] = PlSqlJoin
::left('geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID');
2559 if ($this->with_pal
) {
2560 $joins['gl'] = PlSqlJoin
::left('geoloc_localities', '$ME.id = pa.localityID');
2569 private $pc = false
;
2570 private $pce = array();
2571 private $pcr = false
;
2572 public function addCorpsFilter($type)
2574 $this->requireProfiles();
2576 if ($type == UFC_Corps
::CURRENT
) {
2577 $pce['pcec'] = 'current_corpsid';
2579 } else if ($type == UFC_Corps
::ORIGIN
) {
2580 $pce['pceo'] = 'original_corpsid';
2585 public function addCorpsRankFilter()
2587 $this->requireProfiles();
2593 protected function corpsJoins()
2597 $joins['pc'] = PlSqlJoin
::left('profile_corps', '$ME.pid = $PID');
2600 $joins['pcr'] = PlSqlJoin
::left('profile_corps_rank_enum', '$ME.id = pc.rankid');
2602 foreach($this->pce
as $sub => $field) {
2603 $joins[$sub] = PlSqlJoin
::left('profile_corps_enum', '$ME.id = pc.' . $field);
2611 const JOB_SECTOR
= 0x0001;
2612 const JOB_SUBSECTOR
= 0x0002;
2613 const JOB_SUBSUBSECTOR
= 0x0004;
2614 const JOB_ALTERNATES
= 0x0008;
2615 const JOB_USERDEFINED
= 0x0010;
2616 const JOB_CV
= 0x0020;
2618 const JOB_SECTORIZATION
= 0x000F;
2619 const JOB_ANY
= 0x003F;
2623 * pje => profile_job_enum
2624 * pjse => profile_job_sector_enum
2625 * pjsse => profile_job_subsector_enum
2626 * pjssse => profile_job_subsubsector_enum
2627 * pja => profile_job_alternates
2628 * pjt => profile_job_terms
2630 private $with_pj = false
;
2631 private $with_pje = false
;
2632 private $with_pjse = false
;
2633 private $with_pjsse = false
;
2634 private $with_pjssse = false
;
2635 private $with_pja = false
;
2636 private $with_pjt = 0;
2638 public function addJobFilter()
2640 $this->requireProfiles();
2641 $this->with_pj
= true
;
2645 public function addJobCompanyFilter()
2647 $this->addJobFilter();
2648 $this->with_pje
= true
;
2652 public function addJobSectorizationFilter($type)
2654 $this->addJobFilter();
2655 if ($type == self
::JOB_SECTOR
) {
2656 $this->with_pjse
= true
;
2658 } else if ($type == self
::JOB_SUBSECTOR
) {
2659 $this->with_pjsse
= true
;
2661 } else if ($type == self
::JOB_SUBSUBSECTOR
) {
2662 $this->with_pjssse
= true
;
2664 } else if ($type == self
::JOB_ALTERNATES
) {
2665 $this->with_pja
= true
;
2671 * Adds a filter on job terms of profile.
2672 * @param $nb the number of job terms to use
2673 * @return an array of the fields to filter (one for each term).
2675 public function addJobTermsFilter($nb = 1)
2677 $this->with_pjt
= $nb;
2678 $jobtermstable = array();
2679 for ($i = 1; $i <= $nb; ++
$i) {
2680 $jobtermstable[] = 'pjtr_'.$i;
2682 return $jobtermstable;
2685 protected function jobJoins()
2688 if ($this->with_pj
) {
2689 $joins['pj'] = PlSqlJoin
::left('profile_job', '$ME.pid = $PID');
2691 if ($this->with_pje
) {
2692 $joins['pje'] = PlSqlJoin
::left('profile_job_enum', '$ME.id = pj.jobid');
2694 if ($this->with_pjse
) {
2695 $joins['pjse'] = PlSqlJoin
::left('profile_job_sector_enum', '$ME.id = pj.sectorid');
2697 if ($this->with_pjsse
) {
2698 $joins['pjsse'] = PlSqlJoin
::left('profile_job_subsector_enum', '$ME.id = pj.subsectorid');
2700 if ($this->with_pjssse
) {
2701 $joins['pjssse'] = PlSqlJoin
::left('profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
2703 if ($this->with_pja
) {
2704 $joins['pja'] = PlSqlJoin
::left('profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
2706 if ($this->with_pjt
> 0) {
2707 for ($i = 1; $i <= $this->with_pjt
; ++
$i) {
2708 $joins['pjt_'.$i] = PlSqlJoin
::left('profile_job_term', '$ME.pid = $PID');
2709 $joins['pjtr_'.$i] = PlSqlJoin
::left('profile_job_term_relation', '$ME.jtid_2 = pjt_'.$i.'.jtid');
2718 private $with_pnw = false
;
2719 public function addNetworkingFilter()
2721 $this->requireAccounts();
2722 $this->with_pnw
= true
;
2726 protected function networkingJoins()
2729 if ($this->with_pnw
) {
2730 $joins['pnw'] = PlSqlJoin
::left('profile_networking', '$ME.pid = $PID');
2738 private $with_ptel = false
;
2740 public function addPhoneFilter()
2742 $this->requireAccounts();
2743 $this->with_ptel
= true
;
2747 protected function phoneJoins()
2750 if ($this->with_ptel
) {
2751 $joins['ptel'] = PlSqlJoin
::left('profile_phones', '$ME.pid = $PID');
2759 private $with_pmed = false
;
2760 public function addMedalFilter()
2762 $this->requireProfiles();
2763 $this->with_pmed
= true
;
2767 protected function medalJoins()
2770 if ($this->with_pmed
) {
2771 $joins['pmed'] = PlSqlJoin
::left('profile_medals', '$ME.pid = $PID');
2779 private $pms = array();
2780 private $mjtr = false
;
2782 const MENTOR_EXPERTISE
= 2;
2783 const MENTOR_COUNTRY
= 3;
2784 const MENTOR_SECTOR
= 4;
2785 const MENTOR_TERM
= 5;
2787 public function addMentorFilter($type)
2789 $this->requireAccounts();
2792 $this->pms
['pm'] = 'profile_mentor';
2794 case self
::MENTOR_EXPERTISE
:
2795 $this->pms
['pme'] = 'profile_mentor';
2797 case self
::MENTOR_COUNTRY
:
2798 $this->pms
['pmc'] = 'profile_mentor_country';
2800 case self
::MENTOR_SECTOR
:
2801 $this->pms
['pms'] = 'profile_mentor_sector';
2803 case self
::MENTOR_TERM
:
2804 $this->pms
['pmt'] = 'profile_mentor_term';
2808 Platal
::page()->killError("Undefined mentor filter.");
2812 protected function mentorJoins()
2815 foreach ($this->pms
as $sub => $tab) {
2816 $joins[$sub] = PlSqlJoin
::left($tab, '$ME.pid = $PID');
2819 $joins['mjtr'] = PlSqlJoin
::left('profile_job_term_relation', '$ME.jtid_2 = pmt.jtid');
2826 private $cts = array();
2827 public function addContactFilter($uid = null
)
2829 $this->requireProfiles();
2830 return $this->register_optional($this->cts
, is_null($uid) ? null
: 'user_' . $uid);
2833 protected function contactJoins()
2836 foreach ($this->cts
as $sub=>$key) {
2837 if (is_null($key)) {
2838 $joins['c' . $sub] = PlSqlJoin
::left('contacts', '$ME.contact = $PID');
2840 $joins['c' . $sub] = PlSqlJoin
::left('contacts', '$ME.uid = {?} AND $ME.contact = $PID', substr($key, 5));
2849 private $wn = array();
2850 public function addWatchRegistrationFilter($uid = null
)
2852 $this->requireAccounts();
2853 return $this->register_optional($this->wn
, is_null($uid) ? null
: 'user_' . $uid);
2856 private $wp = array();
2857 public function addWatchPromoFilter($uid = null
)
2859 $this->requireAccounts();
2860 return $this->register_optional($this->wp
, is_null($uid) ? null
: 'user_' . $uid);
2863 private $w = array();
2864 public function addWatchFilter($uid = null
)
2866 $this->requireAccounts();
2867 return $this->register_optional($this->w
, is_null($uid) ? null
: 'user_' . $uid);
2870 protected function watchJoins()
2873 foreach ($this->w
as $sub=>$key) {
2874 if (is_null($key)) {
2875 $joins['w' . $sub] = PlSqlJoin
::left('watch');
2877 $joins['w' . $sub] = PlSqlJoin
::left('watch', '$ME.uid = {?}', substr($key, 5));
2880 foreach ($this->wn
as $sub=>$key) {
2881 if (is_null($key)) {
2882 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.ni_id = $UID');
2884 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
2887 foreach ($this->wn
as $sub=>$key) {
2888 if (is_null($key)) {
2889 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.ni_id = $UID');
2891 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
2894 foreach ($this->wp
as $sub=>$key) {
2895 if (is_null($key)) {
2896 $joins['wp' . $sub] = PlSqlJoin
::left('watch_promo');
2898 $joins['wp' . $sub] = PlSqlJoin
::left('watch_promo', '$ME.uid = {?}', substr($key, 5));
2907 private $with_photo;
2908 public function addPhotoFilter()
2910 $this->requireProfiles();
2911 $this->with_photo
= true
;
2915 protected function photoJoins()
2917 if ($this->with_photo
) {
2918 return array('photo' => PlSqlJoin
::left('profile_photos', '$ME.pid = $PID'));
2928 public function addMarketingHash()
2930 $this->requireAccounts();
2931 $this->with_rm
= true
;
2934 protected function marketingJoins()
2936 if ($this->with_rm
) {
2937 return array('rm' => PlSqlJoin
::left('register_marketing', '$ME.uid = $UID'));
2945 // {{{ class ProfileFilter
2946 class ProfileFilter
extends UserFilter
2948 public function get($limit = null
)
2950 return $this->getProfiles($limit);
2953 public function filter(array $profiles, $limit = null
)
2955 return $this->filterProfiles($profiles, self
::defaultLimit($limit));
2958 public function getTotalCount()
2960 return $this->getTotalProfileCount();
2963 public function getGroups()
2965 return $this->getPIDGroups();
2970 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: