Switch to core-account
[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();
1204 $this->query = 'FROM accounts AS a
eb1449b8
FB
1205 LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
1206 LEFT JOIN profiles AS p ON (p.pid = ap.pid)
784745ce
FB
1207 ' . $joins . '
1208 WHERE (' . $where . ')';
1209 }
1210 }
1211
7ca75030 1212 private function getUIDList($uids = null, PlLimit &$limit)
d865c296
FB
1213 {
1214 $this->buildQuery();
7ca75030 1215 $lim = $limit->getSql();
d865c296
FB
1216 $cond = '';
1217 if (!is_null($uids)) {
07eb5b0e 1218 $cond = ' AND a.uid IN ' . XDB::formatArray($uids);
d865c296
FB
1219 }
1220 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1221 ' . $this->query . $cond . '
1222 GROUP BY a.uid
1223 ' . $this->orderby . '
7ca75030 1224 ' . $lim);
d865c296
FB
1225 $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
1226 return $fetched;
1227 }
1228
a087cc8d
FB
1229 /** Check that the user match the given rule.
1230 */
1231 public function checkUser(PlUser &$user)
1232 {
784745ce
FB
1233 $this->buildQuery();
1234 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1235 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
1236 return $count == 1;
a087cc8d
FB
1237 }
1238
1239 /** Filter a list of user to extract the users matching the rule.
1240 */
7ca75030 1241 public function filter(array $users, PlLimit &$limit)
a087cc8d 1242 {
4927ee54
FB
1243 $this->buildQuery();
1244 $table = array();
1245 $uids = array();
1246 foreach ($users as $user) {
07eb5b0e
FB
1247 if ($user instanceof PlUser) {
1248 $uid = $user->id();
1249 } else {
1250 $uid = $user;
1251 }
1252 $uids[] = $uid;
1253 $table[$uid] = $user;
4927ee54 1254 }
7ca75030 1255 $fetched = $this->getUIDList($uids, $limit);
a087cc8d 1256 $output = array();
4927ee54
FB
1257 foreach ($fetched as $uid) {
1258 $output[] = $table[$uid];
a087cc8d
FB
1259 }
1260 return $output;
1261 }
1262
7ca75030
RB
1263 public function getUIDs(PlLimit &$limit)
1264 {
1265 return $this->getUIDList(null, $limit);
1266 }
1267
1268 public function getUsers(PlLimit &$limit)
4927ee54 1269 {
7ca75030 1270 return User::getBulkUsersWithUIDs($this->getUIDs($limit));
d865c296
FB
1271 }
1272
7ca75030 1273 public function get(PlLimit &$limit)
d865c296 1274 {
7ca75030 1275 return $this->getUsers($limit);
4927ee54
FB
1276 }
1277
d865c296 1278 public function getTotalCount()
4927ee54 1279 {
38c6fe96 1280 if (is_null($this->lastcount)) {
aa21c568 1281 $this->buildQuery();
7e735012
FB
1282 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
1283 ' . $this->query);
38c6fe96
FB
1284 } else {
1285 return $this->lastcount;
1286 }
4927ee54
FB
1287 }
1288
9b8e5fb4 1289 public function setCondition(PlFilterCondition &$cond)
a087cc8d
FB
1290 {
1291 $this->root =& $cond;
784745ce 1292 $this->query = null;
a087cc8d
FB
1293 }
1294
9b8e5fb4 1295 public function addSort(PlFilterOrder &$sort)
24e08e33 1296 {
d865c296
FB
1297 $this->sort[] = $sort;
1298 $this->orderby = null;
24e08e33
FB
1299 }
1300
a087cc8d
FB
1301 static public function getLegacy($promo_min, $promo_max)
1302 {
a087cc8d 1303 if ($promo_min != 0) {
784745ce 1304 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
5dd9d823
FB
1305 } else {
1306 $min = new UFC_True();
a087cc8d 1307 }
a087cc8d 1308 if ($promo_max != 0) {
784745ce 1309 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
a087cc8d 1310 } else {
5dd9d823 1311 $max = new UFC_True();
a087cc8d 1312 }
9b8e5fb4 1313 return new UserFilter(new PFC_And($min, $max));
a087cc8d 1314 }
784745ce 1315
07eb5b0e
FB
1316 static public function sortByName()
1317 {
1318 return array(new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
1319 }
1320
1321 static public function sortByPromo()
1322 {
1323 return array(new UFO_Promo(), new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
1324 }
1325
aa21c568
FB
1326 static private function getDBSuffix($string)
1327 {
1328 return preg_replace('/[^a-z0-9]/i', '', $string);
1329 }
1330
1331
2d83cac9
RB
1332 /** Stores a new (and unique) table alias in the &$table table
1333 * @param &$table Array in which the table alias must be stored
1334 * @param $val Value which will then be used to build the join
1335 * @return Name of the newly created alias
1336 */
aa21c568
FB
1337 private $option = 0;
1338 private function register_optional(array &$table, $val)
1339 {
1340 if (is_null($val)) {
1341 $sub = $this->option++;
1342 $index = null;
1343 } else {
1344 $sub = self::getDBSuffix($val);
1345 $index = $val;
1346 }
1347 $sub = '_' . $sub;
1348 $table[$sub] = $index;
1349 return $sub;
1350 }
784745ce 1351
d865c296
FB
1352 /** DISPLAY
1353 */
38c6fe96 1354 const DISPLAY = 'display';
d865c296
FB
1355 private $pd = false;
1356 public function addDisplayFilter()
1357 {
1358 $this->pd = true;
1359 return '';
1360 }
1361
9b8e5fb4 1362 protected function displayJoins()
d865c296
FB
1363 {
1364 if ($this->pd) {
7ca75030 1365 return array('pd' => new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_display', '$ME.pid = $PID'));
d865c296
FB
1366 } else {
1367 return array();
1368 }
1369 }
1370
784745ce
FB
1371 /** NAMES
1372 */
d865c296 1373 /* name tokens */
784745ce
FB
1374 const LASTNAME = 'lastname';
1375 const FIRSTNAME = 'firstname';
1376 const NICKNAME = 'nickname';
1377 const PSEUDONYM = 'pseudonym';
1378 const NAME = 'name';
d865c296 1379 /* name variants */
784745ce
FB
1380 const VN_MARITAL = 'marital';
1381 const VN_ORDINARY = 'ordinary';
1382 const VN_OTHER = 'other';
1383 const VN_INI = 'ini';
d865c296
FB
1384 /* display names */
1385 const DN_FULL = 'directory_name';
1386 const DN_DISPLAY = 'yourself';
1387 const DN_YOURSELF = 'yourself';
1388 const DN_DIRECTORY = 'directory_name';
1389 const DN_PRIVATE = 'private_name';
1390 const DN_PUBLIC = 'public_name';
1391 const DN_SHORT = 'short_name';
1392 const DN_SORT = 'sort_name';
784745ce
FB
1393
1394 static public $name_variants = array(
1395 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
d865c296
FB
1396 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
1397 );
784745ce
FB
1398
1399 static public function assertName($name)
1400 {
1401 if (!Profile::getNameTypeId($name)) {
9b8e5fb4 1402 Platal::page()->kill('Invalid name type: ' . $name);
784745ce
FB
1403 }
1404 }
1405
d865c296
FB
1406 static public function isDisplayName($name)
1407 {
1408 return $name == self::DN_FULL || $name == self::DN_DISPLAY
1409 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
1410 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
1411 || $name == self::DN_SHORT || $name == self::DN_SORT;
1412 }
1413
784745ce 1414 private $pn = array();
784745ce
FB
1415 public function addNameFilter($type, $variant = null)
1416 {
1417 if (!is_null($variant)) {
1418 $ft = $type . '_' . $variant;
1419 } else {
1420 $ft = $type;
1421 }
1422 $sub = '_' . $ft;
1423 self::assertName($ft);
1424
1425 if (!is_null($variant) && $variant == 'other') {
aa21c568 1426 $sub .= $this->option++;
784745ce
FB
1427 }
1428 $this->pn[$sub] = Profile::getNameTypeId($ft);
1429 return $sub;
1430 }
1431
9b8e5fb4 1432 protected function nameJoins()
784745ce
FB
1433 {
1434 $joins = array();
1435 foreach ($this->pn as $sub => $type) {
7ca75030 1436 $joins['pn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
784745ce
FB
1437 }
1438 return $joins;
1439 }
1440
1441
1442 /** EDUCATION
1443 */
1444 const GRADE_ING = 'Ing.';
1445 const GRADE_PHD = 'PhD';
1446 const GRADE_MST = 'M%';
1447 static public function isGrade($grade)
1448 {
38c6fe96 1449 return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST;
784745ce
FB
1450 }
1451
1452 static public function assertGrade($grade)
1453 {
1454 if (!self::isGrade($grade)) {
1455 Platal::page()->killError("Diplôme non valide");
1456 }
1457 }
1458
d865c296
FB
1459 static public function promoYear($grade)
1460 {
1461 // XXX: Definition of promotion for phds and masters might change in near future.
1462 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
1463 }
1464
784745ce
FB
1465 private $pepe = array();
1466 private $with_pee = false;
784745ce
FB
1467 public function addEducationFilter($x = false, $grade = null)
1468 {
1469 if (!$x) {
aa21c568
FB
1470 $index = $this->option;
1471 $sub = $this->option++;
784745ce
FB
1472 } else {
1473 self::assertGrade($grade);
1474 $index = $grade;
1475 $sub = $grade[0];
1476 $this->with_pee = true;
1477 }
1478 $sub = '_' . $sub;
1479 $this->pepe[$index] = $sub;
1480 return $sub;
1481 }
1482
9b8e5fb4 1483 protected function educationJoins()
784745ce
FB
1484 {
1485 $joins = array();
1486 if ($this->with_pee) {
7ca75030 1487 $joins['pee'] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_enum', 'pee.abbreviation = \'X\'');
784745ce
FB
1488 }
1489 foreach ($this->pepe as $grade => $sub) {
1490 if ($this->isGrade($grade)) {
7ca75030
RB
1491 $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
1492 $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
784745ce
FB
1493 XDB::format('{?}', $grade));
1494 } else {
7ca75030
RB
1495 $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_education', '$ME.uid = $PID');
1496 $joins['pee' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
1497 $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
784745ce
FB
1498 }
1499 }
1500 return $joins;
1501 }
4927ee54
FB
1502
1503
1504 /** GROUPS
1505 */
1506 private $gpm = array();
4927ee54
FB
1507 public function addGroupFilter($group = null)
1508 {
1509 if (!is_null($group)) {
1510 if (ctype_digit($group)) {
1511 $index = $sub = $group;
1512 } else {
1513 $index = $group;
aa21c568 1514 $sub = self::getDBSuffix($group);
4927ee54
FB
1515 }
1516 } else {
aa21c568 1517 $sub = 'group_' . $this->option++;
4927ee54
FB
1518 $index = null;
1519 }
1520 $sub = '_' . $sub;
1521 $this->gpm[$sub] = $index;
1522 return $sub;
1523 }
1524
9b8e5fb4 1525 protected function groupJoins()
4927ee54
FB
1526 {
1527 $joins = array();
1528 foreach ($this->gpm as $sub => $key) {
1529 if (is_null($key)) {
7ca75030
RB
1530 $joins['gpa' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'groupex.asso');
1531 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
4927ee54 1532 } else if (ctype_digit($key)) {
7ca75030 1533 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
4927ee54 1534 } else {
7ca75030
RB
1535 $joins['gpa' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key));
1536 $joins['gpm' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
4927ee54
FB
1537 }
1538 }
1539 return $joins;
1540 }
aa21c568
FB
1541
1542 /** EMAILS
1543 */
1544 private $e = array();
1545 public function addEmailRedirectFilter($email = null)
1546 {
1547 return $this->register_optional($this->e, $email);
1548 }
1549
1550 private $ve = array();
1551 public function addVirtualEmailFilter($email = null)
1552 {
21401768 1553 $this->addAliasFilter(self::ALIAS_FORLIFE);
aa21c568
FB
1554 return $this->register_optional($this->ve, $email);
1555 }
1556
21401768
FB
1557 const ALIAS_BEST = 'bestalias';
1558 const ALIAS_FORLIFE = 'forlife';
aa21c568
FB
1559 private $al = array();
1560 public function addAliasFilter($alias = null)
1561 {
1562 return $this->register_optional($this->al, $alias);
1563 }
1564
9b8e5fb4 1565 protected function emailJoins()
aa21c568
FB
1566 {
1567 global $globals;
1568 $joins = array();
1569 foreach ($this->e as $sub=>$key) {
1570 if (is_null($key)) {
7ca75030 1571 $joins['e' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
aa21c568 1572 } else {
7ca75030 1573 $joins['e' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
aa21c568
FB
1574 }
1575 }
21401768 1576 foreach ($this->al as $sub=>$key) {
aa21c568 1577 if (is_null($key)) {
7ca75030 1578 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
21401768 1579 } else if ($key == self::ALIAS_BEST) {
7ca75030 1580 $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 1581 } else if ($key == self::ALIAS_FORLIFE) {
7ca75030 1582 $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
aa21c568 1583 } else {
7ca75030 1584 $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 1585 }
aa21c568 1586 }
21401768 1587 foreach ($this->ve as $sub=>$key) {
aa21c568 1588 if (is_null($key)) {
7ca75030 1589 $joins['v' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'virtual', '$ME.type = \'user\'');
aa21c568 1590 } else {
7ca75030 1591 $joins['v' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
aa21c568 1592 }
7ca75030 1593 $joins['vr' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid
21401768
FB
1594 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
1595 CONCAT(al_forlife.alias, \'@\', {?}),
1596 a.email))',
1597 $globals->mail->domain, $globals->mail->domain2));
aa21c568
FB
1598 }
1599 return $joins;
1600 }
3f42a6ad
FB
1601
1602
c4b24511
RB
1603 /** ADDRESSES
1604 */
036d1637 1605 private $with_pa = false;
c4b24511
RB
1606 public function addAddressFilter()
1607 {
036d1637
RB
1608 $this->with_pa = true;
1609 return 'pa';
c4b24511
RB
1610 }
1611
9b8e5fb4 1612 protected function addressJoins()
c4b24511
RB
1613 {
1614 $joins = array();
036d1637 1615 if ($this->with_pa) {
7ca75030 1616 $joins['pa'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_address', '$ME.pid = $PID');
c4b24511
RB
1617 }
1618 return $joins;
1619 }
1620
1621
4083b126
RB
1622 /** CORPS
1623 */
1624
1625 private $pc = false;
1626 private $pce = array();
1627 private $pcr = false;
1628 public function addCorpsFilter($type)
1629 {
1630 $this->pc = true;
1631 if ($type == UFC_Corps::CURRENT) {
1632 $pce['pcec'] = 'current_corpsid';
1633 return 'pcec';
1634 } else if ($type == UFC_Corps::ORIGIN) {
1635 $pce['pceo'] = 'original_corpsid';
1636 return 'pceo';
1637 }
1638 }
1639
1640 public function addCorpsRankFilter()
1641 {
1642 $this->pc = true;
1643 $this->pcr = true;
1644 return 'pcr';
1645 }
1646
9b8e5fb4 1647 protected function corpsJoins()
4083b126
RB
1648 {
1649 $joins = array();
1650 if ($this->pc) {
7ca75030 1651 $joins['pc'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps', '$ME.uid = $UID');
4083b126
RB
1652 }
1653 if ($this->pcr) {
7ca75030 1654 $joins['pcr'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps_rank_enum', '$ME.id = pc.rankid');
4083b126
RB
1655 }
1656 foreach($this->pce as $sub => $field) {
7ca75030 1657 $joins[$sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps_enum', '$ME.id = pc.' . $field);
4083b126
RB
1658 }
1659 return $joins;
1660 }
1661
6a99c3ac
RB
1662 /** JOBS
1663 */
1664
1665 const JOB_SECTOR = 1;
1666 const JOB_SUBSECTOR = 2;
1667 const JOB_SUBSUBSECTOR = 4;
1668 const JOB_ALTERNATES = 8;
1669 const JOB_USERDEFINED = 16;
1670
1671 /** Joins :
1672 * pj => profile_job
1673 * pje => profile_job_enum
1674 * pjse => profile_job_sector_enum
1675 * pjsse => profile_job_subsector_enum
1676 * pjssse => profile_job_subsubsector_enum
1677 * pja => profile_job_alternates
1678 */
1679 private $with_pj = false;
1680 private $with_pje = false;
1681 private $with_pjse = false;
1682 private $with_pjsse = false;
1683 private $with_pjssse = false;
1684 private $with_pja = false;
1685
1686 public function addJobFilter()
1687 {
1688 $this->with_pj = true;
1689 return 'pj';
1690 }
1691
1692 public function addJobCompanyFilter()
1693 {
1694 $this->addJobFilter();
1695 $this->with_pje = true;
1696 return 'pje';
1697 }
1698
1699 public function addJobSectorizationFilter($type)
1700 {
1701 $this->addJobFilter();
1702 if ($type == self::JOB_SECTOR) {
1703 $this->with_pjse = true;
1704 return 'pjse';
1705 } else if ($type == self::JOB_SUBSECTOR) {
1706 $this->with_pjsse = true;
1707 return 'pjsse';
1708 } else if ($type == self::JOB_SUBSUBSECTOR) {
1709 $this->with_pjssse = true;
1710 return 'pjssse';
1711 } else if ($type == self::JOB_ALTERNATES) {
1712 $this->with_pja = true;
1713 return 'pja';
1714 }
1715 }
1716
9b8e5fb4 1717 protected function jobJoins()
6a99c3ac
RB
1718 {
1719 $joins = array();
1720 if ($this->with_pj) {
7ca75030 1721 $joins['pj'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job', '$ME.uid = $UID');
6a99c3ac
RB
1722 }
1723 if ($this->with_pje) {
7ca75030 1724 $joins['pje'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_enum', '$ME.id = pj.jobid');
6a99c3ac
RB
1725 }
1726 if ($this->with_pjse) {
7ca75030 1727 $joins['pjse'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_sector_enum', '$ME.id = pj.sectorid');
6a99c3ac
RB
1728 }
1729 if ($this->with_pjsse) {
7ca75030 1730 $joins['pjsse'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_subsector_enum', '$ME.id = pj.subsectorid');
6a99c3ac
RB
1731 }
1732 if ($this->with_pjssse) {
7ca75030 1733 $joins['pjssse'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
6a99c3ac
RB
1734 }
1735 if ($this->with_pja) {
7ca75030 1736 $joins['pja'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
6a99c3ac
RB
1737 }
1738 return $joins;
1739 }
1740
0a2e9c74
RB
1741 /** NETWORKING
1742 */
1743
1744 private $with_pnw = false;
1745 public function addNetworkingFilter()
1746 {
1747 $this->with_pnw = true;
1748 return 'pnw';
1749 }
1750
9b8e5fb4 1751 protected function networkingJoins()
0a2e9c74
RB
1752 {
1753 $joins = array();
1754 if ($this->with_pnw) {
7ca75030 1755 $joins['pnw'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_networking', '$ME.uid = $UID');
0a2e9c74
RB
1756 }
1757 return $joins;
1758 }
1759
6d62969e
RB
1760 /** PHONE
1761 */
1762
2d83cac9 1763 private $with_ptel = false;
6d62969e
RB
1764
1765 public function addPhoneFilter()
1766 {
2d83cac9 1767 $this->with_ptel = true;
6d62969e
RB
1768 return 'ptel';
1769 }
1770
9b8e5fb4 1771 protected function phoneJoins()
6d62969e
RB
1772 {
1773 $joins = array();
2d83cac9 1774 if ($this->with_ptel) {
9b8e5fb4 1775 $joins['ptel'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_phones', '$ME.uid = $UID');
6d62969e
RB
1776 }
1777 return $joins;
1778 }
1779
ceb512d2
RB
1780 /** MEDALS
1781 */
1782
2d83cac9 1783 private $with_pmed = false;
ceb512d2
RB
1784 public function addMedalFilter()
1785 {
2d83cac9 1786 $this->with_pmed = true;
ceb512d2
RB
1787 return 'pmed';
1788 }
1789
9b8e5fb4 1790 protected function medalJoins()
ceb512d2
RB
1791 {
1792 $joins = array();
2d83cac9 1793 if ($this->with_pmed) {
7ca75030 1794 $joins['pmed'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_medals_sub', '$ME.uid = $UID');
ceb512d2
RB
1795 }
1796 return $joins;
1797 }
1798
671b7073
RB
1799 /** MENTORING
1800 */
1801
1802 private $pms = array();
1803 const MENTOR_EXPERTISE = 1;
1804 const MENTOR_COUNTRY = 2;
1805 const MENTOR_SECTOR = 3;
1806
1807 public function addMentorFilter($type)
1808 {
1809 switch($type) {
1810 case MENTOR_EXPERTISE:
1811 $pms['pme'] = 'profile_mentor';
1812 return 'pme';
1813 case MENTOR_COUNTRY:
1814 $pms['pmc'] = 'profile_mentor_country';
1815 return 'pmc';
1816 case MENTOR_SECTOR:
1817 $pms['pms'] = 'profile_mentor_sector';
1818 return 'pms';
1819 default:
5d2e55c7 1820 Platal::page()->killError("Undefined mentor filter.");
671b7073
RB
1821 }
1822 }
1823
9b8e5fb4 1824 protected function mentorJoins()
671b7073
RB
1825 {
1826 $joins = array();
1827 foreach ($this->pms as $sub => $tab) {
7ca75030 1828 $joins[$sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, $tab, '$ME.uid = $UID');
671b7073
RB
1829 }
1830 return $joins;
1831 }
1832
3f42a6ad
FB
1833 /** CONTACTS
1834 */
1835 private $cts = array();
1836 public function addContactFilter($uid = null)
1837 {
1838 return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid);
1839 }
1840
9b8e5fb4 1841 protected function contactJoins()
3f42a6ad
FB
1842 {
1843 $joins = array();
1844 foreach ($this->cts as $sub=>$key) {
1845 if (is_null($key)) {
7ca75030 1846 $joins['c' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'contacts', '$ME.contact = $UID');
3f42a6ad 1847 } else {
7ca75030 1848 $joins['c' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'contacts', XDB::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
3f42a6ad
FB
1849 }
1850 }
1851 return $joins;
1852 }
4e7bf1e0
FB
1853
1854
1855 /** CARNET
1856 */
1857 private $wn = array();
1858 public function addWatchRegistrationFilter($uid = null)
1859 {
1860 return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid);
1861 }
1862
1863 private $wp = array();
1864 public function addWatchPromoFilter($uid = null)
1865 {
1866 return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid);
1867 }
1868
1869 private $w = array();
1870 public function addWatchFilter($uid = null)
1871 {
1872 return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid);
1873 }
1874
9b8e5fb4 1875 protected function watchJoins()
4e7bf1e0
FB
1876 {
1877 $joins = array();
1878 foreach ($this->w as $sub=>$key) {
1879 if (is_null($key)) {
7ca75030 1880 $joins['w' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch');
4e7bf1e0 1881 } else {
7ca75030 1882 $joins['w' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch', XDB::format('$ME.uid = {?}', substr($key, 5)));
4e7bf1e0
FB
1883 }
1884 }
1885 foreach ($this->wn as $sub=>$key) {
1886 if (is_null($key)) {
7ca75030 1887 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', '$ME.ni_id = $UID');
4e7bf1e0 1888 } else {
7ca75030 1889 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
4e7bf1e0
FB
1890 }
1891 }
1892 foreach ($this->wn as $sub=>$key) {
1893 if (is_null($key)) {
7ca75030 1894 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', '$ME.ni_id = $UID');
4e7bf1e0 1895 } else {
7ca75030 1896 $joins['wn' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
4e7bf1e0
FB
1897 }
1898 }
1899 foreach ($this->wp as $sub=>$key) {
1900 if (is_null($key)) {
7ca75030 1901 $joins['wp' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_promo');
4e7bf1e0 1902 } else {
7ca75030 1903 $joins['wp' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'watch_promo', XDB::format('$ME.uid = {?}', substr($key, 5)));
4e7bf1e0
FB
1904 }
1905 }
1906 return $joins;
1907 }
a087cc8d 1908}
8363588b 1909// }}}
3f42a6ad 1910
a087cc8d
FB
1911// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1912?>