Prepares database for job terms.
[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();
658b4c83 132 return '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
FB
559 {
560 $sub = $uf->addGroupFilter($this->group);
561 $where = 'gpm' . $sub . '.perms IS NOT NULL';
5d2e55c7 562 if ($this->anim) {
4927ee54
FB
563 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
564 }
565 return $where;
566 }
567}
8363588b 568// }}}
4927ee54 569
0fb3713c 570// {{{ class UFC_Binet
d7ddf29b
RB
571/** Selects users based on their belonging to a given (list of) binet
572 * @param $binet either a binet_id or an array of binet_ids
573 */
0fb3713c
RB
574class UFC_Binet implements UserFilterCondition
575{
d7ddf29b 576 private $val;
0fb3713c 577
61d1fd8b 578 public function __construct()
0fb3713c 579 {
61d1fd8b 580 $this->val = pl_flatten(func_get_args());
0fb3713c
RB
581 }
582
583 public function buildCondition(PlFilter &$uf)
584 {
d7ddf29b 585 $sub = $uf->addBinetsFilter();
bde68f05 586 return XDB::format($sub . '.binet_id IN {?}', $this->val);
a7f8e48a
RB
587 }
588}
589// }}}
590
591// {{{ class UFC_Section
46e88fe3
RB
592/** Selects users based on section
593 * @param $section ID of the section
594 */
a7f8e48a
RB
595class UFC_Section implements UserFilterCondition
596{
597 private $section;
598
61d1fd8b 599 public function __construct()
a7f8e48a 600 {
61d1fd8b 601 $this->section = pl_flatten(func_get_args());
a7f8e48a
RB
602 }
603
604 public function buildCondition(PlFilter &$uf)
605 {
606 $uf->requireProfiles();
61d1fd8b 607 return XDB::format('p.section IN {?}', $this->section);
0fb3713c
RB
608 }
609}
610// }}}
611
8363588b 612// {{{ class UFC_Email
2d6329a2 613/** Filters users based on an email or a list of emails
53eae167
RB
614 * @param $emails List of emails whose owner must be selected
615 */
2d6329a2 616class UFC_Email implements UserFilterCondition
21401768
FB
617{
618 private $emails;
2d6329a2 619 public function __construct()
21401768 620 {
61d1fd8b 621 $this->emails = pl_flatten(func_get_args());
21401768
FB
622 }
623
dcc63ed5 624 public function buildCondition(PlFilter &$uf)
21401768 625 {
2d6329a2
FB
626 $foreign = array();
627 $virtual = array();
628 $aliases = array();
21401768
FB
629 $cond = array();
630
631 if (count($this->emails) == 0) {
dcc63ed5 632 return PlFilterCondition::COND_TRUE;
21401768
FB
633 }
634
635 foreach ($this->emails as $entry) {
636 if (User::isForeignEmailAddress($entry)) {
2d6329a2 637 $foreign[] = $entry;
21401768 638 } else if (User::isVirtualEmailAddress($entry)) {
2d6329a2 639 $virtual[] = $entry;
21401768 640 } else {
21401768 641 @list($user, $domain) = explode('@', $entry);
2d6329a2 642 $aliases[] = $user;
21401768
FB
643 }
644 }
2d6329a2
FB
645
646 if (count($foreign) > 0) {
647 $sub = $uf->addEmailRedirectFilter($foreign);
bde68f05 648 $cond[] = XDB::format('e' . $sub . '.email IS NOT NULL OR a.email IN {?}', $foreign);
2d6329a2
FB
649 }
650 if (count($virtual) > 0) {
651 $sub = $uf->addVirtualEmailFilter($virtual);
652 $cond[] = 'vr' . $sub . '.redirect IS NOT NULL';
653 }
654 if (count($aliases) > 0) {
655 $sub = $uf->addAliasFilter($aliases);
656 $cond[] = 'al' . $sub . '.alias IS NOT NULL';
657 }
21401768
FB
658 return '(' . implode(') OR (', $cond) . ')';
659 }
660}
8363588b 661// }}}
d865c296 662
8363588b 663// {{{ class UFC_Address
2b9ca54d 664abstract class UFC_Address implements UserFilterCondition
c4b24511 665{
2b9ca54d 666 /** Valid address type ('hq' is reserved for company addresses)
036d1637 667 */
2b9ca54d
RB
668 const TYPE_HOME = 1;
669 const TYPE_PRO = 2;
670 const TYPE_ANY = 3;
c4b24511 671
2b9ca54d 672 /** Text for these types
036d1637 673 */
2b9ca54d
RB
674 protected static $typetexts = array(
675 self::TYPE_HOME => 'home',
676 self::TYPE_PRO => 'pro',
677 );
678
679 protected $type;
c4b24511 680
036d1637
RB
681 /** Flags for addresses
682 */
683 const FLAG_CURRENT = 0x0001;
684 const FLAG_TEMP = 0x0002;
685 const FLAG_SECOND = 0x0004;
686 const FLAG_MAIL = 0x0008;
687 const FLAG_CEDEX = 0x0010;
688
689 // Binary OR of those flags
690 const FLAG_ANY = 0x001F;
691
692 /** Text of these flags
693 */
2b9ca54d 694 protected static $flagtexts = array(
036d1637
RB
695 self::FLAG_CURRENT => 'current',
696 self::FLAG_TEMP => 'temporary',
697 self::FLAG_SECOND => 'secondary',
698 self::FLAG_MAIL => 'mail',
699 self::FLAG_CEDEX => 'cedex',
700 );
701
2b9ca54d
RB
702 protected $flags;
703
704 public function __construct($type = null, $flags = null)
705 {
706 $this->type = $type;
707 $this->flags = $flags;
708 }
709
710 protected function initConds($sub)
711 {
712 $conds = array();
713 $types = array();
714 foreach (self::$typetexts as $flag => $type) {
715 if ($flag & $this->type) {
716 $types[] = $type;
717 }
718 }
719 if (count($types)) {
6b140032 720 $conds[] = XDB::format($sub . '.type IN {?}', $types);
2b9ca54d
RB
721 }
722
723 if ($this->flags != self::FLAG_ANY) {
724 foreach(self::$flagtexts as $flag => $text) {
725 if ($flag & $this->flags) {
726 $conds[] = 'FIND_IN_SET(' . XDB::format('{?}', $text) . ', ' . $sub . '.flags)';
727 }
728 }
729 }
730 return $conds;
731 }
732
733}
734// }}}
735
736// {{{ class UFC_AddressText
737/** Select users based on their address, using full text search
738 * @param $text Text for filter in fulltext search
658b4c83 739 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
2b9ca54d
RB
740 * @param $type Filter on address type
741 * @param $flags Filter on address flags
742 * @param $country Filter on address country
743 * @param $locality Filter on address locality
744 */
745class UFC_AddressText extends UFC_Address
746{
2b9ca54d 747
036d1637 748 private $text;
2b9ca54d
RB
749 private $textSearchMode;
750
658b4c83 751 public function __construct($text = null, $textSearchMode = XDB::WILDCARD_CONTAINS,
2b9ca54d
RB
752 $type = null, $flags = self::FLAG_ANY, $country = null, $locality = null)
753 {
754 parent::__construct($type, $flags);
755 $this->text = $text;
756 $this->textSearchMode = $textSearchMode;
757 $this->country = $country;
758 $this->locality = $locality;
759 }
760
761 private function mkMatch($txt)
762 {
658b4c83 763 return XDB::formatWildcards($this->textSearchMode, $txt);
2b9ca54d
RB
764 }
765
766 public function buildCondition(PlFilter &$uf)
767 {
768 $sub = $uf->addAddressFilter();
769 $conds = $this->initConds($sub);
770 if ($this->text != null) {
771 $conds[] = $sub . '.text' . $this->mkMatch($this->text);
772 }
773
774 if ($this->country != null) {
775 $subc = $uf->addAddressCountryFilter();
776 $subconds = array();
777 $subconds[] = $subc . '.country' . $this->mkMatch($this->country);
778 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country);
779 $conds[] = implode(' OR ', $subconds);
780 }
781
782 if ($this->locality != null) {
783 $subl = $uf->addAddressLocalityFilter();
784 $conds[] = $subl . '.name' . $this->mkMatch($this->locality);
785 }
786
787 return implode(' AND ', $conds);
788 }
789}
790// }}}
791
46e88fe3 792// {{{ class UFC_AddressField
2b9ca54d 793/** Filters users based on their address,
46e88fe3
RB
794 * @param $val Either a code for one of the fields, or an array of such codes
795 * @param $fieldtype The type of field to look for
2b9ca54d
RB
796 * @param $type Filter on address type
797 * @param $flags Filter on address flags
2b9ca54d 798 */
46e88fe3 799class UFC_AddressField extends UFC_Address
2b9ca54d 800{
46e88fe3
RB
801 const FIELD_COUNTRY = 1;
802 const FIELD_ADMAREA = 2;
803 const FIELD_SUBADMAREA = 3;
804 const FIELD_LOCALITY = 4;
805 const FIELD_ZIPCODE = 5;
806
2b9ca54d
RB
807 /** Data of the filter
808 */
46e88fe3
RB
809 private $val;
810 private $fieldtype;
036d1637 811
46e88fe3 812 public function __construct($val, $fieldtype, $type = null, $flags = self::FLAG_ANY)
036d1637 813 {
2b9ca54d 814 parent::__construct($type, $flags);
46e88fe3
RB
815
816 if (!is_array($val)) {
817 $val = array($val);
818 }
819 $this->val = $val;
820 $this->fieldtype = $fieldtype;
c4b24511
RB
821 }
822
dcc63ed5 823 public function buildCondition(PlFilter &$uf)
c4b24511 824 {
036d1637 825 $sub = $uf->addAddressFilter();
d7ddf29b 826 $conds = $this->initConds($sub);
036d1637 827
46e88fe3
RB
828 switch ($this->fieldtype) {
829 case self::FIELD_COUNTRY:
830 $field = 'countryId';
831 break;
832 case self::FIELD_ADMAREA:
833 $field = 'administrativeAreaId';
834 break;
835 case self::FIELD_SUBADMAREA:
836 $field = 'subAdministrativeAreaId';
837 break;
838 case self::FIELD_LOCALITY:
839 $field = 'localityId';
840 break;
841 case self::FIELD_ZIPCODE:
842 $field = 'postalCode';
843 break;
844 default:
61f61261 845 Platal::page()->killError('Invalid address field type: ' . $this->fieldtype);
46e88fe3 846 }
bde68f05 847 $conds[] = XDB::format($sub . '.' . $field . ' IN {?}', $this->val);
036d1637
RB
848
849 return implode(' AND ', $conds);
c4b24511
RB
850 }
851}
8363588b 852// }}}
c4b24511 853
8363588b 854// {{{ class UFC_Corps
4083b126
RB
855/** Filters users based on the corps they belong to
856 * @param $corps Corps we are looking for (abbreviation)
857 * @param $type Whether we search for original or current corps
858 */
859class UFC_Corps implements UserFilterCondition
860{
5d2e55c7
RB
861 const CURRENT = 1;
862 const ORIGIN = 2;
4083b126
RB
863
864 private $corps;
865 private $type;
866
867 public function __construct($corps, $type = self::CURRENT)
868 {
869 $this->corps = $corps;
870 $this->type = $type;
871 }
872
dcc63ed5 873 public function buildCondition(PlFilter &$uf)
4083b126 874 {
5d2e55c7
RB
875 /** Tables shortcuts:
876 * pc for profile_corps,
4083b126
RB
877 * pceo for profile_corps_enum - orginal
878 * pcec for profile_corps_enum - current
879 */
880 $sub = $uf->addCorpsFilter($this->type);
881 $cond = $sub . '.abbreviation = ' . $corps;
882 return $cond;
883 }
884}
8363588b 885// }}}
4083b126 886
8363588b 887// {{{ class UFC_Corps_Rank
4083b126
RB
888/** Filters users based on their rank in the corps
889 * @param $rank Rank we are looking for (abbreviation)
890 */
891class UFC_Corps_Rank implements UserFilterCondition
892{
893 private $rank;
894 public function __construct($rank)
895 {
896 $this->rank = $rank;
897 }
898
dcc63ed5 899 public function buildCondition(PlFilter &$uf)
4083b126 900 {
5d2e55c7 901 /** Tables shortcuts:
4083b126
RB
902 * pcr for profile_corps_rank
903 */
904 $sub = $uf->addCorpsRankFilter();
905 $cond = $sub . '.abbreviation = ' . $rank;
906 return $cond;
907 }
908}
8363588b 909// }}}
4083b126 910
6a99c3ac
RB
911// {{{ class UFC_Job_Company
912/** Filters users based on the company they belong to
913 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
914 * @param $value The searched value
915 */
4e2f2ad2 916class UFC_Job_Company implements UserFilterCondition
6a99c3ac
RB
917{
918 const JOBID = 'id';
919 const JOBNAME = 'name';
920 const JOBACRONYM = 'acronym';
921
922 private $type;
923 private $value;
924
925 public function __construct($type, $value)
926 {
927 $this->assertType($type);
928 $this->type = $type;
929 $this->value = $value;
930 }
931
932 private function assertType($type)
933 {
934 if ($type != self::JOBID && $type != self::JOBNAME && $type != self::JOBACRONYM) {
5d2e55c7 935 Platal::page()->killError("Type de recherche non valide.");
6a99c3ac
RB
936 }
937 }
938
dcc63ed5 939 public function buildCondition(PlFilter &$uf)
6a99c3ac
RB
940 {
941 $sub = $uf->addJobCompanyFilter();
412e2e0f 942 $cond = $sub . '.' . $this->type . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value);
6a99c3ac
RB
943 return $cond;
944 }
945}
946// }}}
947
948// {{{ class UFC_Job_Sectorization
949/** Filters users based on the ((sub)sub)sector they work in
46e88fe3 950 * @param $val The ID of the sector, or an array of such IDs
d7ddf29b 951 * @param $type The kind of search (subsubsector/subsector/sector)
6a99c3ac 952 */
4e2f2ad2 953class UFC_Job_Sectorization implements UserFilterCondition
6a99c3ac 954{
d7ddf29b
RB
955 private $val;
956 private $type;
6a99c3ac 957
d7ddf29b
RB
958 public function __construct($val, $type = UserFilter::JOB_SECTOR)
959 {
960 self::assertType($type);
46e88fe3
RB
961 if (!is_array($val)) {
962 $val = array($val);
963 }
d7ddf29b
RB
964 $this->val = $val;
965 $this->type = $type;
966 }
6a99c3ac 967
d7ddf29b 968 private static function assertType($type)
6a99c3ac 969 {
d7ddf29b
RB
970 if ($type != UserFilter::JOB_SECTOR && $type != UserFilter::JOB_SUBSECTOR && $type != UserFilter::JOB_SUBSUBSECTOR) {
971 Platal::page()->killError("Type de secteur non valide.");
972 }
6a99c3ac
RB
973 }
974
dcc63ed5 975 public function buildCondition(PlFilter &$uf)
6a99c3ac 976 {
d7ddf29b
RB
977 $sub = $uf->addJobSectorizationFilter($this->type);
978 return $sub . '.id = ' . XDB::format('{?}', $this->val);
6a99c3ac
RB
979 }
980}
981// }}}
982
983// {{{ class UFC_Job_Description
984/** Filters users based on their job description
985 * @param $description The text being searched for
986 * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
987 */
4e2f2ad2 988class UFC_Job_Description implements UserFilterCondition
6a99c3ac
RB
989{
990
6a99c3ac
RB
991 private $description;
992 private $fields;
993
01cc5f9e 994 public function __construct($description, $fields)
6a99c3ac
RB
995 {
996 $this->fields = $fields;
997 $this->description = $description;
998 }
999
dcc63ed5 1000 public function buildCondition(PlFilter &$uf)
6a99c3ac
RB
1001 {
1002 $conds = array();
1003 if ($this->fields & UserFilter::JOB_USERDEFINED) {
1004 $sub = $uf->addJobFilter();
658b4c83 1005 $conds[] = $sub . '.description ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
6a99c3ac 1006 }
01cc5f9e
RB
1007 if ($this->fields & UserFilter::JOB_CV) {
1008 $uf->requireProfiles();
658b4c83 1009 $conds[] = 'p.cv ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
01cc5f9e 1010 }
6a99c3ac
RB
1011 if ($this->fields & UserFilter::JOB_SECTOR) {
1012 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SECTOR);
658b4c83 1013 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
6a99c3ac
RB
1014 }
1015 if ($this->fields & UserFilter::JOB_SUBSECTOR) {
1016 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSECTOR);
658b4c83 1017 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
6a99c3ac
RB
1018 }
1019 if ($this->fields & UserFilter::JOB_SUBSUBSECTOR) {
1020 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSUBSECTOR);
658b4c83 1021 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
6a99c3ac 1022 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_ALTERNATES);
658b4c83 1023 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
6a99c3ac
RB
1024 }
1025 return implode(' OR ', $conds);
1026 }
1027}
1028// }}}
1029
0a2e9c74
RB
1030// {{{ class UFC_Networking
1031/** Filters users based on network identity (IRC, ...)
1032 * @param $type Type of network (-1 for any)
1033 * @param $value Value to search
1034 */
4e2f2ad2 1035class UFC_Networking implements UserFilterCondition
0a2e9c74
RB
1036{
1037 private $type;
1038 private $value;
1039
1040 public function __construct($type, $value)
1041 {
1042 $this->type = $type;
1043 $this->value = $value;
1044 }
1045
dcc63ed5 1046 public function buildCondition(PlFilter &$uf)
0a2e9c74
RB
1047 {
1048 $sub = $uf->addNetworkingFilter();
1049 $conds = array();
658b4c83 1050 $conds[] = $sub . '.address ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value);
0a2e9c74 1051 if ($this->type != -1) {
1f5cd004 1052 $conds[] = $sub . '.nwid = ' . XDB::format('{?}', $this->type);
0a2e9c74
RB
1053 }
1054 return implode(' AND ', $conds);
1055 }
1056}
1057// }}}
1058
6d62969e
RB
1059// {{{ class UFC_Phone
1060/** Filters users based on their phone number
1061 * @param $num_type Type of number (pro/user/home)
1062 * @param $phone_type Type of phone (fixed/mobile/fax)
1063 * @param $number Phone number
1064 */
4e2f2ad2 1065class UFC_Phone implements UserFilterCondition
6d62969e
RB
1066{
1067 const NUM_PRO = 'pro';
1068 const NUM_USER = 'user';
1069 const NUM_HOME = 'address';
1070 const NUM_ANY = 'any';
1071
1072 const PHONE_FIXED = 'fixed';
1073 const PHONE_MOBILE = 'mobile';
1074 const PHONE_FAX = 'fax';
1075 const PHONE_ANY = 'any';
1076
1077 private $num_type;
1078 private $phone_type;
1079 private $number;
1080
1081 public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY)
1082 {
da0a9c5a 1083 $phone = new Phone(array('display' => $number));
0b6c8b36
SJ
1084 $phone->format();
1085 $this->number = $phone->search();
6d62969e 1086 $this->num_type = $num_type;
0b6c8b36 1087 $this->phone_type = $phone_type;
6d62969e
RB
1088 }
1089
dcc63ed5 1090 public function buildCondition(PlFilter &$uf)
6d62969e
RB
1091 {
1092 $sub = $uf->addPhoneFilter();
1093 $conds = array();
1094 $conds[] = $sub . '.search_tel = ' . XDB::format('{?}', $this->number);
1095 if ($this->num_type != self::NUM_ANY) {
1096 $conds[] = $sub . '.link_type = ' . XDB::format('{?}', $this->num_type);
1097 }
1098 if ($this->phone_type != self::PHONE_ANY) {
1099 $conds[] = $sub . '.tel_type = ' . XDB::format('{?}', $this->phone_type);
1100 }
1101 return implode(' AND ', $conds);
1102 }
1103}
1104// }}}
1105
ceb512d2
RB
1106// {{{ class UFC_Medal
1107/** Filters users based on their medals
1108 * @param $medal ID of the medal
1109 * @param $grade Grade of the medal (null for 'any')
1110 */
4e2f2ad2 1111class UFC_Medal implements UserFilterCondition
ceb512d2
RB
1112{
1113 private $medal;
1114 private $grade;
1115
1116 public function __construct($medal, $grade = null)
1117 {
1118 $this->medal = $medal;
1119 $this->grade = $grade;
1120 }
1121
dcc63ed5 1122 public function buildCondition(PlFilter &$uf)
ceb512d2
RB
1123 {
1124 $conds = array();
1125 $sub = $uf->addMedalFilter();
1126 $conds[] = $sub . '.mid = ' . XDB::format('{?}', $this->medal);
1127 if ($this->grade != null) {
1128 $conds[] = $sub . '.gid = ' . XDB::format('{?}', $this->grade);
1129 }
1130 return implode(' AND ', $conds);
1131 }
1132}
1133// }}}
1134
470d14f6
FB
1135// {{{ class UFC_Photo
1136/** Filters profiles with photo
1137 */
1138class UFC_Photo implements UserFilterCondition
1139{
1140 public function buildCondition(PlFilter &$uf)
1141 {
1142 $uf->addPhotoFilter();
1143 return 'photo.attach IS NOT NULL';
1144 }
1145}
1146// }}}
1147
671b7073
RB
1148// {{{ class UFC_Mentor_Expertise
1149/** Filters users by mentoring expertise
1150 * @param $expertise Domain of expertise
1151 */
4e2f2ad2 1152class UFC_Mentor_Expertise implements UserFilterCondition
671b7073
RB
1153{
1154 private $expertise;
1155
1156 public function __construct($expertise)
1157 {
1158 $this->expertise = $expertise;
1159 }
1160
dcc63ed5 1161 public function buildCondition(PlFilter &$uf)
671b7073
RB
1162 {
1163 $sub = $uf->addMentorFilter(UserFilter::MENTOR_EXPERTISE);
658b4c83 1164 return $sub . '.expertise ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->expertise);
671b7073
RB
1165 }
1166}
1167// }}}
1168
1169// {{{ class UFC_Mentor_Country
1170/** Filters users by mentoring country
1171 * @param $country Two-letters code of country being searched
1172 */
4e2f2ad2 1173class UFC_Mentor_Country implements UserFilterCondition
671b7073
RB
1174{
1175 private $country;
1176
61d1fd8b 1177 public function __construct()
671b7073 1178 {
61d1fd8b 1179 $this->country = pl_flatten(func_get_args());
671b7073
RB
1180 }
1181
dcc63ed5 1182 public function buildCondition(PlFilter &$uf)
671b7073
RB
1183 {
1184 $sub = $uf->addMentorFilter(UserFilter::MENTOR_COUNTRY);
61d1fd8b 1185 return $sub . '.country IN ' . XDB::format('{?}', $this->country);
671b7073
RB
1186 }
1187}
1188// }}}
1189
1190// {{{ class UFC_Mentor_Sectorization
1191/** Filters users based on mentoring (sub|)sector
c5da8574
RB
1192 * @param $sector ID of (sub)sector
1193 * @param $type Whether we are looking for a sector or a subsector
671b7073 1194 */
4e2f2ad2 1195class UFC_Mentor_Sectorization implements UserFilterCondition
671b7073 1196{
c5da8574
RB
1197 const SECTOR = 1;
1198 const SUBSECTOR = 2;
671b7073 1199 private $sector;
c5da8574 1200 private $type;
671b7073 1201
c5da8574 1202 public function __construct($sector, $type = self::SECTOR)
671b7073
RB
1203 {
1204 $this->sector = $sector;
c5da8574 1205 $this->type = $type;
671b7073
RB
1206 }
1207
dcc63ed5 1208 public function buildCondition(PlFilter &$uf)
671b7073
RB
1209 {
1210 $sub = $uf->addMentorFilter(UserFilter::MENTOR_SECTOR);
c5da8574
RB
1211 if ($this->type == self::SECTOR) {
1212 $field = 'sectorid';
1213 } else {
1214 $field = 'subsectorid';
671b7073 1215 }
c5da8574 1216 return $sub . '.' . $field . ' = ' . XDB::format('{?}', $this->sector);
671b7073
RB
1217 }
1218}
1219// }}}
1220
8363588b 1221// {{{ class UFC_UserRelated
5d2e55c7 1222/** Filters users based on a relation toward a user
53eae167
RB
1223 * @param $user User to which searched users are related
1224 */
4e7bf1e0 1225abstract class UFC_UserRelated implements UserFilterCondition
3f42a6ad 1226{
009b8ab7
FB
1227 protected $user;
1228 public function __construct(PlUser &$user)
1229 {
1230 $this->user =& $user;
3f42a6ad 1231 }
4e7bf1e0 1232}
8363588b 1233// }}}
3f42a6ad 1234
8363588b 1235// {{{ class UFC_Contact
5d2e55c7 1236/** Filters users who belong to selected user's contacts
53eae167 1237 */
4e7bf1e0
FB
1238class UFC_Contact extends UFC_UserRelated
1239{
dcc63ed5 1240 public function buildCondition(PlFilter &$uf)
3f42a6ad 1241 {
009b8ab7 1242 $sub = $uf->addContactFilter($this->user->id());
3f42a6ad
FB
1243 return 'c' . $sub . '.contact IS NOT NULL';
1244 }
1245}
8363588b 1246// }}}
3f42a6ad 1247
8363588b 1248// {{{ class UFC_WatchRegistration
53eae167
RB
1249/** Filters users being watched by selected user
1250 */
4e7bf1e0
FB
1251class UFC_WatchRegistration extends UFC_UserRelated
1252{
dcc63ed5 1253 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1254 {
a87530ea 1255 if (!$this->user->watchType('registration')) {
dcc63ed5 1256 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1257 }
1258 $uids = $this->user->watchUsers();
1259 if (count($uids) == 0) {
dcc63ed5 1260 return PlFilterCondition::COND_FALSE;
009b8ab7 1261 } else {
93b0da52 1262 return XDB::format('$UID IN {?}', $uids);
009b8ab7 1263 }
4e7bf1e0
FB
1264 }
1265}
8363588b 1266// }}}
4e7bf1e0 1267
8363588b 1268// {{{ class UFC_WatchPromo
53eae167
RB
1269/** Filters users belonging to a promo watched by selected user
1270 * @param $user Selected user (the one watching promo)
1271 * @param $grade Formation the user is watching
1272 */
4e7bf1e0
FB
1273class UFC_WatchPromo extends UFC_UserRelated
1274{
1275 private $grade;
009b8ab7 1276 public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING)
4e7bf1e0 1277 {
009b8ab7 1278 parent::__construct($user);
4e7bf1e0
FB
1279 $this->grade = $grade;
1280 }
1281
dcc63ed5 1282 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1283 {
009b8ab7
FB
1284 $promos = $this->user->watchPromos();
1285 if (count($promos) == 0) {
dcc63ed5 1286 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1287 } else {
1288 $sube = $uf->addEducationFilter(true, $this->grade);
1289 $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
bde68f05 1290 return XDB::format($field . ' IN {?}', $promos);
009b8ab7 1291 }
4e7bf1e0
FB
1292 }
1293}
8363588b 1294// }}}
4e7bf1e0 1295
8363588b 1296// {{{ class UFC_WatchContact
53eae167
RB
1297/** Filters users watched by selected user
1298 */
009b8ab7 1299class UFC_WatchContact extends UFC_Contact
4e7bf1e0 1300{
dcc63ed5 1301 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1302 {
009b8ab7 1303 if (!$this->user->watchContacts()) {
dcc63ed5 1304 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1305 }
1306 return parent::buildCondition($uf);
4e7bf1e0
FB
1307 }
1308}
8363588b 1309// }}}
4e7bf1e0 1310
48885bbe
FB
1311// {{{ class UFC_MarketingHash
1312/** Filters users using the hash generated
1313 * to send marketing emails to him.
1314 */
1315class UFC_MarketingHash implements UserFilterCondition
1316{
1317 private $hash;
1318
1319 public function __construct($hash)
1320 {
1321 $this->hash = $hash;
1322 }
1323
1324 public function buildCondition(PlFilter &$uf)
1325 {
1326 $table = $uf->addMarketingHash();
1327 return XDB::format('rm.hash = {?}', $this->hash);
1328 }
1329}
416d4244 1330// }}}
4e7bf1e0 1331
d865c296
FB
1332/******************
1333 * ORDERS
1334 ******************/
1335
8363588b 1336// {{{ class UFO_Promo
5d2e55c7
RB
1337/** Orders users by promotion
1338 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
53eae167
RB
1339 * @param $desc Whether sort is descending
1340 */
ccc951d9 1341class UFO_Promo extends PlFilterGroupableOrder
d865c296
FB
1342{
1343 private $grade;
1344
1345 public function __construct($grade = null, $desc = false)
1346 {
009b8ab7 1347 parent::__construct($desc);
d865c296 1348 $this->grade = $grade;
d865c296
FB
1349 }
1350
61f61261 1351 protected function getSortTokens(PlFilter &$uf)
d865c296
FB
1352 {
1353 if (UserFilter::isGrade($this->grade)) {
1354 $sub = $uf->addEducationFilter($this->grade);
1355 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
1356 } else {
1357 $sub = $uf->addDisplayFilter();
1358 return 'pd' . $sub . '.promo';
1359 }
1360 }
1361}
8363588b 1362// }}}
d865c296 1363
8363588b 1364// {{{ class UFO_Name
53eae167 1365/** Sorts users by name
5d2e55c7
RB
1366 * @param $type Type of name on which to sort (firstname...)
1367 * @param $variant Variant of that name to use (marital, ordinary...)
53eae167
RB
1368 * @param $particle Set to true if particles should be included in the sorting order
1369 * @param $desc If sort order should be descending
1370 */
ccc951d9 1371class UFO_Name extends PlFilterOrder
d865c296
FB
1372{
1373 private $type;
1374 private $variant;
1375 private $particle;
1376
1377 public function __construct($type, $variant = null, $particle = false, $desc = false)
1378 {
009b8ab7 1379 parent::__construct($desc);
d865c296
FB
1380 $this->type = $type;
1381 $this->variant = $variant;
1382 $this->particle = $particle;
d865c296
FB
1383 }
1384
61f61261 1385 protected function getSortTokens(PlFilter &$uf)
d865c296 1386 {
913a4e90 1387 if (Profile::isDisplayName($this->type)) {
d865c296 1388 $sub = $uf->addDisplayFilter();
a9ef52c9
RB
1389 $token = 'pd' . $sub . '.' . $this->type;
1390 if ($uf->accountsRequired()) {
1391 $account_token = Profile::getAccountEquivalentName($this->type);
1392 return 'IFNULL(' . $token . ', a.' . $account_token . ')';
1393 } else {
1394 return $token;
1395 }
d865c296
FB
1396 } else {
1397 $sub = $uf->addNameFilter($this->type, $this->variant);
1398 if ($this->particle) {
1399 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1400 } else {
1401 return 'pn' . $sub . '.name';
1402 }
1403 }
1404 }
1405}
8363588b 1406// }}}
d865c296 1407
40585144 1408// {{{ class UFO_Score
ccc951d9 1409class UFO_Score extends PlFilterOrder
40585144 1410{
61f61261 1411 protected function getSortTokens(PlFilter &$uf)
40585144 1412 {
2a93b634
RB
1413 $toks = $uf->getNameTokens();
1414 $scores = array();
488765e3
RB
1415
1416 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
1417 if (count($toks) == 0) {
1418 return 'NULL';
1419 }
1420
2a93b634
RB
1421 foreach ($toks as $sub => $token) {
1422 $scores[] = XDB::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
1423 }
1424 return implode(' + ', $scores);
40585144
RB
1425 }
1426}
1427// }}}
1428
8363588b 1429// {{{ class UFO_Registration
53eae167
RB
1430/** Sorts users based on registration date
1431 */
ccc951d9 1432class UFO_Registration extends PlFilterOrder
38c6fe96 1433{
61f61261 1434 protected function getSortTokens(PlFilter &$uf)
38c6fe96 1435 {
6c1e97ae 1436 $uf->requireAccounts();
009b8ab7 1437 return 'a.registration_date';
38c6fe96 1438 }
009b8ab7 1439}
8363588b 1440// }}}
38c6fe96 1441
8363588b 1442// {{{ class UFO_Birthday
53eae167
RB
1443/** Sorts users based on next birthday date
1444 */
ccc951d9 1445class UFO_Birthday extends PlFilterOrder
009b8ab7 1446{
61f61261 1447 protected function getSortTokens(PlFilter &$uf)
38c6fe96 1448 {
6c1e97ae 1449 $uf->requireProfiles();
009b8ab7 1450 return 'p.next_birthday';
38c6fe96
FB
1451 }
1452}
8363588b 1453// }}}
38c6fe96 1454
8363588b 1455// {{{ class UFO_ProfileUpdate
53eae167
RB
1456/** Sorts users based on last profile update
1457 */
ccc951d9 1458class UFO_ProfileUpdate extends PlFilterOrder
009b8ab7 1459{
61f61261 1460 protected function getSortTokens(PlFilter &$uf)
009b8ab7 1461 {
6c1e97ae 1462 $uf->requireProfiles();
009b8ab7
FB
1463 return 'p.last_change';
1464 }
1465}
8363588b 1466// }}}
009b8ab7 1467
8363588b 1468// {{{ class UFO_Death
53eae167
RB
1469/** Sorts users based on death date
1470 */
ccc951d9 1471class UFO_Death extends PlFilterOrder
009b8ab7 1472{
61f61261 1473 protected function getSortTokens(PlFilter &$uf)
009b8ab7 1474 {
6c1e97ae 1475 $uf->requireProfiles();
009b8ab7
FB
1476 return 'p.deathdate';
1477 }
1478}
8363588b 1479// }}}
009b8ab7 1480
6c1e97ae
FB
1481// {{{ class UFO_Uid
1482/** Sorts users based on their uid
1483 */
ccc951d9 1484class UFO_Uid extends PlFilterOrder
6c1e97ae
FB
1485{
1486 protected function getSortTokens(PlFilter &$uf)
1487 {
1488 $uf->requireAccounts();
93b0da52 1489 return '$UID';
6c1e97ae
FB
1490 }
1491}
c752a130 1492// }}}
6c1e97ae
FB
1493
1494// {{{ class UFO_Hruid
1495/** Sorts users based on their hruid
1496 */
ccc951d9 1497class UFO_Hruid extends PlFilterOrder
6c1e97ae
FB
1498{
1499 protected function getSortTokens(PlFilter &$uf)
1500 {
1501 $uf->requireAccounts();
1502 return 'a.hruid';
1503 }
1504}
1505// }}}
1506
1507// {{{ class UFO_Pid
1508/** Sorts users based on their pid
1509 */
ccc951d9 1510class UFO_Pid extends PlFilterOrder
6c1e97ae
FB
1511{
1512 protected function getSortTokens(PlFilter &$uf)
1513 {
1514 $uf->requireProfiles();
93b0da52 1515 return '$PID';
6c1e97ae
FB
1516 }
1517}
c752a130 1518// }}}
6c1e97ae
FB
1519
1520// {{{ class UFO_Hrpid
1521/** Sorts users based on their hrpid
1522 */
ccc951d9 1523class UFO_Hrpid extends PlFilterOrder
6c1e97ae
FB
1524{
1525 protected function getSortTokens(PlFilter &$uf)
1526 {
1527 $uf->requireProfiles();
1528 return 'p.hrpid';
1529 }
1530}
1531// }}}
1532
009b8ab7 1533
d865c296
FB
1534/***********************************
1535 *********************************
1536 USER FILTER CLASS
1537 *********************************
1538 ***********************************/
1539
8363588b 1540// {{{ class UserFilter
2d83cac9
RB
1541/** This class provides a convenient and centralized way of filtering users.
1542 *
1543 * Usage:
1544 * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t));
1545 *
1546 * Resulting UserFilter can be used to:
1547 * - get a list of User objects matching the filter
1548 * - get a list of UIDs matching the filter
1549 * - get the number of users matching the filter
1550 * - check whether a given User matches the filter
1551 * - filter a list of User objects depending on whether they match the filter
1552 *
1553 * Usage for UFC and UFO objects:
1554 * A UserFilter will call all private functions named XXXJoins.
1555 * These functions must return an array containing the list of join
1556 * required by the various UFC and UFO associated to the UserFilter.
1557 * Entries in those returned array are of the following form:
1558 * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter')
1559 * which will be translated into :
1560 * join_type JOIN joined_table AS join_tablealias ON (join_criter)
1561 * in the final query.
1562 *
1563 * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with
913a4e90 1564 * profile.pid, and $UID with accounts.uid.
2d83cac9
RB
1565 *
1566 * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined;
1567 * its parameter will be used to set various private vars of the UserFilter describing
1568 * the required joins ; such a function shall return the "join_tablealias" to use
1569 * when referring to the joined table.
1570 *
1571 * For example, if data from profile_job must be available to filter results,
aab2ffdd 1572 * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and
2d83cac9
RB
1573 * return 'pj', the short name to use when referring to profile_job; when building
1574 * the query, calling the jobJoins function will return an array containing a single
1575 * row:
1576 * 'pj' => array('left', 'profile_job', '$ME.pid = $UID');
1577 *
1578 * The 'register_optional' function can be used to generate unique table aliases when
1579 * the same table has to be joined several times with different aliases.
1580 */
9b8e5fb4 1581class UserFilter extends PlFilter
a087cc8d 1582{
9b8e5fb4 1583 protected $joinMethods = array();
7ca75030 1584
61f61261
RB
1585 protected $joinMetas = array(
1586 '$PID' => 'p.pid',
1587 '$UID' => 'a.uid',
9b8e5fb4 1588 );
d865c296 1589
a087cc8d 1590 private $root;
24e08e33 1591 private $sort = array();
ccc951d9 1592 private $grouper = null;
784745ce 1593 private $query = null;
24e08e33 1594 private $orderby = null;
784745ce 1595
2daf7250
RB
1596 private $lastusercount = null;
1597 private $lastprofilecount = null;
d865c296 1598
24e08e33 1599 public function __construct($cond = null, $sort = null)
5dd9d823 1600 {
06598c13 1601 if (empty($this->joinMethods)) {
d865c296
FB
1602 $class = new ReflectionClass('UserFilter');
1603 foreach ($class->getMethods() as $method) {
1604 $name = $method->getName();
1605 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
06598c13 1606 $this->joinMethods[] = $name;
d865c296
FB
1607 }
1608 }
1609 }
5dd9d823 1610 if (!is_null($cond)) {
06598c13 1611 if ($cond instanceof PlFilterCondition) {
5dd9d823
FB
1612 $this->setCondition($cond);
1613 }
1614 }
24e08e33 1615 if (!is_null($sort)) {
ccc951d9 1616 if ($sort instanceof PlFilterOrder) {
24e08e33 1617 $this->addSort($sort);
d865c296
FB
1618 } else if (is_array($sort)) {
1619 foreach ($sort as $s) {
1620 $this->addSort($s);
1621 }
24e08e33
FB
1622 }
1623 }
5dd9d823
FB
1624 }
1625
784745ce
FB
1626 private function buildQuery()
1627 {
2a93b634
RB
1628 // The root condition is built first because some orders need info
1629 // available only once all UFC have set their conditions (UFO_Score)
1630 if (is_null($this->query)) {
1631 $where = $this->root->buildCondition($this);
226626ae
FB
1632 $where = str_replace(array_keys($this->joinMetas),
1633 $this->joinMetas,
1634 $where);
2a93b634 1635 }
d865c296
FB
1636 if (is_null($this->orderby)) {
1637 $orders = array();
1638 foreach ($this->sort as $sort) {
1639 $orders = array_merge($orders, $sort->buildSort($this));
1640 }
1641 if (count($orders) == 0) {
1642 $this->orderby = '';
1643 } else {
1644 $this->orderby = 'ORDER BY ' . implode(', ', $orders);
1645 }
226626ae
FB
1646 $this->orderby = str_replace(array_keys($this->joinMetas),
1647 $this->joinMetas,
1648 $this->orderby);
d865c296 1649 }
784745ce 1650 if (is_null($this->query)) {
2a93b634 1651 if ($this->with_accounts) {
b8dcf62d
RB
1652 $from = 'accounts AS a';
1653 } else {
1654 $this->requireProfiles();
1655 $from = 'profiles AS p';
1656 }
f7ea7450 1657 $joins = $this->buildJoins();
b8dcf62d 1658 $this->query = 'FROM ' . $from . '
784745ce
FB
1659 ' . $joins . '
1660 WHERE (' . $where . ')';
1661 }
1662 }
1663
ccc951d9
RB
1664 public function hasGroups()
1665 {
1666 return $this->grouper != null;
1667 }
1668
1669 public function getGroups()
1670 {
1671 return $this->getUIDGroups();
1672 }
1673
1674 public function getUIDGroups()
1675 {
1676 $this->requireAccounts();
1677 $this->buildQuery();
1678 $token = $this->grouper->getGroupToken($this);
1679
1680 $groups = XDB::fetchAllRow('SELECT ' . $token . ', COUNT(a.uid)
1681 ' . $this->query . '
1682 GROUP BY ' . $token,
1683 0);
1684 return $groups;
1685 }
1686
1687 public function getPIDGroups()
1688 {
1689 $this->requireProfiles();
1690 $this->buildQuery();
1691 $token = $this->grouper->getGroupToken($this);
1692
1693 $groups = XDB::fetchAllRow('SELECT ' . $token . ', COUNT(p.pid)
1694 ' . $this->query . '
1695 GROUP BY ' . $token,
1696 0);
1697 return $groups;
1698 }
1699
7ca75030 1700 private function getUIDList($uids = null, PlLimit &$limit)
d865c296 1701 {
b8dcf62d 1702 $this->requireAccounts();
d865c296 1703 $this->buildQuery();
7ca75030 1704 $lim = $limit->getSql();
d865c296 1705 $cond = '';
45b20ca0 1706 if (!empty($uids)) {
bde68f05 1707 $cond = XDB::format(' AND a.uid IN {?}', $uids);
d865c296
FB
1708 }
1709 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1710 ' . $this->query . $cond . '
1711 GROUP BY a.uid
1712 ' . $this->orderby . '
7ca75030 1713 ' . $lim);
2daf7250 1714 $this->lastusercount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
d865c296
FB
1715 return $fetched;
1716 }
1717
043b104b
RB
1718 private function getPIDList($pids = null, PlLimit &$limit)
1719 {
1720 $this->requireProfiles();
1721 $this->buildQuery();
1722 $lim = $limit->getSql();
1723 $cond = '';
1724 if (!is_null($pids)) {
bde68f05 1725 $cond = XDB::format(' AND p.pid IN {?}', $pids);
043b104b
RB
1726 }
1727 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid
1728 ' . $this->query . $cond . '
1729 GROUP BY p.pid
1730 ' . $this->orderby . '
1731 ' . $lim);
2daf7250 1732 $this->lastprofilecount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
043b104b
RB
1733 return $fetched;
1734 }
1735
434570c4
FB
1736 private static function defaultLimit($limit) {
1737 if ($limit == null) {
1738 return new PlLimit();
1739 } else {
1740 return $limit;
1741 }
1742 }
1743
a087cc8d
FB
1744 /** Check that the user match the given rule.
1745 */
1746 public function checkUser(PlUser &$user)
1747 {
b8dcf62d 1748 $this->requireAccounts();
784745ce
FB
1749 $this->buildQuery();
1750 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1751 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
1752 return $count == 1;
a087cc8d
FB
1753 }
1754
043b104b
RB
1755 /** Check that the profile match the given rule.
1756 */
1757 public function checkProfile(Profile &$profile)
1758 {
1759 $this->requireProfiles();
1760 $this->buildQuery();
1761 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1762 ' . $this->query . XDB::format(' AND p.pid = {?}', $profile->id()));
1763 return $count == 1;
1764 }
1765
1766 /** Default filter is on users
a087cc8d 1767 */
434570c4 1768 public function filter(array $users, $limit = null)
a087cc8d 1769 {
434570c4 1770 return $this->filterUsers($users, self::defaultLimit($limit));
043b104b
RB
1771 }
1772
1773 /** Filter a list of users to extract the users matching the rule.
1774 */
434570c4 1775 public function filterUsers(array $users, $limit = null)
043b104b 1776 {
434570c4 1777 $limit = self::defaultLimit($limit);
b8dcf62d 1778 $this->requireAccounts();
4927ee54
FB
1779 $this->buildQuery();
1780 $table = array();
1781 $uids = array();
1782 foreach ($users as $user) {
07eb5b0e
FB
1783 if ($user instanceof PlUser) {
1784 $uid = $user->id();
1785 } else {
1786 $uid = $user;
1787 }
1788 $uids[] = $uid;
1789 $table[$uid] = $user;
4927ee54 1790 }
7ca75030 1791 $fetched = $this->getUIDList($uids, $limit);
a087cc8d 1792 $output = array();
4927ee54
FB
1793 foreach ($fetched as $uid) {
1794 $output[] = $table[$uid];
a087cc8d
FB
1795 }
1796 return $output;
1797 }
1798
043b104b
RB
1799 /** Filter a list of profiles to extract the users matching the rule.
1800 */
434570c4 1801 public function filterProfiles(array $profiles, $limit = null)
043b104b 1802 {
434570c4 1803 $limit = self::defaultLimit($limit);
043b104b
RB
1804 $this->requireProfiles();
1805 $this->buildQuery();
1806 $table = array();
1807 $pids = array();
1808 foreach ($profiles as $profile) {
1809 if ($profile instanceof Profile) {
1810 $pid = $profile->id();
1811 } else {
1812 $pid = $profile;
1813 }
1814 $pids[] = $pid;
1815 $table[$pid] = $profile;
1816 }
1817 $fetched = $this->getPIDList($pids, $limit);
1818 $output = array();
1819 foreach ($fetched as $pid) {
1820 $output[] = $table[$pid];
1821 }
1822 return $output;
1823 }
1824
434570c4 1825 public function getUIDs($limit = null)
7ca75030 1826 {
833a6e86
FB
1827 $limit = self::defaultLimit($limit);
1828 return $this->getUIDList(null, $limit);
7ca75030
RB
1829 }
1830
ad27b22e
FB
1831 public function getUID($pos = 0)
1832 {
983f3864 1833 $uids =$this->getUIDList(null, new PlLimit(1, $pos));
ad27b22e
FB
1834 if (count($uids) == 0) {
1835 return null;
1836 } else {
1837 return $uids[0];
1838 }
1839 }
1840
434570c4 1841 public function getPIDs($limit = null)
043b104b 1842 {
833a6e86
FB
1843 $limit = self::defaultLimit($limit);
1844 return $this->getPIDList(null, $limit);
043b104b
RB
1845 }
1846
ad27b22e
FB
1847 public function getPID($pos = 0)
1848 {
983f3864 1849 $pids =$this->getPIDList(null, new PlLimit(1, $pos));
ad27b22e
FB
1850 if (count($pids) == 0) {
1851 return null;
1852 } else {
1853 return $pids[0];
1854 }
1855 }
1856
434570c4 1857 public function getUsers($limit = null)
4927ee54 1858 {
7ca75030 1859 return User::getBulkUsersWithUIDs($this->getUIDs($limit));
d865c296
FB
1860 }
1861
ad27b22e
FB
1862 public function getUser($pos = 0)
1863 {
1864 $uid = $this->getUID($pos);
1865 if ($uid == null) {
1866 return null;
1867 } else {
1868 return User::getWithUID($uid);
1869 }
1870 }
1871
0d906109
RB
1872 public function iterUsers($limit = null)
1873 {
1874 return User::iterOverUIDs($this->getUIDs($limit));
1875 }
1876
00f83317 1877 public function getProfiles($limit = null, $fields = 0x0000, $visibility = null)
043b104b 1878 {
00f83317 1879 return Profile::getBulkProfilesWithPIDs($this->getPIDs($limit), $fields, $visibility);
043b104b
RB
1880 }
1881
00f83317 1882 public function getProfile($pos = 0, $fields = 0x0000, $visibility = null)
ad27b22e
FB
1883 {
1884 $pid = $this->getPID($pos);
1885 if ($pid == null) {
1886 return null;
1887 } else {
00f83317 1888 return Profile::get($pid, $fields, $visibility);
ad27b22e
FB
1889 }
1890 }
1891
00f83317 1892 public function iterProfiles($limit = null, $fields = 0x0000, $visibility = null)
0d906109 1893 {
00f83317 1894 return Profile::iterOverPIDs($this->getPIDs($limit), true, $fields, $visibility);
0d906109
RB
1895 }
1896
434570c4 1897 public function get($limit = null)
d865c296 1898 {
7ca75030 1899 return $this->getUsers($limit);
4927ee54
FB
1900 }
1901
aaf70eb8 1902
d865c296 1903 public function getTotalCount()
4927ee54 1904 {
2daf7250
RB
1905 return $this->getTotalUserCount();
1906 }
1907
1908 public function getTotalUserCount()
1909 {
1910 if (is_null($this->lastusercount)) {
1911 $this->requireAccounts();
aa21c568 1912 $this->buildQuery();
2daf7250
RB
1913 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
1914 ' . $this->query);
1915 } else {
1916 return $this->lastusercount;
1917 }
1918 }
1919
1920 public function getTotalProfileCount()
1921 {
1922 if (is_null($this->lastprofilecount)) {
1923 $this->requireProfiles();
1924 $this->buildQuery();
1925 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT p.pid)
7e735012 1926 ' . $this->query);
38c6fe96 1927 } else {
2daf7250 1928 return $this->lastprofilecount;
38c6fe96 1929 }
4927ee54
FB
1930 }
1931
9b8e5fb4 1932 public function setCondition(PlFilterCondition &$cond)
a087cc8d
FB
1933 {
1934 $this->root =& $cond;
784745ce 1935 $this->query = null;
a087cc8d
FB
1936 }
1937
9b8e5fb4 1938 public function addSort(PlFilterOrder &$sort)
24e08e33 1939 {
ccc951d9
RB
1940 if (count($this->sort) == 0 && $sort instanceof PlFilterGroupableOrder)
1941 {
1942 $this->grouper = $sort;
1943 }
d865c296
FB
1944 $this->sort[] = $sort;
1945 $this->orderby = null;
24e08e33
FB
1946 }
1947
a087cc8d
FB
1948 static public function getLegacy($promo_min, $promo_max)
1949 {
a087cc8d 1950 if ($promo_min != 0) {
784745ce 1951 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
5dd9d823 1952 } else {
88c31faf 1953 $min = new PFC_True();
a087cc8d 1954 }
a087cc8d 1955 if ($promo_max != 0) {
784745ce 1956 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
a087cc8d 1957 } else {
88c31faf 1958 $max = new PFC_True();
a087cc8d 1959 }
9b8e5fb4 1960 return new UserFilter(new PFC_And($min, $max));
a087cc8d 1961 }
784745ce 1962
07eb5b0e
FB
1963 static public function sortByName()
1964 {
913a4e90 1965 return array(new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME));
07eb5b0e
FB
1966 }
1967
1968 static public function sortByPromo()
1969 {
913a4e90 1970 return array(new UFO_Promo(), new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME));
07eb5b0e
FB
1971 }
1972
aa21c568
FB
1973 static private function getDBSuffix($string)
1974 {
2d6329a2
FB
1975 if (is_array($string)) {
1976 if (count($string) == 1) {
1977 return self::getDBSuffix(array_pop($string));
1978 }
1979 return md5(implode('|', $string));
1980 } else {
1981 return preg_replace('/[^a-z0-9]/i', '', $string);
1982 }
aa21c568
FB
1983 }
1984
1985
2d83cac9
RB
1986 /** Stores a new (and unique) table alias in the &$table table
1987 * @param &$table Array in which the table alias must be stored
1988 * @param $val Value which will then be used to build the join
1989 * @return Name of the newly created alias
1990 */
aa21c568
FB
1991 private $option = 0;
1992 private function register_optional(array &$table, $val)
1993 {
1994 if (is_null($val)) {
1995 $sub = $this->option++;
1996 $index = null;
1997 } else {
1998 $sub = self::getDBSuffix($val);
1999 $index = $val;
2000 }
2001 $sub = '_' . $sub;
2002 $table[$sub] = $index;
2003 return $sub;
2004 }
784745ce 2005
b8dcf62d
RB
2006 /** PROFILE VS ACCOUNT
2007 */
f7ea7450
RB
2008 private $with_profiles = false;
2009 private $with_accounts = false;
b8dcf62d
RB
2010 public function requireAccounts()
2011 {
2012 $this->with_accounts = true;
2013 }
2014
a9ef52c9
RB
2015 public function accountsRequired()
2016 {
2017 return $this->with_accounts;
2018 }
2019
b8dcf62d
RB
2020 public function requireProfiles()
2021 {
2022 $this->with_profiles = true;
2023 }
2024
a9ef52c9
RB
2025 public function profilesRequired()
2026 {
2027 return $this->with_profiles;
2028 }
2029
b8dcf62d
RB
2030 protected function accountJoins()
2031 {
2032 $joins = array();
2a93b634 2033 if ($this->with_profiles && $this->with_accounts) {
5c412626
FB
2034 $joins['ap'] = PlSqlJoin::left('account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)');
2035 $joins['p'] = PlSqlJoin::left('profiles', '$PID = ap.pid');
b8dcf62d
RB
2036 }
2037 return $joins;
2038 }
2039
d865c296
FB
2040 /** DISPLAY
2041 */
38c6fe96 2042 const DISPLAY = 'display';
d865c296
FB
2043 private $pd = false;
2044 public function addDisplayFilter()
2045 {
b8dcf62d 2046 $this->requireProfiles();
d865c296
FB
2047 $this->pd = true;
2048 return '';
2049 }
2050
9b8e5fb4 2051 protected function displayJoins()
d865c296
FB
2052 {
2053 if ($this->pd) {
5c412626 2054 return array('pd' => PlSqlJoin::left('profile_display', '$ME.pid = $PID'));
d865c296
FB
2055 } else {
2056 return array();
2057 }
2058 }
2059
f73a4f1b
RB
2060 /** LOGGER
2061 */
2062
2063 private $with_logger = false;
2064 public function addLoggerFilter()
2065 {
2066 $this->with_logger = true;
2067 $this->requireAccounts();
2068 return 'ls';
2069 }
2070 protected function loggerJoins()
2071 {
2072 $joins = array();
2073 if ($this->with_logger) {
5c412626 2074 $joins['ls'] = PlSqlJoin::left('log_sessions', '$ME.uid = $UID');
f73a4f1b
RB
2075 }
2076 return $joins;
2077 }
2078
784745ce
FB
2079 /** NAMES
2080 */
784745ce
FB
2081
2082 static public function assertName($name)
2083 {
07613cdd 2084 if (!DirEnum::getID(DirEnum::NAMETYPES, $name)) {
9b8e5fb4 2085 Platal::page()->kill('Invalid name type: ' . $name);
784745ce
FB
2086 }
2087 }
2088
2089 private $pn = array();
784745ce
FB
2090 public function addNameFilter($type, $variant = null)
2091 {
b8dcf62d 2092 $this->requireProfiles();
784745ce
FB
2093 if (!is_null($variant)) {
2094 $ft = $type . '_' . $variant;
2095 } else {
2096 $ft = $type;
2097 }
2098 $sub = '_' . $ft;
2099 self::assertName($ft);
2100
2101 if (!is_null($variant) && $variant == 'other') {
aa21c568 2102 $sub .= $this->option++;
784745ce 2103 }
07613cdd 2104 $this->pn[$sub] = DirEnum::getID(DirEnum::NAMETYPES, $ft);
784745ce
FB
2105 return $sub;
2106 }
2107
9b8e5fb4 2108 protected function nameJoins()
784745ce
FB
2109 {
2110 $joins = array();
2111 foreach ($this->pn as $sub => $type) {
5c412626 2112 $joins['pn' . $sub] = PlSqlJoin::left('profile_name', '$ME.pid = $PID AND $ME.typeid = {?}', $type);
784745ce
FB
2113 }
2114 return $joins;
2115 }
2116
40585144
RB
2117 /** NAMETOKENS
2118 */
2a93b634
RB
2119 private $name_tokens = array();
2120 private $nb_tokens = 0;
2121
2122 public function addNameTokensFilter($token)
40585144
RB
2123 {
2124 $this->requireProfiles();
2a93b634
RB
2125 $sub = 'sn' . (1 + $this->nb_tokens);
2126 $this->nb_tokens++;
2127 $this->name_tokens[$sub] = $token;
2128 return $sub;
40585144
RB
2129 }
2130
2131 protected function nameTokensJoins()
2132 {
f7ea7450 2133 /* We don't return joins, since with_sn forces the SELECT to run on search_name first */
2a93b634
RB
2134 $joins = array();
2135 foreach ($this->name_tokens as $sub => $token) {
2136 $joins[$sub] = PlSqlJoin::left('search_name', '$ME.pid = $PID');
40585144 2137 }
2a93b634
RB
2138 return $joins;
2139 }
2140
2141 public function getNameTokens()
2142 {
2143 return $this->name_tokens;
40585144
RB
2144 }
2145
a7f8e48a
RB
2146 /** NATIONALITY
2147 */
2148
2149 private $with_nat = false;
2150 public function addNationalityFilter()
2151 {
2152 $this->with_nat = true;
2153 return 'ngc';
2154 }
2155
2156 protected function nationalityJoins()
2157 {
2158 $joins = array();
2159 if ($this->with_nat) {
5c412626 2160 $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
2161 }
2162 return $joins;
2163 }
2164
784745ce
FB
2165 /** EDUCATION
2166 */
2167 const GRADE_ING = 'Ing.';
2168 const GRADE_PHD = 'PhD';
2169 const GRADE_MST = 'M%';
2170 static public function isGrade($grade)
2171 {
93c2f133 2172 return ($grade !== 0) && ($grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST);
784745ce
FB
2173 }
2174
2175 static public function assertGrade($grade)
2176 {
2177 if (!self::isGrade($grade)) {
ad27b22e 2178 Platal::page()->killError("Diplôme non valide: $grade");
784745ce
FB
2179 }
2180 }
2181
d865c296
FB
2182 static public function promoYear($grade)
2183 {
2184 // XXX: Definition of promotion for phds and masters might change in near future.
2185 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
2186 }
2187
784745ce
FB
2188 private $pepe = array();
2189 private $with_pee = false;
784745ce
FB
2190 public function addEducationFilter($x = false, $grade = null)
2191 {
b8dcf62d 2192 $this->requireProfiles();
784745ce 2193 if (!$x) {
aa21c568
FB
2194 $index = $this->option;
2195 $sub = $this->option++;
784745ce
FB
2196 } else {
2197 self::assertGrade($grade);
2198 $index = $grade;
2199 $sub = $grade[0];
2200 $this->with_pee = true;
2201 }
2202 $sub = '_' . $sub;
2203 $this->pepe[$index] = $sub;
2204 return $sub;
2205 }
2206
9b8e5fb4 2207 protected function educationJoins()
784745ce
FB
2208 {
2209 $joins = array();
2210 if ($this->with_pee) {
5c412626 2211 $joins['pee'] = PlSqlJoin::inner('profile_education_enum', 'pee.abbreviation = \'X\'');
784745ce
FB
2212 }
2213 foreach ($this->pepe as $grade => $sub) {
2214 if ($this->isGrade($grade)) {
5c412626
FB
2215 $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID');
2216 $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE {?}', $grade);
784745ce 2217 } else {
5c412626
FB
2218 $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.pid = $PID');
2219 $joins['pee' . $sub] = PlSqlJoin::inner('profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
2220 $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
784745ce
FB
2221 }
2222 }
2223 return $joins;
2224 }
4927ee54
FB
2225
2226
2227 /** GROUPS
2228 */
2229 private $gpm = array();
4927ee54
FB
2230 public function addGroupFilter($group = null)
2231 {
b8dcf62d 2232 $this->requireAccounts();
4927ee54 2233 if (!is_null($group)) {
4aae4d2c 2234 if (is_int($group) || ctype_digit($group)) {
4927ee54
FB
2235 $index = $sub = $group;
2236 } else {
2237 $index = $group;
aa21c568 2238 $sub = self::getDBSuffix($group);
4927ee54
FB
2239 }
2240 } else {
aa21c568 2241 $sub = 'group_' . $this->option++;
4927ee54
FB
2242 $index = null;
2243 }
2244 $sub = '_' . $sub;
2245 $this->gpm[$sub] = $index;
2246 return $sub;
2247 }
2248
9b8e5fb4 2249 protected function groupJoins()
4927ee54
FB
2250 {
2251 $joins = array();
2252 foreach ($this->gpm as $sub => $key) {
2253 if (is_null($key)) {
5c412626
FB
2254 $joins['gpa' . $sub] = PlSqlJoin::inner('groups');
2255 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
4aae4d2c 2256 } else if (is_int($key) || ctype_digit($key)) {
5c412626 2257 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
4927ee54 2258 } else {
5c412626
FB
2259 $joins['gpa' . $sub] = PlSqlJoin::inner('groups', '$ME.diminutif = {?}', $key);
2260 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
4927ee54
FB
2261 }
2262 }
2263 return $joins;
0fb3713c
RB
2264 }
2265
2266 /** BINETS
2267 */
2268
a7f8e48a
RB
2269 private $with_bi = false;
2270 private $with_bd = false;
d7ddf29b 2271 public function addBinetsFilter($with_enum = false)
0fb3713c
RB
2272 {
2273 $this->requireProfiles();
a7f8e48a
RB
2274 $this->with_bi = true;
2275 if ($with_enum) {
2276 $this->with_bd = true;
2277 return 'bd';
2278 } else {
2279 return 'bi';
2280 }
0fb3713c
RB
2281 }
2282
2283 protected function binetsJoins()
2284 {
2285 $joins = array();
a7f8e48a 2286 if ($this->with_bi) {
5c412626 2287 $joins['bi'] = PlSqlJoin::left('profile_binets', '$ME.pid = $PID');
0fb3713c 2288 }
a7f8e48a 2289 if ($this->with_bd) {
5c412626 2290 $joins['bd'] = PlSqlJoin::left('profile_binet_enum', '$ME.id = bi.binet_id');
a7f8e48a 2291 }
0fb3713c 2292 return $joins;
4927ee54 2293 }
aa21c568
FB
2294
2295 /** EMAILS
2296 */
2297 private $e = array();
2298 public function addEmailRedirectFilter($email = null)
2299 {
b8dcf62d 2300 $this->requireAccounts();
aa21c568
FB
2301 return $this->register_optional($this->e, $email);
2302 }
2303
2304 private $ve = array();
2305 public function addVirtualEmailFilter($email = null)
2306 {
21401768 2307 $this->addAliasFilter(self::ALIAS_FORLIFE);
aa21c568
FB
2308 return $this->register_optional($this->ve, $email);
2309 }
2310
21401768
FB
2311 const ALIAS_BEST = 'bestalias';
2312 const ALIAS_FORLIFE = 'forlife';
aa21c568
FB
2313 private $al = array();
2314 public function addAliasFilter($alias = null)
2315 {
b8dcf62d 2316 $this->requireAccounts();
aa21c568
FB
2317 return $this->register_optional($this->al, $alias);
2318 }
2319
9b8e5fb4 2320 protected function emailJoins()
aa21c568
FB
2321 {
2322 global $globals;
2323 $joins = array();
2324 foreach ($this->e as $sub=>$key) {
2325 if (is_null($key)) {
5c412626 2326 $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
aa21c568 2327 } else {
2d6329a2
FB
2328 if (!is_array($key)) {
2329 $key = array($key);
2330 }
aab2ffdd 2331 $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'
2f1c94e0 2332 AND $ME.email IN {?}', $key);
aa21c568
FB
2333 }
2334 }
21401768 2335 foreach ($this->al as $sub=>$key) {
aa21c568 2336 if (is_null($key)) {
5c412626 2337 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
21401768 2338 } else if ($key == self::ALIAS_BEST) {
5c412626 2339 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
21401768 2340 } else if ($key == self::ALIAS_FORLIFE) {
5c412626 2341 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type = \'a_vie\'');
aa21c568 2342 } else {
2d6329a2
FB
2343 if (!is_array($key)) {
2344 $key = array($key);
2345 }
aab2ffdd 2346 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')
bde68f05 2347 AND $ME.alias IN {?}', $key);
aa21c568 2348 }
aa21c568 2349 }
21401768 2350 foreach ($this->ve as $sub=>$key) {
aa21c568 2351 if (is_null($key)) {
5c412626 2352 $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\'');
aa21c568 2353 } else {
2d6329a2
FB
2354 if (!is_array($key)) {
2355 $key = array($key);
2356 }
bde68f05 2357 $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\' AND $ME.alias IN {?}', $key);
aa21c568 2358 }
5c412626
FB
2359 $joins['vr' . $sub] = PlSqlJoin::left('virtual_redirect',
2360 '$ME.vid = v' . $sub . '.vid
2361 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
2362 CONCAT(al_forlife.alias, \'@\', {?}),
2363 a.email))',
2364 $globals->mail->domain, $globals->mail->domain2);
aa21c568
FB
2365 }
2366 return $joins;
2367 }
3f42a6ad
FB
2368
2369
c4b24511
RB
2370 /** ADDRESSES
2371 */
036d1637 2372 private $with_pa = false;
c4b24511
RB
2373 public function addAddressFilter()
2374 {
b8dcf62d 2375 $this->requireProfiles();
036d1637
RB
2376 $this->with_pa = true;
2377 return 'pa';
c4b24511
RB
2378 }
2379
2b9ca54d
RB
2380 private $with_pac = false;
2381 public function addAddressCountryFilter()
2382 {
2383 $this->requireProfiles();
2384 $this->addAddressFilter();
2385 $this->with_pac = true;
2386 return 'gc';
2387 }
2388
d7ddf29b 2389 private $with_pal = false;
2b9ca54d
RB
2390 public function addAddressLocalityFilter()
2391 {
2392 $this->requireProfiles();
2393 $this->addAddressFilter();
2394 $this->with_pal = true;
2395 return 'gl';
2396 }
2397
9b8e5fb4 2398 protected function addressJoins()
c4b24511
RB
2399 {
2400 $joins = array();
036d1637 2401 if ($this->with_pa) {
5c412626 2402 $joins['pa'] = PlSqlJoin::left('profile_addresses', '$ME.pid = $PID');
c4b24511 2403 }
2b9ca54d 2404 if ($this->with_pac) {
5c412626 2405 $joins['gc'] = PlSqlJoin::left('geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID');
2b9ca54d
RB
2406 }
2407 if ($this->with_pal) {
5c412626 2408 $joins['gl'] = PlSqlJoin::left('geoloc_localities', '$ME.id = pa.localityID');
2b9ca54d 2409 }
c4b24511
RB
2410 return $joins;
2411 }
2412
2413
4083b126
RB
2414 /** CORPS
2415 */
2416
2417 private $pc = false;
2418 private $pce = array();
2419 private $pcr = false;
2420 public function addCorpsFilter($type)
2421 {
b8dcf62d 2422 $this->requireProfiles();
4083b126
RB
2423 $this->pc = true;
2424 if ($type == UFC_Corps::CURRENT) {
2425 $pce['pcec'] = 'current_corpsid';
2426 return 'pcec';
2427 } else if ($type == UFC_Corps::ORIGIN) {
2428 $pce['pceo'] = 'original_corpsid';
2429 return 'pceo';
2430 }
2431 }
2432
2433 public function addCorpsRankFilter()
2434 {
b8dcf62d 2435 $this->requireProfiles();
4083b126
RB
2436 $this->pc = true;
2437 $this->pcr = true;
2438 return 'pcr';
2439 }
2440
9b8e5fb4 2441 protected function corpsJoins()
4083b126
RB
2442 {
2443 $joins = array();
2444 if ($this->pc) {
5c412626 2445 $joins['pc'] = PlSqlJoin::left('profile_corps', '$ME.pid = $PID');
4083b126
RB
2446 }
2447 if ($this->pcr) {
5c412626 2448 $joins['pcr'] = PlSqlJoin::left('profile_corps_rank_enum', '$ME.id = pc.rankid');
4083b126
RB
2449 }
2450 foreach($this->pce as $sub => $field) {
5c412626 2451 $joins[$sub] = PlSqlJoin::left('profile_corps_enum', '$ME.id = pc.' . $field);
4083b126
RB
2452 }
2453 return $joins;
2454 }
2455
6a99c3ac
RB
2456 /** JOBS
2457 */
2458
61f61261
RB
2459 const JOB_SECTOR = 0x0001;
2460 const JOB_SUBSECTOR = 0x0002;
2461 const JOB_SUBSUBSECTOR = 0x0004;
2462 const JOB_ALTERNATES = 0x0008;
2463 const JOB_USERDEFINED = 0x0010;
2464 const JOB_CV = 0x0020;
2465
2466 const JOB_SECTORIZATION = 0x000F;
2467 const JOB_ANY = 0x003F;
6a99c3ac
RB
2468
2469 /** Joins :
2470 * pj => profile_job
2471 * pje => profile_job_enum
2472 * pjse => profile_job_sector_enum
2473 * pjsse => profile_job_subsector_enum
2474 * pjssse => profile_job_subsubsector_enum
2475 * pja => profile_job_alternates
2476 */
2477 private $with_pj = false;
2478 private $with_pje = false;
2479 private $with_pjse = false;
2480 private $with_pjsse = false;
2481 private $with_pjssse = false;
2482 private $with_pja = false;
2483
2484 public function addJobFilter()
2485 {
b8dcf62d 2486 $this->requireProfiles();
6a99c3ac
RB
2487 $this->with_pj = true;
2488 return 'pj';
2489 }
2490
2491 public function addJobCompanyFilter()
2492 {
2493 $this->addJobFilter();
2494 $this->with_pje = true;
2495 return 'pje';
2496 }
2497
2498 public function addJobSectorizationFilter($type)
2499 {
2500 $this->addJobFilter();
2501 if ($type == self::JOB_SECTOR) {
2502 $this->with_pjse = true;
2503 return 'pjse';
2504 } else if ($type == self::JOB_SUBSECTOR) {
2505 $this->with_pjsse = true;
2506 return 'pjsse';
2507 } else if ($type == self::JOB_SUBSUBSECTOR) {
2508 $this->with_pjssse = true;
2509 return 'pjssse';
2510 } else if ($type == self::JOB_ALTERNATES) {
2511 $this->with_pja = true;
2512 return 'pja';
2513 }
2514 }
2515
9b8e5fb4 2516 protected function jobJoins()
6a99c3ac
RB
2517 {
2518 $joins = array();
2519 if ($this->with_pj) {
5c412626 2520 $joins['pj'] = PlSqlJoin::left('profile_job', '$ME.pid = $PID');
6a99c3ac
RB
2521 }
2522 if ($this->with_pje) {
5c412626 2523 $joins['pje'] = PlSqlJoin::left('profile_job_enum', '$ME.id = pj.jobid');
6a99c3ac
RB
2524 }
2525 if ($this->with_pjse) {
5c412626 2526 $joins['pjse'] = PlSqlJoin::left('profile_job_sector_enum', '$ME.id = pj.sectorid');
6a99c3ac
RB
2527 }
2528 if ($this->with_pjsse) {
5c412626 2529 $joins['pjsse'] = PlSqlJoin::left('profile_job_subsector_enum', '$ME.id = pj.subsectorid');
6a99c3ac
RB
2530 }
2531 if ($this->with_pjssse) {
5c412626 2532 $joins['pjssse'] = PlSqlJoin::left('profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
6a99c3ac
RB
2533 }
2534 if ($this->with_pja) {
5c412626 2535 $joins['pja'] = PlSqlJoin::left('profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
6a99c3ac
RB
2536 }
2537 return $joins;
2538 }
2539
0a2e9c74
RB
2540 /** NETWORKING
2541 */
2542
2543 private $with_pnw = false;
2544 public function addNetworkingFilter()
2545 {
b8dcf62d 2546 $this->requireAccounts();
0a2e9c74
RB
2547 $this->with_pnw = true;
2548 return 'pnw';
2549 }
2550
9b8e5fb4 2551 protected function networkingJoins()
0a2e9c74
RB
2552 {
2553 $joins = array();
2554 if ($this->with_pnw) {
5c412626 2555 $joins['pnw'] = PlSqlJoin::left('profile_networking', '$ME.pid = $PID');
0a2e9c74
RB
2556 }
2557 return $joins;
2558 }
2559
6d62969e
RB
2560 /** PHONE
2561 */
2562
2d83cac9 2563 private $with_ptel = false;
6d62969e
RB
2564
2565 public function addPhoneFilter()
2566 {
b8dcf62d 2567 $this->requireAccounts();
2d83cac9 2568 $this->with_ptel = true;
6d62969e
RB
2569 return 'ptel';
2570 }
2571
9b8e5fb4 2572 protected function phoneJoins()
6d62969e
RB
2573 {
2574 $joins = array();
2d83cac9 2575 if ($this->with_ptel) {
5c412626 2576 $joins['ptel'] = PlSqlJoin::left('profile_phones', '$ME.pid = $PID');
6d62969e
RB
2577 }
2578 return $joins;
2579 }
2580
ceb512d2
RB
2581 /** MEDALS
2582 */
2583
2d83cac9 2584 private $with_pmed = false;
ceb512d2
RB
2585 public function addMedalFilter()
2586 {
b8dcf62d 2587 $this->requireProfiles();
2d83cac9 2588 $this->with_pmed = true;
ceb512d2
RB
2589 return 'pmed';
2590 }
2591
9b8e5fb4 2592 protected function medalJoins()
ceb512d2
RB
2593 {
2594 $joins = array();
2d83cac9 2595 if ($this->with_pmed) {
5c412626 2596 $joins['pmed'] = PlSqlJoin::left('profile_medals', '$ME.pid = $PID');
ceb512d2
RB
2597 }
2598 return $joins;
2599 }
2600
671b7073
RB
2601 /** MENTORING
2602 */
2603
2604 private $pms = array();
2605 const MENTOR_EXPERTISE = 1;
2606 const MENTOR_COUNTRY = 2;
2607 const MENTOR_SECTOR = 3;
2608
2609 public function addMentorFilter($type)
2610 {
b8dcf62d 2611 $this->requireAccounts();
671b7073 2612 switch($type) {
4a93c3a3
RB
2613 case self::MENTOR_EXPERTISE:
2614 $this->pms['pme'] = 'profile_mentor';
671b7073 2615 return 'pme';
4a93c3a3
RB
2616 case self::MENTOR_COUNTRY:
2617 $this->pms['pmc'] = 'profile_mentor_country';
671b7073 2618 return 'pmc';
4a93c3a3
RB
2619 case self::MENTOR_SECTOR:
2620 $this->pms['pms'] = 'profile_mentor_sector';
671b7073
RB
2621 return 'pms';
2622 default:
5d2e55c7 2623 Platal::page()->killError("Undefined mentor filter.");
671b7073
RB
2624 }
2625 }
2626
9b8e5fb4 2627 protected function mentorJoins()
671b7073
RB
2628 {
2629 $joins = array();
2630 foreach ($this->pms as $sub => $tab) {
5c412626 2631 $joins[$sub] = PlSqlJoin::left($tab, '$ME.pid = $PID');
671b7073
RB
2632 }
2633 return $joins;
2634 }
2635
3f42a6ad
FB
2636 /** CONTACTS
2637 */
2638 private $cts = array();
2639 public function addContactFilter($uid = null)
2640 {
c96da6c1 2641 $this->requireProfiles();
3f42a6ad
FB
2642 return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid);
2643 }
2644
9b8e5fb4 2645 protected function contactJoins()
3f42a6ad
FB
2646 {
2647 $joins = array();
2648 foreach ($this->cts as $sub=>$key) {
2649 if (is_null($key)) {
5c412626 2650 $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.contact = $PID');
3f42a6ad 2651 } else {
5c412626 2652 $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.uid = {?} AND $ME.contact = $PID', substr($key, 5));
3f42a6ad
FB
2653 }
2654 }
2655 return $joins;
2656 }
4e7bf1e0
FB
2657
2658
2659 /** CARNET
2660 */
2661 private $wn = array();
2662 public function addWatchRegistrationFilter($uid = null)
2663 {
b8dcf62d 2664 $this->requireAccounts();
4e7bf1e0
FB
2665 return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid);
2666 }
2667
2668 private $wp = array();
2669 public function addWatchPromoFilter($uid = null)
2670 {
b8dcf62d 2671 $this->requireAccounts();
4e7bf1e0
FB
2672 return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid);
2673 }
2674
2675 private $w = array();
2676 public function addWatchFilter($uid = null)
2677 {
b8dcf62d 2678 $this->requireAccounts();
4e7bf1e0
FB
2679 return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid);
2680 }
2681
9b8e5fb4 2682 protected function watchJoins()
4e7bf1e0
FB
2683 {
2684 $joins = array();
2685 foreach ($this->w as $sub=>$key) {
2686 if (is_null($key)) {
5c412626 2687 $joins['w' . $sub] = PlSqlJoin::left('watch');
4e7bf1e0 2688 } else {
5c412626 2689 $joins['w' . $sub] = PlSqlJoin::left('watch', '$ME.uid = {?}', substr($key, 5));
4e7bf1e0
FB
2690 }
2691 }
2692 foreach ($this->wn as $sub=>$key) {
2693 if (is_null($key)) {
5c412626 2694 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID');
4e7bf1e0 2695 } else {
5c412626 2696 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
4e7bf1e0
FB
2697 }
2698 }
2699 foreach ($this->wn as $sub=>$key) {
2700 if (is_null($key)) {
5c412626 2701 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID');
4e7bf1e0 2702 } else {
5c412626 2703 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
4e7bf1e0
FB
2704 }
2705 }
2706 foreach ($this->wp as $sub=>$key) {
2707 if (is_null($key)) {
5c412626 2708 $joins['wp' . $sub] = PlSqlJoin::left('watch_promo');
4e7bf1e0 2709 } else {
5c412626 2710 $joins['wp' . $sub] = PlSqlJoin::left('watch_promo', '$ME.uid = {?}', substr($key, 5));
4e7bf1e0
FB
2711 }
2712 }
2713 return $joins;
2714 }
48885bbe
FB
2715
2716
470d14f6
FB
2717 /** PHOTOS
2718 */
2719 private $with_photo;
2720 public function addPhotoFilter()
2721 {
2722 $this->requireProfiles();
2723 $this->with_photo = true;
2724 }
2725
2726 protected function photoJoins()
2727 {
2728 if ($this->with_photo) {
2729 return array('photo' => PlSqlJoin::left('profile_photos', '$ME.pid = $PID'));
2730 } else {
2731 return array();
2732 }
2733 }
2734
2735
48885bbe
FB
2736 /** MARKETING
2737 */
2738 private $with_rm;
2739 public function addMarketingHash()
2740 {
2741 $this->requireAccounts();
2742 $this->with_rm = true;
2743 }
2744
2745 protected function marketingJoins()
2746 {
2747 if ($this->with_rm) {
5c412626 2748 return array('rm' => PlSqlJoin::left('register_marketing', '$ME.uid = $UID'));
48885bbe
FB
2749 } else {
2750 return array();
2751 }
2752 }
a087cc8d 2753}
8363588b 2754// }}}
3f42a6ad 2755
a7d9ab89
RB
2756// {{{ class ProfileFilter
2757class ProfileFilter extends UserFilter
2758{
434570c4 2759 public function get($limit = null)
a7d9ab89
RB
2760 {
2761 return $this->getProfiles($limit);
2762 }
2daf7250
RB
2763
2764 public function filter(array $profiles, $limit = null)
2765 {
2766 return $this->filterProfiles($profiles, self::defaultLimit($limit));
2767 }
2768
2769 public function getTotalCount()
2770 {
2771 return $this->getTotalProfileCount();
2772 }
ccc951d9
RB
2773
2774 public function getGroups()
2775 {
2776 return $this->getPIDGroups();
2777 }
a7d9ab89
RB
2778}
2779// }}}
2780
a087cc8d
FB
2781// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
2782?>