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