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