Commit | Line | Data |
---|---|---|
a087cc8d FB |
1 | <?php |
2 | /*************************************************************************** | |
3 | * Copyright (C) 2003-2009 Polytechnique.org * | |
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 | ||
d865c296 FB |
22 | |
23 | /****************** | |
24 | * CONDITIONS | |
25 | ******************/ | |
26 | ||
8363588b | 27 | // {{{ interface UserFilterCondition |
2d83cac9 RB |
28 | /** This interface describe objects which filter users based |
29 | * on various parameters. | |
30 | * The parameters of the filter must be given to the constructor. | |
31 | * The buildCondition function is called by UserFilter when | |
32 | * actually building the query. That function must call | |
33 | * $uf->addWheteverFilter so that the UserFilter makes | |
34 | * adequate joins. It must return the 'WHERE' condition to use | |
35 | * with the filter. | |
36 | */ | |
dcc63ed5 | 37 | interface UserFilterCondition extends PlFilterCondition |
a087cc8d | 38 | { |
a087cc8d | 39 | } |
8363588b | 40 | // }}} |
a087cc8d | 41 | |
8363588b | 42 | // {{{ class UFC_Profile |
53eae167 RB |
43 | /** Filters users who have a profile |
44 | */ | |
eb1449b8 FB |
45 | class UFC_Profile implements UserFilterCondition |
46 | { | |
dcc63ed5 | 47 | public function buildCondition(PlFilter &$uf) |
eb1449b8 FB |
48 | { |
49 | return '$PID IS NOT NULL'; | |
50 | } | |
51 | } | |
8363588b | 52 | // }}} |
eb1449b8 | 53 | |
ddba9d4f RB |
54 | // {{{ class UFC_Hruid |
55 | /** Filters users based on their hruid | |
56 | * @param $val Either an hruid, or a list of those | |
57 | */ | |
58 | class UFC_Hruid implements UserFilterCondition | |
59 | { | |
60 | private $hruids; | |
61 | ||
62 | public function __construct($val) | |
63 | { | |
64 | if (!is_array($val)) { | |
65 | $val = array($val); | |
66 | } | |
67 | $this->hruids = $val; | |
68 | } | |
69 | ||
70 | public function buildCondition(PlFilter &$uf) | |
71 | { | |
72 | $ufc->requireAccounts(); | |
73 | ||
74 | return 'a.hruid IN ' . XDB::formatArray($this->hruids); | |
75 | } | |
76 | } | |
77 | // }}} | |
78 | ||
d7ddf29b RB |
79 | // {{{ class UFC_Comment |
80 | class UFC_Comment implements UserFilterCondition | |
81 | { | |
82 | private $text; | |
83 | ||
84 | public function __construct($text) | |
85 | { | |
86 | $this->text = $text; | |
87 | } | |
88 | ||
89 | public function buildCondition(PlFilter &$uf) | |
90 | { | |
91 | $uf->requireProfiles(); | |
92 | return 'p.freetext LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text); | |
93 | } | |
94 | } | |
95 | // }}} | |
96 | ||
8363588b | 97 | // {{{ class UFC_Promo |
5d2e55c7 | 98 | /** Filters users based on promotion |
53eae167 RB |
99 | * @param $comparison Comparison operator (>, =, ...) |
100 | * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation" | |
5d2e55c7 | 101 | * @param $promo Promotion on which the filter is based |
53eae167 | 102 | */ |
a087cc8d FB |
103 | class UFC_Promo implements UserFilterCondition |
104 | { | |
a087cc8d FB |
105 | |
106 | private $grade; | |
107 | private $promo; | |
108 | private $comparison; | |
109 | ||
110 | public function __construct($comparison, $grade, $promo) | |
111 | { | |
112 | $this->grade = $grade; | |
113 | $this->comparison = $comparison; | |
114 | $this->promo = $promo; | |
38c6fe96 FB |
115 | if ($this->grade != UserFilter::DISPLAY) { |
116 | UserFilter::assertGrade($this->grade); | |
117 | } | |
a087cc8d FB |
118 | } |
119 | ||
dcc63ed5 | 120 | public function buildCondition(PlFilter &$uf) |
a087cc8d | 121 | { |
38c6fe96 FB |
122 | if ($this->grade == UserFilter::DISPLAY) { |
123 | $sub = $uf->addDisplayFilter(); | |
1a23a02b | 124 | return XDB::format('pd' . $sub . '.promo ' . $this->comparison . ' {?}', $this->promo); |
38c6fe96 FB |
125 | } else { |
126 | $sub = $uf->addEducationFilter(true, $this->grade); | |
127 | $field = 'pe' . $sub . '.' . UserFilter::promoYear($this->grade); | |
128 | return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison . ' ' . XDB::format('{?}', $this->promo); | |
129 | } | |
784745ce FB |
130 | } |
131 | } | |
8363588b | 132 | // }}} |
784745ce | 133 | |
a7f8e48a | 134 | // {{{ class UFC_Formation |
d7ddf29b RB |
135 | /** Filters users by formation |
136 | * @param $val The formation to search (either ID or array of IDs) | |
137 | */ | |
a7f8e48a RB |
138 | class UFC_Formation implements UserFilterCondition |
139 | { | |
d7ddf29b | 140 | private $val; |
a7f8e48a | 141 | |
d7ddf29b | 142 | public function __construct($val) |
a7f8e48a | 143 | { |
d7ddf29b RB |
144 | if (!is_array($val)) { |
145 | $val = array($val); | |
146 | } | |
147 | $this->val = $val; | |
a7f8e48a RB |
148 | } |
149 | ||
150 | public function buildCondition(PlFilter &$uf) | |
151 | { | |
152 | $sub = $uf->addEducationFilter(); | |
d7ddf29b | 153 | return 'pe' . $sub . '.eduid IN ' . XDB::formatArray($this->val); |
a7f8e48a RB |
154 | } |
155 | } | |
156 | // }}} | |
157 | ||
d7ddf29b RB |
158 | // {{{ class UFC_Diploma |
159 | class UFC_Diploma implements UserFilterCondition | |
160 | { | |
161 | private $diploma; | |
162 | ||
163 | public function __construct($diploma) | |
164 | { | |
165 | if (! is_array($diploma)) { | |
166 | $diploma = array($diploma); | |
167 | } | |
168 | $this->diploma = $diploma; | |
169 | } | |
170 | ||
171 | public function buildCondition(PlFilter &$uf) | |
172 | { | |
173 | $sub = $uf->addEducationFilter(); | |
174 | return 'pee' . $sub . '.degreeid IN ' . XDB::formatArray($this->val); | |
175 | } | |
176 | } | |
177 | // }}} | |
178 | ||
179 | // {{{ class UFC_StudyField | |
180 | class UFC_StudyField implements UserFilterCondition | |
181 | { | |
182 | private $val; | |
183 | ||
184 | public function __construct($val) | |
185 | { | |
46e88fe3 RB |
186 | if (!is_array($val)) { |
187 | $val = array($val); | |
188 | } | |
d7ddf29b RB |
189 | $this->val = $val; |
190 | } | |
191 | ||
192 | public function buildCondition(PlFilter &$uf) | |
193 | { | |
194 | $sub = $uf->addEducationFilter(); | |
195 | return 'pee' . $sub . '.fieldid IN ' . XDB::formatArray($this->val); | |
196 | } | |
197 | } | |
198 | // }}} | |
199 | ||
8363588b | 200 | // {{{ class UFC_Name |
53eae167 | 201 | /** Filters users based on name |
5d2e55c7 | 202 | * @param $type Type of name field on which filtering is done (firstname, lastname...) |
53eae167 | 203 | * @param $text Text on which to filter |
5d2e55c7 | 204 | * @param $mode Flag indicating search type (prefix, suffix, with particule...) |
53eae167 | 205 | */ |
784745ce FB |
206 | class UFC_Name implements UserFilterCondition |
207 | { | |
208 | const PREFIX = 1; | |
209 | const SUFFIX = 2; | |
210 | const PARTICLE = 7; | |
211 | const VARIANTS = 8; | |
212 | const CONTAINS = 3; | |
213 | ||
214 | private $type; | |
215 | private $text; | |
216 | private $mode; | |
217 | ||
218 | public function __construct($type, $text, $mode) | |
219 | { | |
220 | $this->type = $type; | |
221 | $this->text = $text; | |
222 | $this->mode = $mode; | |
223 | } | |
224 | ||
225 | private function buildNameQuery($type, $variant, $where, UserFilter &$uf) | |
226 | { | |
227 | $sub = $uf->addNameFilter($type, $variant); | |
228 | return str_replace('$ME', 'pn' . $sub, $where); | |
229 | } | |
230 | ||
dcc63ed5 | 231 | public function buildCondition(PlFilter &$uf) |
784745ce FB |
232 | { |
233 | $left = '$ME.name'; | |
234 | $op = ' LIKE '; | |
235 | if (($this->mode & self::PARTICLE) == self::PARTICLE) { | |
236 | $left = 'CONCAT($ME.particle, \' \', $ME.name)'; | |
237 | } | |
238 | if (($this->mode & self::CONTAINS) == 0) { | |
239 | $right = XDB::format('{?}', $this->text); | |
240 | $op = ' = '; | |
241 | } else if (($this->mode & self::CONTAINS) == self::PREFIX) { | |
242 | $right = XDB::format('CONCAT({?}, \'%\')', $this->text); | |
243 | } else if (($this->mode & self::CONTAINS) == self::SUFFIX) { | |
244 | $right = XDB::format('CONCAT(\'%\', {?})', $this->text); | |
245 | } else { | |
246 | $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text); | |
247 | } | |
248 | $cond = $left . $op . $right; | |
249 | $conds = array($this->buildNameQuery($this->type, null, $cond, $uf)); | |
d865c296 | 250 | if (($this->mode & self::VARIANTS) != 0 && isset(UserFilter::$name_variants[$this->type])) { |
784745ce FB |
251 | foreach (UserFilter::$name_variants[$this->type] as $var) { |
252 | $conds[] = $this->buildNameQuery($this->type, $var, $cond, $uf); | |
253 | } | |
254 | } | |
255 | return implode(' OR ', $conds); | |
a087cc8d FB |
256 | } |
257 | } | |
8363588b | 258 | // }}} |
a087cc8d | 259 | |
40585144 RB |
260 | // {{{ class UFC_NameTokens |
261 | /** Selects users based on tokens in their name (for quicksearch) | |
262 | * @param $tokens An array of tokens to search | |
263 | * @param $flags Flags the tokens must have (e.g 'public' for public search) | |
264 | * @param $soundex (bool) Whether those tokens are fulltext or soundex | |
265 | */ | |
266 | class UFC_NameTokens implements UserFilterCondition | |
267 | { | |
268 | /* Flags */ | |
269 | const FLAG_PUBLIC = 'public'; | |
270 | ||
271 | private $tokens; | |
272 | private $flags; | |
273 | private $soundex; | |
79a0b464 | 274 | private $exact; |
40585144 | 275 | |
79a0b464 | 276 | public function __construct($tokens, $flags = array(), $soundex = false, $exact = false) |
40585144 RB |
277 | { |
278 | $this->tokens = $tokens; | |
279 | if (is_array($flags)) { | |
280 | $this->flags = $flags; | |
281 | } else { | |
282 | $this->flags = array($flags); | |
283 | } | |
284 | $this->soundex = $soundex; | |
79a0b464 | 285 | $this->exact = $exact; |
40585144 RB |
286 | } |
287 | ||
dcc63ed5 | 288 | public function buildCondition(PlFilter &$uf) |
40585144 | 289 | { |
79a0b464 | 290 | $sub = $uf->addNameTokensFilter(!($this->exact || $this->soundex)); |
40585144 RB |
291 | $conds = array(); |
292 | if ($this->soundex) { | |
293 | $conds[] = $sub . '.soundex IN ' . XDB::formatArray($this->tokens); | |
79a0b464 RB |
294 | } else if ($this->exact) { |
295 | $conds[] = $sub . '.token IN ' . XDB::formatArray($this->tokens); | |
40585144 RB |
296 | } else { |
297 | $tokconds = array(); | |
298 | foreach ($this->tokens as $token) { | |
299 | $tokconds[] = $sub . '.token LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $token); | |
300 | } | |
301 | $conds[] = implode(' OR ', $tokconds); | |
302 | } | |
303 | ||
304 | if ($this->flags != null) { | |
305 | $conds[] = $sub . '.flags IN ' . XDB::formatArray($this->flags); | |
306 | } | |
307 | ||
308 | return implode(' AND ', $conds); | |
309 | } | |
310 | } | |
311 | // }}} | |
312 | ||
0fb3713c RB |
313 | // {{{ class UFC_Nationality |
314 | class UFC_Nationality implements UserFilterCondition | |
315 | { | |
d7ddf29b | 316 | private $val; |
0fb3713c | 317 | |
d7ddf29b | 318 | public function __construct($val) |
0fb3713c | 319 | { |
d7ddf29b RB |
320 | if (!is_array($val)) { |
321 | $val = array($val); | |
322 | } | |
323 | $this->val = $val; | |
0fb3713c RB |
324 | } |
325 | ||
326 | public function buildCondition(PlFilter &$uf) | |
327 | { | |
d7ddf29b RB |
328 | $uf->requireProfiles(); |
329 | $nat = XDB::formatArray($this->val); | |
330 | $conds = array( | |
331 | 'p.nationality1 IN ' . $nat, | |
332 | 'p.nationality2 IN ' . $nat, | |
333 | 'p.nationality3 IN ' . $nat, | |
334 | ); | |
335 | return implode(' OR ', $conds); | |
0fb3713c RB |
336 | } |
337 | } | |
338 | // }}} | |
339 | ||
8363588b | 340 | // {{{ class UFC_Dead |
53eae167 RB |
341 | /** Filters users based on death date |
342 | * @param $comparison Comparison operator | |
343 | * @param $date Date to which death date should be compared | |
344 | */ | |
4927ee54 FB |
345 | class UFC_Dead implements UserFilterCondition |
346 | { | |
38c6fe96 FB |
347 | private $comparison; |
348 | private $date; | |
349 | ||
350 | public function __construct($comparison = null, $date = null) | |
4927ee54 | 351 | { |
38c6fe96 FB |
352 | $this->comparison = $comparison; |
353 | $this->date = $date; | |
4927ee54 FB |
354 | } |
355 | ||
dcc63ed5 | 356 | public function buildCondition(PlFilter &$uf) |
4927ee54 | 357 | { |
d7ddf29b | 358 | $uf->requireProfiles(); |
38c6fe96 FB |
359 | $str = 'p.deathdate IS NOT NULL'; |
360 | if (!is_null($this->comparison)) { | |
361 | $str .= ' AND p.deathdate ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date)); | |
4927ee54 | 362 | } |
38c6fe96 | 363 | return $str; |
4927ee54 FB |
364 | } |
365 | } | |
8363588b | 366 | // }}} |
4927ee54 | 367 | |
8363588b | 368 | // {{{ class UFC_Registered |
53eae167 RB |
369 | /** Filters users based on registration state |
370 | * @param $active Whether we want to use only "active" users (i.e with a valid redirection) | |
371 | * @param $comparison Comparison operator | |
372 | * @param $date Date to which users registration date should be compared | |
373 | */ | |
4927ee54 FB |
374 | class UFC_Registered implements UserFilterCondition |
375 | { | |
376 | private $active; | |
38c6fe96 FB |
377 | private $comparison; |
378 | private $date; | |
379 | ||
380 | public function __construct($active = false, $comparison = null, $date = null) | |
4927ee54 | 381 | { |
b2e8fc54 | 382 | $this->active = $active; |
38c6fe96 FB |
383 | $this->comparison = $comparison; |
384 | $this->date = $date; | |
4927ee54 FB |
385 | } |
386 | ||
dcc63ed5 | 387 | public function buildCondition(PlFilter &$uf) |
4927ee54 | 388 | { |
d7ddf29b | 389 | $uf->requireAccounts(); |
4927ee54 | 390 | if ($this->active) { |
38c6fe96 | 391 | $date = 'a.uid IS NOT NULL AND a.state = \'active\''; |
4927ee54 | 392 | } else { |
38c6fe96 FB |
393 | $date = 'a.uid IS NOT NULL AND a.state != \'pending\''; |
394 | } | |
395 | if (!is_null($this->comparison)) { | |
396 | $date .= ' AND a.registration_date ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date)); | |
4927ee54 | 397 | } |
38c6fe96 | 398 | return $date; |
4927ee54 FB |
399 | } |
400 | } | |
8363588b | 401 | // }}} |
4927ee54 | 402 | |
8363588b | 403 | // {{{ class UFC_ProfileUpdated |
53eae167 RB |
404 | /** Filters users based on profile update date |
405 | * @param $comparison Comparison operator | |
406 | * @param $date Date to which profile update date must be compared | |
407 | */ | |
7e735012 FB |
408 | class UFC_ProfileUpdated implements UserFilterCondition |
409 | { | |
410 | private $comparison; | |
411 | private $date; | |
412 | ||
413 | public function __construct($comparison = null, $date = null) | |
414 | { | |
415 | $this->comparison = $comparison; | |
416 | $this->date = $date; | |
417 | } | |
418 | ||
dcc63ed5 | 419 | public function buildCondition(PlFilter &$uf) |
7e735012 | 420 | { |
d7ddf29b | 421 | $uf->requireProfiles(); |
7e735012 FB |
422 | return 'p.last_change ' . $this->comparison . XDB::format(' {?}', date('Y-m-d H:i:s', $this->date)); |
423 | } | |
424 | } | |
8363588b | 425 | // }}} |
7e735012 | 426 | |
8363588b | 427 | // {{{ class UFC_Birthday |
53eae167 RB |
428 | /** Filters users based on next birthday date |
429 | * @param $comparison Comparison operator | |
430 | * @param $date Date to which users next birthday date should be compared | |
431 | */ | |
7e735012 FB |
432 | class UFC_Birthday implements UserFilterCondition |
433 | { | |
434 | private $comparison; | |
435 | private $date; | |
436 | ||
437 | public function __construct($comparison = null, $date = null) | |
438 | { | |
439 | $this->comparison = $comparison; | |
440 | $this->date = $date; | |
441 | } | |
442 | ||
dcc63ed5 | 443 | public function buildCondition(PlFilter &$uf) |
7e735012 | 444 | { |
d7ddf29b | 445 | $uf->requireProfiles(); |
7e735012 FB |
446 | return 'p.next_birthday ' . $this->comparison . XDB::format(' {?}', date('Y-m-d', $this->date)); |
447 | } | |
448 | } | |
8363588b | 449 | // }}} |
7e735012 | 450 | |
8363588b | 451 | // {{{ class UFC_Sex |
53eae167 RB |
452 | /** Filters users based on sex |
453 | * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users | |
454 | */ | |
4927ee54 FB |
455 | class UFC_Sex implements UserFilterCondition |
456 | { | |
457 | private $sex; | |
458 | public function __construct($sex) | |
459 | { | |
460 | $this->sex = $sex; | |
461 | } | |
462 | ||
dcc63ed5 | 463 | public function buildCondition(PlFilter &$uf) |
4927ee54 FB |
464 | { |
465 | if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) { | |
466 | return self::COND_FALSE; | |
467 | } else { | |
d7ddf29b | 468 | $uf->requireProfiles(); |
24e08e33 | 469 | return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male'); |
4927ee54 FB |
470 | } |
471 | } | |
472 | } | |
8363588b | 473 | // }}} |
4927ee54 | 474 | |
8363588b | 475 | // {{{ class UFC_Group |
53eae167 | 476 | /** Filters users based on group membership |
5d2e55c7 RB |
477 | * @param $group Group whose members we are selecting |
478 | * @param $anim Whether to restrict selection to animators of that group | |
53eae167 | 479 | */ |
4927ee54 FB |
480 | class UFC_Group implements UserFilterCondition |
481 | { | |
482 | private $group; | |
5d2e55c7 RB |
483 | private $anim; |
484 | public function __construct($group, $anim = false) | |
4927ee54 FB |
485 | { |
486 | $this->group = $group; | |
5d2e55c7 | 487 | $this->anim = $anim; |
4927ee54 FB |
488 | } |
489 | ||
dcc63ed5 | 490 | public function buildCondition(PlFilter &$uf) |
4927ee54 FB |
491 | { |
492 | $sub = $uf->addGroupFilter($this->group); | |
493 | $where = 'gpm' . $sub . '.perms IS NOT NULL'; | |
5d2e55c7 | 494 | if ($this->anim) { |
4927ee54 FB |
495 | $where .= ' AND gpm' . $sub . '.perms = \'admin\''; |
496 | } | |
497 | return $where; | |
498 | } | |
499 | } | |
8363588b | 500 | // }}} |
4927ee54 | 501 | |
0fb3713c | 502 | // {{{ class UFC_Binet |
d7ddf29b RB |
503 | /** Selects users based on their belonging to a given (list of) binet |
504 | * @param $binet either a binet_id or an array of binet_ids | |
505 | */ | |
0fb3713c RB |
506 | class UFC_Binet implements UserFilterCondition |
507 | { | |
d7ddf29b | 508 | private $val; |
0fb3713c | 509 | |
d7ddf29b | 510 | public function __construct($val) |
0fb3713c | 511 | { |
d7ddf29b RB |
512 | if (!is_array($val)) { |
513 | $val = array($val); | |
514 | } | |
515 | $this->val = $val; | |
0fb3713c RB |
516 | } |
517 | ||
518 | public function buildCondition(PlFilter &$uf) | |
519 | { | |
d7ddf29b RB |
520 | $sub = $uf->addBinetsFilter(); |
521 | return $sub . '.binet_id IN ' . XDB::formatArray($this->val); | |
a7f8e48a RB |
522 | } |
523 | } | |
524 | // }}} | |
525 | ||
526 | // {{{ class UFC_Section | |
46e88fe3 RB |
527 | /** Selects users based on section |
528 | * @param $section ID of the section | |
529 | */ | |
a7f8e48a RB |
530 | class UFC_Section implements UserFilterCondition |
531 | { | |
532 | private $section; | |
533 | ||
534 | public function __construct($section) | |
535 | { | |
536 | $this->section = $section; | |
537 | } | |
538 | ||
539 | public function buildCondition(PlFilter &$uf) | |
540 | { | |
541 | $uf->requireProfiles(); | |
542 | return 'p.section = ' . XDB::format('{?}', $this->section); | |
0fb3713c RB |
543 | } |
544 | } | |
545 | // }}} | |
546 | ||
8363588b | 547 | // {{{ class UFC_Email |
53eae167 RB |
548 | /** Filters users based on email address |
549 | * @param $email Email whose owner we are looking for | |
550 | */ | |
aa21c568 FB |
551 | class UFC_Email implements UserFilterCondition |
552 | { | |
553 | private $email; | |
554 | public function __construct($email) | |
555 | { | |
556 | $this->email = $email; | |
557 | } | |
558 | ||
dcc63ed5 | 559 | public function buildCondition(PlFilter &$uf) |
aa21c568 FB |
560 | { |
561 | if (User::isForeignEmailAddress($this->email)) { | |
562 | $sub = $uf->addEmailRedirectFilter($this->email); | |
563 | return XDB::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email); | |
21401768 FB |
564 | } else if (User::isVirtualEmailAddress($this->email)) { |
565 | $sub = $uf->addVirtualEmailFilter($this->email); | |
566 | return 'vr' . $sub . '.redirect IS NOT NULL'; | |
aa21c568 | 567 | } else { |
21401768 FB |
568 | @list($user, $domain) = explode('@', $this->email); |
569 | $sub = $uf->addAliasFilter($user); | |
aa21c568 FB |
570 | return 'al' . $sub . '.alias IS NOT NULL'; |
571 | } | |
572 | } | |
573 | } | |
8363588b | 574 | // }}} |
d865c296 | 575 | |
8363588b | 576 | // {{{ class UFC_EmailList |
5d2e55c7 | 577 | /** Filters users based on an email list |
53eae167 RB |
578 | * @param $emails List of emails whose owner must be selected |
579 | */ | |
21401768 FB |
580 | class UFC_EmailList implements UserFilterCondition |
581 | { | |
582 | private $emails; | |
583 | public function __construct($emails) | |
584 | { | |
585 | $this->emails = $emails; | |
586 | } | |
587 | ||
dcc63ed5 | 588 | public function buildCondition(PlFilter &$uf) |
21401768 FB |
589 | { |
590 | $email = null; | |
591 | $virtual = null; | |
592 | $alias = null; | |
593 | $cond = array(); | |
594 | ||
595 | if (count($this->emails) == 0) { | |
dcc63ed5 | 596 | return PlFilterCondition::COND_TRUE; |
21401768 FB |
597 | } |
598 | ||
599 | foreach ($this->emails as $entry) { | |
600 | if (User::isForeignEmailAddress($entry)) { | |
601 | if (is_null($email)) { | |
602 | $email = $uf->addEmailRedirectFilter(); | |
603 | } | |
604 | $cond[] = XDB::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry); | |
605 | } else if (User::isVirtualEmailAddress($entry)) { | |
606 | if (is_null($virtual)) { | |
607 | $virtual = $uf->addVirtualEmailFilter(); | |
608 | } | |
609 | $cond[] = XDB::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry); | |
610 | } else { | |
611 | if (is_null($alias)) { | |
612 | $alias = $uf->addAliasFilter(); | |
613 | } | |
614 | @list($user, $domain) = explode('@', $entry); | |
615 | $cond[] = XDB::format('al' . $alias . '.alias = {?}', $user); | |
616 | } | |
617 | } | |
618 | return '(' . implode(') OR (', $cond) . ')'; | |
619 | } | |
620 | } | |
8363588b | 621 | // }}} |
d865c296 | 622 | |
8363588b | 623 | // {{{ class UFC_Address |
2b9ca54d | 624 | abstract class UFC_Address implements UserFilterCondition |
c4b24511 | 625 | { |
2b9ca54d | 626 | /** Valid address type ('hq' is reserved for company addresses) |
036d1637 | 627 | */ |
2b9ca54d RB |
628 | const TYPE_HOME = 1; |
629 | const TYPE_PRO = 2; | |
630 | const TYPE_ANY = 3; | |
c4b24511 | 631 | |
2b9ca54d | 632 | /** Text for these types |
036d1637 | 633 | */ |
2b9ca54d RB |
634 | protected static $typetexts = array( |
635 | self::TYPE_HOME => 'home', | |
636 | self::TYPE_PRO => 'pro', | |
637 | ); | |
638 | ||
639 | protected $type; | |
c4b24511 | 640 | |
036d1637 RB |
641 | /** Flags for addresses |
642 | */ | |
643 | const FLAG_CURRENT = 0x0001; | |
644 | const FLAG_TEMP = 0x0002; | |
645 | const FLAG_SECOND = 0x0004; | |
646 | const FLAG_MAIL = 0x0008; | |
647 | const FLAG_CEDEX = 0x0010; | |
648 | ||
649 | // Binary OR of those flags | |
650 | const FLAG_ANY = 0x001F; | |
651 | ||
652 | /** Text of these flags | |
653 | */ | |
2b9ca54d | 654 | protected static $flagtexts = array( |
036d1637 RB |
655 | self::FLAG_CURRENT => 'current', |
656 | self::FLAG_TEMP => 'temporary', | |
657 | self::FLAG_SECOND => 'secondary', | |
658 | self::FLAG_MAIL => 'mail', | |
659 | self::FLAG_CEDEX => 'cedex', | |
660 | ); | |
661 | ||
2b9ca54d RB |
662 | protected $flags; |
663 | ||
664 | public function __construct($type = null, $flags = null) | |
665 | { | |
666 | $this->type = $type; | |
667 | $this->flags = $flags; | |
668 | } | |
669 | ||
670 | protected function initConds($sub) | |
671 | { | |
672 | $conds = array(); | |
673 | $types = array(); | |
674 | foreach (self::$typetexts as $flag => $type) { | |
675 | if ($flag & $this->type) { | |
676 | $types[] = $type; | |
677 | } | |
678 | } | |
679 | if (count($types)) { | |
680 | $conds[] = $sub . '.type IN ' . XDB::formatArray($types); | |
681 | } | |
682 | ||
683 | if ($this->flags != self::FLAG_ANY) { | |
684 | foreach(self::$flagtexts as $flag => $text) { | |
685 | if ($flag & $this->flags) { | |
686 | $conds[] = 'FIND_IN_SET(' . XDB::format('{?}', $text) . ', ' . $sub . '.flags)'; | |
687 | } | |
688 | } | |
689 | } | |
690 | return $conds; | |
691 | } | |
692 | ||
693 | } | |
694 | // }}} | |
695 | ||
696 | // {{{ class UFC_AddressText | |
697 | /** Select users based on their address, using full text search | |
698 | * @param $text Text for filter in fulltext search | |
699 | * @param $textSearchMode Mode for search (PREFIX, SUFFIX, ...) | |
700 | * @param $type Filter on address type | |
701 | * @param $flags Filter on address flags | |
702 | * @param $country Filter on address country | |
703 | * @param $locality Filter on address locality | |
704 | */ | |
705 | class UFC_AddressText extends UFC_Address | |
706 | { | |
707 | /** Flags for text search | |
036d1637 | 708 | */ |
2b9ca54d RB |
709 | const PREFIX = 0x0001; |
710 | const SUFFIX = 0x0002; | |
711 | const CONTAINS = 0x0003; | |
712 | ||
036d1637 | 713 | private $text; |
2b9ca54d RB |
714 | private $textSearchMode; |
715 | ||
716 | public function __construct($text = null, $textSearchMode = self::CONTAINS, | |
717 | $type = null, $flags = self::FLAG_ANY, $country = null, $locality = null) | |
718 | { | |
719 | parent::__construct($type, $flags); | |
720 | $this->text = $text; | |
721 | $this->textSearchMode = $textSearchMode; | |
722 | $this->country = $country; | |
723 | $this->locality = $locality; | |
724 | } | |
725 | ||
726 | private function mkMatch($txt) | |
727 | { | |
728 | $op = ' LIKE '; | |
729 | if (($this->textSearchMode & self::CONTAINS) == 0) { | |
730 | $right = XDB::format('{?}', $this->text); | |
731 | $op = ' = '; | |
732 | } else if (($this->mode & self::CONTAINS) == self::PREFIX) { | |
733 | $right = XDB::format('CONCAT({?}, \'%\')', $this->text); | |
734 | } else if (($this->mode & self::CONTAINS) == self::SUFFIX) { | |
735 | $right = XDB::format('CONCAT(\'%\', {?})', $this->text); | |
736 | } else { | |
737 | $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text); | |
738 | } | |
739 | return $op . $right; | |
740 | } | |
741 | ||
742 | public function buildCondition(PlFilter &$uf) | |
743 | { | |
744 | $sub = $uf->addAddressFilter(); | |
745 | $conds = $this->initConds($sub); | |
746 | if ($this->text != null) { | |
747 | $conds[] = $sub . '.text' . $this->mkMatch($this->text); | |
748 | } | |
749 | ||
750 | if ($this->country != null) { | |
751 | $subc = $uf->addAddressCountryFilter(); | |
752 | $subconds = array(); | |
753 | $subconds[] = $subc . '.country' . $this->mkMatch($this->country); | |
754 | $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country); | |
755 | $conds[] = implode(' OR ', $subconds); | |
756 | } | |
757 | ||
758 | if ($this->locality != null) { | |
759 | $subl = $uf->addAddressLocalityFilter(); | |
760 | $conds[] = $subl . '.name' . $this->mkMatch($this->locality); | |
761 | } | |
762 | ||
763 | return implode(' AND ', $conds); | |
764 | } | |
765 | } | |
766 | // }}} | |
767 | ||
46e88fe3 | 768 | // {{{ class UFC_AddressField |
2b9ca54d | 769 | /** Filters users based on their address, |
46e88fe3 RB |
770 | * @param $val Either a code for one of the fields, or an array of such codes |
771 | * @param $fieldtype The type of field to look for | |
2b9ca54d RB |
772 | * @param $type Filter on address type |
773 | * @param $flags Filter on address flags | |
2b9ca54d | 774 | */ |
46e88fe3 | 775 | class UFC_AddressField extends UFC_Address |
2b9ca54d | 776 | { |
46e88fe3 RB |
777 | const FIELD_COUNTRY = 1; |
778 | const FIELD_ADMAREA = 2; | |
779 | const FIELD_SUBADMAREA = 3; | |
780 | const FIELD_LOCALITY = 4; | |
781 | const FIELD_ZIPCODE = 5; | |
782 | ||
2b9ca54d RB |
783 | /** Data of the filter |
784 | */ | |
46e88fe3 RB |
785 | private $val; |
786 | private $fieldtype; | |
036d1637 | 787 | |
46e88fe3 | 788 | public function __construct($val, $fieldtype, $type = null, $flags = self::FLAG_ANY) |
036d1637 | 789 | { |
2b9ca54d | 790 | parent::__construct($type, $flags); |
46e88fe3 RB |
791 | |
792 | if (!is_array($val)) { | |
793 | $val = array($val); | |
794 | } | |
795 | $this->val = $val; | |
796 | $this->fieldtype = $fieldtype; | |
c4b24511 RB |
797 | } |
798 | ||
dcc63ed5 | 799 | public function buildCondition(PlFilter &$uf) |
c4b24511 | 800 | { |
036d1637 | 801 | $sub = $uf->addAddressFilter(); |
d7ddf29b | 802 | $conds = $this->initConds($sub); |
036d1637 | 803 | |
46e88fe3 RB |
804 | switch ($this->fieldtype) { |
805 | case self::FIELD_COUNTRY: | |
806 | $field = 'countryId'; | |
807 | break; | |
808 | case self::FIELD_ADMAREA: | |
809 | $field = 'administrativeAreaId'; | |
810 | break; | |
811 | case self::FIELD_SUBADMAREA: | |
812 | $field = 'subAdministrativeAreaId'; | |
813 | break; | |
814 | case self::FIELD_LOCALITY: | |
815 | $field = 'localityId'; | |
816 | break; | |
817 | case self::FIELD_ZIPCODE: | |
818 | $field = 'postalCode'; | |
819 | break; | |
820 | default: | |
821 | Platal::page()->killError('Invalid address field type : ' . $this->fieldtype); | |
822 | } | |
823 | $conds[] = $sub . '.' . $field . ' IN ' . XDB::formatArray($this->val); | |
036d1637 RB |
824 | |
825 | return implode(' AND ', $conds); | |
c4b24511 RB |
826 | } |
827 | } | |
8363588b | 828 | // }}} |
c4b24511 | 829 | |
8363588b | 830 | // {{{ class UFC_Corps |
4083b126 RB |
831 | /** Filters users based on the corps they belong to |
832 | * @param $corps Corps we are looking for (abbreviation) | |
833 | * @param $type Whether we search for original or current corps | |
834 | */ | |
835 | class UFC_Corps implements UserFilterCondition | |
836 | { | |
5d2e55c7 RB |
837 | const CURRENT = 1; |
838 | const ORIGIN = 2; | |
4083b126 RB |
839 | |
840 | private $corps; | |
841 | private $type; | |
842 | ||
843 | public function __construct($corps, $type = self::CURRENT) | |
844 | { | |
845 | $this->corps = $corps; | |
846 | $this->type = $type; | |
847 | } | |
848 | ||
dcc63ed5 | 849 | public function buildCondition(PlFilter &$uf) |
4083b126 | 850 | { |
5d2e55c7 RB |
851 | /** Tables shortcuts: |
852 | * pc for profile_corps, | |
4083b126 RB |
853 | * pceo for profile_corps_enum - orginal |
854 | * pcec for profile_corps_enum - current | |
855 | */ | |
856 | $sub = $uf->addCorpsFilter($this->type); | |
857 | $cond = $sub . '.abbreviation = ' . $corps; | |
858 | return $cond; | |
859 | } | |
860 | } | |
8363588b | 861 | // }}} |
4083b126 | 862 | |
8363588b | 863 | // {{{ class UFC_Corps_Rank |
4083b126 RB |
864 | /** Filters users based on their rank in the corps |
865 | * @param $rank Rank we are looking for (abbreviation) | |
866 | */ | |
867 | class UFC_Corps_Rank implements UserFilterCondition | |
868 | { | |
869 | private $rank; | |
870 | public function __construct($rank) | |
871 | { | |
872 | $this->rank = $rank; | |
873 | } | |
874 | ||
dcc63ed5 | 875 | public function buildCondition(PlFilter &$uf) |
4083b126 | 876 | { |
5d2e55c7 | 877 | /** Tables shortcuts: |
4083b126 RB |
878 | * pcr for profile_corps_rank |
879 | */ | |
880 | $sub = $uf->addCorpsRankFilter(); | |
881 | $cond = $sub . '.abbreviation = ' . $rank; | |
882 | return $cond; | |
883 | } | |
884 | } | |
8363588b | 885 | // }}} |
4083b126 | 886 | |
6a99c3ac RB |
887 | // {{{ class UFC_Job_Company |
888 | /** Filters users based on the company they belong to | |
889 | * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM) | |
890 | * @param $value The searched value | |
891 | */ | |
4e2f2ad2 | 892 | class UFC_Job_Company implements UserFilterCondition |
6a99c3ac RB |
893 | { |
894 | const JOBID = 'id'; | |
895 | const JOBNAME = 'name'; | |
896 | const JOBACRONYM = 'acronym'; | |
897 | ||
898 | private $type; | |
899 | private $value; | |
900 | ||
901 | public function __construct($type, $value) | |
902 | { | |
903 | $this->assertType($type); | |
904 | $this->type = $type; | |
905 | $this->value = $value; | |
906 | } | |
907 | ||
908 | private function assertType($type) | |
909 | { | |
910 | if ($type != self::JOBID && $type != self::JOBNAME && $type != self::JOBACRONYM) { | |
5d2e55c7 | 911 | Platal::page()->killError("Type de recherche non valide."); |
6a99c3ac RB |
912 | } |
913 | } | |
914 | ||
dcc63ed5 | 915 | public function buildCondition(PlFilter &$uf) |
6a99c3ac RB |
916 | { |
917 | $sub = $uf->addJobCompanyFilter(); | |
918 | $cond = $sub . '.' . $this->type . ' = ' . XDB::format('{?}', $this->value); | |
919 | return $cond; | |
920 | } | |
921 | } | |
922 | // }}} | |
923 | ||
924 | // {{{ class UFC_Job_Sectorization | |
925 | /** Filters users based on the ((sub)sub)sector they work in | |
46e88fe3 | 926 | * @param $val The ID of the sector, or an array of such IDs |
d7ddf29b | 927 | * @param $type The kind of search (subsubsector/subsector/sector) |
6a99c3ac | 928 | */ |
4e2f2ad2 | 929 | class UFC_Job_Sectorization implements UserFilterCondition |
6a99c3ac | 930 | { |
d7ddf29b RB |
931 | private $val; |
932 | private $type; | |
6a99c3ac | 933 | |
d7ddf29b RB |
934 | public function __construct($val, $type = UserFilter::JOB_SECTOR) |
935 | { | |
936 | self::assertType($type); | |
46e88fe3 RB |
937 | if (!is_array($val)) { |
938 | $val = array($val); | |
939 | } | |
d7ddf29b RB |
940 | $this->val = $val; |
941 | $this->type = $type; | |
942 | } | |
6a99c3ac | 943 | |
d7ddf29b | 944 | private static function assertType($type) |
6a99c3ac | 945 | { |
d7ddf29b RB |
946 | if ($type != UserFilter::JOB_SECTOR && $type != UserFilter::JOB_SUBSECTOR && $type != UserFilter::JOB_SUBSUBSECTOR) { |
947 | Platal::page()->killError("Type de secteur non valide."); | |
948 | } | |
6a99c3ac RB |
949 | } |
950 | ||
dcc63ed5 | 951 | public function buildCondition(PlFilter &$uf) |
6a99c3ac | 952 | { |
d7ddf29b RB |
953 | $sub = $uf->addJobSectorizationFilter($this->type); |
954 | return $sub . '.id = ' . XDB::format('{?}', $this->val); | |
6a99c3ac RB |
955 | } |
956 | } | |
957 | // }}} | |
958 | ||
959 | // {{{ class UFC_Job_Description | |
960 | /** Filters users based on their job description | |
961 | * @param $description The text being searched for | |
962 | * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector) | |
963 | */ | |
4e2f2ad2 | 964 | class UFC_Job_Description implements UserFilterCondition |
6a99c3ac RB |
965 | { |
966 | ||
967 | /** Meta-filters | |
968 | * Built with binary OR on UserFilter::JOB_* | |
969 | */ | |
01cc5f9e | 970 | const ANY = 63; |
6a99c3ac RB |
971 | const SECTORIZATION = 15; |
972 | ||
973 | private $description; | |
974 | private $fields; | |
975 | ||
01cc5f9e | 976 | public function __construct($description, $fields) |
6a99c3ac RB |
977 | { |
978 | $this->fields = $fields; | |
979 | $this->description = $description; | |
980 | } | |
981 | ||
dcc63ed5 | 982 | public function buildCondition(PlFilter &$uf) |
6a99c3ac RB |
983 | { |
984 | $conds = array(); | |
985 | if ($this->fields & UserFilter::JOB_USERDEFINED) { | |
986 | $sub = $uf->addJobFilter(); | |
987 | $conds[] = $sub . '.description LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description); | |
988 | } | |
01cc5f9e RB |
989 | if ($this->fields & UserFilter::JOB_CV) { |
990 | $uf->requireProfiles(); | |
991 | $conds[] = 'p.cv LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description); | |
992 | } | |
6a99c3ac RB |
993 | if ($this->fields & UserFilter::JOB_SECTOR) { |
994 | $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SECTOR); | |
995 | $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description); | |
996 | } | |
997 | if ($this->fields & UserFilter::JOB_SUBSECTOR) { | |
998 | $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSECTOR); | |
999 | $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description); | |
1000 | } | |
1001 | if ($this->fields & UserFilter::JOB_SUBSUBSECTOR) { | |
1002 | $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSUBSECTOR); | |
1003 | $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description); | |
1004 | $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_ALTERNATES); | |
1005 | $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description); | |
1006 | } | |
1007 | return implode(' OR ', $conds); | |
1008 | } | |
1009 | } | |
1010 | // }}} | |
1011 | ||
0a2e9c74 RB |
1012 | // {{{ class UFC_Networking |
1013 | /** Filters users based on network identity (IRC, ...) | |
1014 | * @param $type Type of network (-1 for any) | |
1015 | * @param $value Value to search | |
1016 | */ | |
4e2f2ad2 | 1017 | class UFC_Networking implements UserFilterCondition |
0a2e9c74 RB |
1018 | { |
1019 | private $type; | |
1020 | private $value; | |
1021 | ||
1022 | public function __construct($type, $value) | |
1023 | { | |
1024 | $this->type = $type; | |
1025 | $this->value = $value; | |
1026 | } | |
1027 | ||
dcc63ed5 | 1028 | public function buildCondition(PlFilter &$uf) |
0a2e9c74 RB |
1029 | { |
1030 | $sub = $uf->addNetworkingFilter(); | |
1031 | $conds = array(); | |
1032 | $conds[] = $sub . '.address = ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->value); | |
1033 | if ($this->type != -1) { | |
1034 | $conds[] = $sub . '.network_type = ' . XDB::format('{?}', $this->type); | |
1035 | } | |
1036 | return implode(' AND ', $conds); | |
1037 | } | |
1038 | } | |
1039 | // }}} | |
1040 | ||
6d62969e RB |
1041 | // {{{ class UFC_Phone |
1042 | /** Filters users based on their phone number | |
1043 | * @param $num_type Type of number (pro/user/home) | |
1044 | * @param $phone_type Type of phone (fixed/mobile/fax) | |
1045 | * @param $number Phone number | |
1046 | */ | |
4e2f2ad2 | 1047 | class UFC_Phone implements UserFilterCondition |
6d62969e RB |
1048 | { |
1049 | const NUM_PRO = 'pro'; | |
1050 | const NUM_USER = 'user'; | |
1051 | const NUM_HOME = 'address'; | |
1052 | const NUM_ANY = 'any'; | |
1053 | ||
1054 | const PHONE_FIXED = 'fixed'; | |
1055 | const PHONE_MOBILE = 'mobile'; | |
1056 | const PHONE_FAX = 'fax'; | |
1057 | const PHONE_ANY = 'any'; | |
1058 | ||
1059 | private $num_type; | |
1060 | private $phone_type; | |
1061 | private $number; | |
1062 | ||
1063 | public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY) | |
1064 | { | |
9b8e5fb4 | 1065 | require_once('profil.func.inc.php'); |
6d62969e RB |
1066 | $this->number = $number; |
1067 | $this->num_type = $num_type; | |
1068 | $this->phone_type = format_phone_number($phone_type); | |
1069 | } | |
1070 | ||
dcc63ed5 | 1071 | public function buildCondition(PlFilter &$uf) |
6d62969e RB |
1072 | { |
1073 | $sub = $uf->addPhoneFilter(); | |
1074 | $conds = array(); | |
1075 | $conds[] = $sub . '.search_tel = ' . XDB::format('{?}', $this->number); | |
1076 | if ($this->num_type != self::NUM_ANY) { | |
1077 | $conds[] = $sub . '.link_type = ' . XDB::format('{?}', $this->num_type); | |
1078 | } | |
1079 | if ($this->phone_type != self::PHONE_ANY) { | |
1080 | $conds[] = $sub . '.tel_type = ' . XDB::format('{?}', $this->phone_type); | |
1081 | } | |
1082 | return implode(' AND ', $conds); | |
1083 | } | |
1084 | } | |
1085 | // }}} | |
1086 | ||
ceb512d2 RB |
1087 | // {{{ class UFC_Medal |
1088 | /** Filters users based on their medals | |
1089 | * @param $medal ID of the medal | |
1090 | * @param $grade Grade of the medal (null for 'any') | |
1091 | */ | |
4e2f2ad2 | 1092 | class UFC_Medal implements UserFilterCondition |
ceb512d2 RB |
1093 | { |
1094 | private $medal; | |
1095 | private $grade; | |
1096 | ||
1097 | public function __construct($medal, $grade = null) | |
1098 | { | |
1099 | $this->medal = $medal; | |
1100 | $this->grade = $grade; | |
1101 | } | |
1102 | ||
dcc63ed5 | 1103 | public function buildCondition(PlFilter &$uf) |
ceb512d2 RB |
1104 | { |
1105 | $conds = array(); | |
1106 | $sub = $uf->addMedalFilter(); | |
1107 | $conds[] = $sub . '.mid = ' . XDB::format('{?}', $this->medal); | |
1108 | if ($this->grade != null) { | |
1109 | $conds[] = $sub . '.gid = ' . XDB::format('{?}', $this->grade); | |
1110 | } | |
1111 | return implode(' AND ', $conds); | |
1112 | } | |
1113 | } | |
1114 | // }}} | |
1115 | ||
671b7073 RB |
1116 | // {{{ class UFC_Mentor_Expertise |
1117 | /** Filters users by mentoring expertise | |
1118 | * @param $expertise Domain of expertise | |
1119 | */ | |
4e2f2ad2 | 1120 | class UFC_Mentor_Expertise implements UserFilterCondition |
671b7073 RB |
1121 | { |
1122 | private $expertise; | |
1123 | ||
1124 | public function __construct($expertise) | |
1125 | { | |
1126 | $this->expertise = $expertise; | |
1127 | } | |
1128 | ||
dcc63ed5 | 1129 | public function buildCondition(PlFilter &$uf) |
671b7073 RB |
1130 | { |
1131 | $sub = $uf->addMentorFilter(UserFilter::MENTOR_EXPERTISE); | |
1132 | return $sub . '.expertise LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\'', $this->expertise); | |
1133 | } | |
1134 | } | |
1135 | // }}} | |
1136 | ||
1137 | // {{{ class UFC_Mentor_Country | |
1138 | /** Filters users by mentoring country | |
1139 | * @param $country Two-letters code of country being searched | |
1140 | */ | |
4e2f2ad2 | 1141 | class UFC_Mentor_Country implements UserFilterCondition |
671b7073 RB |
1142 | { |
1143 | private $country; | |
1144 | ||
1145 | public function __construct($country) | |
1146 | { | |
1147 | $this->country = $country; | |
1148 | } | |
1149 | ||
dcc63ed5 | 1150 | public function buildCondition(PlFilter &$uf) |
671b7073 RB |
1151 | { |
1152 | $sub = $uf->addMentorFilter(UserFilter::MENTOR_COUNTRY); | |
1153 | return $sub . '.country = ' . XDB::format('{?}', $this->country); | |
1154 | } | |
1155 | } | |
1156 | // }}} | |
1157 | ||
1158 | // {{{ class UFC_Mentor_Sectorization | |
1159 | /** Filters users based on mentoring (sub|)sector | |
1160 | * @param $sector ID of sector | |
1161 | * @param $subsector Subsector (null for any) | |
1162 | */ | |
4e2f2ad2 | 1163 | class UFC_Mentor_Sectorization implements UserFilterCondition |
671b7073 RB |
1164 | { |
1165 | private $sector; | |
1166 | private $subsector; | |
1167 | ||
1168 | public function __construct($sector, $subsector = null) | |
1169 | { | |
1170 | $this->sector = $sector; | |
1171 | $this->subsubsector = $subsector; | |
1172 | } | |
1173 | ||
dcc63ed5 | 1174 | public function buildCondition(PlFilter &$uf) |
671b7073 RB |
1175 | { |
1176 | $sub = $uf->addMentorFilter(UserFilter::MENTOR_SECTOR); | |
1177 | $conds = array(); | |
1178 | $conds[] = $sub . '.sectorid = ' . XDB::format('{?}', $this->sector); | |
1179 | if ($this->subsector != null) { | |
1180 | $conds[] = $sub . '.subsectorid = ' . XDB::format('{?}', $this->subsector); | |
1181 | } | |
1182 | return implode(' AND ', $conds); | |
1183 | } | |
1184 | } | |
1185 | // }}} | |
1186 | ||
8363588b | 1187 | // {{{ class UFC_UserRelated |
5d2e55c7 | 1188 | /** Filters users based on a relation toward a user |
53eae167 RB |
1189 | * @param $user User to which searched users are related |
1190 | */ | |
4e7bf1e0 | 1191 | abstract class UFC_UserRelated implements UserFilterCondition |
3f42a6ad | 1192 | { |
009b8ab7 FB |
1193 | protected $user; |
1194 | public function __construct(PlUser &$user) | |
1195 | { | |
1196 | $this->user =& $user; | |
3f42a6ad | 1197 | } |
4e7bf1e0 | 1198 | } |
8363588b | 1199 | // }}} |
3f42a6ad | 1200 | |
8363588b | 1201 | // {{{ class UFC_Contact |
5d2e55c7 | 1202 | /** Filters users who belong to selected user's contacts |
53eae167 | 1203 | */ |
4e7bf1e0 FB |
1204 | class UFC_Contact extends UFC_UserRelated |
1205 | { | |
dcc63ed5 | 1206 | public function buildCondition(PlFilter &$uf) |
3f42a6ad | 1207 | { |
009b8ab7 | 1208 | $sub = $uf->addContactFilter($this->user->id()); |
3f42a6ad FB |
1209 | return 'c' . $sub . '.contact IS NOT NULL'; |
1210 | } | |
1211 | } | |
8363588b | 1212 | // }}} |
3f42a6ad | 1213 | |
8363588b | 1214 | // {{{ class UFC_WatchRegistration |
53eae167 RB |
1215 | /** Filters users being watched by selected user |
1216 | */ | |
4e7bf1e0 FB |
1217 | class UFC_WatchRegistration extends UFC_UserRelated |
1218 | { | |
dcc63ed5 | 1219 | public function buildCondition(PlFilter &$uf) |
4e7bf1e0 | 1220 | { |
009b8ab7 | 1221 | if (!$this->user->watch('registration')) { |
dcc63ed5 | 1222 | return PlFilterCondition::COND_FALSE; |
009b8ab7 FB |
1223 | } |
1224 | $uids = $this->user->watchUsers(); | |
1225 | if (count($uids) == 0) { | |
dcc63ed5 | 1226 | return PlFilterCondition::COND_FALSE; |
009b8ab7 | 1227 | } else { |
07eb5b0e | 1228 | return '$UID IN ' . XDB::formatArray($uids); |
009b8ab7 | 1229 | } |
4e7bf1e0 FB |
1230 | } |
1231 | } | |
8363588b | 1232 | // }}} |
4e7bf1e0 | 1233 | |
8363588b | 1234 | // {{{ class UFC_WatchPromo |
53eae167 RB |
1235 | /** Filters users belonging to a promo watched by selected user |
1236 | * @param $user Selected user (the one watching promo) | |
1237 | * @param $grade Formation the user is watching | |
1238 | */ | |
4e7bf1e0 FB |
1239 | class UFC_WatchPromo extends UFC_UserRelated |
1240 | { | |
1241 | private $grade; | |
009b8ab7 | 1242 | public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING) |
4e7bf1e0 | 1243 | { |
009b8ab7 | 1244 | parent::__construct($user); |
4e7bf1e0 FB |
1245 | $this->grade = $grade; |
1246 | } | |
1247 | ||
dcc63ed5 | 1248 | public function buildCondition(PlFilter &$uf) |
4e7bf1e0 | 1249 | { |
009b8ab7 FB |
1250 | $promos = $this->user->watchPromos(); |
1251 | if (count($promos) == 0) { | |
dcc63ed5 | 1252 | return PlFilterCondition::COND_FALSE; |
009b8ab7 FB |
1253 | } else { |
1254 | $sube = $uf->addEducationFilter(true, $this->grade); | |
1255 | $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade); | |
07eb5b0e | 1256 | return $field . ' IN ' . XDB::formatArray($promos); |
009b8ab7 | 1257 | } |
4e7bf1e0 FB |
1258 | } |
1259 | } | |
8363588b | 1260 | // }}} |
4e7bf1e0 | 1261 | |
8363588b | 1262 | // {{{ class UFC_WatchContact |
53eae167 RB |
1263 | /** Filters users watched by selected user |
1264 | */ | |
009b8ab7 | 1265 | class UFC_WatchContact extends UFC_Contact |
4e7bf1e0 | 1266 | { |
dcc63ed5 | 1267 | public function buildCondition(PlFilter &$uf) |
4e7bf1e0 | 1268 | { |
009b8ab7 | 1269 | if (!$this->user->watchContacts()) { |
dcc63ed5 | 1270 | return PlFilterCondition::COND_FALSE; |
009b8ab7 FB |
1271 | } |
1272 | return parent::buildCondition($uf); | |
4e7bf1e0 FB |
1273 | } |
1274 | } | |
8363588b | 1275 | // }}} |
4e7bf1e0 FB |
1276 | |
1277 | ||
d865c296 FB |
1278 | /****************** |
1279 | * ORDERS | |
1280 | ******************/ | |
1281 | ||
8363588b | 1282 | // {{{ class UserFilterOrder |
2d83cac9 RB |
1283 | /** Base class for ordering results of a query. |
1284 | * Parameters for the ordering must be given to the constructor ($desc for a | |
1285 | * descending order). | |
1286 | * The getSortTokens function is used to get actual ordering part of the query. | |
1287 | */ | |
7ca75030 | 1288 | abstract class UserFilterOrder extends PlFilterOrder |
d865c296 | 1289 | { |
2d83cac9 RB |
1290 | /** This function must return the tokens to use for ordering |
1291 | * @param &$uf The UserFilter whose results must be ordered | |
1292 | * @return The name of the field to use for ordering results | |
1293 | */ | |
d865c296 FB |
1294 | abstract protected function getSortTokens(UserFilter &$uf); |
1295 | } | |
8363588b | 1296 | // }}} |
d865c296 | 1297 | |
8363588b | 1298 | // {{{ class UFO_Promo |
5d2e55c7 RB |
1299 | /** Orders users by promotion |
1300 | * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation) | |
53eae167 RB |
1301 | * @param $desc Whether sort is descending |
1302 | */ | |
d865c296 FB |
1303 | class UFO_Promo extends UserFilterOrder |
1304 | { | |
1305 | private $grade; | |
1306 | ||
1307 | public function __construct($grade = null, $desc = false) | |
1308 | { | |
009b8ab7 | 1309 | parent::__construct($desc); |
d865c296 | 1310 | $this->grade = $grade; |
d865c296 FB |
1311 | } |
1312 | ||
1313 | protected function getSortTokens(UserFilter &$uf) | |
1314 | { | |
1315 | if (UserFilter::isGrade($this->grade)) { | |
1316 | $sub = $uf->addEducationFilter($this->grade); | |
1317 | return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade); | |
1318 | } else { | |
1319 | $sub = $uf->addDisplayFilter(); | |
1320 | return 'pd' . $sub . '.promo'; | |
1321 | } | |
1322 | } | |
1323 | } | |
8363588b | 1324 | // }}} |
d865c296 | 1325 | |
8363588b | 1326 | // {{{ class UFO_Name |
53eae167 | 1327 | /** Sorts users by name |
5d2e55c7 RB |
1328 | * @param $type Type of name on which to sort (firstname...) |
1329 | * @param $variant Variant of that name to use (marital, ordinary...) | |
53eae167 RB |
1330 | * @param $particle Set to true if particles should be included in the sorting order |
1331 | * @param $desc If sort order should be descending | |
1332 | */ | |
d865c296 FB |
1333 | class UFO_Name extends UserFilterOrder |
1334 | { | |
1335 | private $type; | |
1336 | private $variant; | |
1337 | private $particle; | |
1338 | ||
1339 | public function __construct($type, $variant = null, $particle = false, $desc = false) | |
1340 | { | |
009b8ab7 | 1341 | parent::__construct($desc); |
d865c296 FB |
1342 | $this->type = $type; |
1343 | $this->variant = $variant; | |
1344 | $this->particle = $particle; | |
d865c296 FB |
1345 | } |
1346 | ||
1347 | protected function getSortTokens(UserFilter &$uf) | |
1348 | { | |
1349 | if (UserFilter::isDisplayName($this->type)) { | |
1350 | $sub = $uf->addDisplayFilter(); | |
1351 | return 'pd' . $sub . '.' . $this->type; | |
1352 | } else { | |
1353 | $sub = $uf->addNameFilter($this->type, $this->variant); | |
1354 | if ($this->particle) { | |
1355 | return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)'; | |
1356 | } else { | |
1357 | return 'pn' . $sub . '.name'; | |
1358 | } | |
1359 | } | |
1360 | } | |
1361 | } | |
8363588b | 1362 | // }}} |
d865c296 | 1363 | |
40585144 RB |
1364 | // {{{ class UFO_Score |
1365 | class UFO_Score extends UserFilterOrder | |
1366 | { | |
1367 | protected function getSortTokens(UserFilter &$uf) | |
1368 | { | |
1369 | $sub = $uf->addNameTokensFilter(); | |
1370 | return 'SUM(' . $sub . '.score)'; | |
1371 | } | |
1372 | } | |
1373 | // }}} | |
1374 | ||
8363588b | 1375 | // {{{ class UFO_Registration |
53eae167 RB |
1376 | /** Sorts users based on registration date |
1377 | */ | |
38c6fe96 FB |
1378 | class UFO_Registration extends UserFilterOrder |
1379 | { | |
009b8ab7 | 1380 | protected function getSortTokens(UserFilter &$uf) |
38c6fe96 | 1381 | { |
009b8ab7 | 1382 | return 'a.registration_date'; |
38c6fe96 | 1383 | } |
009b8ab7 | 1384 | } |
8363588b | 1385 | // }}} |
38c6fe96 | 1386 | |
8363588b | 1387 | // {{{ class UFO_Birthday |
53eae167 RB |
1388 | /** Sorts users based on next birthday date |
1389 | */ | |
009b8ab7 FB |
1390 | class UFO_Birthday extends UserFilterOrder |
1391 | { | |
38c6fe96 FB |
1392 | protected function getSortTokens(UserFilter &$uf) |
1393 | { | |
009b8ab7 | 1394 | return 'p.next_birthday'; |
38c6fe96 FB |
1395 | } |
1396 | } | |
8363588b | 1397 | // }}} |
38c6fe96 | 1398 | |
8363588b | 1399 | // {{{ class UFO_ProfileUpdate |
53eae167 RB |
1400 | /** Sorts users based on last profile update |
1401 | */ | |
009b8ab7 FB |
1402 | class UFO_ProfileUpdate extends UserFilterOrder |
1403 | { | |
1404 | protected function getSortTokens(UserFilter &$uf) | |
1405 | { | |
1406 | return 'p.last_change'; | |
1407 | } | |
1408 | } | |
8363588b | 1409 | // }}} |
009b8ab7 | 1410 | |
8363588b | 1411 | // {{{ class UFO_Death |
53eae167 RB |
1412 | /** Sorts users based on death date |
1413 | */ | |
009b8ab7 FB |
1414 | class UFO_Death extends UserFilterOrder |
1415 | { | |
1416 | protected function getSortTokens(UserFilter &$uf) | |
1417 | { | |
1418 | return 'p.deathdate'; | |
1419 | } | |
1420 | } | |
8363588b | 1421 | // }}} |
009b8ab7 FB |
1422 | |
1423 | ||
d865c296 FB |
1424 | /*********************************** |
1425 | ********************************* | |
1426 | USER FILTER CLASS | |
1427 | ********************************* | |
1428 | ***********************************/ | |
1429 | ||
8363588b | 1430 | // {{{ class UserFilter |
2d83cac9 RB |
1431 | /** This class provides a convenient and centralized way of filtering users. |
1432 | * | |
1433 | * Usage: | |
1434 | * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t)); | |
1435 | * | |
1436 | * Resulting UserFilter can be used to: | |
1437 | * - get a list of User objects matching the filter | |
1438 | * - get a list of UIDs matching the filter | |
1439 | * - get the number of users matching the filter | |
1440 | * - check whether a given User matches the filter | |
1441 | * - filter a list of User objects depending on whether they match the filter | |
1442 | * | |
1443 | * Usage for UFC and UFO objects: | |
1444 | * A UserFilter will call all private functions named XXXJoins. | |
1445 | * These functions must return an array containing the list of join | |
1446 | * required by the various UFC and UFO associated to the UserFilter. | |
1447 | * Entries in those returned array are of the following form: | |
1448 | * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter') | |
1449 | * which will be translated into : | |
1450 | * join_type JOIN joined_table AS join_tablealias ON (join_criter) | |
1451 | * in the final query. | |
1452 | * | |
1453 | * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with | |
1454 | * profile.pid, and $UID with auth_user_md5.user_id. | |
1455 | * | |
1456 | * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined; | |
1457 | * its parameter will be used to set various private vars of the UserFilter describing | |
1458 | * the required joins ; such a function shall return the "join_tablealias" to use | |
1459 | * when referring to the joined table. | |
1460 | * | |
1461 | * For example, if data from profile_job must be available to filter results, | |
1462 | * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and | |
1463 | * return 'pj', the short name to use when referring to profile_job; when building | |
1464 | * the query, calling the jobJoins function will return an array containing a single | |
1465 | * row: | |
1466 | * 'pj' => array('left', 'profile_job', '$ME.pid = $UID'); | |
1467 | * | |
1468 | * The 'register_optional' function can be used to generate unique table aliases when | |
1469 | * the same table has to be joined several times with different aliases. | |
1470 | */ | |
9b8e5fb4 | 1471 | class UserFilter extends PlFilter |
a087cc8d | 1472 | { |
9b8e5fb4 | 1473 | protected $joinMethods = array(); |
7ca75030 | 1474 | |
9b8e5fb4 RB |
1475 | protected $joinMetas = array('$PID' => 'p.pid', |
1476 | '$UID' => 'a.uid', | |
1477 | ); | |
d865c296 | 1478 | |
a087cc8d | 1479 | private $root; |
24e08e33 | 1480 | private $sort = array(); |
784745ce | 1481 | private $query = null; |
24e08e33 | 1482 | private $orderby = null; |
784745ce | 1483 | |
aa21c568 | 1484 | private $lastcount = null; |
d865c296 | 1485 | |
24e08e33 | 1486 | public function __construct($cond = null, $sort = null) |
5dd9d823 | 1487 | { |
06598c13 | 1488 | if (empty($this->joinMethods)) { |
d865c296 FB |
1489 | $class = new ReflectionClass('UserFilter'); |
1490 | foreach ($class->getMethods() as $method) { | |
1491 | $name = $method->getName(); | |
1492 | if (substr($name, -5) == 'Joins' && $name != 'buildJoins') { | |
06598c13 | 1493 | $this->joinMethods[] = $name; |
d865c296 FB |
1494 | } |
1495 | } | |
1496 | } | |
5dd9d823 | 1497 | if (!is_null($cond)) { |
06598c13 | 1498 | if ($cond instanceof PlFilterCondition) { |
5dd9d823 FB |
1499 | $this->setCondition($cond); |
1500 | } | |
1501 | } | |
24e08e33 FB |
1502 | if (!is_null($sort)) { |
1503 | if ($sort instanceof UserFilterOrder) { | |
1504 | $this->addSort($sort); | |
d865c296 FB |
1505 | } else if (is_array($sort)) { |
1506 | foreach ($sort as $s) { | |
1507 | $this->addSort($s); | |
1508 | } | |
24e08e33 FB |
1509 | } |
1510 | } | |
5dd9d823 FB |
1511 | } |
1512 | ||
784745ce FB |
1513 | private function buildQuery() |
1514 | { | |
d865c296 FB |
1515 | if (is_null($this->orderby)) { |
1516 | $orders = array(); | |
1517 | foreach ($this->sort as $sort) { | |
1518 | $orders = array_merge($orders, $sort->buildSort($this)); | |
1519 | } | |
1520 | if (count($orders) == 0) { | |
1521 | $this->orderby = ''; | |
1522 | } else { | |
1523 | $this->orderby = 'ORDER BY ' . implode(', ', $orders); | |
1524 | } | |
1525 | } | |
784745ce FB |
1526 | if (is_null($this->query)) { |
1527 | $where = $this->root->buildCondition($this); | |
f7ea7450 RB |
1528 | if ($this->with_forced_sn) { |
1529 | $this->requireProfiles(); | |
1530 | $from = 'search_name AS sn'; | |
1531 | } else if ($this->with_accounts) { | |
b8dcf62d RB |
1532 | $from = 'accounts AS a'; |
1533 | } else { | |
1534 | $this->requireProfiles(); | |
1535 | $from = 'profiles AS p'; | |
1536 | } | |
f7ea7450 | 1537 | $joins = $this->buildJoins(); |
b8dcf62d | 1538 | $this->query = 'FROM ' . $from . ' |
784745ce FB |
1539 | ' . $joins . ' |
1540 | WHERE (' . $where . ')'; | |
1541 | } | |
1542 | } | |
1543 | ||
7ca75030 | 1544 | private function getUIDList($uids = null, PlLimit &$limit) |
d865c296 | 1545 | { |
b8dcf62d | 1546 | $this->requireAccounts(); |
d865c296 | 1547 | $this->buildQuery(); |
7ca75030 | 1548 | $lim = $limit->getSql(); |
d865c296 FB |
1549 | $cond = ''; |
1550 | if (!is_null($uids)) { | |
07eb5b0e | 1551 | $cond = ' AND a.uid IN ' . XDB::formatArray($uids); |
d865c296 FB |
1552 | } |
1553 | $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid | |
1554 | ' . $this->query . $cond . ' | |
1555 | GROUP BY a.uid | |
1556 | ' . $this->orderby . ' | |
7ca75030 | 1557 | ' . $lim); |
d865c296 FB |
1558 | $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()'); |
1559 | return $fetched; | |
1560 | } | |
1561 | ||
043b104b RB |
1562 | private function getPIDList($pids = null, PlLimit &$limit) |
1563 | { | |
1564 | $this->requireProfiles(); | |
1565 | $this->buildQuery(); | |
1566 | $lim = $limit->getSql(); | |
1567 | $cond = ''; | |
1568 | if (!is_null($pids)) { | |
1569 | $cond = ' AND p.pid IN ' . XDB::formatArray($pids); | |
1570 | } | |
1571 | $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid | |
1572 | ' . $this->query . $cond . ' | |
1573 | GROUP BY p.pid | |
1574 | ' . $this->orderby . ' | |
1575 | ' . $lim); | |
1576 | $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()'); | |
1577 | return $fetched; | |
1578 | } | |
1579 | ||
a087cc8d FB |
1580 | /** Check that the user match the given rule. |
1581 | */ | |
1582 | public function checkUser(PlUser &$user) | |
1583 | { | |
b8dcf62d | 1584 | $this->requireAccounts(); |
784745ce FB |
1585 | $this->buildQuery(); |
1586 | $count = (int)XDB::fetchOneCell('SELECT COUNT(*) | |
1587 | ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id())); | |
1588 | return $count == 1; | |
a087cc8d FB |
1589 | } |
1590 | ||
043b104b RB |
1591 | /** Check that the profile match the given rule. |
1592 | */ | |
1593 | public function checkProfile(Profile &$profile) | |
1594 | { | |
1595 | $this->requireProfiles(); | |
1596 | $this->buildQuery(); | |
1597 | $count = (int)XDB::fetchOneCell('SELECT COUNT(*) | |
1598 | ' . $this->query . XDB::format(' AND p.pid = {?}', $profile->id())); | |
1599 | return $count == 1; | |
1600 | } | |
1601 | ||
1602 | /** Default filter is on users | |
a087cc8d | 1603 | */ |
7ca75030 | 1604 | public function filter(array $users, PlLimit &$limit) |
a087cc8d | 1605 | { |
043b104b RB |
1606 | return $this->filterUsers($users, $limit); |
1607 | } | |
1608 | ||
1609 | /** Filter a list of users to extract the users matching the rule. | |
1610 | */ | |
1611 | public function filterUsers(array $users, PlLimit &$limit) | |
1612 | { | |
b8dcf62d | 1613 | $this->requireAccounts(); |
4927ee54 FB |
1614 | $this->buildQuery(); |
1615 | $table = array(); | |
1616 | $uids = array(); | |
1617 | foreach ($users as $user) { | |
07eb5b0e FB |
1618 | if ($user instanceof PlUser) { |
1619 | $uid = $user->id(); | |
1620 | } else { | |
1621 | $uid = $user; | |
1622 | } | |
1623 | $uids[] = $uid; | |
1624 | $table[$uid] = $user; | |
4927ee54 | 1625 | } |
7ca75030 | 1626 | $fetched = $this->getUIDList($uids, $limit); |
a087cc8d | 1627 | $output = array(); |
4927ee54 FB |
1628 | foreach ($fetched as $uid) { |
1629 | $output[] = $table[$uid]; | |
a087cc8d FB |
1630 | } |
1631 | return $output; | |
1632 | } | |
1633 | ||
043b104b RB |
1634 | /** Filter a list of profiles to extract the users matching the rule. |
1635 | */ | |
1636 | public function filterProfiles(array $profiles, PlLimit &$limit) | |
1637 | { | |
1638 | $this->requireProfiles(); | |
1639 | $this->buildQuery(); | |
1640 | $table = array(); | |
1641 | $pids = array(); | |
1642 | foreach ($profiles as $profile) { | |
1643 | if ($profile instanceof Profile) { | |
1644 | $pid = $profile->id(); | |
1645 | } else { | |
1646 | $pid = $profile; | |
1647 | } | |
1648 | $pids[] = $pid; | |
1649 | $table[$pid] = $profile; | |
1650 | } | |
1651 | $fetched = $this->getPIDList($pids, $limit); | |
1652 | $output = array(); | |
1653 | foreach ($fetched as $pid) { | |
1654 | $output[] = $table[$pid]; | |
1655 | } | |
1656 | return $output; | |
1657 | } | |
1658 | ||
7ca75030 RB |
1659 | public function getUIDs(PlLimit &$limit) |
1660 | { | |
1661 | return $this->getUIDList(null, $limit); | |
1662 | } | |
1663 | ||
043b104b RB |
1664 | public function getPIDs(PlLimit &$limit) |
1665 | { | |
1666 | return $this->getPIDList(null, $limit); | |
1667 | } | |
1668 | ||
7ca75030 | 1669 | public function getUsers(PlLimit &$limit) |
4927ee54 | 1670 | { |
7ca75030 | 1671 | return User::getBulkUsersWithUIDs($this->getUIDs($limit)); |
d865c296 FB |
1672 | } |
1673 | ||
043b104b RB |
1674 | public function getProfiles(PlLimit &$limit) |
1675 | { | |
1676 | return Profile::getBulkProfilesWithPIDs($this->getPIDs($limit)); | |
1677 | } | |
1678 | ||
7ca75030 | 1679 | public function get(PlLimit &$limit) |
d865c296 | 1680 | { |
7ca75030 | 1681 | return $this->getUsers($limit); |
4927ee54 FB |
1682 | } |
1683 | ||
d865c296 | 1684 | public function getTotalCount() |
4927ee54 | 1685 | { |
38c6fe96 | 1686 | if (is_null($this->lastcount)) { |
aa21c568 | 1687 | $this->buildQuery(); |
2b9ca54d | 1688 | if ($this->with_accounts) { |
b8dcf62d RB |
1689 | $field = 'a.uid'; |
1690 | } else { | |
1691 | $field = 'p.pid'; | |
1692 | } | |
1693 | return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT ' . $field . ') | |
7e735012 | 1694 | ' . $this->query); |
38c6fe96 FB |
1695 | } else { |
1696 | return $this->lastcount; | |
1697 | } | |
4927ee54 FB |
1698 | } |
1699 | ||
9b8e5fb4 | 1700 | public function setCondition(PlFilterCondition &$cond) |
a087cc8d FB |
1701 | { |
1702 | $this->root =& $cond; | |
784745ce | 1703 | $this->query = null; |
a087cc8d FB |
1704 | } |
1705 | ||
9b8e5fb4 | 1706 | public function addSort(PlFilterOrder &$sort) |
24e08e33 | 1707 | { |
d865c296 FB |
1708 | $this->sort[] = $sort; |
1709 | $this->orderby = null; | |
24e08e33 FB |
1710 | } |
1711 | ||
a087cc8d FB |
1712 | static public function getLegacy($promo_min, $promo_max) |
1713 | { | |
a087cc8d | 1714 | if ($promo_min != 0) { |
784745ce | 1715 | $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min)); |
5dd9d823 FB |
1716 | } else { |
1717 | $min = new UFC_True(); | |
a087cc8d | 1718 | } |
a087cc8d | 1719 | if ($promo_max != 0) { |
784745ce | 1720 | $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max)); |
a087cc8d | 1721 | } else { |
5dd9d823 | 1722 | $max = new UFC_True(); |
a087cc8d | 1723 | } |
9b8e5fb4 | 1724 | return new UserFilter(new PFC_And($min, $max)); |
a087cc8d | 1725 | } |
784745ce | 1726 | |
07eb5b0e FB |
1727 | static public function sortByName() |
1728 | { | |
1729 | return array(new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME)); | |
1730 | } | |
1731 | ||
1732 | static public function sortByPromo() | |
1733 | { | |
1734 | return array(new UFO_Promo(), new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME)); | |
1735 | } | |
1736 | ||
aa21c568 FB |
1737 | static private function getDBSuffix($string) |
1738 | { | |
1739 | return preg_replace('/[^a-z0-9]/i', '', $string); | |
1740 | } | |
1741 | ||
1742 | ||
2d83cac9 RB |
1743 | /** Stores a new (and unique) table alias in the &$table table |
1744 | * @param &$table Array in which the table alias must be stored | |
1745 | * @param $val Value which will then be used to build the join | |
1746 | * @return Name of the newly created alias | |
1747 | */ | |
aa21c568 FB |
1748 | private $option = 0; |
1749 | private function register_optional(array &$table, $val) | |
1750 | { | |
1751 | if (is_null($val)) { | |
1752 | $sub = $this->option++; | |
1753 | $index = null; | |
1754 | } else { | |
1755 | $sub = self::getDBSuffix($val); | |
1756 | $index = $val; | |
1757 | } | |
1758 | $sub = '_' . $sub; | |
1759 | $table[$sub] = $index; | |
1760 | return $sub; | |
1761 | } | |
784745ce | 1762 | |
b8dcf62d RB |
1763 | /** PROFILE VS ACCOUNT |
1764 | */ | |
f7ea7450 RB |
1765 | private $with_profiles = false; |
1766 | private $with_accounts = false; | |
1767 | private $with_forced_sn = false; | |
b8dcf62d RB |
1768 | public function requireAccounts() |
1769 | { | |
1770 | $this->with_accounts = true; | |
1771 | } | |
1772 | ||
1773 | public function requireProfiles() | |
1774 | { | |
1775 | $this->with_profiles = true; | |
1776 | } | |
1777 | ||
f7ea7450 RB |
1778 | /** Forces the "FROM" to use search_name instead of accounts or profiles */ |
1779 | public function forceSearchName() | |
1780 | { | |
1781 | $this->with_forced_sn = true; | |
1782 | } | |
1783 | ||
b8dcf62d RB |
1784 | protected function accountJoins() |
1785 | { | |
1786 | $joins = array(); | |
f7ea7450 RB |
1787 | /** Quick search is much more efficient with sn first and PID second */ |
1788 | if ($this->with_forced_sn) { | |
1789 | $joins['p'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profiles', '$PID = sn.uid'); | |
1790 | if ($this->with_accounts) { | |
1791 | $joins['ap'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'account_profiles', '$ME.pid = $PID'); | |
1792 | $joins['a'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'accounts', '$UID = ap.uid'); | |
1793 | } | |
1794 | } else if ($this->with_profiles && $this->with_accounts) { | |
b8dcf62d RB |
1795 | $joins['ap'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)'); |
1796 | $joins['p'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profiles', '$PID = ap.pid'); | |
1797 | } | |
1798 | return $joins; | |
1799 | } | |
1800 | ||
d865c296 FB |
1801 | /** DISPLAY |
1802 | */ | |
38c6fe96 | 1803 | const DISPLAY = 'display'; |
d865c296 FB |
1804 | private $pd = false; |
1805 | public function addDisplayFilter() | |
1806 | { | |
b8dcf62d | 1807 | $this->requireProfiles(); |
d865c296 FB |
1808 | $this->pd = true; |
1809 | return ''; | |
1810 | } | |
1811 | ||
9b8e5fb4 | 1812 | protected function displayJoins() |
d865c296 FB |
1813 | { |
1814 | if ($this->pd) { | |
7ca75030 | 1815 | return array('pd' => new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_display', '$ME.pid = $PID')); |
d865c296 FB |
1816 | } else { |
1817 | return array(); | |
1818 | } | |
1819 | } | |
1820 | ||
784745ce FB |
1821 | /** NAMES |
1822 | */ | |
d865c296 | 1823 | /* name tokens */ |
784745ce FB |
1824 | const LASTNAME = 'lastname'; |
1825 | const FIRSTNAME = 'firstname'; | |
1826 | const NICKNAME = 'nickname'; | |
1827 | const PSEUDONYM = 'pseudonym'; | |
1828 | const NAME = 'name'; | |
d865c296 | 1829 | /* name variants */ |
784745ce FB |
1830 | const VN_MARITAL = 'marital'; |
1831 | const VN_ORDINARY = 'ordinary'; | |
1832 | const VN_OTHER = 'other'; | |
1833 | const VN_INI = 'ini'; | |
d865c296 FB |
1834 | /* display names */ |
1835 | const DN_FULL = 'directory_name'; | |
1836 | const DN_DISPLAY = 'yourself'; | |
1837 | const DN_YOURSELF = 'yourself'; | |
1838 | const DN_DIRECTORY = 'directory_name'; | |
1839 | const DN_PRIVATE = 'private_name'; | |
1840 | const DN_PUBLIC = 'public_name'; | |
1841 | const DN_SHORT = 'short_name'; | |
1842 | const DN_SORT = 'sort_name'; | |
784745ce FB |
1843 | |
1844 | static public $name_variants = array( | |
1845 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), | |
d865c296 FB |
1846 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) |
1847 | ); | |
784745ce FB |
1848 | |
1849 | static public function assertName($name) | |
1850 | { | |
1851 | if (!Profile::getNameTypeId($name)) { | |
9b8e5fb4 | 1852 | Platal::page()->kill('Invalid name type: ' . $name); |
784745ce FB |
1853 | } |
1854 | } | |
1855 | ||
d865c296 FB |
1856 | static public function isDisplayName($name) |
1857 | { | |
1858 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
1859 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
1860 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
1861 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
1862 | } | |
1863 | ||
784745ce | 1864 | private $pn = array(); |
784745ce FB |
1865 | public function addNameFilter($type, $variant = null) |
1866 | { | |
b8dcf62d | 1867 | $this->requireProfiles(); |
784745ce FB |
1868 | if (!is_null($variant)) { |
1869 | $ft = $type . '_' . $variant; | |
1870 | } else { | |
1871 | $ft = $type; | |
1872 | } | |
1873 | $sub = '_' . $ft; | |
1874 | self::assertName($ft); | |
1875 | ||
1876 | if (!is_null($variant) && $variant == 'other') { | |
aa21c568 | 1877 | $sub .= $this->option++; |
784745ce FB |
1878 | } |
1879 | $this->pn[$sub] = Profile::getNameTypeId($ft); | |
1880 | return $sub; | |
1881 | } | |
1882 | ||
9b8e5fb4 | 1883 | protected function nameJoins() |
784745ce FB |
1884 | { |
1885 | $joins = array(); | |
1886 | foreach ($this->pn as $sub => $type) { | |
7ca75030 | 1887 | $joins['pn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type); |
784745ce FB |
1888 | } |
1889 | return $joins; | |
1890 | } | |
1891 | ||
40585144 RB |
1892 | /** NAMETOKENS |
1893 | */ | |
1894 | private $with_sn = false; | |
f7ea7450 RB |
1895 | // Set $doingQuickSearch to true if you wish to optimize the query |
1896 | public function addNameTokensFilter($doingQuickSearch = false) | |
40585144 RB |
1897 | { |
1898 | $this->requireProfiles(); | |
f7ea7450 | 1899 | $this->with_forced_sn = ($this->with_forced_sn || $doingQuickSearch); |
40585144 RB |
1900 | $this->with_sn = true; |
1901 | return 'sn'; | |
1902 | } | |
1903 | ||
1904 | protected function nameTokensJoins() | |
1905 | { | |
f7ea7450 RB |
1906 | /* We don't return joins, since with_sn forces the SELECT to run on search_name first */ |
1907 | if ($this->with_sn && !$this->with_forced_sn) { | |
1908 | return array( | |
1909 | 'sn' => new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'search_name', '$ME.uid = $PID') | |
1910 | ); | |
1911 | } else { | |
1912 | return array(); | |
40585144 | 1913 | } |
40585144 RB |
1914 | } |
1915 | ||
a7f8e48a RB |
1916 | /** NATIONALITY |
1917 | */ | |
1918 | ||
1919 | private $with_nat = false; | |
1920 | public function addNationalityFilter() | |
1921 | { | |
1922 | $this->with_nat = true; | |
1923 | return 'ngc'; | |
1924 | } | |
1925 | ||
1926 | protected function nationalityJoins() | |
1927 | { | |
1928 | $joins = array(); | |
1929 | if ($this->with_nat) { | |
1930 | $joins['ngc'] = new PlSqlJoin(PlSqlJoin::MODE_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'); | |
1931 | } | |
1932 | return $joins; | |
1933 | } | |
1934 | ||
784745ce FB |
1935 | /** EDUCATION |
1936 | */ | |
1937 | const GRADE_ING = 'Ing.'; | |
1938 | const GRADE_PHD = 'PhD'; | |
1939 | const GRADE_MST = 'M%'; | |
1940 | static public function isGrade($grade) | |
1941 | { | |
38c6fe96 | 1942 | return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST; |
784745ce FB |
1943 | } |
1944 | ||
1945 | static public function assertGrade($grade) | |
1946 | { | |
1947 | if (!self::isGrade($grade)) { | |
1948 | Platal::page()->killError("Diplôme non valide"); | |
1949 | } | |
1950 | } | |
1951 | ||
d865c296 FB |
1952 | static public function promoYear($grade) |
1953 | { | |
1954 | // XXX: Definition of promotion for phds and masters might change in near future. | |
1955 | return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year'; | |
1956 | } | |
1957 | ||
784745ce FB |
1958 | private $pepe = array(); |
1959 | private $with_pee = false; | |
784745ce FB |
1960 | public function addEducationFilter($x = false, $grade = null) |
1961 | { | |
b8dcf62d | 1962 | $this->requireProfiles(); |
784745ce | 1963 | if (!$x) { |
aa21c568 FB |
1964 | $index = $this->option; |
1965 | $sub = $this->option++; | |
784745ce FB |
1966 | } else { |
1967 | self::assertGrade($grade); | |
1968 | $index = $grade; | |
1969 | $sub = $grade[0]; | |
1970 | $this->with_pee = true; | |
1971 | } | |
1972 | $sub = '_' . $sub; | |
1973 | $this->pepe[$index] = $sub; | |
1974 | return $sub; | |
1975 | } | |
1976 | ||
9b8e5fb4 | 1977 | protected function educationJoins() |
784745ce FB |
1978 | { |
1979 | $joins = array(); | |
1980 | if ($this->with_pee) { | |
7ca75030 | 1981 | $joins['pee'] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_enum', 'pee.abbreviation = \'X\''); |
784745ce FB |
1982 | } |
1983 | foreach ($this->pepe as $grade => $sub) { | |
1984 | if ($this->isGrade($grade)) { | |
7ca75030 RB |
1985 | $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID'); |
1986 | $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' . | |
784745ce FB |
1987 | XDB::format('{?}', $grade)); |
1988 | } else { | |
7ca75030 RB |
1989 | $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_education', '$ME.uid = $PID'); |
1990 | $joins['pee' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid'); | |
1991 | $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid'); | |
784745ce FB |
1992 | } |
1993 | } | |
1994 | return $joins; | |
1995 | } | |
4927ee54 FB |
1996 | |
1997 | ||
1998 | /** GROUPS | |
1999 | */ | |
2000 | private $gpm = array(); | |
4927ee54 FB |
2001 | public function addGroupFilter($group = null) |
2002 | { | |
b8dcf62d | 2003 | $this->requireAccounts(); |
4927ee54 FB |
2004 | if (!is_null($group)) { |
2005 | if (ctype_digit($group)) { | |
2006 | $index = $sub = $group; | |
2007 | } else { | |
2008 | $index = $group; | |
aa21c568 | 2009 | $sub = self::getDBSuffix($group); |
4927ee54 FB |
2010 | } |
2011 | } else { | |
aa21c568 | 2012 | $sub = 'group_' . $this->option++; |
4927ee54 FB |
2013 | $index = null; |
2014 | } | |
2015 | $sub = '_' . $sub; | |
2016 | $this->gpm[$sub] = $index; | |
2017 | return $sub; | |
2018 | } | |
2019 | ||
9b8e5fb4 | 2020 | protected function groupJoins() |
4927ee54 FB |
2021 | { |
2022 | $joins = array(); | |
2023 | foreach ($this->gpm as $sub => $key) { | |
2024 | if (is_null($key)) { | |
7ca75030 RB |
2025 | $joins['gpa' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'groupex.asso'); |
2026 | $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
4927ee54 | 2027 | } else if (ctype_digit($key)) { |
7ca75030 | 2028 | $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key); |
4927ee54 | 2029 | } else { |
7ca75030 RB |
2030 | $joins['gpa' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key)); |
2031 | $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
4927ee54 FB |
2032 | } |
2033 | } | |
2034 | return $joins; | |
0fb3713c RB |
2035 | } |
2036 | ||
2037 | /** BINETS | |
2038 | */ | |
2039 | ||
a7f8e48a RB |
2040 | private $with_bi = false; |
2041 | private $with_bd = false; | |
d7ddf29b | 2042 | public function addBinetsFilter($with_enum = false) |
0fb3713c RB |
2043 | { |
2044 | $this->requireProfiles(); | |
a7f8e48a RB |
2045 | $this->with_bi = true; |
2046 | if ($with_enum) { | |
2047 | $this->with_bd = true; | |
2048 | return 'bd'; | |
2049 | } else { | |
2050 | return 'bi'; | |
2051 | } | |
0fb3713c RB |
2052 | } |
2053 | ||
2054 | protected function binetsJoins() | |
2055 | { | |
2056 | $joins = array(); | |
a7f8e48a | 2057 | if ($this->with_bi) { |
d7ddf29b | 2058 | $joins['bi'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'binets_ins', '$ME.user_id = $PID'); |
0fb3713c | 2059 | } |
a7f8e48a RB |
2060 | if ($this->with_bd) { |
2061 | $joins['bd'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'binets_def', '$ME.id = bi.binet_id'); | |
2062 | } | |
0fb3713c | 2063 | return $joins; |
4927ee54 | 2064 | } |
aa21c568 FB |
2065 | |
2066 | /** EMAILS | |
2067 | */ | |
2068 | private $e = array(); | |
2069 | public function addEmailRedirectFilter($email = null) | |
2070 | { | |
b8dcf62d | 2071 | $this->requireAccounts(); |
aa21c568 FB |
2072 | return $this->register_optional($this->e, $email); |
2073 | } | |
2074 | ||
2075 | private $ve = array(); | |
2076 | public function addVirtualEmailFilter($email = null) | |
2077 | { | |
21401768 | 2078 | $this->addAliasFilter(self::ALIAS_FORLIFE); |
aa21c568 FB |
2079 | return $this->register_optional($this->ve, $email); |
2080 | } | |
2081 | ||
21401768 FB |
2082 | const ALIAS_BEST = 'bestalias'; |
2083 | const ALIAS_FORLIFE = 'forlife'; | |
aa21c568 FB |
2084 | private $al = array(); |
2085 | public function addAliasFilter($alias = null) | |
2086 | { | |
b8dcf62d | 2087 | $this->requireAccounts(); |
aa21c568 FB |
2088 | return $this->register_optional($this->al, $alias); |
2089 | } | |
2090 | ||
9b8e5fb4 | 2091 | protected function emailJoins() |
aa21c568 FB |
2092 | { |
2093 | global $globals; | |
2094 | $joins = array(); | |
2095 | foreach ($this->e as $sub=>$key) { | |
2096 | if (is_null($key)) { | |
7ca75030 | 2097 | $joins['e' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\''); |
aa21c568 | 2098 | } else { |
7ca75030 | 2099 | $joins['e' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key)); |
aa21c568 FB |
2100 | } |
2101 | } | |
21401768 | 2102 | foreach ($this->al as $sub=>$key) { |
aa21c568 | 2103 | if (is_null($key)) { |
7ca75030 | 2104 | $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')'); |
21401768 | 2105 | } else if ($key == self::ALIAS_BEST) { |
7ca75030 | 2106 | $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)'); |
21401768 | 2107 | } else if ($key == self::ALIAS_FORLIFE) { |
7ca75030 | 2108 | $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\''); |
aa21c568 | 2109 | } else { |
7ca75030 | 2110 | $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key)); |
aa21c568 | 2111 | } |
aa21c568 | 2112 | } |
21401768 | 2113 | foreach ($this->ve as $sub=>$key) { |
aa21c568 | 2114 | if (is_null($key)) { |
7ca75030 | 2115 | $joins['v' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'virtual', '$ME.type = \'user\''); |
aa21c568 | 2116 | } else { |
7ca75030 | 2117 | $joins['v' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key)); |
aa21c568 | 2118 | } |
7ca75030 | 2119 | $joins['vr' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid |
21401768 FB |
2120 | AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}), |
2121 | CONCAT(al_forlife.alias, \'@\', {?}), | |
2122 | a.email))', | |
2123 | $globals->mail->domain, $globals->mail->domain2)); | |
aa21c568 FB |
2124 | } |
2125 | return $joins; | |
2126 | } | |
3f42a6ad FB |
2127 | |
2128 | ||
c4b24511 RB |
2129 | /** ADDRESSES |
2130 | */ | |
036d1637 | 2131 | private $with_pa = false; |
c4b24511 RB |
2132 | public function addAddressFilter() |
2133 | { | |
b8dcf62d | 2134 | $this->requireProfiles(); |
036d1637 RB |
2135 | $this->with_pa = true; |
2136 | return 'pa'; | |
c4b24511 RB |
2137 | } |
2138 | ||
2b9ca54d RB |
2139 | private $with_pac = false; |
2140 | public function addAddressCountryFilter() | |
2141 | { | |
2142 | $this->requireProfiles(); | |
2143 | $this->addAddressFilter(); | |
2144 | $this->with_pac = true; | |
2145 | return 'gc'; | |
2146 | } | |
2147 | ||
d7ddf29b | 2148 | private $with_pal = false; |
2b9ca54d RB |
2149 | public function addAddressLocalityFilter() |
2150 | { | |
2151 | $this->requireProfiles(); | |
2152 | $this->addAddressFilter(); | |
2153 | $this->with_pal = true; | |
2154 | return 'gl'; | |
2155 | } | |
2156 | ||
9b8e5fb4 | 2157 | protected function addressJoins() |
c4b24511 RB |
2158 | { |
2159 | $joins = array(); | |
036d1637 | 2160 | if ($this->with_pa) { |
d7ddf29b | 2161 | $joins['pa'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_addresses', '$ME.pid = $PID'); |
c4b24511 | 2162 | } |
2b9ca54d RB |
2163 | if ($this->with_pac) { |
2164 | $joins['gc'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID'); | |
2165 | } | |
2166 | if ($this->with_pal) { | |
2167 | $joins['gl'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'geoloc_localities', '$ME.id = pa.localityID'); | |
2168 | } | |
c4b24511 RB |
2169 | return $joins; |
2170 | } | |
2171 | ||
2172 | ||
4083b126 RB |
2173 | /** CORPS |
2174 | */ | |
2175 | ||
2176 | private $pc = false; | |
2177 | private $pce = array(); | |
2178 | private $pcr = false; | |
2179 | public function addCorpsFilter($type) | |
2180 | { | |
b8dcf62d | 2181 | $this->requireProfiles(); |
4083b126 RB |
2182 | $this->pc = true; |
2183 | if ($type == UFC_Corps::CURRENT) { | |
2184 | $pce['pcec'] = 'current_corpsid'; | |
2185 | return 'pcec'; | |
2186 | } else if ($type == UFC_Corps::ORIGIN) { | |
2187 | $pce['pceo'] = 'original_corpsid'; | |
2188 | return 'pceo'; | |
2189 | } | |
2190 | } | |
2191 | ||
2192 | public function addCorpsRankFilter() | |
2193 | { | |
b8dcf62d | 2194 | $this->requireProfiles(); |
4083b126 RB |
2195 | $this->pc = true; |
2196 | $this->pcr = true; | |
2197 | return 'pcr'; | |
2198 | } | |
2199 | ||
9b8e5fb4 | 2200 | protected function corpsJoins() |
4083b126 RB |
2201 | { |
2202 | $joins = array(); | |
2203 | if ($this->pc) { | |
7ca75030 | 2204 | $joins['pc'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps', '$ME.uid = $UID'); |
4083b126 RB |
2205 | } |
2206 | if ($this->pcr) { | |
7ca75030 | 2207 | $joins['pcr'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps_rank_enum', '$ME.id = pc.rankid'); |
4083b126 RB |
2208 | } |
2209 | foreach($this->pce as $sub => $field) { | |
7ca75030 | 2210 | $joins[$sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps_enum', '$ME.id = pc.' . $field); |
4083b126 RB |
2211 | } |
2212 | return $joins; | |
2213 | } | |
2214 | ||
6a99c3ac RB |
2215 | /** JOBS |
2216 | */ | |
2217 | ||
2218 | const JOB_SECTOR = 1; | |
2219 | const JOB_SUBSECTOR = 2; | |
2220 | const JOB_SUBSUBSECTOR = 4; | |
2221 | const JOB_ALTERNATES = 8; | |
2222 | const JOB_USERDEFINED = 16; | |
01cc5f9e | 2223 | const JOB_CV = 32; |
6a99c3ac RB |
2224 | |
2225 | /** Joins : | |
2226 | * pj => profile_job | |
2227 | * pje => profile_job_enum | |
2228 | * pjse => profile_job_sector_enum | |
2229 | * pjsse => profile_job_subsector_enum | |
2230 | * pjssse => profile_job_subsubsector_enum | |
2231 | * pja => profile_job_alternates | |
2232 | */ | |
2233 | private $with_pj = false; | |
2234 | private $with_pje = false; | |
2235 | private $with_pjse = false; | |
2236 | private $with_pjsse = false; | |
2237 | private $with_pjssse = false; | |
2238 | private $with_pja = false; | |
2239 | ||
2240 | public function addJobFilter() | |
2241 | { | |
b8dcf62d | 2242 | $this->requireProfiles(); |
6a99c3ac RB |
2243 | $this->with_pj = true; |
2244 | return 'pj'; | |
2245 | } | |
2246 | ||
2247 | public function addJobCompanyFilter() | |
2248 | { | |
2249 | $this->addJobFilter(); | |
2250 | $this->with_pje = true; | |
2251 | return 'pje'; | |
2252 | } | |
2253 | ||
2254 | public function addJobSectorizationFilter($type) | |
2255 | { | |
2256 | $this->addJobFilter(); | |
2257 | if ($type == self::JOB_SECTOR) { | |
2258 | $this->with_pjse = true; | |
2259 | return 'pjse'; | |
2260 | } else if ($type == self::JOB_SUBSECTOR) { | |
2261 | $this->with_pjsse = true; | |
2262 | return 'pjsse'; | |
2263 | } else if ($type == self::JOB_SUBSUBSECTOR) { | |
2264 | $this->with_pjssse = true; | |
2265 | return 'pjssse'; | |
2266 | } else if ($type == self::JOB_ALTERNATES) { | |
2267 | $this->with_pja = true; | |
2268 | return 'pja'; | |
2269 | } | |
2270 | } | |
2271 | ||
9b8e5fb4 | 2272 | protected function jobJoins() |
6a99c3ac RB |
2273 | { |
2274 | $joins = array(); | |
2275 | if ($this->with_pj) { | |
7ca75030 | 2276 | $joins['pj'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job', '$ME.uid = $UID'); |
6a99c3ac RB |
2277 | } |
2278 | if ($this->with_pje) { | |
7ca75030 | 2279 | $joins['pje'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_enum', '$ME.id = pj.jobid'); |
6a99c3ac RB |
2280 | } |
2281 | if ($this->with_pjse) { | |
7ca75030 | 2282 | $joins['pjse'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_sector_enum', '$ME.id = pj.sectorid'); |
6a99c3ac RB |
2283 | } |
2284 | if ($this->with_pjsse) { | |
7ca75030 | 2285 | $joins['pjsse'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_subsector_enum', '$ME.id = pj.subsectorid'); |
6a99c3ac RB |
2286 | } |
2287 | if ($this->with_pjssse) { | |
7ca75030 | 2288 | $joins['pjssse'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid'); |
6a99c3ac RB |
2289 | } |
2290 | if ($this->with_pja) { | |
7ca75030 | 2291 | $joins['pja'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid'); |
6a99c3ac RB |
2292 | } |
2293 | return $joins; | |
2294 | } | |
2295 | ||
0a2e9c74 RB |
2296 | /** NETWORKING |
2297 | */ | |
2298 | ||
2299 | private $with_pnw = false; | |
2300 | public function addNetworkingFilter() | |
2301 | { | |
b8dcf62d | 2302 | $this->requireAccounts(); |
0a2e9c74 RB |
2303 | $this->with_pnw = true; |
2304 | return 'pnw'; | |
2305 | } | |
2306 | ||
9b8e5fb4 | 2307 | protected function networkingJoins() |
0a2e9c74 RB |
2308 | { |
2309 | $joins = array(); | |
2310 | if ($this->with_pnw) { | |
7ca75030 | 2311 | $joins['pnw'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_networking', '$ME.uid = $UID'); |
0a2e9c74 RB |
2312 | } |
2313 | return $joins; | |
2314 | } | |
2315 | ||
6d62969e RB |
2316 | /** PHONE |
2317 | */ | |
2318 | ||
2d83cac9 | 2319 | private $with_ptel = false; |
6d62969e RB |
2320 | |
2321 | public function addPhoneFilter() | |
2322 | { | |
b8dcf62d | 2323 | $this->requireAccounts(); |
2d83cac9 | 2324 | $this->with_ptel = true; |
6d62969e RB |
2325 | return 'ptel'; |
2326 | } | |
2327 | ||
9b8e5fb4 | 2328 | protected function phoneJoins() |
6d62969e RB |
2329 | { |
2330 | $joins = array(); | |
2d83cac9 | 2331 | if ($this->with_ptel) { |
9b8e5fb4 | 2332 | $joins['ptel'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_phones', '$ME.uid = $UID'); |
6d62969e RB |
2333 | } |
2334 | return $joins; | |
2335 | } | |
2336 | ||
ceb512d2 RB |
2337 | /** MEDALS |
2338 | */ | |
2339 | ||
2d83cac9 | 2340 | private $with_pmed = false; |
ceb512d2 RB |
2341 | public function addMedalFilter() |
2342 | { | |
b8dcf62d | 2343 | $this->requireProfiles(); |
2d83cac9 | 2344 | $this->with_pmed = true; |
ceb512d2 RB |
2345 | return 'pmed'; |
2346 | } | |
2347 | ||
9b8e5fb4 | 2348 | protected function medalJoins() |
ceb512d2 RB |
2349 | { |
2350 | $joins = array(); | |
2d83cac9 | 2351 | if ($this->with_pmed) { |
7ca75030 | 2352 | $joins['pmed'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_medals_sub', '$ME.uid = $UID'); |
ceb512d2 RB |
2353 | } |
2354 | return $joins; | |
2355 | } | |
2356 | ||
671b7073 RB |
2357 | /** MENTORING |
2358 | */ | |
2359 | ||
2360 | private $pms = array(); | |
2361 | const MENTOR_EXPERTISE = 1; | |
2362 | const MENTOR_COUNTRY = 2; | |
2363 | const MENTOR_SECTOR = 3; | |
2364 | ||
2365 | public function addMentorFilter($type) | |
2366 | { | |
b8dcf62d | 2367 | $this->requireAccounts(); |
671b7073 RB |
2368 | switch($type) { |
2369 | case MENTOR_EXPERTISE: | |
2370 | $pms['pme'] = 'profile_mentor'; | |
2371 | return 'pme'; | |
2372 | case MENTOR_COUNTRY: | |
2373 | $pms['pmc'] = 'profile_mentor_country'; | |
2374 | return 'pmc'; | |
2375 | case MENTOR_SECTOR: | |
2376 | $pms['pms'] = 'profile_mentor_sector'; | |
2377 | return 'pms'; | |
2378 | default: | |
5d2e55c7 | 2379 | Platal::page()->killError("Undefined mentor filter."); |
671b7073 RB |
2380 | } |
2381 | } | |
2382 | ||
9b8e5fb4 | 2383 | protected function mentorJoins() |
671b7073 RB |
2384 | { |
2385 | $joins = array(); | |
2386 | foreach ($this->pms as $sub => $tab) { | |
7ca75030 | 2387 | $joins[$sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, $tab, '$ME.uid = $UID'); |
671b7073 RB |
2388 | } |
2389 | return $joins; | |
2390 | } | |
2391 | ||
3f42a6ad FB |
2392 | /** CONTACTS |
2393 | */ | |
2394 | private $cts = array(); | |
2395 | public function addContactFilter($uid = null) | |
2396 | { | |
b8dcf62d | 2397 | $this->requireAccounts(); |
3f42a6ad FB |
2398 | return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid); |
2399 | } | |
2400 | ||
9b8e5fb4 | 2401 | protected function contactJoins() |
3f42a6ad FB |
2402 | { |
2403 | $joins = array(); | |
2404 | foreach ($this->cts as $sub=>$key) { | |
2405 | if (is_null($key)) { | |
7ca75030 | 2406 | $joins['c' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'contacts', '$ME.contact = $UID'); |
3f42a6ad | 2407 | } else { |
7ca75030 | 2408 | $joins['c' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'contacts', XDB::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5))); |
3f42a6ad FB |
2409 | } |
2410 | } | |
2411 | return $joins; | |
2412 | } | |
4e7bf1e0 FB |
2413 | |
2414 | ||
2415 | /** CARNET | |
2416 | */ | |
2417 | private $wn = array(); | |
2418 | public function addWatchRegistrationFilter($uid = null) | |
2419 | { | |
b8dcf62d | 2420 | $this->requireAccounts(); |
4e7bf1e0 FB |
2421 | return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid); |
2422 | } | |
2423 | ||
2424 | private $wp = array(); | |
2425 | public function addWatchPromoFilter($uid = null) | |
2426 | { | |
b8dcf62d | 2427 | $this->requireAccounts(); |
4e7bf1e0 FB |
2428 | return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid); |
2429 | } | |
2430 | ||
2431 | private $w = array(); | |
2432 | public function addWatchFilter($uid = null) | |
2433 | { | |
b8dcf62d | 2434 | $this->requireAccounts(); |
4e7bf1e0 FB |
2435 | return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid); |
2436 | } | |
2437 | ||
9b8e5fb4 | 2438 | protected function watchJoins() |
4e7bf1e0 FB |
2439 | { |
2440 | $joins = array(); | |
2441 | foreach ($this->w as $sub=>$key) { | |
2442 | if (is_null($key)) { | |
7ca75030 | 2443 | $joins['w' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch'); |
4e7bf1e0 | 2444 | } else { |
7ca75030 | 2445 | $joins['w' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch', XDB::format('$ME.uid = {?}', substr($key, 5))); |
4e7bf1e0 FB |
2446 | } |
2447 | } | |
2448 | foreach ($this->wn as $sub=>$key) { | |
2449 | if (is_null($key)) { | |
7ca75030 | 2450 | $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', '$ME.ni_id = $UID'); |
4e7bf1e0 | 2451 | } else { |
7ca75030 | 2452 | $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5))); |
4e7bf1e0 FB |
2453 | } |
2454 | } | |
2455 | foreach ($this->wn as $sub=>$key) { | |
2456 | if (is_null($key)) { | |
7ca75030 | 2457 | $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', '$ME.ni_id = $UID'); |
4e7bf1e0 | 2458 | } else { |
7ca75030 | 2459 | $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5))); |
4e7bf1e0 FB |
2460 | } |
2461 | } | |
2462 | foreach ($this->wp as $sub=>$key) { | |
2463 | if (is_null($key)) { | |
7ca75030 | 2464 | $joins['wp' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_promo'); |
4e7bf1e0 | 2465 | } else { |
7ca75030 | 2466 | $joins['wp' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_promo', XDB::format('$ME.uid = {?}', substr($key, 5))); |
4e7bf1e0 FB |
2467 | } |
2468 | } | |
2469 | return $joins; | |
2470 | } | |
a087cc8d | 2471 | } |
8363588b | 2472 | // }}} |
3f42a6ad | 2473 | |
a087cc8d FB |
2474 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
2475 | ?> |