Improves email handling, add support for query on a list of emails.
[platal.git] / classes / userfilter.php
CommitLineData
a087cc8d
FB
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
d865c296
FB
22
23/******************
24 * CONDITIONS
25 ******************/
26
a087cc8d
FB
27interface UserFilterCondition
28{
5dd9d823
FB
29 const COND_TRUE = 'TRUE';
30 const COND_FALSE = 'FALSE';
31
a087cc8d
FB
32 /** Check that the given user matches the rule.
33 */
784745ce 34 public function buildCondition(UserFilter &$uf);
a087cc8d
FB
35}
36
37abstract class UFC_OneChild implements UserFilterCondition
38{
39 protected $child;
40
5dd9d823
FB
41 public function __construct($child = null)
42 {
43 if (!is_null($child) && ($child instanceof UserFilterCondition)) {
44 $this->setChild($child);
45 }
46 }
47
a087cc8d
FB
48 public function setChild(UserFilterCondition &$cond)
49 {
50 $this->child =& $cond;
51 }
52}
53
54abstract class UFC_NChildren implements UserFilterCondition
55{
56 protected $children = array();
57
5dd9d823
FB
58 public function __construct()
59 {
60 $children = func_get_args();
61 foreach ($children as &$child) {
62 if (!is_null($child) && ($child instanceof UserFilterCondition)) {
63 $this->addChild($child);
64 }
65 }
66 }
67
a087cc8d
FB
68 public function addChild(UserFilterCondition &$cond)
69 {
70 $this->children[] =& $cond;
71 }
5dd9d823
FB
72
73 protected function catConds(array $cond, $op, $fallback)
74 {
75 if (count($cond) == 0) {
76 return $fallback;
77 } else if (count($cond) == 1) {
78 return $cond[0];
79 } else {
4927ee54 80 return '(' . implode(') ' . $op . ' (', $cond) . ')';
5dd9d823
FB
81 }
82 }
a087cc8d
FB
83}
84
85class UFC_True implements UserFilterCondition
86{
784745ce 87 public function buildCondition(UserFilter &$uf)
a087cc8d 88 {
5dd9d823 89 return self::COND_TRUE;
a087cc8d
FB
90 }
91}
92
93class UFC_False implements UserFilterCondition
94{
784745ce 95 public function buildCondition(UserFilter &$uf)
a087cc8d 96 {
5dd9d823 97 return self::COND_FALSE;
a087cc8d
FB
98 }
99}
100
101class UFC_Not extends UFC_OneChild
102{
784745ce 103 public function buildCondition(UserFilter &$uf)
a087cc8d 104 {
5dd9d823
FB
105 $val = $this->child->buildCondition($uf);
106 if ($val == self::COND_TRUE) {
107 return self::COND_FALSE;
108 } else if ($val == self::COND_FALSE) {
109 return self::COND_TRUE;
110 } else {
111 return 'NOT (' . $val . ')';
112 }
a087cc8d
FB
113 }
114}
115
116class UFC_And extends UFC_NChildren
117{
784745ce 118 public function buildCondition(UserFilter &$uf)
a087cc8d 119 {
784745ce 120 if (empty($this->children)) {
5dd9d823 121 return self::COND_FALSE;
784745ce 122 } else {
5dd9d823 123 $true = self::COND_FALSE;
784745ce
FB
124 $conds = array();
125 foreach ($this->children as &$child) {
5dd9d823
FB
126 $val = $child->buildCondition($uf);
127 if ($val == self::COND_TRUE) {
128 $true = self::COND_TRUE;
129 } else if ($val == self::COND_FALSE) {
130 return self::COND_FALSE;
131 } else {
132 $conds[] = $val;
133 }
a087cc8d 134 }
5dd9d823 135 return $this->catConds($conds, 'AND', $true);
a087cc8d 136 }
a087cc8d
FB
137 }
138}
139
140class UFC_Or extends UFC_NChildren
141{
784745ce 142 public function buildCondition(UserFilter &$uf)
a087cc8d 143 {
784745ce 144 if (empty($this->children)) {
5dd9d823 145 return self::COND_TRUE;
784745ce 146 } else {
5dd9d823 147 $true = self::COND_TRUE;
784745ce
FB
148 $conds = array();
149 foreach ($this->children as &$child) {
5dd9d823
FB
150 $val = $child->buildCondition($uf);
151 if ($val == self::COND_TRUE) {
152 return self::COND_TRUE;
153 } else if ($val == self::COND_FALSE) {
154 $true = self::COND_FALSE;
155 } else {
156 $conds[] = $val;
157 }
a087cc8d 158 }
5dd9d823 159 return $this->catConds($conds, 'OR', $true);
a087cc8d 160 }
a087cc8d
FB
161 }
162}
163
164class UFC_Promo implements UserFilterCondition
165{
a087cc8d
FB
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;
38c6fe96
FB
176 if ($this->grade != UserFilter::DISPLAY) {
177 UserFilter::assertGrade($this->grade);
178 }
a087cc8d
FB
179 }
180
784745ce 181 public function buildCondition(UserFilter &$uf)
a087cc8d 182 {
38c6fe96
FB
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 }
784745ce
FB
191 }
192}
193
194class 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));
d865c296 238 if (($this->mode & self::VARIANTS) != 0 && isset(UserFilter::$name_variants[$this->type])) {
784745ce
FB
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);
a087cc8d
FB
244 }
245}
246
4927ee54
FB
247class UFC_Dead implements UserFilterCondition
248{
38c6fe96
FB
249 private $comparison;
250 private $date;
251
252 public function __construct($comparison = null, $date = null)
4927ee54 253 {
38c6fe96
FB
254 $this->comparison = $comparison;
255 $this->date = $date;
4927ee54
FB
256 }
257
258 public function buildCondition(UserFilter &$uf)
259 {
38c6fe96
FB
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));
4927ee54 263 }
38c6fe96 264 return $str;
4927ee54
FB
265 }
266}
267
268class UFC_Registered implements UserFilterCondition
269{
270 private $active;
38c6fe96
FB
271 private $comparison;
272 private $date;
273
274 public function __construct($active = false, $comparison = null, $date = null)
4927ee54
FB
275 {
276 $this->only_active = $active;
38c6fe96
FB
277 $this->comparison = $comparison;
278 $this->date = $date;
4927ee54
FB
279 }
280
281 public function buildCondition(UserFilter &$uf)
282 {
283 if ($this->active) {
38c6fe96 284 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
4927ee54 285 } else {
38c6fe96
FB
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));
4927ee54 290 }
38c6fe96 291 return $date;
4927ee54
FB
292 }
293}
294
295class UFC_Sex implements UserFilterCondition
296{
297 private $sex;
298 public function __construct($sex)
299 {
300 $this->sex = $sex;
301 }
302
303 public function buildCondition(UserFilter &$uf)
304 {
305 if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) {
306 return self::COND_FALSE;
307 } else {
24e08e33 308 return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male');
4927ee54
FB
309 }
310 }
311}
312
313class UFC_Group implements UserFilterCondition
314{
315 private $group;
316 private $admin;
317 public function __construct($group, $admin = false)
318 {
319 $this->group = $group;
320 $this->admin = $admin;
321 }
322
323 public function buildCondition(UserFilter &$uf)
324 {
325 $sub = $uf->addGroupFilter($this->group);
326 $where = 'gpm' . $sub . '.perms IS NOT NULL';
327 if ($this->admin) {
328 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
329 }
330 return $where;
331 }
332}
333
aa21c568
FB
334class UFC_Email implements UserFilterCondition
335{
336 private $email;
337 public function __construct($email)
338 {
339 $this->email = $email;
340 }
341
342 public function buildCondition(UserFilter &$uf)
343 {
344 if (User::isForeignEmailAddress($this->email)) {
345 $sub = $uf->addEmailRedirectFilter($this->email);
346 return XDB::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email);
21401768
FB
347 } else if (User::isVirtualEmailAddress($this->email)) {
348 $sub = $uf->addVirtualEmailFilter($this->email);
349 return 'vr' . $sub . '.redirect IS NOT NULL';
aa21c568 350 } else {
21401768
FB
351 @list($user, $domain) = explode('@', $this->email);
352 $sub = $uf->addAliasFilter($user);
aa21c568
FB
353 return 'al' . $sub . '.alias IS NOT NULL';
354 }
355 }
356}
d865c296 357
21401768
FB
358class UFC_EmailList implements UserFilterCondition
359{
360 private $emails;
361 public function __construct($emails)
362 {
363 $this->emails = $emails;
364 }
365
366 public function buildCondition(UserFilter &$uf)
367 {
368 $email = null;
369 $virtual = null;
370 $alias = null;
371 $cond = array();
372
373 if (count($this->emails) == 0) {
374 return UserFilterCondition::COND_TRUE;
375 }
376
377 foreach ($this->emails as $entry) {
378 if (User::isForeignEmailAddress($entry)) {
379 if (is_null($email)) {
380 $email = $uf->addEmailRedirectFilter();
381 }
382 $cond[] = XDB::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry);
383 } else if (User::isVirtualEmailAddress($entry)) {
384 if (is_null($virtual)) {
385 $virtual = $uf->addVirtualEmailFilter();
386 }
387 $cond[] = XDB::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry);
388 } else {
389 if (is_null($alias)) {
390 $alias = $uf->addAliasFilter();
391 }
392 @list($user, $domain) = explode('@', $entry);
393 $cond[] = XDB::format('al' . $alias . '.alias = {?}', $user);
394 }
395 }
396 return '(' . implode(') OR (', $cond) . ')';
397 }
398}
d865c296
FB
399
400/******************
401 * ORDERS
402 ******************/
403
404abstract class UserFilterOrder
405{
406 protected $desc = false;
407
408 public function buildSort(UserFilter &$uf)
409 {
410 $sel = $this->getSortTokens($uf);
411 if (!is_array($sel)) {
412 $sel = array($sel);
413 }
414 if ($this->desc) {
415 foreach ($sel as $k=>$s) {
416 $sel[$k] = $s . ' DESC';
417 }
418 }
419 return $sel;
420 }
421
422 abstract protected function getSortTokens(UserFilter &$uf);
423}
424
425class UFO_Promo extends UserFilterOrder
426{
427 private $grade;
428
429 public function __construct($grade = null, $desc = false)
430 {
431 $this->grade = $grade;
432 $this->desc = $desc;
433 }
434
435 protected function getSortTokens(UserFilter &$uf)
436 {
437 if (UserFilter::isGrade($this->grade)) {
438 $sub = $uf->addEducationFilter($this->grade);
439 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
440 } else {
441 $sub = $uf->addDisplayFilter();
442 return 'pd' . $sub . '.promo';
443 }
444 }
445}
446
447class UFO_Name extends UserFilterOrder
448{
449 private $type;
450 private $variant;
451 private $particle;
452
453 public function __construct($type, $variant = null, $particle = false, $desc = false)
454 {
455 $this->type = $type;
456 $this->variant = $variant;
457 $this->particle = $particle;
458 $this->desc = $desc;
459 }
460
461 protected function getSortTokens(UserFilter &$uf)
462 {
463 if (UserFilter::isDisplayName($this->type)) {
464 $sub = $uf->addDisplayFilter();
465 return 'pd' . $sub . '.' . $this->type;
466 } else {
467 $sub = $uf->addNameFilter($this->type, $this->variant);
468 if ($this->particle) {
469 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
470 } else {
471 return 'pn' . $sub . '.name';
472 }
473 }
474 }
475}
476
38c6fe96
FB
477class UFO_Registration extends UserFilterOrder
478{
479 public function __construct($desc = false)
480 {
481 $this->desc = $desc;
482 }
483
484 protected function getSortTokens(UserFilter &$uf)
485 {
486 return 'a.registration_date';
487 }
488}
489
d865c296
FB
490/***********************************
491 *********************************
492 USER FILTER CLASS
493 *********************************
494 ***********************************/
495
a087cc8d
FB
496class UserFilter
497{
d865c296
FB
498 static private $joinMethods = array();
499
a087cc8d 500 private $root;
24e08e33 501 private $sort = array();
784745ce 502 private $query = null;
24e08e33 503 private $orderby = null;
784745ce 504
aa21c568 505 private $lastcount = null;
d865c296 506
24e08e33 507 public function __construct($cond = null, $sort = null)
5dd9d823 508 {
d865c296
FB
509 if (empty(self::$joinMethods)) {
510 $class = new ReflectionClass('UserFilter');
511 foreach ($class->getMethods() as $method) {
512 $name = $method->getName();
513 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
514 self::$joinMethods[] = $name;
515 }
516 }
517 }
5dd9d823
FB
518 if (!is_null($cond)) {
519 if ($cond instanceof UserFilterCondition) {
520 $this->setCondition($cond);
521 }
522 }
24e08e33
FB
523 if (!is_null($sort)) {
524 if ($sort instanceof UserFilterOrder) {
525 $this->addSort($sort);
d865c296
FB
526 } else if (is_array($sort)) {
527 foreach ($sort as $s) {
528 $this->addSort($s);
529 }
24e08e33
FB
530 }
531 }
5dd9d823
FB
532 }
533
784745ce
FB
534 private function buildQuery()
535 {
d865c296
FB
536 if (is_null($this->orderby)) {
537 $orders = array();
538 foreach ($this->sort as $sort) {
539 $orders = array_merge($orders, $sort->buildSort($this));
540 }
541 if (count($orders) == 0) {
542 $this->orderby = '';
543 } else {
544 $this->orderby = 'ORDER BY ' . implode(', ', $orders);
545 }
546 }
784745ce
FB
547 if (is_null($this->query)) {
548 $where = $this->root->buildCondition($this);
549 $joins = $this->buildJoins();
550 $this->query = 'FROM accounts AS a
551 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
552 INNER JOIN profiles AS p ON (p.pid = ap.pid)
553 ' . $joins . '
554 WHERE (' . $where . ')';
555 }
556 }
557
558 private function formatJoin(array $joins)
559 {
560 $str = '';
561 foreach ($joins as $key => $infos) {
562 $mode = $infos[0];
563 $table = $infos[1];
564 if ($mode == 'inner') {
565 $str .= 'INNER JOIN ';
566 } else if ($mode == 'left') {
567 $str .= 'LEFT JOIN ';
568 } else {
569 Platal::page()->kill("Join mode error");
570 }
571 $str .= $table . ' AS ' . $key;
572 if (isset($infos[2])) {
4927ee54 573 $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')';
784745ce
FB
574 }
575 $str .= "\n";
576 }
577 return $str;
578 }
579
580 private function buildJoins()
581 {
d865c296
FB
582 $joins = array();
583 foreach (self::$joinMethods as $method) {
584 $joins = array_merge($joins, $this->$method());
585 }
784745ce
FB
586 return $this->formatJoin($joins);
587 }
a087cc8d 588
d865c296
FB
589 private function getUIDList($uids = null, $count = null, $offset = null)
590 {
591 $this->buildQuery();
592 $limit = '';
593 if (!is_null($count)) {
594 if (!is_null($offset)) {
595 $limit = XDB::format('LIMIT {?}, {?}', $offset, $count);
596 } else {
597 $limit = XDB::format('LIMIT {?}', $count);
598 }
599 }
600 $cond = '';
601 if (!is_null($uids)) {
602 $cond = ' AND a.uid IN (' . implode(', ', $uids) . ')';
603 }
604 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
605 ' . $this->query . $cond . '
606 GROUP BY a.uid
607 ' . $this->orderby . '
608 ' . $limit);
609 $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
610 return $fetched;
611 }
612
a087cc8d
FB
613 /** Check that the user match the given rule.
614 */
615 public function checkUser(PlUser &$user)
616 {
784745ce
FB
617 $this->buildQuery();
618 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
619 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
620 return $count == 1;
a087cc8d
FB
621 }
622
623 /** Filter a list of user to extract the users matching the rule.
624 */
d865c296 625 public function filter(array $users, $count = null, $offset = null)
a087cc8d 626 {
4927ee54
FB
627 $this->buildQuery();
628 $table = array();
629 $uids = array();
630 foreach ($users as $user) {
631 $uids[] = $user->id();
632 $table[$user->id()] = $user;
633 }
d865c296 634 $fetched = $this->getUIDList($uids, $count, $offset);
a087cc8d 635 $output = array();
4927ee54
FB
636 foreach ($fetched as $uid) {
637 $output[] = $table[$uid];
a087cc8d
FB
638 }
639 return $output;
640 }
641
d865c296 642 public function getUIDs($count = null, $offset = null)
4927ee54 643 {
d865c296
FB
644 return $this->getUIDList(null, $count, $offset);
645 }
646
647 public function getUsers($count = null, $offset = null)
648 {
649 return User::getBulkUsersWithUIDs($this->getUIDs($count, $offset));
4927ee54
FB
650 }
651
d865c296 652 public function getTotalCount()
4927ee54 653 {
38c6fe96 654 if (is_null($this->lastcount)) {
aa21c568 655 $this->buildQuery();
38c6fe96
FB
656 return (int)XDB::fetchOneCell('SELECT COUNT(*)
657 ' . $this->query . '
658 GROUP BY a.uid');
659 } else {
660 return $this->lastcount;
661 }
4927ee54
FB
662 }
663
a087cc8d
FB
664 public function setCondition(UserFilterCondition &$cond)
665 {
666 $this->root =& $cond;
784745ce 667 $this->query = null;
a087cc8d
FB
668 }
669
24e08e33
FB
670 public function addSort(UserFilterOrder &$sort)
671 {
d865c296
FB
672 $this->sort[] = $sort;
673 $this->orderby = null;
24e08e33
FB
674 }
675
a087cc8d
FB
676 static public function getLegacy($promo_min, $promo_max)
677 {
a087cc8d 678 if ($promo_min != 0) {
784745ce 679 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
5dd9d823
FB
680 } else {
681 $min = new UFC_True();
a087cc8d 682 }
a087cc8d 683 if ($promo_max != 0) {
784745ce 684 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
a087cc8d 685 } else {
5dd9d823 686 $max = new UFC_True();
a087cc8d 687 }
5dd9d823 688 return new UserFilter(new UFC_And($min, $max));
a087cc8d 689 }
784745ce 690
aa21c568
FB
691 static private function getDBSuffix($string)
692 {
693 return preg_replace('/[^a-z0-9]/i', '', $string);
694 }
695
696
697 private $option = 0;
698 private function register_optional(array &$table, $val)
699 {
700 if (is_null($val)) {
701 $sub = $this->option++;
702 $index = null;
703 } else {
704 $sub = self::getDBSuffix($val);
705 $index = $val;
706 }
707 $sub = '_' . $sub;
708 $table[$sub] = $index;
709 return $sub;
710 }
784745ce 711
d865c296
FB
712 /** DISPLAY
713 */
38c6fe96 714 const DISPLAY = 'display';
d865c296
FB
715 private $pd = false;
716 public function addDisplayFilter()
717 {
718 $this->pd = true;
719 return '';
720 }
721
722 private function displayJoins()
723 {
724 if ($this->pd) {
725 return array('pd' => array('left', 'profile_display', '$ME.pid = $PID'));
726 } else {
727 return array();
728 }
729 }
730
784745ce
FB
731 /** NAMES
732 */
d865c296 733 /* name tokens */
784745ce
FB
734 const LASTNAME = 'lastname';
735 const FIRSTNAME = 'firstname';
736 const NICKNAME = 'nickname';
737 const PSEUDONYM = 'pseudonym';
738 const NAME = 'name';
d865c296 739 /* name variants */
784745ce
FB
740 const VN_MARITAL = 'marital';
741 const VN_ORDINARY = 'ordinary';
742 const VN_OTHER = 'other';
743 const VN_INI = 'ini';
d865c296
FB
744 /* display names */
745 const DN_FULL = 'directory_name';
746 const DN_DISPLAY = 'yourself';
747 const DN_YOURSELF = 'yourself';
748 const DN_DIRECTORY = 'directory_name';
749 const DN_PRIVATE = 'private_name';
750 const DN_PUBLIC = 'public_name';
751 const DN_SHORT = 'short_name';
752 const DN_SORT = 'sort_name';
784745ce
FB
753
754 static public $name_variants = array(
755 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
d865c296
FB
756 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
757 );
784745ce
FB
758
759 static public function assertName($name)
760 {
761 if (!Profile::getNameTypeId($name)) {
762 Platal::page()->kill('Invalid name type');
763 }
764 }
765
d865c296
FB
766 static public function isDisplayName($name)
767 {
768 return $name == self::DN_FULL || $name == self::DN_DISPLAY
769 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
770 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
771 || $name == self::DN_SHORT || $name == self::DN_SORT;
772 }
773
784745ce 774 private $pn = array();
784745ce
FB
775 public function addNameFilter($type, $variant = null)
776 {
777 if (!is_null($variant)) {
778 $ft = $type . '_' . $variant;
779 } else {
780 $ft = $type;
781 }
782 $sub = '_' . $ft;
783 self::assertName($ft);
784
785 if (!is_null($variant) && $variant == 'other') {
aa21c568 786 $sub .= $this->option++;
784745ce
FB
787 }
788 $this->pn[$sub] = Profile::getNameTypeId($ft);
789 return $sub;
790 }
791
792 private function nameJoins()
793 {
794 $joins = array();
795 foreach ($this->pn as $sub => $type) {
796 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
797 }
798 return $joins;
799 }
800
801
802 /** EDUCATION
803 */
804 const GRADE_ING = 'Ing.';
805 const GRADE_PHD = 'PhD';
806 const GRADE_MST = 'M%';
807 static public function isGrade($grade)
808 {
38c6fe96 809 return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST;
784745ce
FB
810 }
811
812 static public function assertGrade($grade)
813 {
814 if (!self::isGrade($grade)) {
815 Platal::page()->killError("DiplĂ´me non valide");
816 }
817 }
818
d865c296
FB
819 static public function promoYear($grade)
820 {
821 // XXX: Definition of promotion for phds and masters might change in near future.
822 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
823 }
824
784745ce
FB
825 private $pepe = array();
826 private $with_pee = false;
784745ce
FB
827 public function addEducationFilter($x = false, $grade = null)
828 {
829 if (!$x) {
aa21c568
FB
830 $index = $this->option;
831 $sub = $this->option++;
784745ce
FB
832 } else {
833 self::assertGrade($grade);
834 $index = $grade;
835 $sub = $grade[0];
836 $this->with_pee = true;
837 }
838 $sub = '_' . $sub;
839 $this->pepe[$index] = $sub;
840 return $sub;
841 }
842
843 private function educationJoins()
844 {
845 $joins = array();
846 if ($this->with_pee) {
847 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
848 }
849 foreach ($this->pepe as $grade => $sub) {
850 if ($this->isGrade($grade)) {
851 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
852 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
853 XDB::format('{?}', $grade));
854 } else {
855 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID');
856 $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
857 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
858 }
859 }
860 return $joins;
861 }
4927ee54
FB
862
863
864 /** GROUPS
865 */
866 private $gpm = array();
4927ee54
FB
867 public function addGroupFilter($group = null)
868 {
869 if (!is_null($group)) {
870 if (ctype_digit($group)) {
871 $index = $sub = $group;
872 } else {
873 $index = $group;
aa21c568 874 $sub = self::getDBSuffix($group);
4927ee54
FB
875 }
876 } else {
aa21c568 877 $sub = 'group_' . $this->option++;
4927ee54
FB
878 $index = null;
879 }
880 $sub = '_' . $sub;
881 $this->gpm[$sub] = $index;
882 return $sub;
883 }
884
885 private function groupJoins()
886 {
887 $joins = array();
888 foreach ($this->gpm as $sub => $key) {
889 if (is_null($key)) {
890 $joins['gpa' . $sub] = array('inner', 'groupex.asso');
891 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
892 } else if (ctype_digit($key)) {
893 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
894 } else {
895 $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key));
896 $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
897 }
898 }
899 return $joins;
900 }
aa21c568
FB
901
902 /** EMAILS
903 */
904 private $e = array();
905 public function addEmailRedirectFilter($email = null)
906 {
907 return $this->register_optional($this->e, $email);
908 }
909
910 private $ve = array();
911 public function addVirtualEmailFilter($email = null)
912 {
21401768 913 $this->addAliasFilter(self::ALIAS_FORLIFE);
aa21c568
FB
914 return $this->register_optional($this->ve, $email);
915 }
916
21401768
FB
917 const ALIAS_BEST = 'bestalias';
918 const ALIAS_FORLIFE = 'forlife';
aa21c568
FB
919 private $al = array();
920 public function addAliasFilter($alias = null)
921 {
922 return $this->register_optional($this->al, $alias);
923 }
924
925 private function emailJoins()
926 {
927 global $globals;
928 $joins = array();
929 foreach ($this->e as $sub=>$key) {
930 if (is_null($key)) {
931 $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
932 } else {
933 $joins['e' . $sub] = array('left', 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key));
934 }
935 }
21401768 936 foreach ($this->al as $sub=>$key) {
aa21c568 937 if (is_null($key)) {
21401768
FB
938 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
939 } else if ($key == self::ALIAS_BEST) {
940 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
941 } else if ($key == self::ALIAS_FORLIFE) {
942 $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
aa21c568 943 } else {
21401768 944 $joins['al' . $sub] = array('left', 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
aa21c568 945 }
aa21c568 946 }
21401768 947 foreach ($this->ve as $sub=>$key) {
aa21c568 948 if (is_null($key)) {
21401768 949 $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\'');
aa21c568 950 } else {
21401768 951 $joins['v' . $sub] = array('left', 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key));
aa21c568 952 }
21401768
FB
953 $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid
954 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
955 CONCAT(al_forlife.alias, \'@\', {?}),
956 a.email))',
957 $globals->mail->domain, $globals->mail->domain2));
aa21c568
FB
958 }
959 return $joins;
960 }
a087cc8d
FB
961}
962
963// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
964?>