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