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