Port xnetevents (I hope this works).
[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{
009b8ab7
FB
436 protected $user;
437 public function __construct(PlUser &$user)
438 {
439 $this->user =& $user;
3f42a6ad 440 }
4e7bf1e0 441}
3f42a6ad 442
4e7bf1e0
FB
443class UFC_Contact extends UFC_UserRelated
444{
3f42a6ad
FB
445 public function buildCondition(UserFilter &$uf)
446 {
009b8ab7 447 $sub = $uf->addContactFilter($this->user->id());
3f42a6ad
FB
448 return 'c' . $sub . '.contact IS NOT NULL';
449 }
450}
451
4e7bf1e0
FB
452class UFC_WatchRegistration extends UFC_UserRelated
453{
454 public function buildCondition(UserFilter &$uf)
455 {
009b8ab7
FB
456 if (!$this->user->watch('registration')) {
457 return UserFilterCondition::COND_FALSE;
458 }
459 $uids = $this->user->watchUsers();
460 if (count($uids) == 0) {
461 return UserFilterCondition::COND_FALSE;
462 } else {
07eb5b0e 463 return '$UID IN ' . XDB::formatArray($uids);
009b8ab7 464 }
4e7bf1e0
FB
465 }
466}
467
468class UFC_WatchPromo extends UFC_UserRelated
469{
470 private $grade;
009b8ab7 471 public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING)
4e7bf1e0 472 {
009b8ab7 473 parent::__construct($user);
4e7bf1e0
FB
474 $this->grade = $grade;
475 }
476
477 public function buildCondition(UserFilter &$uf)
478 {
009b8ab7
FB
479 $promos = $this->user->watchPromos();
480 if (count($promos) == 0) {
481 return UserFilterCondition::COND_FALSE;
482 } else {
483 $sube = $uf->addEducationFilter(true, $this->grade);
484 $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
07eb5b0e 485 return $field . ' IN ' . XDB::formatArray($promos);
009b8ab7 486 }
4e7bf1e0
FB
487 }
488}
489
009b8ab7 490class UFC_WatchContact extends UFC_Contact
4e7bf1e0
FB
491{
492 public function buildCondition(UserFilter &$uf)
493 {
009b8ab7
FB
494 if (!$this->user->watchContacts()) {
495 return UserFilterCondition::COND_FALSE;
496 }
497 return parent::buildCondition($uf);
4e7bf1e0
FB
498 }
499}
500
501
d865c296
FB
502/******************
503 * ORDERS
504 ******************/
505
506abstract class UserFilterOrder
507{
508 protected $desc = false;
009b8ab7
FB
509 public function __construct($desc = false)
510 {
511 $this->desc = $desc;
512 }
d865c296
FB
513
514 public function buildSort(UserFilter &$uf)
515 {
516 $sel = $this->getSortTokens($uf);
517 if (!is_array($sel)) {
518 $sel = array($sel);
519 }
520 if ($this->desc) {
521 foreach ($sel as $k=>$s) {
522 $sel[$k] = $s . ' DESC';
523 }
524 }
525 return $sel;
526 }
527
528 abstract protected function getSortTokens(UserFilter &$uf);
529}
530
531class UFO_Promo extends UserFilterOrder
532{
533 private $grade;
534
535 public function __construct($grade = null, $desc = false)
536 {
009b8ab7 537 parent::__construct($desc);
d865c296 538 $this->grade = $grade;
d865c296
FB
539 }
540
541 protected function getSortTokens(UserFilter &$uf)
542 {
543 if (UserFilter::isGrade($this->grade)) {
544 $sub = $uf->addEducationFilter($this->grade);
545 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
546 } else {
547 $sub = $uf->addDisplayFilter();
548 return 'pd' . $sub . '.promo';
549 }
550 }
551}
552
553class UFO_Name extends UserFilterOrder
554{
555 private $type;
556 private $variant;
557 private $particle;
558
559 public function __construct($type, $variant = null, $particle = false, $desc = false)
560 {
009b8ab7 561 parent::__construct($desc);
d865c296
FB
562 $this->type = $type;
563 $this->variant = $variant;
564 $this->particle = $particle;
d865c296
FB
565 }
566
567 protected function getSortTokens(UserFilter &$uf)
568 {
569 if (UserFilter::isDisplayName($this->type)) {
570 $sub = $uf->addDisplayFilter();
571 return 'pd' . $sub . '.' . $this->type;
572 } else {
573 $sub = $uf->addNameFilter($this->type, $this->variant);
574 if ($this->particle) {
575 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
576 } else {
577 return 'pn' . $sub . '.name';
578 }
579 }
580 }
581}
582
38c6fe96
FB
583class UFO_Registration extends UserFilterOrder
584{
009b8ab7 585 protected function getSortTokens(UserFilter &$uf)
38c6fe96 586 {
009b8ab7 587 return 'a.registration_date';
38c6fe96 588 }
009b8ab7 589}
38c6fe96 590
009b8ab7
FB
591class UFO_Birthday extends UserFilterOrder
592{
38c6fe96
FB
593 protected function getSortTokens(UserFilter &$uf)
594 {
009b8ab7 595 return 'p.next_birthday';
38c6fe96
FB
596 }
597}
598
009b8ab7
FB
599class UFO_ProfileUpdate extends UserFilterOrder
600{
601 protected function getSortTokens(UserFilter &$uf)
602 {
603 return 'p.last_change';
604 }
605}
606
607class UFO_Death extends UserFilterOrder
608{
609 protected function getSortTokens(UserFilter &$uf)
610 {
611 return 'p.deathdate';
612 }
613}
614
615
d865c296
FB
616/***********************************
617 *********************************
618 USER FILTER CLASS
619 *********************************
620 ***********************************/
621
a087cc8d
FB
622class UserFilter
623{
d865c296
FB
624 static private $joinMethods = array();
625
a087cc8d 626 private $root;
24e08e33 627 private $sort = array();
784745ce 628 private $query = null;
24e08e33 629 private $orderby = null;
784745ce 630
aa21c568 631 private $lastcount = null;
d865c296 632
24e08e33 633 public function __construct($cond = null, $sort = null)
5dd9d823 634 {
d865c296
FB
635 if (empty(self::$joinMethods)) {
636 $class = new ReflectionClass('UserFilter');
637 foreach ($class->getMethods() as $method) {
638 $name = $method->getName();
639 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
640 self::$joinMethods[] = $name;
641 }
642 }
643 }
5dd9d823
FB
644 if (!is_null($cond)) {
645 if ($cond instanceof UserFilterCondition) {
646 $this->setCondition($cond);
647 }
648 }
24e08e33
FB
649 if (!is_null($sort)) {
650 if ($sort instanceof UserFilterOrder) {
651 $this->addSort($sort);
d865c296
FB
652 } else if (is_array($sort)) {
653 foreach ($sort as $s) {
654 $this->addSort($s);
655 }
24e08e33
FB
656 }
657 }
5dd9d823
FB
658 }
659
784745ce
FB
660 private function buildQuery()
661 {
d865c296
FB
662 if (is_null($this->orderby)) {
663 $orders = array();
664 foreach ($this->sort as $sort) {
665 $orders = array_merge($orders, $sort->buildSort($this));
666 }
667 if (count($orders) == 0) {
668 $this->orderby = '';
669 } else {
670 $this->orderby = 'ORDER BY ' . implode(', ', $orders);
671 }
672 }
784745ce
FB
673 if (is_null($this->query)) {
674 $where = $this->root->buildCondition($this);
675 $joins = $this->buildJoins();
676 $this->query = 'FROM accounts AS a
677 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
678 INNER JOIN profiles AS p ON (p.pid = ap.pid)
679 ' . $joins . '
680 WHERE (' . $where . ')';
681 }
682 }
683
684 private function formatJoin(array $joins)
685 {
686 $str = '';
687 foreach ($joins as $key => $infos) {
688 $mode = $infos[0];
689 $table = $infos[1];
690 if ($mode == 'inner') {
691 $str .= 'INNER JOIN ';
692 } else if ($mode == 'left') {
693 $str .= 'LEFT JOIN ';
694 } else {
695 Platal::page()->kill("Join mode error");
696 }
697 $str .= $table . ' AS ' . $key;
698 if (isset($infos[2])) {
4927ee54 699 $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')';
784745ce
FB
700 }
701 $str .= "\n";
702 }
703 return $str;
704 }
705
706 private function buildJoins()
707 {
d865c296
FB
708 $joins = array();
709 foreach (self::$joinMethods as $method) {
710 $joins = array_merge($joins, $this->$method());
711 }
784745ce
FB
712 return $this->formatJoin($joins);
713 }
a087cc8d 714
d865c296
FB
715 private function getUIDList($uids = null, $count = null, $offset = null)
716 {
717 $this->buildQuery();
718 $limit = '';
719 if (!is_null($count)) {
720 if (!is_null($offset)) {
721 $limit = XDB::format('LIMIT {?}, {?}', $offset, $count);
722 } else {
723 $limit = XDB::format('LIMIT {?}', $count);
724 }
725 }
726 $cond = '';
727 if (!is_null($uids)) {
07eb5b0e 728 $cond = ' AND a.uid IN ' . XDB::formatArray($uids);
d865c296
FB
729 }
730 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
731 ' . $this->query . $cond . '
732 GROUP BY a.uid
733 ' . $this->orderby . '
734 ' . $limit);
735 $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
736 return $fetched;
737 }
738
a087cc8d
FB
739 /** Check that the user match the given rule.
740 */
741 public function checkUser(PlUser &$user)
742 {
784745ce
FB
743 $this->buildQuery();
744 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
745 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
746 return $count == 1;
a087cc8d
FB
747 }
748
749 /** Filter a list of user to extract the users matching the rule.
750 */
d865c296 751 public function filter(array $users, $count = null, $offset = null)
a087cc8d 752 {
4927ee54
FB
753 $this->buildQuery();
754 $table = array();
755 $uids = array();
756 foreach ($users as $user) {
07eb5b0e
FB
757 if ($user instanceof PlUser) {
758 $uid = $user->id();
759 } else {
760 $uid = $user;
761 }
762 $uids[] = $uid;
763 $table[$uid] = $user;
4927ee54 764 }
d865c296 765 $fetched = $this->getUIDList($uids, $count, $offset);
a087cc8d 766 $output = array();
4927ee54
FB
767 foreach ($fetched as $uid) {
768 $output[] = $table[$uid];
a087cc8d
FB
769 }
770 return $output;
771 }
772
d865c296 773 public function getUIDs($count = null, $offset = null)
4927ee54 774 {
d865c296
FB
775 return $this->getUIDList(null, $count, $offset);
776 }
777
778 public function getUsers($count = null, $offset = null)
779 {
780 return User::getBulkUsersWithUIDs($this->getUIDs($count, $offset));
4927ee54
FB
781 }
782
d865c296 783 public function getTotalCount()
4927ee54 784 {
38c6fe96 785 if (is_null($this->lastcount)) {
aa21c568 786 $this->buildQuery();
7e735012
FB
787 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
788 ' . $this->query);
38c6fe96
FB
789 } else {
790 return $this->lastcount;
791 }
4927ee54
FB
792 }
793
a087cc8d
FB
794 public function setCondition(UserFilterCondition &$cond)
795 {
796 $this->root =& $cond;
784745ce 797 $this->query = null;
a087cc8d
FB
798 }
799
24e08e33
FB
800 public function addSort(UserFilterOrder &$sort)
801 {
d865c296
FB
802 $this->sort[] = $sort;
803 $this->orderby = null;
24e08e33
FB
804 }
805
a087cc8d
FB
806 static public function getLegacy($promo_min, $promo_max)
807 {
a087cc8d 808 if ($promo_min != 0) {
784745ce 809 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
5dd9d823
FB
810 } else {
811 $min = new UFC_True();
a087cc8d 812 }
a087cc8d 813 if ($promo_max != 0) {
784745ce 814 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
a087cc8d 815 } else {
5dd9d823 816 $max = new UFC_True();
a087cc8d 817 }
5dd9d823 818 return new UserFilter(new UFC_And($min, $max));
a087cc8d 819 }
784745ce 820
07eb5b0e
FB
821 static public function sortByName()
822 {
823 return array(new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
824 }
825
826 static public function sortByPromo()
827 {
828 return array(new UFO_Promo(), new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
829 }
830
aa21c568
FB
831 static private function getDBSuffix($string)
832 {
833 return preg_replace('/[^a-z0-9]/i', '', $string);
834 }
835
836
837 private $option = 0;
838 private function register_optional(array &$table, $val)
839 {
840 if (is_null($val)) {
841 $sub = $this->option++;
842 $index = null;
843 } else {
844 $sub = self::getDBSuffix($val);
845 $index = $val;
846 }
847 $sub = '_' . $sub;
848 $table[$sub] = $index;
849 return $sub;
850 }
784745ce 851
d865c296
FB
852 /** DISPLAY
853 */
38c6fe96 854 const DISPLAY = 'display';
d865c296
FB
855 private $pd = false;
856 public function addDisplayFilter()
857 {
858 $this->pd = true;
859 return '';
860 }
861
862 private function displayJoins()
863 {
864 if ($this->pd) {
865 return array('pd' => array('left', 'profile_display', '$ME.pid = $PID'));
866 } else {
867 return array();
868 }
869 }
870
784745ce
FB
871 /** NAMES
872 */
d865c296 873 /* name tokens */
784745ce
FB
874 const LASTNAME = 'lastname';
875 const FIRSTNAME = 'firstname';
876 const NICKNAME = 'nickname';
877 const PSEUDONYM = 'pseudonym';
878 const NAME = 'name';
d865c296 879 /* name variants */
784745ce
FB
880 const VN_MARITAL = 'marital';
881 const VN_ORDINARY = 'ordinary';
882 const VN_OTHER = 'other';
883 const VN_INI = 'ini';
d865c296
FB
884 /* display names */
885 const DN_FULL = 'directory_name';
886 const DN_DISPLAY = 'yourself';
887 const DN_YOURSELF = 'yourself';
888 const DN_DIRECTORY = 'directory_name';
889 const DN_PRIVATE = 'private_name';
890 const DN_PUBLIC = 'public_name';
891 const DN_SHORT = 'short_name';
892 const DN_SORT = 'sort_name';
784745ce
FB
893
894 static public $name_variants = array(
895 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
d865c296
FB
896 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
897 );
784745ce
FB
898
899 static public function assertName($name)
900 {
901 if (!Profile::getNameTypeId($name)) {
902 Platal::page()->kill('Invalid name type');
903 }
904 }
905
d865c296
FB
906 static public function isDisplayName($name)
907 {
908 return $name == self::DN_FULL || $name == self::DN_DISPLAY
909 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
910 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
911 || $name == self::DN_SHORT || $name == self::DN_SORT;
912 }
913
784745ce 914 private $pn = array();
784745ce
FB
915 public function addNameFilter($type, $variant = null)
916 {
917 if (!is_null($variant)) {
918 $ft = $type . '_' . $variant;
919 } else {
920 $ft = $type;
921 }
922 $sub = '_' . $ft;
923 self::assertName($ft);
924
925 if (!is_null($variant) && $variant == 'other') {
aa21c568 926 $sub .= $this->option++;
784745ce
FB
927 }
928 $this->pn[$sub] = Profile::getNameTypeId($ft);
929 return $sub;
930 }
931
932 private function nameJoins()
933 {
934 $joins = array();
935 foreach ($this->pn as $sub => $type) {
936 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
937 }
938 return $joins;
939 }
940
941
942 /** EDUCATION
943 */
944 const GRADE_ING = 'Ing.';
945 const GRADE_PHD = 'PhD';
946 const GRADE_MST = 'M%';
947 static public function isGrade($grade)
948 {
38c6fe96 949 return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST;
784745ce
FB
950 }
951
952 static public function assertGrade($grade)
953 {
954 if (!self::isGrade($grade)) {
955 Platal::page()->killError("DiplĂ´me non valide");
956 }
957 }
958
d865c296
FB
959 static public function promoYear($grade)
960 {
961 // XXX: Definition of promotion for phds and masters might change in near future.
962 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
963 }
964
784745ce
FB
965 private $pepe = array();
966 private $with_pee = false;
784745ce
FB
967 public function addEducationFilter($x = false, $grade = null)
968 {
969 if (!$x) {
aa21c568
FB
970 $index = $this->option;
971 $sub = $this->option++;
784745ce
FB
972 } else {
973 self::assertGrade($grade);
974 $index = $grade;
975 $sub = $grade[0];
976 $this->with_pee = true;
977 }
978 $sub = '_' . $sub;
979 $this->pepe[$index] = $sub;
980 return $sub;
981 }
982
983 private function educationJoins()
984 {
985 $joins = array();
986 if ($this->with_pee) {
987 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
988 }
989 foreach ($this->pepe as $grade => $sub) {
990 if ($this->isGrade($grade)) {
991 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
992 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
993 XDB::format('{?}', $grade));
994 } else {
995 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID');
996 $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
997 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
998 }
999 }
1000 return $joins;
1001 }
4927ee54
FB
1002
1003
1004 /** GROUPS
1005 */
1006 private $gpm = array();
4927ee54
FB
1007 public function addGroupFilter($group = null)
1008 {
1009 if (!is_null($group)) {
1010 if (ctype_digit($group)) {
1011 $index = $sub = $group;
1012 } else {
1013 $index = $group;
aa21c568 1014 $sub = self::getDBSuffix($group);
4927ee54
FB
1015 }
1016 } else {
aa21c568 1017 $sub = 'group_' . $this->option++;
4927ee54
FB
1018 $index = null;
1019 }
1020 $sub = '_' . $sub;
1021 $this->gpm[$sub] = $index;
1022 return $sub;
1023 }
1024
1025 private function groupJoins()
1026 {
1027 $joins = array();
1028 foreach ($this->gpm as $sub => $key) {
1029 if (is_null($key)) {
1030 $joins['gpa' . $sub] = array('inner', 'groupex.asso');
1031 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1032 } else if (ctype_digit($key)) {
1033 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
1034 } else {
1035 $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key));
1036 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1037 }
1038 }
1039 return $joins;
1040 }
aa21c568
FB
1041
1042 /** EMAILS
1043 */
1044 private $e = array();
1045 public function addEmailRedirectFilter($email = null)
1046 {
1047 return $this->register_optional($this->e, $email);
1048 }
1049
1050 private $ve = array();
1051 public function addVirtualEmailFilter($email = null)
1052 {
21401768 1053 $this->addAliasFilter(self::ALIAS_FORLIFE);
aa21c568
FB
1054 return $this->register_optional($this->ve, $email);
1055 }
1056
21401768
FB
1057 const ALIAS_BEST = 'bestalias';
1058 const ALIAS_FORLIFE = 'forlife';
aa21c568
FB
1059 private $al = array();
1060 public function addAliasFilter($alias = null)
1061 {
1062 return $this->register_optional($this->al, $alias);
1063 }
1064
1065 private function emailJoins()
1066 {
1067 global $globals;
1068 $joins = array();
1069 foreach ($this->e as $sub=>$key) {
1070 if (is_null($key)) {
1071 $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
1072 } else {
1073 $joins['e' . $sub] = array('left', 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
1074 }
1075 }
21401768 1076 foreach ($this->al as $sub=>$key) {
aa21c568 1077 if (is_null($key)) {
21401768
FB
1078 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
1079 } else if ($key == self::ALIAS_BEST) {
1080 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
1081 } else if ($key == self::ALIAS_FORLIFE) {
1082 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
aa21c568 1083 } else {
21401768 1084 $joins['al' . $sub] = array('left', 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
aa21c568 1085 }
aa21c568 1086 }
21401768 1087 foreach ($this->ve as $sub=>$key) {
aa21c568 1088 if (is_null($key)) {
21401768 1089 $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\'');
aa21c568 1090 } else {
21401768 1091 $joins['v' . $sub] = array('left', 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
aa21c568 1092 }
21401768
FB
1093 $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid
1094 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
1095 CONCAT(al_forlife.alias, \'@\', {?}),
1096 a.email))',
1097 $globals->mail->domain, $globals->mail->domain2));
aa21c568
FB
1098 }
1099 return $joins;
1100 }
3f42a6ad
FB
1101
1102
1103 /** CONTACTS
1104 */
1105 private $cts = array();
1106 public function addContactFilter($uid = null)
1107 {
1108 return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid);
1109 }
1110
1111 private function contactJoins()
1112 {
1113 $joins = array();
1114 foreach ($this->cts as $sub=>$key) {
1115 if (is_null($key)) {
1116 $joins['c' . $sub] = array('left', 'contacts', '$ME.contact = $UID');
1117 } else {
1118 $joins['c' . $sub] = array('left', 'contacts', XDB::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
1119 }
1120 }
1121 return $joins;
1122 }
4e7bf1e0
FB
1123
1124
1125 /** CARNET
1126 */
1127 private $wn = array();
1128 public function addWatchRegistrationFilter($uid = null)
1129 {
1130 return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid);
1131 }
1132
1133 private $wp = array();
1134 public function addWatchPromoFilter($uid = null)
1135 {
1136 return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid);
1137 }
1138
1139 private $w = array();
1140 public function addWatchFilter($uid = null)
1141 {
1142 return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid);
1143 }
1144
1145 private function watchJoins()
1146 {
1147 $joins = array();
1148 foreach ($this->w as $sub=>$key) {
1149 if (is_null($key)) {
1150 $joins['w' . $sub] = array('left', 'watch');
1151 } else {
1152 $joins['w' . $sub] = array('left', 'watch', XDB::format('$ME.uid = {?}', substr($key, 5)));
1153 }
1154 }
1155 foreach ($this->wn as $sub=>$key) {
1156 if (is_null($key)) {
1157 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1158 } else {
1159 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1160 }
1161 }
1162 foreach ($this->wn as $sub=>$key) {
1163 if (is_null($key)) {
1164 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1165 } else {
1166 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1167 }
1168 }
1169 foreach ($this->wp as $sub=>$key) {
1170 if (is_null($key)) {
1171 $joins['wp' . $sub] = array('left', 'watch_promo');
1172 } else {
1173 $joins['wp' . $sub] = array('left', 'watch_promo', XDB::format('$ME.uid = {?}', substr($key, 5)));
1174 }
1175 }
1176 return $joins;
1177 }
a087cc8d
FB
1178}
1179
3f42a6ad 1180
a087cc8d
FB
1181// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1182?>