No more auth_user in stats.
[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 {
463 return '$UID IN (' . implode(', ', $uids) . ')';
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);
485 return $field . ' IN (' . implode(', ', $promos) . ')';
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)) {
728 $cond = ' AND a.uid IN (' . implode(', ', $uids) . ')';
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) {
757 $uids[] = $user->id();
758 $table[$user->id()] = $user;
759 }
d865c296 760 $fetched = $this->getUIDList($uids, $count, $offset);
a087cc8d 761 $output = array();
4927ee54
FB
762 foreach ($fetched as $uid) {
763 $output[] = $table[$uid];
a087cc8d
FB
764 }
765 return $output;
766 }
767
d865c296 768 public function getUIDs($count = null, $offset = null)
4927ee54 769 {
d865c296
FB
770 return $this->getUIDList(null, $count, $offset);
771 }
772
773 public function getUsers($count = null, $offset = null)
774 {
775 return User::getBulkUsersWithUIDs($this->getUIDs($count, $offset));
4927ee54
FB
776 }
777
d865c296 778 public function getTotalCount()
4927ee54 779 {
38c6fe96 780 if (is_null($this->lastcount)) {
aa21c568 781 $this->buildQuery();
7e735012
FB
782 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
783 ' . $this->query);
38c6fe96
FB
784 } else {
785 return $this->lastcount;
786 }
4927ee54
FB
787 }
788
a087cc8d
FB
789 public function setCondition(UserFilterCondition &$cond)
790 {
791 $this->root =& $cond;
784745ce 792 $this->query = null;
a087cc8d
FB
793 }
794
24e08e33
FB
795 public function addSort(UserFilterOrder &$sort)
796 {
d865c296
FB
797 $this->sort[] = $sort;
798 $this->orderby = null;
24e08e33
FB
799 }
800
a087cc8d
FB
801 static public function getLegacy($promo_min, $promo_max)
802 {
a087cc8d 803 if ($promo_min != 0) {
784745ce 804 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
5dd9d823
FB
805 } else {
806 $min = new UFC_True();
a087cc8d 807 }
a087cc8d 808 if ($promo_max != 0) {
784745ce 809 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
a087cc8d 810 } else {
5dd9d823 811 $max = new UFC_True();
a087cc8d 812 }
5dd9d823 813 return new UserFilter(new UFC_And($min, $max));
a087cc8d 814 }
784745ce 815
aa21c568
FB
816 static private function getDBSuffix($string)
817 {
818 return preg_replace('/[^a-z0-9]/i', '', $string);
819 }
820
821
822 private $option = 0;
823 private function register_optional(array &$table, $val)
824 {
825 if (is_null($val)) {
826 $sub = $this->option++;
827 $index = null;
828 } else {
829 $sub = self::getDBSuffix($val);
830 $index = $val;
831 }
832 $sub = '_' . $sub;
833 $table[$sub] = $index;
834 return $sub;
835 }
784745ce 836
d865c296
FB
837 /** DISPLAY
838 */
38c6fe96 839 const DISPLAY = 'display';
d865c296
FB
840 private $pd = false;
841 public function addDisplayFilter()
842 {
843 $this->pd = true;
844 return '';
845 }
846
847 private function displayJoins()
848 {
849 if ($this->pd) {
850 return array('pd' => array('left', 'profile_display', '$ME.pid = $PID'));
851 } else {
852 return array();
853 }
854 }
855
784745ce
FB
856 /** NAMES
857 */
d865c296 858 /* name tokens */
784745ce
FB
859 const LASTNAME = 'lastname';
860 const FIRSTNAME = 'firstname';
861 const NICKNAME = 'nickname';
862 const PSEUDONYM = 'pseudonym';
863 const NAME = 'name';
d865c296 864 /* name variants */
784745ce
FB
865 const VN_MARITAL = 'marital';
866 const VN_ORDINARY = 'ordinary';
867 const VN_OTHER = 'other';
868 const VN_INI = 'ini';
d865c296
FB
869 /* display names */
870 const DN_FULL = 'directory_name';
871 const DN_DISPLAY = 'yourself';
872 const DN_YOURSELF = 'yourself';
873 const DN_DIRECTORY = 'directory_name';
874 const DN_PRIVATE = 'private_name';
875 const DN_PUBLIC = 'public_name';
876 const DN_SHORT = 'short_name';
877 const DN_SORT = 'sort_name';
784745ce
FB
878
879 static public $name_variants = array(
880 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
d865c296
FB
881 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
882 );
784745ce
FB
883
884 static public function assertName($name)
885 {
886 if (!Profile::getNameTypeId($name)) {
887 Platal::page()->kill('Invalid name type');
888 }
889 }
890
d865c296
FB
891 static public function isDisplayName($name)
892 {
893 return $name == self::DN_FULL || $name == self::DN_DISPLAY
894 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
895 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
896 || $name == self::DN_SHORT || $name == self::DN_SORT;
897 }
898
784745ce 899 private $pn = array();
784745ce
FB
900 public function addNameFilter($type, $variant = null)
901 {
902 if (!is_null($variant)) {
903 $ft = $type . '_' . $variant;
904 } else {
905 $ft = $type;
906 }
907 $sub = '_' . $ft;
908 self::assertName($ft);
909
910 if (!is_null($variant) && $variant == 'other') {
aa21c568 911 $sub .= $this->option++;
784745ce
FB
912 }
913 $this->pn[$sub] = Profile::getNameTypeId($ft);
914 return $sub;
915 }
916
917 private function nameJoins()
918 {
919 $joins = array();
920 foreach ($this->pn as $sub => $type) {
921 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
922 }
923 return $joins;
924 }
925
926
927 /** EDUCATION
928 */
929 const GRADE_ING = 'Ing.';
930 const GRADE_PHD = 'PhD';
931 const GRADE_MST = 'M%';
932 static public function isGrade($grade)
933 {
38c6fe96 934 return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST;
784745ce
FB
935 }
936
937 static public function assertGrade($grade)
938 {
939 if (!self::isGrade($grade)) {
940 Platal::page()->killError("DiplĂ´me non valide");
941 }
942 }
943
d865c296
FB
944 static public function promoYear($grade)
945 {
946 // XXX: Definition of promotion for phds and masters might change in near future.
947 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
948 }
949
784745ce
FB
950 private $pepe = array();
951 private $with_pee = false;
784745ce
FB
952 public function addEducationFilter($x = false, $grade = null)
953 {
954 if (!$x) {
aa21c568
FB
955 $index = $this->option;
956 $sub = $this->option++;
784745ce
FB
957 } else {
958 self::assertGrade($grade);
959 $index = $grade;
960 $sub = $grade[0];
961 $this->with_pee = true;
962 }
963 $sub = '_' . $sub;
964 $this->pepe[$index] = $sub;
965 return $sub;
966 }
967
968 private function educationJoins()
969 {
970 $joins = array();
971 if ($this->with_pee) {
972 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
973 }
974 foreach ($this->pepe as $grade => $sub) {
975 if ($this->isGrade($grade)) {
976 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
977 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
978 XDB::format('{?}', $grade));
979 } else {
980 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID');
981 $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
982 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
983 }
984 }
985 return $joins;
986 }
4927ee54
FB
987
988
989 /** GROUPS
990 */
991 private $gpm = array();
4927ee54
FB
992 public function addGroupFilter($group = null)
993 {
994 if (!is_null($group)) {
995 if (ctype_digit($group)) {
996 $index = $sub = $group;
997 } else {
998 $index = $group;
aa21c568 999 $sub = self::getDBSuffix($group);
4927ee54
FB
1000 }
1001 } else {
aa21c568 1002 $sub = 'group_' . $this->option++;
4927ee54
FB
1003 $index = null;
1004 }
1005 $sub = '_' . $sub;
1006 $this->gpm[$sub] = $index;
1007 return $sub;
1008 }
1009
1010 private function groupJoins()
1011 {
1012 $joins = array();
1013 foreach ($this->gpm as $sub => $key) {
1014 if (is_null($key)) {
1015 $joins['gpa' . $sub] = array('inner', 'groupex.asso');
1016 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1017 } else if (ctype_digit($key)) {
1018 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
1019 } else {
1020 $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key));
1021 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
1022 }
1023 }
1024 return $joins;
1025 }
aa21c568
FB
1026
1027 /** EMAILS
1028 */
1029 private $e = array();
1030 public function addEmailRedirectFilter($email = null)
1031 {
1032 return $this->register_optional($this->e, $email);
1033 }
1034
1035 private $ve = array();
1036 public function addVirtualEmailFilter($email = null)
1037 {
21401768 1038 $this->addAliasFilter(self::ALIAS_FORLIFE);
aa21c568
FB
1039 return $this->register_optional($this->ve, $email);
1040 }
1041
21401768
FB
1042 const ALIAS_BEST = 'bestalias';
1043 const ALIAS_FORLIFE = 'forlife';
aa21c568
FB
1044 private $al = array();
1045 public function addAliasFilter($alias = null)
1046 {
1047 return $this->register_optional($this->al, $alias);
1048 }
1049
1050 private function emailJoins()
1051 {
1052 global $globals;
1053 $joins = array();
1054 foreach ($this->e as $sub=>$key) {
1055 if (is_null($key)) {
1056 $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
1057 } else {
1058 $joins['e' . $sub] = array('left', 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
1059 }
1060 }
21401768 1061 foreach ($this->al as $sub=>$key) {
aa21c568 1062 if (is_null($key)) {
21401768
FB
1063 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
1064 } else if ($key == self::ALIAS_BEST) {
1065 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
1066 } else if ($key == self::ALIAS_FORLIFE) {
1067 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
aa21c568 1068 } else {
21401768 1069 $joins['al' . $sub] = array('left', 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
aa21c568 1070 }
aa21c568 1071 }
21401768 1072 foreach ($this->ve as $sub=>$key) {
aa21c568 1073 if (is_null($key)) {
21401768 1074 $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\'');
aa21c568 1075 } else {
21401768 1076 $joins['v' . $sub] = array('left', 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
aa21c568 1077 }
21401768
FB
1078 $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid
1079 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
1080 CONCAT(al_forlife.alias, \'@\', {?}),
1081 a.email))',
1082 $globals->mail->domain, $globals->mail->domain2));
aa21c568
FB
1083 }
1084 return $joins;
1085 }
3f42a6ad
FB
1086
1087
1088 /** CONTACTS
1089 */
1090 private $cts = array();
1091 public function addContactFilter($uid = null)
1092 {
1093 return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid);
1094 }
1095
1096 private function contactJoins()
1097 {
1098 $joins = array();
1099 foreach ($this->cts as $sub=>$key) {
1100 if (is_null($key)) {
1101 $joins['c' . $sub] = array('left', 'contacts', '$ME.contact = $UID');
1102 } else {
1103 $joins['c' . $sub] = array('left', 'contacts', XDB::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5)));
1104 }
1105 }
1106 return $joins;
1107 }
4e7bf1e0
FB
1108
1109
1110 /** CARNET
1111 */
1112 private $wn = array();
1113 public function addWatchRegistrationFilter($uid = null)
1114 {
1115 return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid);
1116 }
1117
1118 private $wp = array();
1119 public function addWatchPromoFilter($uid = null)
1120 {
1121 return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid);
1122 }
1123
1124 private $w = array();
1125 public function addWatchFilter($uid = null)
1126 {
1127 return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid);
1128 }
1129
1130 private function watchJoins()
1131 {
1132 $joins = array();
1133 foreach ($this->w as $sub=>$key) {
1134 if (is_null($key)) {
1135 $joins['w' . $sub] = array('left', 'watch');
1136 } else {
1137 $joins['w' . $sub] = array('left', 'watch', XDB::format('$ME.uid = {?}', substr($key, 5)));
1138 }
1139 }
1140 foreach ($this->wn as $sub=>$key) {
1141 if (is_null($key)) {
1142 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1143 } else {
1144 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1145 }
1146 }
1147 foreach ($this->wn as $sub=>$key) {
1148 if (is_null($key)) {
1149 $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID');
1150 } else {
1151 $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5)));
1152 }
1153 }
1154 foreach ($this->wp as $sub=>$key) {
1155 if (is_null($key)) {
1156 $joins['wp' . $sub] = array('left', 'watch_promo');
1157 } else {
1158 $joins['wp' . $sub] = array('left', 'watch_promo', XDB::format('$ME.uid = {?}', substr($key, 5)));
1159 }
1160 }
1161 return $joins;
1162 }
a087cc8d
FB
1163}
1164
3f42a6ad 1165
a087cc8d
FB
1166// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1167?>