7a1474d1062ec6e07c1a96feb3d1426e5f297676
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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
39 /** Check that the given user matches the rule.
41 public function buildCondition(UserFilter
&$uf);
45 // {{{ class UFC_Profile
46 /** Filters users who have a profile
48 class UFC_Profile
implements UserFilterCondition
50 public function buildCondition(UserFilter
&$uf)
52 return '$PID IS NOT NULL';
57 // {{{ class UFC_Promo
58 /** Filters users based on promotion
59 * @param $comparison Comparison operator (>, =, ...)
60 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
61 * @param $promo Promotion on which the filter is based
63 class UFC_Promo
implements UserFilterCondition
70 public function __construct($comparison, $grade, $promo)
72 $this->grade
= $grade;
73 $this->comparison
= $comparison;
74 $this->promo
= $promo;
75 if ($this->grade
!= UserFilter
::DISPLAY
) {
76 UserFilter
::assertGrade($this->grade
);
80 public function buildCondition(UserFilter
&$uf)
82 if ($this->grade
== UserFilter
::DISPLAY
) {
83 $sub = $uf->addDisplayFilter();
84 return XDB
::format('pd' . $sub . '.promo ' . $this->comparison
. ' {?}', $this->promo
);
86 $sub = $uf->addEducationFilter(true
, $this->grade
);
87 $field = 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
88 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->promo
);
95 /** Filters users based on name
96 * @param $type Type of name field on which filtering is done (firstname, lastname...)
97 * @param $text Text on which to filter
98 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
100 class UFC_Name
implements UserFilterCondition
112 public function __construct($type, $text, $mode)
119 private function buildNameQuery($type, $variant, $where, UserFilter
&$uf)
121 $sub = $uf->addNameFilter($type, $variant);
122 return str_replace('$ME', 'pn' . $sub, $where);
125 public function buildCondition(UserFilter
&$uf)
129 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
130 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
132 if (($this->mode
& self
::CONTAINS
) == 0) {
133 $right = XDB
::format('{?}', $this->text
);
135 } else if (($this->mode
& self
::CONTAINS
) == self
::PREFIX
) {
136 $right = XDB
::format('CONCAT({?}, \'%\')', $this->text
);
137 } else if (($this->mode
& self
::CONTAINS
) == self
::SUFFIX
) {
138 $right = XDB
::format('CONCAT(\'%\', {?})', $this->text
);
140 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->text
);
142 $cond = $left . $op . $right;
143 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
144 if (($this->mode
& self
::VARIANTS
) != 0 && isset(UserFilter
::$name_variants[$this->type
])) {
145 foreach (UserFilter
::$name_variants[$this->type
] as $var) {
146 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
149 return implode(' OR ', $conds);
154 // {{{ class UFC_Dead
155 /** Filters users based on death date
156 * @param $comparison Comparison operator
157 * @param $date Date to which death date should be compared
159 class UFC_Dead
implements UserFilterCondition
164 public function __construct($comparison = null
, $date = null
)
166 $this->comparison
= $comparison;
170 public function buildCondition(UserFilter
&$uf)
172 $str = 'p.deathdate IS NOT NULL';
173 if (!is_null($this->comparison
)) {
174 $str .= ' AND p.deathdate ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
181 // {{{ class UFC_Registered
182 /** Filters users based on registration state
183 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
184 * @param $comparison Comparison operator
185 * @param $date Date to which users registration date should be compared
187 class UFC_Registered
implements UserFilterCondition
193 public function __construct($active = false
, $comparison = null
, $date = null
)
195 $this->active
= $active;
196 $this->comparison
= $comparison;
200 public function buildCondition(UserFilter
&$uf)
203 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
205 $date = 'a.uid IS NOT NULL AND a.state != \'pending\'';
207 if (!is_null($this->comparison
)) {
208 $date .= ' AND a.registration_date ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
215 // {{{ class UFC_ProfileUpdated
216 /** Filters users based on profile update date
217 * @param $comparison Comparison operator
218 * @param $date Date to which profile update date must be compared
220 class UFC_ProfileUpdated
implements UserFilterCondition
225 public function __construct($comparison = null
, $date = null
)
227 $this->comparison
= $comparison;
231 public function buildCondition(UserFilter
&$uf)
233 return 'p.last_change ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d H:i:s', $this->date
));
238 // {{{ class UFC_Birthday
239 /** Filters users based on next birthday date
240 * @param $comparison Comparison operator
241 * @param $date Date to which users next birthday date should be compared
243 class UFC_Birthday
implements UserFilterCondition
248 public function __construct($comparison = null
, $date = null
)
250 $this->comparison
= $comparison;
254 public function buildCondition(UserFilter
&$uf)
256 return 'p.next_birthday ' . $this->comparison
. XDB
::format(' {?}', date('Y-m-d', $this->date
));
262 /** Filters users based on sex
263 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
265 class UFC_Sex
implements UserFilterCondition
268 public function __construct($sex)
273 public function buildCondition(UserFilter
&$uf)
275 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
276 return self
::COND_FALSE
;
278 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
284 // {{{ class UFC_Group
285 /** Filters users based on group membership
286 * @param $group Group whose members we are selecting
287 * @param $anim Whether to restrict selection to animators of that group
289 class UFC_Group
implements UserFilterCondition
293 public function __construct($group, $anim = false
)
295 $this->group
= $group;
299 public function buildCondition(UserFilter
&$uf)
301 $sub = $uf->addGroupFilter($this->group
);
302 $where = 'gpm' . $sub . '.perms IS NOT NULL';
304 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
311 // {{{ class UFC_Email
312 /** Filters users based on email address
313 * @param $email Email whose owner we are looking for
315 class UFC_Email
implements UserFilterCondition
318 public function __construct($email)
320 $this->email
= $email;
323 public function buildCondition(UserFilter
&$uf)
325 if (User
::isForeignEmailAddress($this->email
)) {
326 $sub = $uf->addEmailRedirectFilter($this->email
);
327 return XDB
::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email
);
328 } else if (User
::isVirtualEmailAddress($this->email
)) {
329 $sub = $uf->addVirtualEmailFilter($this->email
);
330 return 'vr' . $sub . '.redirect IS NOT NULL';
332 @list
($user, $domain) = explode('@', $this->email
);
333 $sub = $uf->addAliasFilter($user);
334 return 'al' . $sub . '.alias IS NOT NULL';
340 // {{{ class UFC_EmailList
341 /** Filters users based on an email list
342 * @param $emails List of emails whose owner must be selected
344 class UFC_EmailList
implements UserFilterCondition
347 public function __construct($emails)
349 $this->emails
= $emails;
352 public function buildCondition(UserFilter
&$uf)
359 if (count($this->emails
) == 0) {
360 return UserFilterCondition
::COND_TRUE
;
363 foreach ($this->emails
as $entry) {
364 if (User
::isForeignEmailAddress($entry)) {
365 if (is_null($email)) {
366 $email = $uf->addEmailRedirectFilter();
368 $cond[] = XDB
::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry);
369 } else if (User
::isVirtualEmailAddress($entry)) {
370 if (is_null($virtual)) {
371 $virtual = $uf->addVirtualEmailFilter();
373 $cond[] = XDB
::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry);
375 if (is_null($alias)) {
376 $alias = $uf->addAliasFilter();
378 @list
($user, $domain) = explode('@', $entry);
379 $cond[] = XDB
::format('al' . $alias . '.alias = {?}', $user);
382 return '(' . implode(') OR (', $cond) . ')';
387 // {{{ class UFC_Address
388 /** Filters users based on their address
389 * @param $text Text for filter in fulltext search
390 * @param $textSearchMode Mode for search (PREFIX, SUFFIX, ...)
391 * @param $type Filter on address type
392 * @param $flags Filter on address flags
393 * @param $countryId Filter on address countryId
394 * @param $administrativeAreaId Filter on address administrativeAreaId
395 * @param $subAdministrativeAreaId Filter on address subAdministrativeAreaId
396 * @param $localityId Filter on address localityId
397 * @param $postalCode Filter on address postalCode
399 class UFC_Address
implements UserFilterCondition
401 /** Flags for text search
403 const PREFIX
= 0x0001;
404 const SUFFIX
= 0x0002;
405 const CONTAINS
= 0x0003;
407 /** Valid address type ('hq' is reserved for company addresses)
409 const TYPE_HOME
= 'home';
410 const TYPE_PRO
= 'job';
412 /** Flags for addresses
414 const FLAG_CURRENT
= 0x0001;
415 const FLAG_TEMP
= 0x0002;
416 const FLAG_SECOND
= 0x0004;
417 const FLAG_MAIL
= 0x0008;
418 const FLAG_CEDEX
= 0x0010;
420 // Binary OR of those flags
421 const FLAG_ANY
= 0x001F;
423 /** Text of these flags
425 private static $flagtexts = array(
426 self
::FLAG_CURRENT
=> 'current',
427 self
::FLAG_TEMP
=> 'temporary',
428 self
::FLAG_SECOND
=> 'secondary',
429 self
::FLAG_MAIL
=> 'mail',
430 self
::FLAG_CEDEX
=> 'cedex',
433 /** Data of the filter
439 private $administrativeAreaId;
440 private $subAdministrativeAreaId;
444 private $textSearchMode;
446 public function __construct($text = null
, $textSearchMode = self
::CONTAINS
,
447 $type = null
, $flags = self
::FLAG_ANY
, $countryId = null
, $administrativeAreaId = null
,
448 $subAdministrativeAreaId = null
, $localityId = null
, $postalCode = null
)
451 $this->textSearchMode
= $textSearchMode;
453 $this->flags
= $flags;
454 $this->countryId
= $countryId;
455 $this->administrativeAreaId
= $administrativeAreaId;
456 $this->subAdministrativeAreaId
= $subAdministrativeAreaId;
457 $this->localityId
= $localityId;
458 $this->postalCode
= $postalCode;
461 public function buildCondition(UserFilter
&$uf)
463 $sub = $uf->addAddressFilter();
465 if ($this->text
!= null
) {
466 $left = $sub . '.text ';
468 if (($this->textSearchMode
& self
::CONTAINS
) == 0) {
469 $right = XDB
::format('{?}', $this->text
);
471 } else if (($this->mode
& self
::CONTAINS
) == self
::PREFIX
) {
472 $right = XDB
::format('CONCAT({?}, \'%\')', $this->text
);
473 } else if (($this->mode
& self
::CONTAINS
) == self
::SUFFIX
) {
474 $right = XDB
::format('CONCAT(\'%\', {?})', $this->text
);
476 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->text
);
478 $conds[] = $left . $op . $right;
481 if ($this->type
!= null
) {
482 $conds[] = $sub . '.type = ' . XDB
::format('{?}', $this->type
);
485 if ($this->flags
!= self
::FLAG_ANY
) {
486 foreach(self
::$flagtexts as $flag => $text) {
487 if ($flag & $this->flags
) {
488 $conds[] = 'FIND_IN_SET(' . XDB
::format('{?}', $text) . ', ' . $sub . '.flags)';
493 if ($this->countryId
!= null
) {
494 $conds[] = $sub . '.countryId = ' . XDB
::format('{?}', $this->countryId
);
496 if ($this->administrativeAreaId
!= null
) {
497 $conds[] = $sub . '.administrativeAreaId = ' . XDB
::format('{?}', $this->administrativeAreaId
);
499 if ($this->subAdministrativeAreaId
!= null
) {
500 $conds[] = $sub . '.subAdministrativeAreaId = ' . XDB
::format('{?}', $this->subAdministrativeAreaId
);
502 if ($this->localityId
!= null
) {
503 $conds[] = $sub . '.localityId = ' . XDB
::format('{?}', $this->localityId
);
505 if ($this->postalCode
!= null
) {
506 $conds[] = $sub . '.postalCode = ' . XDB
::format('{?}', $this->postalCode
);
509 return implode(' AND ', $conds);
514 // {{{ class UFC_Corps
515 /** Filters users based on the corps they belong to
516 * @param $corps Corps we are looking for (abbreviation)
517 * @param $type Whether we search for original or current corps
519 class UFC_Corps
implements UserFilterCondition
527 public function __construct($corps, $type = self
::CURRENT
)
529 $this->corps
= $corps;
533 public function buildCondition(UserFilter
&$uf)
535 /** Tables shortcuts:
536 * pc for profile_corps,
537 * pceo for profile_corps_enum - orginal
538 * pcec for profile_corps_enum - current
540 $sub = $uf->addCorpsFilter($this->type
);
541 $cond = $sub . '.abbreviation = ' . $corps;
547 // {{{ class UFC_Corps_Rank
548 /** Filters users based on their rank in the corps
549 * @param $rank Rank we are looking for (abbreviation)
551 class UFC_Corps_Rank
implements UserFilterCondition
554 public function __construct($rank)
559 public function buildCondition(UserFilter
&$uf)
561 /** Tables shortcuts:
562 * pcr for profile_corps_rank
564 $sub = $uf->addCorpsRankFilter();
565 $cond = $sub . '.abbreviation = ' . $rank;
571 // {{{ class UFC_Job_Company
572 /** Filters users based on the company they belong to
573 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
574 * @param $value The searched value
576 class UFC_Job_Company
implements UserFilterCondition
579 const JOBNAME
= 'name';
580 const JOBACRONYM
= 'acronym';
585 public function __construct($type, $value)
587 $this->assertType($type);
589 $this->value
= $value;
592 private function assertType($type)
594 if ($type != self
::JOBID
&& $type != self
::JOBNAME
&& $type != self
::JOBACRONYM
) {
595 Platal
::page()->killError("Type de recherche non valide.");
599 public function buildCondition(UserFilter
&$uf)
601 $sub = $uf->addJobCompanyFilter();
602 $cond = $sub . '.' . $this->type
. ' = ' . XDB
::format('{?}', $this->value
);
608 // {{{ class UFC_Job_Sectorization
609 /** Filters users based on the ((sub)sub)sector they work in
610 * @param $sector The sector searched
611 * @param $subsector The subsector
612 * @param $subsubsector The subsubsector
614 class UFC_Job_Sectorization
implements UserFilterCondition
619 private $subsubsector;
621 public function __construct($sector = null
, $subsector = null
, $subsubsector = null
)
623 $this->sector
= $sector;
624 $this->subsector
= $subsector;
625 $this->subsubsector
= $subsubsector;
628 public function buildCondition(UserFilter
&$uf)
630 // No need to add the JobFilter, it will be done by addJobSectorizationFilter
632 if ($this->sector
!== null
) {
633 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SECTOR
);
634 $conds[] = $sub . '.id = ' . XDB
::format('{?}', $this->sector
);
636 if ($this->subsector
!== null
) {
637 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSECTOR
);
638 $conds[] = $sub . '.id = ' . XDB
::format('{?}', $this->subsector
);
640 if ($this->subsubsector
!== null
) {
641 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSUBSECTOR
);
642 $conds[] = $sub . '.id = ' . XDB
::format('{?}', $this->subsubsector
);
644 return implode(' AND ', $conds);
649 // {{{ class UFC_Job_Description
650 /** Filters users based on their job description
651 * @param $description The text being searched for
652 * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
654 class UFC_Job_Description
implements UserFilterCondition
658 * Built with binary OR on UserFilter::JOB_*
661 const SECTORIZATION
= 15;
663 private $description;
666 public function __construct($description)
668 $this->fields
= $fields;
669 $this->description
= $description;
672 public function buildCondition(UserFilter
&$uf)
675 if ($this->fields
& UserFilter
::JOB_USERDEFINED
) {
676 $sub = $uf->addJobFilter();
677 $conds[] = $sub . '.description LIKE ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->description
);
679 if ($this->fields
& UserFilter
::JOB_SECTOR
) {
680 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SECTOR
);
681 $conds[] = $sub . '.name LIKE ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->description
);
683 if ($this->fields
& UserFilter
::JOB_SUBSECTOR
) {
684 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSECTOR
);
685 $conds[] = $sub . '.name LIKE ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->description
);
687 if ($this->fields
& UserFilter
::JOB_SUBSUBSECTOR
) {
688 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_SUBSUBSECTOR
);
689 $conds[] = $sub . '.name LIKE ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->description
);
690 $sub = $uf->addJobSectorizationFilter(UserFilter
::JOB_ALTERNATES
);
691 $conds[] = $sub . '.name LIKE ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->description
);
693 return implode(' OR ', $conds);
698 // {{{ class UFC_Networking
699 /** Filters users based on network identity (IRC, ...)
700 * @param $type Type of network (-1 for any)
701 * @param $value Value to search
703 class UFC_Networking
implements UserFilterCondition
708 public function __construct($type, $value)
711 $this->value
= $value;
714 public function buildCondition(UserFilter
&$uf)
716 $sub = $uf->addNetworkingFilter();
718 $conds[] = $sub . '.address = ' . XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->value
);
719 if ($this->type
!= -1) {
720 $conds[] = $sub . '.network_type = ' . XDB
::format('{?}', $this->type
);
722 return implode(' AND ', $conds);
727 // {{{ class UFC_Phone
728 /** Filters users based on their phone number
729 * @param $num_type Type of number (pro/user/home)
730 * @param $phone_type Type of phone (fixed/mobile/fax)
731 * @param $number Phone number
733 class UFC_Phone
implements UserFilterCondition
735 const NUM_PRO
= 'pro';
736 const NUM_USER
= 'user';
737 const NUM_HOME
= 'address';
738 const NUM_ANY
= 'any';
740 const PHONE_FIXED
= 'fixed';
741 const PHONE_MOBILE
= 'mobile';
742 const PHONE_FAX
= 'fax';
743 const PHONE_ANY
= 'any';
749 public function __construct($number, $num_type = self
::NUM_ANY
, $phone_type = self
::PHONE_ANY
)
751 require_once('profil.func.inc.php');
752 $this->number
= $number;
753 $this->num_type
= $num_type;
754 $this->phone_type
= format_phone_number($phone_type);
757 public function buildCondition(UserFilter
&$uf)
759 $sub = $uf->addPhoneFilter();
761 $conds[] = $sub . '.search_tel = ' . XDB
::format('{?}', $this->number
);
762 if ($this->num_type
!= self
::NUM_ANY
) {
763 $conds[] = $sub . '.link_type = ' . XDB
::format('{?}', $this->num_type
);
765 if ($this->phone_type
!= self
::PHONE_ANY
) {
766 $conds[] = $sub . '.tel_type = ' . XDB
::format('{?}', $this->phone_type
);
768 return implode(' AND ', $conds);
773 // {{{ class UFC_Medal
774 /** Filters users based on their medals
775 * @param $medal ID of the medal
776 * @param $grade Grade of the medal (null for 'any')
778 class UFC_Medal
implements UserFilterCondition
783 public function __construct($medal, $grade = null
)
785 $this->medal
= $medal;
786 $this->grade
= $grade;
789 public function buildCondition(UserFilter
&$uf)
792 $sub = $uf->addMedalFilter();
793 $conds[] = $sub . '.mid = ' . XDB
::format('{?}', $this->medal
);
794 if ($this->grade
!= null
) {
795 $conds[] = $sub . '.gid = ' . XDB
::format('{?}', $this->grade
);
797 return implode(' AND ', $conds);
802 // {{{ class UFC_Mentor_Expertise
803 /** Filters users by mentoring expertise
804 * @param $expertise Domain of expertise
806 class UFC_Mentor_Expertise
implements UserFilterCondition
810 public function __construct($expertise)
812 $this->expertise
= $expertise;
815 public function buildCondition(UserFilter
&$uf)
817 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_EXPERTISE
);
818 return $sub . '.expertise LIKE ' . XDB
::format('CONCAT(\'%\', {?}, \'%\'', $this->expertise
);
823 // {{{ class UFC_Mentor_Country
824 /** Filters users by mentoring country
825 * @param $country Two-letters code of country being searched
827 class UFC_Mentor_Country
implements UserFilterCondition
831 public function __construct($country)
833 $this->country
= $country;
836 public function buildCondition(UserFilter
&$uf)
838 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_COUNTRY
);
839 return $sub . '.country = ' . XDB
::format('{?}', $this->country
);
844 // {{{ class UFC_Mentor_Sectorization
845 /** Filters users based on mentoring (sub|)sector
846 * @param $sector ID of sector
847 * @param $subsector Subsector (null for any)
849 class UFC_Mentor_Sectorization
implements UserFilterCondition
854 public function __construct($sector, $subsector = null
)
856 $this->sector
= $sector;
857 $this->subsubsector
= $subsector;
860 public function buildCondition(UserFilter
&$uf)
862 $sub = $uf->addMentorFilter(UserFilter
::MENTOR_SECTOR
);
864 $conds[] = $sub . '.sectorid = ' . XDB
::format('{?}', $this->sector
);
865 if ($this->subsector
!= null
) {
866 $conds[] = $sub . '.subsectorid = ' . XDB
::format('{?}', $this->subsector
);
868 return implode(' AND ', $conds);
873 // {{{ class UFC_UserRelated
874 /** Filters users based on a relation toward a user
875 * @param $user User to which searched users are related
877 abstract class UFC_UserRelated
implements UserFilterCondition
880 public function __construct(PlUser
&$user)
882 $this->user
=& $user;
887 // {{{ class UFC_Contact
888 /** Filters users who belong to selected user's contacts
890 class UFC_Contact
extends UFC_UserRelated
892 public function buildCondition(UserFilter
&$uf)
894 $sub = $uf->addContactFilter($this->user
->id());
895 return 'c' . $sub . '.contact IS NOT NULL';
900 // {{{ class UFC_WatchRegistration
901 /** Filters users being watched by selected user
903 class UFC_WatchRegistration
extends UFC_UserRelated
905 public function buildCondition(UserFilter
&$uf)
907 if (!$this->user
->watch('registration')) {
908 return UserFilterCondition
::COND_FALSE
;
910 $uids = $this->user
->watchUsers();
911 if (count($uids) == 0) {
912 return UserFilterCondition
::COND_FALSE
;
914 return '$UID IN ' . XDB
::formatArray($uids);
920 // {{{ class UFC_WatchPromo
921 /** Filters users belonging to a promo watched by selected user
922 * @param $user Selected user (the one watching promo)
923 * @param $grade Formation the user is watching
925 class UFC_WatchPromo
extends UFC_UserRelated
928 public function __construct(PlUser
&$user, $grade = UserFilter
::GRADE_ING
)
930 parent
::__construct($user);
931 $this->grade
= $grade;
934 public function buildCondition(UserFilter
&$uf)
936 $promos = $this->user
->watchPromos();
937 if (count($promos) == 0) {
938 return UserFilterCondition
::COND_FALSE
;
940 $sube = $uf->addEducationFilter(true
, $this->grade
);
941 $field = 'pe' . $sube . '.' . UserFilter
::promoYear($this->grade
);
942 return $field . ' IN ' . XDB
::formatArray($promos);
948 // {{{ class UFC_WatchContact
949 /** Filters users watched by selected user
951 class UFC_WatchContact
extends UFC_Contact
953 public function buildCondition(UserFilter
&$uf)
955 if (!$this->user
->watchContacts()) {
956 return UserFilterCondition
::COND_FALSE
;
958 return parent
::buildCondition($uf);
968 // {{{ class UserFilterOrder
969 /** Base class for ordering results of a query.
970 * Parameters for the ordering must be given to the constructor ($desc for a
972 * The getSortTokens function is used to get actual ordering part of the query.
974 abstract class UserFilterOrder
extends PlFilterOrder
976 /** This function must return the tokens to use for ordering
977 * @param &$uf The UserFilter whose results must be ordered
978 * @return The name of the field to use for ordering results
980 abstract protected function getSortTokens(UserFilter
&$uf);
984 // {{{ class UFO_Promo
985 /** Orders users by promotion
986 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
987 * @param $desc Whether sort is descending
989 class UFO_Promo
extends UserFilterOrder
993 public function __construct($grade = null
, $desc = false
)
995 parent
::__construct($desc);
996 $this->grade
= $grade;
999 protected function getSortTokens(UserFilter
&$uf)
1001 if (UserFilter
::isGrade($this->grade
)) {
1002 $sub = $uf->addEducationFilter($this->grade
);
1003 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
1005 $sub = $uf->addDisplayFilter();
1006 return 'pd' . $sub . '.promo';
1012 // {{{ class UFO_Name
1013 /** Sorts users by name
1014 * @param $type Type of name on which to sort (firstname...)
1015 * @param $variant Variant of that name to use (marital, ordinary...)
1016 * @param $particle Set to true if particles should be included in the sorting order
1017 * @param $desc If sort order should be descending
1019 class UFO_Name
extends UserFilterOrder
1025 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
1027 parent
::__construct($desc);
1028 $this->type
= $type;
1029 $this->variant
= $variant;
1030 $this->particle
= $particle;
1033 protected function getSortTokens(UserFilter
&$uf)
1035 if (UserFilter
::isDisplayName($this->type
)) {
1036 $sub = $uf->addDisplayFilter();
1037 return 'pd' . $sub . '.' . $this->type
;
1039 $sub = $uf->addNameFilter($this->type
, $this->variant
);
1040 if ($this->particle
) {
1041 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1043 return 'pn' . $sub . '.name';
1050 // {{{ class UFO_Registration
1051 /** Sorts users based on registration date
1053 class UFO_Registration
extends UserFilterOrder
1055 protected function getSortTokens(UserFilter
&$uf)
1057 return 'a.registration_date';
1062 // {{{ class UFO_Birthday
1063 /** Sorts users based on next birthday date
1065 class UFO_Birthday
extends UserFilterOrder
1067 protected function getSortTokens(UserFilter
&$uf)
1069 return 'p.next_birthday';
1074 // {{{ class UFO_ProfileUpdate
1075 /** Sorts users based on last profile update
1077 class UFO_ProfileUpdate
extends UserFilterOrder
1079 protected function getSortTokens(UserFilter
&$uf)
1081 return 'p.last_change';
1086 // {{{ class UFO_Death
1087 /** Sorts users based on death date
1089 class UFO_Death
extends UserFilterOrder
1091 protected function getSortTokens(UserFilter
&$uf)
1093 return 'p.deathdate';
1099 /***********************************
1100 *********************************
1102 *********************************
1103 ***********************************/
1105 // {{{ class UserFilter
1106 /** This class provides a convenient and centralized way of filtering users.
1109 * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t));
1111 * Resulting UserFilter can be used to:
1112 * - get a list of User objects matching the filter
1113 * - get a list of UIDs matching the filter
1114 * - get the number of users matching the filter
1115 * - check whether a given User matches the filter
1116 * - filter a list of User objects depending on whether they match the filter
1118 * Usage for UFC and UFO objects:
1119 * A UserFilter will call all private functions named XXXJoins.
1120 * These functions must return an array containing the list of join
1121 * required by the various UFC and UFO associated to the UserFilter.
1122 * Entries in those returned array are of the following form:
1123 * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter')
1124 * which will be translated into :
1125 * join_type JOIN joined_table AS join_tablealias ON (join_criter)
1126 * in the final query.
1128 * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with
1129 * profile.pid, and $UID with auth_user_md5.user_id.
1131 * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined;
1132 * its parameter will be used to set various private vars of the UserFilter describing
1133 * the required joins ; such a function shall return the "join_tablealias" to use
1134 * when referring to the joined table.
1136 * For example, if data from profile_job must be available to filter results,
1137 * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and
1138 * return 'pj', the short name to use when referring to profile_job; when building
1139 * the query, calling the jobJoins function will return an array containing a single
1141 * 'pj' => array('left', 'profile_job', '$ME.pid = $UID');
1143 * The 'register_optional' function can be used to generate unique table aliases when
1144 * the same table has to be joined several times with different aliases.
1146 class UserFilter
extends PlFilter
1148 protected $joinMethods = array();
1150 protected $joinMetas = array('$PID' => 'p.pid',
1155 private $sort = array();
1156 private $query = null
;
1157 private $orderby = null
;
1159 private $lastcount = null
;
1161 public function __construct($cond = null
, $sort = null
)
1163 if (empty($this->joinMethods
)) {
1164 $class = new ReflectionClass('UserFilter');
1165 foreach ($class->getMethods() as $method) {
1166 $name = $method->getName();
1167 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
1168 $this->joinMethods
[] = $name;
1172 if (!is_null($cond)) {
1173 if ($cond instanceof PlFilterCondition
) {
1174 $this->setCondition($cond);
1177 if (!is_null($sort)) {
1178 if ($sort instanceof UserFilterOrder
) {
1179 $this->addSort($sort);
1180 } else if (is_array($sort)) {
1181 foreach ($sort as $s) {
1188 private function buildQuery()
1190 if (is_null($this->orderby
)) {
1192 foreach ($this->sort
as $sort) {
1193 $orders = array_merge($orders, $sort->buildSort($this));
1195 if (count($orders) == 0) {
1196 $this->orderby
= '';
1198 $this->orderby
= 'ORDER BY ' . implode(', ', $orders);
1201 if (is_null($this->query
)) {
1202 $where = $this->root
->buildCondition($this);
1203 $joins = $this->buildJoins();
1204 if ($this->with_accounts
) {
1205 $from = 'accounts AS a';
1207 $this->requireProfiles();
1208 $from = 'profiles AS p';
1210 $this->query
= 'FROM ' . $from . '
1212 WHERE (' . $where . ')';
1216 private function getUIDList($uids = null
, PlLimit
&$limit)
1218 $this->requireAccounts();
1219 $this->buildQuery();
1220 $lim = $limit->getSql();
1222 if (!is_null($uids)) {
1223 $cond = ' AND a.uid IN ' . XDB
::formatArray($uids);
1225 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1226 ' . $this->query
. $cond . '
1228 ' . $this->orderby
. '
1230 $this->lastcount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
1234 /** Check that the user match the given rule.
1236 public function checkUser(PlUser
&$user)
1238 $this->requireAccounts();
1239 $this->buildQuery();
1240 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
1241 ' . $this->query
. XDB
::format(' AND a.uid = {?}', $user->id()));
1245 /** Filter a list of user to extract the users matching the rule.
1247 public function filter(array $users, PlLimit
&$limit)
1249 $this->requireAccounts();
1250 $this->buildQuery();
1253 foreach ($users as $user) {
1254 if ($user instanceof PlUser
) {
1260 $table[$uid] = $user;
1262 $fetched = $this->getUIDList($uids, $limit);
1264 foreach ($fetched as $uid) {
1265 $output[] = $table[$uid];
1270 public function getUIDs(PlLimit
&$limit)
1272 return $this->getUIDList(null
, $limit);
1275 public function getUsers(PlLimit
&$limit)
1277 return User
::getBulkUsersWithUIDs($this->getUIDs($limit));
1280 public function get(PlLimit
&$limit)
1282 return $this->getUsers($limit);
1285 public function getTotalCount()
1287 if (is_null($this->lastcount
)) {
1288 $this->buildQuery();
1289 if ($this->requireAccounts()) {
1294 return (int)XDB
::fetchOneCell('SELECT COUNT(DISTINCT ' . $field . ')
1297 return $this->lastcount
;
1301 public function setCondition(PlFilterCondition
&$cond)
1303 $this->root
=& $cond;
1304 $this->query
= null
;
1307 public function addSort(PlFilterOrder
&$sort)
1309 $this->sort
[] = $sort;
1310 $this->orderby
= null
;
1313 static public function getLegacy($promo_min, $promo_max)
1315 if ($promo_min != 0) {
1316 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
1318 $min = new UFC_True();
1320 if ($promo_max != 0) {
1321 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
1323 $max = new UFC_True();
1325 return new UserFilter(new PFC_And($min, $max));
1328 static public function sortByName()
1330 return array(new UFO_Name(self
::LASTNAME
), new UFO_Name(self
::FIRSTNAME
));
1333 static public function sortByPromo()
1335 return array(new UFO_Promo(), new UFO_Name(self
::LASTNAME
), new UFO_Name(self
::FIRSTNAME
));
1338 static private function getDBSuffix($string)
1340 return preg_replace('/[^a-z0-9]/i', '', $string);
1344 /** Stores a new (and unique) table alias in the &$table table
1345 * @param &$table Array in which the table alias must be stored
1346 * @param $val Value which will then be used to build the join
1347 * @return Name of the newly created alias
1349 private $option = 0;
1350 private function register_optional(array &$table, $val)
1352 if (is_null($val)) {
1353 $sub = $this->option++
;
1356 $sub = self
::getDBSuffix($val);
1360 $table[$sub] = $index;
1364 /** PROFILE VS ACCOUNT
1366 private $with_profiles = false
;
1367 private $with_accounts = false
;
1368 public function requireAccounts()
1370 $this->with_accounts
= true
;
1373 public function requireProfiles()
1375 $this->with_profiles
= true
;
1378 protected function accountJoins()
1381 if ($this->with_profiles
&& $this->with_accounts
) {
1382 $joins['ap'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)');
1383 $joins['p'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profiles', '$PID = ap.pid');
1390 const DISPLAY
= 'display';
1391 private $pd = false
;
1392 public function addDisplayFilter()
1394 $this->requireProfiles();
1399 protected function displayJoins()
1402 return array('pd' => new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_display', '$ME.pid = $PID'));
1411 const LASTNAME
= 'lastname';
1412 const FIRSTNAME
= 'firstname';
1413 const NICKNAME
= 'nickname';
1414 const PSEUDONYM
= 'pseudonym';
1415 const NAME
= 'name';
1417 const VN_MARITAL
= 'marital';
1418 const VN_ORDINARY
= 'ordinary';
1419 const VN_OTHER
= 'other';
1420 const VN_INI
= 'ini';
1422 const DN_FULL
= 'directory_name';
1423 const DN_DISPLAY
= 'yourself';
1424 const DN_YOURSELF
= 'yourself';
1425 const DN_DIRECTORY
= 'directory_name';
1426 const DN_PRIVATE
= 'private_name';
1427 const DN_PUBLIC
= 'public_name';
1428 const DN_SHORT
= 'short_name';
1429 const DN_SORT
= 'sort_name';
1431 static public $name_variants = array(
1432 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
1433 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
1436 static public function assertName($name)
1438 if (!Profile
::getNameTypeId($name)) {
1439 Platal
::page()->kill('Invalid name type: ' . $name);
1443 static public function isDisplayName($name)
1445 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
1446 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
1447 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1448 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1451 private $pn = array();
1452 public function addNameFilter($type, $variant = null
)
1454 $this->requireProfiles();
1455 if (!is_null($variant)) {
1456 $ft = $type . '_' . $variant;
1461 self
::assertName($ft);
1463 if (!is_null($variant) && $variant == 'other') {
1464 $sub .= $this->option++
;
1466 $this->pn
[$sub] = Profile
::getNameTypeId($ft);
1470 protected function nameJoins()
1473 foreach ($this->pn
as $sub => $type) {
1474 $joins['pn' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
1481 const GRADE_ING
= 'Ing.';
1482 const GRADE_PHD
= 'PhD';
1483 const GRADE_MST
= 'M%';
1484 static public function isGrade($grade)
1486 return $grade == self
::GRADE_ING ||
$grade == self
::GRADE_PHD ||
$grade == self
::GRADE_MST
;
1489 static public function assertGrade($grade)
1491 if (!self
::isGrade($grade)) {
1492 Platal
::page()->killError("Diplôme non valide");
1496 static public function promoYear($grade)
1498 // XXX: Definition of promotion for phds and masters might change in near future.
1499 return ($grade == UserFilter
::GRADE_ING
) ?
'entry_year' : 'grad_year';
1502 private $pepe = array();
1503 private $with_pee = false
;
1504 public function addEducationFilter($x = false
, $grade = null
)
1506 $this->requireProfiles();
1508 $index = $this->option
;
1509 $sub = $this->option++
;
1511 self
::assertGrade($grade);
1514 $this->with_pee
= true
;
1517 $this->pepe
[$index] = $sub;
1521 protected function educationJoins()
1524 if ($this->with_pee
) {
1525 $joins['pee'] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'profile_education_enum', 'pee.abbreviation = \'X\'');
1527 foreach ($this->pepe
as $grade => $sub) {
1528 if ($this->isGrade($grade)) {
1529 $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
1530 $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
1531 XDB
::format('{?}', $grade));
1533 $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_education', '$ME.uid = $PID');
1534 $joins['pee' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
1535 $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
1544 private $gpm = array();
1545 public function addGroupFilter($group = null
)
1547 $this->requireAccounts();
1548 if (!is_null($group)) {
1549 if (ctype_digit($group)) {
1550 $index = $sub = $group;
1553 $sub = self
::getDBSuffix($group);
1556 $sub = 'group_' . $this->option++
;
1560 $this->gpm
[$sub] = $index;
1564 protected function groupJoins()
1567 foreach ($this->gpm
as $sub => $key) {
1568 if (is_null($key)) {
1569 $joins['gpa' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'groupex.asso');
1570 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1571 } else if (ctype_digit($key)) {
1572 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
1574 $joins['gpa' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_INNER
, 'groupex.asso', XDB
::format('$ME.diminutif = {?}', $key));
1575 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1583 private $e = array();
1584 public function addEmailRedirectFilter($email = null
)
1586 $this->requireAccounts();
1587 return $this->register_optional($this->e
, $email);
1590 private $ve = array();
1591 public function addVirtualEmailFilter($email = null
)
1593 $this->addAliasFilter(self
::ALIAS_FORLIFE
);
1594 return $this->register_optional($this->ve
, $email);
1597 const ALIAS_BEST
= 'bestalias';
1598 const ALIAS_FORLIFE
= 'forlife';
1599 private $al = array();
1600 public function addAliasFilter($alias = null
)
1602 $this->requireAccounts();
1603 return $this->register_optional($this->al
, $alias);
1606 protected function emailJoins()
1610 foreach ($this->e
as $sub=>$key) {
1611 if (is_null($key)) {
1612 $joins['e' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
1614 $joins['e' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'emails', XDB
::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
1617 foreach ($this->al
as $sub=>$key) {
1618 if (is_null($key)) {
1619 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
1620 } else if ($key == self
::ALIAS_BEST
) {
1621 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
1622 } else if ($key == self
::ALIAS_FORLIFE
) {
1623 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
1625 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'aliases', XDB
::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
1628 foreach ($this->ve
as $sub=>$key) {
1629 if (is_null($key)) {
1630 $joins['v' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'virtual', '$ME.type = \'user\'');
1632 $joins['v' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'virtual', XDB
::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
1634 $joins['vr' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'virtual_redirect', XDB
::format('$ME.vid = v' . $sub . '.vid
1635 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
1636 CONCAT(al_forlife.alias, \'@\', {?}),
1638 $globals->mail
->domain
, $globals->mail
->domain2
));
1646 private $with_pa = false
;
1647 public function addAddressFilter()
1649 $this->requireProfiles();
1650 $this->with_pa
= true
;
1654 protected function addressJoins()
1657 if ($this->with_pa
) {
1658 $joins['pa'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_address', '$ME.pid = $PID');
1667 private $pc = false
;
1668 private $pce = array();
1669 private $pcr = false
;
1670 public function addCorpsFilter($type)
1672 $this->requireProfiles();
1674 if ($type == UFC_Corps
::CURRENT
) {
1675 $pce['pcec'] = 'current_corpsid';
1677 } else if ($type == UFC_Corps
::ORIGIN
) {
1678 $pce['pceo'] = 'original_corpsid';
1683 public function addCorpsRankFilter()
1685 $this->requireProfiles();
1691 protected function corpsJoins()
1695 $joins['pc'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_corps', '$ME.uid = $UID');
1698 $joins['pcr'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_corps_rank_enum', '$ME.id = pc.rankid');
1700 foreach($this->pce
as $sub => $field) {
1701 $joins[$sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_corps_enum', '$ME.id = pc.' . $field);
1709 const JOB_SECTOR
= 1;
1710 const JOB_SUBSECTOR
= 2;
1711 const JOB_SUBSUBSECTOR
= 4;
1712 const JOB_ALTERNATES
= 8;
1713 const JOB_USERDEFINED
= 16;
1717 * pje => profile_job_enum
1718 * pjse => profile_job_sector_enum
1719 * pjsse => profile_job_subsector_enum
1720 * pjssse => profile_job_subsubsector_enum
1721 * pja => profile_job_alternates
1723 private $with_pj = false
;
1724 private $with_pje = false
;
1725 private $with_pjse = false
;
1726 private $with_pjsse = false
;
1727 private $with_pjssse = false
;
1728 private $with_pja = false
;
1730 public function addJobFilter()
1732 $this->requireProfiles();
1733 $this->with_pj
= true
;
1737 public function addJobCompanyFilter()
1739 $this->addJobFilter();
1740 $this->with_pje
= true
;
1744 public function addJobSectorizationFilter($type)
1746 $this->addJobFilter();
1747 if ($type == self
::JOB_SECTOR
) {
1748 $this->with_pjse
= true
;
1750 } else if ($type == self
::JOB_SUBSECTOR
) {
1751 $this->with_pjsse
= true
;
1753 } else if ($type == self
::JOB_SUBSUBSECTOR
) {
1754 $this->with_pjssse
= true
;
1756 } else if ($type == self
::JOB_ALTERNATES
) {
1757 $this->with_pja
= true
;
1762 protected function jobJoins()
1765 if ($this->with_pj
) {
1766 $joins['pj'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job', '$ME.uid = $UID');
1768 if ($this->with_pje
) {
1769 $joins['pje'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job_enum', '$ME.id = pj.jobid');
1771 if ($this->with_pjse
) {
1772 $joins['pjse'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job_sector_enum', '$ME.id = pj.sectorid');
1774 if ($this->with_pjsse
) {
1775 $joins['pjsse'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job_subsector_enum', '$ME.id = pj.subsectorid');
1777 if ($this->with_pjssse
) {
1778 $joins['pjssse'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
1780 if ($this->with_pja
) {
1781 $joins['pja'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
1789 private $with_pnw = false
;
1790 public function addNetworkingFilter()
1792 $this->requireAccounts();
1793 $this->with_pnw
= true
;
1797 protected function networkingJoins()
1800 if ($this->with_pnw
) {
1801 $joins['pnw'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_networking', '$ME.uid = $UID');
1809 private $with_ptel = false
;
1811 public function addPhoneFilter()
1813 $this->requireAccounts();
1814 $this->with_ptel
= true
;
1818 protected function phoneJoins()
1821 if ($this->with_ptel
) {
1822 $joins['ptel'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_phones', '$ME.uid = $UID');
1830 private $with_pmed = false
;
1831 public function addMedalFilter()
1833 $this->requireProfiles();
1834 $this->with_pmed
= true
;
1838 protected function medalJoins()
1841 if ($this->with_pmed
) {
1842 $joins['pmed'] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'profile_medals_sub', '$ME.uid = $UID');
1850 private $pms = array();
1851 const MENTOR_EXPERTISE
= 1;
1852 const MENTOR_COUNTRY
= 2;
1853 const MENTOR_SECTOR
= 3;
1855 public function addMentorFilter($type)
1857 $this->requireAccounts();
1859 case MENTOR_EXPERTISE
:
1860 $pms['pme'] = 'profile_mentor';
1862 case MENTOR_COUNTRY
:
1863 $pms['pmc'] = 'profile_mentor_country';
1866 $pms['pms'] = 'profile_mentor_sector';
1869 Platal
::page()->killError("Undefined mentor filter.");
1873 protected function mentorJoins()
1876 foreach ($this->pms
as $sub => $tab) {
1877 $joins[$sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, $tab, '$ME.uid = $UID');
1884 private $cts = array();
1885 public function addContactFilter($uid = null
)
1887 $this->requireAccounts();
1888 return $this->register_optional($this->cts
, is_null($uid) ? null
: 'user_' . $uid);
1891 protected function contactJoins()
1894 foreach ($this->cts
as $sub=>$key) {
1895 if (is_null($key)) {
1896 $joins['c' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'contacts', '$ME.contact = $UID');
1898 $joins['c' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'contacts', XDB
::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
1907 private $wn = array();
1908 public function addWatchRegistrationFilter($uid = null
)
1910 $this->requireAccounts();
1911 return $this->register_optional($this->wn
, is_null($uid) ? null
: 'user_' . $uid);
1914 private $wp = array();
1915 public function addWatchPromoFilter($uid = null
)
1917 $this->requireAccounts();
1918 return $this->register_optional($this->wp
, is_null($uid) ? null
: 'user_' . $uid);
1921 private $w = array();
1922 public function addWatchFilter($uid = null
)
1924 $this->requireAccounts();
1925 return $this->register_optional($this->w
, is_null($uid) ? null
: 'user_' . $uid);
1928 protected function watchJoins()
1931 foreach ($this->w
as $sub=>$key) {
1932 if (is_null($key)) {
1933 $joins['w' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch');
1935 $joins['w' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch', XDB
::format('$ME.uid = {?}', substr($key, 5)));
1938 foreach ($this->wn
as $sub=>$key) {
1939 if (is_null($key)) {
1940 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_nonins', '$ME.ni_id = $UID');
1942 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_nonins', XDB
::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1945 foreach ($this->wn
as $sub=>$key) {
1946 if (is_null($key)) {
1947 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_nonins', '$ME.ni_id = $UID');
1949 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_nonins', XDB
::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1952 foreach ($this->wp
as $sub=>$key) {
1953 if (is_null($key)) {
1954 $joins['wp' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_promo');
1956 $joins['wp' . $sub] = new PlSqlJoin(PlSqlJoin
::MODE_LEFT
, 'watch_promo', XDB
::format('$ME.uid = {?}', substr($key, 5)));
1964 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: