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