Add folding to userfilter.php
[platal.git] / classes / userfilter.php
CommitLineData
a087cc8d
FB
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
d865c296
FB
22
23/******************
24 * CONDITIONS
25 ******************/
26
8363588b 27// {{{ interface UserFilterCondition
a087cc8d
FB
28interface UserFilterCondition
29{
5dd9d823
FB
30 const COND_TRUE = 'TRUE';
31 const COND_FALSE = 'FALSE';
32
a087cc8d
FB
33 /** Check that the given user matches the rule.
34 */
784745ce 35 public function buildCondition(UserFilter &$uf);
a087cc8d 36}
8363588b 37// }}}
a087cc8d 38
8363588b 39// {{{ class UFC_OneChild
a087cc8d
FB
40abstract class UFC_OneChild implements UserFilterCondition
41{
42 protected $child;
43
5dd9d823
FB
44 public function __construct($child = null)
45 {
46 if (!is_null($child) && ($child instanceof UserFilterCondition)) {
47 $this->setChild($child);
48 }
49 }
50
a087cc8d
FB
51 public function setChild(UserFilterCondition &$cond)
52 {
53 $this->child =& $cond;
54 }
55}
8363588b 56// }}}
a087cc8d 57
8363588b 58// {{{ class UFC_NChildren
a087cc8d
FB
59abstract class UFC_NChildren implements UserFilterCondition
60{
61 protected $children = array();
62
5dd9d823
FB
63 public function __construct()
64 {
65 $children = func_get_args();
66 foreach ($children as &$child) {
67 if (!is_null($child) && ($child instanceof UserFilterCondition)) {
68 $this->addChild($child);
69 }
70 }
71 }
72
a087cc8d
FB
73 public function addChild(UserFilterCondition &$cond)
74 {
75 $this->children[] =& $cond;
76 }
5dd9d823
FB
77
78 protected function catConds(array $cond, $op, $fallback)
79 {
80 if (count($cond) == 0) {
81 return $fallback;
82 } else if (count($cond) == 1) {
83 return $cond[0];
84 } else {
4927ee54 85 return '(' . implode(') ' . $op . ' (', $cond) . ')';
5dd9d823
FB
86 }
87 }
a087cc8d 88}
8363588b 89// }}}
a087cc8d 90
8363588b 91// {{{ class UFC_True
a087cc8d
FB
92class UFC_True implements UserFilterCondition
93{
784745ce 94 public function buildCondition(UserFilter &$uf)
a087cc8d 95 {
5dd9d823 96 return self::COND_TRUE;
a087cc8d
FB
97 }
98}
8363588b 99// }}}
a087cc8d 100
8363588b 101// {{{ class UFC_False
a087cc8d
FB
102class UFC_False implements UserFilterCondition
103{
784745ce 104 public function buildCondition(UserFilter &$uf)
a087cc8d 105 {
5dd9d823 106 return self::COND_FALSE;
a087cc8d
FB
107 }
108}
8363588b 109// }}}
a087cc8d 110
8363588b 111// {{{ class UFC_Not
a087cc8d
FB
112class UFC_Not extends UFC_OneChild
113{
784745ce 114 public function buildCondition(UserFilter &$uf)
a087cc8d 115 {
5dd9d823
FB
116 $val = $this->child->buildCondition($uf);
117 if ($val == self::COND_TRUE) {
118 return self::COND_FALSE;
119 } else if ($val == self::COND_FALSE) {
120 return self::COND_TRUE;
121 } else {
122 return 'NOT (' . $val . ')';
123 }
a087cc8d
FB
124 }
125}
8363588b 126// }}}
a087cc8d 127
8363588b 128// {{{ class UFC_And
a087cc8d
FB
129class UFC_And extends UFC_NChildren
130{
784745ce 131 public function buildCondition(UserFilter &$uf)
a087cc8d 132 {
784745ce 133 if (empty($this->children)) {
5dd9d823 134 return self::COND_FALSE;
784745ce 135 } else {
5dd9d823 136 $true = self::COND_FALSE;
784745ce
FB
137 $conds = array();
138 foreach ($this->children as &$child) {
5dd9d823
FB
139 $val = $child->buildCondition($uf);
140 if ($val == self::COND_TRUE) {
141 $true = self::COND_TRUE;
142 } else if ($val == self::COND_FALSE) {
143 return self::COND_FALSE;
144 } else {
145 $conds[] = $val;
146 }
a087cc8d 147 }
5dd9d823 148 return $this->catConds($conds, 'AND', $true);
a087cc8d 149 }
a087cc8d
FB
150 }
151}
8363588b 152// }}}
a087cc8d 153
8363588b 154// {{{ class UFC_Or
a087cc8d
FB
155class UFC_Or extends UFC_NChildren
156{
784745ce 157 public function buildCondition(UserFilter &$uf)
a087cc8d 158 {
784745ce 159 if (empty($this->children)) {
5dd9d823 160 return self::COND_TRUE;
784745ce 161 } else {
5dd9d823 162 $true = self::COND_TRUE;
784745ce
FB
163 $conds = array();
164 foreach ($this->children as &$child) {
5dd9d823
FB
165 $val = $child->buildCondition($uf);
166 if ($val == self::COND_TRUE) {
167 return self::COND_TRUE;
168 } else if ($val == self::COND_FALSE) {
169 $true = self::COND_FALSE;
170 } else {
171 $conds[] = $val;
172 }
a087cc8d 173 }
5dd9d823 174 return $this->catConds($conds, 'OR', $true);
a087cc8d 175 }
a087cc8d
FB
176 }
177}
8363588b 178// }}}
a087cc8d 179
8363588b 180// {{{ class UFC_Profile
53eae167
RB
181/** Filters users who have a profile
182 */
eb1449b8
FB
183class UFC_Profile implements UserFilterCondition
184{
185 public function buildCondition(UserFilter &$uf)
186 {
187 return '$PID IS NOT NULL';
188 }
189}
8363588b 190// }}}
eb1449b8 191
8363588b 192// {{{ class UFC_Promo
53eae167
RB
193/** Filters users based on promo
194 * @param $comparison Comparison operator (>, =, ...)
195 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
196 * @param $promo Promo on which the filter is based
197 */
a087cc8d
FB
198class UFC_Promo implements UserFilterCondition
199{
a087cc8d
FB
200
201 private $grade;
202 private $promo;
203 private $comparison;
204
205 public function __construct($comparison, $grade, $promo)
206 {
207 $this->grade = $grade;
208 $this->comparison = $comparison;
209 $this->promo = $promo;
38c6fe96
FB
210 if ($this->grade != UserFilter::DISPLAY) {
211 UserFilter::assertGrade($this->grade);
212 }
a087cc8d
FB
213 }
214
784745ce 215 public function buildCondition(UserFilter &$uf)
a087cc8d 216 {
38c6fe96
FB
217 if ($this->grade == UserFilter::DISPLAY) {
218 $sub = $uf->addDisplayFilter();
219 return XDB::format('pd' . $sub . '.promo = {?}', $this->promo);
220 } else {
221 $sub = $uf->addEducationFilter(true, $this->grade);
222 $field = 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
223 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison . ' ' . XDB::format('{?}', $this->promo);
224 }
784745ce
FB
225 }
226}
8363588b 227// }}}
784745ce 228
8363588b 229// {{{ class UFC_Name
53eae167
RB
230/** Filters users based on name
231 * @param $type Type of name field on which filtering is done (firstname, lastname, ...)
232 * @param $text Text on which to filter
233 * @param $mode flag indicating search type (prefix, suffix, with particule, ...)
234 */
784745ce
FB
235class UFC_Name implements UserFilterCondition
236{
237 const PREFIX = 1;
238 const SUFFIX = 2;
239 const PARTICLE = 7;
240 const VARIANTS = 8;
241 const CONTAINS = 3;
242
243 private $type;
244 private $text;
245 private $mode;
246
247 public function __construct($type, $text, $mode)
248 {
249 $this->type = $type;
250 $this->text = $text;
251 $this->mode = $mode;
252 }
253
254 private function buildNameQuery($type, $variant, $where, UserFilter &$uf)
255 {
256 $sub = $uf->addNameFilter($type, $variant);
257 return str_replace('$ME', 'pn' . $sub, $where);
258 }
259
260 public function buildCondition(UserFilter &$uf)
261 {
262 $left = '$ME.name';
263 $op = ' LIKE ';
264 if (($this->mode & self::PARTICLE) == self::PARTICLE) {
265 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
266 }
267 if (($this->mode & self::CONTAINS) == 0) {
268 $right = XDB::format('{?}', $this->text);
269 $op = ' = ';
270 } else if (($this->mode & self::CONTAINS) == self::PREFIX) {
271 $right = XDB::format('CONCAT({?}, \'%\')', $this->text);
272 } else if (($this->mode & self::CONTAINS) == self::SUFFIX) {
273 $right = XDB::format('CONCAT(\'%\', {?})', $this->text);
274 } else {
275 $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text);
276 }
277 $cond = $left . $op . $right;
278 $conds = array($this->buildNameQuery($this->type, null, $cond, $uf));
d865c296 279 if (($this->mode & self::VARIANTS) != 0 && isset(UserFilter::$name_variants[$this->type])) {
784745ce
FB
280 foreach (UserFilter::$name_variants[$this->type] as $var) {
281 $conds[] = $this->buildNameQuery($this->type, $var, $cond, $uf);
282 }
283 }
284 return implode(' OR ', $conds);
a087cc8d
FB
285 }
286}
8363588b 287// }}}
a087cc8d 288
8363588b 289// {{{ class UFC_Dead
53eae167
RB
290/** Filters users based on death date
291 * @param $comparison Comparison operator
292 * @param $date Date to which death date should be compared
293 */
4927ee54
FB
294class UFC_Dead implements UserFilterCondition
295{
38c6fe96
FB
296 private $comparison;
297 private $date;
298
299 public function __construct($comparison = null, $date = null)
4927ee54 300 {
38c6fe96
FB
301 $this->comparison = $comparison;
302 $this->date = $date;
4927ee54
FB
303 }
304
305 public function buildCondition(UserFilter &$uf)
306 {
38c6fe96
FB
307 $str = 'p.deathdate IS NOT NULL';
308 if (!is_null($this->comparison)) {
309 $str .= ' AND p.deathdate ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date));
4927ee54 310 }
38c6fe96 311 return $str;
4927ee54
FB
312 }
313}
8363588b 314// }}}
4927ee54 315
8363588b 316// {{{ class UFC_Registered
53eae167
RB
317/** Filters users based on registration state
318 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
319 * @param $comparison Comparison operator
320 * @param $date Date to which users registration date should be compared
321 */
4927ee54
FB
322class UFC_Registered implements UserFilterCondition
323{
324 private $active;
38c6fe96
FB
325 private $comparison;
326 private $date;
327
328 public function __construct($active = false, $comparison = null, $date = null)
4927ee54
FB
329 {
330 $this->only_active = $active;
38c6fe96
FB
331 $this->comparison = $comparison;
332 $this->date = $date;
4927ee54
FB
333 }
334
335 public function buildCondition(UserFilter &$uf)
336 {
337 if ($this->active) {
38c6fe96 338 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
4927ee54 339 } else {
38c6fe96
FB
340 $date = 'a.uid IS NOT NULL AND a.state != \'pending\'';
341 }
342 if (!is_null($this->comparison)) {
343 $date .= ' AND a.registration_date ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date));
4927ee54 344 }
38c6fe96 345 return $date;
4927ee54
FB
346 }
347}
8363588b 348// }}}
4927ee54 349
8363588b 350// {{{ class UFC_ProfileUpdated
53eae167
RB
351/** Filters users based on profile update date
352 * @param $comparison Comparison operator
353 * @param $date Date to which profile update date must be compared
354 */
7e735012
FB
355class UFC_ProfileUpdated implements UserFilterCondition
356{
357 private $comparison;
358 private $date;
359
360 public function __construct($comparison = null, $date = null)
361 {
362 $this->comparison = $comparison;
363 $this->date = $date;
364 }
365
366 public function buildCondition(UserFilter &$uf)
367 {
368 return 'p.last_change ' . $this->comparison . XDB::format(' {?}', date('Y-m-d H:i:s', $this->date));
369 }
370}
8363588b 371// }}}
7e735012 372
8363588b 373// {{{ class UFC_Birthday
53eae167
RB
374/** Filters users based on next birthday date
375 * @param $comparison Comparison operator
376 * @param $date Date to which users next birthday date should be compared
377 */
7e735012
FB
378class UFC_Birthday implements UserFilterCondition
379{
380 private $comparison;
381 private $date;
382
383 public function __construct($comparison = null, $date = null)
384 {
385 $this->comparison = $comparison;
386 $this->date = $date;
387 }
388
389 public function buildCondition(UserFilter &$uf)
390 {
391 return 'p.next_birthday ' . $this->comparison . XDB::format(' {?}', date('Y-m-d', $this->date));
392 }
393}
8363588b 394// }}}
7e735012 395
8363588b 396// {{{ class UFC_Sex
53eae167
RB
397/** Filters users based on sex
398 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
399 */
4927ee54
FB
400class UFC_Sex implements UserFilterCondition
401{
402 private $sex;
403 public function __construct($sex)
404 {
405 $this->sex = $sex;
406 }
407
408 public function buildCondition(UserFilter &$uf)
409 {
410 if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) {
411 return self::COND_FALSE;
412 } else {
24e08e33 413 return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male');
4927ee54
FB
414 }
415 }
416}
8363588b 417// }}}
4927ee54 418
8363588b 419// {{{ class UFC_Group
53eae167
RB
420/** Filters users based on group membership
421 * @param $group Group whose member we are selecting
422 * @param $admin Whether to restrict selection to admins of that group
423 */
4927ee54
FB
424class UFC_Group implements UserFilterCondition
425{
426 private $group;
427 private $admin;
428 public function __construct($group, $admin = false)
429 {
430 $this->group = $group;
431 $this->admin = $admin;
432 }
433
434 public function buildCondition(UserFilter &$uf)
435 {
436 $sub = $uf->addGroupFilter($this->group);
437 $where = 'gpm' . $sub . '.perms IS NOT NULL';
438 if ($this->admin) {
439 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
440 }
441 return $where;
442 }
443}
8363588b 444// }}}
4927ee54 445
8363588b 446// {{{ class UFC_Email
53eae167
RB
447/** Filters users based on email address
448 * @param $email Email whose owner we are looking for
449 */
aa21c568
FB
450class UFC_Email implements UserFilterCondition
451{
452 private $email;
453 public function __construct($email)
454 {
455 $this->email = $email;
456 }
457
458 public function buildCondition(UserFilter &$uf)
459 {
460 if (User::isForeignEmailAddress($this->email)) {
461 $sub = $uf->addEmailRedirectFilter($this->email);
462 return XDB::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email);
21401768
FB
463 } else if (User::isVirtualEmailAddress($this->email)) {
464 $sub = $uf->addVirtualEmailFilter($this->email);
465 return 'vr' . $sub . '.redirect IS NOT NULL';
aa21c568 466 } else {
21401768
FB
467 @list($user, $domain) = explode('@', $this->email);
468 $sub = $uf->addAliasFilter($user);
aa21c568
FB
469 return 'al' . $sub . '.alias IS NOT NULL';
470 }
471 }
472}
8363588b 473// }}}
d865c296 474
8363588b 475// {{{ class UFC_EmailList
53eae167
RB
476/** Filters users base on an email list
477 * @param $emails List of emails whose owner must be selected
478 */
21401768
FB
479class UFC_EmailList implements UserFilterCondition
480{
481 private $emails;
482 public function __construct($emails)
483 {
484 $this->emails = $emails;
485 }
486
487 public function buildCondition(UserFilter &$uf)
488 {
489 $email = null;
490 $virtual = null;
491 $alias = null;
492 $cond = array();
493
494 if (count($this->emails) == 0) {
495 return UserFilterCondition::COND_TRUE;
496 }
497
498 foreach ($this->emails as $entry) {
499 if (User::isForeignEmailAddress($entry)) {
500 if (is_null($email)) {
501 $email = $uf->addEmailRedirectFilter();
502 }
503 $cond[] = XDB::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry);
504 } else if (User::isVirtualEmailAddress($entry)) {
505 if (is_null($virtual)) {
506 $virtual = $uf->addVirtualEmailFilter();
507 }
508 $cond[] = XDB::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry);
509 } else {
510 if (is_null($alias)) {
511 $alias = $uf->addAliasFilter();
512 }
513 @list($user, $domain) = explode('@', $entry);
514 $cond[] = XDB::format('al' . $alias . '.alias = {?}', $user);
515 }
516 }
517 return '(' . implode(') OR (', $cond) . ')';
518 }
519}
8363588b 520// }}}
d865c296 521
8363588b 522// {{{ class UFC_Address
c4b24511
RB
523/** Filters users based on their address
524 * @param $field Field of the address used for filtering (city, street, ...)
525 * @param $text Text for filter
526 * @param $mode Mode for search (PREFIX, SUFFIX, ...)
527 */
528class UFC_Address implements UserFilterCondition
529{
530 const PREFIX = 1;
531 const SUFFIX = 2;
532 const CONTAINS = 3;
533
534 private $field;
535 private $text;
536 private $mode;
537
538 public function __construct($field, $text, $mode)
539 {
540 $this->field = $field;
541 $this->text = $text;
542 $this->mode = $mode;
543 }
544
545 public function buildCondition(UserFilter &$uf)
546 {
547 $left = 'pa.' . $field;
548 $op = ' LIKE ';
549 if (($this->mode & self::CONTAINS) == 0) {
550 $right = XDB::format('{?}', $this->text);
551 $op = ' = ';
552 } else if (($this->mode & self::CONTAINS) == self::PREFIX) {
553 $right = XDB::format('CONCAT({?}, \'%\')', $this->text);
554 } else if (($this->mode & self::CONTAINS) == self::SUFFIX) {
555 $right = XDB::format('CONCAT(\'%\', {?})', $this->text);
556 } else {
557 $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text);
558 }
559 $cond = $left . $op . $right;
560 $uf->addAddressFilter();
561 return $cond;
562 }
563}
8363588b 564// }}}
c4b24511 565
8363588b 566// {{{ class UFC_Corps
4083b126
RB
567/** Filters users based on the corps they belong to
568 * @param $corps Corps we are looking for (abbreviation)
569 * @param $type Whether we search for original or current corps
570 */
571class UFC_Corps implements UserFilterCondition
572{
573 const CURRENT=1;
574 const ORIGIN=2;
575
576 private $corps;
577 private $type;
578
579 public function __construct($corps, $type = self::CURRENT)
580 {
581 $this->corps = $corps;
582 $this->type = $type;
583 }
584
585 public function buildCondition(UserFilter &$uf)
586 {
587 /** Tables shortcuts :
588 * pc for profile corps,
589 * pceo for profile_corps_enum - orginal
590 * pcec for profile_corps_enum - current
591 */
592 $sub = $uf->addCorpsFilter($this->type);
593 $cond = $sub . '.abbreviation = ' . $corps;
594 return $cond;
595 }
596}
8363588b 597// }}}
4083b126 598
8363588b 599// {{{ class UFC_Corps_Rank
4083b126
RB
600/** Filters users based on their rank in the corps
601 * @param $rank Rank we are looking for (abbreviation)
602 */
603class UFC_Corps_Rank implements UserFilterCondition
604{
605 private $rank;
606 public function __construct($rank)
607 {
608 $this->rank = $rank;
609 }
610
611 public function buildCondition(UserFilter &$uf)
612 {
613 /** Tables shortcuts :
614 * pcr for profile_corps_rank
615 */
616 $sub = $uf->addCorpsRankFilter();
617 $cond = $sub . '.abbreviation = ' . $rank;
618 return $cond;
619 }
620}
8363588b 621// }}}
4083b126 622
8363588b 623// {{{ class UFC_UserRelated
53eae167
RB
624/** Filters users based on a relation toward on user
625 * @param $user User to which searched users are related
626 */
4e7bf1e0 627abstract class UFC_UserRelated implements UserFilterCondition
3f42a6ad 628{
009b8ab7
FB
629 protected $user;
630 public function __construct(PlUser &$user)
631 {
632 $this->user =& $user;
3f42a6ad 633 }
4e7bf1e0 634}
8363588b 635// }}}
3f42a6ad 636
8363588b 637// {{{ class UFC_Contact
53eae167
RB
638/** Filters users who belongs to selected user's contacts
639 */
4e7bf1e0
FB
640class UFC_Contact extends UFC_UserRelated
641{
3f42a6ad
FB
642 public function buildCondition(UserFilter &$uf)
643 {
009b8ab7 644 $sub = $uf->addContactFilter($this->user->id());
3f42a6ad
FB
645 return 'c' . $sub . '.contact IS NOT NULL';
646 }
647}
8363588b 648// }}}
3f42a6ad 649
8363588b 650// {{{ class UFC_WatchRegistration
53eae167
RB
651/** Filters users being watched by selected user
652 */
4e7bf1e0
FB
653class UFC_WatchRegistration extends UFC_UserRelated
654{
655 public function buildCondition(UserFilter &$uf)
656 {
009b8ab7
FB
657 if (!$this->user->watch('registration')) {
658 return UserFilterCondition::COND_FALSE;
659 }
660 $uids = $this->user->watchUsers();
661 if (count($uids) == 0) {
662 return UserFilterCondition::COND_FALSE;
663 } else {
07eb5b0e 664 return '$UID IN ' . XDB::formatArray($uids);
009b8ab7 665 }
4e7bf1e0
FB
666 }
667}
8363588b 668// }}}
4e7bf1e0 669
8363588b 670// {{{ class UFC_WatchPromo
53eae167
RB
671/** Filters users belonging to a promo watched by selected user
672 * @param $user Selected user (the one watching promo)
673 * @param $grade Formation the user is watching
674 */
4e7bf1e0
FB
675class UFC_WatchPromo extends UFC_UserRelated
676{
677 private $grade;
009b8ab7 678 public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING)
4e7bf1e0 679 {
009b8ab7 680 parent::__construct($user);
4e7bf1e0
FB
681 $this->grade = $grade;
682 }
683
684 public function buildCondition(UserFilter &$uf)
685 {
009b8ab7
FB
686 $promos = $this->user->watchPromos();
687 if (count($promos) == 0) {
688 return UserFilterCondition::COND_FALSE;
689 } else {
690 $sube = $uf->addEducationFilter(true, $this->grade);
691 $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
07eb5b0e 692 return $field . ' IN ' . XDB::formatArray($promos);
009b8ab7 693 }
4e7bf1e0
FB
694 }
695}
8363588b 696// }}}
4e7bf1e0 697
8363588b 698// {{{ class UFC_WatchContact
53eae167
RB
699/** Filters users watched by selected user
700 */
009b8ab7 701class UFC_WatchContact extends UFC_Contact
4e7bf1e0
FB
702{
703 public function buildCondition(UserFilter &$uf)
704 {
009b8ab7
FB
705 if (!$this->user->watchContacts()) {
706 return UserFilterCondition::COND_FALSE;
707 }
708 return parent::buildCondition($uf);
4e7bf1e0
FB
709 }
710}
8363588b 711// }}}
4e7bf1e0
FB
712
713
d865c296
FB
714/******************
715 * ORDERS
716 ******************/
717
8363588b 718// {{{ class UserFilterOrder
d865c296
FB
719abstract class UserFilterOrder
720{
721 protected $desc = false;
009b8ab7
FB
722 public function __construct($desc = false)
723 {
724 $this->desc = $desc;
725 }
d865c296
FB
726
727 public function buildSort(UserFilter &$uf)
728 {
729 $sel = $this->getSortTokens($uf);
730 if (!is_array($sel)) {
731 $sel = array($sel);
732 }
733 if ($this->desc) {
734 foreach ($sel as $k=>$s) {
735 $sel[$k] = $s . ' DESC';
736 }
737 }
738 return $sel;
739 }
740
741 abstract protected function getSortTokens(UserFilter &$uf);
742}
8363588b 743// }}}
d865c296 744
8363588b 745// {{{ class UFO_Promo
53eae167
RB
746/** Orders users by promo
747 * @param $grade Formation whose promo users should be sorted by (restricts results to users of that formation)
748 * @param $desc Whether sort is descending
749 */
d865c296
FB
750class UFO_Promo extends UserFilterOrder
751{
752 private $grade;
753
754 public function __construct($grade = null, $desc = false)
755 {
009b8ab7 756 parent::__construct($desc);
d865c296 757 $this->grade = $grade;
d865c296
FB
758 }
759
760 protected function getSortTokens(UserFilter &$uf)
761 {
762 if (UserFilter::isGrade($this->grade)) {
763 $sub = $uf->addEducationFilter($this->grade);
764 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
765 } else {
766 $sub = $uf->addDisplayFilter();
767 return 'pd' . $sub . '.promo';
768 }
769 }
770}
8363588b 771// }}}
d865c296 772
8363588b 773// {{{ class UFO_Name
53eae167
RB
774/** Sorts users by name
775 * @param $type Type of name on which to sort (firstname, ...)
776 * @param $variant Variant of that name to user (marital, ordinary, ...)
777 * @param $particle Set to true if particles should be included in the sorting order
778 * @param $desc If sort order should be descending
779 */
d865c296
FB
780class UFO_Name extends UserFilterOrder
781{
782 private $type;
783 private $variant;
784 private $particle;
785
786 public function __construct($type, $variant = null, $particle = false, $desc = false)
787 {
009b8ab7 788 parent::__construct($desc);
d865c296
FB
789 $this->type = $type;
790 $this->variant = $variant;
791 $this->particle = $particle;
d865c296
FB
792 }
793
794 protected function getSortTokens(UserFilter &$uf)
795 {
796 if (UserFilter::isDisplayName($this->type)) {
797 $sub = $uf->addDisplayFilter();
798 return 'pd' . $sub . '.' . $this->type;
799 } else {
800 $sub = $uf->addNameFilter($this->type, $this->variant);
801 if ($this->particle) {
802 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
803 } else {
804 return 'pn' . $sub . '.name';
805 }
806 }
807 }
808}
8363588b 809// }}}
d865c296 810
8363588b 811// {{{ class UFO_Registration
53eae167
RB
812/** Sorts users based on registration date
813 */
38c6fe96
FB
814class UFO_Registration extends UserFilterOrder
815{
009b8ab7 816 protected function getSortTokens(UserFilter &$uf)
38c6fe96 817 {
009b8ab7 818 return 'a.registration_date';
38c6fe96 819 }
009b8ab7 820}
8363588b 821// }}}
38c6fe96 822
8363588b 823// {{{ class UFO_Birthday
53eae167
RB
824/** Sorts users based on next birthday date
825 */
009b8ab7
FB
826class UFO_Birthday extends UserFilterOrder
827{
38c6fe96
FB
828 protected function getSortTokens(UserFilter &$uf)
829 {
009b8ab7 830 return 'p.next_birthday';
38c6fe96
FB
831 }
832}
8363588b 833// }}}
38c6fe96 834
8363588b 835// {{{ class UFO_ProfileUpdate
53eae167
RB
836/** Sorts users based on last profile update
837 */
009b8ab7
FB
838class UFO_ProfileUpdate extends UserFilterOrder
839{
840 protected function getSortTokens(UserFilter &$uf)
841 {
842 return 'p.last_change';
843 }
844}
8363588b 845// }}}
009b8ab7 846
8363588b 847// {{{ class UFO_Death
53eae167
RB
848/** Sorts users based on death date
849 */
009b8ab7
FB
850class UFO_Death extends UserFilterOrder
851{
852 protected function getSortTokens(UserFilter &$uf)
853 {
854 return 'p.deathdate';
855 }
856}
8363588b 857// }}}
009b8ab7
FB
858
859
d865c296
FB
860/***********************************
861 *********************************
862 USER FILTER CLASS
863 *********************************
864 ***********************************/
865
8363588b 866// {{{ class UserFilter
a087cc8d
FB
867class UserFilter
868{
d865c296
FB
869 static private $joinMethods = array();
870
a087cc8d 871 private $root;
24e08e33 872 private $sort = array();
784745ce 873 private $query = null;
24e08e33 874 private $orderby = null;
784745ce 875
aa21c568 876 private $lastcount = null;
d865c296 877
24e08e33 878 public function __construct($cond = null, $sort = null)
5dd9d823 879 {
d865c296
FB
880 if (empty(self::$joinMethods)) {
881 $class = new ReflectionClass('UserFilter');
882 foreach ($class->getMethods() as $method) {
883 $name = $method->getName();
884 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
885 self::$joinMethods[] = $name;
886 }
887 }
888 }
5dd9d823
FB
889 if (!is_null($cond)) {
890 if ($cond instanceof UserFilterCondition) {
891 $this->setCondition($cond);
892 }
893 }
24e08e33
FB
894 if (!is_null($sort)) {
895 if ($sort instanceof UserFilterOrder) {
896 $this->addSort($sort);
d865c296
FB
897 } else if (is_array($sort)) {
898 foreach ($sort as $s) {
899 $this->addSort($s);
900 }
24e08e33
FB
901 }
902 }
5dd9d823
FB
903 }
904
784745ce
FB
905 private function buildQuery()
906 {
d865c296
FB
907 if (is_null($this->orderby)) {
908 $orders = array();
909 foreach ($this->sort as $sort) {
910 $orders = array_merge($orders, $sort->buildSort($this));
911 }
912 if (count($orders) == 0) {
913 $this->orderby = '';
914 } else {
915 $this->orderby = 'ORDER BY ' . implode(', ', $orders);
916 }
917 }
784745ce
FB
918 if (is_null($this->query)) {
919 $where = $this->root->buildCondition($this);
920 $joins = $this->buildJoins();
921 $this->query = 'FROM accounts AS a
eb1449b8
FB
922 LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
923 LEFT JOIN profiles AS p ON (p.pid = ap.pid)
784745ce
FB
924 ' . $joins . '
925 WHERE (' . $where . ')';
926 }
927 }
928
929 private function formatJoin(array $joins)
930 {
931 $str = '';
932 foreach ($joins as $key => $infos) {
933 $mode = $infos[0];
934 $table = $infos[1];
935 if ($mode == 'inner') {
936 $str .= 'INNER JOIN ';
937 } else if ($mode == 'left') {
938 $str .= 'LEFT JOIN ';
939 } else {
940 Platal::page()->kill("Join mode error");
941 }
942 $str .= $table . ' AS ' . $key;
943 if (isset($infos[2])) {
4927ee54 944 $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')';
784745ce
FB
945 }
946 $str .= "\n";
947 }
948 return $str;
949 }
950
951 private function buildJoins()
952 {
d865c296
FB
953 $joins = array();
954 foreach (self::$joinMethods as $method) {
955 $joins = array_merge($joins, $this->$method());
956 }
784745ce
FB
957 return $this->formatJoin($joins);
958 }
a087cc8d 959
d865c296
FB
960 private function getUIDList($uids = null, $count = null, $offset = null)
961 {
962 $this->buildQuery();
963 $limit = '';
964 if (!is_null($count)) {
965 if (!is_null($offset)) {
76cbe885 966 $limit = XDB::format('LIMIT {?}, {?}', (int)$offset, (int)$count);
d865c296 967 } else {
76cbe885 968 $limit = XDB::format('LIMIT {?}', (int)$count);
d865c296
FB
969 }
970 }
971 $cond = '';
972 if (!is_null($uids)) {
07eb5b0e 973 $cond = ' AND a.uid IN ' . XDB::formatArray($uids);
d865c296
FB
974 }
975 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
976 ' . $this->query . $cond . '
977 GROUP BY a.uid
978 ' . $this->orderby . '
979 ' . $limit);
980 $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
981 return $fetched;
982 }
983
a087cc8d
FB
984 /** Check that the user match the given rule.
985 */
986 public function checkUser(PlUser &$user)
987 {
784745ce
FB
988 $this->buildQuery();
989 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
990 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
991 return $count == 1;
a087cc8d
FB
992 }
993
994 /** Filter a list of user to extract the users matching the rule.
995 */
d865c296 996 public function filter(array $users, $count = null, $offset = null)
a087cc8d 997 {
4927ee54
FB
998 $this->buildQuery();
999 $table = array();
1000 $uids = array();
1001 foreach ($users as $user) {
07eb5b0e
FB
1002 if ($user instanceof PlUser) {
1003 $uid = $user->id();
1004 } else {
1005 $uid = $user;
1006 }
1007 $uids[] = $uid;
1008 $table[$uid] = $user;
4927ee54 1009 }
d865c296 1010 $fetched = $this->getUIDList($uids, $count, $offset);
a087cc8d 1011 $output = array();
4927ee54
FB
1012 foreach ($fetched as $uid) {
1013 $output[] = $table[$uid];
a087cc8d
FB
1014 }
1015 return $output;
1016 }
1017
d865c296 1018 public function getUIDs($count = null, $offset = null)
4927ee54 1019 {
d865c296
FB
1020 return $this->getUIDList(null, $count, $offset);
1021 }
1022
1023 public function getUsers($count = null, $offset = null)
1024 {
1025 return User::getBulkUsersWithUIDs($this->getUIDs($count, $offset));
4927ee54
FB
1026 }
1027
d865c296 1028 public function getTotalCount()
4927ee54 1029 {
38c6fe96 1030 if (is_null($this->lastcount)) {
aa21c568 1031 $this->buildQuery();
7e735012
FB
1032 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
1033 ' . $this->query);
38c6fe96
FB
1034 } else {
1035 return $this->lastcount;
1036 }
4927ee54
FB
1037 }
1038
a087cc8d
FB
1039 public function setCondition(UserFilterCondition &$cond)
1040 {
1041 $this->root =& $cond;
784745ce 1042 $this->query = null;
a087cc8d
FB
1043 }
1044
24e08e33
FB
1045 public function addSort(UserFilterOrder &$sort)
1046 {
d865c296
FB
1047 $this->sort[] = $sort;
1048 $this->orderby = null;
24e08e33
FB
1049 }
1050
a087cc8d
FB
1051 static public function getLegacy($promo_min, $promo_max)
1052 {
a087cc8d 1053 if ($promo_min != 0) {
784745ce 1054 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
5dd9d823
FB
1055 } else {
1056 $min = new UFC_True();
a087cc8d 1057 }
a087cc8d 1058 if ($promo_max != 0) {
784745ce 1059 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
a087cc8d 1060 } else {
5dd9d823 1061 $max = new UFC_True();
a087cc8d 1062 }
5dd9d823 1063 return new UserFilter(new UFC_And($min, $max));
a087cc8d 1064 }
784745ce 1065
07eb5b0e
FB
1066 static public function sortByName()
1067 {
1068 return array(new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
1069 }
1070
1071 static public function sortByPromo()
1072 {
1073 return array(new UFO_Promo(), new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
1074 }
1075
aa21c568
FB
1076 static private function getDBSuffix($string)
1077 {
1078 return preg_replace('/[^a-z0-9]/i', '', $string);
1079 }
1080
1081
1082 private $option = 0;
1083 private function register_optional(array &$table, $val)
1084 {
1085 if (is_null($val)) {
1086 $sub = $this->option++;
1087 $index = null;
1088 } else {
1089 $sub = self::getDBSuffix($val);
1090 $index = $val;
1091 }
1092 $sub = '_' . $sub;
1093 $table[$sub] = $index;
1094 return $sub;
1095 }
784745ce 1096
d865c296
FB
1097 /** DISPLAY
1098 */
38c6fe96 1099 const DISPLAY = 'display';
d865c296
FB
1100 private $pd = false;
1101 public function addDisplayFilter()
1102 {
1103 $this->pd = true;
1104 return '';
1105 }
1106
1107 private function displayJoins()
1108 {
1109 if ($this->pd) {
1110 return array('pd' => array('left', 'profile_display', '$ME.pid = $PID'));
1111 } else {
1112 return array();
1113 }
1114 }
1115
784745ce
FB
1116 /** NAMES
1117 */
d865c296 1118 /* name tokens */
784745ce
FB
1119 const LASTNAME = 'lastname';
1120 const FIRSTNAME = 'firstname';
1121 const NICKNAME = 'nickname';
1122 const PSEUDONYM = 'pseudonym';
1123 const NAME = 'name';
d865c296 1124 /* name variants */
784745ce
FB
1125 const VN_MARITAL = 'marital';
1126 const VN_ORDINARY = 'ordinary';
1127 const VN_OTHER = 'other';
1128 const VN_INI = 'ini';
d865c296
FB
1129 /* display names */
1130 const DN_FULL = 'directory_name';
1131 const DN_DISPLAY = 'yourself';
1132 const DN_YOURSELF = 'yourself';
1133 const DN_DIRECTORY = 'directory_name';
1134 const DN_PRIVATE = 'private_name';
1135 const DN_PUBLIC = 'public_name';
1136 const DN_SHORT = 'short_name';
1137 const DN_SORT = 'sort_name';
784745ce
FB
1138
1139 static public $name_variants = array(
1140 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
d865c296
FB
1141 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
1142 );
784745ce
FB
1143
1144 static public function assertName($name)
1145 {
1146 if (!Profile::getNameTypeId($name)) {
1147 Platal::page()->kill('Invalid name type');
1148 }
1149 }
1150
d865c296
FB
1151 static public function isDisplayName($name)
1152 {
1153 return $name == self::DN_FULL || $name == self::DN_DISPLAY
1154 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
1155 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
1156 || $name == self::DN_SHORT || $name == self::DN_SORT;
1157 }
1158
784745ce 1159 private $pn = array();
784745ce
FB
1160 public function addNameFilter($type, $variant = null)
1161 {
1162 if (!is_null($variant)) {
1163 $ft = $type . '_' . $variant;
1164 } else {
1165 $ft = $type;
1166 }
1167 $sub = '_' . $ft;
1168 self::assertName($ft);
1169
1170 if (!is_null($variant) && $variant == 'other') {
aa21c568 1171 $sub .= $this->option++;
784745ce
FB
1172 }
1173 $this->pn[$sub] = Profile::getNameTypeId($ft);
1174 return $sub;
1175 }
1176
1177 private function nameJoins()
1178 {
1179 $joins = array();
1180 foreach ($this->pn as $sub => $type) {
1181 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
1182 }
1183 return $joins;
1184 }
1185
1186
1187 /** EDUCATION
1188 */
1189 const GRADE_ING = 'Ing.';
1190 const GRADE_PHD = 'PhD';
1191 const GRADE_MST = 'M%';
1192 static public function isGrade($grade)
1193 {
38c6fe96 1194 return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST;
784745ce
FB
1195 }
1196
1197 static public function assertGrade($grade)
1198 {
1199 if (!self::isGrade($grade)) {
1200 Platal::page()->killError("Diplôme non valide");
1201 }
1202 }
1203
d865c296
FB
1204 static public function promoYear($grade)
1205 {
1206 // XXX: Definition of promotion for phds and masters might change in near future.
1207 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
1208 }
1209
784745ce
FB
1210 private $pepe = array();
1211 private $with_pee = false;
784745ce
FB
1212 public function addEducationFilter($x = false, $grade = null)
1213 {
1214 if (!$x) {
aa21c568
FB
1215 $index = $this->option;
1216 $sub = $this->option++;
784745ce
FB
1217 } else {
1218 self::assertGrade($grade);
1219 $index = $grade;
1220 $sub = $grade[0];
1221 $this->with_pee = true;
1222 }
1223 $sub = '_' . $sub;
1224 $this->pepe[$index] = $sub;
1225 return $sub;
1226 }
1227
1228 private function educationJoins()
1229 {
1230 $joins = array();
1231 if ($this->with_pee) {
1232 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
1233 }
1234 foreach ($this->pepe as $grade => $sub) {
1235 if ($this->isGrade($grade)) {
1236 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
1237 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
1238 XDB::format('{?}', $grade));
1239 } else {
1240 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID');
1241 $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
1242 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
1243 }
1244 }
1245 return $joins;
1246 }
4927ee54
FB
1247
1248
1249 /** GROUPS
1250 */
1251 private $gpm = array();
4927ee54
FB
1252 public function addGroupFilter($group = null)
1253 {
1254 if (!is_null($group)) {
1255 if (ctype_digit($group)) {
1256 $index = $sub = $group;
1257 } else {
1258 $index = $group;
aa21c568 1259 $sub = self::getDBSuffix($group);
4927ee54
FB
1260 }
1261 } else {
aa21c568 1262 $sub = 'group_' . $this->option++;
4927ee54
FB
1263 $index = null;
1264 }
1265 $sub = '_' . $sub;
1266 $this->gpm[$sub] = $index;
1267 return $sub;
1268 }
1269
1270 private function groupJoins()
1271 {
1272 $joins = array();
1273 foreach ($this->gpm as $sub => $key) {
1274 if (is_null($key)) {
1275 $joins['gpa' . $sub] = array('inner', 'groupex.asso');
1276 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1277 } else if (ctype_digit($key)) {
1278 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
1279 } else {
1280 $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key));
1281 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1282 }
1283 }
1284 return $joins;
1285 }
aa21c568
FB
1286
1287 /** EMAILS
1288 */
1289 private $e = array();
1290 public function addEmailRedirectFilter($email = null)
1291 {
1292 return $this->register_optional($this->e, $email);
1293 }
1294
1295 private $ve = array();
1296 public function addVirtualEmailFilter($email = null)
1297 {
21401768 1298 $this->addAliasFilter(self::ALIAS_FORLIFE);
aa21c568
FB
1299 return $this->register_optional($this->ve, $email);
1300 }
1301
21401768
FB
1302 const ALIAS_BEST = 'bestalias';
1303 const ALIAS_FORLIFE = 'forlife';
aa21c568
FB
1304 private $al = array();
1305 public function addAliasFilter($alias = null)
1306 {
1307 return $this->register_optional($this->al, $alias);
1308 }
1309
1310 private function emailJoins()
1311 {
1312 global $globals;
1313 $joins = array();
1314 foreach ($this->e as $sub=>$key) {
1315 if (is_null($key)) {
1316 $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
1317 } else {
1318 $joins['e' . $sub] = array('left', 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
1319 }
1320 }
21401768 1321 foreach ($this->al as $sub=>$key) {
aa21c568 1322 if (is_null($key)) {
21401768
FB
1323 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
1324 } else if ($key == self::ALIAS_BEST) {
1325 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
1326 } else if ($key == self::ALIAS_FORLIFE) {
1327 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
aa21c568 1328 } else {
21401768 1329 $joins['al' . $sub] = array('left', 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
aa21c568 1330 }
aa21c568 1331 }
21401768 1332 foreach ($this->ve as $sub=>$key) {
aa21c568 1333 if (is_null($key)) {
21401768 1334 $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\'');
aa21c568 1335 } else {
21401768 1336 $joins['v' . $sub] = array('left', 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
aa21c568 1337 }
21401768
FB
1338 $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid
1339 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
1340 CONCAT(al_forlife.alias, \'@\', {?}),
1341 a.email))',
1342 $globals->mail->domain, $globals->mail->domain2));
aa21c568
FB
1343 }
1344 return $joins;
1345 }
3f42a6ad
FB
1346
1347
c4b24511
RB
1348 /** ADDRESSES
1349 */
1350 private $pa = false;
1351 public function addAddressFilter()
1352 {
1353 $this->pa = true;
1354 }
1355
1356 private function addressJoins()
1357 {
1358 $joins = array();
1359 if ($this->pa) {
1360 $joins['pa'] = array('left', 'profile_address', '$ME.PID = $PID');
1361 }
1362 return $joins;
1363 }
1364
1365
4083b126
RB
1366 /** CORPS
1367 */
1368
1369 private $pc = false;
1370 private $pce = array();
1371 private $pcr = false;
1372 public function addCorpsFilter($type)
1373 {
1374 $this->pc = true;
1375 if ($type == UFC_Corps::CURRENT) {
1376 $pce['pcec'] = 'current_corpsid';
1377 return 'pcec';
1378 } else if ($type == UFC_Corps::ORIGIN) {
1379 $pce['pceo'] = 'original_corpsid';
1380 return 'pceo';
1381 }
1382 }
1383
1384 public function addCorpsRankFilter()
1385 {
1386 $this->pc = true;
1387 $this->pcr = true;
1388 return 'pcr';
1389 }
1390
1391 private function corpsJoins()
1392 {
1393 $joins = array();
1394 if ($this->pc) {
1395 $joins['pc'] = array('left', 'profile_corps', '$ME.uid = $UID');
1396 }
1397 if ($this->pcr) {
1398 $joins['pcr'] = array('left', 'profile_corps_rank_enum', '$ME.id = pc.rankid');
1399 }
1400 foreach($this->pce as $sub => $field) {
1401 $joins[$sub] = array('left', 'profile_corps_enum', '$ME.id = pc.' . $field);
1402 }
1403 return $joins;
1404 }
1405
3f42a6ad
FB
1406 /** CONTACTS
1407 */
1408 private $cts = array();
1409 public function addContactFilter($uid = null)
1410 {
1411 return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid);
1412 }
1413
1414 private function contactJoins()
1415 {
1416 $joins = array();
1417 foreach ($this->cts as $sub=>$key) {
1418 if (is_null($key)) {
1419 $joins['c' . $sub] = array('left', 'contacts', '$ME.contact = $UID');
1420 } else {
1421 $joins['c' . $sub] = array('left', 'contacts', XDB::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
1422 }
1423 }
1424 return $joins;
1425 }
4e7bf1e0
FB
1426
1427
1428 /** CARNET
1429 */
1430 private $wn = array();
1431 public function addWatchRegistrationFilter($uid = null)
1432 {
1433 return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid);
1434 }
1435
1436 private $wp = array();
1437 public function addWatchPromoFilter($uid = null)
1438 {
1439 return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid);
1440 }
1441
1442 private $w = array();
1443 public function addWatchFilter($uid = null)
1444 {
1445 return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid);
1446 }
1447
1448 private function watchJoins()
1449 {
1450 $joins = array();
1451 foreach ($this->w as $sub=>$key) {
1452 if (is_null($key)) {
1453 $joins['w' . $sub] = array('left', 'watch');
1454 } else {
1455 $joins['w' . $sub] = array('left', 'watch', XDB::format('$ME.uid = {?}', substr($key, 5)));
1456 }
1457 }
1458 foreach ($this->wn as $sub=>$key) {
1459 if (is_null($key)) {
1460 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1461 } else {
1462 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1463 }
1464 }
1465 foreach ($this->wn as $sub=>$key) {
1466 if (is_null($key)) {
1467 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1468 } else {
1469 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1470 }
1471 }
1472 foreach ($this->wp as $sub=>$key) {
1473 if (is_null($key)) {
1474 $joins['wp' . $sub] = array('left', 'watch_promo');
1475 } else {
1476 $joins['wp' . $sub] = array('left', 'watch_promo', XDB::format('$ME.uid = {?}', substr($key, 5)));
1477 }
1478 }
1479 return $joins;
1480 }
a087cc8d 1481}
8363588b 1482// }}}
3f42a6ad 1483
a087cc8d
FB
1484// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1485?>