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