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