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