Port xnetevents (I hope this works).
[platal.git] / classes / userfilter.php
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
22
23 /******************
24 * CONDITIONS
25 ******************/
26
27 interface UserFilterCondition
28 {
29 const COND_TRUE = 'TRUE';
30 const COND_FALSE = 'FALSE';
31
32 /** Check that the given user matches the rule.
33 */
34 public function buildCondition(UserFilter &$uf);
35 }
36
37 abstract class UFC_OneChild implements UserFilterCondition
38 {
39 protected $child;
40
41 public function __construct($child = null)
42 {
43 if (!is_null($child) && ($child instanceof UserFilterCondition)) {
44 $this->setChild($child);
45 }
46 }
47
48 public function setChild(UserFilterCondition &$cond)
49 {
50 $this->child =& $cond;
51 }
52 }
53
54 abstract class UFC_NChildren implements UserFilterCondition
55 {
56 protected $children = array();
57
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
68 public function addChild(UserFilterCondition &$cond)
69 {
70 $this->children[] =& $cond;
71 }
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 {
80 return '(' . implode(') ' . $op . ' (', $cond) . ')';
81 }
82 }
83 }
84
85 class UFC_True implements UserFilterCondition
86 {
87 public function buildCondition(UserFilter &$uf)
88 {
89 return self::COND_TRUE;
90 }
91 }
92
93 class UFC_False implements UserFilterCondition
94 {
95 public function buildCondition(UserFilter &$uf)
96 {
97 return self::COND_FALSE;
98 }
99 }
100
101 class UFC_Not extends UFC_OneChild
102 {
103 public function buildCondition(UserFilter &$uf)
104 {
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 }
113 }
114 }
115
116 class UFC_And extends UFC_NChildren
117 {
118 public function buildCondition(UserFilter &$uf)
119 {
120 if (empty($this->children)) {
121 return self::COND_FALSE;
122 } else {
123 $true = self::COND_FALSE;
124 $conds = array();
125 foreach ($this->children as &$child) {
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 }
134 }
135 return $this->catConds($conds, 'AND', $true);
136 }
137 }
138 }
139
140 class UFC_Or extends UFC_NChildren
141 {
142 public function buildCondition(UserFilter &$uf)
143 {
144 if (empty($this->children)) {
145 return self::COND_TRUE;
146 } else {
147 $true = self::COND_TRUE;
148 $conds = array();
149 foreach ($this->children as &$child) {
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 }
158 }
159 return $this->catConds($conds, 'OR', $true);
160 }
161 }
162 }
163
164 class UFC_Promo implements UserFilterCondition
165 {
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;
176 if ($this->grade != UserFilter::DISPLAY) {
177 UserFilter::assertGrade($this->grade);
178 }
179 }
180
181 public function buildCondition(UserFilter &$uf)
182 {
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 }
191 }
192 }
193
194 class 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));
238 if (($this->mode & self::VARIANTS) != 0 && isset(UserFilter::$name_variants[$this->type])) {
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);
244 }
245 }
246
247 class UFC_Dead implements UserFilterCondition
248 {
249 private $comparison;
250 private $date;
251
252 public function __construct($comparison = null, $date = null)
253 {
254 $this->comparison = $comparison;
255 $this->date = $date;
256 }
257
258 public function buildCondition(UserFilter &$uf)
259 {
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));
263 }
264 return $str;
265 }
266 }
267
268 class UFC_Registered implements UserFilterCondition
269 {
270 private $active;
271 private $comparison;
272 private $date;
273
274 public function __construct($active = false, $comparison = null, $date = null)
275 {
276 $this->only_active = $active;
277 $this->comparison = $comparison;
278 $this->date = $date;
279 }
280
281 public function buildCondition(UserFilter &$uf)
282 {
283 if ($this->active) {
284 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
285 } else {
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));
290 }
291 return $date;
292 }
293 }
294
295 class 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
312 class 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
329 class 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 {
342 return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male');
343 }
344 }
345 }
346
347 class 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
368 class 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);
381 } else if (User::isVirtualEmailAddress($this->email)) {
382 $sub = $uf->addVirtualEmailFilter($this->email);
383 return 'vr' . $sub . '.redirect IS NOT NULL';
384 } else {
385 @list($user, $domain) = explode('@', $this->email);
386 $sub = $uf->addAliasFilter($user);
387 return 'al' . $sub . '.alias IS NOT NULL';
388 }
389 }
390 }
391
392 class 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 }
433
434 abstract class UFC_UserRelated implements UserFilterCondition
435 {
436 protected $user;
437 public function __construct(PlUser &$user)
438 {
439 $this->user =& $user;
440 }
441 }
442
443 class UFC_Contact extends UFC_UserRelated
444 {
445 public function buildCondition(UserFilter &$uf)
446 {
447 $sub = $uf->addContactFilter($this->user->id());
448 return 'c' . $sub . '.contact IS NOT NULL';
449 }
450 }
451
452 class UFC_WatchRegistration extends UFC_UserRelated
453 {
454 public function buildCondition(UserFilter &$uf)
455 {
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 ' . XDB::formatArray($uids);
464 }
465 }
466 }
467
468 class UFC_WatchPromo extends UFC_UserRelated
469 {
470 private $grade;
471 public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING)
472 {
473 parent::__construct($user);
474 $this->grade = $grade;
475 }
476
477 public function buildCondition(UserFilter &$uf)
478 {
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 ' . XDB::formatArray($promos);
486 }
487 }
488 }
489
490 class UFC_WatchContact extends UFC_Contact
491 {
492 public function buildCondition(UserFilter &$uf)
493 {
494 if (!$this->user->watchContacts()) {
495 return UserFilterCondition::COND_FALSE;
496 }
497 return parent::buildCondition($uf);
498 }
499 }
500
501
502 /******************
503 * ORDERS
504 ******************/
505
506 abstract class UserFilterOrder
507 {
508 protected $desc = false;
509 public function __construct($desc = false)
510 {
511 $this->desc = $desc;
512 }
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
531 class UFO_Promo extends UserFilterOrder
532 {
533 private $grade;
534
535 public function __construct($grade = null, $desc = false)
536 {
537 parent::__construct($desc);
538 $this->grade = $grade;
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
553 class 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 {
561 parent::__construct($desc);
562 $this->type = $type;
563 $this->variant = $variant;
564 $this->particle = $particle;
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
583 class UFO_Registration extends UserFilterOrder
584 {
585 protected function getSortTokens(UserFilter &$uf)
586 {
587 return 'a.registration_date';
588 }
589 }
590
591 class UFO_Birthday extends UserFilterOrder
592 {
593 protected function getSortTokens(UserFilter &$uf)
594 {
595 return 'p.next_birthday';
596 }
597 }
598
599 class UFO_ProfileUpdate extends UserFilterOrder
600 {
601 protected function getSortTokens(UserFilter &$uf)
602 {
603 return 'p.last_change';
604 }
605 }
606
607 class UFO_Death extends UserFilterOrder
608 {
609 protected function getSortTokens(UserFilter &$uf)
610 {
611 return 'p.deathdate';
612 }
613 }
614
615
616 /***********************************
617 *********************************
618 USER FILTER CLASS
619 *********************************
620 ***********************************/
621
622 class UserFilter
623 {
624 static private $joinMethods = array();
625
626 private $root;
627 private $sort = array();
628 private $query = null;
629 private $orderby = null;
630
631 private $lastcount = null;
632
633 public function __construct($cond = null, $sort = null)
634 {
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 }
644 if (!is_null($cond)) {
645 if ($cond instanceof UserFilterCondition) {
646 $this->setCondition($cond);
647 }
648 }
649 if (!is_null($sort)) {
650 if ($sort instanceof UserFilterOrder) {
651 $this->addSort($sort);
652 } else if (is_array($sort)) {
653 foreach ($sort as $s) {
654 $this->addSort($s);
655 }
656 }
657 }
658 }
659
660 private function buildQuery()
661 {
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 }
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])) {
699 $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')';
700 }
701 $str .= "\n";
702 }
703 return $str;
704 }
705
706 private function buildJoins()
707 {
708 $joins = array();
709 foreach (self::$joinMethods as $method) {
710 $joins = array_merge($joins, $this->$method());
711 }
712 return $this->formatJoin($joins);
713 }
714
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 ' . XDB::formatArray($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
739 /** Check that the user match the given rule.
740 */
741 public function checkUser(PlUser &$user)
742 {
743 $this->buildQuery();
744 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
745 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
746 return $count == 1;
747 }
748
749 /** Filter a list of user to extract the users matching the rule.
750 */
751 public function filter(array $users, $count = null, $offset = null)
752 {
753 $this->buildQuery();
754 $table = array();
755 $uids = array();
756 foreach ($users as $user) {
757 if ($user instanceof PlUser) {
758 $uid = $user->id();
759 } else {
760 $uid = $user;
761 }
762 $uids[] = $uid;
763 $table[$uid] = $user;
764 }
765 $fetched = $this->getUIDList($uids, $count, $offset);
766 $output = array();
767 foreach ($fetched as $uid) {
768 $output[] = $table[$uid];
769 }
770 return $output;
771 }
772
773 public function getUIDs($count = null, $offset = null)
774 {
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));
781 }
782
783 public function getTotalCount()
784 {
785 if (is_null($this->lastcount)) {
786 $this->buildQuery();
787 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
788 ' . $this->query);
789 } else {
790 return $this->lastcount;
791 }
792 }
793
794 public function setCondition(UserFilterCondition &$cond)
795 {
796 $this->root =& $cond;
797 $this->query = null;
798 }
799
800 public function addSort(UserFilterOrder &$sort)
801 {
802 $this->sort[] = $sort;
803 $this->orderby = null;
804 }
805
806 static public function getLegacy($promo_min, $promo_max)
807 {
808 if ($promo_min != 0) {
809 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
810 } else {
811 $min = new UFC_True();
812 }
813 if ($promo_max != 0) {
814 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
815 } else {
816 $max = new UFC_True();
817 }
818 return new UserFilter(new UFC_And($min, $max));
819 }
820
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
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 }
851
852 /** DISPLAY
853 */
854 const DISPLAY = 'display';
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
871 /** NAMES
872 */
873 /* name tokens */
874 const LASTNAME = 'lastname';
875 const FIRSTNAME = 'firstname';
876 const NICKNAME = 'nickname';
877 const PSEUDONYM = 'pseudonym';
878 const NAME = 'name';
879 /* name variants */
880 const VN_MARITAL = 'marital';
881 const VN_ORDINARY = 'ordinary';
882 const VN_OTHER = 'other';
883 const VN_INI = 'ini';
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';
893
894 static public $name_variants = array(
895 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
896 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
897 );
898
899 static public function assertName($name)
900 {
901 if (!Profile::getNameTypeId($name)) {
902 Platal::page()->kill('Invalid name type');
903 }
904 }
905
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
914 private $pn = array();
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') {
926 $sub .= $this->option++;
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 {
949 return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST;
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
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
965 private $pepe = array();
966 private $with_pee = false;
967 public function addEducationFilter($x = false, $grade = null)
968 {
969 if (!$x) {
970 $index = $this->option;
971 $sub = $this->option++;
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 }
1002
1003
1004 /** GROUPS
1005 */
1006 private $gpm = array();
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;
1014 $sub = self::getDBSuffix($group);
1015 }
1016 } else {
1017 $sub = 'group_' . $this->option++;
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 }
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 {
1053 $this->addAliasFilter(self::ALIAS_FORLIFE);
1054 return $this->register_optional($this->ve, $email);
1055 }
1056
1057 const ALIAS_BEST = 'bestalias';
1058 const ALIAS_FORLIFE = 'forlife';
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 }
1076 foreach ($this->al as $sub=>$key) {
1077 if (is_null($key)) {
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\'');
1083 } else {
1084 $joins['al' . $sub] = array('left', 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
1085 }
1086 }
1087 foreach ($this->ve as $sub=>$key) {
1088 if (is_null($key)) {
1089 $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\'');
1090 } else {
1091 $joins['v' . $sub] = array('left', 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
1092 }
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));
1098 }
1099 return $joins;
1100 }
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 }
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 }
1178 }
1179
1180
1181 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1182 ?>