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 | ||
cc9a4dad 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 | ||
82e1ce6c | 455 | public function setCondition(PlFilterCondition $cond) |
a087cc8d FB |
456 | { |
457 | $this->root =& $cond; | |
784745ce | 458 | $this->query = null; |
a087cc8d FB |
459 | } |
460 | ||
82e1ce6c | 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 | ||
7f323fd4 FB |
471 | public function export() |
472 | { | |
5a8306c2 | 473 | $export = array('conditions' => $this->root->export()); |
7f323fd4 | 474 | if (!empty($this->sort)) { |
5a8306c2 | 475 | $export['sorts'] = array(); |
7f323fd4 | 476 | foreach ($this->sort as $sort) { |
5a8306c2 | 477 | $export['sorts'][] = $sort->export(); |
7f323fd4 FB |
478 | } |
479 | } | |
480 | return $export; | |
481 | } | |
482 | ||
5a8306c2 FB |
483 | public function exportConditions() |
484 | { | |
485 | return $this->root->export(); | |
486 | } | |
487 | ||
488 | public static function fromExport(array $export) | |
489 | { | |
490 | $export = new PlDict($export); | |
491 | if (!$export->has('conditions')) { | |
492 | throw new Exception("Cannot build a user filter without conditions"); | |
493 | } | |
494 | $cond = UserFilterCondition::fromExport($export->v('conditions')); | |
495 | $sorts = null; | |
496 | if ($export->has('sorts')) { | |
497 | $sorts = array(); | |
498 | foreach ($export->v('sorts') as $sort) { | |
499 | $sorts[] = UserFilterOrder::fromExport($sort); | |
500 | } | |
501 | } | |
502 | return new UserFilter($cond, $sorts); | |
503 | } | |
504 | ||
505 | public static function fromJSon($json) | |
506 | { | |
507 | $export = json_decode($json, true); | |
508 | if (is_null($export)) { | |
509 | throw new Exception("Invalid json: $json"); | |
510 | } | |
511 | return self::fromExport($json); | |
512 | } | |
513 | ||
514 | public static function fromExportedConditions(array $export) | |
515 | { | |
516 | $cond = UserFilterCondition::fromExport($export); | |
517 | return new UserFilter($cond); | |
518 | } | |
519 | ||
520 | public static function fromJSonConditions($json) | |
521 | { | |
522 | $export = json_decode($json, true); | |
523 | if (is_null($export)) { | |
524 | throw new Exception("Invalid json: $json"); | |
525 | } | |
526 | return self::fromExportedConditions($json); | |
527 | } | |
528 | ||
a087cc8d FB |
529 | static public function getLegacy($promo_min, $promo_max) |
530 | { | |
a087cc8d | 531 | if ($promo_min != 0) { |
784745ce | 532 | $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min)); |
5dd9d823 | 533 | } else { |
88c31faf | 534 | $min = new PFC_True(); |
a087cc8d | 535 | } |
a087cc8d | 536 | if ($promo_max != 0) { |
784745ce | 537 | $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max)); |
a087cc8d | 538 | } else { |
88c31faf | 539 | $max = new PFC_True(); |
a087cc8d | 540 | } |
9b8e5fb4 | 541 | return new UserFilter(new PFC_And($min, $max)); |
a087cc8d | 542 | } |
784745ce | 543 | |
07eb5b0e FB |
544 | static public function sortByName() |
545 | { | |
913a4e90 | 546 | return array(new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME)); |
07eb5b0e FB |
547 | } |
548 | ||
549 | static public function sortByPromo() | |
550 | { | |
913a4e90 | 551 | return array(new UFO_Promo(), new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME)); |
07eb5b0e FB |
552 | } |
553 | ||
aa21c568 FB |
554 | static private function getDBSuffix($string) |
555 | { | |
2d6329a2 FB |
556 | if (is_array($string)) { |
557 | if (count($string) == 1) { | |
558 | return self::getDBSuffix(array_pop($string)); | |
559 | } | |
560 | return md5(implode('|', $string)); | |
561 | } else { | |
562 | return preg_replace('/[^a-z0-9]/i', '', $string); | |
563 | } | |
aa21c568 FB |
564 | } |
565 | ||
566 | ||
2d83cac9 RB |
567 | /** Stores a new (and unique) table alias in the &$table table |
568 | * @param &$table Array in which the table alias must be stored | |
569 | * @param $val Value which will then be used to build the join | |
570 | * @return Name of the newly created alias | |
571 | */ | |
aa21c568 FB |
572 | private $option = 0; |
573 | private function register_optional(array &$table, $val) | |
574 | { | |
575 | if (is_null($val)) { | |
576 | $sub = $this->option++; | |
577 | $index = null; | |
578 | } else { | |
579 | $sub = self::getDBSuffix($val); | |
580 | $index = $val; | |
581 | } | |
582 | $sub = '_' . $sub; | |
583 | $table[$sub] = $index; | |
584 | return $sub; | |
585 | } | |
784745ce | 586 | |
b8dcf62d RB |
587 | /** PROFILE VS ACCOUNT |
588 | */ | |
f7ea7450 RB |
589 | private $with_profiles = false; |
590 | private $with_accounts = false; | |
b8dcf62d RB |
591 | public function requireAccounts() |
592 | { | |
593 | $this->with_accounts = true; | |
594 | } | |
595 | ||
a9ef52c9 RB |
596 | public function accountsRequired() |
597 | { | |
598 | return $this->with_accounts; | |
599 | } | |
600 | ||
b8dcf62d RB |
601 | public function requireProfiles() |
602 | { | |
603 | $this->with_profiles = true; | |
604 | } | |
605 | ||
a9ef52c9 RB |
606 | public function profilesRequired() |
607 | { | |
608 | return $this->with_profiles; | |
609 | } | |
610 | ||
b8dcf62d RB |
611 | protected function accountJoins() |
612 | { | |
613 | $joins = array(); | |
2a93b634 | 614 | if ($this->with_profiles && $this->with_accounts) { |
5c412626 FB |
615 | $joins['ap'] = PlSqlJoin::left('account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)'); |
616 | $joins['p'] = PlSqlJoin::left('profiles', '$PID = ap.pid'); | |
b8dcf62d RB |
617 | } |
618 | return $joins; | |
619 | } | |
620 | ||
1eabd2a4 FB |
621 | /** PERMISSIONS |
622 | */ | |
623 | private $at = false; | |
624 | public function requirePerms() | |
625 | { | |
626 | $this->requireAccounts(); | |
627 | $this->at = true; | |
628 | return 'at'; | |
629 | } | |
630 | ||
631 | protected function permJoins() | |
632 | { | |
633 | if ($this->at) { | |
634 | return array('at' => PlSqlJoin::left('account_types', '$ME.type = a.type')); | |
635 | } else { | |
636 | return array(); | |
637 | } | |
638 | } | |
639 | ||
d865c296 FB |
640 | /** DISPLAY |
641 | */ | |
38c6fe96 | 642 | const DISPLAY = 'display'; |
d865c296 FB |
643 | private $pd = false; |
644 | public function addDisplayFilter() | |
645 | { | |
b8dcf62d | 646 | $this->requireProfiles(); |
d865c296 FB |
647 | $this->pd = true; |
648 | return ''; | |
649 | } | |
650 | ||
9b8e5fb4 | 651 | protected function displayJoins() |
d865c296 FB |
652 | { |
653 | if ($this->pd) { | |
5c412626 | 654 | return array('pd' => PlSqlJoin::left('profile_display', '$ME.pid = $PID')); |
d865c296 FB |
655 | } else { |
656 | return array(); | |
657 | } | |
658 | } | |
659 | ||
f73a4f1b RB |
660 | /** LOGGER |
661 | */ | |
662 | ||
663 | private $with_logger = false; | |
664 | public function addLoggerFilter() | |
665 | { | |
666 | $this->with_logger = true; | |
667 | $this->requireAccounts(); | |
668 | return 'ls'; | |
669 | } | |
670 | protected function loggerJoins() | |
671 | { | |
672 | $joins = array(); | |
673 | if ($this->with_logger) { | |
5c412626 | 674 | $joins['ls'] = PlSqlJoin::left('log_sessions', '$ME.uid = $UID'); |
f73a4f1b RB |
675 | } |
676 | return $joins; | |
677 | } | |
678 | ||
784745ce FB |
679 | /** NAMES |
680 | */ | |
784745ce FB |
681 | |
682 | static public function assertName($name) | |
683 | { | |
07613cdd | 684 | if (!DirEnum::getID(DirEnum::NAMETYPES, $name)) { |
9b8e5fb4 | 685 | Platal::page()->kill('Invalid name type: ' . $name); |
784745ce FB |
686 | } |
687 | } | |
688 | ||
689 | private $pn = array(); | |
784745ce FB |
690 | public function addNameFilter($type, $variant = null) |
691 | { | |
b8dcf62d | 692 | $this->requireProfiles(); |
784745ce FB |
693 | if (!is_null($variant)) { |
694 | $ft = $type . '_' . $variant; | |
695 | } else { | |
696 | $ft = $type; | |
697 | } | |
698 | $sub = '_' . $ft; | |
699 | self::assertName($ft); | |
700 | ||
701 | if (!is_null($variant) && $variant == 'other') { | |
aa21c568 | 702 | $sub .= $this->option++; |
784745ce | 703 | } |
07613cdd | 704 | $this->pn[$sub] = DirEnum::getID(DirEnum::NAMETYPES, $ft); |
784745ce FB |
705 | return $sub; |
706 | } | |
707 | ||
9b8e5fb4 | 708 | protected function nameJoins() |
784745ce FB |
709 | { |
710 | $joins = array(); | |
711 | foreach ($this->pn as $sub => $type) { | |
5c412626 | 712 | $joins['pn' . $sub] = PlSqlJoin::left('profile_name', '$ME.pid = $PID AND $ME.typeid = {?}', $type); |
784745ce FB |
713 | } |
714 | return $joins; | |
715 | } | |
716 | ||
40585144 RB |
717 | /** NAMETOKENS |
718 | */ | |
2a93b634 RB |
719 | private $name_tokens = array(); |
720 | private $nb_tokens = 0; | |
721 | ||
722 | public function addNameTokensFilter($token) | |
40585144 RB |
723 | { |
724 | $this->requireProfiles(); | |
2a93b634 RB |
725 | $sub = 'sn' . (1 + $this->nb_tokens); |
726 | $this->nb_tokens++; | |
727 | $this->name_tokens[$sub] = $token; | |
728 | return $sub; | |
40585144 RB |
729 | } |
730 | ||
731 | protected function nameTokensJoins() | |
732 | { | |
f7ea7450 | 733 | /* We don't return joins, since with_sn forces the SELECT to run on search_name first */ |
2a93b634 RB |
734 | $joins = array(); |
735 | foreach ($this->name_tokens as $sub => $token) { | |
736 | $joins[$sub] = PlSqlJoin::left('search_name', '$ME.pid = $PID'); | |
40585144 | 737 | } |
2a93b634 RB |
738 | return $joins; |
739 | } | |
740 | ||
741 | public function getNameTokens() | |
742 | { | |
743 | return $this->name_tokens; | |
40585144 RB |
744 | } |
745 | ||
a7f8e48a RB |
746 | /** NATIONALITY |
747 | */ | |
748 | ||
749 | private $with_nat = false; | |
750 | public function addNationalityFilter() | |
751 | { | |
752 | $this->with_nat = true; | |
753 | return 'ngc'; | |
754 | } | |
755 | ||
756 | protected function nationalityJoins() | |
757 | { | |
758 | $joins = array(); | |
759 | if ($this->with_nat) { | |
5c412626 | 760 | $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 |
761 | } |
762 | return $joins; | |
763 | } | |
764 | ||
784745ce FB |
765 | /** EDUCATION |
766 | */ | |
767 | const GRADE_ING = 'Ing.'; | |
768 | const GRADE_PHD = 'PhD'; | |
769 | const GRADE_MST = 'M%'; | |
770 | static public function isGrade($grade) | |
771 | { | |
93c2f133 | 772 | return ($grade !== 0) && ($grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST); |
784745ce FB |
773 | } |
774 | ||
775 | static public function assertGrade($grade) | |
776 | { | |
777 | if (!self::isGrade($grade)) { | |
ad27b22e | 778 | Platal::page()->killError("Diplôme non valide: $grade"); |
784745ce FB |
779 | } |
780 | } | |
781 | ||
d865c296 FB |
782 | static public function promoYear($grade) |
783 | { | |
784 | // XXX: Definition of promotion for phds and masters might change in near future. | |
785 | return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year'; | |
786 | } | |
787 | ||
784745ce FB |
788 | private $pepe = array(); |
789 | private $with_pee = false; | |
784745ce FB |
790 | public function addEducationFilter($x = false, $grade = null) |
791 | { | |
b8dcf62d | 792 | $this->requireProfiles(); |
784745ce | 793 | if (!$x) { |
aa21c568 FB |
794 | $index = $this->option; |
795 | $sub = $this->option++; | |
784745ce FB |
796 | } else { |
797 | self::assertGrade($grade); | |
798 | $index = $grade; | |
799 | $sub = $grade[0]; | |
800 | $this->with_pee = true; | |
801 | } | |
802 | $sub = '_' . $sub; | |
803 | $this->pepe[$index] = $sub; | |
804 | return $sub; | |
805 | } | |
806 | ||
9b8e5fb4 | 807 | protected function educationJoins() |
784745ce FB |
808 | { |
809 | $joins = array(); | |
810 | if ($this->with_pee) { | |
5c412626 | 811 | $joins['pee'] = PlSqlJoin::inner('profile_education_enum', 'pee.abbreviation = \'X\''); |
784745ce FB |
812 | } |
813 | foreach ($this->pepe as $grade => $sub) { | |
814 | if ($this->isGrade($grade)) { | |
5c412626 FB |
815 | $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID'); |
816 | $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE {?}', $grade); | |
784745ce | 817 | } else { |
5c412626 FB |
818 | $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.pid = $PID'); |
819 | $joins['pee' . $sub] = PlSqlJoin::inner('profile_education_enum', '$ME.id = pe' . $sub . '.eduid'); | |
820 | $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid'); | |
784745ce FB |
821 | } |
822 | } | |
823 | return $joins; | |
824 | } | |
4927ee54 FB |
825 | |
826 | ||
827 | /** GROUPS | |
828 | */ | |
829 | private $gpm = array(); | |
4927ee54 FB |
830 | public function addGroupFilter($group = null) |
831 | { | |
b8dcf62d | 832 | $this->requireAccounts(); |
4927ee54 | 833 | if (!is_null($group)) { |
4aae4d2c | 834 | if (is_int($group) || ctype_digit($group)) { |
4927ee54 FB |
835 | $index = $sub = $group; |
836 | } else { | |
837 | $index = $group; | |
aa21c568 | 838 | $sub = self::getDBSuffix($group); |
4927ee54 FB |
839 | } |
840 | } else { | |
aa21c568 | 841 | $sub = 'group_' . $this->option++; |
4927ee54 FB |
842 | $index = null; |
843 | } | |
844 | $sub = '_' . $sub; | |
845 | $this->gpm[$sub] = $index; | |
846 | return $sub; | |
847 | } | |
848 | ||
9b8e5fb4 | 849 | protected function groupJoins() |
4927ee54 FB |
850 | { |
851 | $joins = array(); | |
852 | foreach ($this->gpm as $sub => $key) { | |
853 | if (is_null($key)) { | |
5c412626 FB |
854 | $joins['gpa' . $sub] = PlSqlJoin::inner('groups'); |
855 | $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
4aae4d2c | 856 | } else if (is_int($key) || ctype_digit($key)) { |
5c412626 | 857 | $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key); |
4927ee54 | 858 | } else { |
5c412626 FB |
859 | $joins['gpa' . $sub] = PlSqlJoin::inner('groups', '$ME.diminutif = {?}', $key); |
860 | $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
4927ee54 FB |
861 | } |
862 | } | |
863 | return $joins; | |
0fb3713c RB |
864 | } |
865 | ||
866 | /** BINETS | |
867 | */ | |
868 | ||
a7f8e48a RB |
869 | private $with_bi = false; |
870 | private $with_bd = false; | |
d7ddf29b | 871 | public function addBinetsFilter($with_enum = false) |
0fb3713c RB |
872 | { |
873 | $this->requireProfiles(); | |
a7f8e48a RB |
874 | $this->with_bi = true; |
875 | if ($with_enum) { | |
876 | $this->with_bd = true; | |
877 | return 'bd'; | |
878 | } else { | |
879 | return 'bi'; | |
880 | } | |
0fb3713c RB |
881 | } |
882 | ||
883 | protected function binetsJoins() | |
884 | { | |
885 | $joins = array(); | |
a7f8e48a | 886 | if ($this->with_bi) { |
5c412626 | 887 | $joins['bi'] = PlSqlJoin::left('profile_binets', '$ME.pid = $PID'); |
0fb3713c | 888 | } |
a7f8e48a | 889 | if ($this->with_bd) { |
5c412626 | 890 | $joins['bd'] = PlSqlJoin::left('profile_binet_enum', '$ME.id = bi.binet_id'); |
a7f8e48a | 891 | } |
0fb3713c | 892 | return $joins; |
4927ee54 | 893 | } |
aa21c568 FB |
894 | |
895 | /** EMAILS | |
896 | */ | |
897 | private $e = array(); | |
898 | public function addEmailRedirectFilter($email = null) | |
899 | { | |
b8dcf62d | 900 | $this->requireAccounts(); |
aa21c568 FB |
901 | return $this->register_optional($this->e, $email); |
902 | } | |
903 | ||
904 | private $ve = array(); | |
905 | public function addVirtualEmailFilter($email = null) | |
906 | { | |
21401768 | 907 | $this->addAliasFilter(self::ALIAS_FORLIFE); |
aa21c568 FB |
908 | return $this->register_optional($this->ve, $email); |
909 | } | |
910 | ||
21401768 FB |
911 | const ALIAS_BEST = 'bestalias'; |
912 | const ALIAS_FORLIFE = 'forlife'; | |
aa21c568 FB |
913 | private $al = array(); |
914 | public function addAliasFilter($alias = null) | |
915 | { | |
b8dcf62d | 916 | $this->requireAccounts(); |
aa21c568 FB |
917 | return $this->register_optional($this->al, $alias); |
918 | } | |
919 | ||
9b8e5fb4 | 920 | protected function emailJoins() |
aa21c568 FB |
921 | { |
922 | global $globals; | |
923 | $joins = array(); | |
924 | foreach ($this->e as $sub=>$key) { | |
925 | if (is_null($key)) { | |
5c412626 | 926 | $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\''); |
aa21c568 | 927 | } else { |
2d6329a2 FB |
928 | if (!is_array($key)) { |
929 | $key = array($key); | |
930 | } | |
aab2ffdd | 931 | $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\' |
2f1c94e0 | 932 | AND $ME.email IN {?}', $key); |
aa21c568 FB |
933 | } |
934 | } | |
21401768 | 935 | foreach ($this->al as $sub=>$key) { |
aa21c568 | 936 | if (is_null($key)) { |
5c412626 | 937 | $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')'); |
21401768 | 938 | } else if ($key == self::ALIAS_BEST) { |
5c412626 | 939 | $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)'); |
21401768 | 940 | } else if ($key == self::ALIAS_FORLIFE) { |
5c412626 | 941 | $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type = \'a_vie\''); |
aa21c568 | 942 | } else { |
2d6329a2 FB |
943 | if (!is_array($key)) { |
944 | $key = array($key); | |
945 | } | |
aab2ffdd | 946 | $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') |
bde68f05 | 947 | AND $ME.alias IN {?}', $key); |
aa21c568 | 948 | } |
aa21c568 | 949 | } |
21401768 | 950 | foreach ($this->ve as $sub=>$key) { |
aa21c568 | 951 | if (is_null($key)) { |
5c412626 | 952 | $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\''); |
aa21c568 | 953 | } else { |
2d6329a2 FB |
954 | if (!is_array($key)) { |
955 | $key = array($key); | |
956 | } | |
bde68f05 | 957 | $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\' AND $ME.alias IN {?}', $key); |
aa21c568 | 958 | } |
5c412626 FB |
959 | $joins['vr' . $sub] = PlSqlJoin::left('virtual_redirect', |
960 | '$ME.vid = v' . $sub . '.vid | |
961 | AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}), | |
962 | CONCAT(al_forlife.alias, \'@\', {?}), | |
963 | a.email))', | |
964 | $globals->mail->domain, $globals->mail->domain2); | |
aa21c568 FB |
965 | } |
966 | return $joins; | |
967 | } | |
3f42a6ad FB |
968 | |
969 | ||
c4b24511 RB |
970 | /** ADDRESSES |
971 | */ | |
036d1637 | 972 | private $with_pa = false; |
c4b24511 RB |
973 | public function addAddressFilter() |
974 | { | |
b8dcf62d | 975 | $this->requireProfiles(); |
036d1637 RB |
976 | $this->with_pa = true; |
977 | return 'pa'; | |
c4b24511 RB |
978 | } |
979 | ||
2b9ca54d RB |
980 | private $with_pac = false; |
981 | public function addAddressCountryFilter() | |
982 | { | |
983 | $this->requireProfiles(); | |
984 | $this->addAddressFilter(); | |
985 | $this->with_pac = true; | |
986 | return 'gc'; | |
987 | } | |
988 | ||
d7ddf29b | 989 | private $with_pal = false; |
2b9ca54d RB |
990 | public function addAddressLocalityFilter() |
991 | { | |
992 | $this->requireProfiles(); | |
993 | $this->addAddressFilter(); | |
994 | $this->with_pal = true; | |
995 | return 'gl'; | |
996 | } | |
997 | ||
9b8e5fb4 | 998 | protected function addressJoins() |
c4b24511 RB |
999 | { |
1000 | $joins = array(); | |
036d1637 | 1001 | if ($this->with_pa) { |
5c412626 | 1002 | $joins['pa'] = PlSqlJoin::left('profile_addresses', '$ME.pid = $PID'); |
c4b24511 | 1003 | } |
2b9ca54d | 1004 | if ($this->with_pac) { |
5c412626 | 1005 | $joins['gc'] = PlSqlJoin::left('geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID'); |
2b9ca54d RB |
1006 | } |
1007 | if ($this->with_pal) { | |
5c412626 | 1008 | $joins['gl'] = PlSqlJoin::left('geoloc_localities', '$ME.id = pa.localityID'); |
2b9ca54d | 1009 | } |
c4b24511 RB |
1010 | return $joins; |
1011 | } | |
1012 | ||
1013 | ||
4083b126 RB |
1014 | /** CORPS |
1015 | */ | |
1016 | ||
1017 | private $pc = false; | |
1018 | private $pce = array(); | |
1019 | private $pcr = false; | |
1020 | public function addCorpsFilter($type) | |
1021 | { | |
b8dcf62d | 1022 | $this->requireProfiles(); |
4083b126 RB |
1023 | $this->pc = true; |
1024 | if ($type == UFC_Corps::CURRENT) { | |
1025 | $pce['pcec'] = 'current_corpsid'; | |
1026 | return 'pcec'; | |
1027 | } else if ($type == UFC_Corps::ORIGIN) { | |
1028 | $pce['pceo'] = 'original_corpsid'; | |
1029 | return 'pceo'; | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | public function addCorpsRankFilter() | |
1034 | { | |
b8dcf62d | 1035 | $this->requireProfiles(); |
4083b126 RB |
1036 | $this->pc = true; |
1037 | $this->pcr = true; | |
1038 | return 'pcr'; | |
1039 | } | |
1040 | ||
9b8e5fb4 | 1041 | protected function corpsJoins() |
4083b126 RB |
1042 | { |
1043 | $joins = array(); | |
1044 | if ($this->pc) { | |
5c412626 | 1045 | $joins['pc'] = PlSqlJoin::left('profile_corps', '$ME.pid = $PID'); |
4083b126 RB |
1046 | } |
1047 | if ($this->pcr) { | |
5c412626 | 1048 | $joins['pcr'] = PlSqlJoin::left('profile_corps_rank_enum', '$ME.id = pc.rankid'); |
4083b126 RB |
1049 | } |
1050 | foreach($this->pce as $sub => $field) { | |
5c412626 | 1051 | $joins[$sub] = PlSqlJoin::left('profile_corps_enum', '$ME.id = pc.' . $field); |
4083b126 RB |
1052 | } |
1053 | return $joins; | |
1054 | } | |
1055 | ||
6a99c3ac RB |
1056 | /** JOBS |
1057 | */ | |
1058 | ||
da40b2a4 SJ |
1059 | const JOB_USERDEFINED = 0x0001; |
1060 | const JOB_CV = 0x0002; | |
1061 | const JOB_ANY = 0x0003; | |
6a99c3ac RB |
1062 | |
1063 | /** Joins : | |
1064 | * pj => profile_job | |
1065 | * pje => profile_job_enum | |
3ac45f10 | 1066 | * pjt => profile_job_terms |
6a99c3ac | 1067 | */ |
da40b2a4 | 1068 | private $with_pj = false; |
6a99c3ac | 1069 | private $with_pje = false; |
3ac45f10 | 1070 | private $with_pjt = 0; |
6a99c3ac RB |
1071 | |
1072 | public function addJobFilter() | |
1073 | { | |
b8dcf62d | 1074 | $this->requireProfiles(); |
6a99c3ac RB |
1075 | $this->with_pj = true; |
1076 | return 'pj'; | |
1077 | } | |
1078 | ||
1079 | public function addJobCompanyFilter() | |
1080 | { | |
1081 | $this->addJobFilter(); | |
1082 | $this->with_pje = true; | |
1083 | return 'pje'; | |
1084 | } | |
1085 | ||
3ac45f10 PC |
1086 | /** |
1087 | * Adds a filter on job terms of profile. | |
1088 | * @param $nb the number of job terms to use | |
1089 | * @return an array of the fields to filter (one for each term). | |
3ac45f10 PC |
1090 | */ |
1091 | public function addJobTermsFilter($nb = 1) | |
1092 | { | |
1093 | $this->with_pjt = $nb; | |
1094 | $jobtermstable = array(); | |
1095 | for ($i = 1; $i <= $nb; ++$i) { | |
4ec03752 | 1096 | $jobtermstable[] = 'pjtr_'.$i; |
3ac45f10 PC |
1097 | } |
1098 | return $jobtermstable; | |
1099 | } | |
1100 | ||
9b8e5fb4 | 1101 | protected function jobJoins() |
6a99c3ac RB |
1102 | { |
1103 | $joins = array(); | |
1104 | if ($this->with_pj) { | |
5c412626 | 1105 | $joins['pj'] = PlSqlJoin::left('profile_job', '$ME.pid = $PID'); |
6a99c3ac RB |
1106 | } |
1107 | if ($this->with_pje) { | |
5c412626 | 1108 | $joins['pje'] = PlSqlJoin::left('profile_job_enum', '$ME.id = pj.jobid'); |
6a99c3ac | 1109 | } |
3ac45f10 PC |
1110 | if ($this->with_pjt > 0) { |
1111 | for ($i = 1; $i <= $this->with_pjt; ++$i) { | |
1112 | $joins['pjt_'.$i] = PlSqlJoin::left('profile_job_term', '$ME.pid = $PID'); | |
1113 | $joins['pjtr_'.$i] = PlSqlJoin::left('profile_job_term_relation', '$ME.jtid_2 = pjt_'.$i.'.jtid'); | |
1114 | } | |
1115 | } | |
6a99c3ac RB |
1116 | return $joins; |
1117 | } | |
1118 | ||
0a2e9c74 RB |
1119 | /** NETWORKING |
1120 | */ | |
1121 | ||
1122 | private $with_pnw = false; | |
1123 | public function addNetworkingFilter() | |
1124 | { | |
b8dcf62d | 1125 | $this->requireAccounts(); |
0a2e9c74 RB |
1126 | $this->with_pnw = true; |
1127 | return 'pnw'; | |
1128 | } | |
1129 | ||
9b8e5fb4 | 1130 | protected function networkingJoins() |
0a2e9c74 RB |
1131 | { |
1132 | $joins = array(); | |
1133 | if ($this->with_pnw) { | |
5c412626 | 1134 | $joins['pnw'] = PlSqlJoin::left('profile_networking', '$ME.pid = $PID'); |
0a2e9c74 RB |
1135 | } |
1136 | return $joins; | |
1137 | } | |
1138 | ||
6d62969e RB |
1139 | /** PHONE |
1140 | */ | |
1141 | ||
2d83cac9 | 1142 | private $with_ptel = false; |
6d62969e RB |
1143 | |
1144 | public function addPhoneFilter() | |
1145 | { | |
b8dcf62d | 1146 | $this->requireAccounts(); |
2d83cac9 | 1147 | $this->with_ptel = true; |
6d62969e RB |
1148 | return 'ptel'; |
1149 | } | |
1150 | ||
9b8e5fb4 | 1151 | protected function phoneJoins() |
6d62969e RB |
1152 | { |
1153 | $joins = array(); | |
2d83cac9 | 1154 | if ($this->with_ptel) { |
5c412626 | 1155 | $joins['ptel'] = PlSqlJoin::left('profile_phones', '$ME.pid = $PID'); |
6d62969e RB |
1156 | } |
1157 | return $joins; | |
1158 | } | |
1159 | ||
ceb512d2 RB |
1160 | /** MEDALS |
1161 | */ | |
1162 | ||
2d83cac9 | 1163 | private $with_pmed = false; |
ceb512d2 RB |
1164 | public function addMedalFilter() |
1165 | { | |
b8dcf62d | 1166 | $this->requireProfiles(); |
2d83cac9 | 1167 | $this->with_pmed = true; |
ceb512d2 RB |
1168 | return 'pmed'; |
1169 | } | |
1170 | ||
9b8e5fb4 | 1171 | protected function medalJoins() |
ceb512d2 RB |
1172 | { |
1173 | $joins = array(); | |
2d83cac9 | 1174 | if ($this->with_pmed) { |
5c412626 | 1175 | $joins['pmed'] = PlSqlJoin::left('profile_medals', '$ME.pid = $PID'); |
ceb512d2 RB |
1176 | } |
1177 | return $joins; | |
1178 | } | |
1179 | ||
671b7073 RB |
1180 | /** MENTORING |
1181 | */ | |
1182 | ||
1183 | private $pms = array(); | |
459e6f81 | 1184 | private $mjtr = false; |
96f01fba RB |
1185 | const MENTOR = 1; |
1186 | const MENTOR_EXPERTISE = 2; | |
1187 | const MENTOR_COUNTRY = 3; | |
da40b2a4 | 1188 | const MENTOR_TERM = 4; |
671b7073 RB |
1189 | |
1190 | public function addMentorFilter($type) | |
1191 | { | |
b8dcf62d | 1192 | $this->requireAccounts(); |
671b7073 | 1193 | switch($type) { |
96f01fba RB |
1194 | case self::MENTOR: |
1195 | $this->pms['pm'] = 'profile_mentor'; | |
1196 | return 'pm'; | |
4a93c3a3 RB |
1197 | case self::MENTOR_EXPERTISE: |
1198 | $this->pms['pme'] = 'profile_mentor'; | |
671b7073 | 1199 | return 'pme'; |
4a93c3a3 RB |
1200 | case self::MENTOR_COUNTRY: |
1201 | $this->pms['pmc'] = 'profile_mentor_country'; | |
671b7073 | 1202 | return 'pmc'; |
459e6f81 PC |
1203 | case self::MENTOR_TERM: |
1204 | $this->pms['pmt'] = 'profile_mentor_term'; | |
1205 | $this->mjtr = true; | |
1206 | return 'mjtr'; | |
671b7073 | 1207 | default: |
5d2e55c7 | 1208 | Platal::page()->killError("Undefined mentor filter."); |
671b7073 RB |
1209 | } |
1210 | } | |
1211 | ||
9b8e5fb4 | 1212 | protected function mentorJoins() |
671b7073 RB |
1213 | { |
1214 | $joins = array(); | |
1215 | foreach ($this->pms as $sub => $tab) { | |
5c412626 | 1216 | $joins[$sub] = PlSqlJoin::left($tab, '$ME.pid = $PID'); |
671b7073 | 1217 | } |
459e6f81 PC |
1218 | if ($this->mjtr) { |
1219 | $joins['mjtr'] = PlSqlJoin::left('profile_job_term_relation', '$ME.jtid_2 = pmt.jtid'); | |
1220 | } | |
671b7073 RB |
1221 | return $joins; |
1222 | } | |
1223 | ||
3f42a6ad FB |
1224 | /** CONTACTS |
1225 | */ | |
1226 | private $cts = array(); | |
1227 | public function addContactFilter($uid = null) | |
1228 | { | |
c96da6c1 | 1229 | $this->requireProfiles(); |
3f42a6ad FB |
1230 | return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid); |
1231 | } | |
1232 | ||
9b8e5fb4 | 1233 | protected function contactJoins() |
3f42a6ad FB |
1234 | { |
1235 | $joins = array(); | |
1236 | foreach ($this->cts as $sub=>$key) { | |
1237 | if (is_null($key)) { | |
5c412626 | 1238 | $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.contact = $PID'); |
3f42a6ad | 1239 | } else { |
5c412626 | 1240 | $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.uid = {?} AND $ME.contact = $PID', substr($key, 5)); |
3f42a6ad FB |
1241 | } |
1242 | } | |
1243 | return $joins; | |
1244 | } | |
4e7bf1e0 FB |
1245 | |
1246 | ||
1247 | /** CARNET | |
1248 | */ | |
1249 | private $wn = array(); | |
1250 | public function addWatchRegistrationFilter($uid = null) | |
1251 | { | |
b8dcf62d | 1252 | $this->requireAccounts(); |
4e7bf1e0 FB |
1253 | return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid); |
1254 | } | |
1255 | ||
1256 | private $wp = array(); | |
1257 | public function addWatchPromoFilter($uid = null) | |
1258 | { | |
b8dcf62d | 1259 | $this->requireAccounts(); |
4e7bf1e0 FB |
1260 | return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid); |
1261 | } | |
1262 | ||
1263 | private $w = array(); | |
1264 | public function addWatchFilter($uid = null) | |
1265 | { | |
b8dcf62d | 1266 | $this->requireAccounts(); |
4e7bf1e0 FB |
1267 | return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid); |
1268 | } | |
1269 | ||
9b8e5fb4 | 1270 | protected function watchJoins() |
4e7bf1e0 FB |
1271 | { |
1272 | $joins = array(); | |
1273 | foreach ($this->w as $sub=>$key) { | |
1274 | if (is_null($key)) { | |
5c412626 | 1275 | $joins['w' . $sub] = PlSqlJoin::left('watch'); |
4e7bf1e0 | 1276 | } else { |
5c412626 | 1277 | $joins['w' . $sub] = PlSqlJoin::left('watch', '$ME.uid = {?}', substr($key, 5)); |
4e7bf1e0 FB |
1278 | } |
1279 | } | |
1280 | foreach ($this->wn as $sub=>$key) { | |
1281 | if (is_null($key)) { | |
5c412626 | 1282 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID'); |
4e7bf1e0 | 1283 | } else { |
5c412626 | 1284 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)); |
4e7bf1e0 FB |
1285 | } |
1286 | } | |
1287 | foreach ($this->wn as $sub=>$key) { | |
1288 | if (is_null($key)) { | |
5c412626 | 1289 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID'); |
4e7bf1e0 | 1290 | } else { |
5c412626 | 1291 | $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)); |
4e7bf1e0 FB |
1292 | } |
1293 | } | |
1294 | foreach ($this->wp as $sub=>$key) { | |
1295 | if (is_null($key)) { | |
5c412626 | 1296 | $joins['wp' . $sub] = PlSqlJoin::left('watch_promo'); |
4e7bf1e0 | 1297 | } else { |
5c412626 | 1298 | $joins['wp' . $sub] = PlSqlJoin::left('watch_promo', '$ME.uid = {?}', substr($key, 5)); |
4e7bf1e0 FB |
1299 | } |
1300 | } | |
1301 | return $joins; | |
1302 | } | |
48885bbe FB |
1303 | |
1304 | ||
470d14f6 FB |
1305 | /** PHOTOS |
1306 | */ | |
1307 | private $with_photo; | |
1308 | public function addPhotoFilter() | |
1309 | { | |
1310 | $this->requireProfiles(); | |
1311 | $this->with_photo = true; | |
7f26cd69 | 1312 | return 'photo'; |
470d14f6 FB |
1313 | } |
1314 | ||
1315 | protected function photoJoins() | |
1316 | { | |
1317 | if ($this->with_photo) { | |
1318 | return array('photo' => PlSqlJoin::left('profile_photos', '$ME.pid = $PID')); | |
1319 | } else { | |
1320 | return array(); | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | ||
48885bbe FB |
1325 | /** MARKETING |
1326 | */ | |
1327 | private $with_rm; | |
1328 | public function addMarketingHash() | |
1329 | { | |
1330 | $this->requireAccounts(); | |
1331 | $this->with_rm = true; | |
1332 | } | |
1333 | ||
1334 | protected function marketingJoins() | |
1335 | { | |
1336 | if ($this->with_rm) { | |
5c412626 | 1337 | return array('rm' => PlSqlJoin::left('register_marketing', '$ME.uid = $UID')); |
48885bbe FB |
1338 | } else { |
1339 | return array(); | |
1340 | } | |
1341 | } | |
a087cc8d | 1342 | } |
8363588b | 1343 | // }}} |
a7d9ab89 RB |
1344 | // {{{ class ProfileFilter |
1345 | class ProfileFilter extends UserFilter | |
1346 | { | |
434570c4 | 1347 | public function get($limit = null) |
a7d9ab89 RB |
1348 | { |
1349 | return $this->getProfiles($limit); | |
1350 | } | |
2daf7250 | 1351 | |
d5fd47bf SJ |
1352 | public function getIds($limit = null) |
1353 | { | |
1354 | return $this->getPIDs(); | |
1355 | } | |
1356 | ||
2daf7250 RB |
1357 | public function filter(array $profiles, $limit = null) |
1358 | { | |
1359 | return $this->filterProfiles($profiles, self::defaultLimit($limit)); | |
1360 | } | |
1361 | ||
1362 | public function getTotalCount() | |
1363 | { | |
1364 | return $this->getTotalProfileCount(); | |
1365 | } | |
ccc951d9 RB |
1366 | |
1367 | public function getGroups() | |
1368 | { | |
1369 | return $this->getPIDGroups(); | |
1370 | } | |
a7d9ab89 RB |
1371 | } |
1372 | // }}} | |
1373 | ||
a087cc8d FB |
1374 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
1375 | ?> |