Improve UFC_Nationality, adds UFC_Binets, UFC_Formation
[platal.git] / classes / userfilter.php
CommitLineData
a087cc8d
FB
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
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
8363588b 42// {{{ class UFC_Profile
53eae167
RB
43/** Filters users who have a profile
44 */
eb1449b8
FB
45class UFC_Profile implements UserFilterCondition
46{
dcc63ed5 47 public function buildCondition(PlFilter &$uf)
eb1449b8
FB
48 {
49 return '$PID IS NOT NULL';
50 }
51}
8363588b 52// }}}
eb1449b8 53
8363588b 54// {{{ class UFC_Promo
5d2e55c7 55/** Filters users based on promotion
53eae167
RB
56 * @param $comparison Comparison operator (>, =, ...)
57 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
5d2e55c7 58 * @param $promo Promotion on which the filter is based
53eae167 59 */
a087cc8d
FB
60class UFC_Promo implements UserFilterCondition
61{
a087cc8d
FB
62
63 private $grade;
64 private $promo;
65 private $comparison;
66
67 public function __construct($comparison, $grade, $promo)
68 {
69 $this->grade = $grade;
70 $this->comparison = $comparison;
71 $this->promo = $promo;
38c6fe96
FB
72 if ($this->grade != UserFilter::DISPLAY) {
73 UserFilter::assertGrade($this->grade);
74 }
a087cc8d
FB
75 }
76
dcc63ed5 77 public function buildCondition(PlFilter &$uf)
a087cc8d 78 {
38c6fe96
FB
79 if ($this->grade == UserFilter::DISPLAY) {
80 $sub = $uf->addDisplayFilter();
1a23a02b 81 return XDB::format('pd' . $sub . '.promo ' . $this->comparison . ' {?}', $this->promo);
38c6fe96
FB
82 } else {
83 $sub = $uf->addEducationFilter(true, $this->grade);
84 $field = 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
85 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison . ' ' . XDB::format('{?}', $this->promo);
86 }
784745ce
FB
87 }
88}
8363588b 89// }}}
784745ce 90
a7f8e48a
RB
91// {{{ class UFC_Formation
92class UFC_Formation implements UserFilterCondition
93{
94 private $eduid;
95 private $edutext;
96
97 public function __construct($eduid = null, $edutext = null)
98 {
99 $this->eduid = $eduid;
100 $this->edutext = $edutext;
101 }
102
103 public function buildCondition(PlFilter &$uf)
104 {
105 $sub = $uf->addEducationFilter();
106 if ($this->eduid != null) {
107 return 'pe' . $sub . '.eduid = ' . XDB::format('{?}', $this->eduid);
108 } else {
109 return 'pee' . $sub . '.name LIKE ' . XDB::format('CONCAT( \'%\', {?}, \'%\')', $this->edutext);
110 }
111 }
112}
113// }}}
114
8363588b 115// {{{ class UFC_Name
53eae167 116/** Filters users based on name
5d2e55c7 117 * @param $type Type of name field on which filtering is done (firstname, lastname...)
53eae167 118 * @param $text Text on which to filter
5d2e55c7 119 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
53eae167 120 */
784745ce
FB
121class UFC_Name implements UserFilterCondition
122{
123 const PREFIX = 1;
124 const SUFFIX = 2;
125 const PARTICLE = 7;
126 const VARIANTS = 8;
127 const CONTAINS = 3;
128
129 private $type;
130 private $text;
131 private $mode;
132
133 public function __construct($type, $text, $mode)
134 {
135 $this->type = $type;
136 $this->text = $text;
137 $this->mode = $mode;
138 }
139
140 private function buildNameQuery($type, $variant, $where, UserFilter &$uf)
141 {
142 $sub = $uf->addNameFilter($type, $variant);
143 return str_replace('$ME', 'pn' . $sub, $where);
144 }
145
dcc63ed5 146 public function buildCondition(PlFilter &$uf)
784745ce
FB
147 {
148 $left = '$ME.name';
149 $op = ' LIKE ';
150 if (($this->mode & self::PARTICLE) == self::PARTICLE) {
151 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
152 }
153 if (($this->mode & self::CONTAINS) == 0) {
154 $right = XDB::format('{?}', $this->text);
155 $op = ' = ';
156 } else if (($this->mode & self::CONTAINS) == self::PREFIX) {
157 $right = XDB::format('CONCAT({?}, \'%\')', $this->text);
158 } else if (($this->mode & self::CONTAINS) == self::SUFFIX) {
159 $right = XDB::format('CONCAT(\'%\', {?})', $this->text);
160 } else {
161 $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text);
162 }
163 $cond = $left . $op . $right;
164 $conds = array($this->buildNameQuery($this->type, null, $cond, $uf));
d865c296 165 if (($this->mode & self::VARIANTS) != 0 && isset(UserFilter::$name_variants[$this->type])) {
784745ce
FB
166 foreach (UserFilter::$name_variants[$this->type] as $var) {
167 $conds[] = $this->buildNameQuery($this->type, $var, $cond, $uf);
168 }
169 }
170 return implode(' OR ', $conds);
a087cc8d
FB
171 }
172}
8363588b 173// }}}
a087cc8d 174
40585144
RB
175// {{{ class UFC_NameTokens
176/** Selects users based on tokens in their name (for quicksearch)
177 * @param $tokens An array of tokens to search
178 * @param $flags Flags the tokens must have (e.g 'public' for public search)
179 * @param $soundex (bool) Whether those tokens are fulltext or soundex
180 */
181class UFC_NameTokens implements UserFilterCondition
182{
183 /* Flags */
184 const FLAG_PUBLIC = 'public';
185
186 private $tokens;
187 private $flags;
188 private $soundex;
79a0b464 189 private $exact;
40585144 190
79a0b464 191 public function __construct($tokens, $flags = array(), $soundex = false, $exact = false)
40585144
RB
192 {
193 $this->tokens = $tokens;
194 if (is_array($flags)) {
195 $this->flags = $flags;
196 } else {
197 $this->flags = array($flags);
198 }
199 $this->soundex = $soundex;
79a0b464 200 $this->exact = $exact;
40585144
RB
201 }
202
dcc63ed5 203 public function buildCondition(PlFilter &$uf)
40585144 204 {
79a0b464 205 $sub = $uf->addNameTokensFilter(!($this->exact || $this->soundex));
40585144
RB
206 $conds = array();
207 if ($this->soundex) {
208 $conds[] = $sub . '.soundex IN ' . XDB::formatArray($this->tokens);
79a0b464
RB
209 } else if ($this->exact) {
210 $conds[] = $sub . '.token IN ' . XDB::formatArray($this->tokens);
40585144
RB
211 } else {
212 $tokconds = array();
213 foreach ($this->tokens as $token) {
214 $tokconds[] = $sub . '.token LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $token);
215 }
216 $conds[] = implode(' OR ', $tokconds);
217 }
218
219 if ($this->flags != null) {
220 $conds[] = $sub . '.flags IN ' . XDB::formatArray($this->flags);
221 }
222
223 return implode(' AND ', $conds);
224 }
225}
226// }}}
227
0fb3713c
RB
228// {{{ class UFC_Nationality
229class UFC_Nationality implements UserFilterCondition
230{
a7f8e48a
RB
231 private $nid;
232 private $ntext;
0fb3713c 233
a7f8e48a 234 public function __construct($nid = null, $ntext = null)
0fb3713c 235 {
a7f8e48a
RB
236 $this->nid = $nid;
237 $this->ntext = $ntext;
0fb3713c
RB
238 }
239
240 public function buildCondition(PlFilter &$uf)
241 {
a7f8e48a
RB
242 if ($this->nid != null) {
243 $uf->requireProfiles();
244 $nat = XDB::format('{?}', $this->nid);
245 $conds = array(
246 'p.nationality1 = ' . $nat,
247 'p.nationality2 = ' . $nat,
248 'p.nationality3 = ' . $nat,
249 );
250 return implode(' OR ', $conds);
251 } else {
252 $sub = $uf->addNationalityJoins();
253 $nat = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->ntext);
254 $cond = $sub . '.nationality LIKE ' . $nat . ' OR ' .
255 $sub . '.nationalityFR LIKE ' . $nat;
256 return $cond;
257 }
0fb3713c
RB
258 }
259}
260// }}}
261
8363588b 262// {{{ class UFC_Dead
53eae167
RB
263/** Filters users based on death date
264 * @param $comparison Comparison operator
265 * @param $date Date to which death date should be compared
266 */
4927ee54
FB
267class UFC_Dead implements UserFilterCondition
268{
38c6fe96
FB
269 private $comparison;
270 private $date;
271
272 public function __construct($comparison = null, $date = null)
4927ee54 273 {
38c6fe96
FB
274 $this->comparison = $comparison;
275 $this->date = $date;
4927ee54
FB
276 }
277
dcc63ed5 278 public function buildCondition(PlFilter &$uf)
4927ee54 279 {
38c6fe96
FB
280 $str = 'p.deathdate IS NOT NULL';
281 if (!is_null($this->comparison)) {
282 $str .= ' AND p.deathdate ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date));
4927ee54 283 }
38c6fe96 284 return $str;
4927ee54
FB
285 }
286}
8363588b 287// }}}
4927ee54 288
8363588b 289// {{{ class UFC_Registered
53eae167
RB
290/** Filters users based on registration state
291 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
292 * @param $comparison Comparison operator
293 * @param $date Date to which users registration date should be compared
294 */
4927ee54
FB
295class UFC_Registered implements UserFilterCondition
296{
297 private $active;
38c6fe96
FB
298 private $comparison;
299 private $date;
300
301 public function __construct($active = false, $comparison = null, $date = null)
4927ee54 302 {
b2e8fc54 303 $this->active = $active;
38c6fe96
FB
304 $this->comparison = $comparison;
305 $this->date = $date;
4927ee54
FB
306 }
307
dcc63ed5 308 public function buildCondition(PlFilter &$uf)
4927ee54
FB
309 {
310 if ($this->active) {
38c6fe96 311 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
4927ee54 312 } else {
38c6fe96
FB
313 $date = 'a.uid IS NOT NULL AND a.state != \'pending\'';
314 }
315 if (!is_null($this->comparison)) {
316 $date .= ' AND a.registration_date ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date));
4927ee54 317 }
38c6fe96 318 return $date;
4927ee54
FB
319 }
320}
8363588b 321// }}}
4927ee54 322
8363588b 323// {{{ class UFC_ProfileUpdated
53eae167
RB
324/** Filters users based on profile update date
325 * @param $comparison Comparison operator
326 * @param $date Date to which profile update date must be compared
327 */
7e735012
FB
328class UFC_ProfileUpdated implements UserFilterCondition
329{
330 private $comparison;
331 private $date;
332
333 public function __construct($comparison = null, $date = null)
334 {
335 $this->comparison = $comparison;
336 $this->date = $date;
337 }
338
dcc63ed5 339 public function buildCondition(PlFilter &$uf)
7e735012
FB
340 {
341 return 'p.last_change ' . $this->comparison . XDB::format(' {?}', date('Y-m-d H:i:s', $this->date));
342 }
343}
8363588b 344// }}}
7e735012 345
8363588b 346// {{{ class UFC_Birthday
53eae167
RB
347/** Filters users based on next birthday date
348 * @param $comparison Comparison operator
349 * @param $date Date to which users next birthday date should be compared
350 */
7e735012
FB
351class UFC_Birthday implements UserFilterCondition
352{
353 private $comparison;
354 private $date;
355
356 public function __construct($comparison = null, $date = null)
357 {
358 $this->comparison = $comparison;
359 $this->date = $date;
360 }
361
dcc63ed5 362 public function buildCondition(PlFilter &$uf)
7e735012
FB
363 {
364 return 'p.next_birthday ' . $this->comparison . XDB::format(' {?}', date('Y-m-d', $this->date));
365 }
366}
8363588b 367// }}}
7e735012 368
8363588b 369// {{{ class UFC_Sex
53eae167
RB
370/** Filters users based on sex
371 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
372 */
4927ee54
FB
373class UFC_Sex implements UserFilterCondition
374{
375 private $sex;
376 public function __construct($sex)
377 {
378 $this->sex = $sex;
379 }
380
dcc63ed5 381 public function buildCondition(PlFilter &$uf)
4927ee54
FB
382 {
383 if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) {
384 return self::COND_FALSE;
385 } else {
24e08e33 386 return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male');
4927ee54
FB
387 }
388 }
389}
8363588b 390// }}}
4927ee54 391
8363588b 392// {{{ class UFC_Group
53eae167 393/** Filters users based on group membership
5d2e55c7
RB
394 * @param $group Group whose members we are selecting
395 * @param $anim Whether to restrict selection to animators of that group
53eae167 396 */
4927ee54
FB
397class UFC_Group implements UserFilterCondition
398{
399 private $group;
5d2e55c7
RB
400 private $anim;
401 public function __construct($group, $anim = false)
4927ee54
FB
402 {
403 $this->group = $group;
5d2e55c7 404 $this->anim = $anim;
4927ee54
FB
405 }
406
dcc63ed5 407 public function buildCondition(PlFilter &$uf)
4927ee54
FB
408 {
409 $sub = $uf->addGroupFilter($this->group);
410 $where = 'gpm' . $sub . '.perms IS NOT NULL';
5d2e55c7 411 if ($this->anim) {
4927ee54
FB
412 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
413 }
414 return $where;
415 }
416}
8363588b 417// }}}
4927ee54 418
0fb3713c
RB
419// {{{ class UFC_Binet
420class UFC_Binet implements UserFilterCondition
421{
a7f8e48a
RB
422 private $binetid;
423 private $binetname;
0fb3713c 424
a7f8e48a 425 public function __construct($binetid = null, $binetname = null)
0fb3713c 426 {
a7f8e48a
RB
427 $this->binetid = $binetid;
428 $this->binetname = $binetname;
0fb3713c
RB
429 }
430
431 public function buildCondition(PlFilter &$uf)
432 {
a7f8e48a
RB
433 if ($this->binetid != null) {
434 $sub = $uf->addBinetsFilter();
435 return $sub . 'binet_id = ' . XDB::format('{?}', $this->binetid);
436 } else {
437 $sub = $uf->addBinetsFilter(true);
438 return $sub . 'test LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->binetname);
439 }
440 }
441}
442// }}}
443
444// {{{ class UFC_Section
445class UFC_Section implements UserFilterCondition
446{
447 private $section;
448
449 public function __construct($section)
450 {
451 $this->section = $section;
452 }
453
454 public function buildCondition(PlFilter &$uf)
455 {
456 $uf->requireProfiles();
457 return 'p.section = ' . XDB::format('{?}', $this->section);
0fb3713c
RB
458 }
459}
460// }}}
461
8363588b 462// {{{ class UFC_Email
53eae167
RB
463/** Filters users based on email address
464 * @param $email Email whose owner we are looking for
465 */
aa21c568
FB
466class UFC_Email implements UserFilterCondition
467{
468 private $email;
469 public function __construct($email)
470 {
471 $this->email = $email;
472 }
473
dcc63ed5 474 public function buildCondition(PlFilter &$uf)
aa21c568
FB
475 {
476 if (User::isForeignEmailAddress($this->email)) {
477 $sub = $uf->addEmailRedirectFilter($this->email);
478 return XDB::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email);
21401768
FB
479 } else if (User::isVirtualEmailAddress($this->email)) {
480 $sub = $uf->addVirtualEmailFilter($this->email);
481 return 'vr' . $sub . '.redirect IS NOT NULL';
aa21c568 482 } else {
21401768
FB
483 @list($user, $domain) = explode('@', $this->email);
484 $sub = $uf->addAliasFilter($user);
aa21c568
FB
485 return 'al' . $sub . '.alias IS NOT NULL';
486 }
487 }
488}
8363588b 489// }}}
d865c296 490
8363588b 491// {{{ class UFC_EmailList
5d2e55c7 492/** Filters users based on an email list
53eae167
RB
493 * @param $emails List of emails whose owner must be selected
494 */
21401768
FB
495class UFC_EmailList implements UserFilterCondition
496{
497 private $emails;
498 public function __construct($emails)
499 {
500 $this->emails = $emails;
501 }
502
dcc63ed5 503 public function buildCondition(PlFilter &$uf)
21401768
FB
504 {
505 $email = null;
506 $virtual = null;
507 $alias = null;
508 $cond = array();
509
510 if (count($this->emails) == 0) {
dcc63ed5 511 return PlFilterCondition::COND_TRUE;
21401768
FB
512 }
513
514 foreach ($this->emails as $entry) {
515 if (User::isForeignEmailAddress($entry)) {
516 if (is_null($email)) {
517 $email = $uf->addEmailRedirectFilter();
518 }
519 $cond[] = XDB::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry);
520 } else if (User::isVirtualEmailAddress($entry)) {
521 if (is_null($virtual)) {
522 $virtual = $uf->addVirtualEmailFilter();
523 }
524 $cond[] = XDB::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry);
525 } else {
526 if (is_null($alias)) {
527 $alias = $uf->addAliasFilter();
528 }
529 @list($user, $domain) = explode('@', $entry);
530 $cond[] = XDB::format('al' . $alias . '.alias = {?}', $user);
531 }
532 }
533 return '(' . implode(') OR (', $cond) . ')';
534 }
535}
8363588b 536// }}}
d865c296 537
8363588b 538// {{{ class UFC_Address
2b9ca54d 539abstract class UFC_Address implements UserFilterCondition
c4b24511 540{
2b9ca54d 541 /** Valid address type ('hq' is reserved for company addresses)
036d1637 542 */
2b9ca54d
RB
543 const TYPE_HOME = 1;
544 const TYPE_PRO = 2;
545 const TYPE_ANY = 3;
c4b24511 546
2b9ca54d 547 /** Text for these types
036d1637 548 */
2b9ca54d
RB
549 protected static $typetexts = array(
550 self::TYPE_HOME => 'home',
551 self::TYPE_PRO => 'pro',
552 );
553
554 protected $type;
c4b24511 555
036d1637
RB
556 /** Flags for addresses
557 */
558 const FLAG_CURRENT = 0x0001;
559 const FLAG_TEMP = 0x0002;
560 const FLAG_SECOND = 0x0004;
561 const FLAG_MAIL = 0x0008;
562 const FLAG_CEDEX = 0x0010;
563
564 // Binary OR of those flags
565 const FLAG_ANY = 0x001F;
566
567 /** Text of these flags
568 */
2b9ca54d 569 protected static $flagtexts = array(
036d1637
RB
570 self::FLAG_CURRENT => 'current',
571 self::FLAG_TEMP => 'temporary',
572 self::FLAG_SECOND => 'secondary',
573 self::FLAG_MAIL => 'mail',
574 self::FLAG_CEDEX => 'cedex',
575 );
576
2b9ca54d
RB
577 protected $flags;
578
579 public function __construct($type = null, $flags = null)
580 {
581 $this->type = $type;
582 $this->flags = $flags;
583 }
584
585 protected function initConds($sub)
586 {
587 $conds = array();
588 $types = array();
589 foreach (self::$typetexts as $flag => $type) {
590 if ($flag & $this->type) {
591 $types[] = $type;
592 }
593 }
594 if (count($types)) {
595 $conds[] = $sub . '.type IN ' . XDB::formatArray($types);
596 }
597
598 if ($this->flags != self::FLAG_ANY) {
599 foreach(self::$flagtexts as $flag => $text) {
600 if ($flag & $this->flags) {
601 $conds[] = 'FIND_IN_SET(' . XDB::format('{?}', $text) . ', ' . $sub . '.flags)';
602 }
603 }
604 }
605 return $conds;
606 }
607
608}
609// }}}
610
611// {{{ class UFC_AddressText
612/** Select users based on their address, using full text search
613 * @param $text Text for filter in fulltext search
614 * @param $textSearchMode Mode for search (PREFIX, SUFFIX, ...)
615 * @param $type Filter on address type
616 * @param $flags Filter on address flags
617 * @param $country Filter on address country
618 * @param $locality Filter on address locality
619 */
620class UFC_AddressText extends UFC_Address
621{
622 /** Flags for text search
036d1637 623 */
2b9ca54d
RB
624 const PREFIX = 0x0001;
625 const SUFFIX = 0x0002;
626 const CONTAINS = 0x0003;
627
036d1637 628 private $text;
2b9ca54d
RB
629 private $textSearchMode;
630
631 public function __construct($text = null, $textSearchMode = self::CONTAINS,
632 $type = null, $flags = self::FLAG_ANY, $country = null, $locality = null)
633 {
634 parent::__construct($type, $flags);
635 $this->text = $text;
636 $this->textSearchMode = $textSearchMode;
637 $this->country = $country;
638 $this->locality = $locality;
639 }
640
641 private function mkMatch($txt)
642 {
643 $op = ' LIKE ';
644 if (($this->textSearchMode & self::CONTAINS) == 0) {
645 $right = XDB::format('{?}', $this->text);
646 $op = ' = ';
647 } else if (($this->mode & self::CONTAINS) == self::PREFIX) {
648 $right = XDB::format('CONCAT({?}, \'%\')', $this->text);
649 } else if (($this->mode & self::CONTAINS) == self::SUFFIX) {
650 $right = XDB::format('CONCAT(\'%\', {?})', $this->text);
651 } else {
652 $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text);
653 }
654 return $op . $right;
655 }
656
657 public function buildCondition(PlFilter &$uf)
658 {
659 $sub = $uf->addAddressFilter();
660 $conds = $this->initConds($sub);
661 if ($this->text != null) {
662 $conds[] = $sub . '.text' . $this->mkMatch($this->text);
663 }
664
665 if ($this->country != null) {
666 $subc = $uf->addAddressCountryFilter();
667 $subconds = array();
668 $subconds[] = $subc . '.country' . $this->mkMatch($this->country);
669 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country);
670 $conds[] = implode(' OR ', $subconds);
671 }
672
673 if ($this->locality != null) {
674 $subl = $uf->addAddressLocalityFilter();
675 $conds[] = $subl . '.name' . $this->mkMatch($this->locality);
676 }
677
678 return implode(' AND ', $conds);
679 }
680}
681// }}}
682
683// {{{ class UFC_AddressFields
684/** Filters users based on their address,
685 * @param $type Filter on address type
686 * @param $flags Filter on address flags
687 * @param $countryId Filter on address countryId
688 * @param $administrativeAreaId Filter on address administrativeAreaId
689 * @param $subAdministrativeAreaId Filter on address subAdministrativeAreaId
690 * @param $localityId Filter on address localityId
691 * @param $postalCode Filter on address postalCode
692 */
693class UFC_AddressFields extends UFC_Address
694{
695 /** Data of the filter
696 */
036d1637
RB
697 private $countryId;
698 private $administrativeAreaId;
699 private $subAdministrativeAreaId;
700 private $localityId;
701 private $postalCode;
702
2b9ca54d 703 public function __construct($type = null, $flags = self::FLAG_ANY, $countryId = null, $administrativeAreaId = null,
036d1637
RB
704 $subAdministrativeAreaId = null, $localityId = null, $postalCode = null)
705 {
2b9ca54d 706 parent::__construct($type, $flags);
036d1637
RB
707 $this->countryId = $countryId;
708 $this->administrativeAreaId = $administrativeAreaId;
709 $this->subAdministrativeAreaId = $subAdministrativeAreaId;
710 $this->localityId = $localityId;
711 $this->postalCode = $postalCode;
c4b24511
RB
712 }
713
dcc63ed5 714 public function buildCondition(PlFilter &$uf)
c4b24511 715 {
036d1637 716 $sub = $uf->addAddressFilter();
2b9ca54d 717 $conds = $this->initConds();
036d1637
RB
718
719 if ($this->countryId != null) {
720 $conds[] = $sub . '.countryId = ' . XDB::format('{?}', $this->countryId);
721 }
722 if ($this->administrativeAreaId != null) {
723 $conds[] = $sub . '.administrativeAreaId = ' . XDB::format('{?}', $this->administrativeAreaId);
724 }
725 if ($this->subAdministrativeAreaId != null) {
726 $conds[] = $sub . '.subAdministrativeAreaId = ' . XDB::format('{?}', $this->subAdministrativeAreaId);
727 }
728 if ($this->localityId != null) {
729 $conds[] = $sub . '.localityId = ' . XDB::format('{?}', $this->localityId);
730 }
731 if ($this->postalCode != null) {
732 $conds[] = $sub . '.postalCode = ' . XDB::format('{?}', $this->postalCode);
733 }
734
735 return implode(' AND ', $conds);
c4b24511
RB
736 }
737}
8363588b 738// }}}
c4b24511 739
8363588b 740// {{{ class UFC_Corps
4083b126
RB
741/** Filters users based on the corps they belong to
742 * @param $corps Corps we are looking for (abbreviation)
743 * @param $type Whether we search for original or current corps
744 */
745class UFC_Corps implements UserFilterCondition
746{
5d2e55c7
RB
747 const CURRENT = 1;
748 const ORIGIN = 2;
4083b126
RB
749
750 private $corps;
751 private $type;
752
753 public function __construct($corps, $type = self::CURRENT)
754 {
755 $this->corps = $corps;
756 $this->type = $type;
757 }
758
dcc63ed5 759 public function buildCondition(PlFilter &$uf)
4083b126 760 {
5d2e55c7
RB
761 /** Tables shortcuts:
762 * pc for profile_corps,
4083b126
RB
763 * pceo for profile_corps_enum - orginal
764 * pcec for profile_corps_enum - current
765 */
766 $sub = $uf->addCorpsFilter($this->type);
767 $cond = $sub . '.abbreviation = ' . $corps;
768 return $cond;
769 }
770}
8363588b 771// }}}
4083b126 772
8363588b 773// {{{ class UFC_Corps_Rank
4083b126
RB
774/** Filters users based on their rank in the corps
775 * @param $rank Rank we are looking for (abbreviation)
776 */
777class UFC_Corps_Rank implements UserFilterCondition
778{
779 private $rank;
780 public function __construct($rank)
781 {
782 $this->rank = $rank;
783 }
784
dcc63ed5 785 public function buildCondition(PlFilter &$uf)
4083b126 786 {
5d2e55c7 787 /** Tables shortcuts:
4083b126
RB
788 * pcr for profile_corps_rank
789 */
790 $sub = $uf->addCorpsRankFilter();
791 $cond = $sub . '.abbreviation = ' . $rank;
792 return $cond;
793 }
794}
8363588b 795// }}}
4083b126 796
6a99c3ac
RB
797// {{{ class UFC_Job_Company
798/** Filters users based on the company they belong to
799 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
800 * @param $value The searched value
801 */
4e2f2ad2 802class UFC_Job_Company implements UserFilterCondition
6a99c3ac
RB
803{
804 const JOBID = 'id';
805 const JOBNAME = 'name';
806 const JOBACRONYM = 'acronym';
807
808 private $type;
809 private $value;
810
811 public function __construct($type, $value)
812 {
813 $this->assertType($type);
814 $this->type = $type;
815 $this->value = $value;
816 }
817
818 private function assertType($type)
819 {
820 if ($type != self::JOBID && $type != self::JOBNAME && $type != self::JOBACRONYM) {
5d2e55c7 821 Platal::page()->killError("Type de recherche non valide.");
6a99c3ac
RB
822 }
823 }
824
dcc63ed5 825 public function buildCondition(PlFilter &$uf)
6a99c3ac
RB
826 {
827 $sub = $uf->addJobCompanyFilter();
828 $cond = $sub . '.' . $this->type . ' = ' . XDB::format('{?}', $this->value);
829 return $cond;
830 }
831}
832// }}}
833
834// {{{ class UFC_Job_Sectorization
835/** Filters users based on the ((sub)sub)sector they work in
836 * @param $sector The sector searched
837 * @param $subsector The subsector
838 * @param $subsubsector The subsubsector
839 */
4e2f2ad2 840class UFC_Job_Sectorization implements UserFilterCondition
6a99c3ac
RB
841{
842
843 private $sector;
844 private $subsector;
845 private $subsubsector;
846
847 public function __construct($sector = null, $subsector = null, $subsubsector = null)
848 {
849 $this->sector = $sector;
850 $this->subsector = $subsector;
851 $this->subsubsector = $subsubsector;
852 }
853
dcc63ed5 854 public function buildCondition(PlFilter &$uf)
6a99c3ac
RB
855 {
856 // No need to add the JobFilter, it will be done by addJobSectorizationFilter
857 $conds = array();
858 if ($this->sector !== null) {
859 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SECTOR);
860 $conds[] = $sub . '.id = ' . XDB::format('{?}', $this->sector);
861 }
862 if ($this->subsector !== null) {
863 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSECTOR);
864 $conds[] = $sub . '.id = ' . XDB::format('{?}', $this->subsector);
865 }
866 if ($this->subsubsector !== null) {
867 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSUBSECTOR);
868 $conds[] = $sub . '.id = ' . XDB::format('{?}', $this->subsubsector);
869 }
870 return implode(' AND ', $conds);
871 }
872}
873// }}}
874
875// {{{ class UFC_Job_Description
876/** Filters users based on their job description
877 * @param $description The text being searched for
878 * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
879 */
4e2f2ad2 880class UFC_Job_Description implements UserFilterCondition
6a99c3ac
RB
881{
882
883 /** Meta-filters
884 * Built with binary OR on UserFilter::JOB_*
885 */
01cc5f9e 886 const ANY = 63;
6a99c3ac
RB
887 const SECTORIZATION = 15;
888
889 private $description;
890 private $fields;
891
01cc5f9e 892 public function __construct($description, $fields)
6a99c3ac
RB
893 {
894 $this->fields = $fields;
895 $this->description = $description;
896 }
897
dcc63ed5 898 public function buildCondition(PlFilter &$uf)
6a99c3ac
RB
899 {
900 $conds = array();
901 if ($this->fields & UserFilter::JOB_USERDEFINED) {
902 $sub = $uf->addJobFilter();
903 $conds[] = $sub . '.description LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
904 }
01cc5f9e
RB
905 if ($this->fields & UserFilter::JOB_CV) {
906 $uf->requireProfiles();
907 $conds[] = 'p.cv LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
908 }
6a99c3ac
RB
909 if ($this->fields & UserFilter::JOB_SECTOR) {
910 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SECTOR);
911 $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
912 }
913 if ($this->fields & UserFilter::JOB_SUBSECTOR) {
914 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSECTOR);
915 $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
916 }
917 if ($this->fields & UserFilter::JOB_SUBSUBSECTOR) {
918 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSUBSECTOR);
919 $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
920 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_ALTERNATES);
921 $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
922 }
923 return implode(' OR ', $conds);
924 }
925}
926// }}}
927
0a2e9c74
RB
928// {{{ class UFC_Networking
929/** Filters users based on network identity (IRC, ...)
930 * @param $type Type of network (-1 for any)
931 * @param $value Value to search
932 */
4e2f2ad2 933class UFC_Networking implements UserFilterCondition
0a2e9c74
RB
934{
935 private $type;
936 private $value;
937
938 public function __construct($type, $value)
939 {
940 $this->type = $type;
941 $this->value = $value;
942 }
943
dcc63ed5 944 public function buildCondition(PlFilter &$uf)
0a2e9c74
RB
945 {
946 $sub = $uf->addNetworkingFilter();
947 $conds = array();
948 $conds[] = $sub . '.address = ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->value);
949 if ($this->type != -1) {
950 $conds[] = $sub . '.network_type = ' . XDB::format('{?}', $this->type);
951 }
952 return implode(' AND ', $conds);
953 }
954}
955// }}}
956
6d62969e
RB
957// {{{ class UFC_Phone
958/** Filters users based on their phone number
959 * @param $num_type Type of number (pro/user/home)
960 * @param $phone_type Type of phone (fixed/mobile/fax)
961 * @param $number Phone number
962 */
4e2f2ad2 963class UFC_Phone implements UserFilterCondition
6d62969e
RB
964{
965 const NUM_PRO = 'pro';
966 const NUM_USER = 'user';
967 const NUM_HOME = 'address';
968 const NUM_ANY = 'any';
969
970 const PHONE_FIXED = 'fixed';
971 const PHONE_MOBILE = 'mobile';
972 const PHONE_FAX = 'fax';
973 const PHONE_ANY = 'any';
974
975 private $num_type;
976 private $phone_type;
977 private $number;
978
979 public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY)
980 {
9b8e5fb4 981 require_once('profil.func.inc.php');
6d62969e
RB
982 $this->number = $number;
983 $this->num_type = $num_type;
984 $this->phone_type = format_phone_number($phone_type);
985 }
986
dcc63ed5 987 public function buildCondition(PlFilter &$uf)
6d62969e
RB
988 {
989 $sub = $uf->addPhoneFilter();
990 $conds = array();
991 $conds[] = $sub . '.search_tel = ' . XDB::format('{?}', $this->number);
992 if ($this->num_type != self::NUM_ANY) {
993 $conds[] = $sub . '.link_type = ' . XDB::format('{?}', $this->num_type);
994 }
995 if ($this->phone_type != self::PHONE_ANY) {
996 $conds[] = $sub . '.tel_type = ' . XDB::format('{?}', $this->phone_type);
997 }
998 return implode(' AND ', $conds);
999 }
1000}
1001// }}}
1002
ceb512d2
RB
1003// {{{ class UFC_Medal
1004/** Filters users based on their medals
1005 * @param $medal ID of the medal
1006 * @param $grade Grade of the medal (null for 'any')
1007 */
4e2f2ad2 1008class UFC_Medal implements UserFilterCondition
ceb512d2
RB
1009{
1010 private $medal;
1011 private $grade;
1012
1013 public function __construct($medal, $grade = null)
1014 {
1015 $this->medal = $medal;
1016 $this->grade = $grade;
1017 }
1018
dcc63ed5 1019 public function buildCondition(PlFilter &$uf)
ceb512d2
RB
1020 {
1021 $conds = array();
1022 $sub = $uf->addMedalFilter();
1023 $conds[] = $sub . '.mid = ' . XDB::format('{?}', $this->medal);
1024 if ($this->grade != null) {
1025 $conds[] = $sub . '.gid = ' . XDB::format('{?}', $this->grade);
1026 }
1027 return implode(' AND ', $conds);
1028 }
1029}
1030// }}}
1031
671b7073
RB
1032// {{{ class UFC_Mentor_Expertise
1033/** Filters users by mentoring expertise
1034 * @param $expertise Domain of expertise
1035 */
4e2f2ad2 1036class UFC_Mentor_Expertise implements UserFilterCondition
671b7073
RB
1037{
1038 private $expertise;
1039
1040 public function __construct($expertise)
1041 {
1042 $this->expertise = $expertise;
1043 }
1044
dcc63ed5 1045 public function buildCondition(PlFilter &$uf)
671b7073
RB
1046 {
1047 $sub = $uf->addMentorFilter(UserFilter::MENTOR_EXPERTISE);
1048 return $sub . '.expertise LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\'', $this->expertise);
1049 }
1050}
1051// }}}
1052
1053// {{{ class UFC_Mentor_Country
1054/** Filters users by mentoring country
1055 * @param $country Two-letters code of country being searched
1056 */
4e2f2ad2 1057class UFC_Mentor_Country implements UserFilterCondition
671b7073
RB
1058{
1059 private $country;
1060
1061 public function __construct($country)
1062 {
1063 $this->country = $country;
1064 }
1065
dcc63ed5 1066 public function buildCondition(PlFilter &$uf)
671b7073
RB
1067 {
1068 $sub = $uf->addMentorFilter(UserFilter::MENTOR_COUNTRY);
1069 return $sub . '.country = ' . XDB::format('{?}', $this->country);
1070 }
1071}
1072// }}}
1073
1074// {{{ class UFC_Mentor_Sectorization
1075/** Filters users based on mentoring (sub|)sector
1076 * @param $sector ID of sector
1077 * @param $subsector Subsector (null for any)
1078 */
4e2f2ad2 1079class UFC_Mentor_Sectorization implements UserFilterCondition
671b7073
RB
1080{
1081 private $sector;
1082 private $subsector;
1083
1084 public function __construct($sector, $subsector = null)
1085 {
1086 $this->sector = $sector;
1087 $this->subsubsector = $subsector;
1088 }
1089
dcc63ed5 1090 public function buildCondition(PlFilter &$uf)
671b7073
RB
1091 {
1092 $sub = $uf->addMentorFilter(UserFilter::MENTOR_SECTOR);
1093 $conds = array();
1094 $conds[] = $sub . '.sectorid = ' . XDB::format('{?}', $this->sector);
1095 if ($this->subsector != null) {
1096 $conds[] = $sub . '.subsectorid = ' . XDB::format('{?}', $this->subsector);
1097 }
1098 return implode(' AND ', $conds);
1099 }
1100}
1101// }}}
1102
8363588b 1103// {{{ class UFC_UserRelated
5d2e55c7 1104/** Filters users based on a relation toward a user
53eae167
RB
1105 * @param $user User to which searched users are related
1106 */
4e7bf1e0 1107abstract class UFC_UserRelated implements UserFilterCondition
3f42a6ad 1108{
009b8ab7
FB
1109 protected $user;
1110 public function __construct(PlUser &$user)
1111 {
1112 $this->user =& $user;
3f42a6ad 1113 }
4e7bf1e0 1114}
8363588b 1115// }}}
3f42a6ad 1116
8363588b 1117// {{{ class UFC_Contact
5d2e55c7 1118/** Filters users who belong to selected user's contacts
53eae167 1119 */
4e7bf1e0
FB
1120class UFC_Contact extends UFC_UserRelated
1121{
dcc63ed5 1122 public function buildCondition(PlFilter &$uf)
3f42a6ad 1123 {
009b8ab7 1124 $sub = $uf->addContactFilter($this->user->id());
3f42a6ad
FB
1125 return 'c' . $sub . '.contact IS NOT NULL';
1126 }
1127}
8363588b 1128// }}}
3f42a6ad 1129
8363588b 1130// {{{ class UFC_WatchRegistration
53eae167
RB
1131/** Filters users being watched by selected user
1132 */
4e7bf1e0
FB
1133class UFC_WatchRegistration extends UFC_UserRelated
1134{
dcc63ed5 1135 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1136 {
009b8ab7 1137 if (!$this->user->watch('registration')) {
dcc63ed5 1138 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1139 }
1140 $uids = $this->user->watchUsers();
1141 if (count($uids) == 0) {
dcc63ed5 1142 return PlFilterCondition::COND_FALSE;
009b8ab7 1143 } else {
07eb5b0e 1144 return '$UID IN ' . XDB::formatArray($uids);
009b8ab7 1145 }
4e7bf1e0
FB
1146 }
1147}
8363588b 1148// }}}
4e7bf1e0 1149
8363588b 1150// {{{ class UFC_WatchPromo
53eae167
RB
1151/** Filters users belonging to a promo watched by selected user
1152 * @param $user Selected user (the one watching promo)
1153 * @param $grade Formation the user is watching
1154 */
4e7bf1e0
FB
1155class UFC_WatchPromo extends UFC_UserRelated
1156{
1157 private $grade;
009b8ab7 1158 public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING)
4e7bf1e0 1159 {
009b8ab7 1160 parent::__construct($user);
4e7bf1e0
FB
1161 $this->grade = $grade;
1162 }
1163
dcc63ed5 1164 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1165 {
009b8ab7
FB
1166 $promos = $this->user->watchPromos();
1167 if (count($promos) == 0) {
dcc63ed5 1168 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1169 } else {
1170 $sube = $uf->addEducationFilter(true, $this->grade);
1171 $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
07eb5b0e 1172 return $field . ' IN ' . XDB::formatArray($promos);
009b8ab7 1173 }
4e7bf1e0
FB
1174 }
1175}
8363588b 1176// }}}
4e7bf1e0 1177
8363588b 1178// {{{ class UFC_WatchContact
53eae167
RB
1179/** Filters users watched by selected user
1180 */
009b8ab7 1181class UFC_WatchContact extends UFC_Contact
4e7bf1e0 1182{
dcc63ed5 1183 public function buildCondition(PlFilter &$uf)
4e7bf1e0 1184 {
009b8ab7 1185 if (!$this->user->watchContacts()) {
dcc63ed5 1186 return PlFilterCondition::COND_FALSE;
009b8ab7
FB
1187 }
1188 return parent::buildCondition($uf);
4e7bf1e0
FB
1189 }
1190}
8363588b 1191// }}}
4e7bf1e0
FB
1192
1193
d865c296
FB
1194/******************
1195 * ORDERS
1196 ******************/
1197
8363588b 1198// {{{ class UserFilterOrder
2d83cac9
RB
1199/** Base class for ordering results of a query.
1200 * Parameters for the ordering must be given to the constructor ($desc for a
1201 * descending order).
1202 * The getSortTokens function is used to get actual ordering part of the query.
1203 */
7ca75030 1204abstract class UserFilterOrder extends PlFilterOrder
d865c296 1205{
2d83cac9
RB
1206 /** This function must return the tokens to use for ordering
1207 * @param &$uf The UserFilter whose results must be ordered
1208 * @return The name of the field to use for ordering results
1209 */
d865c296
FB
1210 abstract protected function getSortTokens(UserFilter &$uf);
1211}
8363588b 1212// }}}
d865c296 1213
8363588b 1214// {{{ class UFO_Promo
5d2e55c7
RB
1215/** Orders users by promotion
1216 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
53eae167
RB
1217 * @param $desc Whether sort is descending
1218 */
d865c296
FB
1219class UFO_Promo extends UserFilterOrder
1220{
1221 private $grade;
1222
1223 public function __construct($grade = null, $desc = false)
1224 {
009b8ab7 1225 parent::__construct($desc);
d865c296 1226 $this->grade = $grade;
d865c296
FB
1227 }
1228
1229 protected function getSortTokens(UserFilter &$uf)
1230 {
1231 if (UserFilter::isGrade($this->grade)) {
1232 $sub = $uf->addEducationFilter($this->grade);
1233 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
1234 } else {
1235 $sub = $uf->addDisplayFilter();
1236 return 'pd' . $sub . '.promo';
1237 }
1238 }
1239}
8363588b 1240// }}}
d865c296 1241
8363588b 1242// {{{ class UFO_Name
53eae167 1243/** Sorts users by name
5d2e55c7
RB
1244 * @param $type Type of name on which to sort (firstname...)
1245 * @param $variant Variant of that name to use (marital, ordinary...)
53eae167
RB
1246 * @param $particle Set to true if particles should be included in the sorting order
1247 * @param $desc If sort order should be descending
1248 */
d865c296
FB
1249class UFO_Name extends UserFilterOrder
1250{
1251 private $type;
1252 private $variant;
1253 private $particle;
1254
1255 public function __construct($type, $variant = null, $particle = false, $desc = false)
1256 {
009b8ab7 1257 parent::__construct($desc);
d865c296
FB
1258 $this->type = $type;
1259 $this->variant = $variant;
1260 $this->particle = $particle;
d865c296
FB
1261 }
1262
1263 protected function getSortTokens(UserFilter &$uf)
1264 {
1265 if (UserFilter::isDisplayName($this->type)) {
1266 $sub = $uf->addDisplayFilter();
1267 return 'pd' . $sub . '.' . $this->type;
1268 } else {
1269 $sub = $uf->addNameFilter($this->type, $this->variant);
1270 if ($this->particle) {
1271 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1272 } else {
1273 return 'pn' . $sub . '.name';
1274 }
1275 }
1276 }
1277}
8363588b 1278// }}}
d865c296 1279
40585144
RB
1280// {{{ class UFO_Score
1281class UFO_Score extends UserFilterOrder
1282{
1283 protected function getSortTokens(UserFilter &$uf)
1284 {
1285 $sub = $uf->addNameTokensFilter();
1286 return 'SUM(' . $sub . '.score)';
1287 }
1288}
1289// }}}
1290
8363588b 1291// {{{ class UFO_Registration
53eae167
RB
1292/** Sorts users based on registration date
1293 */
38c6fe96
FB
1294class UFO_Registration extends UserFilterOrder
1295{
009b8ab7 1296 protected function getSortTokens(UserFilter &$uf)
38c6fe96 1297 {
009b8ab7 1298 return 'a.registration_date';
38c6fe96 1299 }
009b8ab7 1300}
8363588b 1301// }}}
38c6fe96 1302
8363588b 1303// {{{ class UFO_Birthday
53eae167
RB
1304/** Sorts users based on next birthday date
1305 */
009b8ab7
FB
1306class UFO_Birthday extends UserFilterOrder
1307{
38c6fe96
FB
1308 protected function getSortTokens(UserFilter &$uf)
1309 {
009b8ab7 1310 return 'p.next_birthday';
38c6fe96
FB
1311 }
1312}
8363588b 1313// }}}
38c6fe96 1314
8363588b 1315// {{{ class UFO_ProfileUpdate
53eae167
RB
1316/** Sorts users based on last profile update
1317 */
009b8ab7
FB
1318class UFO_ProfileUpdate extends UserFilterOrder
1319{
1320 protected function getSortTokens(UserFilter &$uf)
1321 {
1322 return 'p.last_change';
1323 }
1324}
8363588b 1325// }}}
009b8ab7 1326
8363588b 1327// {{{ class UFO_Death
53eae167
RB
1328/** Sorts users based on death date
1329 */
009b8ab7
FB
1330class UFO_Death extends UserFilterOrder
1331{
1332 protected function getSortTokens(UserFilter &$uf)
1333 {
1334 return 'p.deathdate';
1335 }
1336}
8363588b 1337// }}}
009b8ab7
FB
1338
1339
d865c296
FB
1340/***********************************
1341 *********************************
1342 USER FILTER CLASS
1343 *********************************
1344 ***********************************/
1345
8363588b 1346// {{{ class UserFilter
2d83cac9
RB
1347/** This class provides a convenient and centralized way of filtering users.
1348 *
1349 * Usage:
1350 * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t));
1351 *
1352 * Resulting UserFilter can be used to:
1353 * - get a list of User objects matching the filter
1354 * - get a list of UIDs matching the filter
1355 * - get the number of users matching the filter
1356 * - check whether a given User matches the filter
1357 * - filter a list of User objects depending on whether they match the filter
1358 *
1359 * Usage for UFC and UFO objects:
1360 * A UserFilter will call all private functions named XXXJoins.
1361 * These functions must return an array containing the list of join
1362 * required by the various UFC and UFO associated to the UserFilter.
1363 * Entries in those returned array are of the following form:
1364 * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter')
1365 * which will be translated into :
1366 * join_type JOIN joined_table AS join_tablealias ON (join_criter)
1367 * in the final query.
1368 *
1369 * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with
1370 * profile.pid, and $UID with auth_user_md5.user_id.
1371 *
1372 * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined;
1373 * its parameter will be used to set various private vars of the UserFilter describing
1374 * the required joins ; such a function shall return the "join_tablealias" to use
1375 * when referring to the joined table.
1376 *
1377 * For example, if data from profile_job must be available to filter results,
1378 * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and
1379 * return 'pj', the short name to use when referring to profile_job; when building
1380 * the query, calling the jobJoins function will return an array containing a single
1381 * row:
1382 * 'pj' => array('left', 'profile_job', '$ME.pid = $UID');
1383 *
1384 * The 'register_optional' function can be used to generate unique table aliases when
1385 * the same table has to be joined several times with different aliases.
1386 */
9b8e5fb4 1387class UserFilter extends PlFilter
a087cc8d 1388{
9b8e5fb4 1389 protected $joinMethods = array();
7ca75030 1390
9b8e5fb4
RB
1391 protected $joinMetas = array('$PID' => 'p.pid',
1392 '$UID' => 'a.uid',
1393 );
d865c296 1394
a087cc8d 1395 private $root;
24e08e33 1396 private $sort = array();
784745ce 1397 private $query = null;
24e08e33 1398 private $orderby = null;
784745ce 1399
aa21c568 1400 private $lastcount = null;
d865c296 1401
24e08e33 1402 public function __construct($cond = null, $sort = null)
5dd9d823 1403 {
06598c13 1404 if (empty($this->joinMethods)) {
d865c296
FB
1405 $class = new ReflectionClass('UserFilter');
1406 foreach ($class->getMethods() as $method) {
1407 $name = $method->getName();
1408 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
06598c13 1409 $this->joinMethods[] = $name;
d865c296
FB
1410 }
1411 }
1412 }
5dd9d823 1413 if (!is_null($cond)) {
06598c13 1414 if ($cond instanceof PlFilterCondition) {
5dd9d823
FB
1415 $this->setCondition($cond);
1416 }
1417 }
24e08e33
FB
1418 if (!is_null($sort)) {
1419 if ($sort instanceof UserFilterOrder) {
1420 $this->addSort($sort);
d865c296
FB
1421 } else if (is_array($sort)) {
1422 foreach ($sort as $s) {
1423 $this->addSort($s);
1424 }
24e08e33
FB
1425 }
1426 }
5dd9d823
FB
1427 }
1428
784745ce
FB
1429 private function buildQuery()
1430 {
d865c296
FB
1431 if (is_null($this->orderby)) {
1432 $orders = array();
1433 foreach ($this->sort as $sort) {
1434 $orders = array_merge($orders, $sort->buildSort($this));
1435 }
1436 if (count($orders) == 0) {
1437 $this->orderby = '';
1438 } else {
1439 $this->orderby = 'ORDER BY ' . implode(', ', $orders);
1440 }
1441 }
784745ce
FB
1442 if (is_null($this->query)) {
1443 $where = $this->root->buildCondition($this);
f7ea7450
RB
1444 if ($this->with_forced_sn) {
1445 $this->requireProfiles();
1446 $from = 'search_name AS sn';
1447 } else if ($this->with_accounts) {
b8dcf62d
RB
1448 $from = 'accounts AS a';
1449 } else {
1450 $this->requireProfiles();
1451 $from = 'profiles AS p';
1452 }
f7ea7450 1453 $joins = $this->buildJoins();
b8dcf62d 1454 $this->query = 'FROM ' . $from . '
784745ce
FB
1455 ' . $joins . '
1456 WHERE (' . $where . ')';
1457 }
1458 }
1459
7ca75030 1460 private function getUIDList($uids = null, PlLimit &$limit)
d865c296 1461 {
b8dcf62d 1462 $this->requireAccounts();
d865c296 1463 $this->buildQuery();
7ca75030 1464 $lim = $limit->getSql();
d865c296
FB
1465 $cond = '';
1466 if (!is_null($uids)) {
07eb5b0e 1467 $cond = ' AND a.uid IN ' . XDB::formatArray($uids);
d865c296
FB
1468 }
1469 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1470 ' . $this->query . $cond . '
1471 GROUP BY a.uid
1472 ' . $this->orderby . '
7ca75030 1473 ' . $lim);
d865c296
FB
1474 $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
1475 return $fetched;
1476 }
1477
043b104b
RB
1478 private function getPIDList($pids = null, PlLimit &$limit)
1479 {
1480 $this->requireProfiles();
1481 $this->buildQuery();
1482 $lim = $limit->getSql();
1483 $cond = '';
1484 if (!is_null($pids)) {
1485 $cond = ' AND p.pid IN ' . XDB::formatArray($pids);
1486 }
1487 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid
1488 ' . $this->query . $cond . '
1489 GROUP BY p.pid
1490 ' . $this->orderby . '
1491 ' . $lim);
1492 $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
1493 return $fetched;
1494 }
1495
a087cc8d
FB
1496 /** Check that the user match the given rule.
1497 */
1498 public function checkUser(PlUser &$user)
1499 {
b8dcf62d 1500 $this->requireAccounts();
784745ce
FB
1501 $this->buildQuery();
1502 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1503 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
1504 return $count == 1;
a087cc8d
FB
1505 }
1506
043b104b
RB
1507 /** Check that the profile match the given rule.
1508 */
1509 public function checkProfile(Profile &$profile)
1510 {
1511 $this->requireProfiles();
1512 $this->buildQuery();
1513 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1514 ' . $this->query . XDB::format(' AND p.pid = {?}', $profile->id()));
1515 return $count == 1;
1516 }
1517
1518 /** Default filter is on users
a087cc8d 1519 */
7ca75030 1520 public function filter(array $users, PlLimit &$limit)
a087cc8d 1521 {
043b104b
RB
1522 return $this->filterUsers($users, $limit);
1523 }
1524
1525 /** Filter a list of users to extract the users matching the rule.
1526 */
1527 public function filterUsers(array $users, PlLimit &$limit)
1528 {
b8dcf62d 1529 $this->requireAccounts();
4927ee54
FB
1530 $this->buildQuery();
1531 $table = array();
1532 $uids = array();
1533 foreach ($users as $user) {
07eb5b0e
FB
1534 if ($user instanceof PlUser) {
1535 $uid = $user->id();
1536 } else {
1537 $uid = $user;
1538 }
1539 $uids[] = $uid;
1540 $table[$uid] = $user;
4927ee54 1541 }
7ca75030 1542 $fetched = $this->getUIDList($uids, $limit);
a087cc8d 1543 $output = array();
4927ee54
FB
1544 foreach ($fetched as $uid) {
1545 $output[] = $table[$uid];
a087cc8d
FB
1546 }
1547 return $output;
1548 }
1549
043b104b
RB
1550 /** Filter a list of profiles to extract the users matching the rule.
1551 */
1552 public function filterProfiles(array $profiles, PlLimit &$limit)
1553 {
1554 $this->requireProfiles();
1555 $this->buildQuery();
1556 $table = array();
1557 $pids = array();
1558 foreach ($profiles as $profile) {
1559 if ($profile instanceof Profile) {
1560 $pid = $profile->id();
1561 } else {
1562 $pid = $profile;
1563 }
1564 $pids[] = $pid;
1565 $table[$pid] = $profile;
1566 }
1567 $fetched = $this->getPIDList($pids, $limit);
1568 $output = array();
1569 foreach ($fetched as $pid) {
1570 $output[] = $table[$pid];
1571 }
1572 return $output;
1573 }
1574
7ca75030
RB
1575 public function getUIDs(PlLimit &$limit)
1576 {
1577 return $this->getUIDList(null, $limit);
1578 }
1579
043b104b
RB
1580 public function getPIDs(PlLimit &$limit)
1581 {
1582 return $this->getPIDList(null, $limit);
1583 }
1584
7ca75030 1585 public function getUsers(PlLimit &$limit)
4927ee54 1586 {
7ca75030 1587 return User::getBulkUsersWithUIDs($this->getUIDs($limit));
d865c296
FB
1588 }
1589
043b104b
RB
1590 public function getProfiles(PlLimit &$limit)
1591 {
1592 return Profile::getBulkProfilesWithPIDs($this->getPIDs($limit));
1593 }
1594
7ca75030 1595 public function get(PlLimit &$limit)
d865c296 1596 {
7ca75030 1597 return $this->getUsers($limit);
4927ee54
FB
1598 }
1599
d865c296 1600 public function getTotalCount()
4927ee54 1601 {
38c6fe96 1602 if (is_null($this->lastcount)) {
aa21c568 1603 $this->buildQuery();
2b9ca54d 1604 if ($this->with_accounts) {
b8dcf62d
RB
1605 $field = 'a.uid';
1606 } else {
1607 $field = 'p.pid';
1608 }
1609 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT ' . $field . ')
7e735012 1610 ' . $this->query);
38c6fe96
FB
1611 } else {
1612 return $this->lastcount;
1613 }
4927ee54
FB
1614 }
1615
9b8e5fb4 1616 public function setCondition(PlFilterCondition &$cond)
a087cc8d
FB
1617 {
1618 $this->root =& $cond;
784745ce 1619 $this->query = null;
a087cc8d
FB
1620 }
1621
9b8e5fb4 1622 public function addSort(PlFilterOrder &$sort)
24e08e33 1623 {
d865c296
FB
1624 $this->sort[] = $sort;
1625 $this->orderby = null;
24e08e33
FB
1626 }
1627
a087cc8d
FB
1628 static public function getLegacy($promo_min, $promo_max)
1629 {
a087cc8d 1630 if ($promo_min != 0) {
784745ce 1631 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
5dd9d823
FB
1632 } else {
1633 $min = new UFC_True();
a087cc8d 1634 }
a087cc8d 1635 if ($promo_max != 0) {
784745ce 1636 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
a087cc8d 1637 } else {
5dd9d823 1638 $max = new UFC_True();
a087cc8d 1639 }
9b8e5fb4 1640 return new UserFilter(new PFC_And($min, $max));
a087cc8d 1641 }
784745ce 1642
07eb5b0e
FB
1643 static public function sortByName()
1644 {
1645 return array(new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
1646 }
1647
1648 static public function sortByPromo()
1649 {
1650 return array(new UFO_Promo(), new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
1651 }
1652
aa21c568
FB
1653 static private function getDBSuffix($string)
1654 {
1655 return preg_replace('/[^a-z0-9]/i', '', $string);
1656 }
1657
1658
2d83cac9
RB
1659 /** Stores a new (and unique) table alias in the &$table table
1660 * @param &$table Array in which the table alias must be stored
1661 * @param $val Value which will then be used to build the join
1662 * @return Name of the newly created alias
1663 */
aa21c568
FB
1664 private $option = 0;
1665 private function register_optional(array &$table, $val)
1666 {
1667 if (is_null($val)) {
1668 $sub = $this->option++;
1669 $index = null;
1670 } else {
1671 $sub = self::getDBSuffix($val);
1672 $index = $val;
1673 }
1674 $sub = '_' . $sub;
1675 $table[$sub] = $index;
1676 return $sub;
1677 }
784745ce 1678
b8dcf62d
RB
1679 /** PROFILE VS ACCOUNT
1680 */
f7ea7450
RB
1681 private $with_profiles = false;
1682 private $with_accounts = false;
1683 private $with_forced_sn = false;
b8dcf62d
RB
1684 public function requireAccounts()
1685 {
1686 $this->with_accounts = true;
1687 }
1688
1689 public function requireProfiles()
1690 {
1691 $this->with_profiles = true;
1692 }
1693
f7ea7450
RB
1694 /** Forces the "FROM" to use search_name instead of accounts or profiles */
1695 public function forceSearchName()
1696 {
1697 $this->with_forced_sn = true;
1698 }
1699
b8dcf62d
RB
1700 protected function accountJoins()
1701 {
1702 $joins = array();
f7ea7450
RB
1703 /** Quick search is much more efficient with sn first and PID second */
1704 if ($this->with_forced_sn) {
1705 $joins['p'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profiles', '$PID = sn.uid');
1706 if ($this->with_accounts) {
1707 $joins['ap'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'account_profiles', '$ME.pid = $PID');
1708 $joins['a'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'accounts', '$UID = ap.uid');
1709 }
1710 } else if ($this->with_profiles && $this->with_accounts) {
b8dcf62d
RB
1711 $joins['ap'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)');
1712 $joins['p'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profiles', '$PID = ap.pid');
1713 }
1714 return $joins;
1715 }
1716
d865c296
FB
1717 /** DISPLAY
1718 */
38c6fe96 1719 const DISPLAY = 'display';
d865c296
FB
1720 private $pd = false;
1721 public function addDisplayFilter()
1722 {
b8dcf62d 1723 $this->requireProfiles();
d865c296
FB
1724 $this->pd = true;
1725 return '';
1726 }
1727
9b8e5fb4 1728 protected function displayJoins()
d865c296
FB
1729 {
1730 if ($this->pd) {
7ca75030 1731 return array('pd' => new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_display', '$ME.pid = $PID'));
d865c296
FB
1732 } else {
1733 return array();
1734 }
1735 }
1736
784745ce
FB
1737 /** NAMES
1738 */
d865c296 1739 /* name tokens */
784745ce
FB
1740 const LASTNAME = 'lastname';
1741 const FIRSTNAME = 'firstname';
1742 const NICKNAME = 'nickname';
1743 const PSEUDONYM = 'pseudonym';
1744 const NAME = 'name';
d865c296 1745 /* name variants */
784745ce
FB
1746 const VN_MARITAL = 'marital';
1747 const VN_ORDINARY = 'ordinary';
1748 const VN_OTHER = 'other';
1749 const VN_INI = 'ini';
d865c296
FB
1750 /* display names */
1751 const DN_FULL = 'directory_name';
1752 const DN_DISPLAY = 'yourself';
1753 const DN_YOURSELF = 'yourself';
1754 const DN_DIRECTORY = 'directory_name';
1755 const DN_PRIVATE = 'private_name';
1756 const DN_PUBLIC = 'public_name';
1757 const DN_SHORT = 'short_name';
1758 const DN_SORT = 'sort_name';
784745ce
FB
1759
1760 static public $name_variants = array(
1761 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
d865c296
FB
1762 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
1763 );
784745ce
FB
1764
1765 static public function assertName($name)
1766 {
1767 if (!Profile::getNameTypeId($name)) {
9b8e5fb4 1768 Platal::page()->kill('Invalid name type: ' . $name);
784745ce
FB
1769 }
1770 }
1771
d865c296
FB
1772 static public function isDisplayName($name)
1773 {
1774 return $name == self::DN_FULL || $name == self::DN_DISPLAY
1775 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
1776 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
1777 || $name == self::DN_SHORT || $name == self::DN_SORT;
1778 }
1779
784745ce 1780 private $pn = array();
784745ce
FB
1781 public function addNameFilter($type, $variant = null)
1782 {
b8dcf62d 1783 $this->requireProfiles();
784745ce
FB
1784 if (!is_null($variant)) {
1785 $ft = $type . '_' . $variant;
1786 } else {
1787 $ft = $type;
1788 }
1789 $sub = '_' . $ft;
1790 self::assertName($ft);
1791
1792 if (!is_null($variant) && $variant == 'other') {
aa21c568 1793 $sub .= $this->option++;
784745ce
FB
1794 }
1795 $this->pn[$sub] = Profile::getNameTypeId($ft);
1796 return $sub;
1797 }
1798
9b8e5fb4 1799 protected function nameJoins()
784745ce
FB
1800 {
1801 $joins = array();
1802 foreach ($this->pn as $sub => $type) {
7ca75030 1803 $joins['pn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
784745ce
FB
1804 }
1805 return $joins;
1806 }
1807
40585144
RB
1808 /** NAMETOKENS
1809 */
1810 private $with_sn = false;
f7ea7450
RB
1811 // Set $doingQuickSearch to true if you wish to optimize the query
1812 public function addNameTokensFilter($doingQuickSearch = false)
40585144
RB
1813 {
1814 $this->requireProfiles();
f7ea7450 1815 $this->with_forced_sn = ($this->with_forced_sn || $doingQuickSearch);
40585144
RB
1816 $this->with_sn = true;
1817 return 'sn';
1818 }
1819
1820 protected function nameTokensJoins()
1821 {
f7ea7450
RB
1822 /* We don't return joins, since with_sn forces the SELECT to run on search_name first */
1823 if ($this->with_sn && !$this->with_forced_sn) {
1824 return array(
1825 'sn' => new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'search_name', '$ME.uid = $PID')
1826 );
1827 } else {
1828 return array();
40585144 1829 }
40585144
RB
1830 }
1831
a7f8e48a
RB
1832 /** NATIONALITY
1833 */
1834
1835 private $with_nat = false;
1836 public function addNationalityFilter()
1837 {
1838 $this->with_nat = true;
1839 return 'ngc';
1840 }
1841
1842 protected function nationalityJoins()
1843 {
1844 $joins = array();
1845 if ($this->with_nat) {
1846 $joins['ngc'] = new PlSqlJoin(PlSqlJoin::MODE_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');
1847 }
1848 return $joins;
1849 }
1850
784745ce
FB
1851 /** EDUCATION
1852 */
1853 const GRADE_ING = 'Ing.';
1854 const GRADE_PHD = 'PhD';
1855 const GRADE_MST = 'M%';
1856 static public function isGrade($grade)
1857 {
38c6fe96 1858 return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST;
784745ce
FB
1859 }
1860
1861 static public function assertGrade($grade)
1862 {
1863 if (!self::isGrade($grade)) {
1864 Platal::page()->killError("Diplôme non valide");
1865 }
1866 }
1867
d865c296
FB
1868 static public function promoYear($grade)
1869 {
1870 // XXX: Definition of promotion for phds and masters might change in near future.
1871 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
1872 }
1873
784745ce
FB
1874 private $pepe = array();
1875 private $with_pee = false;
784745ce
FB
1876 public function addEducationFilter($x = false, $grade = null)
1877 {
b8dcf62d 1878 $this->requireProfiles();
784745ce 1879 if (!$x) {
aa21c568
FB
1880 $index = $this->option;
1881 $sub = $this->option++;
784745ce
FB
1882 } else {
1883 self::assertGrade($grade);
1884 $index = $grade;
1885 $sub = $grade[0];
1886 $this->with_pee = true;
1887 }
1888 $sub = '_' . $sub;
1889 $this->pepe[$index] = $sub;
1890 return $sub;
1891 }
1892
9b8e5fb4 1893 protected function educationJoins()
784745ce
FB
1894 {
1895 $joins = array();
1896 if ($this->with_pee) {
7ca75030 1897 $joins['pee'] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_enum', 'pee.abbreviation = \'X\'');
784745ce
FB
1898 }
1899 foreach ($this->pepe as $grade => $sub) {
1900 if ($this->isGrade($grade)) {
7ca75030
RB
1901 $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
1902 $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
784745ce
FB
1903 XDB::format('{?}', $grade));
1904 } else {
7ca75030
RB
1905 $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_education', '$ME.uid = $PID');
1906 $joins['pee' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
1907 $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
784745ce
FB
1908 }
1909 }
1910 return $joins;
1911 }
4927ee54
FB
1912
1913
1914 /** GROUPS
1915 */
1916 private $gpm = array();
4927ee54
FB
1917 public function addGroupFilter($group = null)
1918 {
b8dcf62d 1919 $this->requireAccounts();
4927ee54
FB
1920 if (!is_null($group)) {
1921 if (ctype_digit($group)) {
1922 $index = $sub = $group;
1923 } else {
1924 $index = $group;
aa21c568 1925 $sub = self::getDBSuffix($group);
4927ee54
FB
1926 }
1927 } else {
aa21c568 1928 $sub = 'group_' . $this->option++;
4927ee54
FB
1929 $index = null;
1930 }
1931 $sub = '_' . $sub;
1932 $this->gpm[$sub] = $index;
1933 return $sub;
1934 }
1935
9b8e5fb4 1936 protected function groupJoins()
4927ee54
FB
1937 {
1938 $joins = array();
1939 foreach ($this->gpm as $sub => $key) {
1940 if (is_null($key)) {
7ca75030
RB
1941 $joins['gpa' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'groupex.asso');
1942 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
4927ee54 1943 } else if (ctype_digit($key)) {
7ca75030 1944 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
4927ee54 1945 } else {
7ca75030
RB
1946 $joins['gpa' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key));
1947 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
4927ee54
FB
1948 }
1949 }
1950 return $joins;
0fb3713c
RB
1951 }
1952
1953 /** BINETS
1954 */
1955
a7f8e48a
RB
1956 private $with_bi = false;
1957 private $with_bd = false;
1958 public function addBinetsFilter($with_enum = false;)
0fb3713c
RB
1959 {
1960 $this->requireProfiles();
a7f8e48a
RB
1961 $this->with_bi = true;
1962 if ($with_enum) {
1963 $this->with_bd = true;
1964 return 'bd';
1965 } else {
1966 return 'bi';
1967 }
0fb3713c
RB
1968 }
1969
1970 protected function binetsJoins()
1971 {
1972 $joins = array();
a7f8e48a 1973 if ($this->with_bi) {
0fb3713c
RB
1974 $joins['bi'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'binets_ins', '$ME.uid = $PID');
1975 }
a7f8e48a
RB
1976 if ($this->with_bd) {
1977 $joins['bd'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'binets_def', '$ME.id = bi.binet_id');
1978 }
0fb3713c 1979 return $joins;
4927ee54 1980 }
aa21c568
FB
1981
1982 /** EMAILS
1983 */
1984 private $e = array();
1985 public function addEmailRedirectFilter($email = null)
1986 {
b8dcf62d 1987 $this->requireAccounts();
aa21c568
FB
1988 return $this->register_optional($this->e, $email);
1989 }
1990
1991 private $ve = array();
1992 public function addVirtualEmailFilter($email = null)
1993 {
21401768 1994 $this->addAliasFilter(self::ALIAS_FORLIFE);
aa21c568
FB
1995 return $this->register_optional($this->ve, $email);
1996 }
1997
21401768
FB
1998 const ALIAS_BEST = 'bestalias';
1999 const ALIAS_FORLIFE = 'forlife';
aa21c568
FB
2000 private $al = array();
2001 public function addAliasFilter($alias = null)
2002 {
b8dcf62d 2003 $this->requireAccounts();
aa21c568
FB
2004 return $this->register_optional($this->al, $alias);
2005 }
2006
9b8e5fb4 2007 protected function emailJoins()
aa21c568
FB
2008 {
2009 global $globals;
2010 $joins = array();
2011 foreach ($this->e as $sub=>$key) {
2012 if (is_null($key)) {
7ca75030 2013 $joins['e' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
aa21c568 2014 } else {
7ca75030 2015 $joins['e' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
aa21c568
FB
2016 }
2017 }
21401768 2018 foreach ($this->al as $sub=>$key) {
aa21c568 2019 if (is_null($key)) {
7ca75030 2020 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
21401768 2021 } else if ($key == self::ALIAS_BEST) {
7ca75030 2022 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
21401768 2023 } else if ($key == self::ALIAS_FORLIFE) {
7ca75030 2024 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
aa21c568 2025 } else {
7ca75030 2026 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
aa21c568 2027 }
aa21c568 2028 }
21401768 2029 foreach ($this->ve as $sub=>$key) {
aa21c568 2030 if (is_null($key)) {
7ca75030 2031 $joins['v' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'virtual', '$ME.type = \'user\'');
aa21c568 2032 } else {
7ca75030 2033 $joins['v' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
aa21c568 2034 }
7ca75030 2035 $joins['vr' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid
21401768
FB
2036 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
2037 CONCAT(al_forlife.alias, \'@\', {?}),
2038 a.email))',
2039 $globals->mail->domain, $globals->mail->domain2));
aa21c568
FB
2040 }
2041 return $joins;
2042 }
3f42a6ad
FB
2043
2044
c4b24511
RB
2045 /** ADDRESSES
2046 */
036d1637 2047 private $with_pa = false;
c4b24511
RB
2048 public function addAddressFilter()
2049 {
b8dcf62d 2050 $this->requireProfiles();
036d1637
RB
2051 $this->with_pa = true;
2052 return 'pa';
c4b24511
RB
2053 }
2054
2b9ca54d
RB
2055 private $with_pac = false;
2056 public function addAddressCountryFilter()
2057 {
2058 $this->requireProfiles();
2059 $this->addAddressFilter();
2060 $this->with_pac = true;
2061 return 'gc';
2062 }
2063
2064 private $with_pal = true;
2065 public function addAddressLocalityFilter()
2066 {
2067 $this->requireProfiles();
2068 $this->addAddressFilter();
2069 $this->with_pal = true;
2070 return 'gl';
2071 }
2072
9b8e5fb4 2073 protected function addressJoins()
c4b24511
RB
2074 {
2075 $joins = array();
036d1637 2076 if ($this->with_pa) {
7ca75030 2077 $joins['pa'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_address', '$ME.pid = $PID');
c4b24511 2078 }
2b9ca54d
RB
2079 if ($this->with_pac) {
2080 $joins['gc'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID');
2081 }
2082 if ($this->with_pal) {
2083 $joins['gl'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'geoloc_localities', '$ME.id = pa.localityID');
2084 }
c4b24511
RB
2085 return $joins;
2086 }
2087
2088
4083b126
RB
2089 /** CORPS
2090 */
2091
2092 private $pc = false;
2093 private $pce = array();
2094 private $pcr = false;
2095 public function addCorpsFilter($type)
2096 {
b8dcf62d 2097 $this->requireProfiles();
4083b126
RB
2098 $this->pc = true;
2099 if ($type == UFC_Corps::CURRENT) {
2100 $pce['pcec'] = 'current_corpsid';
2101 return 'pcec';
2102 } else if ($type == UFC_Corps::ORIGIN) {
2103 $pce['pceo'] = 'original_corpsid';
2104 return 'pceo';
2105 }
2106 }
2107
2108 public function addCorpsRankFilter()
2109 {
b8dcf62d 2110 $this->requireProfiles();
4083b126
RB
2111 $this->pc = true;
2112 $this->pcr = true;
2113 return 'pcr';
2114 }
2115
9b8e5fb4 2116 protected function corpsJoins()
4083b126
RB
2117 {
2118 $joins = array();
2119 if ($this->pc) {
7ca75030 2120 $joins['pc'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps', '$ME.uid = $UID');
4083b126
RB
2121 }
2122 if ($this->pcr) {
7ca75030 2123 $joins['pcr'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps_rank_enum', '$ME.id = pc.rankid');
4083b126
RB
2124 }
2125 foreach($this->pce as $sub => $field) {
7ca75030 2126 $joins[$sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps_enum', '$ME.id = pc.' . $field);
4083b126
RB
2127 }
2128 return $joins;
2129 }
2130
6a99c3ac
RB
2131 /** JOBS
2132 */
2133
2134 const JOB_SECTOR = 1;
2135 const JOB_SUBSECTOR = 2;
2136 const JOB_SUBSUBSECTOR = 4;
2137 const JOB_ALTERNATES = 8;
2138 const JOB_USERDEFINED = 16;
01cc5f9e 2139 const JOB_CV = 32;
6a99c3ac
RB
2140
2141 /** Joins :
2142 * pj => profile_job
2143 * pje => profile_job_enum
2144 * pjse => profile_job_sector_enum
2145 * pjsse => profile_job_subsector_enum
2146 * pjssse => profile_job_subsubsector_enum
2147 * pja => profile_job_alternates
2148 */
2149 private $with_pj = false;
2150 private $with_pje = false;
2151 private $with_pjse = false;
2152 private $with_pjsse = false;
2153 private $with_pjssse = false;
2154 private $with_pja = false;
2155
2156 public function addJobFilter()
2157 {
b8dcf62d 2158 $this->requireProfiles();
6a99c3ac
RB
2159 $this->with_pj = true;
2160 return 'pj';
2161 }
2162
2163 public function addJobCompanyFilter()
2164 {
2165 $this->addJobFilter();
2166 $this->with_pje = true;
2167 return 'pje';
2168 }
2169
2170 public function addJobSectorizationFilter($type)
2171 {
2172 $this->addJobFilter();
2173 if ($type == self::JOB_SECTOR) {
2174 $this->with_pjse = true;
2175 return 'pjse';
2176 } else if ($type == self::JOB_SUBSECTOR) {
2177 $this->with_pjsse = true;
2178 return 'pjsse';
2179 } else if ($type == self::JOB_SUBSUBSECTOR) {
2180 $this->with_pjssse = true;
2181 return 'pjssse';
2182 } else if ($type == self::JOB_ALTERNATES) {
2183 $this->with_pja = true;
2184 return 'pja';
2185 }
2186 }
2187
9b8e5fb4 2188 protected function jobJoins()
6a99c3ac
RB
2189 {
2190 $joins = array();
2191 if ($this->with_pj) {
7ca75030 2192 $joins['pj'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job', '$ME.uid = $UID');
6a99c3ac
RB
2193 }
2194 if ($this->with_pje) {
7ca75030 2195 $joins['pje'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_enum', '$ME.id = pj.jobid');
6a99c3ac
RB
2196 }
2197 if ($this->with_pjse) {
7ca75030 2198 $joins['pjse'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_sector_enum', '$ME.id = pj.sectorid');
6a99c3ac
RB
2199 }
2200 if ($this->with_pjsse) {
7ca75030 2201 $joins['pjsse'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_subsector_enum', '$ME.id = pj.subsectorid');
6a99c3ac
RB
2202 }
2203 if ($this->with_pjssse) {
7ca75030 2204 $joins['pjssse'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
6a99c3ac
RB
2205 }
2206 if ($this->with_pja) {
7ca75030 2207 $joins['pja'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
6a99c3ac
RB
2208 }
2209 return $joins;
2210 }
2211
0a2e9c74
RB
2212 /** NETWORKING
2213 */
2214
2215 private $with_pnw = false;
2216 public function addNetworkingFilter()
2217 {
b8dcf62d 2218 $this->requireAccounts();
0a2e9c74
RB
2219 $this->with_pnw = true;
2220 return 'pnw';
2221 }
2222
9b8e5fb4 2223 protected function networkingJoins()
0a2e9c74
RB
2224 {
2225 $joins = array();
2226 if ($this->with_pnw) {
7ca75030 2227 $joins['pnw'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_networking', '$ME.uid = $UID');
0a2e9c74
RB
2228 }
2229 return $joins;
2230 }
2231
6d62969e
RB
2232 /** PHONE
2233 */
2234
2d83cac9 2235 private $with_ptel = false;
6d62969e
RB
2236
2237 public function addPhoneFilter()
2238 {
b8dcf62d 2239 $this->requireAccounts();
2d83cac9 2240 $this->with_ptel = true;
6d62969e
RB
2241 return 'ptel';
2242 }
2243
9b8e5fb4 2244 protected function phoneJoins()
6d62969e
RB
2245 {
2246 $joins = array();
2d83cac9 2247 if ($this->with_ptel) {
9b8e5fb4 2248 $joins['ptel'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_phones', '$ME.uid = $UID');
6d62969e
RB
2249 }
2250 return $joins;
2251 }
2252
ceb512d2
RB
2253 /** MEDALS
2254 */
2255
2d83cac9 2256 private $with_pmed = false;
ceb512d2
RB
2257 public function addMedalFilter()
2258 {
b8dcf62d 2259 $this->requireProfiles();
2d83cac9 2260 $this->with_pmed = true;
ceb512d2
RB
2261 return 'pmed';
2262 }
2263
9b8e5fb4 2264 protected function medalJoins()
ceb512d2
RB
2265 {
2266 $joins = array();
2d83cac9 2267 if ($this->with_pmed) {
7ca75030 2268 $joins['pmed'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_medals_sub', '$ME.uid = $UID');
ceb512d2
RB
2269 }
2270 return $joins;
2271 }
2272
671b7073
RB
2273 /** MENTORING
2274 */
2275
2276 private $pms = array();
2277 const MENTOR_EXPERTISE = 1;
2278 const MENTOR_COUNTRY = 2;
2279 const MENTOR_SECTOR = 3;
2280
2281 public function addMentorFilter($type)
2282 {
b8dcf62d 2283 $this->requireAccounts();
671b7073
RB
2284 switch($type) {
2285 case MENTOR_EXPERTISE:
2286 $pms['pme'] = 'profile_mentor';
2287 return 'pme';
2288 case MENTOR_COUNTRY:
2289 $pms['pmc'] = 'profile_mentor_country';
2290 return 'pmc';
2291 case MENTOR_SECTOR:
2292 $pms['pms'] = 'profile_mentor_sector';
2293 return 'pms';
2294 default:
5d2e55c7 2295 Platal::page()->killError("Undefined mentor filter.");
671b7073
RB
2296 }
2297 }
2298
9b8e5fb4 2299 protected function mentorJoins()
671b7073
RB
2300 {
2301 $joins = array();
2302 foreach ($this->pms as $sub => $tab) {
7ca75030 2303 $joins[$sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, $tab, '$ME.uid = $UID');
671b7073
RB
2304 }
2305 return $joins;
2306 }
2307
3f42a6ad
FB
2308 /** CONTACTS
2309 */
2310 private $cts = array();
2311 public function addContactFilter($uid = null)
2312 {
b8dcf62d 2313 $this->requireAccounts();
3f42a6ad
FB
2314 return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid);
2315 }
2316
9b8e5fb4 2317 protected function contactJoins()
3f42a6ad
FB
2318 {
2319 $joins = array();
2320 foreach ($this->cts as $sub=>$key) {
2321 if (is_null($key)) {
7ca75030 2322 $joins['c' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'contacts', '$ME.contact = $UID');
3f42a6ad 2323 } else {
7ca75030 2324 $joins['c' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'contacts', XDB::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
3f42a6ad
FB
2325 }
2326 }
2327 return $joins;
2328 }
4e7bf1e0
FB
2329
2330
2331 /** CARNET
2332 */
2333 private $wn = array();
2334 public function addWatchRegistrationFilter($uid = null)
2335 {
b8dcf62d 2336 $this->requireAccounts();
4e7bf1e0
FB
2337 return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid);
2338 }
2339
2340 private $wp = array();
2341 public function addWatchPromoFilter($uid = null)
2342 {
b8dcf62d 2343 $this->requireAccounts();
4e7bf1e0
FB
2344 return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid);
2345 }
2346
2347 private $w = array();
2348 public function addWatchFilter($uid = null)
2349 {
b8dcf62d 2350 $this->requireAccounts();
4e7bf1e0
FB
2351 return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid);
2352 }
2353
9b8e5fb4 2354 protected function watchJoins()
4e7bf1e0
FB
2355 {
2356 $joins = array();
2357 foreach ($this->w as $sub=>$key) {
2358 if (is_null($key)) {
7ca75030 2359 $joins['w' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch');
4e7bf1e0 2360 } else {
7ca75030 2361 $joins['w' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch', XDB::format('$ME.uid = {?}', substr($key, 5)));
4e7bf1e0
FB
2362 }
2363 }
2364 foreach ($this->wn as $sub=>$key) {
2365 if (is_null($key)) {
7ca75030 2366 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', '$ME.ni_id = $UID');
4e7bf1e0 2367 } else {
7ca75030 2368 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
4e7bf1e0
FB
2369 }
2370 }
2371 foreach ($this->wn as $sub=>$key) {
2372 if (is_null($key)) {
7ca75030 2373 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', '$ME.ni_id = $UID');
4e7bf1e0 2374 } else {
7ca75030 2375 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
4e7bf1e0
FB
2376 }
2377 }
2378 foreach ($this->wp as $sub=>$key) {
2379 if (is_null($key)) {
7ca75030 2380 $joins['wp' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_promo');
4e7bf1e0 2381 } else {
7ca75030 2382 $joins['wp' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_promo', XDB::format('$ME.uid = {?}', substr($key, 5)));
4e7bf1e0
FB
2383 }
2384 }
2385 return $joins;
2386 }
a087cc8d 2387}
8363588b 2388// }}}
3f42a6ad 2389
a087cc8d
FB
2390// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
2391?>