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