Use flag 'registration'.
[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
164class UFC_Promo implements UserFilterCondition
165{
a087cc8d
FB
166
167 private $grade;
168 private $promo;
169 private $comparison;
170
171 public function __construct($comparison, $grade, $promo)
172 {
173 $this->grade = $grade;
174 $this->comparison = $comparison;
175 $this->promo = $promo;
38c6fe96
FB
176 if ($this->grade != UserFilter::DISPLAY) {
177 UserFilter::assertGrade($this->grade);
178 }
a087cc8d
FB
179 }
180
784745ce 181 public function buildCondition(UserFilter &$uf)
a087cc8d 182 {
38c6fe96
FB
183 if ($this->grade == UserFilter::DISPLAY) {
184 $sub = $uf->addDisplayFilter();
185 return XDB::format('pd' . $sub . '.promo = {?}', $this->promo);
186 } else {
187 $sub = $uf->addEducationFilter(true, $this->grade);
188 $field = 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
189 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison . ' ' . XDB::format('{?}', $this->promo);
190 }
784745ce
FB
191 }
192}
193
194class UFC_Name implements UserFilterCondition
195{
196 const PREFIX = 1;
197 const SUFFIX = 2;
198 const PARTICLE = 7;
199 const VARIANTS = 8;
200 const CONTAINS = 3;
201
202 private $type;
203 private $text;
204 private $mode;
205
206 public function __construct($type, $text, $mode)
207 {
208 $this->type = $type;
209 $this->text = $text;
210 $this->mode = $mode;
211 }
212
213 private function buildNameQuery($type, $variant, $where, UserFilter &$uf)
214 {
215 $sub = $uf->addNameFilter($type, $variant);
216 return str_replace('$ME', 'pn' . $sub, $where);
217 }
218
219 public function buildCondition(UserFilter &$uf)
220 {
221 $left = '$ME.name';
222 $op = ' LIKE ';
223 if (($this->mode & self::PARTICLE) == self::PARTICLE) {
224 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
225 }
226 if (($this->mode & self::CONTAINS) == 0) {
227 $right = XDB::format('{?}', $this->text);
228 $op = ' = ';
229 } else if (($this->mode & self::CONTAINS) == self::PREFIX) {
230 $right = XDB::format('CONCAT({?}, \'%\')', $this->text);
231 } else if (($this->mode & self::CONTAINS) == self::SUFFIX) {
232 $right = XDB::format('CONCAT(\'%\', {?})', $this->text);
233 } else {
234 $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text);
235 }
236 $cond = $left . $op . $right;
237 $conds = array($this->buildNameQuery($this->type, null, $cond, $uf));
d865c296 238 if (($this->mode & self::VARIANTS) != 0 && isset(UserFilter::$name_variants[$this->type])) {
784745ce
FB
239 foreach (UserFilter::$name_variants[$this->type] as $var) {
240 $conds[] = $this->buildNameQuery($this->type, $var, $cond, $uf);
241 }
242 }
243 return implode(' OR ', $conds);
a087cc8d
FB
244 }
245}
246
4927ee54
FB
247class UFC_Dead implements UserFilterCondition
248{
38c6fe96
FB
249 private $comparison;
250 private $date;
251
252 public function __construct($comparison = null, $date = null)
4927ee54 253 {
38c6fe96
FB
254 $this->comparison = $comparison;
255 $this->date = $date;
4927ee54
FB
256 }
257
258 public function buildCondition(UserFilter &$uf)
259 {
38c6fe96
FB
260 $str = 'p.deathdate IS NOT NULL';
261 if (!is_null($this->comparison)) {
262 $str .= ' AND p.deathdate ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date));
4927ee54 263 }
38c6fe96 264 return $str;
4927ee54
FB
265 }
266}
267
268class UFC_Registered implements UserFilterCondition
269{
270 private $active;
38c6fe96
FB
271 private $comparison;
272 private $date;
273
274 public function __construct($active = false, $comparison = null, $date = null)
4927ee54
FB
275 {
276 $this->only_active = $active;
38c6fe96
FB
277 $this->comparison = $comparison;
278 $this->date = $date;
4927ee54
FB
279 }
280
281 public function buildCondition(UserFilter &$uf)
282 {
283 if ($this->active) {
38c6fe96 284 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
4927ee54 285 } else {
38c6fe96
FB
286 $date = 'a.uid IS NOT NULL AND a.state != \'pending\'';
287 }
288 if (!is_null($this->comparison)) {
289 $date .= ' AND a.registration_date ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date));
4927ee54 290 }
38c6fe96 291 return $date;
4927ee54
FB
292 }
293}
294
7e735012
FB
295class UFC_ProfileUpdated implements UserFilterCondition
296{
297 private $comparison;
298 private $date;
299
300 public function __construct($comparison = null, $date = null)
301 {
302 $this->comparison = $comparison;
303 $this->date = $date;
304 }
305
306 public function buildCondition(UserFilter &$uf)
307 {
308 return 'p.last_change ' . $this->comparison . XDB::format(' {?}', date('Y-m-d H:i:s', $this->date));
309 }
310}
311
312class UFC_Birthday implements UserFilterCondition
313{
314 private $comparison;
315 private $date;
316
317 public function __construct($comparison = null, $date = null)
318 {
319 $this->comparison = $comparison;
320 $this->date = $date;
321 }
322
323 public function buildCondition(UserFilter &$uf)
324 {
325 return 'p.next_birthday ' . $this->comparison . XDB::format(' {?}', date('Y-m-d', $this->date));
326 }
327}
328
4927ee54
FB
329class UFC_Sex implements UserFilterCondition
330{
331 private $sex;
332 public function __construct($sex)
333 {
334 $this->sex = $sex;
335 }
336
337 public function buildCondition(UserFilter &$uf)
338 {
339 if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) {
340 return self::COND_FALSE;
341 } else {
24e08e33 342 return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male');
4927ee54
FB
343 }
344 }
345}
346
347class UFC_Group implements UserFilterCondition
348{
349 private $group;
350 private $admin;
351 public function __construct($group, $admin = false)
352 {
353 $this->group = $group;
354 $this->admin = $admin;
355 }
356
357 public function buildCondition(UserFilter &$uf)
358 {
359 $sub = $uf->addGroupFilter($this->group);
360 $where = 'gpm' . $sub . '.perms IS NOT NULL';
361 if ($this->admin) {
362 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
363 }
364 return $where;
365 }
366}
367
aa21c568
FB
368class UFC_Email implements UserFilterCondition
369{
370 private $email;
371 public function __construct($email)
372 {
373 $this->email = $email;
374 }
375
376 public function buildCondition(UserFilter &$uf)
377 {
378 if (User::isForeignEmailAddress($this->email)) {
379 $sub = $uf->addEmailRedirectFilter($this->email);
380 return XDB::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email);
21401768
FB
381 } else if (User::isVirtualEmailAddress($this->email)) {
382 $sub = $uf->addVirtualEmailFilter($this->email);
383 return 'vr' . $sub . '.redirect IS NOT NULL';
aa21c568 384 } else {
21401768
FB
385 @list($user, $domain) = explode('@', $this->email);
386 $sub = $uf->addAliasFilter($user);
aa21c568
FB
387 return 'al' . $sub . '.alias IS NOT NULL';
388 }
389 }
390}
d865c296 391
21401768
FB
392class UFC_EmailList implements UserFilterCondition
393{
394 private $emails;
395 public function __construct($emails)
396 {
397 $this->emails = $emails;
398 }
399
400 public function buildCondition(UserFilter &$uf)
401 {
402 $email = null;
403 $virtual = null;
404 $alias = null;
405 $cond = array();
406
407 if (count($this->emails) == 0) {
408 return UserFilterCondition::COND_TRUE;
409 }
410
411 foreach ($this->emails as $entry) {
412 if (User::isForeignEmailAddress($entry)) {
413 if (is_null($email)) {
414 $email = $uf->addEmailRedirectFilter();
415 }
416 $cond[] = XDB::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry);
417 } else if (User::isVirtualEmailAddress($entry)) {
418 if (is_null($virtual)) {
419 $virtual = $uf->addVirtualEmailFilter();
420 }
421 $cond[] = XDB::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry);
422 } else {
423 if (is_null($alias)) {
424 $alias = $uf->addAliasFilter();
425 }
426 @list($user, $domain) = explode('@', $entry);
427 $cond[] = XDB::format('al' . $alias . '.alias = {?}', $user);
428 }
429 }
430 return '(' . implode(') OR (', $cond) . ')';
431 }
432}
d865c296 433
4e7bf1e0 434abstract class UFC_UserRelated implements UserFilterCondition
3f42a6ad 435{
4e7bf1e0 436 protected $uid;
3f42a6ad
FB
437 public function __construct($uid = null)
438 {
439 if (is_null($uid)) {
440 $this->uid = S::i('uid');
441 } else if ($uid instanceof PlUser) {
442 $this->uid = $uid->id();
443 } else if (ctype_digit($uid)) {
444 $this->uid = (int)$uid;
445 } else {
446 Platal::page()->kill("Invalid contact type");
447 }
448 }
4e7bf1e0 449}
3f42a6ad 450
4e7bf1e0
FB
451class UFC_Contact extends UFC_UserRelated
452{
3f42a6ad
FB
453 public function buildCondition(UserFilter &$uf)
454 {
455 $sub = $uf->addContactFilter($this->uid);
456 return 'c' . $sub . '.contact IS NOT NULL';
457 }
458}
459
4e7bf1e0
FB
460class UFC_WatchRegistration extends UFC_UserRelated
461{
462 public function buildCondition(UserFilter &$uf)
463 {
464 $sub = $uf->addWatchRegistrationFilter($this->uid);
7e735012
FB
465 $su = $uf->addWatchFilter($this->uid);
466 return 'FIND_IN_SET(\'registration\', w' . $su . '.flags) OR wn' . $sub . '.uid IS NOT NULL';
4e7bf1e0
FB
467 }
468}
469
470class UFC_WatchPromo extends UFC_UserRelated
471{
472 private $grade;
473 public function __construct($uid = null, $grade = UserFilter::GRADE_ING)
474 {
475 parent::__construct($uid);
476 $this->grade = $grade;
477 }
478
479 public function buildCondition(UserFilter &$uf)
480 {
481 $sube = $uf->addEducationFilter(true, $this->grade);
482 $subw = $uf->addWatchPromoFilter($this->uid);
483 $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
484 return $field . ' IS NOT NULL AND ' . $field . ' = wp' . $subw . '.promo';
485 }
486}
487
488class UFC_WatchContacts extends UFC_Contact
489{
490 public function buildCondition(UserFilter &$uf)
491 {
492 $sub = $uf->addWatchFilter($this->uid);
7e735012 493 return 'FIND_IN_SET(\'contacts\', w' . $sub . '.flags) AND ' . parent::buildCondition($uf);
4e7bf1e0
FB
494 }
495}
496
497
7e735012 498
d865c296
FB
499/******************
500 * ORDERS
501 ******************/
502
503abstract class UserFilterOrder
504{
505 protected $desc = false;
506
507 public function buildSort(UserFilter &$uf)
508 {
509 $sel = $this->getSortTokens($uf);
510 if (!is_array($sel)) {
511 $sel = array($sel);
512 }
513 if ($this->desc) {
514 foreach ($sel as $k=>$s) {
515 $sel[$k] = $s . ' DESC';
516 }
517 }
518 return $sel;
519 }
520
521 abstract protected function getSortTokens(UserFilter &$uf);
522}
523
524class UFO_Promo extends UserFilterOrder
525{
526 private $grade;
527
528 public function __construct($grade = null, $desc = false)
529 {
530 $this->grade = $grade;
531 $this->desc = $desc;
532 }
533
534 protected function getSortTokens(UserFilter &$uf)
535 {
536 if (UserFilter::isGrade($this->grade)) {
537 $sub = $uf->addEducationFilter($this->grade);
538 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
539 } else {
540 $sub = $uf->addDisplayFilter();
541 return 'pd' . $sub . '.promo';
542 }
543 }
544}
545
546class UFO_Name extends UserFilterOrder
547{
548 private $type;
549 private $variant;
550 private $particle;
551
552 public function __construct($type, $variant = null, $particle = false, $desc = false)
553 {
554 $this->type = $type;
555 $this->variant = $variant;
556 $this->particle = $particle;
557 $this->desc = $desc;
558 }
559
560 protected function getSortTokens(UserFilter &$uf)
561 {
562 if (UserFilter::isDisplayName($this->type)) {
563 $sub = $uf->addDisplayFilter();
564 return 'pd' . $sub . '.' . $this->type;
565 } else {
566 $sub = $uf->addNameFilter($this->type, $this->variant);
567 if ($this->particle) {
568 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
569 } else {
570 return 'pn' . $sub . '.name';
571 }
572 }
573 }
574}
575
38c6fe96
FB
576class UFO_Registration extends UserFilterOrder
577{
578 public function __construct($desc = false)
579 {
580 $this->desc = $desc;
581 }
582
583 protected function getSortTokens(UserFilter &$uf)
584 {
585 return 'a.registration_date';
586 }
587}
588
d865c296
FB
589/***********************************
590 *********************************
591 USER FILTER CLASS
592 *********************************
593 ***********************************/
594
a087cc8d
FB
595class UserFilter
596{
d865c296
FB
597 static private $joinMethods = array();
598
a087cc8d 599 private $root;
24e08e33 600 private $sort = array();
784745ce 601 private $query = null;
24e08e33 602 private $orderby = null;
784745ce 603
aa21c568 604 private $lastcount = null;
d865c296 605
24e08e33 606 public function __construct($cond = null, $sort = null)
5dd9d823 607 {
d865c296
FB
608 if (empty(self::$joinMethods)) {
609 $class = new ReflectionClass('UserFilter');
610 foreach ($class->getMethods() as $method) {
611 $name = $method->getName();
612 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
613 self::$joinMethods[] = $name;
614 }
615 }
616 }
5dd9d823
FB
617 if (!is_null($cond)) {
618 if ($cond instanceof UserFilterCondition) {
619 $this->setCondition($cond);
620 }
621 }
24e08e33
FB
622 if (!is_null($sort)) {
623 if ($sort instanceof UserFilterOrder) {
624 $this->addSort($sort);
d865c296
FB
625 } else if (is_array($sort)) {
626 foreach ($sort as $s) {
627 $this->addSort($s);
628 }
24e08e33
FB
629 }
630 }
5dd9d823
FB
631 }
632
784745ce
FB
633 private function buildQuery()
634 {
d865c296
FB
635 if (is_null($this->orderby)) {
636 $orders = array();
637 foreach ($this->sort as $sort) {
638 $orders = array_merge($orders, $sort->buildSort($this));
639 }
640 if (count($orders) == 0) {
641 $this->orderby = '';
642 } else {
643 $this->orderby = 'ORDER BY ' . implode(', ', $orders);
644 }
645 }
784745ce
FB
646 if (is_null($this->query)) {
647 $where = $this->root->buildCondition($this);
648 $joins = $this->buildJoins();
649 $this->query = 'FROM accounts AS a
650 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
651 INNER JOIN profiles AS p ON (p.pid = ap.pid)
652 ' . $joins . '
653 WHERE (' . $where . ')';
654 }
655 }
656
657 private function formatJoin(array $joins)
658 {
659 $str = '';
660 foreach ($joins as $key => $infos) {
661 $mode = $infos[0];
662 $table = $infos[1];
663 if ($mode == 'inner') {
664 $str .= 'INNER JOIN ';
665 } else if ($mode == 'left') {
666 $str .= 'LEFT JOIN ';
667 } else {
668 Platal::page()->kill("Join mode error");
669 }
670 $str .= $table . ' AS ' . $key;
671 if (isset($infos[2])) {
4927ee54 672 $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')';
784745ce
FB
673 }
674 $str .= "\n";
675 }
676 return $str;
677 }
678
679 private function buildJoins()
680 {
d865c296
FB
681 $joins = array();
682 foreach (self::$joinMethods as $method) {
683 $joins = array_merge($joins, $this->$method());
684 }
784745ce
FB
685 return $this->formatJoin($joins);
686 }
a087cc8d 687
d865c296
FB
688 private function getUIDList($uids = null, $count = null, $offset = null)
689 {
690 $this->buildQuery();
691 $limit = '';
692 if (!is_null($count)) {
693 if (!is_null($offset)) {
694 $limit = XDB::format('LIMIT {?}, {?}', $offset, $count);
695 } else {
696 $limit = XDB::format('LIMIT {?}', $count);
697 }
698 }
699 $cond = '';
700 if (!is_null($uids)) {
701 $cond = ' AND a.uid IN (' . implode(', ', $uids) . ')';
702 }
703 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
704 ' . $this->query . $cond . '
705 GROUP BY a.uid
706 ' . $this->orderby . '
707 ' . $limit);
708 $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
709 return $fetched;
710 }
711
a087cc8d
FB
712 /** Check that the user match the given rule.
713 */
714 public function checkUser(PlUser &$user)
715 {
784745ce
FB
716 $this->buildQuery();
717 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
718 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
719 return $count == 1;
a087cc8d
FB
720 }
721
722 /** Filter a list of user to extract the users matching the rule.
723 */
d865c296 724 public function filter(array $users, $count = null, $offset = null)
a087cc8d 725 {
4927ee54
FB
726 $this->buildQuery();
727 $table = array();
728 $uids = array();
729 foreach ($users as $user) {
730 $uids[] = $user->id();
731 $table[$user->id()] = $user;
732 }
d865c296 733 $fetched = $this->getUIDList($uids, $count, $offset);
a087cc8d 734 $output = array();
4927ee54
FB
735 foreach ($fetched as $uid) {
736 $output[] = $table[$uid];
a087cc8d
FB
737 }
738 return $output;
739 }
740
d865c296 741 public function getUIDs($count = null, $offset = null)
4927ee54 742 {
d865c296
FB
743 return $this->getUIDList(null, $count, $offset);
744 }
745
746 public function getUsers($count = null, $offset = null)
747 {
748 return User::getBulkUsersWithUIDs($this->getUIDs($count, $offset));
4927ee54
FB
749 }
750
d865c296 751 public function getTotalCount()
4927ee54 752 {
38c6fe96 753 if (is_null($this->lastcount)) {
aa21c568 754 $this->buildQuery();
7e735012
FB
755 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
756 ' . $this->query);
38c6fe96
FB
757 } else {
758 return $this->lastcount;
759 }
4927ee54
FB
760 }
761
a087cc8d
FB
762 public function setCondition(UserFilterCondition &$cond)
763 {
764 $this->root =& $cond;
784745ce 765 $this->query = null;
a087cc8d
FB
766 }
767
24e08e33
FB
768 public function addSort(UserFilterOrder &$sort)
769 {
d865c296
FB
770 $this->sort[] = $sort;
771 $this->orderby = null;
24e08e33
FB
772 }
773
a087cc8d
FB
774 static public function getLegacy($promo_min, $promo_max)
775 {
a087cc8d 776 if ($promo_min != 0) {
784745ce 777 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
5dd9d823
FB
778 } else {
779 $min = new UFC_True();
a087cc8d 780 }
a087cc8d 781 if ($promo_max != 0) {
784745ce 782 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
a087cc8d 783 } else {
5dd9d823 784 $max = new UFC_True();
a087cc8d 785 }
5dd9d823 786 return new UserFilter(new UFC_And($min, $max));
a087cc8d 787 }
784745ce 788
aa21c568
FB
789 static private function getDBSuffix($string)
790 {
791 return preg_replace('/[^a-z0-9]/i', '', $string);
792 }
793
794
795 private $option = 0;
796 private function register_optional(array &$table, $val)
797 {
798 if (is_null($val)) {
799 $sub = $this->option++;
800 $index = null;
801 } else {
802 $sub = self::getDBSuffix($val);
803 $index = $val;
804 }
805 $sub = '_' . $sub;
806 $table[$sub] = $index;
807 return $sub;
808 }
784745ce 809
d865c296
FB
810 /** DISPLAY
811 */
38c6fe96 812 const DISPLAY = 'display';
d865c296
FB
813 private $pd = false;
814 public function addDisplayFilter()
815 {
816 $this->pd = true;
817 return '';
818 }
819
820 private function displayJoins()
821 {
822 if ($this->pd) {
823 return array('pd' => array('left', 'profile_display', '$ME.pid = $PID'));
824 } else {
825 return array();
826 }
827 }
828
784745ce
FB
829 /** NAMES
830 */
d865c296 831 /* name tokens */
784745ce
FB
832 const LASTNAME = 'lastname';
833 const FIRSTNAME = 'firstname';
834 const NICKNAME = 'nickname';
835 const PSEUDONYM = 'pseudonym';
836 const NAME = 'name';
d865c296 837 /* name variants */
784745ce
FB
838 const VN_MARITAL = 'marital';
839 const VN_ORDINARY = 'ordinary';
840 const VN_OTHER = 'other';
841 const VN_INI = 'ini';
d865c296
FB
842 /* display names */
843 const DN_FULL = 'directory_name';
844 const DN_DISPLAY = 'yourself';
845 const DN_YOURSELF = 'yourself';
846 const DN_DIRECTORY = 'directory_name';
847 const DN_PRIVATE = 'private_name';
848 const DN_PUBLIC = 'public_name';
849 const DN_SHORT = 'short_name';
850 const DN_SORT = 'sort_name';
784745ce
FB
851
852 static public $name_variants = array(
853 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
d865c296
FB
854 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
855 );
784745ce
FB
856
857 static public function assertName($name)
858 {
859 if (!Profile::getNameTypeId($name)) {
860 Platal::page()->kill('Invalid name type');
861 }
862 }
863
d865c296
FB
864 static public function isDisplayName($name)
865 {
866 return $name == self::DN_FULL || $name == self::DN_DISPLAY
867 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
868 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
869 || $name == self::DN_SHORT || $name == self::DN_SORT;
870 }
871
784745ce 872 private $pn = array();
784745ce
FB
873 public function addNameFilter($type, $variant = null)
874 {
875 if (!is_null($variant)) {
876 $ft = $type . '_' . $variant;
877 } else {
878 $ft = $type;
879 }
880 $sub = '_' . $ft;
881 self::assertName($ft);
882
883 if (!is_null($variant) && $variant == 'other') {
aa21c568 884 $sub .= $this->option++;
784745ce
FB
885 }
886 $this->pn[$sub] = Profile::getNameTypeId($ft);
887 return $sub;
888 }
889
890 private function nameJoins()
891 {
892 $joins = array();
893 foreach ($this->pn as $sub => $type) {
894 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
895 }
896 return $joins;
897 }
898
899
900 /** EDUCATION
901 */
902 const GRADE_ING = 'Ing.';
903 const GRADE_PHD = 'PhD';
904 const GRADE_MST = 'M%';
905 static public function isGrade($grade)
906 {
38c6fe96 907 return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST;
784745ce
FB
908 }
909
910 static public function assertGrade($grade)
911 {
912 if (!self::isGrade($grade)) {
913 Platal::page()->killError("DiplĂ´me non valide");
914 }
915 }
916
d865c296
FB
917 static public function promoYear($grade)
918 {
919 // XXX: Definition of promotion for phds and masters might change in near future.
920 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
921 }
922
784745ce
FB
923 private $pepe = array();
924 private $with_pee = false;
784745ce
FB
925 public function addEducationFilter($x = false, $grade = null)
926 {
927 if (!$x) {
aa21c568
FB
928 $index = $this->option;
929 $sub = $this->option++;
784745ce
FB
930 } else {
931 self::assertGrade($grade);
932 $index = $grade;
933 $sub = $grade[0];
934 $this->with_pee = true;
935 }
936 $sub = '_' . $sub;
937 $this->pepe[$index] = $sub;
938 return $sub;
939 }
940
941 private function educationJoins()
942 {
943 $joins = array();
944 if ($this->with_pee) {
945 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
946 }
947 foreach ($this->pepe as $grade => $sub) {
948 if ($this->isGrade($grade)) {
949 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
950 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
951 XDB::format('{?}', $grade));
952 } else {
953 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID');
954 $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
955 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
956 }
957 }
958 return $joins;
959 }
4927ee54
FB
960
961
962 /** GROUPS
963 */
964 private $gpm = array();
4927ee54
FB
965 public function addGroupFilter($group = null)
966 {
967 if (!is_null($group)) {
968 if (ctype_digit($group)) {
969 $index = $sub = $group;
970 } else {
971 $index = $group;
aa21c568 972 $sub = self::getDBSuffix($group);
4927ee54
FB
973 }
974 } else {
aa21c568 975 $sub = 'group_' . $this->option++;
4927ee54
FB
976 $index = null;
977 }
978 $sub = '_' . $sub;
979 $this->gpm[$sub] = $index;
980 return $sub;
981 }
982
983 private function groupJoins()
984 {
985 $joins = array();
986 foreach ($this->gpm as $sub => $key) {
987 if (is_null($key)) {
988 $joins['gpa' . $sub] = array('inner', 'groupex.asso');
989 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
990 } else if (ctype_digit($key)) {
991 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
992 } else {
993 $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key));
994 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
995 }
996 }
997 return $joins;
998 }
aa21c568
FB
999
1000 /** EMAILS
1001 */
1002 private $e = array();
1003 public function addEmailRedirectFilter($email = null)
1004 {
1005 return $this->register_optional($this->e, $email);
1006 }
1007
1008 private $ve = array();
1009 public function addVirtualEmailFilter($email = null)
1010 {
21401768 1011 $this->addAliasFilter(self::ALIAS_FORLIFE);
aa21c568
FB
1012 return $this->register_optional($this->ve, $email);
1013 }
1014
21401768
FB
1015 const ALIAS_BEST = 'bestalias';
1016 const ALIAS_FORLIFE = 'forlife';
aa21c568
FB
1017 private $al = array();
1018 public function addAliasFilter($alias = null)
1019 {
1020 return $this->register_optional($this->al, $alias);
1021 }
1022
1023 private function emailJoins()
1024 {
1025 global $globals;
1026 $joins = array();
1027 foreach ($this->e as $sub=>$key) {
1028 if (is_null($key)) {
1029 $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
1030 } else {
1031 $joins['e' . $sub] = array('left', 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
1032 }
1033 }
21401768 1034 foreach ($this->al as $sub=>$key) {
aa21c568 1035 if (is_null($key)) {
21401768
FB
1036 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
1037 } else if ($key == self::ALIAS_BEST) {
1038 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
1039 } else if ($key == self::ALIAS_FORLIFE) {
1040 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
aa21c568 1041 } else {
21401768 1042 $joins['al' . $sub] = array('left', 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
aa21c568 1043 }
aa21c568 1044 }
21401768 1045 foreach ($this->ve as $sub=>$key) {
aa21c568 1046 if (is_null($key)) {
21401768 1047 $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\'');
aa21c568 1048 } else {
21401768 1049 $joins['v' . $sub] = array('left', 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
aa21c568 1050 }
21401768
FB
1051 $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid
1052 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
1053 CONCAT(al_forlife.alias, \'@\', {?}),
1054 a.email))',
1055 $globals->mail->domain, $globals->mail->domain2));
aa21c568
FB
1056 }
1057 return $joins;
1058 }
3f42a6ad
FB
1059
1060
1061 /** CONTACTS
1062 */
1063 private $cts = array();
1064 public function addContactFilter($uid = null)
1065 {
1066 return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid);
1067 }
1068
1069 private function contactJoins()
1070 {
1071 $joins = array();
1072 foreach ($this->cts as $sub=>$key) {
1073 if (is_null($key)) {
1074 $joins['c' . $sub] = array('left', 'contacts', '$ME.contact = $UID');
1075 } else {
1076 $joins['c' . $sub] = array('left', 'contacts', XDB::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
1077 }
1078 }
1079 return $joins;
1080 }
4e7bf1e0
FB
1081
1082
1083 /** CARNET
1084 */
1085 private $wn = array();
1086 public function addWatchRegistrationFilter($uid = null)
1087 {
1088 return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid);
1089 }
1090
1091 private $wp = array();
1092 public function addWatchPromoFilter($uid = null)
1093 {
1094 return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid);
1095 }
1096
1097 private $w = array();
1098 public function addWatchFilter($uid = null)
1099 {
1100 return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid);
1101 }
1102
1103 private function watchJoins()
1104 {
1105 $joins = array();
1106 foreach ($this->w as $sub=>$key) {
1107 if (is_null($key)) {
1108 $joins['w' . $sub] = array('left', 'watch');
1109 } else {
1110 $joins['w' . $sub] = array('left', 'watch', XDB::format('$ME.uid = {?}', substr($key, 5)));
1111 }
1112 }
1113 foreach ($this->wn as $sub=>$key) {
1114 if (is_null($key)) {
1115 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1116 } else {
1117 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1118 }
1119 }
1120 foreach ($this->wn as $sub=>$key) {
1121 if (is_null($key)) {
1122 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1123 } else {
1124 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1125 }
1126 }
1127 foreach ($this->wp as $sub=>$key) {
1128 if (is_null($key)) {
1129 $joins['wp' . $sub] = array('left', 'watch_promo');
1130 } else {
1131 $joins['wp' . $sub] = array('left', 'watch_promo', XDB::format('$ME.uid = {?}', substr($key, 5)));
1132 }
1133 }
1134 return $joins;
1135 }
a087cc8d
FB
1136}
1137
3f42a6ad 1138
a087cc8d
FB
1139// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1140?>