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