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