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