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