Cannot configure mbox-helper and banana's spool root anymore.
[platal.git] / classes / userfilter.php
CommitLineData
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 37interface UserFilterCondition extends PlFilterCondition
a087cc8d 38{
a087cc8d 39}
8363588b 40// }}}
a087cc8d 41
77902bed 42// {{{ class UFC_HasProfile
53eae167
RB
43/** Filters users who have a profile
44 */
77902bed 45class UFC_HasProfile implements UserFilterCondition
eb1449b8 46{
dcc63ed5 47 public function buildCondition(PlFilter &$uf)
eb1449b8 48 {
aaf70eb8 49 $uf->requireProfiles();
93b0da52 50 return '$PID IS NOT NULL';
eb1449b8
FB
51 }
52}
8363588b 53// }}}
eb1449b8 54
55aa88ed
FB
55// {{{ class UFC_AccountType
56/** Filters users who have the given account types
57 */
58class UFC_AccountType implements UserFilterCondition
59{
60 private $types;
61
62 public function __construct()
63 {
64 $this->types = pl_flatten(func_get_args());
65 }
66
67 public function buildCondition(PlFilter &$uf)
68 {
69 $uf->requireAccounts();
70 return XDB::format('a.type IN {?}', $this->types);
71 }
72}
73
ddba9d4f
RB
74// {{{ class UFC_Hruid
75/** Filters users based on their hruid
76 * @param $val Either an hruid, or a list of those
77 */
78class UFC_Hruid implements UserFilterCondition
79{
80 private $hruids;
81
61d1fd8b 82 public function __construct()
ddba9d4f 83 {
61d1fd8b 84 $this->hruids = pl_flatten(func_get_args());
ddba9d4f
RB
85 }
86
87 public function buildCondition(PlFilter &$uf)
88 {
7752f73f 89 $uf->requireAccounts();
bde68f05 90 return XDB::format('a.hruid IN {?}', $this->hruids);
ddba9d4f
RB
91 }
92}
93// }}}
94
3e993f7a
FB
95// {{{ class UFC_Hrpid
96/** Filters users based on the hrpid of their profiles
97 * @param $val Either an hrpid, or a list of those
98 */
99class UFC_Hrpid implements UserFilterCondition
100{
101 private $hrpids;
102
61d1fd8b 103 public function __construct()
3e993f7a 104 {
61d1fd8b 105 $this->hrpids = pl_flatten(func_get_args());
3e993f7a
FB
106 }
107
108 public function buildCondition(PlFilter &$uf)
109 {
110 $uf->requireProfiles();
bde68f05 111 return XDB::format('p.hrpid IN {?}', $this->hrpids);
3e993f7a
FB
112 }
113}
114// }}}
115
f73a4f1b
RB
116// {{{ class UFC_Ip
117/** Filters users based on one of their last IPs
118 * @param $ip IP from which connection are checked
119 */
120class UFC_Ip implements UserFilterCondition
121{
122 private $ip;
123
124 public function __construct($ip)
125 {
126 $this->ip = $ip;
127 }
128
129 public function buildCondition(PlFilter &$uf)
130 {
131 $sub = $uf->addLoggerFilter();
132 $ip = ip_to_uint($this->ip);
133 return XDB::format($sub . '.ip = {?} OR ' . $sub . '.forward_ip = {?}', $ip, $ip);
134 }
135}
136// }}}
137
d7ddf29b
RB
138// {{{ class UFC_Comment
139class UFC_Comment implements UserFilterCondition
140{
141 private $text;
142
143 public function __construct($text)
144 {
145 $this->text = $text;
146 }
147
148 public function buildCondition(PlFilter &$uf)
149 {
150 $uf->requireProfiles();
7f26cd69 151 return $uf->getVisibilityCondition('p.freetext_pub') . ' AND p.freetext ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->text);
d7ddf29b
RB
152 }
153}
154// }}}
155
8363588b 156// {{{ class UFC_Promo
5d2e55c7 157/** Filters users based on promotion
53eae167
RB
158 * @param $comparison Comparison operator (>, =, ...)
159 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
5d2e55c7 160 * @param $promo Promotion on which the filter is based
53eae167 161 */
a087cc8d
FB
162class UFC_Promo implements UserFilterCondition
163{
a087cc8d
FB
164
165 private $grade;
166 private $promo;
167 private $comparison;
168
169 public function __construct($comparison, $grade, $promo)
170 {
171 $this->grade = $grade;
172 $this->comparison = $comparison;
173 $this->promo = $promo;
38c6fe96
FB
174 if ($this->grade != UserFilter::DISPLAY) {
175 UserFilter::assertGrade($this->grade);
176 }
6c1e97ae 177 if ($this->grade == UserFilter::DISPLAY && $this->comparison != '=') {
999db5aa
RB
178 // XXX: we might try to guess the grade from the first char of the promo and forbid only '<= 2004', but allow '<= X2004'
179 Platal::page()->killError("Il n'est pas possible d'appliquer la comparaison '" . $this->comparison . "' aux promotions sans spécifier de formation (X/M/D)");
6c1e97ae 180 }
a087cc8d
FB
181 }
182
dcc63ed5 183 public function buildCondition(PlFilter &$uf)
a087cc8d 184 {
38c6fe96
FB
185 if ($this->grade == UserFilter::DISPLAY) {
186 $sub = $uf->addDisplayFilter();
1a23a02b 187 return XDB::format('pd' . $sub . '.promo ' . $this->comparison . ' {?}', $this->promo);
38c6fe96
FB
188 } else {
189 $sub = $uf->addEducationFilter(true, $this->grade);
190 $field = 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
191 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison . ' ' . XDB::format('{?}', $this->promo);
192 }
784745ce
FB
193 }
194}
8363588b 195// }}}
784745ce 196
9e8bacfb
FB
197// {{{ class UFC_SchoolId
198/** Filters users based on their shoold identifier
199 * @param type Parameter type (Xorg, AX, School)
200 * @param value School id value
201 */
57f8776b 202class UFC_SchoolId implements UserFilterCondition
9e8bacfb
FB
203{
204 const AX = 'ax';
205 const Xorg = 'xorg';
206 const School = 'school';
207
208 private $type;
209 private $id;
210
211 static public function assertType($type)
212 {
213 if ($type != self::AX && $type != self::Xorg && $type != self::School) {
214 Platal::page()->killError("Type de matricule invalide: $type");
215 }
216 }
217
218 public function __construct($type, $id)
219 {
220 $this->type = $type;
221 $this->id = $id;
222 self::assertType($type);
223 }
224
225 public function buildCondition(PlFilter &$uf)
226 {
227 $uf->requireProfiles();
228 $id = $this->id;
229 $type = $this->type;
230 if ($type == self::School) {
231 $type = self::Xorg;
232 $id = Profile::getXorgId($id);
233 }
234 return XDB::format('p.' . $type . '_id = {?}', $id);
235 }
236}
237// }}}
238
fb3b6547 239// {{{ class UFC_EducationSchool
d7ddf29b
RB
240/** Filters users by formation
241 * @param $val The formation to search (either ID or array of IDs)
242 */
fb3b6547 243class UFC_EducationSchool implements UserFilterCondition
a7f8e48a 244{
d7ddf29b 245 private $val;
a7f8e48a 246
61d1fd8b 247 public function __construct()
a7f8e48a 248 {
61d1fd8b 249 $this->val = pl_flatten(func_get_args());
a7f8e48a
RB
250 }
251
252 public function buildCondition(PlFilter &$uf)
253 {
254 $sub = $uf->addEducationFilter();
bde68f05 255 return XDB::format('pe' . $sub . '.eduid IN {?}', $this->val);
a7f8e48a
RB
256 }
257}
258// }}}
259
fb3b6547
RB
260// {{{ class UFC_EducationDegree
261class UFC_EducationDegree implements UserFilterCondition
d7ddf29b
RB
262{
263 private $diploma;
264
61d1fd8b 265 public function __construct()
d7ddf29b 266 {
61d1fd8b 267 $this->diploma = pl_flatten(func_get_args());
d7ddf29b
RB
268 }
269
270 public function buildCondition(PlFilter &$uf)
271 {
272 $sub = $uf->addEducationFilter();
d115b6df 273 return XDB::format('pe' . $sub . '.degreeid IN {?}', $this->diploma);
d7ddf29b
RB
274 }
275}
276// }}}
277
fb3b6547
RB
278// {{{ class UFC_EducationField
279class UFC_EducationField implements UserFilterCondition
d7ddf29b
RB
280{
281 private $val;
282
61d1fd8b 283 public function __construct()
d7ddf29b 284 {
61d1fd8b 285 $this->val = pl_flatten(func_get_args());
d7ddf29b
RB
286 }
287
288 public function buildCondition(PlFilter &$uf)
289 {
290 $sub = $uf->addEducationFilter();
4290d54e 291 return XDB::format('pe' . $sub . '.fieldid IN {?}', $this->val);
d7ddf29b
RB
292 }
293}
294// }}}
295
8363588b 296// {{{ class UFC_Name
53eae167 297/** Filters users based on name
5d2e55c7 298 * @param $type Type of name field on which filtering is done (firstname, lastname...)
53eae167 299 * @param $text Text on which to filter
5d2e55c7 300 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
53eae167 301 */
784745ce
FB
302class UFC_Name implements UserFilterCondition
303{
706f7a0f
RB
304 const EXACT = XDB::WILDCARD_EXACT; // 0x000
305 const PREFIX = XDB::WILDCARD_PREFIX; // 0x001
306 const SUFFIX = XDB::WILDCARD_SUFFIX; // 0x002
658b4c83 307 const CONTAINS = XDB::WILDCARD_CONTAINS; // 0x003
5bb60f74 308 const PARTICLE = 0x004;
658b4c83 309 const VARIANTS = 0x008;
784745ce
FB
310
311 private $type;
312 private $text;
313 private $mode;
314
315 public function __construct($type, $text, $mode)
316 {
317 $this->type = $type;
318 $this->text = $text;
319 $this->mode = $mode;
320 }
321
322 private function buildNameQuery($type, $variant, $where, UserFilter &$uf)
323 {
324 $sub = $uf->addNameFilter($type, $variant);
325 return str_replace('$ME', 'pn' . $sub, $where);
326 }
327
dcc63ed5 328 public function buildCondition(PlFilter &$uf)
784745ce
FB
329 {
330 $left = '$ME.name';
784745ce
FB
331 if (($this->mode & self::PARTICLE) == self::PARTICLE) {
332 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
333 }
658b4c83
RB
334 $right = XDB::formatWildcards($this->mode & self::CONTAINS, $this->text);
335
336 $cond = $left . $right;
784745ce 337 $conds = array($this->buildNameQuery($this->type, null, $cond, $uf));
913a4e90
RB
338 if (($this->mode & self::VARIANTS) != 0 && isset(Profile::$name_variants[$this->type])) {
339 foreach (Profile::$name_variants[$this->type] as $var) {
784745ce
FB
340 $conds[] = $this->buildNameQuery($this->type, $var, $cond, $uf);
341 }
342 }
343 return implode(' OR ', $conds);
a087cc8d
FB
344 }
345}
8363588b 346// }}}
a087cc8d 347
40585144
RB
348// {{{ class UFC_NameTokens
349/** Selects users based on tokens in their name (for quicksearch)
350 * @param $tokens An array of tokens to search
351 * @param $flags Flags the tokens must have (e.g 'public' for public search)
352 * @param $soundex (bool) Whether those tokens are fulltext or soundex
353 */
354class UFC_NameTokens implements UserFilterCondition
355{
356 /* Flags */
357 const FLAG_PUBLIC = 'public';
358
359 private $tokens;
360 private $flags;
361 private $soundex;
79a0b464 362 private $exact;
40585144 363
79a0b464 364 public function __construct($tokens, $flags = array(), $soundex = false, $exact = false)
40585144 365 {
a867dfba
RB
366 if (is_array($tokens)) {
367 $this->tokens = $tokens;
368 } else {
369 $this->tokens = array($tokens);
370 }
40585144
RB
371 if (is_array($flags)) {
372 $this->flags = $flags;
373 } else {
374 $this->flags = array($flags);
375 }
376 $this->soundex = $soundex;
79a0b464 377 $this->exact = $exact;
40585144
RB
378 }
379
dcc63ed5 380 public function buildCondition(PlFilter &$uf)
40585144 381 {
40585144 382 $conds = array();
2a93b634
RB
383 foreach ($this->tokens as $i => $token) {
384 $sub = $uf->addNameTokensFilter($token);
385 if ($this->soundex) {
386 $c = XDB::format($sub . '.soundex = {?}', $token);
387 } else if ($this->exact) {
388 $c = XDB::format($sub . '.token = {?}', $token);
389 } else {
390 $c = $sub . '.token ' . XDB::formatWildcards(XDB::WILDCARD_PREFIX, $token);
40585144 391 }
2a93b634
RB
392 if ($this->flags != null) {
393 $c .= XDB::format(' AND ' . $sub . '.flags IN {?}', $this->flags);
394 }
395 $conds[] = $c;
40585144
RB
396 }
397
398 return implode(' AND ', $conds);
399 }
400}
401// }}}
402
0fb3713c
RB
403// {{{ class UFC_Nationality
404class UFC_Nationality implements UserFilterCondition
405{
d7ddf29b 406 private $val;
0fb3713c 407
61d1fd8b 408 public function __construct()
0fb3713c 409 {
61d1fd8b 410 $this->val = pl_flatten(func_get_args());
0fb3713c
RB
411 }
412
413 public function buildCondition(PlFilter &$uf)
414 {
d7ddf29b
RB
415 $uf->requireProfiles();
416 $nat = XDB::formatArray($this->val);
417 $conds = array(
418 'p.nationality1 IN ' . $nat,
419 'p.nationality2 IN ' . $nat,
420 'p.nationality3 IN ' . $nat,
421 );
422 return implode(' OR ', $conds);
0fb3713c
RB
423 }
424}
425// }}}
426
8363588b 427// {{{ class UFC_Dead
53eae167
RB
428/** Filters users based on death date
429 * @param $comparison Comparison operator
8fcb783a 430 * @param $date Date to which death date should be compared (DateTime object, string or timestamp)
53eae167 431 */
4927ee54
FB
432class UFC_Dead implements UserFilterCondition
433{
38c6fe96
FB
434 private $comparison;
435 private $date;
436
437 public function __construct($comparison = null, $date = null)
4927ee54 438 {
38c6fe96 439 $this->comparison = $comparison;
a0d47f5c 440 $this->date = make_datetime($date);
4927ee54
FB
441 }
442
dcc63ed5 443 public function buildCondition(PlFilter &$uf)
4927ee54 444 {
d7ddf29b 445 $uf->requireProfiles();
38c6fe96
FB
446 $str = 'p.deathdate IS NOT NULL';
447 if (!is_null($this->comparison)) {
8fcb783a 448 $str .= ' AND p.deathdate ' . $this->comparison . ' ' . XDB::format('{?}', $this->date->format('Y-m-d'));
4927ee54 449 }
38c6fe96 450 return $str;
4927ee54
FB
451 }
452}
8363588b 453// }}}
4927ee54 454
8363588b 455// {{{ class UFC_Registered
53eae167
RB
456/** Filters users based on registration state
457 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
458 * @param $comparison Comparison operator
459 * @param $date Date to which users registration date should be compared
460 */
4927ee54
FB
461class UFC_Registered implements UserFilterCondition
462{
463 private $active;
38c6fe96
FB
464 private $comparison;
465 private $date;
466
467 public function __construct($active = false, $comparison = null, $date = null)
4927ee54 468 {
b2e8fc54 469 $this->active = $active;
38c6fe96 470 $this->comparison = $comparison;
a0d47f5c 471 $this->date = make_datetime($date);
4927ee54
FB
472 }
473
dcc63ed5 474 public function buildCondition(PlFilter &$uf)
4927ee54 475 {
d7ddf29b 476 $uf->requireAccounts();
4927ee54 477 if ($this->active) {
93b0da52 478 $date = '$UID IS NOT NULL AND a.state = \'active\'';
4927ee54 479 } else {
93b0da52 480 $date = '$UID IS NOT NULL AND a.state != \'pending\'';
38c6fe96
FB
481 }
482 if (!is_null($this->comparison)) {
c9c74e4f 483 $date .= ' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date ' . $this->comparison . ' ' . XDB::format('{?}', $this->date->format('Y-m-d'));
4927ee54 484 }
38c6fe96 485 return $date;
4927ee54
FB
486 }
487}
8363588b 488// }}}
4927ee54 489
8363588b 490// {{{ class UFC_ProfileUpdated
53eae167
RB
491/** Filters users based on profile update date
492 * @param $comparison Comparison operator
493 * @param $date Date to which profile update date must be compared
494 */
7e735012
FB
495class UFC_ProfileUpdated implements UserFilterCondition
496{
497 private $comparison;
498 private $date;
499
500 public function __construct($comparison = null, $date = null)
501 {
502 $this->comparison = $comparison;
503 $this->date = $date;
504 }
505
dcc63ed5 506 public function buildCondition(PlFilter &$uf)
7e735012 507 {
d7ddf29b 508 $uf->requireProfiles();
7e735012
FB
509 return 'p.last_change ' . $this->comparison . XDB::format(' {?}', date('Y-m-d H:i:s', $this->date));
510 }
511}
8363588b 512// }}}
7e735012 513
8363588b 514// {{{ class UFC_Birthday
53eae167
RB
515/** Filters users based on next birthday date
516 * @param $comparison Comparison operator
517 * @param $date Date to which users next birthday date should be compared
518 */
7e735012
FB
519class UFC_Birthday implements UserFilterCondition
520{
521 private $comparison;
522 private $date;
523
524 public function __construct($comparison = null, $date = null)
525 {
526 $this->comparison = $comparison;
527 $this->date = $date;
528 }
529
dcc63ed5 530 public function buildCondition(PlFilter &$uf)
7e735012 531 {
d7ddf29b 532 $uf->requireProfiles();
7e735012
FB
533 return 'p.next_birthday ' . $this->comparison . XDB::format(' {?}', date('Y-m-d', $this->date));
534 }
535}
8363588b 536// }}}
7e735012 537
8363588b 538// {{{ class UFC_Sex
53eae167
RB
539/** Filters users based on sex
540 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
541 */
4927ee54
FB
542class UFC_Sex implements UserFilterCondition
543{
544 private $sex;
545 public function __construct($sex)
546 {
547 $this->sex = $sex;
548 }
549
dcc63ed5 550 public function buildCondition(PlFilter &$uf)
4927ee54
FB
551 {
552 if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) {
553 return self::COND_FALSE;
554 } else {
d7ddf29b 555 $uf->requireProfiles();
24e08e33 556 return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male');
4927ee54
FB
557 }
558 }
559}
8363588b 560// }}}
4927ee54 561
8363588b 562// {{{ class UFC_Group
53eae167 563/** Filters users based on group membership
5d2e55c7
RB
564 * @param $group Group whose members we are selecting
565 * @param $anim Whether to restrict selection to animators of that group
53eae167 566 */
4927ee54
FB
567class UFC_Group implements UserFilterCondition
568{
569 private $group;
5d2e55c7
RB
570 private $anim;
571 public function __construct($group, $anim = false)
4927ee54
FB
572 {
573 $this->group = $group;
5d2e55c7 574 $this->anim = $anim;
4927ee54
FB
575 }
576
dcc63ed5 577 public function buildCondition(PlFilter &$uf)
4927ee54 578 {
7f26cd69
RB
579 // Groups have AX visibility.
580 if ($uf->getVisibilityLevel() == ProfileVisibility::VIS_PUBLIC) {
9f169a65 581 return PlFilter::COND_TRUE;
7f26cd69 582 }
4927ee54
FB
583 $sub = $uf->addGroupFilter($this->group);
584 $where = 'gpm' . $sub . '.perms IS NOT NULL';
5d2e55c7 585 if ($this->anim) {
4927ee54
FB
586 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
587 }
588 return $where;
589 }
590}
8363588b 591// }}}
4927ee54 592
0fb3713c 593// {{{ class UFC_Binet
d7ddf29b
RB
594/** Selects users based on their belonging to a given (list of) binet
595 * @param $binet either a binet_id or an array of binet_ids
596 */
0fb3713c
RB
597class UFC_Binet implements UserFilterCondition
598{
d7ddf29b 599 private $val;
0fb3713c 600
61d1fd8b 601 public function __construct()
0fb3713c 602 {
61d1fd8b 603 $this->val = pl_flatten(func_get_args());
0fb3713c
RB
604 }
605
606 public function buildCondition(PlFilter &$uf)
607 {
7f26cd69
RB
608 // Binets are private.
609 if ($uf->getVisibilityLevel() != ProfileVisibility::VIS_PRIVATE) {
9f169a65 610 return PlFilter::COND_TRUE;
7f26cd69 611 }
d7ddf29b 612 $sub = $uf->addBinetsFilter();
bde68f05 613 return XDB::format($sub . '.binet_id IN {?}', $this->val);
a7f8e48a
RB
614 }
615}
616// }}}
617
618// {{{ class UFC_Section
46e88fe3
RB
619/** Selects users based on section
620 * @param $section ID of the section
621 */
a7f8e48a
RB
622class UFC_Section implements UserFilterCondition
623{
624 private $section;
625
61d1fd8b 626 public function __construct()
a7f8e48a 627 {
61d1fd8b 628 $this->section = pl_flatten(func_get_args());
a7f8e48a
RB
629 }
630
631 public function buildCondition(PlFilter &$uf)
632 {
7f26cd69
RB
633 // Sections are private.
634 if ($uf->getVisibilityLevel() != ProfileVisibility::VIS_PRIVATE) {
9f169a65 635 return PlFilter::COND_TRUE;
7f26cd69 636 }
a7f8e48a 637 $uf->requireProfiles();
61d1fd8b 638 return XDB::format('p.section IN {?}', $this->section);
0fb3713c
RB
639 }
640}
641// }}}
642
8363588b 643// {{{ class UFC_Email
2d6329a2 644/** Filters users based on an email or a list of emails
53eae167
RB
645 * @param $emails List of emails whose owner must be selected
646 */
2d6329a2 647class UFC_Email implements UserFilterCondition
21401768
FB
648{
649 private $emails;
2d6329a2 650 public function __construct()
21401768 651 {
61d1fd8b 652 $this->emails = pl_flatten(func_get_args());
21401768
FB
653 }
654
dcc63ed5 655 public function buildCondition(PlFilter &$uf)
21401768 656 {
2d6329a2
FB
657 $foreign = array();
658 $virtual = array();
659 $aliases = array();
21401768
FB
660 $cond = array();
661
662 if (count($this->emails) == 0) {
dcc63ed5 663 return PlFilterCondition::COND_TRUE;
21401768
FB
664 }
665
666 foreach ($this->emails as $entry) {
667 if (User::isForeignEmailAddress($entry)) {
2d6329a2 668 $foreign[] = $entry;
21401768 669 } else if (User::isVirtualEmailAddress($entry)) {
2d6329a2 670 $virtual[] = $entry;
21401768 671 } else {
21401768 672 @list($user, $domain) = explode('@', $entry);
2d6329a2 673 $aliases[] = $user;
21401768
FB
674 }
675 }
2d6329a2
FB
676
677 if (count($foreign) > 0) {
678 $sub = $uf->addEmailRedirectFilter($foreign);
bde68f05 679 $cond[] = XDB::format('e' . $sub . '.email IS NOT NULL OR a.email IN {?}', $foreign);
2d6329a2
FB
680 }
681 if (count($virtual) > 0) {
682 $sub = $uf->addVirtualEmailFilter($virtual);
683 $cond[] = 'vr' . $sub . '.redirect IS NOT NULL';
684 }
685 if (count($aliases) > 0) {
686 $sub = $uf->addAliasFilter($aliases);
687 $cond[] = 'al' . $sub . '.alias IS NOT NULL';
688 }
21401768
FB
689 return '(' . implode(') OR (', $cond) . ')';
690 }
691}
8363588b 692// }}}
d865c296 693
8363588b 694// {{{ class UFC_Address
2b9ca54d 695abstract class UFC_Address implements UserFilterCondition
c4b24511 696{
2b9ca54d 697 /** Valid address type ('hq' is reserved for company addresses)
036d1637 698 */
2b9ca54d
RB
699 const TYPE_HOME = 1;
700 const TYPE_PRO = 2;
701 const TYPE_ANY = 3;
c4b24511 702
2b9ca54d 703 /** Text for these types
036d1637 704 */
2b9ca54d
RB
705 protected static $typetexts = array(
706 self::TYPE_HOME => 'home',
707 self::TYPE_PRO => 'pro',
708 );
709
710 protected $type;
c4b24511 711
036d1637
RB
712 /** Flags for addresses
713 */
714 const FLAG_CURRENT = 0x0001;
715 const FLAG_TEMP = 0x0002;
716 const FLAG_SECOND = 0x0004;
717 const FLAG_MAIL = 0x0008;
718 const FLAG_CEDEX = 0x0010;
719
720 // Binary OR of those flags
721 const FLAG_ANY = 0x001F;
722
723 /** Text of these flags
724 */
2b9ca54d 725 protected static $flagtexts = array(
036d1637
RB
726 self::FLAG_CURRENT => 'current',
727 self::FLAG_TEMP => 'temporary',
728 self::FLAG_SECOND => 'secondary',
729 self::FLAG_MAIL => 'mail',
730 self::FLAG_CEDEX => 'cedex',
731 );
732
2b9ca54d
RB
733 protected $flags;
734
735 public function __construct($type = null, $flags = null)
736 {
737 $this->type = $type;
738 $this->flags = $flags;
739 }
740
7f26cd69 741 protected function initConds($sub, $vis_cond)
2b9ca54d 742 {
7f26cd69
RB
743 $conds = array($vis_cond);
744
2b9ca54d
RB
745 $types = array();
746 foreach (self::$typetexts as $flag => $type) {
747 if ($flag & $this->type) {
748 $types[] = $type;
749 }
750 }
751 if (count($types)) {
6b140032 752 $conds[] = XDB::format($sub . '.type IN {?}', $types);
2b9ca54d
RB
753 }
754
755 if ($this->flags != self::FLAG_ANY) {
756 foreach(self::$flagtexts as $flag => $text) {
757 if ($flag & $this->flags) {
758 $conds[] = 'FIND_IN_SET(' . XDB::format('{?}', $text) . ', ' . $sub . '.flags)';
759 }
760 }
761 }
762 return $conds;
763 }
764
765}
766// }}}
767
768// {{{ class UFC_AddressText
769/** Select users based on their address, using full text search
770 * @param $text Text for filter in fulltext search
658b4c83 771 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
2b9ca54d
RB
772 * @param $type Filter on address type
773 * @param $flags Filter on address flags
774 * @param $country Filter on address country
775 * @param $locality Filter on address locality
776 */
777class UFC_AddressText extends UFC_Address
778{
2b9ca54d 779
036d1637 780 private $text;
2b9ca54d
RB
781 private $textSearchMode;
782
658b4c83 783 public function __construct($text = null, $textSearchMode = XDB::WILDCARD_CONTAINS,
2b9ca54d
RB
784 $type = null, $flags = self::FLAG_ANY, $country = null, $locality = null)
785 {
786 parent::__construct($type, $flags);
787 $this->text = $text;
788 $this->textSearchMode = $textSearchMode;
789 $this->country = $country;
790 $this->locality = $locality;
791 }
792
793 private function mkMatch($txt)
794 {
658b4c83 795 return XDB::formatWildcards($this->textSearchMode, $txt);
2b9ca54d
RB
796 }
797
798 public function buildCondition(PlFilter &$uf)
799 {
800 $sub = $uf->addAddressFilter();
7f26cd69 801 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
2b9ca54d
RB
802 if ($this->text != null) {
803 $conds[] = $sub . '.text' . $this->mkMatch($this->text);
804 }
805
806 if ($this->country != null) {
807 $subc = $uf->addAddressCountryFilter();
808 $subconds = array();
809 $subconds[] = $subc . '.country' . $this->mkMatch($this->country);
810 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country);
811 $conds[] = implode(' OR ', $subconds);
812 }
813
814 if ($this->locality != null) {
815 $subl = $uf->addAddressLocalityFilter();
816 $conds[] = $subl . '.name' . $this->mkMatch($this->locality);
817 }
818
819 return implode(' AND ', $conds);
820 }
821}
822// }}}
823
46e88fe3 824// {{{ class UFC_AddressField
2b9ca54d 825/** Filters users based on their address,
46e88fe3
RB
826 * @param $val Either a code for one of the fields, or an array of such codes
827 * @param $fieldtype The type of field to look for
2b9ca54d
RB
828 * @param $type Filter on address type
829 * @param $flags Filter on address flags
2b9ca54d 830 */
46e88fe3 831class UFC_AddressField extends UFC_Address
2b9ca54d 832{
46e88fe3
RB
833 const FIELD_COUNTRY = 1;
834 const FIELD_ADMAREA = 2;
835 const FIELD_SUBADMAREA = 3;
836 const FIELD_LOCALITY = 4;
837 const FIELD_ZIPCODE = 5;
838
2b9ca54d
RB
839 /** Data of the filter
840 */
46e88fe3
RB
841 private $val;
842 private $fieldtype;
036d1637 843
46e88fe3 844 public function __construct($val, $fieldtype, $type = null, $flags = self::FLAG_ANY)
036d1637 845 {
2b9ca54d 846 parent::__construct($type, $flags);
46e88fe3
RB
847
848 if (!is_array($val)) {
849 $val = array($val);
850 }
851 $this->val = $val;
852 $this->fieldtype = $fieldtype;
c4b24511
RB
853 }
854
dcc63ed5 855 public function buildCondition(PlFilter &$uf)
c4b24511 856 {
036d1637 857 $sub = $uf->addAddressFilter();
7f26cd69 858 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
036d1637 859
46e88fe3
RB
860 switch ($this->fieldtype) {
861 case self::FIELD_COUNTRY:
862 $field = 'countryId';
863 break;
864 case self::FIELD_ADMAREA:
865 $field = 'administrativeAreaId';
866 break;
867 case self::FIELD_SUBADMAREA:
868 $field = 'subAdministrativeAreaId';
869 break;
870 case self::FIELD_LOCALITY:
871 $field = 'localityId';
872 break;
873 case self::FIELD_ZIPCODE:
874 $field = 'postalCode';
875 break;
876 default:
61f61261 877 Platal::page()->killError('Invalid address field type: ' . $this->fieldtype);
46e88fe3 878 }
bde68f05 879 $conds[] = XDB::format($sub . '.' . $field . ' IN {?}', $this->val);
036d1637
RB
880
881 return implode(' AND ', $conds);
c4b24511
RB
882 }
883}
8363588b 884// }}}
c4b24511 885
8363588b 886// {{{ class UFC_Corps
4083b126
RB
887/** Filters users based on the corps they belong to
888 * @param $corps Corps we are looking for (abbreviation)
889 * @param $type Whether we search for original or current corps
890 */
891class UFC_Corps implements UserFilterCondition
892{
5d2e55c7
RB
893 const CURRENT = 1;
894 const ORIGIN = 2;
4083b126
RB
895
896 private $corps;
897 private $type;
898
899 public function __construct($corps, $type = self::CURRENT)
900 {
901 $this->corps = $corps;
902 $this->type = $type;
903 }
904
dcc63ed5 905 public function buildCondition(PlFilter &$uf)
4083b126 906 {
5d2e55c7
RB
907 /** Tables shortcuts:
908 * pc for profile_corps,
4083b126
RB
909 * pceo for profile_corps_enum - orginal
910 * pcec for profile_corps_enum - current
911 */
912 $sub = $uf->addCorpsFilter($this->type);
913 $cond = $sub . '.abbreviation = ' . $corps;
7f26cd69 914 $cond .= ' AND ' . $uf->getVisibilityCondition($sub . '.corps_pub');
4083b126
RB
915 return $cond;
916 }
917}
8363588b 918// }}}
4083b126 919
8363588b 920// {{{ class UFC_Corps_Rank
4083b126
RB
921/** Filters users based on their rank in the corps
922 * @param $rank Rank we are looking for (abbreviation)
923 */
924class UFC_Corps_Rank implements UserFilterCondition
925{
926 private $rank;
927 public function __construct($rank)
928 {
929 $this->rank = $rank;
930 }
931
dcc63ed5 932 public function buildCondition(PlFilter &$uf)
4083b126 933 {
5d2e55c7 934 /** Tables shortcuts:
7f26cd69 935 * pc for profile_corps
4083b126
RB
936 * pcr for profile_corps_rank
937 */
938 $sub = $uf->addCorpsRankFilter();
939 $cond = $sub . '.abbreviation = ' . $rank;
7f26cd69
RB
940 // XXX(x2006barrois): find a way to get rid of that hardcoded
941 // reference to 'pc'.
942 $cond .= ' AND ' . $uf->getVisibilityCondition('pc.corps_pub');
4083b126
RB
943 return $cond;
944 }
945}
8363588b 946// }}}
4083b126 947
6a99c3ac
RB
948// {{{ class UFC_Job_Company
949/** Filters users based on the company they belong to
950 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
951 * @param $value The searched value
952 */
4e2f2ad2 953class UFC_Job_Company implements UserFilterCondition
6a99c3ac
RB
954{
955 const JOBID = 'id';
956 const JOBNAME = 'name';
957 const JOBACRONYM = 'acronym';
958
959 private $type;
960 private $value;
961
962 public function __construct($type, $value)
963 {
964 $this->assertType($type);
965 $this->type = $type;
966 $this->value = $value;
967 }
968
969 private function assertType($type)
970 {
971 if ($type != self::JOBID && $type != self::JOBNAME && $type != self::JOBACRONYM) {
5d2e55c7 972 Platal::page()->killError("Type de recherche non valide.");
6a99c3ac
RB
973 }
974 }
975
dcc63ed5 976 public function buildCondition(PlFilter &$uf)
6a99c3ac
RB
977 {
978 $sub = $uf->addJobCompanyFilter();
412e2e0f 979 $cond = $sub . '.' . $this->type . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value);
7f26cd69
RB
980 $jsub = $uf->addJobFilter();
981 $cond .= ' AND ' . $uf->getVisibilityCondition($jsub . '.pub');
6a99c3ac
RB
982 return $cond;
983 }
984}
985// }}}
986
987// {{{ class UFC_Job_Sectorization
988/** Filters users based on the ((sub)sub)sector they work in
46e88fe3 989 * @param $val The ID of the sector, or an array of such IDs
d7ddf29b 990 * @param $type The kind of search (subsubsector/subsector/sector)
6a99c3ac 991 */
4e2f2ad2 992class UFC_Job_Sectorization implements UserFilterCondition
6a99c3ac 993{
d7ddf29b
RB
994 private $val;
995 private $type;
6a99c3ac 996
d7ddf29b
RB
997 public function __construct($val, $type = UserFilter::JOB_SECTOR)
998 {
999 self::assertType($type);
46e88fe3
RB
1000 if (!is_array($val)) {
1001 $val = array($val);
1002 }
d7ddf29b
RB
1003 $this->val = $val;
1004 $this->type = $type;
1005 }
6a99c3ac 1006
d7ddf29b 1007 private static function assertType($type)
6a99c3ac 1008 {
d7ddf29b
RB
1009 if ($type != UserFilter::JOB_SECTOR && $type != UserFilter::JOB_SUBSECTOR && $type != UserFilter::JOB_SUBSUBSECTOR) {
1010 Platal::page()->killError("Type de secteur non valide.");
1011 }
6a99c3ac
RB
1012 }
1013
dcc63ed5 1014 public function buildCondition(PlFilter &$uf)
6a99c3ac 1015 {
d7ddf29b 1016 $sub = $uf->addJobSectorizationFilter($this->type);
7f26cd69
RB
1017 $cond = $sub . '.id = ' . XDB::format('{?}', $this->val);
1018 $jsub = $uf->addJobFilter();
1019 $cond .= ' AND ' . $uf->getVisibilityCondition($jsub . '.pub');
1020 return $cond;
6a99c3ac
RB
1021 }
1022}
1023// }}}
1024
3ac45f10
PC
1025// {{{ class UFC_Job_Terms
1026/** Filters users based on the job terms they assigned to one of their
1027 * jobs.
1028 * @param $val The ID of the job term, or an array of such IDs
1029 */
1030class UFC_Job_Terms implements UserFilterCondition
1031{
1032 private $val;
1033
1034 public function __construct($val)
1035 {
1036 if (!is_array($val)) {
1037 $val = array($val);
1038 }
1039 $this->val = $val;
1040 }
1041
1042 public function buildCondition(PlFilter &$uf)
1043 {
1044 $sub = $uf->addJobTermsFilter(count($this->val));
1045 $conditions = array();
1046 foreach ($this->val as $i => $jtid) {
4ec03752 1047 $conditions[] = $sub[$i] . '.jtid_1 = ' . XDB::escape($jtid);
3ac45f10 1048 }
7f26cd69
RB
1049 $jsub = $uf->addJobFilter();
1050 $conditions[] = $uf->getVisibilityCondition($jsub . '.pub');
3ac45f10
PC
1051 return implode(' AND ', $conditions);
1052 }
1053}
1054// }}}
1055
6a99c3ac
RB
1056// {{{ class UFC_Job_Description
1057/** Filters users based on their job description
1058 * @param $description The text being searched for
1059 * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
1060 */
4e2f2ad2 1061class UFC_Job_Description implements UserFilterCondition
6a99c3ac
RB
1062{
1063
6a99c3ac
RB
1064 private $description;
1065 private $fields;
1066
01cc5f9e 1067 public function __construct($description, $fields)
6a99c3ac
RB
1068 {
1069 $this->fields = $fields;
1070 $this->description = $description;
1071 }
1072
dcc63ed5 1073 public function buildCondition(PlFilter &$uf)
6a99c3ac
RB
1074 {
1075 $conds = array();
7f26cd69
RB
1076
1077 $jsub = $uf->addJobFilter();
1078 // CV is private => if only CV requested, and not private,
1079 // don't do anything. Otherwise restrict to standard job visibility.
9f169a65
RB
1080 if ($this->fields == UserFilter::JOB_CV) {
1081 if ($uf->getVisibilityLevel() != ProfileVisibility::VIS_PRIVATE) {
1082 return PlFilter::COND_TRUE;
1083 }
1084 } else {
7f26cd69
RB
1085 $conds[] = $uf->getVisibilityCondition($jsub . '.pub');
1086 }
1087
6a99c3ac 1088 if ($this->fields & UserFilter::JOB_USERDEFINED) {
7f26cd69 1089 $conds[] = $jsub . '.description ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
6a99c3ac 1090 }
7f26cd69 1091 if ($this->fields & UserFilter::JOB_CV && $uf->getVisibilityLevel == ProfileVisibility::VIS_PRIVATE) {
01cc5f9e 1092 $uf->requireProfiles();
658b4c83 1093 $conds[] = 'p.cv ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
01cc5f9e 1094 }
6a99c3ac
RB
1095 if ($this->fields & UserFilter::JOB_SECTOR) {
1096 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SECTOR);
658b4c83 1097 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
6a99c3ac
RB
1098 }
1099 if ($this->fields & UserFilter::JOB_SUBSECTOR) {
1100 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSECTOR);
658b4c83 1101 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
6a99c3ac
RB
1102 }
1103 if ($this->fields & UserFilter::JOB_SUBSUBSECTOR) {
1104 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSUBSECTOR);
658b4c83 1105 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
6a99c3ac 1106 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_ALTERNATES);
658b4c83 1107 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
6a99c3ac
RB
1108 }
1109 return implode(' OR ', $conds);
1110 }
1111}
1112// }}}
1113
0a2e9c74
RB
1114// {{{ class UFC_Networking
1115/** Filters users based on network identity (IRC, ...)
1116 * @param $type Type of network (-1 for any)
1117 * @param $value Value to search
1118 */
4e2f2ad2 1119class UFC_Networking implements UserFilterCondition
0a2e9c74
RB
1120{
1121 private $type;
1122 private $value;
1123
1124 public function __construct($type, $value)
1125 {
1126 $this->type = $type;
1127 $this->value = $value;
1128 }
1129
dcc63ed5 1130 public function buildCondition(PlFilter &$uf)
0a2e9c74
RB
1131 {
1132 $sub = $uf->addNetworkingFilter();
1133 $conds = array();
7f26cd69 1134 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
658b4c83 1135 $conds[] = $sub . '.address ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value);
0a2e9c74 1136 if ($this->type != -1) {
1f5cd004 1137 $conds[] = $sub . '.nwid = ' . XDB::format('{?}', $this->type);
0a2e9c74
RB
1138 }
1139 return implode(' AND ', $conds);
1140 }
1141}
1142// }}}
1143
6d62969e
RB
1144// {{{ class UFC_Phone
1145/** Filters users based on their phone number
1146 * @param $num_type Type of number (pro/user/home)
1147 * @param $phone_type Type of phone (fixed/mobile/fax)
1148 * @param $number Phone number
1149 */
4e2f2ad2 1150class UFC_Phone implements UserFilterCondition
6d62969e
RB
1151{
1152 const NUM_PRO = 'pro';
1153 const NUM_USER = 'user';
1154 const NUM_HOME = 'address';
1155 const NUM_ANY = 'any';
1156
1157 const PHONE_FIXED = 'fixed';
1158 const PHONE_MOBILE = 'mobile';
1159 const PHONE_FAX = 'fax';
1160 const PHONE_ANY = 'any';
1161
1162 private $num_type;
1163 private $phone_type;
1164 private $number;
1165
1166 public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY)
1167 {
da0a9c5a 1168 $phone = new Phone(array('display' => $number));
0b6c8b36
SJ
1169 $phone->format();
1170 $this->number = $phone->search();
6d62969e 1171 $this->num_type = $num_type;
0b6c8b36 1172 $this->phone_type = $phone_type;
6d62969e
RB
1173 }
1174
dcc63ed5 1175 public function buildCondition(PlFilter &$uf)
6d62969e
RB
1176 {
1177 $sub = $uf->addPhoneFilter();
1178 $conds = array();
7f26cd69
RB
1179
1180 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1181
6d62969e
RB
1182 $conds[] = $sub . '.search_tel = ' . XDB::format('{?}', $this->number);
1183 if ($this->num_type != self::NUM_ANY) {
1184 $conds[] = $sub . '.link_type = ' . XDB::format('{?}', $this->num_type);
1185 }
1186 if ($this->phone_type != self::PHONE_ANY) {
1187 $conds[] = $sub . '.tel_type = ' . XDB::format('{?}', $this->phone_type);
1188 }
1189 return implode(' AND ', $conds);
1190 }
1191}
1192// }}}
1193
ceb512d2
RB
1194// {{{ class UFC_Medal
1195/** Filters users based on their medals
1196 * @param $medal ID of the medal
1197 * @param $grade Grade of the medal (null for 'any')
1198 */
4e2f2ad2 1199class UFC_Medal implements UserFilterCondition
ceb512d2
RB
1200{
1201 private $medal;
1202 private $grade;
1203
1204 public function __construct($medal, $grade = null)
1205 {
1206 $this->medal = $medal;
1207 $this->grade = $grade;
1208 }
1209
dcc63ed5 1210 public function buildCondition(PlFilter &$uf)
ceb512d2
RB
1211 {
1212 $conds = array();
7f26cd69
RB
1213
1214 // This will require profiles => table 'p' will be available.
ceb512d2 1215 $sub = $uf->addMedalFilter();
7f26cd69
RB
1216
1217 $conds[] = $uf->getVisibilityCondition('p.medals_pub');
1218
ceb512d2
RB
1219 $conds[] = $sub . '.mid = ' . XDB::format('{?}', $this->medal);
1220 if ($this->grade != null) {
1221 $conds[] = $sub . '.gid = ' . XDB::format('{?}', $this->grade);
1222 }
1223 return implode(' AND ', $conds);
1224 }
1225}
1226// }}}
1227
470d14f6
FB
1228// {{{ class UFC_Photo
1229/** Filters profiles with photo
1230 */
1231class UFC_Photo implements UserFilterCondition
1232{
1233 public function buildCondition(PlFilter &$uf)
1234 {
7f26cd69
RB
1235 $sub = $uf->addPhotoFilter();
1236 return $sub . '.attach IS NOT NULL AND ' . $uf->getVisibilityCondition($sub . '.pub');
470d14f6
FB
1237 }
1238}
1239// }}}
1240
96f01fba
RB
1241// {{{ class UFC_Mentor
1242class UFC_Mentor implements UserFilterCondition
1243{
1244 public function buildCondition(PlFilter &$uf)
1245 {
1246 $sub = $uf->addMentorFilter(UserFilter::MENTOR);
1247 return $sub . '.expertise IS NOT NULL';
1248 }
1249}
1250// }}}
1251
1252
671b7073
RB
1253// {{{ class UFC_Mentor_Expertise
1254/** Filters users by mentoring expertise
1255 * @param $expertise Domain of expertise
1256 */
4e2f2ad2 1257class UFC_Mentor_Expertise implements UserFilterCondition
671b7073
RB
1258{
1259 private $expertise;
1260
1261 public function __construct($expertise)
1262 {
1263 $this->expertise = $expertise;
1264 }
1265
dcc63ed5 1266 public function buildCondition(PlFilter &$uf)
671b7073
RB
1267 {
1268 $sub = $uf->addMentorFilter(UserFilter::MENTOR_EXPERTISE);
658b4c83 1269 return $sub . '.expertise ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->expertise);
671b7073
RB
1270 }
1271}
1272// }}}
1273
1274// {{{ class UFC_Mentor_Country
1275/** Filters users by mentoring country
1276 * @param $country Two-letters code of country being searched
1277 */
4e2f2ad2 1278class UFC_Mentor_Country implements UserFilterCondition
671b7073
RB
1279{
1280 private $country;
1281
61d1fd8b 1282 public function __construct()
671b7073 1283 {
61d1fd8b 1284 $this->country = pl_flatten(func_get_args());
671b7073
RB
1285 }
1286
dcc63ed5 1287 public function buildCondition(PlFilter &$uf)
671b7073
RB
1288 {
1289 $sub = $uf->addMentorFilter(UserFilter::MENTOR_COUNTRY);
61d1fd8b 1290 return $sub . '.country IN ' . XDB::format('{?}', $this->country);
671b7073
RB
1291 }
1292}
1293// }}}
1294
459e6f81
PC
1295// {{{ class UFC_Mentor_Terms
1296/** Filters users based on the job terms they used in mentoring.
1297 * @param $val The ID of the job term, or an array of such IDs
1298 */
1299class UFC_Mentor_Terms implements UserFilterCondition
1300{
1301 private $val;
1302
1303 public function __construct($val)
1304 {
1305 $this->val = $val;
1306 }
1307
1308 public function buildCondition(PlFilter &$uf)
1309 {
1310 $sub = $uf->addMentorFilter(UserFilter::MENTOR_TERM);
1311 return $sub . '.jtid_1 = ' . XDB::escape($this->val);
1312 }
1313}
1314// }}}
1315
671b7073
RB
1316// {{{ class UFC_Mentor_Sectorization
1317/** Filters users based on mentoring (sub|)sector
c5da8574
RB
1318 * @param $sector ID of (sub)sector
1319 * @param $type Whether we are looking for a sector or a subsector
671b7073 1320 */
4e2f2ad2 1321class UFC_Mentor_Sectorization implements UserFilterCondition
671b7073 1322{
c5da8574
RB
1323 const SECTOR = 1;
1324 const SUBSECTOR = 2;
671b7073 1325 private $sector;
c5da8574 1326 private $type;
671b7073 1327
c5da8574 1328 public function __construct($sector, $type = self::SECTOR)
671b7073
RB
1329 {
1330 $this->sector = $sector;
c5da8574 1331 $this->type = $type;
671b7073
RB
1332 }
1333
dcc63ed5 1334 public function buildCondition(PlFilter &$uf)
671b7073
RB
1335 {
1336 $sub = $uf->addMentorFilter(UserFilter::MENTOR_SECTOR);
c5da8574
RB
1337 if ($this->type == self::SECTOR) {
1338 $field = 'sectorid';
1339 } else {
1340 $field = 'subsectorid';
671b7073 1341 }
c5da8574 1342 return $sub . '.' . $field . ' = ' . XDB::format('{?}', $this->sector);
671b7073
RB
1343 }
1344}
1345// }}}
1346
8363588b 1347// {{{ class UFC_UserRelated
5d2e55c7 1348/** Filters users based on a relation toward a user
53eae167
RB
1349 * @param $user User to which searched users are related
1350 */
4e7bf1e0 1351abstract class UFC_UserRelated implements UserFilterCondition
3f42a6ad 1352{
009b8ab7
FB
1353 protected $user;
1354 public function __construct(PlUser &$user)
1355 {
1356 $this->user =& $user;
3f42a6ad 1357 }
4e7bf1e0 1358}
8363588b 1359// }}}
3f42a6ad 1360
8363588b 1361// {{{ class UFC_Contact
5d2e55c7 1362/** Filters users who belong to selected user's contacts
53eae167 1363 */
4e7bf1e0
FB
1364class UFC_Contact extends UFC_UserRelated
1365{
dcc63ed5 1366 public function buildCondition(PlFilter &$uf)
3f42a6ad 1367 {
009b8ab7 1368 $sub = $uf->addContactFilter($this->user->id());
3f42a6ad
FB
1369 return 'c' . $sub . '.contact IS NOT NULL';
1370 }
1371}
8363588b 1372// }}}
3f42a6ad 1373
8363588b 1374// {{{ class UFC_WatchRegistration
53eae167
RB
1375/** Filters users being watched by selected user
1376 */
4e7bf1e0
FB
1377class UFC_WatchRegistration extends UFC_UserRelated
1378{
dcc63ed5 1379 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1380 {
a87530ea 1381 if (!$this->user->watchType('registration')) {
dcc63ed5 1382 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1383 }
1384 $uids = $this->user->watchUsers();
1385 if (count($uids) == 0) {
dcc63ed5 1386 return PlFilterCondition::COND_FALSE;
009b8ab7 1387 } else {
93b0da52 1388 return XDB::format('$UID IN {?}', $uids);
009b8ab7 1389 }
4e7bf1e0
FB
1390 }
1391}
8363588b 1392// }}}
4e7bf1e0 1393
8363588b 1394// {{{ class UFC_WatchPromo
53eae167
RB
1395/** Filters users belonging to a promo watched by selected user
1396 * @param $user Selected user (the one watching promo)
1397 * @param $grade Formation the user is watching
1398 */
4e7bf1e0
FB
1399class UFC_WatchPromo extends UFC_UserRelated
1400{
1401 private $grade;
009b8ab7 1402 public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING)
4e7bf1e0 1403 {
009b8ab7 1404 parent::__construct($user);
4e7bf1e0
FB
1405 $this->grade = $grade;
1406 }
1407
dcc63ed5 1408 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1409 {
009b8ab7
FB
1410 $promos = $this->user->watchPromos();
1411 if (count($promos) == 0) {
dcc63ed5 1412 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1413 } else {
1414 $sube = $uf->addEducationFilter(true, $this->grade);
1415 $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
bde68f05 1416 return XDB::format($field . ' IN {?}', $promos);
009b8ab7 1417 }
4e7bf1e0
FB
1418 }
1419}
8363588b 1420// }}}
4e7bf1e0 1421
8363588b 1422// {{{ class UFC_WatchContact
53eae167
RB
1423/** Filters users watched by selected user
1424 */
009b8ab7 1425class UFC_WatchContact extends UFC_Contact
4e7bf1e0 1426{
dcc63ed5 1427 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1428 {
009b8ab7 1429 if (!$this->user->watchContacts()) {
dcc63ed5 1430 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1431 }
1432 return parent::buildCondition($uf);
4e7bf1e0
FB
1433 }
1434}
8363588b 1435// }}}
4e7bf1e0 1436
48885bbe
FB
1437// {{{ class UFC_MarketingHash
1438/** Filters users using the hash generated
1439 * to send marketing emails to him.
1440 */
1441class UFC_MarketingHash implements UserFilterCondition
1442{
1443 private $hash;
1444
1445 public function __construct($hash)
1446 {
1447 $this->hash = $hash;
1448 }
1449
1450 public function buildCondition(PlFilter &$uf)
1451 {
1452 $table = $uf->addMarketingHash();
1453 return XDB::format('rm.hash = {?}', $this->hash);
1454 }
1455}
416d4244 1456// }}}
4e7bf1e0 1457
d865c296
FB
1458/******************
1459 * ORDERS
1460 ******************/
1461
8363588b 1462// {{{ class UFO_Promo
5d2e55c7
RB
1463/** Orders users by promotion
1464 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
53eae167
RB
1465 * @param $desc Whether sort is descending
1466 */
ccc951d9 1467class UFO_Promo extends PlFilterGroupableOrder
d865c296
FB
1468{
1469 private $grade;
1470
1471 public function __construct($grade = null, $desc = false)
1472 {
009b8ab7 1473 parent::__construct($desc);
d865c296 1474 $this->grade = $grade;
d865c296
FB
1475 }
1476
61f61261 1477 protected function getSortTokens(PlFilter &$uf)
d865c296
FB
1478 {
1479 if (UserFilter::isGrade($this->grade)) {
1480 $sub = $uf->addEducationFilter($this->grade);
1481 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
1482 } else {
1483 $sub = $uf->addDisplayFilter();
1484 return 'pd' . $sub . '.promo';
1485 }
1486 }
1487}
8363588b 1488// }}}
d865c296 1489
8363588b 1490// {{{ class UFO_Name
53eae167 1491/** Sorts users by name
5d2e55c7
RB
1492 * @param $type Type of name on which to sort (firstname...)
1493 * @param $variant Variant of that name to use (marital, ordinary...)
53eae167
RB
1494 * @param $particle Set to true if particles should be included in the sorting order
1495 * @param $desc If sort order should be descending
1496 */
ccc951d9 1497class UFO_Name extends PlFilterOrder
d865c296
FB
1498{
1499 private $type;
1500 private $variant;
1501 private $particle;
1502
1503 public function __construct($type, $variant = null, $particle = false, $desc = false)
1504 {
009b8ab7 1505 parent::__construct($desc);
d865c296
FB
1506 $this->type = $type;
1507 $this->variant = $variant;
1508 $this->particle = $particle;
d865c296
FB
1509 }
1510
61f61261 1511 protected function getSortTokens(PlFilter &$uf)
d865c296 1512 {
913a4e90 1513 if (Profile::isDisplayName($this->type)) {
d865c296 1514 $sub = $uf->addDisplayFilter();
a9ef52c9
RB
1515 $token = 'pd' . $sub . '.' . $this->type;
1516 if ($uf->accountsRequired()) {
1517 $account_token = Profile::getAccountEquivalentName($this->type);
1518 return 'IFNULL(' . $token . ', a.' . $account_token . ')';
1519 } else {
1520 return $token;
1521 }
d865c296
FB
1522 } else {
1523 $sub = $uf->addNameFilter($this->type, $this->variant);
1524 if ($this->particle) {
1525 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1526 } else {
1527 return 'pn' . $sub . '.name';
1528 }
1529 }
1530 }
1531}
8363588b 1532// }}}
d865c296 1533
40585144 1534// {{{ class UFO_Score
ccc951d9 1535class UFO_Score extends PlFilterOrder
40585144 1536{
61f61261 1537 protected function getSortTokens(PlFilter &$uf)
40585144 1538 {
2a93b634
RB
1539 $toks = $uf->getNameTokens();
1540 $scores = array();
488765e3
RB
1541
1542 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
1543 if (count($toks) == 0) {
1544 return 'NULL';
1545 }
1546
2a93b634
RB
1547 foreach ($toks as $sub => $token) {
1548 $scores[] = XDB::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
1549 }
1550 return implode(' + ', $scores);
40585144
RB
1551 }
1552}
1553// }}}
1554
8363588b 1555// {{{ class UFO_Registration
53eae167
RB
1556/** Sorts users based on registration date
1557 */
ccc951d9 1558class UFO_Registration extends PlFilterOrder
38c6fe96 1559{
61f61261 1560 protected function getSortTokens(PlFilter &$uf)
38c6fe96 1561 {
6c1e97ae 1562 $uf->requireAccounts();
009b8ab7 1563 return 'a.registration_date';
38c6fe96 1564 }
009b8ab7 1565}
8363588b 1566// }}}
38c6fe96 1567
8363588b 1568// {{{ class UFO_Birthday
53eae167
RB
1569/** Sorts users based on next birthday date
1570 */
ccc951d9 1571class UFO_Birthday extends PlFilterOrder
009b8ab7 1572{
61f61261 1573 protected function getSortTokens(PlFilter &$uf)
38c6fe96 1574 {
6c1e97ae 1575 $uf->requireProfiles();
009b8ab7 1576 return 'p.next_birthday';
38c6fe96
FB
1577 }
1578}
8363588b 1579// }}}
38c6fe96 1580
8363588b 1581// {{{ class UFO_ProfileUpdate
53eae167
RB
1582/** Sorts users based on last profile update
1583 */
ccc951d9 1584class UFO_ProfileUpdate extends PlFilterOrder
009b8ab7 1585{
61f61261 1586 protected function getSortTokens(PlFilter &$uf)
009b8ab7 1587 {
6c1e97ae 1588 $uf->requireProfiles();
009b8ab7
FB
1589 return 'p.last_change';
1590 }
1591}
8363588b 1592// }}}
009b8ab7 1593
8363588b 1594// {{{ class UFO_Death
53eae167
RB
1595/** Sorts users based on death date
1596 */
ccc951d9 1597class UFO_Death extends PlFilterOrder
009b8ab7 1598{
61f61261 1599 protected function getSortTokens(PlFilter &$uf)
009b8ab7 1600 {
6c1e97ae 1601 $uf->requireProfiles();
009b8ab7
FB
1602 return 'p.deathdate';
1603 }
1604}
8363588b 1605// }}}
009b8ab7 1606
6c1e97ae
FB
1607// {{{ class UFO_Uid
1608/** Sorts users based on their uid
1609 */
ccc951d9 1610class UFO_Uid extends PlFilterOrder
6c1e97ae
FB
1611{
1612 protected function getSortTokens(PlFilter &$uf)
1613 {
1614 $uf->requireAccounts();
93b0da52 1615 return '$UID';
6c1e97ae
FB
1616 }
1617}
c752a130 1618// }}}
6c1e97ae
FB
1619
1620// {{{ class UFO_Hruid
1621/** Sorts users based on their hruid
1622 */
ccc951d9 1623class UFO_Hruid extends PlFilterOrder
6c1e97ae
FB
1624{
1625 protected function getSortTokens(PlFilter &$uf)
1626 {
1627 $uf->requireAccounts();
1628 return 'a.hruid';
1629 }
1630}
1631// }}}
1632
1633// {{{ class UFO_Pid
1634/** Sorts users based on their pid
1635 */
ccc951d9 1636class UFO_Pid extends PlFilterOrder
6c1e97ae
FB
1637{
1638 protected function getSortTokens(PlFilter &$uf)
1639 {
1640 $uf->requireProfiles();
93b0da52 1641 return '$PID';
6c1e97ae
FB
1642 }
1643}
c752a130 1644// }}}
6c1e97ae
FB
1645
1646// {{{ class UFO_Hrpid
1647/** Sorts users based on their hrpid
1648 */
ccc951d9 1649class UFO_Hrpid extends PlFilterOrder
6c1e97ae
FB
1650{
1651 protected function getSortTokens(PlFilter &$uf)
1652 {
1653 $uf->requireProfiles();
1654 return 'p.hrpid';
1655 }
1656}
1657// }}}
1658
009b8ab7 1659
d865c296
FB
1660/***********************************
1661 *********************************
1662 USER FILTER CLASS
1663 *********************************
1664 ***********************************/
1665
8363588b 1666// {{{ class UserFilter
2d83cac9
RB
1667/** This class provides a convenient and centralized way of filtering users.
1668 *
1669 * Usage:
1670 * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t));
1671 *
1672 * Resulting UserFilter can be used to:
1673 * - get a list of User objects matching the filter
1674 * - get a list of UIDs matching the filter
1675 * - get the number of users matching the filter
1676 * - check whether a given User matches the filter
1677 * - filter a list of User objects depending on whether they match the filter
1678 *
1679 * Usage for UFC and UFO objects:
1680 * A UserFilter will call all private functions named XXXJoins.
1681 * These functions must return an array containing the list of join
1682 * required by the various UFC and UFO associated to the UserFilter.
1683 * Entries in those returned array are of the following form:
1684 * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter')
1685 * which will be translated into :
1686 * join_type JOIN joined_table AS join_tablealias ON (join_criter)
1687 * in the final query.
1688 *
1689 * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with
913a4e90 1690 * profile.pid, and $UID with accounts.uid.
2d83cac9
RB
1691 *
1692 * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined;
1693 * its parameter will be used to set various private vars of the UserFilter describing
1694 * the required joins ; such a function shall return the "join_tablealias" to use
1695 * when referring to the joined table.
1696 *
1697 * For example, if data from profile_job must be available to filter results,
aab2ffdd 1698 * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and
2d83cac9
RB
1699 * return 'pj', the short name to use when referring to profile_job; when building
1700 * the query, calling the jobJoins function will return an array containing a single
1701 * row:
1702 * 'pj' => array('left', 'profile_job', '$ME.pid = $UID');
1703 *
1704 * The 'register_optional' function can be used to generate unique table aliases when
1705 * the same table has to be joined several times with different aliases.
1706 */
9b8e5fb4 1707class UserFilter extends PlFilter
a087cc8d 1708{
9b8e5fb4 1709 protected $joinMethods = array();
7ca75030 1710
61f61261
RB
1711 protected $joinMetas = array(
1712 '$PID' => 'p.pid',
1713 '$UID' => 'a.uid',
9b8e5fb4 1714 );
d865c296 1715
a087cc8d 1716 private $root;
24e08e33 1717 private $sort = array();
ccc951d9 1718 private $grouper = null;
784745ce 1719 private $query = null;
24e08e33 1720 private $orderby = null;
784745ce 1721
7f26cd69
RB
1722 // Store the current 'search' visibility.
1723 private $profile_visibility = null;
1724
2daf7250
RB
1725 private $lastusercount = null;
1726 private $lastprofilecount = null;
d865c296 1727
24e08e33 1728 public function __construct($cond = null, $sort = null)
5dd9d823 1729 {
06598c13 1730 if (empty($this->joinMethods)) {
d865c296
FB
1731 $class = new ReflectionClass('UserFilter');
1732 foreach ($class->getMethods() as $method) {
1733 $name = $method->getName();
1734 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
06598c13 1735 $this->joinMethods[] = $name;
d865c296
FB
1736 }
1737 }
1738 }
5dd9d823 1739 if (!is_null($cond)) {
06598c13 1740 if ($cond instanceof PlFilterCondition) {
5dd9d823
FB
1741 $this->setCondition($cond);
1742 }
1743 }
24e08e33 1744 if (!is_null($sort)) {
ccc951d9 1745 if ($sort instanceof PlFilterOrder) {
24e08e33 1746 $this->addSort($sort);
d865c296
FB
1747 } else if (is_array($sort)) {
1748 foreach ($sort as $s) {
1749 $this->addSort($s);
1750 }
24e08e33
FB
1751 }
1752 }
7f26cd69
RB
1753
1754 // This will set the visibility to the default correct level.
1755 $this->profile_visibility = new ProfileVisibility();
1756 }
1757
1758 public function getVisibilityLevels()
1759 {
1760 return $this->profile_visibility->levels();
1761 }
1762
1763 public function getVisibilityLevel()
1764 {
1765 return $this->profile_visibility->level();
1766 }
1767
1768 public function restrictVisibilityTo($level)
1769 {
1770 $this->profile_visibility->setLevel($level);
1771 }
1772
1773 public function getVisibilityCondition($field)
1774 {
1775 return $field . ' IN ' . XDB::formatArray($this->getVisibilityLevels());
5dd9d823
FB
1776 }
1777
784745ce
FB
1778 private function buildQuery()
1779 {
2a93b634
RB
1780 // The root condition is built first because some orders need info
1781 // available only once all UFC have set their conditions (UFO_Score)
1782 if (is_null($this->query)) {
1783 $where = $this->root->buildCondition($this);
226626ae
FB
1784 $where = str_replace(array_keys($this->joinMetas),
1785 $this->joinMetas,
1786 $where);
2a93b634 1787 }
d865c296
FB
1788 if (is_null($this->orderby)) {
1789 $orders = array();
1790 foreach ($this->sort as $sort) {
1791 $orders = array_merge($orders, $sort->buildSort($this));
1792 }
1793 if (count($orders) == 0) {
1794 $this->orderby = '';
1795 } else {
1796 $this->orderby = 'ORDER BY ' . implode(', ', $orders);
1797 }
226626ae
FB
1798 $this->orderby = str_replace(array_keys($this->joinMetas),
1799 $this->joinMetas,
1800 $this->orderby);
d865c296 1801 }
784745ce 1802 if (is_null($this->query)) {
2a93b634 1803 if ($this->with_accounts) {
b8dcf62d
RB
1804 $from = 'accounts AS a';
1805 } else {
1806 $this->requireProfiles();
1807 $from = 'profiles AS p';
1808 }
f7ea7450 1809 $joins = $this->buildJoins();
b8dcf62d 1810 $this->query = 'FROM ' . $from . '
784745ce
FB
1811 ' . $joins . '
1812 WHERE (' . $where . ')';
1813 }
1814 }
1815
ccc951d9
RB
1816 public function hasGroups()
1817 {
1818 return $this->grouper != null;
1819 }
1820
1821 public function getGroups()
1822 {
1823 return $this->getUIDGroups();
1824 }
1825
1826 public function getUIDGroups()
1827 {
1828 $this->requireAccounts();
1829 $this->buildQuery();
1830 $token = $this->grouper->getGroupToken($this);
1831
1832 $groups = XDB::fetchAllRow('SELECT ' . $token . ', COUNT(a.uid)
1833 ' . $this->query . '
1834 GROUP BY ' . $token,
1835 0);
1836 return $groups;
1837 }
1838
1839 public function getPIDGroups()
1840 {
1841 $this->requireProfiles();
1842 $this->buildQuery();
1843 $token = $this->grouper->getGroupToken($this);
1844
1845 $groups = XDB::fetchAllRow('SELECT ' . $token . ', COUNT(p.pid)
1846 ' . $this->query . '
1847 GROUP BY ' . $token,
1848 0);
1849 return $groups;
1850 }
1851
7ca75030 1852 private function getUIDList($uids = null, PlLimit &$limit)
d865c296 1853 {
b8dcf62d 1854 $this->requireAccounts();
d865c296 1855 $this->buildQuery();
7ca75030 1856 $lim = $limit->getSql();
d865c296 1857 $cond = '';
45b20ca0 1858 if (!empty($uids)) {
bde68f05 1859 $cond = XDB::format(' AND a.uid IN {?}', $uids);
d865c296
FB
1860 }
1861 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1862 ' . $this->query . $cond . '
1863 GROUP BY a.uid
1864 ' . $this->orderby . '
7ca75030 1865 ' . $lim);
2daf7250 1866 $this->lastusercount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
d865c296
FB
1867 return $fetched;
1868 }
1869
043b104b
RB
1870 private function getPIDList($pids = null, PlLimit &$limit)
1871 {
1872 $this->requireProfiles();
1873 $this->buildQuery();
1874 $lim = $limit->getSql();
1875 $cond = '';
1876 if (!is_null($pids)) {
bde68f05 1877 $cond = XDB::format(' AND p.pid IN {?}', $pids);
043b104b
RB
1878 }
1879 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid
1880 ' . $this->query . $cond . '
1881 GROUP BY p.pid
1882 ' . $this->orderby . '
1883 ' . $lim);
2daf7250 1884 $this->lastprofilecount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
043b104b
RB
1885 return $fetched;
1886 }
1887
434570c4
FB
1888 private static function defaultLimit($limit) {
1889 if ($limit == null) {
1890 return new PlLimit();
1891 } else {
1892 return $limit;
1893 }
1894 }
1895
a087cc8d
FB
1896 /** Check that the user match the given rule.
1897 */
1898 public function checkUser(PlUser &$user)
1899 {
b8dcf62d 1900 $this->requireAccounts();
784745ce
FB
1901 $this->buildQuery();
1902 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1903 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
1904 return $count == 1;
a087cc8d
FB
1905 }
1906
043b104b
RB
1907 /** Check that the profile match the given rule.
1908 */
1909 public function checkProfile(Profile &$profile)
1910 {
1911 $this->requireProfiles();
1912 $this->buildQuery();
1913 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1914 ' . $this->query . XDB::format(' AND p.pid = {?}', $profile->id()));
1915 return $count == 1;
1916 }
1917
1918 /** Default filter is on users
a087cc8d 1919 */
434570c4 1920 public function filter(array $users, $limit = null)
a087cc8d 1921 {
434570c4 1922 return $this->filterUsers($users, self::defaultLimit($limit));
043b104b
RB
1923 }
1924
1925 /** Filter a list of users to extract the users matching the rule.
1926 */
434570c4 1927 public function filterUsers(array $users, $limit = null)
043b104b 1928 {
434570c4 1929 $limit = self::defaultLimit($limit);
b8dcf62d 1930 $this->requireAccounts();
4927ee54
FB
1931 $this->buildQuery();
1932 $table = array();
1933 $uids = array();
1934 foreach ($users as $user) {
07eb5b0e
FB
1935 if ($user instanceof PlUser) {
1936 $uid = $user->id();
1937 } else {
1938 $uid = $user;
1939 }
1940 $uids[] = $uid;
1941 $table[$uid] = $user;
4927ee54 1942 }
7ca75030 1943 $fetched = $this->getUIDList($uids, $limit);
a087cc8d 1944 $output = array();
4927ee54
FB
1945 foreach ($fetched as $uid) {
1946 $output[] = $table[$uid];
a087cc8d
FB
1947 }
1948 return $output;
1949 }
1950
043b104b
RB
1951 /** Filter a list of profiles to extract the users matching the rule.
1952 */
434570c4 1953 public function filterProfiles(array $profiles, $limit = null)
043b104b 1954 {
434570c4 1955 $limit = self::defaultLimit($limit);
043b104b
RB
1956 $this->requireProfiles();
1957 $this->buildQuery();
1958 $table = array();
1959 $pids = array();
1960 foreach ($profiles as $profile) {
1961 if ($profile instanceof Profile) {
1962 $pid = $profile->id();
1963 } else {
1964 $pid = $profile;
1965 }
1966 $pids[] = $pid;
1967 $table[$pid] = $profile;
1968 }
1969 $fetched = $this->getPIDList($pids, $limit);
1970 $output = array();
1971 foreach ($fetched as $pid) {
1972 $output[] = $table[$pid];
1973 }
1974 return $output;
1975 }
1976
434570c4 1977 public function getUIDs($limit = null)
7ca75030 1978 {
833a6e86
FB
1979 $limit = self::defaultLimit($limit);
1980 return $this->getUIDList(null, $limit);
7ca75030
RB
1981 }
1982
ad27b22e
FB
1983 public function getUID($pos = 0)
1984 {
983f3864 1985 $uids =$this->getUIDList(null, new PlLimit(1, $pos));
ad27b22e
FB
1986 if (count($uids) == 0) {
1987 return null;
1988 } else {
1989 return $uids[0];
1990 }
1991 }
1992
434570c4 1993 public function getPIDs($limit = null)
043b104b 1994 {
833a6e86
FB
1995 $limit = self::defaultLimit($limit);
1996 return $this->getPIDList(null, $limit);
043b104b
RB
1997 }
1998
ad27b22e
FB
1999 public function getPID($pos = 0)
2000 {
983f3864 2001 $pids =$this->getPIDList(null, new PlLimit(1, $pos));
ad27b22e
FB
2002 if (count($pids) == 0) {
2003 return null;
2004 } else {
2005 return $pids[0];
2006 }
2007 }
2008
434570c4 2009 public function getUsers($limit = null)
4927ee54 2010 {
7ca75030 2011 return User::getBulkUsersWithUIDs($this->getUIDs($limit));
d865c296
FB
2012 }
2013
ad27b22e
FB
2014 public function getUser($pos = 0)
2015 {
2016 $uid = $this->getUID($pos);
2017 if ($uid == null) {
2018 return null;
2019 } else {
2020 return User::getWithUID($uid);
2021 }
2022 }
2023
0d906109
RB
2024 public function iterUsers($limit = null)
2025 {
2026 return User::iterOverUIDs($this->getUIDs($limit));
2027 }
2028
00f83317 2029 public function getProfiles($limit = null, $fields = 0x0000, $visibility = null)
043b104b 2030 {
00f83317 2031 return Profile::getBulkProfilesWithPIDs($this->getPIDs($limit), $fields, $visibility);
043b104b
RB
2032 }
2033
00f83317 2034 public function getProfile($pos = 0, $fields = 0x0000, $visibility = null)
ad27b22e
FB
2035 {
2036 $pid = $this->getPID($pos);
2037 if ($pid == null) {
2038 return null;
2039 } else {
00f83317 2040 return Profile::get($pid, $fields, $visibility);
ad27b22e
FB
2041 }
2042 }
2043
00f83317 2044 public function iterProfiles($limit = null, $fields = 0x0000, $visibility = null)
0d906109 2045 {
00f83317 2046 return Profile::iterOverPIDs($this->getPIDs($limit), true, $fields, $visibility);
0d906109
RB
2047 }
2048
434570c4 2049 public function get($limit = null)
d865c296 2050 {
7ca75030 2051 return $this->getUsers($limit);
4927ee54
FB
2052 }
2053
aaf70eb8 2054
d865c296 2055 public function getTotalCount()
4927ee54 2056 {
2daf7250
RB
2057 return $this->getTotalUserCount();
2058 }
2059
2060 public function getTotalUserCount()
2061 {
2062 if (is_null($this->lastusercount)) {
2063 $this->requireAccounts();
aa21c568 2064 $this->buildQuery();
2daf7250
RB
2065 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
2066 ' . $this->query);
2067 } else {
2068 return $this->lastusercount;
2069 }
2070 }
2071
2072 public function getTotalProfileCount()
2073 {
2074 if (is_null($this->lastprofilecount)) {
2075 $this->requireProfiles();
2076 $this->buildQuery();
2077 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT p.pid)
7e735012 2078 ' . $this->query);
38c6fe96 2079 } else {
2daf7250 2080 return $this->lastprofilecount;
38c6fe96 2081 }
4927ee54
FB
2082 }
2083
9b8e5fb4 2084 public function setCondition(PlFilterCondition &$cond)
a087cc8d
FB
2085 {
2086 $this->root =& $cond;
784745ce 2087 $this->query = null;
a087cc8d
FB
2088 }
2089
9b8e5fb4 2090 public function addSort(PlFilterOrder &$sort)
24e08e33 2091 {
ccc951d9
RB
2092 if (count($this->sort) == 0 && $sort instanceof PlFilterGroupableOrder)
2093 {
2094 $this->grouper = $sort;
2095 }
d865c296
FB
2096 $this->sort[] = $sort;
2097 $this->orderby = null;
24e08e33
FB
2098 }
2099
a087cc8d
FB
2100 static public function getLegacy($promo_min, $promo_max)
2101 {
a087cc8d 2102 if ($promo_min != 0) {
784745ce 2103 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
5dd9d823 2104 } else {
88c31faf 2105 $min = new PFC_True();
a087cc8d 2106 }
a087cc8d 2107 if ($promo_max != 0) {
784745ce 2108 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
a087cc8d 2109 } else {
88c31faf 2110 $max = new PFC_True();
a087cc8d 2111 }
9b8e5fb4 2112 return new UserFilter(new PFC_And($min, $max));
a087cc8d 2113 }
784745ce 2114
07eb5b0e
FB
2115 static public function sortByName()
2116 {
913a4e90 2117 return array(new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME));
07eb5b0e
FB
2118 }
2119
2120 static public function sortByPromo()
2121 {
913a4e90 2122 return array(new UFO_Promo(), new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME));
07eb5b0e
FB
2123 }
2124
aa21c568
FB
2125 static private function getDBSuffix($string)
2126 {
2d6329a2
FB
2127 if (is_array($string)) {
2128 if (count($string) == 1) {
2129 return self::getDBSuffix(array_pop($string));
2130 }
2131 return md5(implode('|', $string));
2132 } else {
2133 return preg_replace('/[^a-z0-9]/i', '', $string);
2134 }
aa21c568
FB
2135 }
2136
2137
2d83cac9
RB
2138 /** Stores a new (and unique) table alias in the &$table table
2139 * @param &$table Array in which the table alias must be stored
2140 * @param $val Value which will then be used to build the join
2141 * @return Name of the newly created alias
2142 */
aa21c568
FB
2143 private $option = 0;
2144 private function register_optional(array &$table, $val)
2145 {
2146 if (is_null($val)) {
2147 $sub = $this->option++;
2148 $index = null;
2149 } else {
2150 $sub = self::getDBSuffix($val);
2151 $index = $val;
2152 }
2153 $sub = '_' . $sub;
2154 $table[$sub] = $index;
2155 return $sub;
2156 }
784745ce 2157
b8dcf62d
RB
2158 /** PROFILE VS ACCOUNT
2159 */
f7ea7450
RB
2160 private $with_profiles = false;
2161 private $with_accounts = false;
b8dcf62d
RB
2162 public function requireAccounts()
2163 {
2164 $this->with_accounts = true;
2165 }
2166
a9ef52c9
RB
2167 public function accountsRequired()
2168 {
2169 return $this->with_accounts;
2170 }
2171
b8dcf62d
RB
2172 public function requireProfiles()
2173 {
2174 $this->with_profiles = true;
2175 }
2176
a9ef52c9
RB
2177 public function profilesRequired()
2178 {
2179 return $this->with_profiles;
2180 }
2181
b8dcf62d
RB
2182 protected function accountJoins()
2183 {
2184 $joins = array();
2a93b634 2185 if ($this->with_profiles && $this->with_accounts) {
5c412626
FB
2186 $joins['ap'] = PlSqlJoin::left('account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)');
2187 $joins['p'] = PlSqlJoin::left('profiles', '$PID = ap.pid');
b8dcf62d
RB
2188 }
2189 return $joins;
2190 }
2191
d865c296
FB
2192 /** DISPLAY
2193 */
38c6fe96 2194 const DISPLAY = 'display';
d865c296
FB
2195 private $pd = false;
2196 public function addDisplayFilter()
2197 {
b8dcf62d 2198 $this->requireProfiles();
d865c296
FB
2199 $this->pd = true;
2200 return '';
2201 }
2202
9b8e5fb4 2203 protected function displayJoins()
d865c296
FB
2204 {
2205 if ($this->pd) {
5c412626 2206 return array('pd' => PlSqlJoin::left('profile_display', '$ME.pid = $PID'));
d865c296
FB
2207 } else {
2208 return array();
2209 }
2210 }
2211
f73a4f1b
RB
2212 /** LOGGER
2213 */
2214
2215 private $with_logger = false;
2216 public function addLoggerFilter()
2217 {
2218 $this->with_logger = true;
2219 $this->requireAccounts();
2220 return 'ls';
2221 }
2222 protected function loggerJoins()
2223 {
2224 $joins = array();
2225 if ($this->with_logger) {
5c412626 2226 $joins['ls'] = PlSqlJoin::left('log_sessions', '$ME.uid = $UID');
f73a4f1b
RB
2227 }
2228 return $joins;
2229 }
2230
784745ce
FB
2231 /** NAMES
2232 */
784745ce
FB
2233
2234 static public function assertName($name)
2235 {
07613cdd 2236 if (!DirEnum::getID(DirEnum::NAMETYPES, $name)) {
9b8e5fb4 2237 Platal::page()->kill('Invalid name type: ' . $name);
784745ce
FB
2238 }
2239 }
2240
2241 private $pn = array();
784745ce
FB
2242 public function addNameFilter($type, $variant = null)
2243 {
b8dcf62d 2244 $this->requireProfiles();
784745ce
FB
2245 if (!is_null($variant)) {
2246 $ft = $type . '_' . $variant;
2247 } else {
2248 $ft = $type;
2249 }
2250 $sub = '_' . $ft;
2251 self::assertName($ft);
2252
2253 if (!is_null($variant) && $variant == 'other') {
aa21c568 2254 $sub .= $this->option++;
784745ce 2255 }
07613cdd 2256 $this->pn[$sub] = DirEnum::getID(DirEnum::NAMETYPES, $ft);
784745ce
FB
2257 return $sub;
2258 }
2259
9b8e5fb4 2260 protected function nameJoins()
784745ce
FB
2261 {
2262 $joins = array();
2263 foreach ($this->pn as $sub => $type) {
5c412626 2264 $joins['pn' . $sub] = PlSqlJoin::left('profile_name', '$ME.pid = $PID AND $ME.typeid = {?}', $type);
784745ce
FB
2265 }
2266 return $joins;
2267 }
2268
40585144
RB
2269 /** NAMETOKENS
2270 */
2a93b634
RB
2271 private $name_tokens = array();
2272 private $nb_tokens = 0;
2273
2274 public function addNameTokensFilter($token)
40585144
RB
2275 {
2276 $this->requireProfiles();
2a93b634
RB
2277 $sub = 'sn' . (1 + $this->nb_tokens);
2278 $this->nb_tokens++;
2279 $this->name_tokens[$sub] = $token;
2280 return $sub;
40585144
RB
2281 }
2282
2283 protected function nameTokensJoins()
2284 {
f7ea7450 2285 /* We don't return joins, since with_sn forces the SELECT to run on search_name first */
2a93b634
RB
2286 $joins = array();
2287 foreach ($this->name_tokens as $sub => $token) {
2288 $joins[$sub] = PlSqlJoin::left('search_name', '$ME.pid = $PID');
40585144 2289 }
2a93b634
RB
2290 return $joins;
2291 }
2292
2293 public function getNameTokens()
2294 {
2295 return $this->name_tokens;
40585144
RB
2296 }
2297
a7f8e48a
RB
2298 /** NATIONALITY
2299 */
2300
2301 private $with_nat = false;
2302 public function addNationalityFilter()
2303 {
2304 $this->with_nat = true;
2305 return 'ngc';
2306 }
2307
2308 protected function nationalityJoins()
2309 {
2310 $joins = array();
2311 if ($this->with_nat) {
5c412626 2312 $joins['ngc'] = PlSqlJoin::left('geoloc_countries', '$ME.iso_3166_1_a2 = p.nationality1 OR $ME.iso_3166_1_a2 = p.nationality2 OR $ME.iso_3166_1_a2 = p.nationality3');
a7f8e48a
RB
2313 }
2314 return $joins;
2315 }
2316
784745ce
FB
2317 /** EDUCATION
2318 */
2319 const GRADE_ING = 'Ing.';
2320 const GRADE_PHD = 'PhD';
2321 const GRADE_MST = 'M%';
2322 static public function isGrade($grade)
2323 {
93c2f133 2324 return ($grade !== 0) && ($grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST);
784745ce
FB
2325 }
2326
2327 static public function assertGrade($grade)
2328 {
2329 if (!self::isGrade($grade)) {
ad27b22e 2330 Platal::page()->killError("Diplôme non valide: $grade");
784745ce
FB
2331 }
2332 }
2333
d865c296
FB
2334 static public function promoYear($grade)
2335 {
2336 // XXX: Definition of promotion for phds and masters might change in near future.
2337 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
2338 }
2339
784745ce
FB
2340 private $pepe = array();
2341 private $with_pee = false;
784745ce
FB
2342 public function addEducationFilter($x = false, $grade = null)
2343 {
b8dcf62d 2344 $this->requireProfiles();
784745ce 2345 if (!$x) {
aa21c568
FB
2346 $index = $this->option;
2347 $sub = $this->option++;
784745ce
FB
2348 } else {
2349 self::assertGrade($grade);
2350 $index = $grade;
2351 $sub = $grade[0];
2352 $this->with_pee = true;
2353 }
2354 $sub = '_' . $sub;
2355 $this->pepe[$index] = $sub;
2356 return $sub;
2357 }
2358
9b8e5fb4 2359 protected function educationJoins()
784745ce
FB
2360 {
2361 $joins = array();
2362 if ($this->with_pee) {
5c412626 2363 $joins['pee'] = PlSqlJoin::inner('profile_education_enum', 'pee.abbreviation = \'X\'');
784745ce
FB
2364 }
2365 foreach ($this->pepe as $grade => $sub) {
2366 if ($this->isGrade($grade)) {
5c412626
FB
2367 $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID');
2368 $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE {?}', $grade);
784745ce 2369 } else {
5c412626
FB
2370 $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.pid = $PID');
2371 $joins['pee' . $sub] = PlSqlJoin::inner('profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
2372 $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
784745ce
FB
2373 }
2374 }
2375 return $joins;
2376 }
4927ee54
FB
2377
2378
2379 /** GROUPS
2380 */
2381 private $gpm = array();
4927ee54
FB
2382 public function addGroupFilter($group = null)
2383 {
b8dcf62d 2384 $this->requireAccounts();
4927ee54 2385 if (!is_null($group)) {
4aae4d2c 2386 if (is_int($group) || ctype_digit($group)) {
4927ee54
FB
2387 $index = $sub = $group;
2388 } else {
2389 $index = $group;
aa21c568 2390 $sub = self::getDBSuffix($group);
4927ee54
FB
2391 }
2392 } else {
aa21c568 2393 $sub = 'group_' . $this->option++;
4927ee54
FB
2394 $index = null;
2395 }
2396 $sub = '_' . $sub;
2397 $this->gpm[$sub] = $index;
2398 return $sub;
2399 }
2400
9b8e5fb4 2401 protected function groupJoins()
4927ee54
FB
2402 {
2403 $joins = array();
2404 foreach ($this->gpm as $sub => $key) {
2405 if (is_null($key)) {
5c412626
FB
2406 $joins['gpa' . $sub] = PlSqlJoin::inner('groups');
2407 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
4aae4d2c 2408 } else if (is_int($key) || ctype_digit($key)) {
5c412626 2409 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
4927ee54 2410 } else {
5c412626
FB
2411 $joins['gpa' . $sub] = PlSqlJoin::inner('groups', '$ME.diminutif = {?}', $key);
2412 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
4927ee54
FB
2413 }
2414 }
2415 return $joins;
0fb3713c
RB
2416 }
2417
2418 /** BINETS
2419 */
2420
a7f8e48a
RB
2421 private $with_bi = false;
2422 private $with_bd = false;
d7ddf29b 2423 public function addBinetsFilter($with_enum = false)
0fb3713c
RB
2424 {
2425 $this->requireProfiles();
a7f8e48a
RB
2426 $this->with_bi = true;
2427 if ($with_enum) {
2428 $this->with_bd = true;
2429 return 'bd';
2430 } else {
2431 return 'bi';
2432 }
0fb3713c
RB
2433 }
2434
2435 protected function binetsJoins()
2436 {
2437 $joins = array();
a7f8e48a 2438 if ($this->with_bi) {
5c412626 2439 $joins['bi'] = PlSqlJoin::left('profile_binets', '$ME.pid = $PID');
0fb3713c 2440 }
a7f8e48a 2441 if ($this->with_bd) {
5c412626 2442 $joins['bd'] = PlSqlJoin::left('profile_binet_enum', '$ME.id = bi.binet_id');
a7f8e48a 2443 }
0fb3713c 2444 return $joins;
4927ee54 2445 }
aa21c568
FB
2446
2447 /** EMAILS
2448 */
2449 private $e = array();
2450 public function addEmailRedirectFilter($email = null)
2451 {
b8dcf62d 2452 $this->requireAccounts();
aa21c568
FB
2453 return $this->register_optional($this->e, $email);
2454 }
2455
2456 private $ve = array();
2457 public function addVirtualEmailFilter($email = null)
2458 {
21401768 2459 $this->addAliasFilter(self::ALIAS_FORLIFE);
aa21c568
FB
2460 return $this->register_optional($this->ve, $email);
2461 }
2462
21401768
FB
2463 const ALIAS_BEST = 'bestalias';
2464 const ALIAS_FORLIFE = 'forlife';
aa21c568
FB
2465 private $al = array();
2466 public function addAliasFilter($alias = null)
2467 {
b8dcf62d 2468 $this->requireAccounts();
aa21c568
FB
2469 return $this->register_optional($this->al, $alias);
2470 }
2471
9b8e5fb4 2472 protected function emailJoins()
aa21c568
FB
2473 {
2474 global $globals;
2475 $joins = array();
2476 foreach ($this->e as $sub=>$key) {
2477 if (is_null($key)) {
5c412626 2478 $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
aa21c568 2479 } else {
2d6329a2
FB
2480 if (!is_array($key)) {
2481 $key = array($key);
2482 }
aab2ffdd 2483 $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'
2f1c94e0 2484 AND $ME.email IN {?}', $key);
aa21c568
FB
2485 }
2486 }
21401768 2487 foreach ($this->al as $sub=>$key) {
aa21c568 2488 if (is_null($key)) {
5c412626 2489 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
21401768 2490 } else if ($key == self::ALIAS_BEST) {
5c412626 2491 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
21401768 2492 } else if ($key == self::ALIAS_FORLIFE) {
5c412626 2493 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type = \'a_vie\'');
aa21c568 2494 } else {
2d6329a2
FB
2495 if (!is_array($key)) {
2496 $key = array($key);
2497 }
aab2ffdd 2498 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')
bde68f05 2499 AND $ME.alias IN {?}', $key);
aa21c568 2500 }
aa21c568 2501 }
21401768 2502 foreach ($this->ve as $sub=>$key) {
aa21c568 2503 if (is_null($key)) {
5c412626 2504 $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\'');
aa21c568 2505 } else {
2d6329a2
FB
2506 if (!is_array($key)) {
2507 $key = array($key);
2508 }
bde68f05 2509 $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\' AND $ME.alias IN {?}', $key);
aa21c568 2510 }
5c412626
FB
2511 $joins['vr' . $sub] = PlSqlJoin::left('virtual_redirect',
2512 '$ME.vid = v' . $sub . '.vid
2513 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
2514 CONCAT(al_forlife.alias, \'@\', {?}),
2515 a.email))',
2516 $globals->mail->domain, $globals->mail->domain2);
aa21c568
FB
2517 }
2518 return $joins;
2519 }
3f42a6ad
FB
2520
2521
c4b24511
RB
2522 /** ADDRESSES
2523 */
036d1637 2524 private $with_pa = false;
c4b24511
RB
2525 public function addAddressFilter()
2526 {
b8dcf62d 2527 $this->requireProfiles();
036d1637
RB
2528 $this->with_pa = true;
2529 return 'pa';
c4b24511
RB
2530 }
2531
2b9ca54d
RB
2532 private $with_pac = false;
2533 public function addAddressCountryFilter()
2534 {
2535 $this->requireProfiles();
2536 $this->addAddressFilter();
2537 $this->with_pac = true;
2538 return 'gc';
2539 }
2540
d7ddf29b 2541 private $with_pal = false;
2b9ca54d
RB
2542 public function addAddressLocalityFilter()
2543 {
2544 $this->requireProfiles();
2545 $this->addAddressFilter();
2546 $this->with_pal = true;
2547 return 'gl';
2548 }
2549
9b8e5fb4 2550 protected function addressJoins()
c4b24511
RB
2551 {
2552 $joins = array();
036d1637 2553 if ($this->with_pa) {
5c412626 2554 $joins['pa'] = PlSqlJoin::left('profile_addresses', '$ME.pid = $PID');
c4b24511 2555 }
2b9ca54d 2556 if ($this->with_pac) {
5c412626 2557 $joins['gc'] = PlSqlJoin::left('geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID');
2b9ca54d
RB
2558 }
2559 if ($this->with_pal) {
5c412626 2560 $joins['gl'] = PlSqlJoin::left('geoloc_localities', '$ME.id = pa.localityID');
2b9ca54d 2561 }
c4b24511
RB
2562 return $joins;
2563 }
2564
2565
4083b126
RB
2566 /** CORPS
2567 */
2568
2569 private $pc = false;
2570 private $pce = array();
2571 private $pcr = false;
2572 public function addCorpsFilter($type)
2573 {
b8dcf62d 2574 $this->requireProfiles();
4083b126
RB
2575 $this->pc = true;
2576 if ($type == UFC_Corps::CURRENT) {
2577 $pce['pcec'] = 'current_corpsid';
2578 return 'pcec';
2579 } else if ($type == UFC_Corps::ORIGIN) {
2580 $pce['pceo'] = 'original_corpsid';
2581 return 'pceo';
2582 }
2583 }
2584
2585 public function addCorpsRankFilter()
2586 {
b8dcf62d 2587 $this->requireProfiles();
4083b126
RB
2588 $this->pc = true;
2589 $this->pcr = true;
2590 return 'pcr';
2591 }
2592
9b8e5fb4 2593 protected function corpsJoins()
4083b126
RB
2594 {
2595 $joins = array();
2596 if ($this->pc) {
5c412626 2597 $joins['pc'] = PlSqlJoin::left('profile_corps', '$ME.pid = $PID');
4083b126
RB
2598 }
2599 if ($this->pcr) {
5c412626 2600 $joins['pcr'] = PlSqlJoin::left('profile_corps_rank_enum', '$ME.id = pc.rankid');
4083b126
RB
2601 }
2602 foreach($this->pce as $sub => $field) {
5c412626 2603 $joins[$sub] = PlSqlJoin::left('profile_corps_enum', '$ME.id = pc.' . $field);
4083b126
RB
2604 }
2605 return $joins;
2606 }
2607
6a99c3ac
RB
2608 /** JOBS
2609 */
2610
61f61261
RB
2611 const JOB_SECTOR = 0x0001;
2612 const JOB_SUBSECTOR = 0x0002;
2613 const JOB_SUBSUBSECTOR = 0x0004;
2614 const JOB_ALTERNATES = 0x0008;
2615 const JOB_USERDEFINED = 0x0010;
2616 const JOB_CV = 0x0020;
2617
2618 const JOB_SECTORIZATION = 0x000F;
2619 const JOB_ANY = 0x003F;
6a99c3ac
RB
2620
2621 /** Joins :
2622 * pj => profile_job
2623 * pje => profile_job_enum
2624 * pjse => profile_job_sector_enum
2625 * pjsse => profile_job_subsector_enum
2626 * pjssse => profile_job_subsubsector_enum
2627 * pja => profile_job_alternates
3ac45f10 2628 * pjt => profile_job_terms
6a99c3ac
RB
2629 */
2630 private $with_pj = false;
2631 private $with_pje = false;
2632 private $with_pjse = false;
2633 private $with_pjsse = false;
2634 private $with_pjssse = false;
2635 private $with_pja = false;
3ac45f10 2636 private $with_pjt = 0;
6a99c3ac
RB
2637
2638 public function addJobFilter()
2639 {
b8dcf62d 2640 $this->requireProfiles();
6a99c3ac
RB
2641 $this->with_pj = true;
2642 return 'pj';
2643 }
2644
2645 public function addJobCompanyFilter()
2646 {
2647 $this->addJobFilter();
2648 $this->with_pje = true;
2649 return 'pje';
2650 }
2651
2652 public function addJobSectorizationFilter($type)
2653 {
2654 $this->addJobFilter();
2655 if ($type == self::JOB_SECTOR) {
2656 $this->with_pjse = true;
2657 return 'pjse';
2658 } else if ($type == self::JOB_SUBSECTOR) {
2659 $this->with_pjsse = true;
2660 return 'pjsse';
2661 } else if ($type == self::JOB_SUBSUBSECTOR) {
2662 $this->with_pjssse = true;
2663 return 'pjssse';
2664 } else if ($type == self::JOB_ALTERNATES) {
2665 $this->with_pja = true;
2666 return 'pja';
2667 }
2668 }
2669
3ac45f10
PC
2670 /**
2671 * Adds a filter on job terms of profile.
2672 * @param $nb the number of job terms to use
2673 * @return an array of the fields to filter (one for each term).
3ac45f10
PC
2674 */
2675 public function addJobTermsFilter($nb = 1)
2676 {
2677 $this->with_pjt = $nb;
2678 $jobtermstable = array();
2679 for ($i = 1; $i <= $nb; ++$i) {
4ec03752 2680 $jobtermstable[] = 'pjtr_'.$i;
3ac45f10
PC
2681 }
2682 return $jobtermstable;
2683 }
2684
9b8e5fb4 2685 protected function jobJoins()
6a99c3ac
RB
2686 {
2687 $joins = array();
2688 if ($this->with_pj) {
5c412626 2689 $joins['pj'] = PlSqlJoin::left('profile_job', '$ME.pid = $PID');
6a99c3ac
RB
2690 }
2691 if ($this->with_pje) {
5c412626 2692 $joins['pje'] = PlSqlJoin::left('profile_job_enum', '$ME.id = pj.jobid');
6a99c3ac
RB
2693 }
2694 if ($this->with_pjse) {
5c412626 2695 $joins['pjse'] = PlSqlJoin::left('profile_job_sector_enum', '$ME.id = pj.sectorid');
6a99c3ac
RB
2696 }
2697 if ($this->with_pjsse) {
5c412626 2698 $joins['pjsse'] = PlSqlJoin::left('profile_job_subsector_enum', '$ME.id = pj.subsectorid');
6a99c3ac
RB
2699 }
2700 if ($this->with_pjssse) {
5c412626 2701 $joins['pjssse'] = PlSqlJoin::left('profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
6a99c3ac
RB
2702 }
2703 if ($this->with_pja) {
5c412626 2704 $joins['pja'] = PlSqlJoin::left('profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
6a99c3ac 2705 }
3ac45f10
PC
2706 if ($this->with_pjt > 0) {
2707 for ($i = 1; $i <= $this->with_pjt; ++$i) {
2708 $joins['pjt_'.$i] = PlSqlJoin::left('profile_job_term', '$ME.pid = $PID');
2709 $joins['pjtr_'.$i] = PlSqlJoin::left('profile_job_term_relation', '$ME.jtid_2 = pjt_'.$i.'.jtid');
2710 }
2711 }
6a99c3ac
RB
2712 return $joins;
2713 }
2714
0a2e9c74
RB
2715 /** NETWORKING
2716 */
2717
2718 private $with_pnw = false;
2719 public function addNetworkingFilter()
2720 {
b8dcf62d 2721 $this->requireAccounts();
0a2e9c74
RB
2722 $this->with_pnw = true;
2723 return 'pnw';
2724 }
2725
9b8e5fb4 2726 protected function networkingJoins()
0a2e9c74
RB
2727 {
2728 $joins = array();
2729 if ($this->with_pnw) {
5c412626 2730 $joins['pnw'] = PlSqlJoin::left('profile_networking', '$ME.pid = $PID');
0a2e9c74
RB
2731 }
2732 return $joins;
2733 }
2734
6d62969e
RB
2735 /** PHONE
2736 */
2737
2d83cac9 2738 private $with_ptel = false;
6d62969e
RB
2739
2740 public function addPhoneFilter()
2741 {
b8dcf62d 2742 $this->requireAccounts();
2d83cac9 2743 $this->with_ptel = true;
6d62969e
RB
2744 return 'ptel';
2745 }
2746
9b8e5fb4 2747 protected function phoneJoins()
6d62969e
RB
2748 {
2749 $joins = array();
2d83cac9 2750 if ($this->with_ptel) {
5c412626 2751 $joins['ptel'] = PlSqlJoin::left('profile_phones', '$ME.pid = $PID');
6d62969e
RB
2752 }
2753 return $joins;
2754 }
2755
ceb512d2
RB
2756 /** MEDALS
2757 */
2758
2d83cac9 2759 private $with_pmed = false;
ceb512d2
RB
2760 public function addMedalFilter()
2761 {
b8dcf62d 2762 $this->requireProfiles();
2d83cac9 2763 $this->with_pmed = true;
ceb512d2
RB
2764 return 'pmed';
2765 }
2766
9b8e5fb4 2767 protected function medalJoins()
ceb512d2
RB
2768 {
2769 $joins = array();
2d83cac9 2770 if ($this->with_pmed) {
5c412626 2771 $joins['pmed'] = PlSqlJoin::left('profile_medals', '$ME.pid = $PID');
ceb512d2
RB
2772 }
2773 return $joins;
2774 }
2775
671b7073
RB
2776 /** MENTORING
2777 */
2778
2779 private $pms = array();
459e6f81 2780 private $mjtr = false;
96f01fba
RB
2781 const MENTOR = 1;
2782 const MENTOR_EXPERTISE = 2;
2783 const MENTOR_COUNTRY = 3;
2784 const MENTOR_SECTOR = 4;
2785 const MENTOR_TERM = 5;
671b7073
RB
2786
2787 public function addMentorFilter($type)
2788 {
b8dcf62d 2789 $this->requireAccounts();
671b7073 2790 switch($type) {
96f01fba
RB
2791 case self::MENTOR:
2792 $this->pms['pm'] = 'profile_mentor';
2793 return 'pm';
4a93c3a3
RB
2794 case self::MENTOR_EXPERTISE:
2795 $this->pms['pme'] = 'profile_mentor';
671b7073 2796 return 'pme';
4a93c3a3
RB
2797 case self::MENTOR_COUNTRY:
2798 $this->pms['pmc'] = 'profile_mentor_country';
671b7073 2799 return 'pmc';
4a93c3a3
RB
2800 case self::MENTOR_SECTOR:
2801 $this->pms['pms'] = 'profile_mentor_sector';
671b7073 2802 return 'pms';
459e6f81
PC
2803 case self::MENTOR_TERM:
2804 $this->pms['pmt'] = 'profile_mentor_term';
2805 $this->mjtr = true;
2806 return 'mjtr';
671b7073 2807 default:
5d2e55c7 2808 Platal::page()->killError("Undefined mentor filter.");
671b7073
RB
2809 }
2810 }
2811
9b8e5fb4 2812 protected function mentorJoins()
671b7073
RB
2813 {
2814 $joins = array();
2815 foreach ($this->pms as $sub => $tab) {
5c412626 2816 $joins[$sub] = PlSqlJoin::left($tab, '$ME.pid = $PID');
671b7073 2817 }
459e6f81
PC
2818 if ($this->mjtr) {
2819 $joins['mjtr'] = PlSqlJoin::left('profile_job_term_relation', '$ME.jtid_2 = pmt.jtid');
2820 }
671b7073
RB
2821 return $joins;
2822 }
2823
3f42a6ad
FB
2824 /** CONTACTS
2825 */
2826 private $cts = array();
2827 public function addContactFilter($uid = null)
2828 {
c96da6c1 2829 $this->requireProfiles();
3f42a6ad
FB
2830 return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid);
2831 }
2832
9b8e5fb4 2833 protected function contactJoins()
3f42a6ad
FB
2834 {
2835 $joins = array();
2836 foreach ($this->cts as $sub=>$key) {
2837 if (is_null($key)) {
5c412626 2838 $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.contact = $PID');
3f42a6ad 2839 } else {
5c412626 2840 $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.uid = {?} AND $ME.contact = $PID', substr($key, 5));
3f42a6ad
FB
2841 }
2842 }
2843 return $joins;
2844 }
4e7bf1e0
FB
2845
2846
2847 /** CARNET
2848 */
2849 private $wn = array();
2850 public function addWatchRegistrationFilter($uid = null)
2851 {
b8dcf62d 2852 $this->requireAccounts();
4e7bf1e0
FB
2853 return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid);
2854 }
2855
2856 private $wp = array();
2857 public function addWatchPromoFilter($uid = null)
2858 {
b8dcf62d 2859 $this->requireAccounts();
4e7bf1e0
FB
2860 return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid);
2861 }
2862
2863 private $w = array();
2864 public function addWatchFilter($uid = null)
2865 {
b8dcf62d 2866 $this->requireAccounts();
4e7bf1e0
FB
2867 return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid);
2868 }
2869
9b8e5fb4 2870 protected function watchJoins()
4e7bf1e0
FB
2871 {
2872 $joins = array();
2873 foreach ($this->w as $sub=>$key) {
2874 if (is_null($key)) {
5c412626 2875 $joins['w' . $sub] = PlSqlJoin::left('watch');
4e7bf1e0 2876 } else {
5c412626 2877 $joins['w' . $sub] = PlSqlJoin::left('watch', '$ME.uid = {?}', substr($key, 5));
4e7bf1e0
FB
2878 }
2879 }
2880 foreach ($this->wn as $sub=>$key) {
2881 if (is_null($key)) {
5c412626 2882 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID');
4e7bf1e0 2883 } else {
5c412626 2884 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
4e7bf1e0
FB
2885 }
2886 }
2887 foreach ($this->wn as $sub=>$key) {
2888 if (is_null($key)) {
5c412626 2889 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID');
4e7bf1e0 2890 } else {
5c412626 2891 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
4e7bf1e0
FB
2892 }
2893 }
2894 foreach ($this->wp as $sub=>$key) {
2895 if (is_null($key)) {
5c412626 2896 $joins['wp' . $sub] = PlSqlJoin::left('watch_promo');
4e7bf1e0 2897 } else {
5c412626 2898 $joins['wp' . $sub] = PlSqlJoin::left('watch_promo', '$ME.uid = {?}', substr($key, 5));
4e7bf1e0
FB
2899 }
2900 }
2901 return $joins;
2902 }
48885bbe
FB
2903
2904
470d14f6
FB
2905 /** PHOTOS
2906 */
2907 private $with_photo;
2908 public function addPhotoFilter()
2909 {
2910 $this->requireProfiles();
2911 $this->with_photo = true;
7f26cd69 2912 return 'photo';
470d14f6
FB
2913 }
2914
2915 protected function photoJoins()
2916 {
2917 if ($this->with_photo) {
2918 return array('photo' => PlSqlJoin::left('profile_photos', '$ME.pid = $PID'));
2919 } else {
2920 return array();
2921 }
2922 }
2923
2924
48885bbe
FB
2925 /** MARKETING
2926 */
2927 private $with_rm;
2928 public function addMarketingHash()
2929 {
2930 $this->requireAccounts();
2931 $this->with_rm = true;
2932 }
2933
2934 protected function marketingJoins()
2935 {
2936 if ($this->with_rm) {
5c412626 2937 return array('rm' => PlSqlJoin::left('register_marketing', '$ME.uid = $UID'));
48885bbe
FB
2938 } else {
2939 return array();
2940 }
2941 }
a087cc8d 2942}
8363588b 2943// }}}
3f42a6ad 2944
a7d9ab89
RB
2945// {{{ class ProfileFilter
2946class ProfileFilter extends UserFilter
2947{
434570c4 2948 public function get($limit = null)
a7d9ab89
RB
2949 {
2950 return $this->getProfiles($limit);
2951 }
2daf7250
RB
2952
2953 public function filter(array $profiles, $limit = null)
2954 {
2955 return $this->filterProfiles($profiles, self::defaultLimit($limit));
2956 }
2957
2958 public function getTotalCount()
2959 {
2960 return $this->getTotalProfileCount();
2961 }
ccc951d9
RB
2962
2963 public function getGroups()
2964 {
2965 return $this->getPIDGroups();
2966 }
a7d9ab89
RB
2967}
2968// }}}
2969
a087cc8d
FB
2970// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
2971?>