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