Merge branch 'xorg/f/xnet-accounts' into xorg/master
[platal.git] / classes / userfilter / conditions.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 // {{{ abstract class UserFilterCondition
23 /** This class describe objects which filter users based
24 * on various parameters.
25 * The parameters of the filter must be given to the constructor.
26 * The buildCondition function is called by UserFilter when
27 * actually building the query. That function must call
28 * $uf->addWheteverFilter so that the UserFilter makes
29 * adequate joins. It must return the 'WHERE' condition to use
30 * with the filter.
31 */
32 abstract class UserFilterCondition implements PlFilterCondition
33 {
34 const OP_EQUALS = '=';
35 const OP_GREATER = '>';
36 const OP_NOTGREATER = '<=';
37 const OP_LESSER = '<';
38 const OP_NOTLESSER = '>=';
39 const OP_NULL = 'null';
40 const OP_NOTNULL = 'not null';
41 const OP_CONTAINS = 'contains';
42 const OP_PREFIX = 'prefix';
43 const OP_SUFFIX = 'suffix';
44
45 protected function buildExport($type)
46 {
47 $export = array('type' => $type);
48 return $export;
49 }
50
51 public function export()
52 {
53 throw new Exception("This class is not exportable");
54 }
55
56 public static function comparisonFromXDBWildcard($wildcard)
57 {
58 switch ($wildcard) {
59 case XDB::WILDCARD_EXACT:
60 return self::OP_EQUALS;
61 case XDB::WILDCARD_PREFIX:
62 return self::OP_PREFIX;
63 case XDB::WILDCARD_SUFFIX:
64 return self::OP_SUFFIX;
65 case XDB::WILDCARD_CONTAINS:
66 return self::OP_CONTAINS;
67 }
68 throw new Exception("Unknown wildcard mode: $wildcard");
69 }
70
71 public static function xdbWildcardFromComparison($comparison)
72 {
73 if (!self::isStringComparison($comparison)) {
74 throw new Exception("Unknown string coparison: $comparison");
75 }
76 switch ($comparison) {
77 case self::OP_EQUALS:
78 return XDB::WILDCARD_EXACT;
79 case self::OP_PREFIX:
80 return XDB::WILDCARD_PREFIX;
81 case self::OP_SUFFIX:
82 return XDB::WILDCARD_SUFFIX;
83 case self::OP_CONTAINS:
84 return XDB::WILDCARD_CONTAINS;
85 }
86 }
87
88 private static function isNumericComparison($comparison)
89 {
90 return $comparison == self::OP_EQUALS
91 || $comparison == self::OP_GREATER
92 || $comparison == self::OP_NOTGREATER
93 || $comparison == self::OP_LESSER
94 || $comparison == self::OP_NOTLESSER;
95 }
96
97 private static function isStringComparison($comparison)
98 {
99 return $comparison == self::OP_EQUALS
100 || $comparison == self::OP_CONTAINS
101 || $comparison == self::OP_PREFIX
102 || $comparison == self::OP_SUFFIX;
103 }
104
105 public static function fromExport(array $export)
106 {
107 $export = new PlDict($export);
108 if (!$export->has('type')) {
109 throw new Exception("Missing type in export");
110 }
111 $type = $export->s('type');
112 $cond = null;
113 switch ($type) {
114 case 'and':
115 case 'or':
116 case 'not':
117 case 'true':
118 case 'false':
119 $class = 'pfc_' . $type;
120 $cond = new $class();
121 break;
122
123 case 'host':
124 if ($export->has('ip')) {
125 $cond = new UFC_Ip($export->s('ip'));
126 }
127 break;
128
129 case 'comment':
130 if ($export->has('text') && $export->s('comparison') == self::OP_CONTAINS) {
131 $cond = new UFC_Comment($export->s('text'));
132 }
133 break;
134
135 case 'promo':
136 if ($export->has('promo') && self::isNumericComparison($export->s('comparison'))) {
137 $cond = new UFC_Promo($export->s('comparison'),
138 $export->s('grade', UserFilter::DISPLAY),
139 $export->s('promo'));
140 }
141 break;
142
143 case 'lastname':
144 case 'name':
145 case 'firstname':
146 case 'nickname':
147 case 'pseudonym':
148 if ($export->has('text')) {
149 $flag = self::xdbWildcardFromComparison($export->s('comparison'));
150 if ($export->b('search_in_variants')) {
151 $flag |= UFC_Name::VARIANTS;
152 }
153 if ($export->b('search_in_particle')) {
154 $flag |= UFC_Name::PARTICLE;
155 }
156 $cond = new UFC_Name($type, $export->s('text'), $flag);
157 }
158 break;
159
160 case 'account_type':
161 case 'account_perm':
162 case 'hrpid':
163 case 'hruid':
164 $values = $export->v('values', array());
165 $class = 'ufc_' . str_replace('_', '', $type);
166 $cond = new $class($values);
167 break;
168
169 case 'school_id':
170 $values = $export->v('values', array());
171 $school_type = $export->s('school_type');
172 $cond = new UFC_SchoolId($school_type, $values);
173 break;
174
175 case 'has_profile':
176 case 'has_email_redirect':
177 case 'has_valid_email':
178 $class = 'ufc_' . str_replace('_', '', $type);
179 $cond = new $class();
180 break;
181
182 default:
183 throw new Exception("Unknown condition type: $type");
184 }
185 if (is_null($cond)) {
186 throw new Exception("Unsupported $type definition");
187 }
188 if ($cond instanceof PFC_NChildren) {
189 $children = $export->v('children', array());
190 foreach ($children as $child) {
191 $cond->addChild(self::fromExport($child));
192 }
193 } else if ($cond instanceof PFC_OneChild) {
194 if ($export->has('child')) {
195 $cond->setChild(self::fromExport($export->v('child')));
196 }
197 }
198 return $cond;
199 }
200 }
201 // }}}
202 // {{{ class UFC_HasProfile
203 /** Filters users who have a profile
204 */
205 class UFC_HasProfile extends UserFilterCondition
206 {
207 public function buildCondition(PlFilter $uf)
208 {
209 $uf->requireProfiles();
210 return '$PID IS NOT NULL';
211 }
212
213 public function export()
214 {
215 return $this->buildExport('has_profile');
216 }
217 }
218 // }}}
219 // {{{ class UFC_AccountType
220 /** Filters users who have one of the given account types
221 */
222 class UFC_AccountType extends UserFilterCondition
223 {
224 private $types;
225
226 public function __construct()
227 {
228 $this->types = pl_flatten(func_get_args());
229 }
230
231 public function buildCondition(PlFilter $uf)
232 {
233 $uf->requireAccounts();
234 return XDB::format('a.type IN {?}', $this->types);
235 }
236
237 public function export()
238 {
239 $export = $this->buildExport('account_type');
240 $export['values'] = $this->types;
241 return $export;
242 }
243 }
244 // }}}
245 // {{{ class UFC_AccountPerm
246 /** Filters users who have one of the given permissions
247 */
248 class UFC_AccountPerm extends UserFilterCondition
249 {
250 private $perms;
251
252 public function __construct()
253 {
254 $this->perms = pl_flatten(func_get_args());
255 }
256
257 public function buildCondition(PlFilter $uf)
258 {
259 $uf->requirePerms();
260 $conds = array();
261 foreach ($this->perms as $perm) {
262 $conds[] = XDB::format('FIND_IN_SET({?}, IF(a.user_perms IS NULL, at.perms,
263 CONCAT(at.perms, \',\', a.user_perms)))',
264 $perm);
265 }
266 if (empty($conds)) {
267 return self::COND_TRUE;
268 } else {
269 return implode(' OR ', $conds);
270 }
271 }
272
273 public function export()
274 {
275 $export = $this->buildExport('account_perm');
276 $export['values'] = $this->perms;
277 return $export;
278 }
279 }
280 // }}}
281 // {{{ class UFC_Hruid
282 /** Filters users based on their hruid
283 * @param $val Either an hruid, or a list of those
284 */
285 class UFC_Hruid extends UserFilterCondition
286 {
287 private $hruids;
288
289 public function __construct()
290 {
291 $this->hruids = pl_flatten(func_get_args());
292 }
293
294 public function buildCondition(PlFilter $uf)
295 {
296 $uf->requireAccounts();
297 return XDB::format('a.hruid IN {?}', $this->hruids);
298 }
299
300 public function export()
301 {
302 $export = $this->buildExport('hruid');
303 $export['values'] = $this->hruids;
304 return $export;
305 }
306 }
307 // }}}
308 // {{{ class UFC_Hrpid
309 /** Filters users based on the hrpid of their profiles
310 * @param $val Either an hrpid, or a list of those
311 */
312 class UFC_Hrpid extends UserFilterCondition
313 {
314 private $hrpids;
315
316 public function __construct()
317 {
318 $this->hrpids = pl_flatten(func_get_args());
319 }
320
321 public function buildCondition(PlFilter $uf)
322 {
323 $uf->requireProfiles();
324 return XDB::format('p.hrpid IN {?}', $this->hrpids);
325 }
326
327 public function export()
328 {
329 $export = $this->buildExport('hrpid');
330 $export['values'] = $this->hrpids;
331 return $export;
332 }
333 }
334 // }}}
335 // {{{ class UFC_HasEmailRedirect
336 /** Filters users, keeping only those with a valid email redirection (only X.org accounts).
337 */
338 class UFC_HasEmailRedirect extends UserFilterCondition
339 {
340 public function buildCondition(PlFilter $uf)
341 {
342 $sub_redirect = $uf->addEmailRedirectFilter();
343 return 'ra' . $sub_redirect . '.flags = \'active\'';
344 }
345
346 public function export()
347 {
348 $export = $this->buildExport('has_email_redirect');
349 return $export;
350 }
351 }
352 // }}}
353 // {{{ class UFC_HasValidEmail
354 /** Filters users, keeping only those with a valid email address (all accounts).
355 */
356 class UFC_HasValidEmail extends UserFilterCondition
357 {
358 public function buildCondition(PlFilter $uf)
359 {
360 $sub_redirect = $uf->addEmailRedirectFilter();
361 $uf->requireAccounts();
362 return 'ra' . $sub_redirect . '.flags = \'active\' OR a.email IS NOT NULL';
363 }
364
365 public function export()
366 {
367 $export = $this->buildExport('has_valid_email');
368 return $export;
369 }
370 }
371 // }}}
372 // {{{ class UFC_Ip
373 /** Filters users based on one of their last IPs
374 * @param $ip IP from which connection are checked
375 */
376 class UFC_Ip extends UserFilterCondition
377 {
378 private $ip;
379
380 public function __construct($ip)
381 {
382 $this->ip = $ip;
383 }
384
385 public function buildCondition(PlFilter $uf)
386 {
387 $sub = $uf->addLoggerFilter();
388 $ip = ip_to_uint($this->ip);
389 return XDB::format($sub . '.ip = {?} OR ' . $sub . '.forward_ip = {?}', $ip, $ip);
390 }
391
392 public function export()
393 {
394 $export = $this->buildExport('host');
395 $export['ip'] = $this->ip;
396 return $export;
397 }
398 }
399 // }}}
400 // {{{ class UFC_Comment
401 class UFC_Comment extends UserFilterCondition
402 {
403 private $text;
404
405 public function __construct($text)
406 {
407 $this->text = $text;
408 }
409
410 public function buildCondition(PlFilter $uf)
411 {
412 $uf->requireProfiles();
413 return $uf->getVisibilityCondition('p.freetext_pub') . ' AND p.freetext ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->text);
414 }
415
416 public function export()
417 {
418 $export = $this->buildExport('comment');
419 $export['comparison'] = self::OP_CONTAINS;
420 $export['text'] = $this->text;
421 return $export;
422 }
423 }
424 // }}}
425 // {{{ class UFC_Promo
426 /** Filters users based on promotion
427 * @param $comparison Comparison operator (>, =, ...)
428 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
429 * @param $promo Promotion on which the filter is based
430 */
431 class UFC_Promo extends UserFilterCondition
432 {
433
434 private $grade;
435 private $promo;
436 private $comparison;
437
438 public function __construct($comparison, $grade, $promo)
439 {
440 $this->grade = $grade;
441 $this->comparison = $comparison;
442 $this->promo = $promo;
443 if ($this->grade != UserFilter::DISPLAY) {
444 UserFilter::assertGrade($this->grade);
445 }
446 if ($this->grade == UserFilter::DISPLAY && $this->comparison != '=') {
447 // XXX: we might try to guess the grade from the first char of the promo and forbid only '<= 2004', but allow '<= X2004'
448 Platal::page()->killError("Il n'est pas possible d'appliquer la comparaison '" . $this->comparison . "' aux promotions sans spécifier de formation (X/M/D)");
449 }
450 }
451
452 public function buildCondition(PlFilter $uf)
453 {
454 if ($this->grade == UserFilter::DISPLAY) {
455 $sub = $uf->addDisplayFilter();
456 return XDB::format('pd' . $sub . '.promo ' . $this->comparison . ' {?}', $this->promo);
457 } else {
458 $sub = $uf->addEducationFilter(true, $this->grade);
459 $field = 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
460 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison . ' ' . XDB::format('{?}', $this->promo);
461 }
462 }
463
464 public function export()
465 {
466 $export = $this->buildExport('promo');
467 $export['comparison'] = $this->comparison;
468 if ($this->grade != UserFilter::DISPLAY) {
469 $export['grade'] = $this->grade;
470 }
471 $export['promo'] = $this->promo;
472 return $export;
473 }
474 }
475 // }}}
476 // {{{ class UFC_SchoolId
477 /** Filters users based on their shoold identifier
478 * @param type Parameter type (Xorg, AX, School)
479 * @param value Array of school ids
480 */
481 class UFC_SchoolId extends UserFilterCondition
482 {
483 const AX = 'ax';
484 const Xorg = 'xorg';
485 const School = 'school';
486
487 private $type;
488 private $id;
489
490 static public function assertType($type)
491 {
492 if ($type != self::AX && $type != self::Xorg && $type != self::School) {
493 Platal::page()->killError("Type de matricule invalide: $type");
494 }
495 }
496
497 /** Construct a UFC_SchoolId
498 * The first argument is the type, all following arguments can be either ids
499 * or arrays of ids to use:
500 * $ufc = new UFC_SchoolId(UFC_SchoolId::AX, $id1, $id2, array($id3, $id4));
501 */
502 public function __construct($type)
503 {
504 $this->type = $type;
505 $ids = func_get_args();
506 array_shift($ids);
507 $this->ids = pl_flatten($ids);
508 self::assertType($type);
509 }
510
511 public function buildCondition(PlFilter $uf)
512 {
513 $uf->requireProfiles();
514 $ids = $this->ids;
515 $type = $this->type;
516 if ($type == self::School) {
517 $type = self::Xorg;
518 $ids = array_map(array('Profile', 'getXorgId'), $ids);
519 }
520 return XDB::format('p.' . $type . '_id IN {?}', $ids);
521 }
522
523 public function export()
524 {
525 $export = $this->buildExport('school_id');
526 $export['school_type'] = $this->type;
527 $export['values'] = $this->ids;
528 return $export;
529 }
530 }
531 // }}}
532 // {{{ class UFC_EducationSchool
533 /** Filters users by formation
534 * @param $val The formation to search (either ID or array of IDs)
535 */
536 class UFC_EducationSchool extends UserFilterCondition
537 {
538 private $val;
539
540 public function __construct()
541 {
542 $this->val = pl_flatten(func_get_args());
543 }
544
545 public function buildCondition(PlFilter $uf)
546 {
547 $sub = $uf->addEducationFilter();
548 return XDB::format('pe' . $sub . '.eduid IN {?}', $this->val);
549 }
550 }
551 // }}}
552 // {{{ class UFC_EducationDegree
553 class UFC_EducationDegree extends UserFilterCondition
554 {
555 private $diploma;
556
557 public function __construct()
558 {
559 $this->diploma = pl_flatten(func_get_args());
560 }
561
562 public function buildCondition(PlFilter $uf)
563 {
564 $sub = $uf->addEducationFilter();
565 return XDB::format('pe' . $sub . '.degreeid IN {?}', $this->diploma);
566 }
567 }
568 // }}}
569 // {{{ class UFC_EducationField
570 class UFC_EducationField extends UserFilterCondition
571 {
572 private $val;
573
574 public function __construct()
575 {
576 $this->val = pl_flatten(func_get_args());
577 }
578
579 public function buildCondition(PlFilter $uf)
580 {
581 $sub = $uf->addEducationFilter();
582 return XDB::format('pe' . $sub . '.fieldid IN {?}', $this->val);
583 }
584 }
585 // }}}
586 // {{{ class UFC_Name
587 /** Filters users based on name
588 * @param $type Type of name field on which filtering is done (firstname, lastname...)
589 * @param $text Text on which to filter
590 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
591 */
592 class UFC_Name extends UserFilterCondition
593 {
594 const EXACT = XDB::WILDCARD_EXACT; // 0x000
595 const PREFIX = XDB::WILDCARD_PREFIX; // 0x001
596 const SUFFIX = XDB::WILDCARD_SUFFIX; // 0x002
597 const CONTAINS = XDB::WILDCARD_CONTAINS; // 0x003
598 const PARTICLE = 0x004;
599 const VARIANTS = 0x008;
600
601 private $type;
602 private $text;
603 private $mode;
604
605 public function __construct($type, $text, $mode)
606 {
607 $this->type = $type;
608 $this->text = $text;
609 $this->mode = $mode;
610 }
611
612 private function buildNameQuery($type, $variant, $where, UserFilter $uf)
613 {
614 $sub = $uf->addNameFilter($type, $variant);
615 return str_replace('$ME', 'pn' . $sub, $where);
616 }
617
618 public function buildCondition(PlFilter $uf)
619 {
620 $left = '$ME.name';
621 if (($this->mode & self::PARTICLE) == self::PARTICLE) {
622 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
623 }
624 $right = XDB::formatWildcards($this->mode & self::CONTAINS, $this->text);
625
626 $cond = $left . $right;
627 $conds = array($this->buildNameQuery($this->type, null, $cond, $uf));
628 if (($this->mode & self::VARIANTS) != 0 && isset(Profile::$name_variants[$this->type])) {
629 foreach (Profile::$name_variants[$this->type] as $var) {
630 $conds[] = $this->buildNameQuery($this->type, $var, $cond, $uf);
631 }
632 }
633 return implode(' OR ', $conds);
634 }
635
636 public function export()
637 {
638 $export = $this->buildExport($this->type);
639 if ($this->mode & self::VARIANTS) {
640 $export['search_in_variants'] = true;
641 }
642 if ($this->mode & self::PARTICLE) {
643 $export['search_in_particle'] = true;
644 }
645 $export['comparison'] = self::comparisonFromXDBWildcard($this->mode & 0x3);
646 $export['text'] = $this->text;
647 return $export;
648 }
649 }
650 // }}}
651 // {{{ class UFC_NameTokens
652 /** Selects users based on tokens in their name (for quicksearch)
653 * @param $tokens An array of tokens to search
654 * @param $flags Flags the tokens must have (e.g 'public' for public search)
655 * @param $soundex (bool) Whether those tokens are fulltext or soundex
656 */
657 class UFC_NameTokens extends UserFilterCondition
658 {
659 /* Flags */
660 const FLAG_PUBLIC = 'public';
661
662 private $tokens;
663 private $flags;
664 private $soundex;
665 private $exact;
666
667 public function __construct($tokens, $flags = array(), $soundex = false, $exact = false)
668 {
669 if (is_array($tokens)) {
670 $this->tokens = $tokens;
671 } else {
672 $this->tokens = array($tokens);
673 }
674 if (is_array($flags)) {
675 $this->flags = $flags;
676 } else {
677 $this->flags = array($flags);
678 }
679 $this->soundex = $soundex;
680 $this->exact = $exact;
681 }
682
683 public function buildCondition(PlFilter $uf)
684 {
685 $conds = array();
686 foreach ($this->tokens as $i => $token) {
687 $sub = $uf->addNameTokensFilter($token);
688 if ($this->soundex) {
689 $c = XDB::format($sub . '.soundex = {?}', soundex_fr($token));
690 } else if ($this->exact) {
691 $c = XDB::format($sub . '.token = {?}', $token);
692 } else {
693 $c = $sub . '.token ' . XDB::formatWildcards(XDB::WILDCARD_PREFIX, $token);
694 }
695 if ($this->flags != null) {
696 $c .= XDB::format(' AND ' . $sub . '.flags IN {?}', $this->flags);
697 }
698 $conds[] = $c;
699 }
700
701 return implode(' AND ', $conds);
702 }
703 }
704 // }}}
705 // {{{ class UFC_Nationality
706 class UFC_Nationality extends UserFilterCondition
707 {
708 private $val;
709
710 public function __construct()
711 {
712 $this->val = pl_flatten(func_get_args());
713 }
714
715 public function buildCondition(PlFilter $uf)
716 {
717 $uf->requireProfiles();
718 $nat = XDB::formatArray($this->val);
719 $conds = array(
720 'p.nationality1 IN ' . $nat,
721 'p.nationality2 IN ' . $nat,
722 'p.nationality3 IN ' . $nat,
723 );
724 return implode(' OR ', $conds);
725 }
726 }
727 // }}}
728 // {{{ class UFC_Dead
729 /** Filters users based on death date
730 * @param $comparison Comparison operator
731 * @param $date Date to which death date should be compared (DateTime object, string or timestamp)
732 */
733 class UFC_Dead extends UserFilterCondition
734 {
735 private $comparison;
736 private $date;
737
738 public function __construct($comparison = null, $date = null)
739 {
740 $this->comparison = $comparison;
741 $this->date = make_datetime($date);
742 }
743
744 public function buildCondition(PlFilter $uf)
745 {
746 $uf->requireProfiles();
747 $str = 'p.deathdate IS NOT NULL';
748 if (!is_null($this->comparison)) {
749 $str .= ' AND p.deathdate ' . $this->comparison . ' ' . XDB::format('{?}', $this->date->format('Y-m-d'));
750 }
751 return $str;
752 }
753 }
754 // }}}
755 // {{{ class UFC_Registered
756 /** Filters users based on registration state
757 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
758 * @param $comparison Comparison operator
759 * @param $date Date to which users registration date should be compared
760 */
761 class UFC_Registered extends UserFilterCondition
762 {
763 private $active;
764 private $comparison;
765 private $date;
766
767 public function __construct($active = false, $comparison = null, $date = null)
768 {
769 $this->active = $active;
770 $this->comparison = $comparison;
771 $this->date = make_datetime($date);
772 }
773
774 public function buildCondition(PlFilter $uf)
775 {
776 $uf->requireAccounts();
777 if ($this->active) {
778 $date = '$UID IS NOT NULL AND a.state = \'active\'';
779 } else {
780 $date = '$UID IS NOT NULL AND a.state != \'pending\'';
781 }
782 if (!is_null($this->comparison)) {
783 $date .= ' AND a.registration_date != \'0000-00-00 00:00:00\' AND a.registration_date ' . $this->comparison . ' ' . XDB::format('{?}', $this->date->format('Y-m-d'));
784 }
785 return $date;
786 }
787 }
788 // }}}
789 // {{{ class UFC_ProfileUpdated
790 /** Filters users based on profile update date
791 * @param $comparison Comparison operator
792 * @param $date Date to which profile update date must be compared
793 */
794 class UFC_ProfileUpdated extends UserFilterCondition
795 {
796 private $comparison;
797 private $date;
798
799 public function __construct($comparison = null, $date = null)
800 {
801 $this->comparison = $comparison;
802 $this->date = $date;
803 }
804
805 public function buildCondition(PlFilter $uf)
806 {
807 $uf->requireProfiles();
808 return 'p.last_change ' . $this->comparison . XDB::format(' {?}', date('Y-m-d H:i:s', $this->date));
809 }
810 }
811 // }}}
812 // {{{ class UFC_Birthday
813 /** Filters users based on next birthday date
814 * @param $comparison Comparison operator
815 * @param $date Date to which users next birthday date should be compared
816 */
817 class UFC_Birthday extends UserFilterCondition
818 {
819 private $comparison;
820 private $date;
821
822 public function __construct($comparison = null, $date = null)
823 {
824 $this->comparison = $comparison;
825 $this->date = $date;
826 }
827
828 public function buildCondition(PlFilter $uf)
829 {
830 $uf->requireProfiles();
831 return 'p.next_birthday ' . $this->comparison . XDB::format(' {?}', date('Y-m-d', $this->date));
832 }
833 }
834 // }}}
835 // {{{ class UFC_Sex
836 /** Filters users based on sex
837 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
838 */
839 class UFC_Sex extends UserFilterCondition
840 {
841 private $sex;
842 public function __construct($sex)
843 {
844 $this->sex = $sex;
845 }
846
847 public function buildCondition(PlFilter $uf)
848 {
849 if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) {
850 return self::COND_FALSE;
851 } else {
852 $uf->requireProfiles();
853 return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male');
854 }
855 }
856 }
857 // }}}
858 // {{{ class UFC_NLSubscribed
859 /** Filters users based on NL subscription
860 * @param $nlid NL whose subscribers we are selecting
861 * @param $issue Select only subscribers who have not yet received that issue
862 */
863 class UFC_NLSubscribed extends UserFilterCondition
864 {
865 private $nlid;
866 private $issue_id;
867 public function __construct($nlid, $issue_id)
868 {
869 $this->nlid = $nlid;
870 $this->issue_id = $issue_id;
871 }
872
873 public function buildCondition(PlFilter $uf)
874 {
875 $sub = $uf->addNewsLetterFilter($this->nlid);
876 return XDB::format($sub . '.last < {?}', $this->issue_id);
877 }
878 }
879 // }}}
880 // {{{ class UFC_Group
881 /** Filters users based on group membership
882 * @param $group Group whose members we are selecting
883 * @param $anim Whether to restrict selection to animators of that group
884 */
885 class UFC_Group extends UserFilterCondition
886 {
887 private $group;
888 private $anim;
889 public function __construct($group, $anim = false)
890 {
891 $this->group = $group;
892 $this->anim = $anim;
893 }
894
895 public function buildCondition(PlFilter $uf)
896 {
897 // Groups are only visible for users with perm 'groups'.
898 if (!S::user()->checkPerms(User::PERM_GROUPS)) {
899 return self::COND_FALSE;
900 }
901 $sub = $uf->addGroupFilter($this->group);
902 $where = 'gpm' . $sub . '.perms IS NOT NULL';
903 if ($this->anim) {
904 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
905 }
906 return $where;
907 }
908 }
909 // }}}
910 // {{{ class UFC_Binet
911 /** Selects users based on their belonging to a given (list of) binet
912 * @param $binet either a binet_id or an array of binet_ids
913 */
914 class UFC_Binet extends UserFilterCondition
915 {
916 private $val;
917
918 public function __construct()
919 {
920 $this->val = pl_flatten(func_get_args());
921 }
922
923 public function buildCondition(PlFilter $uf)
924 {
925 // Binets are private.
926 if ($uf->getVisibilityLevel() != ProfileVisibility::VIS_PRIVATE) {
927 return self::COND_TRUE;
928 }
929 $sub = $uf->addBinetsFilter();
930 return XDB::format($sub . '.binet_id IN {?}', $this->val);
931 }
932 }
933 // }}}
934 // {{{ class UFC_Section
935 /** Selects users based on section
936 * @param $section ID of the section
937 */
938 class UFC_Section extends UserFilterCondition
939 {
940 private $section;
941
942 public function __construct()
943 {
944 $this->section = pl_flatten(func_get_args());
945 }
946
947 public function buildCondition(PlFilter $uf)
948 {
949 // Sections are private.
950 if ($uf->getVisibilityLevel() != ProfileVisibility::VIS_PRIVATE) {
951 return self::COND_TRUE;
952 }
953 $uf->requireProfiles();
954 return XDB::format('p.section IN {?}', $this->section);
955 }
956 }
957 // }}}
958 // {{{ class UFC_Email
959 /** Filters users based on an email or a list of emails
960 * @param $emails List of emails whose owner must be selected
961 */
962 class UFC_Email extends UserFilterCondition
963 {
964 private $emails;
965 public function __construct()
966 {
967 $this->emails = pl_flatten(func_get_args());
968 }
969
970 public function buildCondition(PlFilter $uf)
971 {
972 $foreign = array();
973 $local = array();
974 $cond = array();
975
976 if (count($this->emails) == 0) {
977 return PlFilterCondition::COND_TRUE;
978 }
979
980 foreach ($this->emails as $entry) {
981 if (User::isForeignEmailAddress($entry)) {
982 $foreign[] = $entry;
983 } else {
984 list($local_part, ) = explode('@', $entry);
985 $local[] = $local_part;
986 }
987 }
988
989 if (count($foreign) > 0) {
990 $sub = $uf->addEmailRedirectFilter($foreign);
991 $cond[] = XDB::format('ra' . $sub . '.redirect IS NOT NULL OR ra.redirect IN {?}', $foreign);
992 }
993 if (count($local) > 0) {
994 $sub = $uf->addAliasFilter($local);
995 $cond[] = 'sa' . $sub . '.email IS NOT NULL';
996 }
997 return '(' . implode(') OR (', $cond) . ')';
998 }
999 }
1000 // }}}
1001 // {{{ class UFC_Address
1002 abstract class UFC_Address extends UserFilterCondition
1003 {
1004 /** Valid address type ('hq' is reserved for company addresses)
1005 */
1006 const TYPE_HOME = 1;
1007 const TYPE_PRO = 2;
1008 const TYPE_ANY = 3;
1009
1010 /** Text for these types
1011 */
1012 protected static $typetexts = array(
1013 self::TYPE_HOME => 'home',
1014 self::TYPE_PRO => 'pro',
1015 );
1016
1017 protected $type;
1018
1019 /** Flags for addresses
1020 */
1021 const FLAG_CURRENT = 0x0001;
1022 const FLAG_TEMP = 0x0002;
1023 const FLAG_SECOND = 0x0004;
1024 const FLAG_MAIL = 0x0008;
1025 const FLAG_CEDEX = 0x0010;
1026
1027 // Binary OR of those flags
1028 const FLAG_ANY = 0x001F;
1029
1030 /** Text of these flags
1031 */
1032 protected static $flagtexts = array(
1033 self::FLAG_CURRENT => 'current',
1034 self::FLAG_TEMP => 'temporary',
1035 self::FLAG_SECOND => 'secondary',
1036 self::FLAG_MAIL => 'mail',
1037 self::FLAG_CEDEX => 'cedex',
1038 );
1039
1040 protected $flags;
1041
1042 public function __construct($type = null, $flags = null)
1043 {
1044 $this->type = $type;
1045 $this->flags = $flags;
1046 }
1047
1048 protected function initConds($sub, $vis_cond)
1049 {
1050 $conds = array($vis_cond);
1051
1052 $types = array();
1053 foreach (self::$typetexts as $flag => $type) {
1054 if ($flag & $this->type) {
1055 $types[] = $type;
1056 }
1057 }
1058 if (count($types)) {
1059 $conds[] = XDB::format($sub . '.type IN {?}', $types);
1060 }
1061
1062 if ($this->flags != self::FLAG_ANY) {
1063 foreach(self::$flagtexts as $flag => $text) {
1064 if ($flag & $this->flags) {
1065 $conds[] = 'FIND_IN_SET(' . XDB::format('{?}', $text) . ', ' . $sub . '.flags)';
1066 }
1067 }
1068 }
1069 return $conds;
1070 }
1071
1072 }
1073 // }}}
1074 // {{{ class UFC_AddressText
1075 /** Select users based on their address, using full text search
1076 * @param $text Text for filter in fulltext search
1077 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
1078 * @param $type Filter on address type
1079 * @param $flags Filter on address flags
1080 * @param $country Filter on address country
1081 * @param $locality Filter on address locality
1082 */
1083 class UFC_AddressText extends UFC_Address
1084 {
1085
1086 private $text;
1087 private $textSearchMode;
1088
1089 public function __construct($text = null, $textSearchMode = XDB::WILDCARD_CONTAINS,
1090 $type = null, $flags = self::FLAG_ANY, $country = null, $locality = null)
1091 {
1092 parent::__construct($type, $flags);
1093 $this->text = $text;
1094 $this->textSearchMode = $textSearchMode;
1095 $this->country = $country;
1096 $this->locality = $locality;
1097 }
1098
1099 private function mkMatch($txt)
1100 {
1101 return XDB::formatWildcards($this->textSearchMode, $txt);
1102 }
1103
1104 public function buildCondition(PlFilter $uf)
1105 {
1106 $sub = $uf->addAddressFilter();
1107 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1108 if ($this->text != null) {
1109 $conds[] = $sub . '.text' . $this->mkMatch($this->text);
1110 }
1111
1112 if ($this->country != null) {
1113 $subc = $uf->addAddressCountryFilter();
1114 $subconds = array();
1115 $subconds[] = $subc . '.country' . $this->mkMatch($this->country);
1116 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country);
1117 $conds[] = implode(' OR ', $subconds);
1118 }
1119
1120 if ($this->locality != null) {
1121 $subl = $uf->addAddressLocalityFilter();
1122 $conds[] = $subl . '.name' . $this->mkMatch($this->locality);
1123 }
1124
1125 return implode(' AND ', $conds);
1126 }
1127 }
1128 // }}}
1129 // {{{ class UFC_AddressField
1130 /** Filters users based on their address,
1131 * @param $val Either a code for one of the fields, or an array of such codes
1132 * @param $fieldtype The type of field to look for
1133 * @param $type Filter on address type
1134 * @param $flags Filter on address flags
1135 */
1136 class UFC_AddressField extends UFC_Address
1137 {
1138 const FIELD_COUNTRY = 1;
1139 const FIELD_ADMAREA = 2;
1140 const FIELD_SUBADMAREA = 3;
1141 const FIELD_LOCALITY = 4;
1142 const FIELD_ZIPCODE = 5;
1143
1144 /** Data of the filter
1145 */
1146 private $val;
1147 private $fieldtype;
1148
1149 public function __construct($val, $fieldtype, $type = null, $flags = self::FLAG_ANY)
1150 {
1151 parent::__construct($type, $flags);
1152
1153 if (!is_array($val)) {
1154 $val = array($val);
1155 }
1156 $this->val = $val;
1157 $this->fieldtype = $fieldtype;
1158 }
1159
1160 public function buildCondition(PlFilter $uf)
1161 {
1162 $sub = $uf->addAddressFilter();
1163 $conds = $this->initConds($sub, $uf->getVisibilityCondition($sub . '.pub'));
1164
1165 switch ($this->fieldtype) {
1166 case self::FIELD_COUNTRY:
1167 $field = 'countryId';
1168 break;
1169 case self::FIELD_ADMAREA:
1170 $field = 'administrativeAreaId';
1171 break;
1172 case self::FIELD_SUBADMAREA:
1173 $field = 'subAdministrativeAreaId';
1174 break;
1175 case self::FIELD_LOCALITY:
1176 $field = 'localityId';
1177 break;
1178 case self::FIELD_ZIPCODE:
1179 $field = 'postalCode';
1180 break;
1181 default:
1182 Platal::page()->killError('Invalid address field type: ' . $this->fieldtype);
1183 }
1184 $conds[] = XDB::format($sub . '.' . $field . ' IN {?}', $this->val);
1185
1186 return implode(' AND ', $conds);
1187 }
1188 }
1189 // }}}
1190 // {{{ class UFC_Corps
1191 /** Filters users based on the corps they belong to
1192 * @param $corps Corps we are looking for (abbreviation)
1193 * @param $type Whether we search for original or current corps
1194 */
1195 class UFC_Corps extends UserFilterCondition
1196 {
1197 const CURRENT = 1;
1198 const ORIGIN = 2;
1199
1200 private $corps;
1201 private $type;
1202
1203 public function __construct($corps, $type = self::CURRENT)
1204 {
1205 $this->corps = $corps;
1206 $this->type = $type;
1207 }
1208
1209 public function buildCondition(PlFilter $uf)
1210 {
1211 /** Tables shortcuts:
1212 * pc for profile_corps,
1213 * pceo for profile_corps_enum - orginal
1214 * pcec for profile_corps_enum - current
1215 */
1216 $sub = $uf->addCorpsFilter($this->type);
1217 $cond = $sub . '.abbreviation = ' . $corps;
1218 $cond .= ' AND ' . $uf->getVisibilityCondition($sub . '.corps_pub');
1219 return $cond;
1220 }
1221 }
1222 // }}}
1223 // {{{ class UFC_Corps_Rank
1224 /** Filters users based on their rank in the corps
1225 * @param $rank Rank we are looking for (abbreviation)
1226 */
1227 class UFC_Corps_Rank extends UserFilterCondition
1228 {
1229 private $rank;
1230 public function __construct($rank)
1231 {
1232 $this->rank = $rank;
1233 }
1234
1235 public function buildCondition(PlFilter $uf)
1236 {
1237 /** Tables shortcuts:
1238 * pc for profile_corps
1239 * pcr for profile_corps_rank
1240 */
1241 $sub = $uf->addCorpsRankFilter();
1242 $cond = $sub . '.abbreviation = ' . $rank;
1243 // XXX(x2006barrois): find a way to get rid of that hardcoded
1244 // reference to 'pc'.
1245 $cond .= ' AND ' . $uf->getVisibilityCondition('pc.corps_pub');
1246 return $cond;
1247 }
1248 }
1249 // }}}
1250 // {{{ class UFC_Job_Company
1251 /** Filters users based on the company they belong to
1252 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
1253 * @param $value The searched value
1254 */
1255 class UFC_Job_Company extends UserFilterCondition
1256 {
1257 const JOBID = 'id';
1258 const JOBNAME = 'name';
1259 const JOBACRONYM = 'acronym';
1260
1261 private $type;
1262 private $value;
1263
1264 public function __construct($type, $value)
1265 {
1266 $this->assertType($type);
1267 $this->type = $type;
1268 $this->value = $value;
1269 }
1270
1271 private function assertType($type)
1272 {
1273 if ($type != self::JOBID && $type != self::JOBNAME && $type != self::JOBACRONYM) {
1274 Platal::page()->killError("Type de recherche non valide.");
1275 }
1276 }
1277
1278 public function buildCondition(PlFilter $uf)
1279 {
1280 $sub = $uf->addJobCompanyFilter();
1281 $cond = $sub . '.' . $this->type . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value);
1282 $jsub = $uf->addJobFilter();
1283 $cond .= ' AND ' . $uf->getVisibilityCondition($jsub . '.pub');
1284 return $cond;
1285 }
1286 }
1287 // }}}
1288 // {{{ class UFC_Job_Terms
1289 /** Filters users based on the job terms they assigned to one of their
1290 * jobs.
1291 * @param $val The ID of the job term, or an array of such IDs
1292 */
1293 class UFC_Job_Terms extends UserFilterCondition
1294 {
1295 private $val;
1296
1297 public function __construct($val)
1298 {
1299 if (!is_array($val)) {
1300 $val = array($val);
1301 }
1302 $this->val = $val;
1303 }
1304
1305 public function buildCondition(PlFilter $uf)
1306 {
1307 $sub = $uf->addJobTermsFilter(count($this->val));
1308 $conditions = array();
1309 foreach ($this->val as $i => $jtid) {
1310 $conditions[] = $sub[$i] . '.jtid_1 = ' . XDB::escape($jtid);
1311 }
1312 $jsub = $uf->addJobFilter();
1313 $conditions[] = $uf->getVisibilityCondition($jsub . '.pub');
1314 return implode(' AND ', $conditions);
1315 }
1316 }
1317 // }}}
1318 // {{{ class UFC_Job_Description
1319 /** Filters users based on their job description
1320 * @param $description The text being searched for
1321 * @param $fields The fields to search for (CV, user-defined)
1322 */
1323 class UFC_Job_Description extends UserFilterCondition
1324 {
1325
1326 private $description;
1327 private $fields;
1328
1329 public function __construct($description, $fields)
1330 {
1331 $this->fields = $fields;
1332 $this->description = $description;
1333 }
1334
1335 public function buildCondition(PlFilter $uf)
1336 {
1337 $conds = array();
1338
1339 $jsub = $uf->addJobFilter();
1340 // CV is private => if only CV requested, and not private,
1341 // don't do anything. Otherwise restrict to standard job visibility.
1342 if ($this->fields == UserFilter::JOB_CV) {
1343 if ($uf->getVisibilityLevel() != ProfileVisibility::VIS_PRIVATE) {
1344 return self::COND_TRUE;
1345 }
1346 }
1347 if ($this->fields & UserFilter::JOB_USERDEFINED) {
1348 $conds[] = $jsub . '.description ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
1349 }
1350 if ($this->fields & UserFilter::JOB_CV && $uf->getVisibilityLevel() == ProfileVisibility::VIS_PRIVATE) {
1351 $uf->requireProfiles();
1352 $conds[] = 'p.cv ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
1353 }
1354 if (count($conds) == 0) {
1355 return self::COND_TRUE;
1356 }
1357 return $uf->getVisibilityCondition($jsub . '.pub') . ' AND ( ' . implode(' OR ', $conds) . ' )';
1358 }
1359 }
1360 // }}}
1361 // {{{ class UFC_Networking
1362 /** Filters users based on network identity (IRC, ...)
1363 * @param $type Type of network (-1 for any)
1364 * @param $value Value to search
1365 */
1366 class UFC_Networking extends UserFilterCondition
1367 {
1368 private $type;
1369 private $value;
1370
1371 public function __construct($type, $value)
1372 {
1373 $this->type = $type;
1374 $this->value = $value;
1375 }
1376
1377 public function buildCondition(PlFilter $uf)
1378 {
1379 $sub = $uf->addNetworkingFilter();
1380 $conds = array();
1381 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1382 $conds[] = $sub . '.address ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value);
1383 if ($this->type != -1) {
1384 $conds[] = $sub . '.nwid = ' . XDB::format('{?}', $this->type);
1385 }
1386 return implode(' AND ', $conds);
1387 }
1388 }
1389 // }}}
1390 // {{{ class UFC_Phone
1391 /** Filters users based on their phone number
1392 * @param $num_type Type of number (pro/user/home)
1393 * @param $phone_type Type of phone (fixed/mobile/fax)
1394 * @param $number Phone number
1395 */
1396 class UFC_Phone extends UserFilterCondition
1397 {
1398 const NUM_PRO = 'pro';
1399 const NUM_USER = 'user';
1400 const NUM_HOME = 'address';
1401 const NUM_ANY = 'any';
1402
1403 const PHONE_FIXED = 'fixed';
1404 const PHONE_MOBILE = 'mobile';
1405 const PHONE_FAX = 'fax';
1406 const PHONE_ANY = 'any';
1407
1408 private $num_type;
1409 private $phone_type;
1410 private $number;
1411
1412 public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY)
1413 {
1414 $phone = new Phone(array('display' => $number));
1415 $phone->format();
1416 $this->number = $phone->search();
1417 $this->num_type = $num_type;
1418 $this->phone_type = $phone_type;
1419 }
1420
1421 public function buildCondition(PlFilter $uf)
1422 {
1423 $sub = $uf->addPhoneFilter();
1424 $conds = array();
1425
1426 $conds[] = $uf->getVisibilityCondition($sub . '.pub');
1427
1428 $conds[] = $sub . '.search_tel = ' . XDB::format('{?}', $this->number);
1429 if ($this->num_type != self::NUM_ANY) {
1430 $conds[] = $sub . '.link_type = ' . XDB::format('{?}', $this->num_type);
1431 }
1432 if ($this->phone_type != self::PHONE_ANY) {
1433 $conds[] = $sub . '.tel_type = ' . XDB::format('{?}', $this->phone_type);
1434 }
1435 return implode(' AND ', $conds);
1436 }
1437 }
1438 // }}}
1439 // {{{ class UFC_Medal
1440 /** Filters users based on their medals
1441 * @param $medal ID of the medal
1442 * @param $grade Grade of the medal (null for 'any')
1443 */
1444 class UFC_Medal extends UserFilterCondition
1445 {
1446 private $medal;
1447 private $grade;
1448
1449 public function __construct($medal, $grade = null)
1450 {
1451 $this->medal = $medal;
1452 $this->grade = $grade;
1453 }
1454
1455 public function buildCondition(PlFilter $uf)
1456 {
1457 $conds = array();
1458
1459 // This will require profiles => table 'p' will be available.
1460 $sub = $uf->addMedalFilter();
1461
1462 $conds[] = $uf->getVisibilityCondition('p.medals_pub');
1463
1464 $conds[] = $sub . '.mid = ' . XDB::format('{?}', $this->medal);
1465 if ($this->grade != null) {
1466 $conds[] = $sub . '.gid = ' . XDB::format('{?}', $this->grade);
1467 }
1468 return implode(' AND ', $conds);
1469 }
1470 }
1471 // }}}
1472 // {{{ class UFC_Photo
1473 /** Filters profiles with photo
1474 */
1475 class UFC_Photo extends UserFilterCondition
1476 {
1477 public function buildCondition(PlFilter $uf)
1478 {
1479 $sub = $uf->addPhotoFilter();
1480 return $sub . '.attach IS NOT NULL AND ' . $uf->getVisibilityCondition($sub . '.pub');
1481 }
1482 }
1483 // }}}
1484 // {{{ class UFC_Mentor
1485 class UFC_Mentor extends UserFilterCondition
1486 {
1487 public function buildCondition(PlFilter $uf)
1488 {
1489 $sub = $uf->addMentorFilter(UserFilter::MENTOR);
1490 return $sub . '.expertise IS NOT NULL';
1491 }
1492 }
1493 // }}}
1494 // {{{ class UFC_Mentor_Expertise
1495 /** Filters users by mentoring expertise
1496 * @param $expertise Domain of expertise
1497 */
1498 class UFC_Mentor_Expertise extends UserFilterCondition
1499 {
1500 private $expertise;
1501
1502 public function __construct($expertise)
1503 {
1504 $this->expertise = $expertise;
1505 }
1506
1507 public function buildCondition(PlFilter $uf)
1508 {
1509 $sub = $uf->addMentorFilter(UserFilter::MENTOR_EXPERTISE);
1510 return $sub . '.expertise ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->expertise);
1511 }
1512 }
1513 // }}}
1514 // {{{ class UFC_Mentor_Country
1515 /** Filters users by mentoring country
1516 * @param $country Two-letters code of country being searched
1517 */
1518 class UFC_Mentor_Country extends UserFilterCondition
1519 {
1520 private $country;
1521
1522 public function __construct()
1523 {
1524 $this->country = pl_flatten(func_get_args());
1525 }
1526
1527 public function buildCondition(PlFilter $uf)
1528 {
1529 $sub = $uf->addMentorFilter(UserFilter::MENTOR_COUNTRY);
1530 return $sub . '.country IN ' . XDB::format('{?}', $this->country);
1531 }
1532 }
1533 // }}}
1534 // {{{ class UFC_Mentor_Terms
1535 /** Filters users based on the job terms they used in mentoring.
1536 * @param $val The ID of the job term, or an array of such IDs
1537 */
1538 class UFC_Mentor_Terms extends UserFilterCondition
1539 {
1540 private $val;
1541
1542 public function __construct($val)
1543 {
1544 $this->val = $val;
1545 }
1546
1547 public function buildCondition(PlFilter $uf)
1548 {
1549 $sub = $uf->addMentorFilter(UserFilter::MENTOR_TERM);
1550 return $sub . '.jtid_1 = ' . XDB::escape($this->val);
1551 }
1552 }
1553 // }}}
1554 // {{{ class UFC_UserRelated
1555 /** Filters users based on a relation toward a user
1556 * @param $user User to which searched users are related
1557 */
1558 abstract class UFC_UserRelated extends UserFilterCondition
1559 {
1560 protected $user;
1561 public function __construct(PlUser $user)
1562 {
1563 $this->user =& $user;
1564 }
1565 }
1566 // }}}
1567 // {{{ class UFC_Contact
1568 /** Filters users who belong to selected user's contacts
1569 */
1570 class UFC_Contact extends UFC_UserRelated
1571 {
1572 public function buildCondition(PlFilter $uf)
1573 {
1574 $sub = $uf->addContactFilter($this->user->id());
1575 return 'c' . $sub . '.contact IS NOT NULL';
1576 }
1577 }
1578 // }}}
1579 // {{{ class UFC_WatchRegistration
1580 /** Filters users being watched by selected user
1581 */
1582 class UFC_WatchRegistration extends UFC_UserRelated
1583 {
1584 public function buildCondition(PlFilter $uf)
1585 {
1586 if (!$this->user->watchType('registration')) {
1587 return PlFilterCondition::COND_FALSE;
1588 }
1589 $uids = $this->user->watchUsers();
1590 if (count($uids) == 0) {
1591 return PlFilterCondition::COND_FALSE;
1592 } else {
1593 return XDB::format('$UID IN {?}', $uids);
1594 }
1595 }
1596 }
1597 // }}}
1598 // {{{ class UFC_WatchPromo
1599 /** Filters users belonging to a promo watched by selected user
1600 * @param $user Selected user (the one watching promo)
1601 * @param $grade Formation the user is watching
1602 */
1603 class UFC_WatchPromo extends UFC_UserRelated
1604 {
1605 private $grade;
1606 public function __construct(PlUser $user, $grade = UserFilter::GRADE_ING)
1607 {
1608 parent::__construct($user);
1609 $this->grade = $grade;
1610 }
1611
1612 public function buildCondition(PlFilter $uf)
1613 {
1614 $promos = $this->user->watchPromos();
1615 if (count($promos) == 0) {
1616 return PlFilterCondition::COND_FALSE;
1617 } else {
1618 $sube = $uf->addEducationFilter(true, $this->grade);
1619 $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
1620 return XDB::format($field . ' IN {?}', $promos);
1621 }
1622 }
1623 }
1624 // }}}
1625 // {{{ class UFC_WatchContact
1626 /** Filters users watched by selected user
1627 */
1628 class UFC_WatchContact extends UFC_Contact
1629 {
1630 public function buildCondition(PlFilter $uf)
1631 {
1632 if (!$this->user->watchContacts()) {
1633 return PlFilterCondition::COND_FALSE;
1634 }
1635 return parent::buildCondition($uf);
1636 }
1637 }
1638 // }}}
1639 // {{{ class UFC_MarketingHash
1640 /** Filters users using the hash generated
1641 * to send marketing emails to him.
1642 */
1643 class UFC_MarketingHash extends UserFilterCondition
1644 {
1645 private $hash;
1646
1647 public function __construct($hash)
1648 {
1649 $this->hash = $hash;
1650 }
1651
1652 public function buildCondition(PlFilter $uf)
1653 {
1654 $table = $uf->addMarketingHash();
1655 return XDB::format('rm.hash = {?}', $this->hash);
1656 }
1657 }
1658 // }}}
1659
1660 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1661 ?>