Add UFC_Mentor_*
[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
a087cc8d
FB
28interface UserFilterCondition
29{
5dd9d823
FB
30 const COND_TRUE = 'TRUE';
31 const COND_FALSE = 'FALSE';
32
a087cc8d
FB
33 /** Check that the given user matches the rule.
34 */
784745ce 35 public function buildCondition(UserFilter &$uf);
a087cc8d 36}
8363588b 37// }}}
a087cc8d 38
8363588b 39// {{{ class UFC_OneChild
a087cc8d
FB
40abstract class UFC_OneChild implements UserFilterCondition
41{
42 protected $child;
43
5dd9d823
FB
44 public function __construct($child = null)
45 {
46 if (!is_null($child) && ($child instanceof UserFilterCondition)) {
47 $this->setChild($child);
48 }
49 }
50
a087cc8d
FB
51 public function setChild(UserFilterCondition &$cond)
52 {
53 $this->child =& $cond;
54 }
55}
8363588b 56// }}}
a087cc8d 57
8363588b 58// {{{ class UFC_NChildren
a087cc8d
FB
59abstract class UFC_NChildren implements UserFilterCondition
60{
61 protected $children = array();
62
5dd9d823
FB
63 public function __construct()
64 {
65 $children = func_get_args();
66 foreach ($children as &$child) {
67 if (!is_null($child) && ($child instanceof UserFilterCondition)) {
68 $this->addChild($child);
69 }
70 }
71 }
72
a087cc8d
FB
73 public function addChild(UserFilterCondition &$cond)
74 {
75 $this->children[] =& $cond;
76 }
5dd9d823
FB
77
78 protected function catConds(array $cond, $op, $fallback)
79 {
80 if (count($cond) == 0) {
81 return $fallback;
82 } else if (count($cond) == 1) {
83 return $cond[0];
84 } else {
4927ee54 85 return '(' . implode(') ' . $op . ' (', $cond) . ')';
5dd9d823
FB
86 }
87 }
a087cc8d 88}
8363588b 89// }}}
a087cc8d 90
8363588b 91// {{{ class UFC_True
a087cc8d
FB
92class UFC_True implements UserFilterCondition
93{
784745ce 94 public function buildCondition(UserFilter &$uf)
a087cc8d 95 {
5dd9d823 96 return self::COND_TRUE;
a087cc8d
FB
97 }
98}
8363588b 99// }}}
a087cc8d 100
8363588b 101// {{{ class UFC_False
a087cc8d
FB
102class UFC_False implements UserFilterCondition
103{
784745ce 104 public function buildCondition(UserFilter &$uf)
a087cc8d 105 {
5dd9d823 106 return self::COND_FALSE;
a087cc8d
FB
107 }
108}
8363588b 109// }}}
a087cc8d 110
8363588b 111// {{{ class UFC_Not
a087cc8d
FB
112class UFC_Not extends UFC_OneChild
113{
784745ce 114 public function buildCondition(UserFilter &$uf)
a087cc8d 115 {
5dd9d823
FB
116 $val = $this->child->buildCondition($uf);
117 if ($val == self::COND_TRUE) {
118 return self::COND_FALSE;
119 } else if ($val == self::COND_FALSE) {
120 return self::COND_TRUE;
121 } else {
122 return 'NOT (' . $val . ')';
123 }
a087cc8d
FB
124 }
125}
8363588b 126// }}}
a087cc8d 127
8363588b 128// {{{ class UFC_And
a087cc8d
FB
129class UFC_And extends UFC_NChildren
130{
784745ce 131 public function buildCondition(UserFilter &$uf)
a087cc8d 132 {
784745ce 133 if (empty($this->children)) {
5dd9d823 134 return self::COND_FALSE;
784745ce 135 } else {
5dd9d823 136 $true = self::COND_FALSE;
784745ce
FB
137 $conds = array();
138 foreach ($this->children as &$child) {
5dd9d823
FB
139 $val = $child->buildCondition($uf);
140 if ($val == self::COND_TRUE) {
141 $true = self::COND_TRUE;
142 } else if ($val == self::COND_FALSE) {
143 return self::COND_FALSE;
144 } else {
145 $conds[] = $val;
146 }
a087cc8d 147 }
5dd9d823 148 return $this->catConds($conds, 'AND', $true);
a087cc8d 149 }
a087cc8d
FB
150 }
151}
8363588b 152// }}}
a087cc8d 153
8363588b 154// {{{ class UFC_Or
a087cc8d
FB
155class UFC_Or extends UFC_NChildren
156{
784745ce 157 public function buildCondition(UserFilter &$uf)
a087cc8d 158 {
784745ce 159 if (empty($this->children)) {
5dd9d823 160 return self::COND_TRUE;
784745ce 161 } else {
5dd9d823 162 $true = self::COND_TRUE;
784745ce
FB
163 $conds = array();
164 foreach ($this->children as &$child) {
5dd9d823
FB
165 $val = $child->buildCondition($uf);
166 if ($val == self::COND_TRUE) {
167 return self::COND_TRUE;
168 } else if ($val == self::COND_FALSE) {
169 $true = self::COND_FALSE;
170 } else {
171 $conds[] = $val;
172 }
a087cc8d 173 }
5dd9d823 174 return $this->catConds($conds, 'OR', $true);
a087cc8d 175 }
a087cc8d
FB
176 }
177}
8363588b 178// }}}
a087cc8d 179
8363588b 180// {{{ class UFC_Profile
53eae167
RB
181/** Filters users who have a profile
182 */
eb1449b8
FB
183class UFC_Profile implements UserFilterCondition
184{
185 public function buildCondition(UserFilter &$uf)
186 {
187 return '$PID IS NOT NULL';
188 }
189}
8363588b 190// }}}
eb1449b8 191
8363588b 192// {{{ class UFC_Promo
53eae167
RB
193/** Filters users based on promo
194 * @param $comparison Comparison operator (>, =, ...)
195 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
196 * @param $promo Promo on which the filter is based
197 */
a087cc8d
FB
198class UFC_Promo implements UserFilterCondition
199{
a087cc8d
FB
200
201 private $grade;
202 private $promo;
203 private $comparison;
204
205 public function __construct($comparison, $grade, $promo)
206 {
207 $this->grade = $grade;
208 $this->comparison = $comparison;
209 $this->promo = $promo;
38c6fe96
FB
210 if ($this->grade != UserFilter::DISPLAY) {
211 UserFilter::assertGrade($this->grade);
212 }
a087cc8d
FB
213 }
214
784745ce 215 public function buildCondition(UserFilter &$uf)
a087cc8d 216 {
38c6fe96
FB
217 if ($this->grade == UserFilter::DISPLAY) {
218 $sub = $uf->addDisplayFilter();
219 return XDB::format('pd' . $sub . '.promo = {?}', $this->promo);
220 } else {
221 $sub = $uf->addEducationFilter(true, $this->grade);
222 $field = 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
223 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison . ' ' . XDB::format('{?}', $this->promo);
224 }
784745ce
FB
225 }
226}
8363588b 227// }}}
784745ce 228
8363588b 229// {{{ class UFC_Name
53eae167
RB
230/** Filters users based on name
231 * @param $type Type of name field on which filtering is done (firstname, lastname, ...)
232 * @param $text Text on which to filter
233 * @param $mode flag indicating search type (prefix, suffix, with particule, ...)
234 */
784745ce
FB
235class UFC_Name implements UserFilterCondition
236{
237 const PREFIX = 1;
238 const SUFFIX = 2;
239 const PARTICLE = 7;
240 const VARIANTS = 8;
241 const CONTAINS = 3;
242
243 private $type;
244 private $text;
245 private $mode;
246
247 public function __construct($type, $text, $mode)
248 {
249 $this->type = $type;
250 $this->text = $text;
251 $this->mode = $mode;
252 }
253
254 private function buildNameQuery($type, $variant, $where, UserFilter &$uf)
255 {
256 $sub = $uf->addNameFilter($type, $variant);
257 return str_replace('$ME', 'pn' . $sub, $where);
258 }
259
260 public function buildCondition(UserFilter &$uf)
261 {
262 $left = '$ME.name';
263 $op = ' LIKE ';
264 if (($this->mode & self::PARTICLE) == self::PARTICLE) {
265 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
266 }
267 if (($this->mode & self::CONTAINS) == 0) {
268 $right = XDB::format('{?}', $this->text);
269 $op = ' = ';
270 } else if (($this->mode & self::CONTAINS) == self::PREFIX) {
271 $right = XDB::format('CONCAT({?}, \'%\')', $this->text);
272 } else if (($this->mode & self::CONTAINS) == self::SUFFIX) {
273 $right = XDB::format('CONCAT(\'%\', {?})', $this->text);
274 } else {
275 $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text);
276 }
277 $cond = $left . $op . $right;
278 $conds = array($this->buildNameQuery($this->type, null, $cond, $uf));
d865c296 279 if (($this->mode & self::VARIANTS) != 0 && isset(UserFilter::$name_variants[$this->type])) {
784745ce
FB
280 foreach (UserFilter::$name_variants[$this->type] as $var) {
281 $conds[] = $this->buildNameQuery($this->type, $var, $cond, $uf);
282 }
283 }
284 return implode(' OR ', $conds);
a087cc8d
FB
285 }
286}
8363588b 287// }}}
a087cc8d 288
8363588b 289// {{{ class UFC_Dead
53eae167
RB
290/** Filters users based on death date
291 * @param $comparison Comparison operator
292 * @param $date Date to which death date should be compared
293 */
4927ee54
FB
294class UFC_Dead implements UserFilterCondition
295{
38c6fe96
FB
296 private $comparison;
297 private $date;
298
299 public function __construct($comparison = null, $date = null)
4927ee54 300 {
38c6fe96
FB
301 $this->comparison = $comparison;
302 $this->date = $date;
4927ee54
FB
303 }
304
305 public function buildCondition(UserFilter &$uf)
306 {
38c6fe96
FB
307 $str = 'p.deathdate IS NOT NULL';
308 if (!is_null($this->comparison)) {
309 $str .= ' AND p.deathdate ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date));
4927ee54 310 }
38c6fe96 311 return $str;
4927ee54
FB
312 }
313}
8363588b 314// }}}
4927ee54 315
8363588b 316// {{{ class UFC_Registered
53eae167
RB
317/** Filters users based on registration state
318 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
319 * @param $comparison Comparison operator
320 * @param $date Date to which users registration date should be compared
321 */
4927ee54
FB
322class UFC_Registered implements UserFilterCondition
323{
324 private $active;
38c6fe96
FB
325 private $comparison;
326 private $date;
327
328 public function __construct($active = false, $comparison = null, $date = null)
4927ee54
FB
329 {
330 $this->only_active = $active;
38c6fe96
FB
331 $this->comparison = $comparison;
332 $this->date = $date;
4927ee54
FB
333 }
334
335 public function buildCondition(UserFilter &$uf)
336 {
337 if ($this->active) {
38c6fe96 338 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
4927ee54 339 } else {
38c6fe96
FB
340 $date = 'a.uid IS NOT NULL AND a.state != \'pending\'';
341 }
342 if (!is_null($this->comparison)) {
343 $date .= ' AND a.registration_date ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date));
4927ee54 344 }
38c6fe96 345 return $date;
4927ee54
FB
346 }
347}
8363588b 348// }}}
4927ee54 349
8363588b 350// {{{ class UFC_ProfileUpdated
53eae167
RB
351/** Filters users based on profile update date
352 * @param $comparison Comparison operator
353 * @param $date Date to which profile update date must be compared
354 */
7e735012
FB
355class UFC_ProfileUpdated implements UserFilterCondition
356{
357 private $comparison;
358 private $date;
359
360 public function __construct($comparison = null, $date = null)
361 {
362 $this->comparison = $comparison;
363 $this->date = $date;
364 }
365
366 public function buildCondition(UserFilter &$uf)
367 {
368 return 'p.last_change ' . $this->comparison . XDB::format(' {?}', date('Y-m-d H:i:s', $this->date));
369 }
370}
8363588b 371// }}}
7e735012 372
8363588b 373// {{{ class UFC_Birthday
53eae167
RB
374/** Filters users based on next birthday date
375 * @param $comparison Comparison operator
376 * @param $date Date to which users next birthday date should be compared
377 */
7e735012
FB
378class UFC_Birthday implements UserFilterCondition
379{
380 private $comparison;
381 private $date;
382
383 public function __construct($comparison = null, $date = null)
384 {
385 $this->comparison = $comparison;
386 $this->date = $date;
387 }
388
389 public function buildCondition(UserFilter &$uf)
390 {
391 return 'p.next_birthday ' . $this->comparison . XDB::format(' {?}', date('Y-m-d', $this->date));
392 }
393}
8363588b 394// }}}
7e735012 395
8363588b 396// {{{ class UFC_Sex
53eae167
RB
397/** Filters users based on sex
398 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
399 */
4927ee54
FB
400class UFC_Sex implements UserFilterCondition
401{
402 private $sex;
403 public function __construct($sex)
404 {
405 $this->sex = $sex;
406 }
407
408 public function buildCondition(UserFilter &$uf)
409 {
410 if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) {
411 return self::COND_FALSE;
412 } else {
24e08e33 413 return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male');
4927ee54
FB
414 }
415 }
416}
8363588b 417// }}}
4927ee54 418
8363588b 419// {{{ class UFC_Group
53eae167
RB
420/** Filters users based on group membership
421 * @param $group Group whose member we are selecting
422 * @param $admin Whether to restrict selection to admins of that group
423 */
4927ee54
FB
424class UFC_Group implements UserFilterCondition
425{
426 private $group;
427 private $admin;
428 public function __construct($group, $admin = false)
429 {
430 $this->group = $group;
431 $this->admin = $admin;
432 }
433
434 public function buildCondition(UserFilter &$uf)
435 {
436 $sub = $uf->addGroupFilter($this->group);
437 $where = 'gpm' . $sub . '.perms IS NOT NULL';
438 if ($this->admin) {
439 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
440 }
441 return $where;
442 }
443}
8363588b 444// }}}
4927ee54 445
8363588b 446// {{{ class UFC_Email
53eae167
RB
447/** Filters users based on email address
448 * @param $email Email whose owner we are looking for
449 */
aa21c568
FB
450class UFC_Email implements UserFilterCondition
451{
452 private $email;
453 public function __construct($email)
454 {
455 $this->email = $email;
456 }
457
458 public function buildCondition(UserFilter &$uf)
459 {
460 if (User::isForeignEmailAddress($this->email)) {
461 $sub = $uf->addEmailRedirectFilter($this->email);
462 return XDB::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email);
21401768
FB
463 } else if (User::isVirtualEmailAddress($this->email)) {
464 $sub = $uf->addVirtualEmailFilter($this->email);
465 return 'vr' . $sub . '.redirect IS NOT NULL';
aa21c568 466 } else {
21401768
FB
467 @list($user, $domain) = explode('@', $this->email);
468 $sub = $uf->addAliasFilter($user);
aa21c568
FB
469 return 'al' . $sub . '.alias IS NOT NULL';
470 }
471 }
472}
8363588b 473// }}}
d865c296 474
8363588b 475// {{{ class UFC_EmailList
53eae167
RB
476/** Filters users base on an email list
477 * @param $emails List of emails whose owner must be selected
478 */
21401768
FB
479class UFC_EmailList implements UserFilterCondition
480{
481 private $emails;
482 public function __construct($emails)
483 {
484 $this->emails = $emails;
485 }
486
487 public function buildCondition(UserFilter &$uf)
488 {
489 $email = null;
490 $virtual = null;
491 $alias = null;
492 $cond = array();
493
494 if (count($this->emails) == 0) {
495 return UserFilterCondition::COND_TRUE;
496 }
497
498 foreach ($this->emails as $entry) {
499 if (User::isForeignEmailAddress($entry)) {
500 if (is_null($email)) {
501 $email = $uf->addEmailRedirectFilter();
502 }
503 $cond[] = XDB::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry);
504 } else if (User::isVirtualEmailAddress($entry)) {
505 if (is_null($virtual)) {
506 $virtual = $uf->addVirtualEmailFilter();
507 }
508 $cond[] = XDB::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry);
509 } else {
510 if (is_null($alias)) {
511 $alias = $uf->addAliasFilter();
512 }
513 @list($user, $domain) = explode('@', $entry);
514 $cond[] = XDB::format('al' . $alias . '.alias = {?}', $user);
515 }
516 }
517 return '(' . implode(') OR (', $cond) . ')';
518 }
519}
8363588b 520// }}}
d865c296 521
8363588b 522// {{{ class UFC_Address
c4b24511
RB
523/** Filters users based on their address
524 * @param $field Field of the address used for filtering (city, street, ...)
525 * @param $text Text for filter
526 * @param $mode Mode for search (PREFIX, SUFFIX, ...)
527 */
528class UFC_Address implements UserFilterCondition
529{
530 const PREFIX = 1;
531 const SUFFIX = 2;
532 const CONTAINS = 3;
533
534 private $field;
535 private $text;
536 private $mode;
537
538 public function __construct($field, $text, $mode)
539 {
540 $this->field = $field;
541 $this->text = $text;
542 $this->mode = $mode;
543 }
544
545 public function buildCondition(UserFilter &$uf)
546 {
547 $left = 'pa.' . $field;
548 $op = ' LIKE ';
549 if (($this->mode & self::CONTAINS) == 0) {
550 $right = XDB::format('{?}', $this->text);
551 $op = ' = ';
552 } else if (($this->mode & self::CONTAINS) == self::PREFIX) {
553 $right = XDB::format('CONCAT({?}, \'%\')', $this->text);
554 } else if (($this->mode & self::CONTAINS) == self::SUFFIX) {
555 $right = XDB::format('CONCAT(\'%\', {?})', $this->text);
556 } else {
557 $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text);
558 }
559 $cond = $left . $op . $right;
560 $uf->addAddressFilter();
561 return $cond;
562 }
563}
8363588b 564// }}}
c4b24511 565
8363588b 566// {{{ class UFC_Corps
4083b126
RB
567/** Filters users based on the corps they belong to
568 * @param $corps Corps we are looking for (abbreviation)
569 * @param $type Whether we search for original or current corps
570 */
571class UFC_Corps implements UserFilterCondition
572{
573 const CURRENT=1;
574 const ORIGIN=2;
575
576 private $corps;
577 private $type;
578
579 public function __construct($corps, $type = self::CURRENT)
580 {
581 $this->corps = $corps;
582 $this->type = $type;
583 }
584
585 public function buildCondition(UserFilter &$uf)
586 {
587 /** Tables shortcuts :
588 * pc for profile corps,
589 * pceo for profile_corps_enum - orginal
590 * pcec for profile_corps_enum - current
591 */
592 $sub = $uf->addCorpsFilter($this->type);
593 $cond = $sub . '.abbreviation = ' . $corps;
594 return $cond;
595 }
596}
8363588b 597// }}}
4083b126 598
8363588b 599// {{{ class UFC_Corps_Rank
4083b126
RB
600/** Filters users based on their rank in the corps
601 * @param $rank Rank we are looking for (abbreviation)
602 */
603class UFC_Corps_Rank implements UserFilterCondition
604{
605 private $rank;
606 public function __construct($rank)
607 {
608 $this->rank = $rank;
609 }
610
611 public function buildCondition(UserFilter &$uf)
612 {
613 /** Tables shortcuts :
614 * pcr for profile_corps_rank
615 */
616 $sub = $uf->addCorpsRankFilter();
617 $cond = $sub . '.abbreviation = ' . $rank;
618 return $cond;
619 }
620}
8363588b 621// }}}
4083b126 622
6a99c3ac
RB
623// {{{ class UFC_Job_Company
624/** Filters users based on the company they belong to
625 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
626 * @param $value The searched value
627 */
628class UFC_Job_Company extends UserFilterCondition
629{
630 const JOBID = 'id';
631 const JOBNAME = 'name';
632 const JOBACRONYM = 'acronym';
633
634 private $type;
635 private $value;
636
637 public function __construct($type, $value)
638 {
639 $this->assertType($type);
640 $this->type = $type;
641 $this->value = $value;
642 }
643
644 private function assertType($type)
645 {
646 if ($type != self::JOBID && $type != self::JOBNAME && $type != self::JOBACRONYM) {
647 Platal::page()->killError("Type de recherche non valide");
648 }
649 }
650
651 public function buildCondition(UserFilter &$uf)
652 {
653 $sub = $uf->addJobCompanyFilter();
654 $cond = $sub . '.' . $this->type . ' = ' . XDB::format('{?}', $this->value);
655 return $cond;
656 }
657}
658// }}}
659
660// {{{ class UFC_Job_Sectorization
661/** Filters users based on the ((sub)sub)sector they work in
662 * @param $sector The sector searched
663 * @param $subsector The subsector
664 * @param $subsubsector The subsubsector
665 */
666class UFC_Job_Sectorization extends UserFilterCondition
667{
668
669 private $sector;
670 private $subsector;
671 private $subsubsector;
672
673 public function __construct($sector = null, $subsector = null, $subsubsector = null)
674 {
675 $this->sector = $sector;
676 $this->subsector = $subsector;
677 $this->subsubsector = $subsubsector;
678 }
679
680 public function buildCondition(UserFilter &$uf)
681 {
682 // No need to add the JobFilter, it will be done by addJobSectorizationFilter
683 $conds = array();
684 if ($this->sector !== null) {
685 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SECTOR);
686 $conds[] = $sub . '.id = ' . XDB::format('{?}', $this->sector);
687 }
688 if ($this->subsector !== null) {
689 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSECTOR);
690 $conds[] = $sub . '.id = ' . XDB::format('{?}', $this->subsector);
691 }
692 if ($this->subsubsector !== null) {
693 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSUBSECTOR);
694 $conds[] = $sub . '.id = ' . XDB::format('{?}', $this->subsubsector);
695 }
696 return implode(' AND ', $conds);
697 }
698}
699// }}}
700
701// {{{ class UFC_Job_Description
702/** Filters users based on their job description
703 * @param $description The text being searched for
704 * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
705 */
706class UFC_Job_Description extends UserFilterCondition
707{
708
709 /** Meta-filters
710 * Built with binary OR on UserFilter::JOB_*
711 */
712 const ANY = 31;
713 const SECTORIZATION = 15;
714
715 private $description;
716 private $fields;
717
718 public function __construct($description)
719 {
720 $this->fields = $fields;
721 $this->description = $description;
722 }
723
724 public function buildCondition(UserFilter &$uf)
725 {
726 $conds = array();
727 if ($this->fields & UserFilter::JOB_USERDEFINED) {
728 $sub = $uf->addJobFilter();
729 $conds[] = $sub . '.description LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
730 }
731 if ($this->fields & UserFilter::JOB_SECTOR) {
732 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SECTOR);
733 $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
734 }
735 if ($this->fields & UserFilter::JOB_SUBSECTOR) {
736 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSECTOR);
737 $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
738 }
739 if ($this->fields & UserFilter::JOB_SUBSUBSECTOR) {
740 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSUBSECTOR);
741 $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
742 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_ALTERNATES);
743 $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
744 }
745 return implode(' OR ', $conds);
746 }
747}
748// }}}
749
0a2e9c74
RB
750// {{{ class UFC_Networking
751/** Filters users based on network identity (IRC, ...)
752 * @param $type Type of network (-1 for any)
753 * @param $value Value to search
754 */
755class UFC_Networking extends UserFilterCondition
756{
757 private $type;
758 private $value;
759
760 public function __construct($type, $value)
761 {
762 $this->type = $type;
763 $this->value = $value;
764 }
765
766 public function buildCondition(UserFilter &$uf)
767 {
768 $sub = $uf->addNetworkingFilter();
769 $conds = array();
770 $conds[] = $sub . '.address = ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->value);
771 if ($this->type != -1) {
772 $conds[] = $sub . '.network_type = ' . XDB::format('{?}', $this->type);
773 }
774 return implode(' AND ', $conds);
775 }
776}
777// }}}
778
6d62969e
RB
779// {{{ class UFC_Phone
780/** Filters users based on their phone number
781 * @param $num_type Type of number (pro/user/home)
782 * @param $phone_type Type of phone (fixed/mobile/fax)
783 * @param $number Phone number
784 */
785class UFC_Phone extends UserFilterCondition
786{
787 const NUM_PRO = 'pro';
788 const NUM_USER = 'user';
789 const NUM_HOME = 'address';
790 const NUM_ANY = 'any';
791
792 const PHONE_FIXED = 'fixed';
793 const PHONE_MOBILE = 'mobile';
794 const PHONE_FAX = 'fax';
795 const PHONE_ANY = 'any';
796
797 private $num_type;
798 private $phone_type;
799 private $number;
800
801 public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY)
802 {
803 require_once('profil.inc.php');
804 $this->number = $number;
805 $this->num_type = $num_type;
806 $this->phone_type = format_phone_number($phone_type);
807 }
808
809 public function buildCondition(UserFilter &$uf)
810 {
811 $sub = $uf->addPhoneFilter();
812 $conds = array();
813 $conds[] = $sub . '.search_tel = ' . XDB::format('{?}', $this->number);
814 if ($this->num_type != self::NUM_ANY) {
815 $conds[] = $sub . '.link_type = ' . XDB::format('{?}', $this->num_type);
816 }
817 if ($this->phone_type != self::PHONE_ANY) {
818 $conds[] = $sub . '.tel_type = ' . XDB::format('{?}', $this->phone_type);
819 }
820 return implode(' AND ', $conds);
821 }
822}
823// }}}
824
ceb512d2
RB
825// {{{ class UFC_Medal
826/** Filters users based on their medals
827 * @param $medal ID of the medal
828 * @param $grade Grade of the medal (null for 'any')
829 */
830class UFC_Medal extends UserFilterCondition
831{
832 private $medal;
833 private $grade;
834
835 public function __construct($medal, $grade = null)
836 {
837 $this->medal = $medal;
838 $this->grade = $grade;
839 }
840
841 public function buildCondition(UserFilter &$uf)
842 {
843 $conds = array();
844 $sub = $uf->addMedalFilter();
845 $conds[] = $sub . '.mid = ' . XDB::format('{?}', $this->medal);
846 if ($this->grade != null) {
847 $conds[] = $sub . '.gid = ' . XDB::format('{?}', $this->grade);
848 }
849 return implode(' AND ', $conds);
850 }
851}
852// }}}
853
671b7073
RB
854// {{{ class UFC_Mentor_Expertise
855/** Filters users by mentoring expertise
856 * @param $expertise Domain of expertise
857 */
858class UFC_Mentor_Expertise extends UserFilterCondition
859{
860 private $expertise;
861
862 public function __construct($expertise)
863 {
864 $this->expertise = $expertise;
865 }
866
867 public function buildCondition(UserFilter &$uf)
868 {
869 $sub = $uf->addMentorFilter(UserFilter::MENTOR_EXPERTISE);
870 return $sub . '.expertise LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\'', $this->expertise);
871 }
872}
873// }}}
874
875// {{{ class UFC_Mentor_Country
876/** Filters users by mentoring country
877 * @param $country Two-letters code of country being searched
878 */
879class UFC_Mentor_Country extends UserFilterCondition
880{
881 private $country;
882
883 public function __construct($country)
884 {
885 $this->country = $country;
886 }
887
888 public function buildCondition(UserFilter &$uf)
889 {
890 $sub = $uf->addMentorFilter(UserFilter::MENTOR_COUNTRY);
891 return $sub . '.country = ' . XDB::format('{?}', $this->country);
892 }
893}
894// }}}
895
896// {{{ class UFC_Mentor_Sectorization
897/** Filters users based on mentoring (sub|)sector
898 * @param $sector ID of sector
899 * @param $subsector Subsector (null for any)
900 */
901class UFC_Mentor_Sectorization extends UserFilterCondition
902{
903 private $sector;
904 private $subsector;
905
906 public function __construct($sector, $subsector = null)
907 {
908 $this->sector = $sector;
909 $this->subsubsector = $subsector;
910 }
911
912 public function buildCondition(UserFilter &$uf)
913 {
914 $sub = $uf->addMentorFilter(UserFilter::MENTOR_SECTOR);
915 $conds = array();
916 $conds[] = $sub . '.sectorid = ' . XDB::format('{?}', $this->sector);
917 if ($this->subsector != null) {
918 $conds[] = $sub . '.subsectorid = ' . XDB::format('{?}', $this->subsector);
919 }
920 return implode(' AND ', $conds);
921 }
922}
923// }}}
924
8363588b 925// {{{ class UFC_UserRelated
53eae167
RB
926/** Filters users based on a relation toward on user
927 * @param $user User to which searched users are related
928 */
4e7bf1e0 929abstract class UFC_UserRelated implements UserFilterCondition
3f42a6ad 930{
009b8ab7
FB
931 protected $user;
932 public function __construct(PlUser &$user)
933 {
934 $this->user =& $user;
3f42a6ad 935 }
4e7bf1e0 936}
8363588b 937// }}}
3f42a6ad 938
8363588b 939// {{{ class UFC_Contact
53eae167
RB
940/** Filters users who belongs to selected user's contacts
941 */
4e7bf1e0
FB
942class UFC_Contact extends UFC_UserRelated
943{
3f42a6ad
FB
944 public function buildCondition(UserFilter &$uf)
945 {
009b8ab7 946 $sub = $uf->addContactFilter($this->user->id());
3f42a6ad
FB
947 return 'c' . $sub . '.contact IS NOT NULL';
948 }
949}
8363588b 950// }}}
3f42a6ad 951
8363588b 952// {{{ class UFC_WatchRegistration
53eae167
RB
953/** Filters users being watched by selected user
954 */
4e7bf1e0
FB
955class UFC_WatchRegistration extends UFC_UserRelated
956{
957 public function buildCondition(UserFilter &$uf)
958 {
009b8ab7
FB
959 if (!$this->user->watch('registration')) {
960 return UserFilterCondition::COND_FALSE;
961 }
962 $uids = $this->user->watchUsers();
963 if (count($uids) == 0) {
964 return UserFilterCondition::COND_FALSE;
965 } else {
07eb5b0e 966 return '$UID IN ' . XDB::formatArray($uids);
009b8ab7 967 }
4e7bf1e0
FB
968 }
969}
8363588b 970// }}}
4e7bf1e0 971
8363588b 972// {{{ class UFC_WatchPromo
53eae167
RB
973/** Filters users belonging to a promo watched by selected user
974 * @param $user Selected user (the one watching promo)
975 * @param $grade Formation the user is watching
976 */
4e7bf1e0
FB
977class UFC_WatchPromo extends UFC_UserRelated
978{
979 private $grade;
009b8ab7 980 public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING)
4e7bf1e0 981 {
009b8ab7 982 parent::__construct($user);
4e7bf1e0
FB
983 $this->grade = $grade;
984 }
985
986 public function buildCondition(UserFilter &$uf)
987 {
009b8ab7
FB
988 $promos = $this->user->watchPromos();
989 if (count($promos) == 0) {
990 return UserFilterCondition::COND_FALSE;
991 } else {
992 $sube = $uf->addEducationFilter(true, $this->grade);
993 $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
07eb5b0e 994 return $field . ' IN ' . XDB::formatArray($promos);
009b8ab7 995 }
4e7bf1e0
FB
996 }
997}
8363588b 998// }}}
4e7bf1e0 999
8363588b 1000// {{{ class UFC_WatchContact
53eae167
RB
1001/** Filters users watched by selected user
1002 */
009b8ab7 1003class UFC_WatchContact extends UFC_Contact
4e7bf1e0
FB
1004{
1005 public function buildCondition(UserFilter &$uf)
1006 {
009b8ab7
FB
1007 if (!$this->user->watchContacts()) {
1008 return UserFilterCondition::COND_FALSE;
1009 }
1010 return parent::buildCondition($uf);
4e7bf1e0
FB
1011 }
1012}
8363588b 1013// }}}
4e7bf1e0
FB
1014
1015
d865c296
FB
1016/******************
1017 * ORDERS
1018 ******************/
1019
8363588b 1020// {{{ class UserFilterOrder
d865c296
FB
1021abstract class UserFilterOrder
1022{
1023 protected $desc = false;
009b8ab7
FB
1024 public function __construct($desc = false)
1025 {
1026 $this->desc = $desc;
1027 }
d865c296
FB
1028
1029 public function buildSort(UserFilter &$uf)
1030 {
1031 $sel = $this->getSortTokens($uf);
1032 if (!is_array($sel)) {
1033 $sel = array($sel);
1034 }
1035 if ($this->desc) {
1036 foreach ($sel as $k=>$s) {
1037 $sel[$k] = $s . ' DESC';
1038 }
1039 }
1040 return $sel;
1041 }
1042
1043 abstract protected function getSortTokens(UserFilter &$uf);
1044}
8363588b 1045// }}}
d865c296 1046
8363588b 1047// {{{ class UFO_Promo
53eae167
RB
1048/** Orders users by promo
1049 * @param $grade Formation whose promo users should be sorted by (restricts results to users of that formation)
1050 * @param $desc Whether sort is descending
1051 */
d865c296
FB
1052class UFO_Promo extends UserFilterOrder
1053{
1054 private $grade;
1055
1056 public function __construct($grade = null, $desc = false)
1057 {
009b8ab7 1058 parent::__construct($desc);
d865c296 1059 $this->grade = $grade;
d865c296
FB
1060 }
1061
1062 protected function getSortTokens(UserFilter &$uf)
1063 {
1064 if (UserFilter::isGrade($this->grade)) {
1065 $sub = $uf->addEducationFilter($this->grade);
1066 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
1067 } else {
1068 $sub = $uf->addDisplayFilter();
1069 return 'pd' . $sub . '.promo';
1070 }
1071 }
1072}
8363588b 1073// }}}
d865c296 1074
8363588b 1075// {{{ class UFO_Name
53eae167
RB
1076/** Sorts users by name
1077 * @param $type Type of name on which to sort (firstname, ...)
1078 * @param $variant Variant of that name to user (marital, ordinary, ...)
1079 * @param $particle Set to true if particles should be included in the sorting order
1080 * @param $desc If sort order should be descending
1081 */
d865c296
FB
1082class UFO_Name extends UserFilterOrder
1083{
1084 private $type;
1085 private $variant;
1086 private $particle;
1087
1088 public function __construct($type, $variant = null, $particle = false, $desc = false)
1089 {
009b8ab7 1090 parent::__construct($desc);
d865c296
FB
1091 $this->type = $type;
1092 $this->variant = $variant;
1093 $this->particle = $particle;
d865c296
FB
1094 }
1095
1096 protected function getSortTokens(UserFilter &$uf)
1097 {
1098 if (UserFilter::isDisplayName($this->type)) {
1099 $sub = $uf->addDisplayFilter();
1100 return 'pd' . $sub . '.' . $this->type;
1101 } else {
1102 $sub = $uf->addNameFilter($this->type, $this->variant);
1103 if ($this->particle) {
1104 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1105 } else {
1106 return 'pn' . $sub . '.name';
1107 }
1108 }
1109 }
1110}
8363588b 1111// }}}
d865c296 1112
8363588b 1113// {{{ class UFO_Registration
53eae167
RB
1114/** Sorts users based on registration date
1115 */
38c6fe96
FB
1116class UFO_Registration extends UserFilterOrder
1117{
009b8ab7 1118 protected function getSortTokens(UserFilter &$uf)
38c6fe96 1119 {
009b8ab7 1120 return 'a.registration_date';
38c6fe96 1121 }
009b8ab7 1122}
8363588b 1123// }}}
38c6fe96 1124
8363588b 1125// {{{ class UFO_Birthday
53eae167
RB
1126/** Sorts users based on next birthday date
1127 */
009b8ab7
FB
1128class UFO_Birthday extends UserFilterOrder
1129{
38c6fe96
FB
1130 protected function getSortTokens(UserFilter &$uf)
1131 {
009b8ab7 1132 return 'p.next_birthday';
38c6fe96
FB
1133 }
1134}
8363588b 1135// }}}
38c6fe96 1136
8363588b 1137// {{{ class UFO_ProfileUpdate
53eae167
RB
1138/** Sorts users based on last profile update
1139 */
009b8ab7
FB
1140class UFO_ProfileUpdate extends UserFilterOrder
1141{
1142 protected function getSortTokens(UserFilter &$uf)
1143 {
1144 return 'p.last_change';
1145 }
1146}
8363588b 1147// }}}
009b8ab7 1148
8363588b 1149// {{{ class UFO_Death
53eae167
RB
1150/** Sorts users based on death date
1151 */
009b8ab7
FB
1152class UFO_Death extends UserFilterOrder
1153{
1154 protected function getSortTokens(UserFilter &$uf)
1155 {
1156 return 'p.deathdate';
1157 }
1158}
8363588b 1159// }}}
009b8ab7
FB
1160
1161
d865c296
FB
1162/***********************************
1163 *********************************
1164 USER FILTER CLASS
1165 *********************************
1166 ***********************************/
1167
8363588b 1168// {{{ class UserFilter
a087cc8d
FB
1169class UserFilter
1170{
d865c296
FB
1171 static private $joinMethods = array();
1172
a087cc8d 1173 private $root;
24e08e33 1174 private $sort = array();
784745ce 1175 private $query = null;
24e08e33 1176 private $orderby = null;
784745ce 1177
aa21c568 1178 private $lastcount = null;
d865c296 1179
24e08e33 1180 public function __construct($cond = null, $sort = null)
5dd9d823 1181 {
d865c296
FB
1182 if (empty(self::$joinMethods)) {
1183 $class = new ReflectionClass('UserFilter');
1184 foreach ($class->getMethods() as $method) {
1185 $name = $method->getName();
1186 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
1187 self::$joinMethods[] = $name;
1188 }
1189 }
1190 }
5dd9d823
FB
1191 if (!is_null($cond)) {
1192 if ($cond instanceof UserFilterCondition) {
1193 $this->setCondition($cond);
1194 }
1195 }
24e08e33
FB
1196 if (!is_null($sort)) {
1197 if ($sort instanceof UserFilterOrder) {
1198 $this->addSort($sort);
d865c296
FB
1199 } else if (is_array($sort)) {
1200 foreach ($sort as $s) {
1201 $this->addSort($s);
1202 }
24e08e33
FB
1203 }
1204 }
5dd9d823
FB
1205 }
1206
784745ce
FB
1207 private function buildQuery()
1208 {
d865c296
FB
1209 if (is_null($this->orderby)) {
1210 $orders = array();
1211 foreach ($this->sort as $sort) {
1212 $orders = array_merge($orders, $sort->buildSort($this));
1213 }
1214 if (count($orders) == 0) {
1215 $this->orderby = '';
1216 } else {
1217 $this->orderby = 'ORDER BY ' . implode(', ', $orders);
1218 }
1219 }
784745ce
FB
1220 if (is_null($this->query)) {
1221 $where = $this->root->buildCondition($this);
1222 $joins = $this->buildJoins();
1223 $this->query = 'FROM accounts AS a
eb1449b8
FB
1224 LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
1225 LEFT JOIN profiles AS p ON (p.pid = ap.pid)
784745ce
FB
1226 ' . $joins . '
1227 WHERE (' . $where . ')';
1228 }
1229 }
1230
1231 private function formatJoin(array $joins)
1232 {
1233 $str = '';
1234 foreach ($joins as $key => $infos) {
1235 $mode = $infos[0];
1236 $table = $infos[1];
1237 if ($mode == 'inner') {
1238 $str .= 'INNER JOIN ';
1239 } else if ($mode == 'left') {
1240 $str .= 'LEFT JOIN ';
1241 } else {
1242 Platal::page()->kill("Join mode error");
1243 }
1244 $str .= $table . ' AS ' . $key;
1245 if (isset($infos[2])) {
4927ee54 1246 $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')';
784745ce
FB
1247 }
1248 $str .= "\n";
1249 }
1250 return $str;
1251 }
1252
1253 private function buildJoins()
1254 {
d865c296
FB
1255 $joins = array();
1256 foreach (self::$joinMethods as $method) {
1257 $joins = array_merge($joins, $this->$method());
1258 }
784745ce
FB
1259 return $this->formatJoin($joins);
1260 }
a087cc8d 1261
d865c296
FB
1262 private function getUIDList($uids = null, $count = null, $offset = null)
1263 {
1264 $this->buildQuery();
1265 $limit = '';
1266 if (!is_null($count)) {
1267 if (!is_null($offset)) {
76cbe885 1268 $limit = XDB::format('LIMIT {?}, {?}', (int)$offset, (int)$count);
d865c296 1269 } else {
76cbe885 1270 $limit = XDB::format('LIMIT {?}', (int)$count);
d865c296
FB
1271 }
1272 }
1273 $cond = '';
1274 if (!is_null($uids)) {
07eb5b0e 1275 $cond = ' AND a.uid IN ' . XDB::formatArray($uids);
d865c296
FB
1276 }
1277 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1278 ' . $this->query . $cond . '
1279 GROUP BY a.uid
1280 ' . $this->orderby . '
1281 ' . $limit);
1282 $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
1283 return $fetched;
1284 }
1285
a087cc8d
FB
1286 /** Check that the user match the given rule.
1287 */
1288 public function checkUser(PlUser &$user)
1289 {
784745ce
FB
1290 $this->buildQuery();
1291 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1292 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
1293 return $count == 1;
a087cc8d
FB
1294 }
1295
1296 /** Filter a list of user to extract the users matching the rule.
1297 */
d865c296 1298 public function filter(array $users, $count = null, $offset = null)
a087cc8d 1299 {
4927ee54
FB
1300 $this->buildQuery();
1301 $table = array();
1302 $uids = array();
1303 foreach ($users as $user) {
07eb5b0e
FB
1304 if ($user instanceof PlUser) {
1305 $uid = $user->id();
1306 } else {
1307 $uid = $user;
1308 }
1309 $uids[] = $uid;
1310 $table[$uid] = $user;
4927ee54 1311 }
d865c296 1312 $fetched = $this->getUIDList($uids, $count, $offset);
a087cc8d 1313 $output = array();
4927ee54
FB
1314 foreach ($fetched as $uid) {
1315 $output[] = $table[$uid];
a087cc8d
FB
1316 }
1317 return $output;
1318 }
1319
d865c296 1320 public function getUIDs($count = null, $offset = null)
4927ee54 1321 {
d865c296
FB
1322 return $this->getUIDList(null, $count, $offset);
1323 }
1324
1325 public function getUsers($count = null, $offset = null)
1326 {
1327 return User::getBulkUsersWithUIDs($this->getUIDs($count, $offset));
4927ee54
FB
1328 }
1329
d865c296 1330 public function getTotalCount()
4927ee54 1331 {
38c6fe96 1332 if (is_null($this->lastcount)) {
aa21c568 1333 $this->buildQuery();
7e735012
FB
1334 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
1335 ' . $this->query);
38c6fe96
FB
1336 } else {
1337 return $this->lastcount;
1338 }
4927ee54
FB
1339 }
1340
a087cc8d
FB
1341 public function setCondition(UserFilterCondition &$cond)
1342 {
1343 $this->root =& $cond;
784745ce 1344 $this->query = null;
a087cc8d
FB
1345 }
1346
24e08e33
FB
1347 public function addSort(UserFilterOrder &$sort)
1348 {
d865c296
FB
1349 $this->sort[] = $sort;
1350 $this->orderby = null;
24e08e33
FB
1351 }
1352
a087cc8d
FB
1353 static public function getLegacy($promo_min, $promo_max)
1354 {
a087cc8d 1355 if ($promo_min != 0) {
784745ce 1356 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
5dd9d823
FB
1357 } else {
1358 $min = new UFC_True();
a087cc8d 1359 }
a087cc8d 1360 if ($promo_max != 0) {
784745ce 1361 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
a087cc8d 1362 } else {
5dd9d823 1363 $max = new UFC_True();
a087cc8d 1364 }
5dd9d823 1365 return new UserFilter(new UFC_And($min, $max));
a087cc8d 1366 }
784745ce 1367
07eb5b0e
FB
1368 static public function sortByName()
1369 {
1370 return array(new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
1371 }
1372
1373 static public function sortByPromo()
1374 {
1375 return array(new UFO_Promo(), new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
1376 }
1377
aa21c568
FB
1378 static private function getDBSuffix($string)
1379 {
1380 return preg_replace('/[^a-z0-9]/i', '', $string);
1381 }
1382
1383
1384 private $option = 0;
1385 private function register_optional(array &$table, $val)
1386 {
1387 if (is_null($val)) {
1388 $sub = $this->option++;
1389 $index = null;
1390 } else {
1391 $sub = self::getDBSuffix($val);
1392 $index = $val;
1393 }
1394 $sub = '_' . $sub;
1395 $table[$sub] = $index;
1396 return $sub;
1397 }
784745ce 1398
d865c296
FB
1399 /** DISPLAY
1400 */
38c6fe96 1401 const DISPLAY = 'display';
d865c296
FB
1402 private $pd = false;
1403 public function addDisplayFilter()
1404 {
1405 $this->pd = true;
1406 return '';
1407 }
1408
1409 private function displayJoins()
1410 {
1411 if ($this->pd) {
1412 return array('pd' => array('left', 'profile_display', '$ME.pid = $PID'));
1413 } else {
1414 return array();
1415 }
1416 }
1417
784745ce
FB
1418 /** NAMES
1419 */
d865c296 1420 /* name tokens */
784745ce
FB
1421 const LASTNAME = 'lastname';
1422 const FIRSTNAME = 'firstname';
1423 const NICKNAME = 'nickname';
1424 const PSEUDONYM = 'pseudonym';
1425 const NAME = 'name';
d865c296 1426 /* name variants */
784745ce
FB
1427 const VN_MARITAL = 'marital';
1428 const VN_ORDINARY = 'ordinary';
1429 const VN_OTHER = 'other';
1430 const VN_INI = 'ini';
d865c296
FB
1431 /* display names */
1432 const DN_FULL = 'directory_name';
1433 const DN_DISPLAY = 'yourself';
1434 const DN_YOURSELF = 'yourself';
1435 const DN_DIRECTORY = 'directory_name';
1436 const DN_PRIVATE = 'private_name';
1437 const DN_PUBLIC = 'public_name';
1438 const DN_SHORT = 'short_name';
1439 const DN_SORT = 'sort_name';
784745ce
FB
1440
1441 static public $name_variants = array(
1442 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
d865c296
FB
1443 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
1444 );
784745ce
FB
1445
1446 static public function assertName($name)
1447 {
1448 if (!Profile::getNameTypeId($name)) {
1449 Platal::page()->kill('Invalid name type');
1450 }
1451 }
1452
d865c296
FB
1453 static public function isDisplayName($name)
1454 {
1455 return $name == self::DN_FULL || $name == self::DN_DISPLAY
1456 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
1457 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
1458 || $name == self::DN_SHORT || $name == self::DN_SORT;
1459 }
1460
784745ce 1461 private $pn = array();
784745ce
FB
1462 public function addNameFilter($type, $variant = null)
1463 {
1464 if (!is_null($variant)) {
1465 $ft = $type . '_' . $variant;
1466 } else {
1467 $ft = $type;
1468 }
1469 $sub = '_' . $ft;
1470 self::assertName($ft);
1471
1472 if (!is_null($variant) && $variant == 'other') {
aa21c568 1473 $sub .= $this->option++;
784745ce
FB
1474 }
1475 $this->pn[$sub] = Profile::getNameTypeId($ft);
1476 return $sub;
1477 }
1478
1479 private function nameJoins()
1480 {
1481 $joins = array();
1482 foreach ($this->pn as $sub => $type) {
1483 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
1484 }
1485 return $joins;
1486 }
1487
1488
1489 /** EDUCATION
1490 */
1491 const GRADE_ING = 'Ing.';
1492 const GRADE_PHD = 'PhD';
1493 const GRADE_MST = 'M%';
1494 static public function isGrade($grade)
1495 {
38c6fe96 1496 return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST;
784745ce
FB
1497 }
1498
1499 static public function assertGrade($grade)
1500 {
1501 if (!self::isGrade($grade)) {
1502 Platal::page()->killError("Diplôme non valide");
1503 }
1504 }
1505
d865c296
FB
1506 static public function promoYear($grade)
1507 {
1508 // XXX: Definition of promotion for phds and masters might change in near future.
1509 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
1510 }
1511
784745ce
FB
1512 private $pepe = array();
1513 private $with_pee = false;
784745ce
FB
1514 public function addEducationFilter($x = false, $grade = null)
1515 {
1516 if (!$x) {
aa21c568
FB
1517 $index = $this->option;
1518 $sub = $this->option++;
784745ce
FB
1519 } else {
1520 self::assertGrade($grade);
1521 $index = $grade;
1522 $sub = $grade[0];
1523 $this->with_pee = true;
1524 }
1525 $sub = '_' . $sub;
1526 $this->pepe[$index] = $sub;
1527 return $sub;
1528 }
1529
1530 private function educationJoins()
1531 {
1532 $joins = array();
1533 if ($this->with_pee) {
1534 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
1535 }
1536 foreach ($this->pepe as $grade => $sub) {
1537 if ($this->isGrade($grade)) {
1538 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
1539 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
1540 XDB::format('{?}', $grade));
1541 } else {
1542 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID');
1543 $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
1544 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
1545 }
1546 }
1547 return $joins;
1548 }
4927ee54
FB
1549
1550
1551 /** GROUPS
1552 */
1553 private $gpm = array();
4927ee54
FB
1554 public function addGroupFilter($group = null)
1555 {
1556 if (!is_null($group)) {
1557 if (ctype_digit($group)) {
1558 $index = $sub = $group;
1559 } else {
1560 $index = $group;
aa21c568 1561 $sub = self::getDBSuffix($group);
4927ee54
FB
1562 }
1563 } else {
aa21c568 1564 $sub = 'group_' . $this->option++;
4927ee54
FB
1565 $index = null;
1566 }
1567 $sub = '_' . $sub;
1568 $this->gpm[$sub] = $index;
1569 return $sub;
1570 }
1571
1572 private function groupJoins()
1573 {
1574 $joins = array();
1575 foreach ($this->gpm as $sub => $key) {
1576 if (is_null($key)) {
1577 $joins['gpa' . $sub] = array('inner', 'groupex.asso');
1578 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1579 } else if (ctype_digit($key)) {
1580 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
1581 } else {
1582 $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key));
1583 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1584 }
1585 }
1586 return $joins;
1587 }
aa21c568
FB
1588
1589 /** EMAILS
1590 */
1591 private $e = array();
1592 public function addEmailRedirectFilter($email = null)
1593 {
1594 return $this->register_optional($this->e, $email);
1595 }
1596
1597 private $ve = array();
1598 public function addVirtualEmailFilter($email = null)
1599 {
21401768 1600 $this->addAliasFilter(self::ALIAS_FORLIFE);
aa21c568
FB
1601 return $this->register_optional($this->ve, $email);
1602 }
1603
21401768
FB
1604 const ALIAS_BEST = 'bestalias';
1605 const ALIAS_FORLIFE = 'forlife';
aa21c568
FB
1606 private $al = array();
1607 public function addAliasFilter($alias = null)
1608 {
1609 return $this->register_optional($this->al, $alias);
1610 }
1611
1612 private function emailJoins()
1613 {
1614 global $globals;
1615 $joins = array();
1616 foreach ($this->e as $sub=>$key) {
1617 if (is_null($key)) {
1618 $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
1619 } else {
1620 $joins['e' . $sub] = array('left', 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
1621 }
1622 }
21401768 1623 foreach ($this->al as $sub=>$key) {
aa21c568 1624 if (is_null($key)) {
21401768
FB
1625 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
1626 } else if ($key == self::ALIAS_BEST) {
1627 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
1628 } else if ($key == self::ALIAS_FORLIFE) {
1629 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
aa21c568 1630 } else {
21401768 1631 $joins['al' . $sub] = array('left', 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
aa21c568 1632 }
aa21c568 1633 }
21401768 1634 foreach ($this->ve as $sub=>$key) {
aa21c568 1635 if (is_null($key)) {
21401768 1636 $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\'');
aa21c568 1637 } else {
21401768 1638 $joins['v' . $sub] = array('left', 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
aa21c568 1639 }
21401768
FB
1640 $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid
1641 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
1642 CONCAT(al_forlife.alias, \'@\', {?}),
1643 a.email))',
1644 $globals->mail->domain, $globals->mail->domain2));
aa21c568
FB
1645 }
1646 return $joins;
1647 }
3f42a6ad
FB
1648
1649
c4b24511
RB
1650 /** ADDRESSES
1651 */
1652 private $pa = false;
1653 public function addAddressFilter()
1654 {
1655 $this->pa = true;
1656 }
1657
1658 private function addressJoins()
1659 {
1660 $joins = array();
1661 if ($this->pa) {
1662 $joins['pa'] = array('left', 'profile_address', '$ME.PID = $PID');
1663 }
1664 return $joins;
1665 }
1666
1667
4083b126
RB
1668 /** CORPS
1669 */
1670
1671 private $pc = false;
1672 private $pce = array();
1673 private $pcr = false;
1674 public function addCorpsFilter($type)
1675 {
1676 $this->pc = true;
1677 if ($type == UFC_Corps::CURRENT) {
1678 $pce['pcec'] = 'current_corpsid';
1679 return 'pcec';
1680 } else if ($type == UFC_Corps::ORIGIN) {
1681 $pce['pceo'] = 'original_corpsid';
1682 return 'pceo';
1683 }
1684 }
1685
1686 public function addCorpsRankFilter()
1687 {
1688 $this->pc = true;
1689 $this->pcr = true;
1690 return 'pcr';
1691 }
1692
1693 private function corpsJoins()
1694 {
1695 $joins = array();
1696 if ($this->pc) {
1697 $joins['pc'] = array('left', 'profile_corps', '$ME.uid = $UID');
1698 }
1699 if ($this->pcr) {
1700 $joins['pcr'] = array('left', 'profile_corps_rank_enum', '$ME.id = pc.rankid');
1701 }
1702 foreach($this->pce as $sub => $field) {
1703 $joins[$sub] = array('left', 'profile_corps_enum', '$ME.id = pc.' . $field);
1704 }
1705 return $joins;
1706 }
1707
6a99c3ac
RB
1708 /** JOBS
1709 */
1710
1711 const JOB_SECTOR = 1;
1712 const JOB_SUBSECTOR = 2;
1713 const JOB_SUBSUBSECTOR = 4;
1714 const JOB_ALTERNATES = 8;
1715 const JOB_USERDEFINED = 16;
1716
1717 /** Joins :
1718 * pj => profile_job
1719 * pje => profile_job_enum
1720 * pjse => profile_job_sector_enum
1721 * pjsse => profile_job_subsector_enum
1722 * pjssse => profile_job_subsubsector_enum
1723 * pja => profile_job_alternates
1724 */
1725 private $with_pj = false;
1726 private $with_pje = false;
1727 private $with_pjse = false;
1728 private $with_pjsse = false;
1729 private $with_pjssse = false;
1730 private $with_pja = false;
1731
1732 public function addJobFilter()
1733 {
1734 $this->with_pj = true;
1735 return 'pj';
1736 }
1737
1738 public function addJobCompanyFilter()
1739 {
1740 $this->addJobFilter();
1741 $this->with_pje = true;
1742 return 'pje';
1743 }
1744
1745 public function addJobSectorizationFilter($type)
1746 {
1747 $this->addJobFilter();
1748 if ($type == self::JOB_SECTOR) {
1749 $this->with_pjse = true;
1750 return 'pjse';
1751 } else if ($type == self::JOB_SUBSECTOR) {
1752 $this->with_pjsse = true;
1753 return 'pjsse';
1754 } else if ($type == self::JOB_SUBSUBSECTOR) {
1755 $this->with_pjssse = true;
1756 return 'pjssse';
1757 } else if ($type == self::JOB_ALTERNATES) {
1758 $this->with_pja = true;
1759 return 'pja';
1760 }
1761 }
1762
1763 private function jobJoins()
1764 {
1765 $joins = array();
1766 if ($this->with_pj) {
1767 $joins['pj'] = array('left', 'profile_job', '$ME.uid = $UID');
1768 }
1769 if ($this->with_pje) {
1770 $joins['pje'] = array('left', 'profile_job_enum', '$ME.id = pj.jobid');
1771 }
1772 if ($this->with_pjse) {
1773 $joins['pjse'] = array('left', 'profile_job_sector_enum', '$ME.id = pj.sectorid');
1774 }
1775 if ($this->with_pjsse) {
1776 $joins['pjsse'] = array('left', 'profile_job_subsector_enum', '$ME.id = pj.subsectorid');
1777 }
1778 if ($this->with_pjssse) {
1779 $joins['pjssse'] = array('left', 'profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
1780 }
1781 if ($this->with_pja) {
1782 $joins['pja'] = array('left', 'profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
1783 }
1784 return $joins;
1785 }
1786
0a2e9c74
RB
1787 /** NETWORKING
1788 */
1789
1790 private $with_pnw = false;
1791 public function addNetworkingFilter()
1792 {
1793 $this->with_pnw = true;
1794 return 'pnw';
1795 }
1796
1797 private function networkingJoins()
1798 {
1799 $joins = array();
1800 if ($this->with_pnw) {
1801 $joins['pnw'] = array('left', 'profile_networking', '$ME.uid = $UID');
1802 }
1803 return $joins;
1804 }
1805
6d62969e
RB
1806 /** PHONE
1807 */
1808
1809 private $with_phone = false;
1810
1811 public function addPhoneFilter()
1812 {
1813 $this->with_phone = true;
1814 return 'ptel';
1815 }
1816
1817 private function phoneJoins()
1818 {
1819 $joins = array();
1820 if ($this->with_phone) {
1821 $joins['ptel'] = array('left', 'profile_phone', '$ME.uid = $UID');
1822 }
1823 return $joins;
1824 }
1825
ceb512d2
RB
1826 /** MEDALS
1827 */
1828
1829 private $with_medals = false;
1830 public function addMedalFilter()
1831 {
1832 $this->with_medals = true;
1833 return 'pmed';
1834 }
1835
1836 private function medalJoins()
1837 {
1838 $joins = array();
1839 if ($this->with_medals) {
1840 $joins['pmed'] = array('left', 'profile_medals_sub', '$ME.uid = $UID');
1841 }
1842 return $joins;
1843 }
1844
671b7073
RB
1845 /** MENTORING
1846 */
1847
1848 private $pms = array();
1849 const MENTOR_EXPERTISE = 1;
1850 const MENTOR_COUNTRY = 2;
1851 const MENTOR_SECTOR = 3;
1852
1853 public function addMentorFilter($type)
1854 {
1855 switch($type) {
1856 case MENTOR_EXPERTISE:
1857 $pms['pme'] = 'profile_mentor';
1858 return 'pme';
1859 case MENTOR_COUNTRY:
1860 $pms['pmc'] = 'profile_mentor_country';
1861 return 'pmc';
1862 case MENTOR_SECTOR:
1863 $pms['pms'] = 'profile_mentor_sector';
1864 return 'pms';
1865 default:
1866 return;
1867 }
1868 }
1869
1870 private function mentorJoins()
1871 {
1872 $joins = array();
1873 foreach ($this->pms as $sub => $tab) {
1874 $joins[$sub] = array('left', $tab, '$ME.uid = $UID');
1875 }
1876 return $joins;
1877 }
1878
3f42a6ad
FB
1879 /** CONTACTS
1880 */
1881 private $cts = array();
1882 public function addContactFilter($uid = null)
1883 {
1884 return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid);
1885 }
1886
1887 private function contactJoins()
1888 {
1889 $joins = array();
1890 foreach ($this->cts as $sub=>$key) {
1891 if (is_null($key)) {
1892 $joins['c' . $sub] = array('left', 'contacts', '$ME.contact = $UID');
1893 } else {
1894 $joins['c' . $sub] = array('left', 'contacts', XDB::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
1895 }
1896 }
1897 return $joins;
1898 }
4e7bf1e0
FB
1899
1900
1901 /** CARNET
1902 */
1903 private $wn = array();
1904 public function addWatchRegistrationFilter($uid = null)
1905 {
1906 return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid);
1907 }
1908
1909 private $wp = array();
1910 public function addWatchPromoFilter($uid = null)
1911 {
1912 return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid);
1913 }
1914
1915 private $w = array();
1916 public function addWatchFilter($uid = null)
1917 {
1918 return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid);
1919 }
1920
1921 private function watchJoins()
1922 {
1923 $joins = array();
1924 foreach ($this->w as $sub=>$key) {
1925 if (is_null($key)) {
1926 $joins['w' . $sub] = array('left', 'watch');
1927 } else {
1928 $joins['w' . $sub] = array('left', 'watch', XDB::format('$ME.uid = {?}', substr($key, 5)));
1929 }
1930 }
1931 foreach ($this->wn as $sub=>$key) {
1932 if (is_null($key)) {
1933 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1934 } else {
1935 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1936 }
1937 }
1938 foreach ($this->wn as $sub=>$key) {
1939 if (is_null($key)) {
1940 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1941 } else {
1942 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1943 }
1944 }
1945 foreach ($this->wp as $sub=>$key) {
1946 if (is_null($key)) {
1947 $joins['wp' . $sub] = array('left', 'watch_promo');
1948 } else {
1949 $joins['wp' . $sub] = array('left', 'watch_promo', XDB::format('$ME.uid = {?}', substr($key, 5)));
1950 }
1951 }
1952 return $joins;
1953 }
a087cc8d 1954}
8363588b 1955// }}}
3f42a6ad 1956
a087cc8d
FB
1957// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1958?>