Commit | Line | Data |
---|---|---|
a087cc8d FB |
1 | <?php |
2 | /*************************************************************************** | |
d4c08d89 | 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
a087cc8d FB |
4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
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. * | |
10 | * * | |
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. * | |
15 | * * | |
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 * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
cd0c2ac4 FB |
22 | require_once dirname(__FILE__) . '/userfilter/conditions.inc.php'; |
23 | require_once dirname(__FILE__) . '/userfilter/orders.inc.php'; | |
009b8ab7 | 24 | |
d865c296 FB |
25 | /*********************************** |
26 | ********************************* | |
27 | USER FILTER CLASS | |
28 | ********************************* | |
29 | ***********************************/ | |
30 | ||
8363588b | 31 | // {{{ class UserFilter |
2d83cac9 RB |
32 | /** This class provides a convenient and centralized way of filtering users. |
33 | * | |
34 | * Usage: | |
35 | * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t)); | |
36 | * | |
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 | |
43 | * | |
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) | |
52 | * in the final query. | |
53 | * | |
54 | * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with | |
913a4e90 | 55 | * profile.pid, and $UID with accounts.uid. |
2d83cac9 RB |
56 | * |
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. | |
61 | * | |
62 | * For example, if data from profile_job must be available to filter results, | |
aab2ffdd | 63 | * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and |
2d83cac9 RB |
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 | |
66 | * row: | |
67 | * 'pj' => array('left', 'profile_job', '$ME.pid = $UID'); | |
68 | * | |
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. | |
71 | */ | |
9b8e5fb4 | 72 | class UserFilter extends PlFilter |
a087cc8d | 73 | { |
9b8e5fb4 | 74 | protected $joinMethods = array(); |
7ca75030 | 75 | |
61f61261 RB |
76 | protected $joinMetas = array( |
77 | '$PID' => 'p.pid', | |
78 | '$UID' => 'a.uid', | |
9b8e5fb4 | 79 | ); |
d865c296 | 80 | |
a087cc8d | 81 | private $root; |
24e08e33 | 82 | private $sort = array(); |
ccc951d9 | 83 | private $grouper = null; |
784745ce | 84 | private $query = null; |
24e08e33 | 85 | private $orderby = null; |
784745ce | 86 | |
7f26cd69 RB |
87 | // Store the current 'search' visibility. |
88 | private $profile_visibility = null; | |
89 | ||
2daf7250 RB |
90 | private $lastusercount = null; |
91 | private $lastprofilecount = null; | |
d865c296 | 92 | |
24e08e33 | 93 | public function __construct($cond = null, $sort = null) |
5dd9d823 | 94 | { |
06598c13 | 95 | if (empty($this->joinMethods)) { |
d865c296 FB |
96 | $class = new ReflectionClass('UserFilter'); |
97 | foreach ($class->getMethods() as $method) { | |
98 | $name = $method->getName(); | |
99 | if (substr($name, -5) == 'Joins' && $name != 'buildJoins') { | |
06598c13 | 100 | $this->joinMethods[] = $name; |
d865c296 FB |
101 | } |
102 | } | |
103 | } | |
5dd9d823 | 104 | if (!is_null($cond)) { |
06598c13 | 105 | if ($cond instanceof PlFilterCondition) { |
5dd9d823 FB |
106 | $this->setCondition($cond); |
107 | } | |
108 | } | |
24e08e33 | 109 | if (!is_null($sort)) { |
ccc951d9 | 110 | if ($sort instanceof PlFilterOrder) { |
24e08e33 | 111 | $this->addSort($sort); |
d865c296 FB |
112 | } else if (is_array($sort)) { |
113 | foreach ($sort as $s) { | |
114 | $this->addSort($s); | |
115 | } | |
24e08e33 FB |
116 | } |
117 | } | |
7f26cd69 RB |
118 | |
119 | // This will set the visibility to the default correct level. | |
120 | $this->profile_visibility = new ProfileVisibility(); | |
121 | } | |
122 | ||
123 | public function getVisibilityLevels() | |
124 | { | |
125 | return $this->profile_visibility->levels(); | |
126 | } | |
127 | ||
128 | public function getVisibilityLevel() | |
129 | { | |
130 | return $this->profile_visibility->level(); | |
131 | } | |
132 | ||
133 | public function restrictVisibilityTo($level) | |
134 | { | |
135 | $this->profile_visibility->setLevel($level); | |
136 | } | |
137 | ||
138 | public function getVisibilityCondition($field) | |
139 | { | |
140 | return $field . ' IN ' . XDB::formatArray($this->getVisibilityLevels()); | |
5dd9d823 FB |
141 | } |
142 | ||
784745ce FB |
143 | private function buildQuery() |
144 | { | |
2a93b634 RB |
145 | // The root condition is built first because some orders need info |
146 | // available only once all UFC have set their conditions (UFO_Score) | |
147 | if (is_null($this->query)) { | |
148 | $where = $this->root->buildCondition($this); | |
226626ae FB |
149 | $where = str_replace(array_keys($this->joinMetas), |
150 | $this->joinMetas, | |
151 | $where); | |
2a93b634 | 152 | } |
d865c296 FB |
153 | if (is_null($this->orderby)) { |
154 | $orders = array(); | |
155 | foreach ($this->sort as $sort) { | |
156 | $orders = array_merge($orders, $sort->buildSort($this)); | |
157 | } | |
158 | if (count($orders) == 0) { | |
159 | $this->orderby = ''; | |
160 | } else { | |
161 | $this->orderby = 'ORDER BY ' . implode(', ', $orders); | |
162 | } | |
226626ae FB |
163 | $this->orderby = str_replace(array_keys($this->joinMetas), |
164 | $this->joinMetas, | |
165 | $this->orderby); | |
d865c296 | 166 | } |
784745ce | 167 | if (is_null($this->query)) { |
2a93b634 | 168 | if ($this->with_accounts) { |
b8dcf62d RB |
169 | $from = 'accounts AS a'; |
170 | } else { | |
171 | $this->requireProfiles(); | |
172 | $from = 'profiles AS p'; | |
173 | } | |
f7ea7450 | 174 | $joins = $this->buildJoins(); |
b8dcf62d | 175 | $this->query = 'FROM ' . $from . ' |
784745ce FB |
176 | ' . $joins . ' |
177 | WHERE (' . $where . ')'; | |
178 | } | |
179 | } | |
180 | ||
ccc951d9 RB |
181 | public function hasGroups() |
182 | { | |
183 | return $this->grouper != null; | |
184 | } | |
185 | ||
186 | public function getGroups() | |
187 | { | |
188 | return $this->getUIDGroups(); | |
189 | } | |
190 | ||
191 | public function getUIDGroups() | |
192 | { | |
193 | $this->requireAccounts(); | |
194 | $this->buildQuery(); | |
195 | $token = $this->grouper->getGroupToken($this); | |
196 | ||
59db57ab FB |
197 | $groups = XDB::rawFetchAllRow('SELECT ' . $token . ', COUNT(a.uid) |
198 | ' . $this->query . ' | |
199 | GROUP BY ' . $token, | |
200 | 0); | |
ccc951d9 RB |
201 | return $groups; |
202 | } | |
203 | ||
204 | public function getPIDGroups() | |
205 | { | |
206 | $this->requireProfiles(); | |
207 | $this->buildQuery(); | |
208 | $token = $this->grouper->getGroupToken($this); | |
209 | ||
59db57ab FB |
210 | $groups = XDB::rawFetchAllRow('SELECT ' . $token . ', COUNT(p.pid) |
211 | ' . $this->query . ' | |
212 | GROUP BY ' . $token, | |
213 | 0); | |
ccc951d9 RB |
214 | return $groups; |
215 | } | |
216 | ||
7ca75030 | 217 | private function getUIDList($uids = null, PlLimit &$limit) |
d865c296 | 218 | { |
b8dcf62d | 219 | $this->requireAccounts(); |
d865c296 | 220 | $this->buildQuery(); |
7ca75030 | 221 | $lim = $limit->getSql(); |
d865c296 | 222 | $cond = ''; |
45b20ca0 | 223 | if (!empty($uids)) { |
bde68f05 | 224 | $cond = XDB::format(' AND a.uid IN {?}', $uids); |
d865c296 | 225 | } |
59db57ab FB |
226 | $fetched = XDB::rawFetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid |
227 | ' . $this->query . $cond . ' | |
228 | GROUP BY a.uid | |
229 | ' . $this->orderby . ' | |
230 | ' . $lim); | |
2daf7250 | 231 | $this->lastusercount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()'); |
d865c296 FB |
232 | return $fetched; |
233 | } | |
234 | ||
043b104b RB |
235 | private function getPIDList($pids = null, PlLimit &$limit) |
236 | { | |
237 | $this->requireProfiles(); | |
238 | $this->buildQuery(); | |
239 | $lim = $limit->getSql(); | |
240 | $cond = ''; | |
241 | if (!is_null($pids)) { | |
bde68f05 | 242 | $cond = XDB::format(' AND p.pid IN {?}', $pids); |
043b104b | 243 | } |
59db57ab FB |
244 | $fetched = XDB::rawFetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid |
245 | ' . $this->query . $cond . ' | |
246 | GROUP BY p.pid | |
247 | ' . $this->orderby . ' | |
248 | ' . $lim); | |
2daf7250 | 249 | $this->lastprofilecount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()'); |
043b104b RB |
250 | return $fetched; |
251 | } | |
252 | ||
434570c4 FB |
253 | private static function defaultLimit($limit) { |
254 | if ($limit == null) { | |
255 | return new PlLimit(); | |
256 | } else { | |
257 | return $limit; | |
258 | } | |
259 | } | |
260 | ||
a087cc8d FB |
261 | /** Check that the user match the given rule. |
262 | */ | |
263 | public function checkUser(PlUser &$user) | |
264 | { | |
b8dcf62d | 265 | $this->requireAccounts(); |
784745ce | 266 | $this->buildQuery(); |
59db57ab FB |
267 | $count = (int)XDB::rawFetchOneCell('SELECT COUNT(*) |
268 | ' . $this->query | |
269 | . XDB::format(' AND a.uid = {?}', $user->id())); | |
784745ce | 270 | return $count == 1; |
a087cc8d FB |
271 | } |
272 | ||
043b104b RB |
273 | /** Check that the profile match the given rule. |
274 | */ | |
275 | public function checkProfile(Profile &$profile) | |
276 | { | |
277 | $this->requireProfiles(); | |
278 | $this->buildQuery(); | |
59db57ab FB |
279 | $count = (int)XDB::rawFetchOneCell('SELECT COUNT(*) |
280 | ' . $this->query | |
281 | . XDB::format(' AND p.pid = {?}', $profile->id())); | |
043b104b RB |
282 | return $count == 1; |
283 | } | |
284 | ||
285 | /** Default filter is on users | |
a087cc8d | 286 | */ |
434570c4 | 287 | public function filter(array $users, $limit = null) |
a087cc8d | 288 | { |
434570c4 | 289 | return $this->filterUsers($users, self::defaultLimit($limit)); |
043b104b RB |
290 | } |
291 | ||
292 | /** Filter a list of users to extract the users matching the rule. | |
293 | */ | |
434570c4 | 294 | public function filterUsers(array $users, $limit = null) |
043b104b | 295 | { |
434570c4 | 296 | $limit = self::defaultLimit($limit); |
b8dcf62d | 297 | $this->requireAccounts(); |
4927ee54 FB |
298 | $this->buildQuery(); |
299 | $table = array(); | |
300 | $uids = array(); | |
301 | foreach ($users as $user) { | |
07eb5b0e FB |
302 | if ($user instanceof PlUser) { |
303 | $uid = $user->id(); | |
304 | } else { | |
305 | $uid = $user; | |
306 | } | |
307 | $uids[] = $uid; | |
308 | $table[$uid] = $user; | |
4927ee54 | 309 | } |
7ca75030 | 310 | $fetched = $this->getUIDList($uids, $limit); |
a087cc8d | 311 | $output = array(); |
4927ee54 FB |
312 | foreach ($fetched as $uid) { |
313 | $output[] = $table[$uid]; | |
a087cc8d FB |
314 | } |
315 | return $output; | |
316 | } | |
317 | ||
043b104b RB |
318 | /** Filter a list of profiles to extract the users matching the rule. |
319 | */ | |
434570c4 | 320 | public function filterProfiles(array $profiles, $limit = null) |
043b104b | 321 | { |
434570c4 | 322 | $limit = self::defaultLimit($limit); |
043b104b RB |
323 | $this->requireProfiles(); |
324 | $this->buildQuery(); | |
325 | $table = array(); | |
326 | $pids = array(); | |
327 | foreach ($profiles as $profile) { | |
328 | if ($profile instanceof Profile) { | |
329 | $pid = $profile->id(); | |
330 | } else { | |
331 | $pid = $profile; | |
332 | } | |
333 | $pids[] = $pid; | |
334 | $table[$pid] = $profile; | |
335 | } | |
336 | $fetched = $this->getPIDList($pids, $limit); | |
337 | $output = array(); | |
338 | foreach ($fetched as $pid) { | |
339 | $output[] = $table[$pid]; | |
340 | } | |
341 | return $output; | |
342 | } | |
343 | ||
434570c4 | 344 | public function getUIDs($limit = null) |
7ca75030 | 345 | { |
833a6e86 FB |
346 | $limit = self::defaultLimit($limit); |
347 | return $this->getUIDList(null, $limit); | |
7ca75030 RB |
348 | } |
349 | ||
ad27b22e FB |
350 | public function getUID($pos = 0) |
351 | { | |
983f3864 | 352 | $uids =$this->getUIDList(null, new PlLimit(1, $pos)); |
ad27b22e FB |
353 | if (count($uids) == 0) { |
354 | return null; | |
355 | } else { | |
356 | return $uids[0]; | |
357 | } | |
358 | } | |
359 | ||
434570c4 | 360 | public function getPIDs($limit = null) |
043b104b | 361 | { |
833a6e86 FB |
362 | $limit = self::defaultLimit($limit); |
363 | return $this->getPIDList(null, $limit); | |
043b104b RB |
364 | } |
365 | ||
ad27b22e FB |
366 | public function getPID($pos = 0) |
367 | { | |
983f3864 | 368 | $pids =$this->getPIDList(null, new PlLimit(1, $pos)); |
ad27b22e FB |
369 | if (count($pids) == 0) { |
370 | return null; | |
371 | } else { | |
372 | return $pids[0]; | |
373 | } | |
374 | } | |
375 | ||
434570c4 | 376 | public function getUsers($limit = null) |
4927ee54 | 377 | { |
7ca75030 | 378 | return User::getBulkUsersWithUIDs($this->getUIDs($limit)); |
d865c296 FB |
379 | } |
380 | ||
ad27b22e FB |
381 | public function getUser($pos = 0) |
382 | { | |
383 | $uid = $this->getUID($pos); | |
384 | if ($uid == null) { | |
385 | return null; | |
386 | } else { | |
387 | return User::getWithUID($uid); | |
388 | } | |
389 | } | |
390 | ||
0d906109 RB |
391 | public function iterUsers($limit = null) |
392 | { | |
393 | return User::iterOverUIDs($this->getUIDs($limit)); | |
394 | } | |
395 | ||
00f83317 | 396 | public function getProfiles($limit = null, $fields = 0x0000, $visibility = null) |
043b104b | 397 | { |
00f83317 | 398 | return Profile::getBulkProfilesWithPIDs($this->getPIDs($limit), $fields, $visibility); |
043b104b RB |
399 | } |
400 | ||
00f83317 | 401 | public function getProfile($pos = 0, $fields = 0x0000, $visibility = null) |
ad27b22e FB |
402 | { |
403 | $pid = $this->getPID($pos); | |
404 | if ($pid == null) { | |
405 | return null; | |
406 | } else { | |
00f83317 | 407 | return Profile::get($pid, $fields, $visibility); |
ad27b22e FB |
408 | } |
409 | } | |
410 | ||
00f83317 | 411 | public function iterProfiles($limit = null, $fields = 0x0000, $visibility = null) |
0d906109 | 412 | { |
00f83317 | 413 | return Profile::iterOverPIDs($this->getPIDs($limit), true, $fields, $visibility); |
0d906109 RB |
414 | } |
415 | ||
434570c4 | 416 | public function get($limit = null) |
d865c296 | 417 | { |
7ca75030 | 418 | return $this->getUsers($limit); |
4927ee54 FB |
419 | } |
420 | ||
d5fd47bf SJ |
421 | public function getIds($limit = null) |
422 | { | |
423 | return $this->getUIDs(); | |
424 | } | |
aaf70eb8 | 425 | |
d865c296 | 426 | public function getTotalCount() |
4927ee54 | 427 | { |
2daf7250 RB |
428 | return $this->getTotalUserCount(); |
429 | } | |
430 | ||
431 | public function getTotalUserCount() | |
432 | { | |
433 | if (is_null($this->lastusercount)) { | |
434 | $this->requireAccounts(); | |
aa21c568 | 435 | $this->buildQuery(); |
59db57ab | 436 | return (int)XDB::rawFetchOneCell('SELECT COUNT(DISTINCT a.uid) |
2daf7250 RB |
437 | ' . $this->query); |
438 | } else { | |
439 | return $this->lastusercount; | |
440 | } | |
441 | } | |
442 | ||
443 | public function getTotalProfileCount() | |
444 | { | |
445 | if (is_null($this->lastprofilecount)) { | |
446 | $this->requireProfiles(); | |
447 | $this->buildQuery(); | |
59db57ab | 448 | return (int)XDB::rawFetchOneCell('SELECT COUNT(DISTINCT p.pid) |
7e735012 | 449 | ' . $this->query); |
38c6fe96 | 450 | } else { |
2daf7250 | 451 | return $this->lastprofilecount; |
38c6fe96 | 452 | } |
4927ee54 FB |
453 | } |
454 | ||
93ded2f4 | 455 | public function setCondition(PlFilterCondition $cond) |
a087cc8d FB |
456 | { |
457 | $this->root =& $cond; | |
784745ce | 458 | $this->query = null; |
a087cc8d FB |
459 | } |
460 | ||
93ded2f4 | 461 | public function addSort(PlFilterOrder $sort) |
24e08e33 | 462 | { |
ccc951d9 RB |
463 | if (count($this->sort) == 0 && $sort instanceof PlFilterGroupableOrder) |
464 | { | |
465 | $this->grouper = $sort; | |
466 | } | |
d865c296 FB |
467 | $this->sort[] = $sort; |
468 | $this->orderby = null; | |
24e08e33 FB |
469 | } |
470 | ||
30eedabf FB |
471 | public function export() |
472 | { | |
473 | $export = array('condition' => $this->root->export()); | |
474 | if (!empty($this->sort)) { | |
475 | $export['sort'] = array(); | |
476 | foreach ($this->sort as $sort) { | |
477 | $export['sort'][] = $sort->export(); | |
478 | } | |
479 | } | |
480 | return $export; | |
481 | } | |
482 | ||
a087cc8d FB |
483 | static public function getLegacy($promo_min, $promo_max) |
484 | { | |
a087cc8d | 485 | if ($promo_min != 0) { |
784745ce | 486 | $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min)); |
5dd9d823 | 487 | } else { |
88c31faf | 488 | $min = new PFC_True(); |
a087cc8d | 489 | } |
a087cc8d | 490 | if ($promo_max != 0) { |
784745ce | 491 | $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max)); |
a087cc8d | 492 | } else { |
88c31faf | 493 | $max = new PFC_True(); |
a087cc8d | 494 | } |
9b8e5fb4 | 495 | return new UserFilter(new PFC_And($min, $max)); |
a087cc8d | 496 | } |
784745ce | 497 | |
07eb5b0e FB |
498 | static public function sortByName() |
499 | { | |
913a4e90 | 500 | return array(new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME)); |
07eb5b0e FB |
501 | } |
502 | ||
503 | static public function sortByPromo() | |
504 | { | |
913a4e90 | 505 | return array(new UFO_Promo(), new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME)); |
07eb5b0e FB |
506 | } |
507 | ||
aa21c568 FB |
508 | static private function getDBSuffix($string) |
509 | { | |
2d6329a2 FB |
510 | if (is_array($string)) { |
511 | if (count($string) == 1) { | |
512 | return self::getDBSuffix(array_pop($string)); | |
513 | } | |
514 | return md5(implode('|', $string)); | |
515 | } else { | |
516 | return preg_replace('/[^a-z0-9]/i', '', $string); | |
517 | } | |
aa21c568 FB |
518 | } |
519 | ||
520 | ||
2d83cac9 RB |
521 | /** Stores a new (and unique) table alias in the &$table table |
522 | * @param &$table Array in which the table alias must be stored | |
523 | * @param $val Value which will then be used to build the join | |
524 | * @return Name of the newly created alias | |
525 | */ | |
aa21c568 FB |
526 | private $option = 0; |
527 | private function register_optional(array &$table, $val) | |
528 | { | |
529 | if (is_null($val)) { | |
530 | $sub = $this->option++; | |
531 | $index = null; | |
532 | } else { | |
533 | $sub = self::getDBSuffix($val); | |
534 | $index = $val; | |
535 | } | |
536 | $sub = '_' . $sub; | |
537 | $table[$sub] = $index; | |
538 | return $sub; | |
539 | } | |
784745ce | 540 | |
b8dcf62d RB |
541 | /** PROFILE VS ACCOUNT |
542 | */ | |
f7ea7450 RB |
543 | private $with_profiles = false; |
544 | private $with_accounts = false; | |
b8dcf62d RB |
545 | public function requireAccounts() |
546 | { | |
547 | $this->with_accounts = true; | |
548 | } | |
549 | ||
a9ef52c9 RB |
550 | public function accountsRequired() |
551 | { | |
552 | return $this->with_accounts; | |
553 | } | |
554 | ||
b8dcf62d RB |
555 | public function requireProfiles() |
556 | { | |
557 | $this->with_profiles = true; | |
558 | } | |
559 | ||
a9ef52c9 RB |
560 | public function profilesRequired() |
561 | { | |
562 | return $this->with_profiles; | |
563 | } | |
564 | ||
b8dcf62d RB |
565 | protected function accountJoins() |
566 | { | |
567 | $joins = array(); | |
2a93b634 | 568 | if ($this->with_profiles && $this->with_accounts) { |
5c412626 FB |
569 | $joins['ap'] = PlSqlJoin::left('account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)'); |
570 | $joins['p'] = PlSqlJoin::left('profiles', '$PID = ap.pid'); | |
b8dcf62d RB |
571 | } |
572 | return $joins; | |
573 | } | |
574 | ||
1eabd2a4 FB |
575 | /** PERMISSIONS |
576 | */ | |
577 | private $at = false; | |
578 | public function requirePerms() | |
579 | { | |
580 | $this->requireAccounts(); | |
581 | $this->at = true; | |
582 | return 'at'; | |
583 | } | |
584 | ||
585 | protected function permJoins() | |
586 | { | |
587 | if ($this->at) { | |
588 | return array('at' => PlSqlJoin::left('account_types', '$ME.type = a.type')); | |
589 | } else { | |
590 | return array(); | |
591 | } | |
592 | } | |
593 | ||
d865c296 FB |
594 | /** DISPLAY |
595 | */ | |
38c6fe96 | 596 | const DISPLAY = 'display'; |
d865c296 FB |
597 | private $pd = false; |
598 | public function addDisplayFilter() | |
599 | { | |
b8dcf62d | 600 | $this->requireProfiles(); |
d865c296 FB |
601 | $this->pd = true; |
602 | return ''; | |
603 | } | |
604 | ||
9b8e5fb4 | 605 | protected function displayJoins() |
d865c296 FB |
606 | { |
607 | if ($this->pd) { | |
5c412626 | 608 | return array('pd' => PlSqlJoin::left('profile_display', '$ME.pid = $PID')); |
d865c296 FB |
609 | } else { |
610 | return array(); | |
611 | } | |
612 | } | |
613 | ||
f73a4f1b RB |
614 | /** LOGGER |
615 | */ | |
616 | ||
617 | private $with_logger = false; | |
618 | public function addLoggerFilter() | |
619 | { | |
620 | $this->with_logger = true; | |
621 | $this->requireAccounts(); | |
622 | return 'ls'; | |
623 | } | |
624 | protected function loggerJoins() | |
625 | { | |
626 | $joins = array(); | |
627 | if ($this->with_logger) { | |
5c412626 | 628 | $joins['ls'] = PlSqlJoin::left('log_sessions', '$ME.uid = $UID'); |
f73a4f1b RB |
629 | } |
630 | return $joins; | |
631 | } | |
632 | ||
784745ce FB |
633 | /** NAMES |
634 | */ | |
784745ce FB |
635 | |
636 | static public function assertName($name) | |
637 | { | |
07613cdd | 638 | if (!DirEnum::getID(DirEnum::NAMETYPES, $name)) { |
9b8e5fb4 | 639 | Platal::page()->kill('Invalid name type: ' . $name); |
784745ce FB |
640 | } |
641 | } | |
642 | ||
643 | private $pn = array(); | |
784745ce FB |
644 | public function addNameFilter($type, $variant = null) |
645 | { | |
b8dcf62d | 646 | $this->requireProfiles(); |
784745ce FB |
647 | if (!is_null($variant)) { |
648 | $ft = $type . '_' . $variant; | |
649 | } else { | |
650 | $ft = $type; | |
651 | } | |
652 | $sub = '_' . $ft; | |
653 | self::assertName($ft); | |
654 | ||
655 | if (!is_null($variant) && $variant == 'other') { | |
aa21c568 | 656 | $sub .= $this->option++; |
784745ce | 657 | } |
07613cdd | 658 | $this->pn[$sub] = DirEnum::getID(DirEnum::NAMETYPES, $ft); |
784745ce FB |
659 | return $sub; |
660 | } | |
661 | ||
9b8e5fb4 | 662 | protected function nameJoins() |
784745ce FB |
663 | { |
664 | $joins = array(); | |
665 | foreach ($this->pn as $sub => $type) { | |
5c412626 | 666 | $joins['pn' . $sub] = PlSqlJoin::left('profile_name', '$ME.pid = $PID AND $ME.typeid = {?}', $type); |
784745ce FB |
667 | } |
668 | return $joins; | |
669 | } | |
670 | ||
40585144 RB |
671 | /** NAMETOKENS |
672 | */ | |
2a93b634 RB |
673 | private $name_tokens = array(); |
674 | private $nb_tokens = 0; | |
675 | ||
676 | public function addNameTokensFilter($token) | |
40585144 RB |
677 | { |
678 | $this->requireProfiles(); | |
2a93b634 RB |
679 | $sub = 'sn' . (1 + $this->nb_tokens); |
680 | $this->nb_tokens++; | |
681 | $this->name_tokens[$sub] = $token; | |
682 | return $sub; | |
40585144 RB |
683 | } |
684 | ||
685 | protected function nameTokensJoins() | |
686 | { | |
f7ea7450 | 687 | /* We don't return joins, since with_sn forces the SELECT to run on search_name first */ |
2a93b634 RB |
688 | $joins = array(); |
689 | foreach ($this->name_tokens as $sub => $token) { | |
690 | $joins[$sub] = PlSqlJoin::left('search_name', '$ME.pid = $PID'); | |
40585144 | 691 | } |
2a93b634 RB |
692 | return $joins; |
693 | } | |
694 | ||
695 | public function getNameTokens() | |
696 | { | |
697 | return $this->name_tokens; | |
40585144 RB |
698 | } |
699 | ||
a7f8e48a RB |
700 | /** NATIONALITY |
701 | */ | |
702 | ||
703 | private $with_nat = false; | |
704 | public function addNationalityFilter() | |
705 | { | |
706 | $this->with_nat = true; | |
707 | return 'ngc'; | |
708 | } | |
709 | ||
710 | protected function nationalityJoins() | |
711 | { | |
712 | $joins = array(); | |
713 | if ($this->with_nat) { | |
5c412626 | 714 | $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'); |
a7f8e48a RB |
715 | } |
716 | return $joins; | |
717 | } | |
718 | ||
784745ce FB |
719 | /** EDUCATION |
720 | */ | |
721 | const GRADE_ING = 'Ing.'; | |
722 | const GRADE_PHD = 'PhD'; | |
723 | const GRADE_MST = 'M%'; | |
724 | static public function isGrade($grade) | |
725 | { | |
93c2f133 | 726 | return ($grade !== 0) && ($grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST); |
784745ce FB |
727 | } |
728 | ||
729 | static public function assertGrade($grade) | |
730 | { | |
731 | if (!self::isGrade($grade)) { | |
ad27b22e | 732 | Platal::page()->killError("Diplôme non valide: $grade"); |
784745ce FB |
733 | } |
734 | } | |
735 | ||
d865c296 FB |
736 | static public function promoYear($grade) |
737 | { | |
738 | // XXX: Definition of promotion for phds and masters might change in near future. | |
739 | return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year'; | |
740 | } | |
741 | ||
784745ce FB |
742 | private $pepe = array(); |
743 | private $with_pee = false; | |
784745ce FB |
744 | public function addEducationFilter($x = false, $grade = null) |
745 | { | |
b8dcf62d | 746 | $this->requireProfiles(); |
784745ce | 747 | if (!$x) { |
aa21c568 FB |
748 | $index = $this->option; |
749 | $sub = $this->option++; | |
784745ce FB |
750 | } else { |
751 | self::assertGrade($grade); | |
752 | $index = $grade; | |
753 | $sub = $grade[0]; | |
754 | $this->with_pee = true; | |
755 | } | |
756 | $sub = '_' . $sub; | |
757 | $this->pepe[$index] = $sub; | |
758 | return $sub; | |
759 | } | |
760 | ||
9b8e5fb4 | 761 | protected function educationJoins() |
784745ce FB |
762 | { |
763 | $joins = array(); | |
764 | if ($this->with_pee) { | |
5c412626 | 765 | $joins['pee'] = PlSqlJoin::inner('profile_education_enum', 'pee.abbreviation = \'X\''); |
784745ce FB |
766 | } |
767 | foreach ($this->pepe as $grade => $sub) { | |
768 | if ($this->isGrade($grade)) { | |
5c412626 FB |
769 | $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID'); |
770 | $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE {?}', $grade); | |
784745ce | 771 | } else { |
5c412626 FB |
772 | $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.pid = $PID'); |
773 | $joins['pee' . $sub] = PlSqlJoin::inner('profile_education_enum', '$ME.id = pe' . $sub . '.eduid'); | |
774 | $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid'); | |
784745ce FB |
775 | } |
776 | } | |
777 | return $joins; | |
778 | } | |
4927ee54 FB |
779 | |
780 | ||
781 | /** GROUPS | |
782 | */ | |
783 | private $gpm = array(); | |
4927ee54 FB |
784 | public function addGroupFilter($group = null) |
785 | { | |
b8dcf62d | 786 | $this->requireAccounts(); |
4927ee54 | 787 | if (!is_null($group)) { |
4aae4d2c | 788 | if (is_int($group) || ctype_digit($group)) { |
4927ee54 FB |
789 | $index = $sub = $group; |
790 | } else { | |
791 | $index = $group; | |
aa21c568 | 792 | $sub = self::getDBSuffix($group); |
4927ee54 FB |
793 | } |
794 | } else { | |
aa21c568 | 795 | $sub = 'group_' . $this->option++; |
4927ee54 FB |
796 | $index = null; |
797 | } | |
798 | $sub = '_' . $sub; | |
799 | $this->gpm[$sub] = $index; | |
800 | return $sub; | |
801 | } | |
802 | ||
9b8e5fb4 | 803 | protected function groupJoins() |
4927ee54 FB |
804 | { |
805 | $joins = array(); | |
806 | foreach ($this->gpm as $sub => $key) { | |
807 | if (is_null($key)) { | |
5c412626 FB |
808 | $joins['gpa' . $sub] = PlSqlJoin::inner('groups'); |
809 | $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
4aae4d2c | 810 | } else if (is_int($key) || ctype_digit($key)) { |
5c412626 | 811 | $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key); |
4927ee54 | 812 | } else { |
5c412626 FB |
813 | $joins['gpa' . $sub] = PlSqlJoin::inner('groups', '$ME.diminutif = {?}', $key); |
814 | $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
4927ee54 FB |
815 | } |
816 | } | |
817 | return $joins; | |
0fb3713c RB |
818 | } |
819 | ||
820 | /** BINETS | |
821 | */ | |
822 | ||
a7f8e48a RB |
823 | private $with_bi = false; |
824 | private $with_bd = false; | |
d7ddf29b | 825 | public function addBinetsFilter($with_enum = false) |
0fb3713c RB |
826 | { |
827 | $this->requireProfiles(); | |
a7f8e48a RB |
828 | $this->with_bi = true; |
829 | if ($with_enum) { | |
830 | $this->with_bd = true; | |
831 | return 'bd'; | |
832 | } else { | |
833 | return 'bi'; | |
834 | } | |
0fb3713c RB |
835 | } |
836 | ||
837 | protected function binetsJoins() | |
838 | { | |
839 | $joins = array(); | |
a7f8e48a | 840 | if ($this->with_bi) { |
5c412626 | 841 | $joins['bi'] = PlSqlJoin::left('profile_binets', '$ME.pid = $PID'); |
0fb3713c | 842 | } |
a7f8e48a | 843 | if ($this->with_bd) { |
5c412626 | 844 | $joins['bd'] = PlSqlJoin::left('profile_binet_enum', '$ME.id = bi.binet_id'); |
a7f8e48a | 845 | } |
0fb3713c | 846 | return $joins; |
4927ee54 | 847 | } |
aa21c568 FB |
848 | |
849 | /** EMAILS | |
850 | */ | |
851 | private $e = array(); | |
852 | public function addEmailRedirectFilter($email = null) | |
853 | { | |
b8dcf62d | 854 | $this->requireAccounts(); |
aa21c568 FB |
855 | return $this->register_optional($this->e, $email); |
856 | } | |
857 | ||
858 | private $ve = array(); | |
859 | public function addVirtualEmailFilter($email = null) | |
860 | { | |
21401768 | 861 | $this->addAliasFilter(self::ALIAS_FORLIFE); |
aa21c568 FB |
862 | return $this->register_optional($this->ve, $email); |
863 | } | |
864 | ||
21401768 FB |
865 | const ALIAS_BEST = 'bestalias'; |
866 | const ALIAS_FORLIFE = 'forlife'; | |
aa21c568 FB |
867 | private $al = array(); |
868 | public function addAliasFilter($alias = null) | |
869 | { | |
b8dcf62d | 870 | $this->requireAccounts(); |
aa21c568 FB |
871 | return $this->register_optional($this->al, $alias); |
872 | } | |
873 | ||
9b8e5fb4 | 874 | protected function emailJoins() |
aa21c568 FB |
875 | { |
876 | global $globals; | |
877 | $joins = array(); | |
878 | foreach ($this->e as $sub=>$key) { | |
879 | if (is_null($key)) { | |
5c412626 | 880 | $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\''); |
aa21c568 | 881 | } else { |
2d6329a2 FB |
882 | if (!is_array($key)) { |
883 | $key = array($key); | |
884 | } | |
aab2ffdd | 885 | $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\' |
2f1c94e0 | 886 | AND $ME.email IN {?}', $key); |
aa21c568 FB |
887 | } |
888 | } | |
21401768 | 889 | foreach ($this->al as $sub=>$key) { |
aa21c568 | 890 | if (is_null($key)) { |
5c412626 | 891 | $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')'); |
21401768 | 892 | } else if ($key == self::ALIAS_BEST) { |
5c412626 | 893 | $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)'); |
21401768 | 894 | } else if ($key == self::ALIAS_FORLIFE) { |
5c412626 | 895 | $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type = \'a_vie\''); |
aa21c568 | 896 | } else { |
2d6329a2 FB |
897 | if (!is_array($key)) { |
898 | $key = array($key); | |
899 | } | |
aab2ffdd | 900 | $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') |
bde68f05 | 901 | AND $ME.alias IN {?}', $key); |
aa21c568 | 902 | } |
aa21c568 | 903 | } |
21401768 | 904 | foreach ($this->ve as $sub=>$key) { |
aa21c568 | 905 | if (is_null($key)) { |
5c412626 | 906 | $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\''); |
aa21c568 | 907 | } else { |
2d6329a2 FB |
908 | if (!is_array($key)) { |
909 | $key = array($key); | |
910 | } | |
bde68f05 | 911 | $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\' AND $ME.alias IN {?}', $key); |
aa21c568 | 912 | } |
5c412626 FB |
913 | $joins['vr' . $sub] = PlSqlJoin::left('virtual_redirect', |
914 | '$ME.vid = v' . $sub . '.vid | |
915 | AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}), | |
916 | CONCAT(al_forlife.alias, \'@\', {?}), | |
917 | a.email))', | |
918 | $globals->mail->domain, $globals->mail->domain2); | |
aa21c568 FB |
919 | } |
920 | return $joins; | |
921 | } | |
3f42a6ad FB |
922 | |
923 | ||
c4b24511 RB |
924 | /** ADDRESSES |
925 | */ | |
036d1637 | 926 | private $with_pa = false; |
c4b24511 RB |
927 | public function addAddressFilter() |
928 | { | |
b8dcf62d | 929 | $this->requireProfiles(); |
036d1637 RB |
930 | $this->with_pa = true; |
931 | return 'pa'; | |
c4b24511 RB |
932 | } |
933 | ||
2b9ca54d RB |
934 | private $with_pac = false; |
935 | public function addAddressCountryFilter() | |
936 | { | |
937 | $this->requireProfiles(); | |
938 | $this->addAddressFilter(); | |
939 | $this->with_pac = true; | |
940 | return 'gc'; | |
941 | } | |
942 | ||
d7ddf29b | 943 | private $with_pal = false; |
2b9ca54d RB |
944 | public function addAddressLocalityFilter() |
945 | { | |
946 | $this->requireProfiles(); | |
947 | $this->addAddressFilter(); | |
948 | $this->with_pal = true; | |
949 | return 'gl'; | |
950 | } | |
951 | ||
9b8e5fb4 | 952 | protected function addressJoins() |
c4b24511 RB |
953 | { |
954 | $joins = array(); | |
036d1637 | 955 | if ($this->with_pa) { |
5c412626 | 956 | $joins['pa'] = PlSqlJoin::left('profile_addresses', '$ME.pid = $PID'); |
c4b24511 | 957 | } |
2b9ca54d | 958 | if ($this->with_pac) { |
5c412626 | 959 | $joins['gc'] = PlSqlJoin::left('geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID'); |
2b9ca54d RB |
960 | } |
961 | if ($this->with_pal) { | |
5c412626 | 962 | $joins['gl'] = PlSqlJoin::left('geoloc_localities', '$ME.id = pa.localityID'); |
2b9ca54d | 963 | } |
c4b24511 RB |
964 | return $joins; |
965 | } | |
966 | ||
967 | ||
4083b126 RB |
968 | /** CORPS |
969 | */ | |
970 | ||
971 | private $pc = false; | |
972 | private $pce = array(); | |
973 | private $pcr = false; | |
974 | public function addCorpsFilter($type) | |
975 | { | |
b8dcf62d | 976 | $this->requireProfiles(); |
4083b126 RB |
977 | $this->pc = true; |
978 | if ($type == UFC_Corps::CURRENT) { | |
979 | $pce['pcec'] = 'current_corpsid'; | |
980 | return 'pcec'; | |
981 | } else if ($type == UFC_Corps::ORIGIN) { | |
982 | $pce['pceo'] = 'original_corpsid'; | |
983 | return 'pceo'; | |
984 | } | |
985 | } | |
986 | ||
987 | public function addCorpsRankFilter() | |
988 | { | |
b8dcf62d | 989 | $this->requireProfiles(); |
4083b126 RB |
990 | $this->pc = true; |
991 | $this->pcr = true; | |
992 | return 'pcr'; | |
993 | } | |
994 | ||
9b8e5fb4 | 995 | protected function corpsJoins() |
4083b126 RB |
996 | { |
997 | $joins = array(); | |
998 | if ($this->pc) { | |
5c412626 | 999 | $joins['pc'] = PlSqlJoin::left('profile_corps', '$ME.pid = $PID'); |
4083b126 RB |
1000 | } |
1001 | if ($this->pcr) { | |
5c412626 | 1002 | $joins['pcr'] = PlSqlJoin::left('profile_corps_rank_enum', '$ME.id = pc.rankid'); |
4083b126 RB |
1003 | } |
1004 | foreach($this->pce as $sub => $field) { | |
5c412626 | 1005 | $joins[$sub] = PlSqlJoin::left('profile_corps_enum', '$ME.id = pc.' . $field); |
4083b126 RB |
1006 | } |
1007 | return $joins; | |
1008 | } | |
1009 | ||
6a99c3ac RB |
1010 | /** JOBS |
1011 | */ | |
1012 | ||
da40b2a4 SJ |
1013 | const JOB_USERDEFINED = 0x0001; |
1014 | const JOB_CV = 0x0002; | |
1015 | const JOB_ANY = 0x0003; | |
6a99c3ac RB |
1016 | |
1017 | /** Joins : | |
1018 | * pj => profile_job | |
1019 | * pje => profile_job_enum | |
3ac45f10 | 1020 | * pjt => profile_job_terms |
6a99c3ac | 1021 | */ |
da40b2a4 | 1022 | private $with_pj = false; |
6a99c3ac | 1023 | private $with_pje = false; |
3ac45f10 | 1024 | private $with_pjt = 0; |
6a99c3ac RB |
1025 | |
1026 | public function addJobFilter() | |
1027 | { | |
b8dcf62d | 1028 | $this->requireProfiles(); |
6a99c3ac RB |
1029 | $this->with_pj = true; |
1030 | return 'pj'; | |
1031 | } | |
1032 | ||
1033 | public function addJobCompanyFilter() | |
1034 | { | |
1035 | $this->addJobFilter(); | |
1036 | $this->with_pje = true; | |
1037 | return 'pje'; | |
1038 | } | |
1039 | ||
3ac45f10 PC |
1040 | /** |
1041 | * Adds a filter on job terms of profile. | |
1042 | * @param $nb the number of job terms to use | |
1043 | * @return an array of the fields to filter (one for each term). | |
3ac45f10 PC |
1044 | */ |
1045 | public function addJobTermsFilter($nb = 1) | |
1046 | { | |
1047 | $this->with_pjt = $nb; | |
1048 | $jobtermstable = array(); | |
1049 | for ($i = 1; $i <= $nb; ++$i) { | |
4ec03752 | 1050 | $jobtermstable[] = 'pjtr_'.$i; |
3ac45f10 PC |
1051 | } |
1052 | return $jobtermstable; | |
1053 | } | |
1054 | ||
9b8e5fb4 | 1055 | protected function jobJoins() |
6a99c3ac RB |
1056 | { |
1057 | $joins = array(); | |
1058 | if ($this->with_pj) { | |
5c412626 | 1059 | $joins['pj'] = PlSqlJoin::left('profile_job', '$ME.pid = $PID'); |
6a99c3ac RB |
1060 | } |
1061 | if ($this->with_pje) { | |
5c412626 | 1062 | $joins['pje'] = PlSqlJoin::left('profile_job_enum', '$ME.id = pj.jobid'); |
6a99c3ac | 1063 | } |
3ac45f10 PC |
1064 | if ($this->with_pjt > 0) { |
1065 | for ($i = 1; $i <= $this->with_pjt; ++$i) { | |
1066 | $joins['pjt_'.$i] = PlSqlJoin::left('profile_job_term', '$ME.pid = $PID'); | |
1067 | $joins['pjtr_'.$i] = PlSqlJoin::left('profile_job_term_relation', '$ME.jtid_2 = pjt_'.$i.'.jtid'); | |
1068 | } | |
1069 | } | |
6a99c3ac RB |
1070 | return $joins; |
1071 | } | |
1072 | ||
0a2e9c74 RB |
1073 | /** NETWORKING |
1074 | */ | |
1075 | ||
1076 | private $with_pnw = false; | |
1077 | public function addNetworkingFilter() | |
1078 | { | |
b8dcf62d | 1079 | $this->requireAccounts(); |
0a2e9c74 RB |
1080 | $this->with_pnw = true; |
1081 | return 'pnw'; | |
1082 | } | |
1083 | ||
9b8e5fb4 | 1084 | protected function networkingJoins() |
0a2e9c74 RB |
1085 | { |
1086 | $joins = array(); | |
1087 | if ($this->with_pnw) { | |
5c412626 | 1088 | $joins['pnw'] = PlSqlJoin::left('profile_networking', '$ME.pid = $PID'); |
0a2e9c74 RB |
1089 | } |
1090 | return $joins; | |
1091 | } | |
1092 | ||
6d62969e RB |
1093 | /** PHONE |
1094 | */ | |
1095 | ||
2d83cac9 | 1096 | private $with_ptel = false; |
6d62969e RB |
1097 | |
1098 | public function addPhoneFilter() | |
1099 | { | |
b8dcf62d | 1100 | $this->requireAccounts(); |
2d83cac9 | 1101 | $this->with_ptel = true; |
6d62969e RB |
1102 | return 'ptel'; |
1103 | } | |
1104 | ||
9b8e5fb4 | 1105 | protected function phoneJoins() |
6d62969e RB |
1106 | { |
1107 | $joins = array(); | |
2d83cac9 | 1108 | if ($this->with_ptel) { |
5c412626 | 1109 | $joins['ptel'] = PlSqlJoin::left('profile_phones', '$ME.pid = $PID'); |
6d62969e RB |
1110 | } |
1111 | return $joins; | |
1112 | } | |
1113 | ||
ceb512d2 RB |
1114 | /** MEDALS |
1115 | */ | |
1116 | ||
2d83cac9 | 1117 | private $with_pmed = false; |
ceb512d2 RB |
1118 | public function addMedalFilter() |
1119 | { | |
b8dcf62d | 1120 | $this->requireProfiles(); |
2d83cac9 | 1121 | $this->with_pmed = true; |
ceb512d2 RB |
1122 | return 'pmed'; |
1123 | } | |
1124 | ||
9b8e5fb4 | 1125 | protected function medalJoins() |
ceb512d2 RB |
1126 | { |
1127 | $joins = array(); | |
2d83cac9 | 1128 | if ($this->with_pmed) { |
5c412626 | 1129 | $joins['pmed'] = PlSqlJoin::left('profile_medals', '$ME.pid = $PID'); |
ceb512d2 RB |
1130 | } |
1131 | return $joins; | |
1132 | } | |
1133 | ||
671b7073 RB |
1134 | /** MENTORING |
1135 | */ | |
1136 | ||
1137 | private $pms = array(); | |
459e6f81 | 1138 | private $mjtr = false; |
96f01fba RB |
1139 | const MENTOR = 1; |
1140 | const MENTOR_EXPERTISE = 2; | |
1141 | const MENTOR_COUNTRY = 3; | |
da40b2a4 | 1142 | const MENTOR_TERM = 4; |
671b7073 RB |
1143 | |
1144 | public function addMentorFilter($type) | |
1145 | { | |
b8dcf62d | 1146 | $this->requireAccounts(); |
671b7073 | 1147 | switch($type) { |
96f01fba RB |
1148 | case self::MENTOR: |
1149 | $this->pms['pm'] = 'profile_mentor'; | |
1150 | return 'pm'; | |
4a93c3a3 RB |
1151 | case self::MENTOR_EXPERTISE: |
1152 | $this->pms['pme'] = 'profile_mentor'; | |
671b7073 | 1153 | return 'pme'; |
4a93c3a3 RB |
1154 | case self::MENTOR_COUNTRY: |
1155 | $this->pms['pmc'] = 'profile_mentor_country'; | |
671b7073 | 1156 | return 'pmc'; |
459e6f81 PC |
1157 | case self::MENTOR_TERM: |
1158 | $this->pms['pmt'] = 'profile_mentor_term'; | |
1159 | $this->mjtr = true; | |
1160 | return 'mjtr'; | |
671b7073 | 1161 | default: |
5d2e55c7 | 1162 | Platal::page()->killError("Undefined mentor filter."); |
671b7073 RB |
1163 | } |
1164 | } | |
1165 | ||
9b8e5fb4 | 1166 | protected function mentorJoins() |
671b7073 RB |
1167 | { |
1168 | $joins = array(); | |
1169 | foreach ($this->pms as $sub => $tab) { | |
5c412626 | 1170 | $joins[$sub] = PlSqlJoin::left($tab, '$ME.pid = $PID'); |
671b7073 | 1171 | } |
459e6f81 PC |
1172 | if ($this->mjtr) { |
1173 | $joins['mjtr'] = PlSqlJoin::left('profile_job_term_relation', '$ME.jtid_2 = pmt.jtid'); | |
1174 | } | |
671b7073 RB |
1175 | return $joins; |
1176 | } | |
1177 | ||
3f42a6ad FB |
1178 | /** CONTACTS |
1179 | */ | |
1180 | private $cts = array(); | |
1181 | public function addContactFilter($uid = null) | |
1182 | { | |
c96da6c1 | 1183 | $this->requireProfiles(); |
3f42a6ad FB |
1184 | return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid); |
1185 | } | |
1186 | ||
9b8e5fb4 | 1187 | protected function contactJoins() |
3f42a6ad FB |
1188 | { |
1189 | $joins = array(); | |
1190 | foreach ($this->cts as $sub=>$key) { | |
1191 | if (is_null($key)) { | |
5c412626 | 1192 | $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.contact = $PID'); |
3f42a6ad | 1193 | } else { |
5c412626 | 1194 | $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.uid = {?} AND $ME.contact = $PID', substr($key, 5)); |
3f42a6ad FB |
1195 | } |
1196 | } | |
1197 | return $joins; | |
1198 | } | |
4e7bf1e0 FB |
1199 | |
1200 | ||
1201 | /** CARNET | |
1202 | */ | |
1203 | private $wn = array(); | |
1204 | public function addWatchRegistrationFilter($uid = null) | |
1205 | { | |
b8dcf62d | 1206 | $this->requireAccounts(); |
4e7bf1e0 FB |
1207 | return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid); |
1208 | } | |
1209 | ||
1210 | private $wp = array(); | |
1211 | public function addWatchPromoFilter($uid = null) | |
1212 | { | |
b8dcf62d | 1213 | $this->requireAccounts(); |
4e7bf1e0 FB |
1214 | return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid); |
1215 | } | |
1216 | ||
1217 | private $w = array(); | |
1218 | public function addWatchFilter($uid = null) | |
1219 | { | |
b8dcf62d | 1220 | $this->requireAccounts(); |
4e7bf1e0 FB |
1221 | return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid); |
1222 | } | |
1223 | ||
9b8e5fb4 | 1224 | protected function watchJoins() |
4e7bf1e0 FB |
1225 | { |
1226 | $joins = array(); | |
1227 | foreach ($this->w as $sub=>$key) { | |
1228 | if (is_null($key)) { | |
5c412626 | 1229 | $joins['w' . $sub] = PlSqlJoin::left('watch'); |
4e7bf1e0 | 1230 | } else { |
5c412626 | 1231 | $joins['w' . $sub] = PlSqlJoin::left('watch', '$ME.uid = {?}', substr($key, 5)); |
4e7bf1e0 FB |
1232 | } |
1233 | } | |
1234 | foreach ($this->wn as $sub=>$key) { | |
1235 | if (is_null($key)) { | |
5c412626 | 1236 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID'); |
4e7bf1e0 | 1237 | } else { |
5c412626 | 1238 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)); |
4e7bf1e0 FB |
1239 | } |
1240 | } | |
1241 | foreach ($this->wn as $sub=>$key) { | |
1242 | if (is_null($key)) { | |
5c412626 | 1243 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID'); |
4e7bf1e0 | 1244 | } else { |
5c412626 | 1245 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)); |
4e7bf1e0 FB |
1246 | } |
1247 | } | |
1248 | foreach ($this->wp as $sub=>$key) { | |
1249 | if (is_null($key)) { | |
5c412626 | 1250 | $joins['wp' . $sub] = PlSqlJoin::left('watch_promo'); |
4e7bf1e0 | 1251 | } else { |
5c412626 | 1252 | $joins['wp' . $sub] = PlSqlJoin::left('watch_promo', '$ME.uid = {?}', substr($key, 5)); |
4e7bf1e0 FB |
1253 | } |
1254 | } | |
1255 | return $joins; | |
1256 | } | |
48885bbe FB |
1257 | |
1258 | ||
470d14f6 FB |
1259 | /** PHOTOS |
1260 | */ | |
1261 | private $with_photo; | |
1262 | public function addPhotoFilter() | |
1263 | { | |
1264 | $this->requireProfiles(); | |
1265 | $this->with_photo = true; | |
7f26cd69 | 1266 | return 'photo'; |
470d14f6 FB |
1267 | } |
1268 | ||
1269 | protected function photoJoins() | |
1270 | { | |
1271 | if ($this->with_photo) { | |
1272 | return array('photo' => PlSqlJoin::left('profile_photos', '$ME.pid = $PID')); | |
1273 | } else { | |
1274 | return array(); | |
1275 | } | |
1276 | } | |
1277 | ||
1278 | ||
48885bbe FB |
1279 | /** MARKETING |
1280 | */ | |
1281 | private $with_rm; | |
1282 | public function addMarketingHash() | |
1283 | { | |
1284 | $this->requireAccounts(); | |
1285 | $this->with_rm = true; | |
1286 | } | |
1287 | ||
1288 | protected function marketingJoins() | |
1289 | { | |
1290 | if ($this->with_rm) { | |
5c412626 | 1291 | return array('rm' => PlSqlJoin::left('register_marketing', '$ME.uid = $UID')); |
48885bbe FB |
1292 | } else { |
1293 | return array(); | |
1294 | } | |
1295 | } | |
a087cc8d | 1296 | } |
8363588b | 1297 | // }}} |
3f42a6ad | 1298 | |
a7d9ab89 RB |
1299 | // {{{ class ProfileFilter |
1300 | class ProfileFilter extends UserFilter | |
1301 | { | |
434570c4 | 1302 | public function get($limit = null) |
a7d9ab89 RB |
1303 | { |
1304 | return $this->getProfiles($limit); | |
1305 | } | |
2daf7250 | 1306 | |
d5fd47bf SJ |
1307 | public function getIds($limit = null) |
1308 | { | |
1309 | return $this->getPIDs(); | |
1310 | } | |
1311 | ||
2daf7250 RB |
1312 | public function filter(array $profiles, $limit = null) |
1313 | { | |
1314 | return $this->filterProfiles($profiles, self::defaultLimit($limit)); | |
1315 | } | |
1316 | ||
1317 | public function getTotalCount() | |
1318 | { | |
1319 | return $this->getTotalProfileCount(); | |
1320 | } | |
ccc951d9 RB |
1321 | |
1322 | public function getGroups() | |
1323 | { | |
1324 | return $this->getPIDGroups(); | |
1325 | } | |
a7d9ab89 RB |
1326 | } |
1327 | // }}} | |
1328 | ||
a087cc8d FB |
1329 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
1330 | ?> |