bbaceba4179b2fc0b7bd51a94ce8348bcc0ed7de
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 ***************************************************************************/
22 require_once dirname(__FILE__
) . '/userfilter/conditions.inc.php';
23 require_once dirname(__FILE__
) . '/userfilter/orders.inc.php';
25 /***********************************
26 *********************************
28 *********************************
29 ***********************************/
31 // {{{ class UserFilter
32 /** This class provides a convenient and centralized way of filtering users.
35 * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t));
37 * Resulting UserFilter can be used to:
38 * - get a list of User objects matching the filter
39 * - get a list of UIDs matching the filter
40 * - get the number of users matching the filter
41 * - check whether a given User matches the filter
42 * - filter a list of User objects depending on whether they match the filter
44 * Usage for UFC and UFO objects:
45 * A UserFilter will call all private functions named XXXJoins.
46 * These functions must return an array containing the list of join
47 * required by the various UFC and UFO associated to the UserFilter.
48 * Entries in those returned array are of the following form:
49 * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter')
50 * which will be translated into :
51 * join_type JOIN joined_table AS join_tablealias ON (join_criter)
54 * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with
55 * profile.pid, and $UID with accounts.uid.
57 * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined;
58 * its parameter will be used to set various private vars of the UserFilter describing
59 * the required joins ; such a function shall return the "join_tablealias" to use
60 * when referring to the joined table.
62 * For example, if data from profile_job must be available to filter results,
63 * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and
64 * return 'pj', the short name to use when referring to profile_job; when building
65 * the query, calling the jobJoins function will return an array containing a single
67 * 'pj' => array('left', 'profile_job', '$ME.pid = $UID');
69 * The 'register_optional' function can be used to generate unique table aliases when
70 * the same table has to be joined several times with different aliases.
72 class UserFilter
extends PlFilter
74 protected $joinMethods = array();
76 protected $joinMetas = array(
82 private $sort = array();
83 private $grouper = null
;
84 private $query = null
;
85 private $orderby = null
;
87 // Store the current 'search' visibility.
88 private $profile_visibility = null
;
90 private $lastusercount = null
;
91 private $lastprofilecount = null
;
93 public function __construct($cond = null
, $sort = null
)
95 if (empty($this->joinMethods
)) {
96 $class = new ReflectionClass('UserFilter');
97 foreach ($class->getMethods() as $method) {
98 $name = $method->getName();
99 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
100 $this->joinMethods
[] = $name;
104 if (!is_null($cond)) {
105 if ($cond instanceof PlFilterCondition
) {
106 $this->setCondition($cond);
109 if (!is_null($sort)) {
110 if ($sort instanceof PlFilterOrder
) {
111 $this->addSort($sort);
112 } else if (is_array($sort)) {
113 foreach ($sort as $s) {
119 // This will set the visibility to the default correct level.
120 $this->profile_visibility
= ProfileVisibility
::defaultForRead();
123 public function isVisible($level)
125 return $this->profile_visibility
->isVisible($level);
128 public function getVisibilityLevel()
130 return $this->profile_visibility
->level();
133 public function getVisibilityCondition($field)
135 return XDB
::format($field . ' >= {?}', $this->getVisibilityLevel());
138 private function buildQuery()
140 // The root condition is built first because some orders need info
141 // available only once all UFC have set their conditions (UFO_Score)
142 if (is_null($this->query
)) {
143 $where = $this->root
->buildCondition($this);
144 $where = str_replace(array_keys($this->joinMetas
),
148 if (is_null($this->orderby
)) {
150 foreach ($this->sort
as $sort) {
151 $orders = array_merge($orders, $sort->buildSort($this));
153 if (count($orders) == 0) {
156 $this->orderby
= 'ORDER BY ' . implode(', ', $orders);
158 $this->orderby
= str_replace(array_keys($this->joinMetas
),
162 if (is_null($this->query
)) {
163 if ($this->with_accounts
) {
164 $from = 'accounts AS a';
166 $this->requireProfiles();
167 $from = 'profiles AS p';
169 $joins = $this->buildJoins();
170 $this->query
= 'FROM ' . $from . '
172 WHERE (' . $where . ')';
176 public function hasGroups()
178 return $this->grouper
!= null
;
181 public function getGroups()
183 return $this->getUIDGroups();
186 public function getUIDGroups()
188 $this->requireAccounts();
190 $token = $this->grouper
->getGroupToken($this);
192 $groups = XDB
::rawFetchAllRow('SELECT ' . $token . ', COUNT(a.uid)
199 public function getPIDGroups()
201 $this->requireProfiles();
203 $token = $this->grouper
->getGroupToken($this);
205 $groups = XDB
::rawFetchAllRow('SELECT ' . $token . ', COUNT(p.pid)
212 private function getUIDList($uids = null
, PlLimit
$limit)
214 $this->requireAccounts();
216 $lim = $limit->getSql();
219 $cond = XDB
::format(' AND a.uid IN {?}', $uids);
221 $fetched = XDB
::rawFetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
222 ' . $this->query
. $cond . '
224 ' . $this->orderby
. '
226 $this->lastusercount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
230 private function getPIDList($pids = null
, PlLimit
$limit)
232 $this->requireProfiles();
234 $lim = $limit->getSql();
236 if (!is_null($pids)) {
237 $cond = XDB
::format(' AND p.pid IN {?}', $pids);
239 $fetched = XDB
::rawFetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid
240 ' . $this->query
. $cond . '
242 ' . $this->orderby
. '
244 $this->lastprofilecount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
248 private static function defaultLimit($limit) {
249 if ($limit == null
) {
250 return new PlLimit();
256 /** Check that the user match the given rule.
258 public function checkUser(PlUser
$user)
260 $this->requireAccounts();
262 $count = (int)XDB
::rawFetchOneCell('SELECT COUNT(*)
264 . XDB
::format(' AND a.uid = {?}', $user->id()));
268 /** Check that the profile match the given rule.
270 public function checkProfile(Profile
$profile)
272 $this->requireProfiles();
274 $count = (int)XDB
::rawFetchOneCell('SELECT COUNT(*)
276 . XDB
::format(' AND p.pid = {?}', $profile->id()));
280 /** Default filter is on users
282 public function filter(array $users, $limit = null
)
284 return $this->filterUsers($users, self
::defaultLimit($limit));
287 /** Filter a list of users to extract the users matching the rule.
289 public function filterUsers(array $users, $limit = null
)
291 $limit = self
::defaultLimit($limit);
292 $this->requireAccounts();
296 foreach ($users as $user) {
297 if ($user instanceof PlUser
) {
303 $table[$uid] = $user;
305 $fetched = $this->getUIDList($uids, $limit);
307 foreach ($fetched as $uid) {
308 $output[] = $table[$uid];
313 /** Filter a list of profiles to extract the users matching the rule.
315 public function filterProfiles(array $profiles, $limit = null
)
317 $limit = self
::defaultLimit($limit);
318 $this->requireProfiles();
322 foreach ($profiles as $profile) {
323 if ($profile instanceof Profile
) {
324 $pid = $profile->id();
329 $table[$pid] = $profile;
331 $fetched = $this->getPIDList($pids, $limit);
333 foreach ($fetched as $pid) {
334 $output[] = $table[$pid];
339 public function getUIDs($limit = null
)
341 $limit = self
::defaultLimit($limit);
342 return $this->getUIDList(null
, $limit);
345 public function getUID($pos = 0)
347 $uids =$this->getUIDList(null
, new PlLimit(1, $pos));
348 if (count($uids) == 0) {
355 public function getPIDs($limit = null
)
357 $limit = self
::defaultLimit($limit);
358 return $this->getPIDList(null
, $limit);
361 public function getPID($pos = 0)
363 $pids =$this->getPIDList(null
, new PlLimit(1, $pos));
364 if (count($pids) == 0) {
371 public function getUsers($limit = null
)
373 return User
::getBulkUsersWithUIDs($this->getUIDs($limit));
376 public function getUser($pos = 0)
378 $uid = $this->getUID($pos);
382 return User
::getWithUID($uid);
386 public function iterUsers($limit = null
)
388 return User
::iterOverUIDs($this->getUIDs($limit));
391 public function getProfiles($limit = null
, $fields = 0x0000, ProfileVisibility
$visibility = null
)
393 return Profile
::getBulkProfilesWithPIDs($this->getPIDs($limit), $fields, $visibility);
396 public function getProfile($pos = 0, $fields = 0x0000, ProfileVisibility
$visibility = null
)
398 $pid = $this->getPID($pos);
402 return Profile
::get($pid, $fields, $visibility);
406 public function iterProfiles($limit = null
, $fields = 0x0000, ProfileVisibility
$visibility = null
)
408 return Profile
::iterOverPIDs($this->getPIDs($limit), true
, $fields, $visibility);
411 public function get($limit = null
)
413 return $this->getUsers($limit);
416 public function getIds($limit = null
)
418 return $this->getUIDs();
421 public function getTotalCount()
423 return $this->getTotalUserCount();
426 public function getTotalUserCount()
428 if (is_null($this->lastusercount
)) {
429 $this->requireAccounts();
431 return (int)XDB
::rawFetchOneCell('SELECT COUNT(DISTINCT a.uid)
434 return $this->lastusercount
;
438 public function getTotalProfileCount()
440 if (is_null($this->lastprofilecount
)) {
441 $this->requireProfiles();
443 return (int)XDB
::rawFetchOneCell('SELECT COUNT(DISTINCT p.pid)
446 return $this->lastprofilecount
;
450 public function setCondition(PlFilterCondition
$cond)
452 $this->root
=& $cond;
456 public function addSort(PlFilterOrder
$sort)
458 if (count($this->sort
) == 0 && $sort instanceof PlFilterGroupableOrder
)
460 $this->grouper
= $sort;
462 $this->sort
[] = $sort;
463 $this->orderby
= null
;
466 public function export()
468 $export = array('conditions' => $this->root
->export());
469 if (!empty($this->sort
)) {
470 $export['sorts'] = array();
471 foreach ($this->sort
as $sort) {
472 $export['sorts'][] = $sort->export();
478 public function exportConditions()
480 return $this->root
->export();
483 public static function fromExport(array $export)
485 $export = new PlDict($export);
486 if (!$export->has('conditions')) {
487 throw new Exception("Cannot build a user filter without conditions");
489 $cond = UserFilterCondition
::fromExport($export->v('conditions'));
491 if ($export->has('sorts')) {
493 foreach ($export->v('sorts') as $sort) {
494 $sorts[] = UserFilterOrder
::fromExport($sort);
497 return new UserFilter($cond, $sorts);
500 public static function fromJSon($json)
502 $export = json_decode($json, true
);
503 if (is_null($export)) {
504 throw new Exception("Invalid json: $json");
506 return self
::fromExport($json);
509 public static function fromExportedConditions(array $export)
511 $cond = UserFilterCondition
::fromExport($export);
512 return new UserFilter($cond);
515 public static function fromJSonConditions($json)
517 $export = json_decode($json, true
);
518 if (is_null($export)) {
519 throw new Exception("Invalid json: $json");
521 return self
::fromExportedConditions($json);
524 static public function getLegacy($promo_min, $promo_max)
526 if ($promo_min != 0) {
527 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
529 $min = new PFC_True();
531 if ($promo_max != 0) {
532 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
534 $max = new PFC_True();
536 return new UserFilter(new PFC_And($min, $max));
539 static public function sortByName()
541 return array(new UFO_Name());
544 static public function sortByPromo()
546 return array(new UFO_Promo(), new UFO_Name());
549 static private function getDBSuffix($string)
551 if (is_array($string)) {
552 if (count($string) == 1) {
553 return self
::getDBSuffix(array_pop($string));
555 return md5(implode('|', $string));
557 return preg_replace('/[^a-z0-9]/i', '', $string);
562 /** Stores a new (and unique) table alias in the &$table table
563 * @param &$table Array in which the table alias must be stored
564 * @param $val Value which will then be used to build the join
565 * @return Name of the newly created alias
568 private function register_optional(array &$table, $val)
571 $sub = $this->option++
;
574 $sub = self
::getDBSuffix($val);
578 $table[$sub] = $index;
582 /** PROFILE VS ACCOUNT
584 private $with_profiles = false
;
585 private $with_accounts = false
;
586 public function requireAccounts()
588 $this->with_accounts
= true
;
591 public function accountsRequired()
593 return $this->with_accounts
;
596 public function requireProfiles()
598 $this->with_profiles
= true
;
601 public function profilesRequired()
603 return $this->with_profiles
;
606 protected function accountJoins()
609 if ($this->with_profiles
&& $this->with_accounts
) {
610 $joins['ap'] = PlSqlJoin
::left('account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)');
611 $joins['p'] = PlSqlJoin
::left('profiles', '$PID = ap.pid');
619 public function requirePerms()
621 $this->requireAccounts();
626 protected function permJoins()
629 return array('at' => PlSqlJoin
::left('account_types', '$ME.type = a.type'));
637 const DISPLAY
= 'display';
639 public function addDisplayFilter()
641 $this->requireProfiles();
646 protected function displayJoins()
649 return array('pd' => PlSqlJoin
::left('profile_display', '$ME.pid = $PID'));
658 private $with_logger = false
;
659 public function addLoggerFilter()
661 $this->with_logger
= true
;
662 $this->requireAccounts();
665 protected function loggerJoins()
668 if ($this->with_logger
) {
669 $joins['ls'] = PlSqlJoin
::left('log_sessions', '$ME.uid = $UID');
676 private $name_tokens = array();
677 private $nb_tokens = 0;
679 public function addNameTokensFilter($token)
681 $this->requireProfiles();
682 $sub = 'sn' . (1 +
$this->nb_tokens
);
684 $this->name_tokens
[$sub] = $token;
688 protected function nameTokensJoins()
690 /* We don't return joins, since with_sn forces the SELECT to run on search_name first */
692 foreach ($this->name_tokens
as $sub => $token) {
693 $joins[$sub] = PlSqlJoin
::left('search_name', '$ME.pid = $PID');
698 public function getNameTokens()
700 return $this->name_tokens
;
706 private $with_nat = false
;
707 public function addNationalityFilter()
709 $this->with_nat
= true
;
713 protected function nationalityJoins()
716 if ($this->with_nat
) {
717 $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');
724 const GRADE_ING
= Profile
::DEGREE_X
;
725 const GRADE_PHD
= Profile
::DEGREE_D
;
726 const GRADE_MST
= Profile
::DEGREE_M
;
727 static public function isGrade($grade)
729 return ($grade !== 0) && ($grade == self
::GRADE_ING ||
$grade == self
::GRADE_PHD ||
$grade == self
::GRADE_MST
);
732 static public function assertGrade($grade)
734 if (!self
::isGrade($grade)) {
735 Platal
::page()->killError("Diplôme non valide: $grade");
739 static public function promoYear($grade)
741 // XXX: Definition of promotion for phds and masters might change in near future.
742 return ($grade == UserFilter
::GRADE_ING
) ?
'entry_year' : 'grad_year';
745 private $pepe = array();
746 private $with_pee = false
;
747 public function addEducationFilter($x = false
, $grade = null
)
749 $this->requireProfiles();
751 $index = $this->option
;
752 $sub = $this->option++
;
754 self
::assertGrade($grade);
757 $this->with_pee
= true
;
760 $this->pepe
[$index] = $sub;
764 protected function educationJoins()
767 if ($this->with_pee
) {
768 $joins['pee'] = PlSqlJoin
::inner('profile_education_enum', 'pee.abbreviation = \'X\'');
770 foreach ($this->pepe
as $grade => $sub) {
771 if ($this->isGrade($grade)) {
772 $joins['pe' . $sub] = PlSqlJoin
::left('profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID');
773 $joins['pede' . $sub] = PlSqlJoin
::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.degree LIKE {?}', $grade);
775 $joins['pe' . $sub] = PlSqlJoin
::left('profile_education', '$ME.pid = $PID');
776 $joins['pee' . $sub] = PlSqlJoin
::inner('profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
777 $joins['pede' . $sub] = PlSqlJoin
::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
786 private $gpm = array();
787 public function addGroupFilter($group = null
)
789 $this->requireAccounts();
790 if (!is_null($group)) {
791 if (is_int($group) ||
ctype_digit($group)) {
792 $index = $sub = $group;
795 $sub = self
::getDBSuffix($group);
798 $sub = 'group_' . $this->option++
;
802 $this->gpm
[$sub] = $index;
806 private $gpfm = array();
807 public function addGroupFormerMemberFilter()
809 $this->requireAccounts();
810 $sub = '_' . $this->option++
;
811 $this->gpfm
[] = $sub;
815 protected function groupJoins()
818 foreach ($this->gpm
as $sub => $key) {
820 $joins['gpa' . $sub] = PlSqlJoin
::inner('groups');
821 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
822 } else if (is_int($key) ||
ctype_digit($key)) {
823 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
825 $joins['gpa' . $sub] = PlSqlJoin
::inner('groups', '$ME.diminutif = {?}', $key);
826 $joins['gpm' . $sub] = PlSqlJoin
::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
829 foreach ($this->gpfm
as $sub) {
830 $joins['gpfm' . $sub] = PlSqlJoin
::left('group_former_members', '$ME.uid = $UID');
837 private $nls = array();
838 public function addNewsLetterFilter($nlid)
840 $this->requireAccounts();
841 $sub = 'nl_' . $nlid;
842 $this->nls
[$nlid] = $sub;
846 protected function newsLetterJoins()
849 foreach ($this->nls
as $key => $sub) {
850 $joins[$sub] = PlSqlJoin
::left('newsletter_ins', '$ME.nlid = {?} AND $ME.uid = $UID', $key);
858 private $with_bi = false
;
859 private $with_bd = false
;
860 public function addBinetsFilter($with_enum = false
)
862 $this->requireProfiles();
863 $this->with_bi
= true
;
865 $this->with_bd
= true
;
872 protected function binetsJoins()
875 if ($this->with_bi
) {
876 $joins['bi'] = PlSqlJoin
::left('profile_binets', '$ME.pid = $PID');
878 if ($this->with_bd
) {
879 $joins['bd'] = PlSqlJoin
::left('profile_binet_enum', '$ME.id = bi.binet_id');
886 private $ra = array();
887 /** Allows filtering by redirection.
888 * @param $email If null, enable a left join on the email redirection table
889 * (email_redirect_account); otherwise, perform a left join on users having
890 * that email as a redirection.
891 * @return Suffix to use to access the adequate table.
893 public function addEmailRedirectFilter($email = null
)
895 $this->requireAccounts();
896 return $this->register_optional($this->ra
, $email);
899 const ALIAS_BEST
= 'bestalias';
900 const ALIAS_FORLIFE
= 'forlife';
901 const ALIAS_AUXILIARY
= 'alias_aux';
902 private $sa = array();
903 /** Allows filtering by source email.
904 * @param $email If null, enable a left join on the email source table
905 * (email_source_account); otherwise, perform a left join on users having
906 * that email as a source email.
907 * @return Suffix to use to access the adequate table.
909 public function addAliasFilter($email = null
)
911 $this->requireAccounts();
912 return $this->register_optional($this->sa
, $email);
915 private $with_rf = false
;
916 /** Allows filtering by active redirection.
917 * @return Suffix to use to access the adequate table.
919 public function addActiveEmailRedirectFilter($email = null
)
921 $this->requireAccounts();
922 $this->with_rf
= true
;
925 protected function emailJoins()
929 foreach ($this->ra
as $sub => $redirections) {
930 if (is_null($redirections)) {
931 $joins['ra' . $sub] = PlSqlJoin
::left('email_redirect_account', '$ME.uid = $UID AND $ME.type != \'imap\'');
933 if (!is_array($redirections)) {
934 $key = array($redirections);
936 $joins['ra' . $sub] = PlSqlJoin
::left('email_redirect_account', '$ME.uid = $UID AND $ME.type != \'imap\'
937 AND $ME.redirect IN {?}', $redirections);
940 foreach ($this->sa
as $sub => $emails) {
941 if (is_null($emails)) {
942 $joins['sa' . $sub] = PlSqlJoin
::left('email_source_account', '$ME.uid = $UID');
943 } else if ($sub == self
::ALIAS_BEST
) {
944 $joins['sa' . $sub] = PlSqlJoin
::left('email_source_account', '$ME.uid = $UID AND FIND_IN_SET(\'bestalias\', $ME.flags)');
945 } else if ($sub == self
::ALIAS_FORLIFE
) {
946 $joins['sa' . $sub] = PlSqlJoin
::left('email_source_account', '$ME.uid = $UID AND $ME.type = \'forlife\'');
947 } else if ($sub == self
::ALIAS_AUXILIARY
) {
948 $joins['sa' . $sub] = PlSqlJoin
::left('email_source_account', '$ME.uid = $UID AND $ME.type = \'alias_aux\'');
950 if (!is_array($emails)) {
951 $key = array($emails);
953 $joins['sa' . $sub] = PlSqlJoin
::left('email_source_account', '$ME.uid = $UID AND $ME.email IN {?}', $emails);
956 if ($this->with_rf
) {
957 $joins['rf'] = PlSqlJoin
::left('email_redirect_account', '$ME.uid = $UID AND $ME.type != \'imap\' AND $ME.flags = \'active\'');;
965 private $types = array();
966 public function addAddressFilter($type)
968 $this->requireProfiles();
969 $this->with_pa
= true
;
971 $sub = '_' . $this->option++
;
972 $this->types
[$type] = $sub;
976 protected function addressJoins()
979 foreach ($this->types
as $type => $sub) {
980 $joins['pa' . $sub] = PlSqlJoin
::inner('profile_addresses', '$ME.pid = $PID');
981 $joins['pac' . $sub] = PlSqlJoin
::inner('profile_addresses_components',
982 '$ME.pid = pa' . $sub . '.pid AND $ME.jobid = pa' . $sub . '.jobid AND $ME.groupid = pa' . $sub . '.groupid AND $ME.type = pa' . $sub . '.type AND $ME.id = pa' . $sub . '.id');
983 $joins['pace' . $sub] = PlSqlJoin
::inner('profile_addresses_components_enum',
984 '$ME.id = pac' . $sub . '.component_id AND FIND_IN_SET({?}, $ME.types)', $type);
995 private $pce = array();
996 private $pcr = false
;
997 public function addCorpsFilter($type)
999 $this->requireProfiles();
1001 if ($type == UFC_Corps
::CURRENT
) {
1002 $this->pce
['pcec'] = 'current_corpsid';
1004 } else if ($type == UFC_Corps
::ORIGIN
) {
1005 $this->pce
['pceo'] = 'original_corpsid';
1010 public function addCorpsRankFilter()
1012 $this->requireProfiles();
1018 protected function corpsJoins()
1022 $joins['pc'] = PlSqlJoin
::left('profile_corps', '$ME.pid = $PID');
1025 $joins['pcr'] = PlSqlJoin
::left('profile_corps_rank_enum', '$ME.id = pc.rankid');
1027 foreach($this->pce
as $sub => $field) {
1028 $joins[$sub] = PlSqlJoin
::left('profile_corps_enum', '$ME.id = pc.' . $field);
1036 const JOB_USERDEFINED
= 0x0001;
1037 const JOB_CV
= 0x0002;
1038 const JOB_ANY
= 0x0003;
1042 * pje => profile_job_enum
1043 * pjt => profile_job_terms
1045 private $with_pj = false
;
1046 private $with_pje = false
;
1047 private $with_pjt = 0;
1049 public function addJobFilter()
1051 $this->requireProfiles();
1052 $this->with_pj
= true
;
1056 public function addJobCompanyFilter()
1058 $this->addJobFilter();
1059 $this->with_pje
= true
;
1064 * Adds a filter on job terms of profile.
1065 * @param $nb the number of job terms to use
1066 * @return an array of the fields to filter (one for each term).
1068 public function addJobTermsFilter($nb = 1)
1070 $this->with_pjt
= $nb;
1071 $jobtermstable = array();
1072 for ($i = 1; $i <= $nb; ++
$i) {
1073 $jobtermstable[] = 'pjtr_'.$i;
1075 return $jobtermstable;
1078 protected function jobJoins()
1081 if ($this->with_pj
) {
1082 $joins['pj'] = PlSqlJoin
::left('profile_job', '$ME.pid = $PID');
1084 if ($this->with_pje
) {
1085 $joins['pje'] = PlSqlJoin
::left('profile_job_enum', '$ME.id = pj.jobid');
1087 if ($this->with_pjt
> 0) {
1088 for ($i = 1; $i <= $this->with_pjt
; ++
$i) {
1089 $joins['pjt_'.$i] = PlSqlJoin
::left('profile_job_term', '$ME.pid = $PID');
1090 $joins['pjtr_'.$i] = PlSqlJoin
::left('profile_job_term_relation', '$ME.jtid_2 = pjt_'.$i.'.jtid');
1099 private $with_pnw = false
;
1100 public function addNetworkingFilter()
1102 $this->requireAccounts();
1103 $this->with_pnw
= true
;
1107 protected function networkingJoins()
1110 if ($this->with_pnw
) {
1111 $joins['pnw'] = PlSqlJoin
::left('profile_networking', '$ME.pid = $PID');
1119 private $with_ptel = false
;
1121 public function addPhoneFilter()
1123 $this->requireAccounts();
1124 $this->with_ptel
= true
;
1128 protected function phoneJoins()
1131 if ($this->with_ptel
) {
1132 $joins['ptel'] = PlSqlJoin
::left('profile_phones', '$ME.pid = $PID');
1140 private $with_pmed = false
;
1141 public function addMedalFilter()
1143 $this->requireProfiles();
1144 $this->with_pmed
= true
;
1148 protected function medalJoins()
1151 if ($this->with_pmed
) {
1152 $joins['pmed'] = PlSqlJoin
::left('profile_medals', '$ME.pid = $PID');
1159 private $dts = array();
1161 const DELTATEN_MESSAGE
= 2;
1164 public function addDeltaTenFilter($type)
1166 $this->requireProfiles();
1168 case self
::DELTATEN
:
1169 $this->dts
['pdt'] = 'profile_deltaten';
1171 case self
::DELTATEN_MESSAGE
:
1172 $this->dts
['pdtm'] = 'profile_deltaten';
1175 Platal
::page()->killError("Undefined DeltaTen filter.");
1179 protected function deltatenJoins()
1182 foreach ($this->dts
as $sub => $tab) {
1183 $joins[$sub] = PlSqlJoin
::left($tab, '$ME.pid = $PID');
1191 private $pms = array();
1192 private $mjtr = false
;
1194 const MENTOR_EXPERTISE
= 2;
1195 const MENTOR_COUNTRY
= 3;
1196 const MENTOR_TERM
= 4;
1198 public function addMentorFilter($type)
1200 $this->requireProfiles();
1203 $this->pms
['pm'] = 'profile_mentor';
1205 case self
::MENTOR_EXPERTISE
:
1206 $this->pms
['pme'] = 'profile_mentor';
1208 case self
::MENTOR_COUNTRY
:
1209 $this->pms
['pmc'] = 'profile_mentor_country';
1211 case self
::MENTOR_TERM
:
1212 $this->pms
['pmt'] = 'profile_mentor_term';
1216 Platal
::page()->killError("Undefined mentor filter.");
1220 protected function mentorJoins()
1223 foreach ($this->pms
as $sub => $tab) {
1224 $joins[$sub] = PlSqlJoin
::left($tab, '$ME.pid = $PID');
1227 $joins['mjtr'] = PlSqlJoin
::left('profile_job_term_relation', '$ME.jtid_2 = pmt.jtid');
1234 private $cts = array();
1235 public function addContactFilter($uid = null
)
1237 $this->requireProfiles();
1238 return $this->register_optional($this->cts
, is_null($uid) ? null
: 'user_' . $uid);
1241 protected function contactJoins()
1244 foreach ($this->cts
as $sub=>$key) {
1245 if (is_null($key)) {
1246 $joins['c' . $sub] = PlSqlJoin
::left('contacts', '$ME.contact = $PID');
1248 $joins['c' . $sub] = PlSqlJoin
::left('contacts', '$ME.uid = {?} AND $ME.contact = $PID', substr($key, 5));
1257 private $wn = array();
1258 public function addWatchRegistrationFilter($uid = null
)
1260 $this->requireAccounts();
1261 return $this->register_optional($this->wn
, is_null($uid) ? null
: 'user_' . $uid);
1264 private $wp = array();
1265 public function addWatchPromoFilter($uid = null
)
1267 $this->requireAccounts();
1268 return $this->register_optional($this->wp
, is_null($uid) ? null
: 'user_' . $uid);
1271 private $w = array();
1272 public function addWatchFilter($uid = null
)
1274 $this->requireAccounts();
1275 return $this->register_optional($this->w
, is_null($uid) ? null
: 'user_' . $uid);
1278 protected function watchJoins()
1281 foreach ($this->w
as $sub=>$key) {
1282 if (is_null($key)) {
1283 $joins['w' . $sub] = PlSqlJoin
::left('watch');
1285 $joins['w' . $sub] = PlSqlJoin
::left('watch', '$ME.uid = {?}', substr($key, 5));
1288 foreach ($this->wn
as $sub=>$key) {
1289 if (is_null($key)) {
1290 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.ni_id = $UID');
1292 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
1295 foreach ($this->wn
as $sub=>$key) {
1296 if (is_null($key)) {
1297 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.ni_id = $UID');
1299 $joins['wn' . $sub] = PlSqlJoin
::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
1302 foreach ($this->wp
as $sub=>$key) {
1303 if (is_null($key)) {
1304 $joins['wp' . $sub] = PlSqlJoin
::left('watch_promo');
1306 $joins['wp' . $sub] = PlSqlJoin
::left('watch_promo', '$ME.uid = {?}', substr($key, 5));
1315 private $with_photo;
1316 public function addPhotoFilter()
1318 $this->requireProfiles();
1319 $this->with_photo
= true
;
1323 protected function photoJoins()
1325 if ($this->with_photo
) {
1326 return array('photo' => PlSqlJoin
::left('profile_photos', '$ME.pid = $PID'));
1336 public function addMarketingHash()
1338 $this->requireAccounts();
1339 $this->with_rm
= true
;
1342 protected function marketingJoins()
1344 if ($this->with_rm
) {
1345 return array('rm' => PlSqlJoin
::left('register_marketing', '$ME.uid = $UID'));
1352 // {{{ class ProfileFilter
1353 class ProfileFilter
extends UserFilter
1355 public function get($limit = null
)
1357 return $this->getProfiles($limit);
1360 public function getIds($limit = null
)
1362 return $this->getPIDs();
1365 public function filter(array $profiles, $limit = null
)
1367 return $this->filterProfiles($profiles, self
::defaultLimit($limit));
1370 public function getTotalCount()
1372 return $this->getTotalProfileCount();
1375 public function getGroups()
1377 return $this->getPIDGroups();
1382 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: