Deletes all references to sectors as they have been replaced by jobterms.
[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
3ac45f10
PC
987// {{{ class UFC_Job_Terms
988/** Filters users based on the job terms they assigned to one of their
989 * jobs.
990 * @param $val The ID of the job term, or an array of such IDs
991 */
992class UFC_Job_Terms implements UserFilterCondition
993{
994 private $val;
995
996 public function __construct($val)
997 {
998 if (!is_array($val)) {
999 $val = array($val);
1000 }
1001 $this->val = $val;
1002 }
1003
1004 public function buildCondition(PlFilter &$uf)
1005 {
1006 $sub = $uf->addJobTermsFilter(count($this->val));
1007 $conditions = array();
1008 foreach ($this->val as $i => $jtid) {
4ec03752 1009 $conditions[] = $sub[$i] . '.jtid_1 = ' . XDB::escape($jtid);
3ac45f10 1010 }
7f26cd69
RB
1011 $jsub = $uf->addJobFilter();
1012 $conditions[] = $uf->getVisibilityCondition($jsub . '.pub');
3ac45f10
PC
1013 return implode(' AND ', $conditions);
1014 }
1015}
1016// }}}
1017
6a99c3ac
RB
1018// {{{ class UFC_Job_Description
1019/** Filters users based on their job description
1020 * @param $description The text being searched for
da40b2a4 1021 * @param $fields The fields to search for (CV, user-defined)
6a99c3ac 1022 */
4e2f2ad2 1023class UFC_Job_Description implements UserFilterCondition
6a99c3ac
RB
1024{
1025
6a99c3ac
RB
1026 private $description;
1027 private $fields;
1028
01cc5f9e 1029 public function __construct($description, $fields)
6a99c3ac
RB
1030 {
1031 $this->fields = $fields;
1032 $this->description = $description;
1033 }
1034
dcc63ed5 1035 public function buildCondition(PlFilter &$uf)
6a99c3ac
RB
1036 {
1037 $conds = array();
7f26cd69
RB
1038
1039 $jsub = $uf->addJobFilter();
1040 // CV is private => if only CV requested, and not private,
1041 // don't do anything. Otherwise restrict to standard job visibility.
9f169a65
RB
1042 if ($this->fields == UserFilter::JOB_CV) {
1043 if ($uf->getVisibilityLevel() != ProfileVisibility::VIS_PRIVATE) {
1044 return PlFilter::COND_TRUE;
1045 }
1046 } else {
7f26cd69
RB
1047 $conds[] = $uf->getVisibilityCondition($jsub . '.pub');
1048 }
1049
6a99c3ac 1050 if ($this->fields & UserFilter::JOB_USERDEFINED) {
7f26cd69 1051 $conds[] = $jsub . '.description ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
6a99c3ac 1052 }
7f26cd69 1053 if ($this->fields & UserFilter::JOB_CV && $uf->getVisibilityLevel == ProfileVisibility::VIS_PRIVATE) {
01cc5f9e 1054 $uf->requireProfiles();
658b4c83 1055 $conds[] = 'p.cv ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
01cc5f9e 1056 }
6a99c3ac
RB
1057 return implode(' OR ', $conds);
1058 }
1059}
1060// }}}
1061
0a2e9c74
RB
1062// {{{ class UFC_Networking
1063/** Filters users based on network identity (IRC, ...)
1064 * @param $type Type of network (-1 for any)
1065 * @param $value Value to search
1066 */
4e2f2ad2 1067class UFC_Networking implements UserFilterCondition
0a2e9c74
RB
1068{
1069 private $type;
1070 private $value;
1071
1072 public function __construct($type, $value)
1073 {
1074 $this->type = $type;
1075 $this->value = $value;
1076 }
1077
dcc63ed5 1078 public function buildCondition(PlFilter &$uf)
0a2e9c74
RB
1079 {
1080 $sub = $uf->addNetworkingFilter();
1081 $conds = array();
7f26cd69 1082 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
658b4c83 1083 $conds[] = $sub . '.address ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value);
0a2e9c74 1084 if ($this->type != -1) {
1f5cd004 1085 $conds[] = $sub . '.nwid = ' . XDB::format('{?}', $this->type);
0a2e9c74
RB
1086 }
1087 return implode(' AND ', $conds);
1088 }
1089}
1090// }}}
1091
6d62969e
RB
1092// {{{ class UFC_Phone
1093/** Filters users based on their phone number
1094 * @param $num_type Type of number (pro/user/home)
1095 * @param $phone_type Type of phone (fixed/mobile/fax)
1096 * @param $number Phone number
1097 */
4e2f2ad2 1098class UFC_Phone implements UserFilterCondition
6d62969e
RB
1099{
1100 const NUM_PRO = 'pro';
1101 const NUM_USER = 'user';
1102 const NUM_HOME = 'address';
1103 const NUM_ANY = 'any';
1104
1105 const PHONE_FIXED = 'fixed';
1106 const PHONE_MOBILE = 'mobile';
1107 const PHONE_FAX = 'fax';
1108 const PHONE_ANY = 'any';
1109
1110 private $num_type;
1111 private $phone_type;
1112 private $number;
1113
1114 public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY)
1115 {
da0a9c5a 1116 $phone = new Phone(array('display' => $number));
0b6c8b36
SJ
1117 $phone->format();
1118 $this->number = $phone->search();
6d62969e 1119 $this->num_type = $num_type;
0b6c8b36 1120 $this->phone_type = $phone_type;
6d62969e
RB
1121 }
1122
dcc63ed5 1123 public function buildCondition(PlFilter &$uf)
6d62969e
RB
1124 {
1125 $sub = $uf->addPhoneFilter();
1126 $conds = array();
7f26cd69
RB
1127
1128 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1129
6d62969e
RB
1130 $conds[] = $sub . '.search_tel = ' . XDB::format('{?}', $this->number);
1131 if ($this->num_type != self::NUM_ANY) {
1132 $conds[] = $sub . '.link_type = ' . XDB::format('{?}', $this->num_type);
1133 }
1134 if ($this->phone_type != self::PHONE_ANY) {
1135 $conds[] = $sub . '.tel_type = ' . XDB::format('{?}', $this->phone_type);
1136 }
1137 return implode(' AND ', $conds);
1138 }
1139}
1140// }}}
1141
ceb512d2
RB
1142// {{{ class UFC_Medal
1143/** Filters users based on their medals
1144 * @param $medal ID of the medal
1145 * @param $grade Grade of the medal (null for 'any')
1146 */
4e2f2ad2 1147class UFC_Medal implements UserFilterCondition
ceb512d2
RB
1148{
1149 private $medal;
1150 private $grade;
1151
1152 public function __construct($medal, $grade = null)
1153 {
1154 $this->medal = $medal;
1155 $this->grade = $grade;
1156 }
1157
dcc63ed5 1158 public function buildCondition(PlFilter &$uf)
ceb512d2
RB
1159 {
1160 $conds = array();
7f26cd69
RB
1161
1162 // This will require profiles => table 'p' will be available.
ceb512d2 1163 $sub = $uf->addMedalFilter();
7f26cd69
RB
1164
1165 $conds[] = $uf->getVisibilityCondition('p.medals_pub');
1166
ceb512d2
RB
1167 $conds[] = $sub . '.mid = ' . XDB::format('{?}', $this->medal);
1168 if ($this->grade != null) {
1169 $conds[] = $sub . '.gid = ' . XDB::format('{?}', $this->grade);
1170 }
1171 return implode(' AND ', $conds);
1172 }
1173}
1174// }}}
1175
470d14f6
FB
1176// {{{ class UFC_Photo
1177/** Filters profiles with photo
1178 */
1179class UFC_Photo implements UserFilterCondition
1180{
1181 public function buildCondition(PlFilter &$uf)
1182 {
7f26cd69
RB
1183 $sub = $uf->addPhotoFilter();
1184 return $sub . '.attach IS NOT NULL AND ' . $uf->getVisibilityCondition($sub . '.pub');
470d14f6
FB
1185 }
1186}
1187// }}}
1188
96f01fba
RB
1189// {{{ class UFC_Mentor
1190class UFC_Mentor implements UserFilterCondition
1191{
1192 public function buildCondition(PlFilter &$uf)
1193 {
1194 $sub = $uf->addMentorFilter(UserFilter::MENTOR);
1195 return $sub . '.expertise IS NOT NULL';
1196 }
1197}
1198// }}}
1199
1200
671b7073
RB
1201// {{{ class UFC_Mentor_Expertise
1202/** Filters users by mentoring expertise
1203 * @param $expertise Domain of expertise
1204 */
4e2f2ad2 1205class UFC_Mentor_Expertise implements UserFilterCondition
671b7073
RB
1206{
1207 private $expertise;
1208
1209 public function __construct($expertise)
1210 {
1211 $this->expertise = $expertise;
1212 }
1213
dcc63ed5 1214 public function buildCondition(PlFilter &$uf)
671b7073
RB
1215 {
1216 $sub = $uf->addMentorFilter(UserFilter::MENTOR_EXPERTISE);
658b4c83 1217 return $sub . '.expertise ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->expertise);
671b7073
RB
1218 }
1219}
1220// }}}
1221
1222// {{{ class UFC_Mentor_Country
1223/** Filters users by mentoring country
1224 * @param $country Two-letters code of country being searched
1225 */
4e2f2ad2 1226class UFC_Mentor_Country implements UserFilterCondition
671b7073
RB
1227{
1228 private $country;
1229
61d1fd8b 1230 public function __construct()
671b7073 1231 {
61d1fd8b 1232 $this->country = pl_flatten(func_get_args());
671b7073
RB
1233 }
1234
dcc63ed5 1235 public function buildCondition(PlFilter &$uf)
671b7073
RB
1236 {
1237 $sub = $uf->addMentorFilter(UserFilter::MENTOR_COUNTRY);
61d1fd8b 1238 return $sub . '.country IN ' . XDB::format('{?}', $this->country);
671b7073
RB
1239 }
1240}
1241// }}}
1242
459e6f81
PC
1243// {{{ class UFC_Mentor_Terms
1244/** Filters users based on the job terms they used in mentoring.
1245 * @param $val The ID of the job term, or an array of such IDs
1246 */
1247class UFC_Mentor_Terms implements UserFilterCondition
1248{
1249 private $val;
1250
1251 public function __construct($val)
1252 {
1253 $this->val = $val;
1254 }
1255
1256 public function buildCondition(PlFilter &$uf)
1257 {
1258 $sub = $uf->addMentorFilter(UserFilter::MENTOR_TERM);
1259 return $sub . '.jtid_1 = ' . XDB::escape($this->val);
1260 }
1261}
1262// }}}
1263
8363588b 1264// {{{ class UFC_UserRelated
5d2e55c7 1265/** Filters users based on a relation toward a user
53eae167
RB
1266 * @param $user User to which searched users are related
1267 */
4e7bf1e0 1268abstract class UFC_UserRelated implements UserFilterCondition
3f42a6ad 1269{
009b8ab7
FB
1270 protected $user;
1271 public function __construct(PlUser &$user)
1272 {
1273 $this->user =& $user;
3f42a6ad 1274 }
4e7bf1e0 1275}
8363588b 1276// }}}
3f42a6ad 1277
8363588b 1278// {{{ class UFC_Contact
5d2e55c7 1279/** Filters users who belong to selected user's contacts
53eae167 1280 */
4e7bf1e0
FB
1281class UFC_Contact extends UFC_UserRelated
1282{
dcc63ed5 1283 public function buildCondition(PlFilter &$uf)
3f42a6ad 1284 {
009b8ab7 1285 $sub = $uf->addContactFilter($this->user->id());
3f42a6ad
FB
1286 return 'c' . $sub . '.contact IS NOT NULL';
1287 }
1288}
8363588b 1289// }}}
3f42a6ad 1290
8363588b 1291// {{{ class UFC_WatchRegistration
53eae167
RB
1292/** Filters users being watched by selected user
1293 */
4e7bf1e0
FB
1294class UFC_WatchRegistration extends UFC_UserRelated
1295{
dcc63ed5 1296 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1297 {
a87530ea 1298 if (!$this->user->watchType('registration')) {
dcc63ed5 1299 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1300 }
1301 $uids = $this->user->watchUsers();
1302 if (count($uids) == 0) {
dcc63ed5 1303 return PlFilterCondition::COND_FALSE;
009b8ab7 1304 } else {
93b0da52 1305 return XDB::format('$UID IN {?}', $uids);
009b8ab7 1306 }
4e7bf1e0
FB
1307 }
1308}
8363588b 1309// }}}
4e7bf1e0 1310
8363588b 1311// {{{ class UFC_WatchPromo
53eae167
RB
1312/** Filters users belonging to a promo watched by selected user
1313 * @param $user Selected user (the one watching promo)
1314 * @param $grade Formation the user is watching
1315 */
4e7bf1e0
FB
1316class UFC_WatchPromo extends UFC_UserRelated
1317{
1318 private $grade;
009b8ab7 1319 public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING)
4e7bf1e0 1320 {
009b8ab7 1321 parent::__construct($user);
4e7bf1e0
FB
1322 $this->grade = $grade;
1323 }
1324
dcc63ed5 1325 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1326 {
009b8ab7
FB
1327 $promos = $this->user->watchPromos();
1328 if (count($promos) == 0) {
dcc63ed5 1329 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1330 } else {
1331 $sube = $uf->addEducationFilter(true, $this->grade);
1332 $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
bde68f05 1333 return XDB::format($field . ' IN {?}', $promos);
009b8ab7 1334 }
4e7bf1e0
FB
1335 }
1336}
8363588b 1337// }}}
4e7bf1e0 1338
8363588b 1339// {{{ class UFC_WatchContact
53eae167
RB
1340/** Filters users watched by selected user
1341 */
009b8ab7 1342class UFC_WatchContact extends UFC_Contact
4e7bf1e0 1343{
dcc63ed5 1344 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1345 {
009b8ab7 1346 if (!$this->user->watchContacts()) {
dcc63ed5 1347 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1348 }
1349 return parent::buildCondition($uf);
4e7bf1e0
FB
1350 }
1351}
8363588b 1352// }}}
4e7bf1e0 1353
48885bbe
FB
1354// {{{ class UFC_MarketingHash
1355/** Filters users using the hash generated
1356 * to send marketing emails to him.
1357 */
1358class UFC_MarketingHash implements UserFilterCondition
1359{
1360 private $hash;
1361
1362 public function __construct($hash)
1363 {
1364 $this->hash = $hash;
1365 }
1366
1367 public function buildCondition(PlFilter &$uf)
1368 {
1369 $table = $uf->addMarketingHash();
1370 return XDB::format('rm.hash = {?}', $this->hash);
1371 }
1372}
416d4244 1373// }}}
4e7bf1e0 1374
d865c296
FB
1375/******************
1376 * ORDERS
1377 ******************/
1378
8363588b 1379// {{{ class UFO_Promo
5d2e55c7
RB
1380/** Orders users by promotion
1381 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
53eae167
RB
1382 * @param $desc Whether sort is descending
1383 */
ccc951d9 1384class UFO_Promo extends PlFilterGroupableOrder
d865c296
FB
1385{
1386 private $grade;
1387
1388 public function __construct($grade = null, $desc = false)
1389 {
009b8ab7 1390 parent::__construct($desc);
d865c296 1391 $this->grade = $grade;
d865c296
FB
1392 }
1393
61f61261 1394 protected function getSortTokens(PlFilter &$uf)
d865c296
FB
1395 {
1396 if (UserFilter::isGrade($this->grade)) {
1397 $sub = $uf->addEducationFilter($this->grade);
1398 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
1399 } else {
1400 $sub = $uf->addDisplayFilter();
1401 return 'pd' . $sub . '.promo';
1402 }
1403 }
1404}
8363588b 1405// }}}
d865c296 1406
8363588b 1407// {{{ class UFO_Name
53eae167 1408/** Sorts users by name
5d2e55c7
RB
1409 * @param $type Type of name on which to sort (firstname...)
1410 * @param $variant Variant of that name to use (marital, ordinary...)
53eae167
RB
1411 * @param $particle Set to true if particles should be included in the sorting order
1412 * @param $desc If sort order should be descending
1413 */
ccc951d9 1414class UFO_Name extends PlFilterOrder
d865c296
FB
1415{
1416 private $type;
1417 private $variant;
1418 private $particle;
1419
1420 public function __construct($type, $variant = null, $particle = false, $desc = false)
1421 {
009b8ab7 1422 parent::__construct($desc);
d865c296
FB
1423 $this->type = $type;
1424 $this->variant = $variant;
1425 $this->particle = $particle;
d865c296
FB
1426 }
1427
61f61261 1428 protected function getSortTokens(PlFilter &$uf)
d865c296 1429 {
913a4e90 1430 if (Profile::isDisplayName($this->type)) {
d865c296 1431 $sub = $uf->addDisplayFilter();
a9ef52c9
RB
1432 $token = 'pd' . $sub . '.' . $this->type;
1433 if ($uf->accountsRequired()) {
1434 $account_token = Profile::getAccountEquivalentName($this->type);
1435 return 'IFNULL(' . $token . ', a.' . $account_token . ')';
1436 } else {
1437 return $token;
1438 }
d865c296
FB
1439 } else {
1440 $sub = $uf->addNameFilter($this->type, $this->variant);
1441 if ($this->particle) {
1442 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1443 } else {
1444 return 'pn' . $sub . '.name';
1445 }
1446 }
1447 }
1448}
8363588b 1449// }}}
d865c296 1450
40585144 1451// {{{ class UFO_Score
ccc951d9 1452class UFO_Score extends PlFilterOrder
40585144 1453{
61f61261 1454 protected function getSortTokens(PlFilter &$uf)
40585144 1455 {
2a93b634
RB
1456 $toks = $uf->getNameTokens();
1457 $scores = array();
488765e3
RB
1458
1459 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
1460 if (count($toks) == 0) {
1461 return 'NULL';
1462 }
1463
2a93b634
RB
1464 foreach ($toks as $sub => $token) {
1465 $scores[] = XDB::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
1466 }
1467 return implode(' + ', $scores);
40585144
RB
1468 }
1469}
1470// }}}
1471
8363588b 1472// {{{ class UFO_Registration
53eae167
RB
1473/** Sorts users based on registration date
1474 */
ccc951d9 1475class UFO_Registration extends PlFilterOrder
38c6fe96 1476{
61f61261 1477 protected function getSortTokens(PlFilter &$uf)
38c6fe96 1478 {
6c1e97ae 1479 $uf->requireAccounts();
009b8ab7 1480 return 'a.registration_date';
38c6fe96 1481 }
009b8ab7 1482}
8363588b 1483// }}}
38c6fe96 1484
8363588b 1485// {{{ class UFO_Birthday
53eae167
RB
1486/** Sorts users based on next birthday date
1487 */
ccc951d9 1488class UFO_Birthday extends PlFilterOrder
009b8ab7 1489{
61f61261 1490 protected function getSortTokens(PlFilter &$uf)
38c6fe96 1491 {
6c1e97ae 1492 $uf->requireProfiles();
009b8ab7 1493 return 'p.next_birthday';
38c6fe96
FB
1494 }
1495}
8363588b 1496// }}}
38c6fe96 1497
8363588b 1498// {{{ class UFO_ProfileUpdate
53eae167
RB
1499/** Sorts users based on last profile update
1500 */
ccc951d9 1501class UFO_ProfileUpdate extends PlFilterOrder
009b8ab7 1502{
61f61261 1503 protected function getSortTokens(PlFilter &$uf)
009b8ab7 1504 {
6c1e97ae 1505 $uf->requireProfiles();
009b8ab7
FB
1506 return 'p.last_change';
1507 }
1508}
8363588b 1509// }}}
009b8ab7 1510
8363588b 1511// {{{ class UFO_Death
53eae167
RB
1512/** Sorts users based on death date
1513 */
ccc951d9 1514class UFO_Death extends PlFilterOrder
009b8ab7 1515{
61f61261 1516 protected function getSortTokens(PlFilter &$uf)
009b8ab7 1517 {
6c1e97ae 1518 $uf->requireProfiles();
009b8ab7
FB
1519 return 'p.deathdate';
1520 }
1521}
8363588b 1522// }}}
009b8ab7 1523
6c1e97ae
FB
1524// {{{ class UFO_Uid
1525/** Sorts users based on their uid
1526 */
ccc951d9 1527class UFO_Uid extends PlFilterOrder
6c1e97ae
FB
1528{
1529 protected function getSortTokens(PlFilter &$uf)
1530 {
1531 $uf->requireAccounts();
93b0da52 1532 return '$UID';
6c1e97ae
FB
1533 }
1534}
c752a130 1535// }}}
6c1e97ae
FB
1536
1537// {{{ class UFO_Hruid
1538/** Sorts users based on their hruid
1539 */
ccc951d9 1540class UFO_Hruid extends PlFilterOrder
6c1e97ae
FB
1541{
1542 protected function getSortTokens(PlFilter &$uf)
1543 {
1544 $uf->requireAccounts();
1545 return 'a.hruid';
1546 }
1547}
1548// }}}
1549
1550// {{{ class UFO_Pid
1551/** Sorts users based on their pid
1552 */
ccc951d9 1553class UFO_Pid extends PlFilterOrder
6c1e97ae
FB
1554{
1555 protected function getSortTokens(PlFilter &$uf)
1556 {
1557 $uf->requireProfiles();
93b0da52 1558 return '$PID';
6c1e97ae
FB
1559 }
1560}
c752a130 1561// }}}
6c1e97ae
FB
1562
1563// {{{ class UFO_Hrpid
1564/** Sorts users based on their hrpid
1565 */
ccc951d9 1566class UFO_Hrpid extends PlFilterOrder
6c1e97ae
FB
1567{
1568 protected function getSortTokens(PlFilter &$uf)
1569 {
1570 $uf->requireProfiles();
1571 return 'p.hrpid';
1572 }
1573}
1574// }}}
1575
009b8ab7 1576
d865c296
FB
1577/***********************************
1578 *********************************
1579 USER FILTER CLASS
1580 *********************************
1581 ***********************************/
1582
8363588b 1583// {{{ class UserFilter
2d83cac9
RB
1584/** This class provides a convenient and centralized way of filtering users.
1585 *
1586 * Usage:
1587 * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t));
1588 *
1589 * Resulting UserFilter can be used to:
1590 * - get a list of User objects matching the filter
1591 * - get a list of UIDs matching the filter
1592 * - get the number of users matching the filter
1593 * - check whether a given User matches the filter
1594 * - filter a list of User objects depending on whether they match the filter
1595 *
1596 * Usage for UFC and UFO objects:
1597 * A UserFilter will call all private functions named XXXJoins.
1598 * These functions must return an array containing the list of join
1599 * required by the various UFC and UFO associated to the UserFilter.
1600 * Entries in those returned array are of the following form:
1601 * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter')
1602 * which will be translated into :
1603 * join_type JOIN joined_table AS join_tablealias ON (join_criter)
1604 * in the final query.
1605 *
1606 * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with
913a4e90 1607 * profile.pid, and $UID with accounts.uid.
2d83cac9
RB
1608 *
1609 * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined;
1610 * its parameter will be used to set various private vars of the UserFilter describing
1611 * the required joins ; such a function shall return the "join_tablealias" to use
1612 * when referring to the joined table.
1613 *
1614 * For example, if data from profile_job must be available to filter results,
aab2ffdd 1615 * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and
2d83cac9
RB
1616 * return 'pj', the short name to use when referring to profile_job; when building
1617 * the query, calling the jobJoins function will return an array containing a single
1618 * row:
1619 * 'pj' => array('left', 'profile_job', '$ME.pid = $UID');
1620 *
1621 * The 'register_optional' function can be used to generate unique table aliases when
1622 * the same table has to be joined several times with different aliases.
1623 */
9b8e5fb4 1624class UserFilter extends PlFilter
a087cc8d 1625{
9b8e5fb4 1626 protected $joinMethods = array();
7ca75030 1627
61f61261
RB
1628 protected $joinMetas = array(
1629 '$PID' => 'p.pid',
1630 '$UID' => 'a.uid',
9b8e5fb4 1631 );
d865c296 1632
a087cc8d 1633 private $root;
24e08e33 1634 private $sort = array();
ccc951d9 1635 private $grouper = null;
784745ce 1636 private $query = null;
24e08e33 1637 private $orderby = null;
784745ce 1638
7f26cd69
RB
1639 // Store the current 'search' visibility.
1640 private $profile_visibility = null;
1641
2daf7250
RB
1642 private $lastusercount = null;
1643 private $lastprofilecount = null;
d865c296 1644
24e08e33 1645 public function __construct($cond = null, $sort = null)
5dd9d823 1646 {
06598c13 1647 if (empty($this->joinMethods)) {
d865c296
FB
1648 $class = new ReflectionClass('UserFilter');
1649 foreach ($class->getMethods() as $method) {
1650 $name = $method->getName();
1651 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
06598c13 1652 $this->joinMethods[] = $name;
d865c296
FB
1653 }
1654 }
1655 }
5dd9d823 1656 if (!is_null($cond)) {
06598c13 1657 if ($cond instanceof PlFilterCondition) {
5dd9d823
FB
1658 $this->setCondition($cond);
1659 }
1660 }
24e08e33 1661 if (!is_null($sort)) {
ccc951d9 1662 if ($sort instanceof PlFilterOrder) {
24e08e33 1663 $this->addSort($sort);
d865c296
FB
1664 } else if (is_array($sort)) {
1665 foreach ($sort as $s) {
1666 $this->addSort($s);
1667 }
24e08e33
FB
1668 }
1669 }
7f26cd69
RB
1670
1671 // This will set the visibility to the default correct level.
1672 $this->profile_visibility = new ProfileVisibility();
1673 }
1674
1675 public function getVisibilityLevels()
1676 {
1677 return $this->profile_visibility->levels();
1678 }
1679
1680 public function getVisibilityLevel()
1681 {
1682 return $this->profile_visibility->level();
1683 }
1684
1685 public function restrictVisibilityTo($level)
1686 {
1687 $this->profile_visibility->setLevel($level);
1688 }
1689
1690 public function getVisibilityCondition($field)
1691 {
1692 return $field . ' IN ' . XDB::formatArray($this->getVisibilityLevels());
5dd9d823
FB
1693 }
1694
784745ce
FB
1695 private function buildQuery()
1696 {
2a93b634
RB
1697 // The root condition is built first because some orders need info
1698 // available only once all UFC have set their conditions (UFO_Score)
1699 if (is_null($this->query)) {
1700 $where = $this->root->buildCondition($this);
226626ae
FB
1701 $where = str_replace(array_keys($this->joinMetas),
1702 $this->joinMetas,
1703 $where);
2a93b634 1704 }
d865c296
FB
1705 if (is_null($this->orderby)) {
1706 $orders = array();
1707 foreach ($this->sort as $sort) {
1708 $orders = array_merge($orders, $sort->buildSort($this));
1709 }
1710 if (count($orders) == 0) {
1711 $this->orderby = '';
1712 } else {
1713 $this->orderby = 'ORDER BY ' . implode(', ', $orders);
1714 }
226626ae
FB
1715 $this->orderby = str_replace(array_keys($this->joinMetas),
1716 $this->joinMetas,
1717 $this->orderby);
d865c296 1718 }
784745ce 1719 if (is_null($this->query)) {
2a93b634 1720 if ($this->with_accounts) {
b8dcf62d
RB
1721 $from = 'accounts AS a';
1722 } else {
1723 $this->requireProfiles();
1724 $from = 'profiles AS p';
1725 }
f7ea7450 1726 $joins = $this->buildJoins();
b8dcf62d 1727 $this->query = 'FROM ' . $from . '
784745ce
FB
1728 ' . $joins . '
1729 WHERE (' . $where . ')';
1730 }
1731 }
1732
ccc951d9
RB
1733 public function hasGroups()
1734 {
1735 return $this->grouper != null;
1736 }
1737
1738 public function getGroups()
1739 {
1740 return $this->getUIDGroups();
1741 }
1742
1743 public function getUIDGroups()
1744 {
1745 $this->requireAccounts();
1746 $this->buildQuery();
1747 $token = $this->grouper->getGroupToken($this);
1748
1749 $groups = XDB::fetchAllRow('SELECT ' . $token . ', COUNT(a.uid)
1750 ' . $this->query . '
1751 GROUP BY ' . $token,
1752 0);
1753 return $groups;
1754 }
1755
1756 public function getPIDGroups()
1757 {
1758 $this->requireProfiles();
1759 $this->buildQuery();
1760 $token = $this->grouper->getGroupToken($this);
1761
1762 $groups = XDB::fetchAllRow('SELECT ' . $token . ', COUNT(p.pid)
1763 ' . $this->query . '
1764 GROUP BY ' . $token,
1765 0);
1766 return $groups;
1767 }
1768
7ca75030 1769 private function getUIDList($uids = null, PlLimit &$limit)
d865c296 1770 {
b8dcf62d 1771 $this->requireAccounts();
d865c296 1772 $this->buildQuery();
7ca75030 1773 $lim = $limit->getSql();
d865c296 1774 $cond = '';
45b20ca0 1775 if (!empty($uids)) {
bde68f05 1776 $cond = XDB::format(' AND a.uid IN {?}', $uids);
d865c296
FB
1777 }
1778 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1779 ' . $this->query . $cond . '
1780 GROUP BY a.uid
1781 ' . $this->orderby . '
7ca75030 1782 ' . $lim);
2daf7250 1783 $this->lastusercount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
d865c296
FB
1784 return $fetched;
1785 }
1786
043b104b
RB
1787 private function getPIDList($pids = null, PlLimit &$limit)
1788 {
1789 $this->requireProfiles();
1790 $this->buildQuery();
1791 $lim = $limit->getSql();
1792 $cond = '';
1793 if (!is_null($pids)) {
bde68f05 1794 $cond = XDB::format(' AND p.pid IN {?}', $pids);
043b104b
RB
1795 }
1796 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid
1797 ' . $this->query . $cond . '
1798 GROUP BY p.pid
1799 ' . $this->orderby . '
1800 ' . $lim);
2daf7250 1801 $this->lastprofilecount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
043b104b
RB
1802 return $fetched;
1803 }
1804
434570c4
FB
1805 private static function defaultLimit($limit) {
1806 if ($limit == null) {
1807 return new PlLimit();
1808 } else {
1809 return $limit;
1810 }
1811 }
1812
a087cc8d
FB
1813 /** Check that the user match the given rule.
1814 */
1815 public function checkUser(PlUser &$user)
1816 {
b8dcf62d 1817 $this->requireAccounts();
784745ce
FB
1818 $this->buildQuery();
1819 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1820 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
1821 return $count == 1;
a087cc8d
FB
1822 }
1823
043b104b
RB
1824 /** Check that the profile match the given rule.
1825 */
1826 public function checkProfile(Profile &$profile)
1827 {
1828 $this->requireProfiles();
1829 $this->buildQuery();
1830 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1831 ' . $this->query . XDB::format(' AND p.pid = {?}', $profile->id()));
1832 return $count == 1;
1833 }
1834
1835 /** Default filter is on users
a087cc8d 1836 */
434570c4 1837 public function filter(array $users, $limit = null)
a087cc8d 1838 {
434570c4 1839 return $this->filterUsers($users, self::defaultLimit($limit));
043b104b
RB
1840 }
1841
1842 /** Filter a list of users to extract the users matching the rule.
1843 */
434570c4 1844 public function filterUsers(array $users, $limit = null)
043b104b 1845 {
434570c4 1846 $limit = self::defaultLimit($limit);
b8dcf62d 1847 $this->requireAccounts();
4927ee54
FB
1848 $this->buildQuery();
1849 $table = array();
1850 $uids = array();
1851 foreach ($users as $user) {
07eb5b0e
FB
1852 if ($user instanceof PlUser) {
1853 $uid = $user->id();
1854 } else {
1855 $uid = $user;
1856 }
1857 $uids[] = $uid;
1858 $table[$uid] = $user;
4927ee54 1859 }
7ca75030 1860 $fetched = $this->getUIDList($uids, $limit);
a087cc8d 1861 $output = array();
4927ee54
FB
1862 foreach ($fetched as $uid) {
1863 $output[] = $table[$uid];
a087cc8d
FB
1864 }
1865 return $output;
1866 }
1867
043b104b
RB
1868 /** Filter a list of profiles to extract the users matching the rule.
1869 */
434570c4 1870 public function filterProfiles(array $profiles, $limit = null)
043b104b 1871 {
434570c4 1872 $limit = self::defaultLimit($limit);
043b104b
RB
1873 $this->requireProfiles();
1874 $this->buildQuery();
1875 $table = array();
1876 $pids = array();
1877 foreach ($profiles as $profile) {
1878 if ($profile instanceof Profile) {
1879 $pid = $profile->id();
1880 } else {
1881 $pid = $profile;
1882 }
1883 $pids[] = $pid;
1884 $table[$pid] = $profile;
1885 }
1886 $fetched = $this->getPIDList($pids, $limit);
1887 $output = array();
1888 foreach ($fetched as $pid) {
1889 $output[] = $table[$pid];
1890 }
1891 return $output;
1892 }
1893
434570c4 1894 public function getUIDs($limit = null)
7ca75030 1895 {
833a6e86
FB
1896 $limit = self::defaultLimit($limit);
1897 return $this->getUIDList(null, $limit);
7ca75030
RB
1898 }
1899
ad27b22e
FB
1900 public function getUID($pos = 0)
1901 {
983f3864 1902 $uids =$this->getUIDList(null, new PlLimit(1, $pos));
ad27b22e
FB
1903 if (count($uids) == 0) {
1904 return null;
1905 } else {
1906 return $uids[0];
1907 }
1908 }
1909
434570c4 1910 public function getPIDs($limit = null)
043b104b 1911 {
833a6e86
FB
1912 $limit = self::defaultLimit($limit);
1913 return $this->getPIDList(null, $limit);
043b104b
RB
1914 }
1915
ad27b22e
FB
1916 public function getPID($pos = 0)
1917 {
983f3864 1918 $pids =$this->getPIDList(null, new PlLimit(1, $pos));
ad27b22e
FB
1919 if (count($pids) == 0) {
1920 return null;
1921 } else {
1922 return $pids[0];
1923 }
1924 }
1925
434570c4 1926 public function getUsers($limit = null)
4927ee54 1927 {
7ca75030 1928 return User::getBulkUsersWithUIDs($this->getUIDs($limit));
d865c296
FB
1929 }
1930
ad27b22e
FB
1931 public function getUser($pos = 0)
1932 {
1933 $uid = $this->getUID($pos);
1934 if ($uid == null) {
1935 return null;
1936 } else {
1937 return User::getWithUID($uid);
1938 }
1939 }
1940
0d906109
RB
1941 public function iterUsers($limit = null)
1942 {
1943 return User::iterOverUIDs($this->getUIDs($limit));
1944 }
1945
00f83317 1946 public function getProfiles($limit = null, $fields = 0x0000, $visibility = null)
043b104b 1947 {
00f83317 1948 return Profile::getBulkProfilesWithPIDs($this->getPIDs($limit), $fields, $visibility);
043b104b
RB
1949 }
1950
00f83317 1951 public function getProfile($pos = 0, $fields = 0x0000, $visibility = null)
ad27b22e
FB
1952 {
1953 $pid = $this->getPID($pos);
1954 if ($pid == null) {
1955 return null;
1956 } else {
00f83317 1957 return Profile::get($pid, $fields, $visibility);
ad27b22e
FB
1958 }
1959 }
1960
00f83317 1961 public function iterProfiles($limit = null, $fields = 0x0000, $visibility = null)
0d906109 1962 {
00f83317 1963 return Profile::iterOverPIDs($this->getPIDs($limit), true, $fields, $visibility);
0d906109
RB
1964 }
1965
434570c4 1966 public function get($limit = null)
d865c296 1967 {
7ca75030 1968 return $this->getUsers($limit);
4927ee54
FB
1969 }
1970
aaf70eb8 1971
d865c296 1972 public function getTotalCount()
4927ee54 1973 {
2daf7250
RB
1974 return $this->getTotalUserCount();
1975 }
1976
1977 public function getTotalUserCount()
1978 {
1979 if (is_null($this->lastusercount)) {
1980 $this->requireAccounts();
aa21c568 1981 $this->buildQuery();
2daf7250
RB
1982 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
1983 ' . $this->query);
1984 } else {
1985 return $this->lastusercount;
1986 }
1987 }
1988
1989 public function getTotalProfileCount()
1990 {
1991 if (is_null($this->lastprofilecount)) {
1992 $this->requireProfiles();
1993 $this->buildQuery();
1994 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT p.pid)
7e735012 1995 ' . $this->query);
38c6fe96 1996 } else {
2daf7250 1997 return $this->lastprofilecount;
38c6fe96 1998 }
4927ee54
FB
1999 }
2000
9b8e5fb4 2001 public function setCondition(PlFilterCondition &$cond)
a087cc8d
FB
2002 {
2003 $this->root =& $cond;
784745ce 2004 $this->query = null;
a087cc8d
FB
2005 }
2006
9b8e5fb4 2007 public function addSort(PlFilterOrder &$sort)
24e08e33 2008 {
ccc951d9
RB
2009 if (count($this->sort) == 0 && $sort instanceof PlFilterGroupableOrder)
2010 {
2011 $this->grouper = $sort;
2012 }
d865c296
FB
2013 $this->sort[] = $sort;
2014 $this->orderby = null;
24e08e33
FB
2015 }
2016
a087cc8d
FB
2017 static public function getLegacy($promo_min, $promo_max)
2018 {
a087cc8d 2019 if ($promo_min != 0) {
784745ce 2020 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
5dd9d823 2021 } else {
88c31faf 2022 $min = new PFC_True();
a087cc8d 2023 }
a087cc8d 2024 if ($promo_max != 0) {
784745ce 2025 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
a087cc8d 2026 } else {
88c31faf 2027 $max = new PFC_True();
a087cc8d 2028 }
9b8e5fb4 2029 return new UserFilter(new PFC_And($min, $max));
a087cc8d 2030 }
784745ce 2031
07eb5b0e
FB
2032 static public function sortByName()
2033 {
913a4e90 2034 return array(new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME));
07eb5b0e
FB
2035 }
2036
2037 static public function sortByPromo()
2038 {
913a4e90 2039 return array(new UFO_Promo(), new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME));
07eb5b0e
FB
2040 }
2041
aa21c568
FB
2042 static private function getDBSuffix($string)
2043 {
2d6329a2
FB
2044 if (is_array($string)) {
2045 if (count($string) == 1) {
2046 return self::getDBSuffix(array_pop($string));
2047 }
2048 return md5(implode('|', $string));
2049 } else {
2050 return preg_replace('/[^a-z0-9]/i', '', $string);
2051 }
aa21c568
FB
2052 }
2053
2054
2d83cac9
RB
2055 /** Stores a new (and unique) table alias in the &$table table
2056 * @param &$table Array in which the table alias must be stored
2057 * @param $val Value which will then be used to build the join
2058 * @return Name of the newly created alias
2059 */
aa21c568
FB
2060 private $option = 0;
2061 private function register_optional(array &$table, $val)
2062 {
2063 if (is_null($val)) {
2064 $sub = $this->option++;
2065 $index = null;
2066 } else {
2067 $sub = self::getDBSuffix($val);
2068 $index = $val;
2069 }
2070 $sub = '_' . $sub;
2071 $table[$sub] = $index;
2072 return $sub;
2073 }
784745ce 2074
b8dcf62d
RB
2075 /** PROFILE VS ACCOUNT
2076 */
f7ea7450
RB
2077 private $with_profiles = false;
2078 private $with_accounts = false;
b8dcf62d
RB
2079 public function requireAccounts()
2080 {
2081 $this->with_accounts = true;
2082 }
2083
a9ef52c9
RB
2084 public function accountsRequired()
2085 {
2086 return $this->with_accounts;
2087 }
2088
b8dcf62d
RB
2089 public function requireProfiles()
2090 {
2091 $this->with_profiles = true;
2092 }
2093
a9ef52c9
RB
2094 public function profilesRequired()
2095 {
2096 return $this->with_profiles;
2097 }
2098
b8dcf62d
RB
2099 protected function accountJoins()
2100 {
2101 $joins = array();
2a93b634 2102 if ($this->with_profiles && $this->with_accounts) {
5c412626
FB
2103 $joins['ap'] = PlSqlJoin::left('account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)');
2104 $joins['p'] = PlSqlJoin::left('profiles', '$PID = ap.pid');
b8dcf62d
RB
2105 }
2106 return $joins;
2107 }
2108
d865c296
FB
2109 /** DISPLAY
2110 */
38c6fe96 2111 const DISPLAY = 'display';
d865c296
FB
2112 private $pd = false;
2113 public function addDisplayFilter()
2114 {
b8dcf62d 2115 $this->requireProfiles();
d865c296
FB
2116 $this->pd = true;
2117 return '';
2118 }
2119
9b8e5fb4 2120 protected function displayJoins()
d865c296
FB
2121 {
2122 if ($this->pd) {
5c412626 2123 return array('pd' => PlSqlJoin::left('profile_display', '$ME.pid = $PID'));
d865c296
FB
2124 } else {
2125 return array();
2126 }
2127 }
2128
f73a4f1b
RB
2129 /** LOGGER
2130 */
2131
2132 private $with_logger = false;
2133 public function addLoggerFilter()
2134 {
2135 $this->with_logger = true;
2136 $this->requireAccounts();
2137 return 'ls';
2138 }
2139 protected function loggerJoins()
2140 {
2141 $joins = array();
2142 if ($this->with_logger) {
5c412626 2143 $joins['ls'] = PlSqlJoin::left('log_sessions', '$ME.uid = $UID');
f73a4f1b
RB
2144 }
2145 return $joins;
2146 }
2147
784745ce
FB
2148 /** NAMES
2149 */
784745ce
FB
2150
2151 static public function assertName($name)
2152 {
07613cdd 2153 if (!DirEnum::getID(DirEnum::NAMETYPES, $name)) {
9b8e5fb4 2154 Platal::page()->kill('Invalid name type: ' . $name);
784745ce
FB
2155 }
2156 }
2157
2158 private $pn = array();
784745ce
FB
2159 public function addNameFilter($type, $variant = null)
2160 {
b8dcf62d 2161 $this->requireProfiles();
784745ce
FB
2162 if (!is_null($variant)) {
2163 $ft = $type . '_' . $variant;
2164 } else {
2165 $ft = $type;
2166 }
2167 $sub = '_' . $ft;
2168 self::assertName($ft);
2169
2170 if (!is_null($variant) && $variant == 'other') {
aa21c568 2171 $sub .= $this->option++;
784745ce 2172 }
07613cdd 2173 $this->pn[$sub] = DirEnum::getID(DirEnum::NAMETYPES, $ft);
784745ce
FB
2174 return $sub;
2175 }
2176
9b8e5fb4 2177 protected function nameJoins()
784745ce
FB
2178 {
2179 $joins = array();
2180 foreach ($this->pn as $sub => $type) {
5c412626 2181 $joins['pn' . $sub] = PlSqlJoin::left('profile_name', '$ME.pid = $PID AND $ME.typeid = {?}', $type);
784745ce
FB
2182 }
2183 return $joins;
2184 }
2185
40585144
RB
2186 /** NAMETOKENS
2187 */
2a93b634
RB
2188 private $name_tokens = array();
2189 private $nb_tokens = 0;
2190
2191 public function addNameTokensFilter($token)
40585144
RB
2192 {
2193 $this->requireProfiles();
2a93b634
RB
2194 $sub = 'sn' . (1 + $this->nb_tokens);
2195 $this->nb_tokens++;
2196 $this->name_tokens[$sub] = $token;
2197 return $sub;
40585144
RB
2198 }
2199
2200 protected function nameTokensJoins()
2201 {
f7ea7450 2202 /* We don't return joins, since with_sn forces the SELECT to run on search_name first */
2a93b634
RB
2203 $joins = array();
2204 foreach ($this->name_tokens as $sub => $token) {
2205 $joins[$sub] = PlSqlJoin::left('search_name', '$ME.pid = $PID');
40585144 2206 }
2a93b634
RB
2207 return $joins;
2208 }
2209
2210 public function getNameTokens()
2211 {
2212 return $this->name_tokens;
40585144
RB
2213 }
2214
a7f8e48a
RB
2215 /** NATIONALITY
2216 */
2217
2218 private $with_nat = false;
2219 public function addNationalityFilter()
2220 {
2221 $this->with_nat = true;
2222 return 'ngc';
2223 }
2224
2225 protected function nationalityJoins()
2226 {
2227 $joins = array();
2228 if ($this->with_nat) {
5c412626 2229 $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
2230 }
2231 return $joins;
2232 }
2233
784745ce
FB
2234 /** EDUCATION
2235 */
2236 const GRADE_ING = 'Ing.';
2237 const GRADE_PHD = 'PhD';
2238 const GRADE_MST = 'M%';
2239 static public function isGrade($grade)
2240 {
93c2f133 2241 return ($grade !== 0) && ($grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST);
784745ce
FB
2242 }
2243
2244 static public function assertGrade($grade)
2245 {
2246 if (!self::isGrade($grade)) {
ad27b22e 2247 Platal::page()->killError("Diplôme non valide: $grade");
784745ce
FB
2248 }
2249 }
2250
d865c296
FB
2251 static public function promoYear($grade)
2252 {
2253 // XXX: Definition of promotion for phds and masters might change in near future.
2254 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
2255 }
2256
784745ce
FB
2257 private $pepe = array();
2258 private $with_pee = false;
784745ce
FB
2259 public function addEducationFilter($x = false, $grade = null)
2260 {
b8dcf62d 2261 $this->requireProfiles();
784745ce 2262 if (!$x) {
aa21c568
FB
2263 $index = $this->option;
2264 $sub = $this->option++;
784745ce
FB
2265 } else {
2266 self::assertGrade($grade);
2267 $index = $grade;
2268 $sub = $grade[0];
2269 $this->with_pee = true;
2270 }
2271 $sub = '_' . $sub;
2272 $this->pepe[$index] = $sub;
2273 return $sub;
2274 }
2275
9b8e5fb4 2276 protected function educationJoins()
784745ce
FB
2277 {
2278 $joins = array();
2279 if ($this->with_pee) {
5c412626 2280 $joins['pee'] = PlSqlJoin::inner('profile_education_enum', 'pee.abbreviation = \'X\'');
784745ce
FB
2281 }
2282 foreach ($this->pepe as $grade => $sub) {
2283 if ($this->isGrade($grade)) {
5c412626
FB
2284 $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID');
2285 $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE {?}', $grade);
784745ce 2286 } else {
5c412626
FB
2287 $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.pid = $PID');
2288 $joins['pee' . $sub] = PlSqlJoin::inner('profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
2289 $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
784745ce
FB
2290 }
2291 }
2292 return $joins;
2293 }
4927ee54
FB
2294
2295
2296 /** GROUPS
2297 */
2298 private $gpm = array();
4927ee54
FB
2299 public function addGroupFilter($group = null)
2300 {
b8dcf62d 2301 $this->requireAccounts();
4927ee54 2302 if (!is_null($group)) {
4aae4d2c 2303 if (is_int($group) || ctype_digit($group)) {
4927ee54
FB
2304 $index = $sub = $group;
2305 } else {
2306 $index = $group;
aa21c568 2307 $sub = self::getDBSuffix($group);
4927ee54
FB
2308 }
2309 } else {
aa21c568 2310 $sub = 'group_' . $this->option++;
4927ee54
FB
2311 $index = null;
2312 }
2313 $sub = '_' . $sub;
2314 $this->gpm[$sub] = $index;
2315 return $sub;
2316 }
2317
9b8e5fb4 2318 protected function groupJoins()
4927ee54
FB
2319 {
2320 $joins = array();
2321 foreach ($this->gpm as $sub => $key) {
2322 if (is_null($key)) {
5c412626
FB
2323 $joins['gpa' . $sub] = PlSqlJoin::inner('groups');
2324 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
4aae4d2c 2325 } else if (is_int($key) || ctype_digit($key)) {
5c412626 2326 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
4927ee54 2327 } else {
5c412626
FB
2328 $joins['gpa' . $sub] = PlSqlJoin::inner('groups', '$ME.diminutif = {?}', $key);
2329 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
4927ee54
FB
2330 }
2331 }
2332 return $joins;
0fb3713c
RB
2333 }
2334
2335 /** BINETS
2336 */
2337
a7f8e48a
RB
2338 private $with_bi = false;
2339 private $with_bd = false;
d7ddf29b 2340 public function addBinetsFilter($with_enum = false)
0fb3713c
RB
2341 {
2342 $this->requireProfiles();
a7f8e48a
RB
2343 $this->with_bi = true;
2344 if ($with_enum) {
2345 $this->with_bd = true;
2346 return 'bd';
2347 } else {
2348 return 'bi';
2349 }
0fb3713c
RB
2350 }
2351
2352 protected function binetsJoins()
2353 {
2354 $joins = array();
a7f8e48a 2355 if ($this->with_bi) {
5c412626 2356 $joins['bi'] = PlSqlJoin::left('profile_binets', '$ME.pid = $PID');
0fb3713c 2357 }
a7f8e48a 2358 if ($this->with_bd) {
5c412626 2359 $joins['bd'] = PlSqlJoin::left('profile_binet_enum', '$ME.id = bi.binet_id');
a7f8e48a 2360 }
0fb3713c 2361 return $joins;
4927ee54 2362 }
aa21c568
FB
2363
2364 /** EMAILS
2365 */
2366 private $e = array();
2367 public function addEmailRedirectFilter($email = null)
2368 {
b8dcf62d 2369 $this->requireAccounts();
aa21c568
FB
2370 return $this->register_optional($this->e, $email);
2371 }
2372
2373 private $ve = array();
2374 public function addVirtualEmailFilter($email = null)
2375 {
21401768 2376 $this->addAliasFilter(self::ALIAS_FORLIFE);
aa21c568
FB
2377 return $this->register_optional($this->ve, $email);
2378 }
2379
21401768
FB
2380 const ALIAS_BEST = 'bestalias';
2381 const ALIAS_FORLIFE = 'forlife';
aa21c568
FB
2382 private $al = array();
2383 public function addAliasFilter($alias = null)
2384 {
b8dcf62d 2385 $this->requireAccounts();
aa21c568
FB
2386 return $this->register_optional($this->al, $alias);
2387 }
2388
9b8e5fb4 2389 protected function emailJoins()
aa21c568
FB
2390 {
2391 global $globals;
2392 $joins = array();
2393 foreach ($this->e as $sub=>$key) {
2394 if (is_null($key)) {
5c412626 2395 $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
aa21c568 2396 } else {
2d6329a2
FB
2397 if (!is_array($key)) {
2398 $key = array($key);
2399 }
aab2ffdd 2400 $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'
2f1c94e0 2401 AND $ME.email IN {?}', $key);
aa21c568
FB
2402 }
2403 }
21401768 2404 foreach ($this->al as $sub=>$key) {
aa21c568 2405 if (is_null($key)) {
5c412626 2406 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
21401768 2407 } else if ($key == self::ALIAS_BEST) {
5c412626 2408 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
21401768 2409 } else if ($key == self::ALIAS_FORLIFE) {
5c412626 2410 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type = \'a_vie\'');
aa21c568 2411 } else {
2d6329a2
FB
2412 if (!is_array($key)) {
2413 $key = array($key);
2414 }
aab2ffdd 2415 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')
bde68f05 2416 AND $ME.alias IN {?}', $key);
aa21c568 2417 }
aa21c568 2418 }
21401768 2419 foreach ($this->ve as $sub=>$key) {
aa21c568 2420 if (is_null($key)) {
5c412626 2421 $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\'');
aa21c568 2422 } else {
2d6329a2
FB
2423 if (!is_array($key)) {
2424 $key = array($key);
2425 }
bde68f05 2426 $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\' AND $ME.alias IN {?}', $key);
aa21c568 2427 }
5c412626
FB
2428 $joins['vr' . $sub] = PlSqlJoin::left('virtual_redirect',
2429 '$ME.vid = v' . $sub . '.vid
2430 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
2431 CONCAT(al_forlife.alias, \'@\', {?}),
2432 a.email))',
2433 $globals->mail->domain, $globals->mail->domain2);
aa21c568
FB
2434 }
2435 return $joins;
2436 }
3f42a6ad
FB
2437
2438
c4b24511
RB
2439 /** ADDRESSES
2440 */
036d1637 2441 private $with_pa = false;
c4b24511
RB
2442 public function addAddressFilter()
2443 {
b8dcf62d 2444 $this->requireProfiles();
036d1637
RB
2445 $this->with_pa = true;
2446 return 'pa';
c4b24511
RB
2447 }
2448
2b9ca54d
RB
2449 private $with_pac = false;
2450 public function addAddressCountryFilter()
2451 {
2452 $this->requireProfiles();
2453 $this->addAddressFilter();
2454 $this->with_pac = true;
2455 return 'gc';
2456 }
2457
d7ddf29b 2458 private $with_pal = false;
2b9ca54d
RB
2459 public function addAddressLocalityFilter()
2460 {
2461 $this->requireProfiles();
2462 $this->addAddressFilter();
2463 $this->with_pal = true;
2464 return 'gl';
2465 }
2466
9b8e5fb4 2467 protected function addressJoins()
c4b24511
RB
2468 {
2469 $joins = array();
036d1637 2470 if ($this->with_pa) {
5c412626 2471 $joins['pa'] = PlSqlJoin::left('profile_addresses', '$ME.pid = $PID');
c4b24511 2472 }
2b9ca54d 2473 if ($this->with_pac) {
5c412626 2474 $joins['gc'] = PlSqlJoin::left('geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID');
2b9ca54d
RB
2475 }
2476 if ($this->with_pal) {
5c412626 2477 $joins['gl'] = PlSqlJoin::left('geoloc_localities', '$ME.id = pa.localityID');
2b9ca54d 2478 }
c4b24511
RB
2479 return $joins;
2480 }
2481
2482
4083b126
RB
2483 /** CORPS
2484 */
2485
2486 private $pc = false;
2487 private $pce = array();
2488 private $pcr = false;
2489 public function addCorpsFilter($type)
2490 {
b8dcf62d 2491 $this->requireProfiles();
4083b126
RB
2492 $this->pc = true;
2493 if ($type == UFC_Corps::CURRENT) {
2494 $pce['pcec'] = 'current_corpsid';
2495 return 'pcec';
2496 } else if ($type == UFC_Corps::ORIGIN) {
2497 $pce['pceo'] = 'original_corpsid';
2498 return 'pceo';
2499 }
2500 }
2501
2502 public function addCorpsRankFilter()
2503 {
b8dcf62d 2504 $this->requireProfiles();
4083b126
RB
2505 $this->pc = true;
2506 $this->pcr = true;
2507 return 'pcr';
2508 }
2509
9b8e5fb4 2510 protected function corpsJoins()
4083b126
RB
2511 {
2512 $joins = array();
2513 if ($this->pc) {
5c412626 2514 $joins['pc'] = PlSqlJoin::left('profile_corps', '$ME.pid = $PID');
4083b126
RB
2515 }
2516 if ($this->pcr) {
5c412626 2517 $joins['pcr'] = PlSqlJoin::left('profile_corps_rank_enum', '$ME.id = pc.rankid');
4083b126
RB
2518 }
2519 foreach($this->pce as $sub => $field) {
5c412626 2520 $joins[$sub] = PlSqlJoin::left('profile_corps_enum', '$ME.id = pc.' . $field);
4083b126
RB
2521 }
2522 return $joins;
2523 }
2524
6a99c3ac
RB
2525 /** JOBS
2526 */
2527
da40b2a4
SJ
2528 const JOB_USERDEFINED = 0x0001;
2529 const JOB_CV = 0x0002;
2530 const JOB_ANY = 0x0003;
6a99c3ac
RB
2531
2532 /** Joins :
2533 * pj => profile_job
2534 * pje => profile_job_enum
3ac45f10 2535 * pjt => profile_job_terms
6a99c3ac 2536 */
da40b2a4 2537 private $with_pj = false;
6a99c3ac 2538 private $with_pje = false;
3ac45f10 2539 private $with_pjt = 0;
6a99c3ac
RB
2540
2541 public function addJobFilter()
2542 {
b8dcf62d 2543 $this->requireProfiles();
6a99c3ac
RB
2544 $this->with_pj = true;
2545 return 'pj';
2546 }
2547
2548 public function addJobCompanyFilter()
2549 {
2550 $this->addJobFilter();
2551 $this->with_pje = true;
2552 return 'pje';
2553 }
2554
3ac45f10
PC
2555 /**
2556 * Adds a filter on job terms of profile.
2557 * @param $nb the number of job terms to use
2558 * @return an array of the fields to filter (one for each term).
3ac45f10
PC
2559 */
2560 public function addJobTermsFilter($nb = 1)
2561 {
2562 $this->with_pjt = $nb;
2563 $jobtermstable = array();
2564 for ($i = 1; $i <= $nb; ++$i) {
4ec03752 2565 $jobtermstable[] = 'pjtr_'.$i;
3ac45f10
PC
2566 }
2567 return $jobtermstable;
2568 }
2569
9b8e5fb4 2570 protected function jobJoins()
6a99c3ac
RB
2571 {
2572 $joins = array();
2573 if ($this->with_pj) {
5c412626 2574 $joins['pj'] = PlSqlJoin::left('profile_job', '$ME.pid = $PID');
6a99c3ac
RB
2575 }
2576 if ($this->with_pje) {
5c412626 2577 $joins['pje'] = PlSqlJoin::left('profile_job_enum', '$ME.id = pj.jobid');
6a99c3ac 2578 }
3ac45f10
PC
2579 if ($this->with_pjt > 0) {
2580 for ($i = 1; $i <= $this->with_pjt; ++$i) {
2581 $joins['pjt_'.$i] = PlSqlJoin::left('profile_job_term', '$ME.pid = $PID');
2582 $joins['pjtr_'.$i] = PlSqlJoin::left('profile_job_term_relation', '$ME.jtid_2 = pjt_'.$i.'.jtid');
2583 }
2584 }
6a99c3ac
RB
2585 return $joins;
2586 }
2587
0a2e9c74
RB
2588 /** NETWORKING
2589 */
2590
2591 private $with_pnw = false;
2592 public function addNetworkingFilter()
2593 {
b8dcf62d 2594 $this->requireAccounts();
0a2e9c74
RB
2595 $this->with_pnw = true;
2596 return 'pnw';
2597 }
2598
9b8e5fb4 2599 protected function networkingJoins()
0a2e9c74
RB
2600 {
2601 $joins = array();
2602 if ($this->with_pnw) {
5c412626 2603 $joins['pnw'] = PlSqlJoin::left('profile_networking', '$ME.pid = $PID');
0a2e9c74
RB
2604 }
2605 return $joins;
2606 }
2607
6d62969e
RB
2608 /** PHONE
2609 */
2610
2d83cac9 2611 private $with_ptel = false;
6d62969e
RB
2612
2613 public function addPhoneFilter()
2614 {
b8dcf62d 2615 $this->requireAccounts();
2d83cac9 2616 $this->with_ptel = true;
6d62969e
RB
2617 return 'ptel';
2618 }
2619
9b8e5fb4 2620 protected function phoneJoins()
6d62969e
RB
2621 {
2622 $joins = array();
2d83cac9 2623 if ($this->with_ptel) {
5c412626 2624 $joins['ptel'] = PlSqlJoin::left('profile_phones', '$ME.pid = $PID');
6d62969e
RB
2625 }
2626 return $joins;
2627 }
2628
ceb512d2
RB
2629 /** MEDALS
2630 */
2631
2d83cac9 2632 private $with_pmed = false;
ceb512d2
RB
2633 public function addMedalFilter()
2634 {
b8dcf62d 2635 $this->requireProfiles();
2d83cac9 2636 $this->with_pmed = true;
ceb512d2
RB
2637 return 'pmed';
2638 }
2639
9b8e5fb4 2640 protected function medalJoins()
ceb512d2
RB
2641 {
2642 $joins = array();
2d83cac9 2643 if ($this->with_pmed) {
5c412626 2644 $joins['pmed'] = PlSqlJoin::left('profile_medals', '$ME.pid = $PID');
ceb512d2
RB
2645 }
2646 return $joins;
2647 }
2648
671b7073
RB
2649 /** MENTORING
2650 */
2651
2652 private $pms = array();
459e6f81 2653 private $mjtr = false;
96f01fba
RB
2654 const MENTOR = 1;
2655 const MENTOR_EXPERTISE = 2;
2656 const MENTOR_COUNTRY = 3;
da40b2a4 2657 const MENTOR_TERM = 4;
671b7073
RB
2658
2659 public function addMentorFilter($type)
2660 {
b8dcf62d 2661 $this->requireAccounts();
671b7073 2662 switch($type) {
96f01fba
RB
2663 case self::MENTOR:
2664 $this->pms['pm'] = 'profile_mentor';
2665 return 'pm';
4a93c3a3
RB
2666 case self::MENTOR_EXPERTISE:
2667 $this->pms['pme'] = 'profile_mentor';
671b7073 2668 return 'pme';
4a93c3a3
RB
2669 case self::MENTOR_COUNTRY:
2670 $this->pms['pmc'] = 'profile_mentor_country';
671b7073 2671 return 'pmc';
459e6f81
PC
2672 case self::MENTOR_TERM:
2673 $this->pms['pmt'] = 'profile_mentor_term';
2674 $this->mjtr = true;
2675 return 'mjtr';
671b7073 2676 default:
5d2e55c7 2677 Platal::page()->killError("Undefined mentor filter.");
671b7073
RB
2678 }
2679 }
2680
9b8e5fb4 2681 protected function mentorJoins()
671b7073
RB
2682 {
2683 $joins = array();
2684 foreach ($this->pms as $sub => $tab) {
5c412626 2685 $joins[$sub] = PlSqlJoin::left($tab, '$ME.pid = $PID');
671b7073 2686 }
459e6f81
PC
2687 if ($this->mjtr) {
2688 $joins['mjtr'] = PlSqlJoin::left('profile_job_term_relation', '$ME.jtid_2 = pmt.jtid');
2689 }
671b7073
RB
2690 return $joins;
2691 }
2692
3f42a6ad
FB
2693 /** CONTACTS
2694 */
2695 private $cts = array();
2696 public function addContactFilter($uid = null)
2697 {
c96da6c1 2698 $this->requireProfiles();
3f42a6ad
FB
2699 return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid);
2700 }
2701
9b8e5fb4 2702 protected function contactJoins()
3f42a6ad
FB
2703 {
2704 $joins = array();
2705 foreach ($this->cts as $sub=>$key) {
2706 if (is_null($key)) {
5c412626 2707 $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.contact = $PID');
3f42a6ad 2708 } else {
5c412626 2709 $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.uid = {?} AND $ME.contact = $PID', substr($key, 5));
3f42a6ad
FB
2710 }
2711 }
2712 return $joins;
2713 }
4e7bf1e0
FB
2714
2715
2716 /** CARNET
2717 */
2718 private $wn = array();
2719 public function addWatchRegistrationFilter($uid = null)
2720 {
b8dcf62d 2721 $this->requireAccounts();
4e7bf1e0
FB
2722 return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid);
2723 }
2724
2725 private $wp = array();
2726 public function addWatchPromoFilter($uid = null)
2727 {
b8dcf62d 2728 $this->requireAccounts();
4e7bf1e0
FB
2729 return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid);
2730 }
2731
2732 private $w = array();
2733 public function addWatchFilter($uid = null)
2734 {
b8dcf62d 2735 $this->requireAccounts();
4e7bf1e0
FB
2736 return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid);
2737 }
2738
9b8e5fb4 2739 protected function watchJoins()
4e7bf1e0
FB
2740 {
2741 $joins = array();
2742 foreach ($this->w as $sub=>$key) {
2743 if (is_null($key)) {
5c412626 2744 $joins['w' . $sub] = PlSqlJoin::left('watch');
4e7bf1e0 2745 } else {
5c412626 2746 $joins['w' . $sub] = PlSqlJoin::left('watch', '$ME.uid = {?}', substr($key, 5));
4e7bf1e0
FB
2747 }
2748 }
2749 foreach ($this->wn as $sub=>$key) {
2750 if (is_null($key)) {
5c412626 2751 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID');
4e7bf1e0 2752 } else {
5c412626 2753 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
4e7bf1e0
FB
2754 }
2755 }
2756 foreach ($this->wn as $sub=>$key) {
2757 if (is_null($key)) {
5c412626 2758 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID');
4e7bf1e0 2759 } else {
5c412626 2760 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
4e7bf1e0
FB
2761 }
2762 }
2763 foreach ($this->wp as $sub=>$key) {
2764 if (is_null($key)) {
5c412626 2765 $joins['wp' . $sub] = PlSqlJoin::left('watch_promo');
4e7bf1e0 2766 } else {
5c412626 2767 $joins['wp' . $sub] = PlSqlJoin::left('watch_promo', '$ME.uid = {?}', substr($key, 5));
4e7bf1e0
FB
2768 }
2769 }
2770 return $joins;
2771 }
48885bbe
FB
2772
2773
470d14f6
FB
2774 /** PHOTOS
2775 */
2776 private $with_photo;
2777 public function addPhotoFilter()
2778 {
2779 $this->requireProfiles();
2780 $this->with_photo = true;
7f26cd69 2781 return 'photo';
470d14f6
FB
2782 }
2783
2784 protected function photoJoins()
2785 {
2786 if ($this->with_photo) {
2787 return array('photo' => PlSqlJoin::left('profile_photos', '$ME.pid = $PID'));
2788 } else {
2789 return array();
2790 }
2791 }
2792
2793
48885bbe
FB
2794 /** MARKETING
2795 */
2796 private $with_rm;
2797 public function addMarketingHash()
2798 {
2799 $this->requireAccounts();
2800 $this->with_rm = true;
2801 }
2802
2803 protected function marketingJoins()
2804 {
2805 if ($this->with_rm) {
5c412626 2806 return array('rm' => PlSqlJoin::left('register_marketing', '$ME.uid = $UID'));
48885bbe
FB
2807 } else {
2808 return array();
2809 }
2810 }
a087cc8d 2811}
8363588b 2812// }}}
3f42a6ad 2813
a7d9ab89
RB
2814// {{{ class ProfileFilter
2815class ProfileFilter extends UserFilter
2816{
434570c4 2817 public function get($limit = null)
a7d9ab89
RB
2818 {
2819 return $this->getProfiles($limit);
2820 }
2daf7250
RB
2821
2822 public function filter(array $profiles, $limit = null)
2823 {
2824 return $this->filterProfiles($profiles, self::defaultLimit($limit));
2825 }
2826
2827 public function getTotalCount()
2828 {
2829 return $this->getTotalProfileCount();
2830 }
ccc951d9
RB
2831
2832 public function getGroups()
2833 {
2834 return $this->getPIDGroups();
2835 }
a7d9ab89
RB
2836}
2837// }}}
2838
a087cc8d
FB
2839// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
2840?>