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