Phone class: new class to access profile_phones.
[platal.git] / classes / userfilter.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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
23 /******************
24 * CONDITIONS
25 ******************/
26
27 // {{{ interface UserFilterCondition
28 /** This interface describe objects which filter users based
29 * on various parameters.
30 * The parameters of the filter must be given to the constructor.
31 * The buildCondition function is called by UserFilter when
32 * actually building the query. That function must call
33 * $uf->addWheteverFilter so that the UserFilter makes
34 * adequate joins. It must return the 'WHERE' condition to use
35 * with the filter.
36 */
37 interface UserFilterCondition extends PlFilterCondition
38 {
39 }
40 // }}}
41
42 // {{{ class UFC_HasProfile
43 /** Filters users who have a profile
44 */
45 class UFC_HasProfile implements UserFilterCondition
46 {
47 public function buildCondition(PlFilter &$uf)
48 {
49 $uf->requireProfiles();
50 return '$PID IS NOT NULL';
51 }
52 }
53 // }}}
54
55 // {{{ class UFC_Hruid
56 /** Filters users based on their hruid
57 * @param $val Either an hruid, or a list of those
58 */
59 class UFC_Hruid implements UserFilterCondition
60 {
61 private $hruids;
62
63 public function __construct()
64 {
65 $this->hruids = pl_flatten(func_get_args());
66 }
67
68 public function buildCondition(PlFilter &$uf)
69 {
70 $uf->requireAccounts();
71 return XDB::format('a.hruid IN {?}', $this->hruids);
72 }
73 }
74 // }}}
75
76 // {{{ class UFC_Hrpid
77 /** Filters users based on the hrpid of their profiles
78 * @param $val Either an hrpid, or a list of those
79 */
80 class UFC_Hrpid implements UserFilterCondition
81 {
82 private $hrpids;
83
84 public function __construct()
85 {
86 $this->hrpids = pl_flatten(func_get_args());
87 }
88
89 public function buildCondition(PlFilter &$uf)
90 {
91 $uf->requireProfiles();
92 return XDB::format('p.hrpid IN {?}', $this->hrpids);
93 }
94 }
95 // }}}
96
97 // {{{ class UFC_Ip
98 /** Filters users based on one of their last IPs
99 * @param $ip IP from which connection are checked
100 */
101 class UFC_Ip implements UserFilterCondition
102 {
103 private $ip;
104
105 public function __construct($ip)
106 {
107 $this->ip = $ip;
108 }
109
110 public function buildCondition(PlFilter &$uf)
111 {
112 $sub = $uf->addLoggerFilter();
113 $ip = ip_to_uint($this->ip);
114 return XDB::format($sub . '.ip = {?} OR ' . $sub . '.forward_ip = {?}', $ip, $ip);
115 }
116 }
117 // }}}
118
119 // {{{ class UFC_Comment
120 class UFC_Comment implements UserFilterCondition
121 {
122 private $text;
123
124 public function __construct($text)
125 {
126 $this->text = $text;
127 }
128
129 public function buildCondition(PlFilter &$uf)
130 {
131 $uf->requireProfiles();
132 return 'p.freetext ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->text);
133 }
134 }
135 // }}}
136
137 // {{{ class UFC_Promo
138 /** Filters users based on promotion
139 * @param $comparison Comparison operator (>, =, ...)
140 * @param $grade Formation on which to restrict, UserFilter::DISPLAY for "any formation"
141 * @param $promo Promotion on which the filter is based
142 */
143 class UFC_Promo implements UserFilterCondition
144 {
145
146 private $grade;
147 private $promo;
148 private $comparison;
149
150 public function __construct($comparison, $grade, $promo)
151 {
152 $this->grade = $grade;
153 $this->comparison = $comparison;
154 $this->promo = $promo;
155 if ($this->grade != UserFilter::DISPLAY) {
156 UserFilter::assertGrade($this->grade);
157 }
158 if ($this->grade == UserFilter::DISPLAY && $this->comparison != '=') {
159 // XXX: we might try to guess the grade from the first char of the promo and forbid only '<= 2004', but allow '<= X2004'
160 Platal::page()->killError("Il n'est pas possible d'appliquer la comparaison '" . $this->comparison . "' aux promotions sans spécifier de formation (X/M/D)");
161 }
162 }
163
164 public function buildCondition(PlFilter &$uf)
165 {
166 if ($this->grade == UserFilter::DISPLAY) {
167 $sub = $uf->addDisplayFilter();
168 return XDB::format('pd' . $sub . '.promo ' . $this->comparison . ' {?}', $this->promo);
169 } else {
170 $sub = $uf->addEducationFilter(true, $this->grade);
171 $field = 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
172 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison . ' ' . XDB::format('{?}', $this->promo);
173 }
174 }
175 }
176 // }}}
177
178 // {{{ class UFC_SchoolId
179 /** Filters users based on their shoold identifier
180 * @param type Parameter type (Xorg, AX, School)
181 * @param value School id value
182 */
183 class UFC_SchoolId implements UserFilterCondition
184 {
185 const AX = 'ax';
186 const Xorg = 'xorg';
187 const School = 'school';
188
189 private $type;
190 private $id;
191
192 static public function assertType($type)
193 {
194 if ($type != self::AX && $type != self::Xorg && $type != self::School) {
195 Platal::page()->killError("Type de matricule invalide: $type");
196 }
197 }
198
199 public function __construct($type, $id)
200 {
201 $this->type = $type;
202 $this->id = $id;
203 self::assertType($type);
204 }
205
206 public function buildCondition(PlFilter &$uf)
207 {
208 $uf->requireProfiles();
209 $id = $this->id;
210 $type = $this->type;
211 if ($type == self::School) {
212 $type = self::Xorg;
213 $id = Profile::getXorgId($id);
214 }
215 return XDB::format('p.' . $type . '_id = {?}', $id);
216 }
217 }
218 // }}}
219
220 // {{{ class UFC_EducationSchool
221 /** Filters users by formation
222 * @param $val The formation to search (either ID or array of IDs)
223 */
224 class UFC_EducationSchool implements UserFilterCondition
225 {
226 private $val;
227
228 public function __construct()
229 {
230 $this->val = pl_flatten(func_get_args());
231 }
232
233 public function buildCondition(PlFilter &$uf)
234 {
235 $sub = $uf->addEducationFilter();
236 return XDB::format('pe' . $sub . '.eduid IN {?}', $this->val);
237 }
238 }
239 // }}}
240
241 // {{{ class UFC_EducationDegree
242 class UFC_EducationDegree implements UserFilterCondition
243 {
244 private $diploma;
245
246 public function __construct()
247 {
248 $this->diploma = pl_flatten(func_get_args());
249 }
250
251 public function buildCondition(PlFilter &$uf)
252 {
253 $sub = $uf->addEducationFilter();
254 return XDB::format('pe' . $sub . '.degreeid IN {?}', $this->diploma);
255 }
256 }
257 // }}}
258
259 // {{{ class UFC_EducationField
260 class UFC_EducationField implements UserFilterCondition
261 {
262 private $val;
263
264 public function __construct()
265 {
266 $this->val = pl_flatten(func_get_args());
267 }
268
269 public function buildCondition(PlFilter &$uf)
270 {
271 $sub = $uf->addEducationFilter();
272 return XDB::format('pe' . $sub . '.fieldid IN {?}', $this->val);
273 }
274 }
275 // }}}
276
277 // {{{ class UFC_Name
278 /** Filters users based on name
279 * @param $type Type of name field on which filtering is done (firstname, lastname...)
280 * @param $text Text on which to filter
281 * @param $mode Flag indicating search type (prefix, suffix, with particule...)
282 */
283 class UFC_Name implements UserFilterCondition
284 {
285 const EXACT = XDB::WILDCARD_EXACT; // 0x000
286 const PREFIX = XDB::WILDCARD_PREFIX; // 0x001
287 const SUFFIX = XDB::WILDCARD_SUFFIX; // 0x002
288 const CONTAINS = XDB::WILDCARD_CONTAINS; // 0x003
289 const PARTICLE = 0x004;
290 const VARIANTS = 0x008;
291
292 private $type;
293 private $text;
294 private $mode;
295
296 public function __construct($type, $text, $mode)
297 {
298 $this->type = $type;
299 $this->text = $text;
300 $this->mode = $mode;
301 }
302
303 private function buildNameQuery($type, $variant, $where, UserFilter &$uf)
304 {
305 $sub = $uf->addNameFilter($type, $variant);
306 return str_replace('$ME', 'pn' . $sub, $where);
307 }
308
309 public function buildCondition(PlFilter &$uf)
310 {
311 $left = '$ME.name';
312 if (($this->mode & self::PARTICLE) == self::PARTICLE) {
313 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
314 }
315 $right = XDB::formatWildcards($this->mode & self::CONTAINS, $this->text);
316
317 $cond = $left . $right;
318 $conds = array($this->buildNameQuery($this->type, null, $cond, $uf));
319 if (($this->mode & self::VARIANTS) != 0 && isset(Profile::$name_variants[$this->type])) {
320 foreach (Profile::$name_variants[$this->type] as $var) {
321 $conds[] = $this->buildNameQuery($this->type, $var, $cond, $uf);
322 }
323 }
324 return implode(' OR ', $conds);
325 }
326 }
327 // }}}
328
329 // {{{ class UFC_NameTokens
330 /** Selects users based on tokens in their name (for quicksearch)
331 * @param $tokens An array of tokens to search
332 * @param $flags Flags the tokens must have (e.g 'public' for public search)
333 * @param $soundex (bool) Whether those tokens are fulltext or soundex
334 */
335 class UFC_NameTokens implements UserFilterCondition
336 {
337 /* Flags */
338 const FLAG_PUBLIC = 'public';
339
340 private $tokens;
341 private $flags;
342 private $soundex;
343 private $exact;
344
345 public function __construct($tokens, $flags = array(), $soundex = false, $exact = false)
346 {
347 if (is_array($tokens)) {
348 $this->tokens = $tokens;
349 } else {
350 $this->tokens = array($tokens);
351 }
352 if (is_array($flags)) {
353 $this->flags = $flags;
354 } else {
355 $this->flags = array($flags);
356 }
357 $this->soundex = $soundex;
358 $this->exact = $exact;
359 }
360
361 public function buildCondition(PlFilter &$uf)
362 {
363 $conds = array();
364 foreach ($this->tokens as $i => $token) {
365 $sub = $uf->addNameTokensFilter($token);
366 if ($this->soundex) {
367 $c = XDB::format($sub . '.soundex = {?}', $token);
368 } else if ($this->exact) {
369 $c = XDB::format($sub . '.token = {?}', $token);
370 } else {
371 $c = $sub . '.token ' . XDB::formatWildcards(XDB::WILDCARD_PREFIX, $token);
372 }
373 if ($this->flags != null) {
374 $c .= XDB::format(' AND ' . $sub . '.flags IN {?}', $this->flags);
375 }
376 $conds[] = $c;
377 }
378
379 return implode(' AND ', $conds);
380 }
381 }
382 // }}}
383
384 // {{{ class UFC_Nationality
385 class UFC_Nationality implements UserFilterCondition
386 {
387 private $val;
388
389 public function __construct()
390 {
391 $this->val = pl_flatten(func_get_args());
392 }
393
394 public function buildCondition(PlFilter &$uf)
395 {
396 $uf->requireProfiles();
397 $nat = XDB::formatArray($this->val);
398 $conds = array(
399 'p.nationality1 IN ' . $nat,
400 'p.nationality2 IN ' . $nat,
401 'p.nationality3 IN ' . $nat,
402 );
403 return implode(' OR ', $conds);
404 }
405 }
406 // }}}
407
408 // {{{ class UFC_Dead
409 /** Filters users based on death date
410 * @param $comparison Comparison operator
411 * @param $date Date to which death date should be compared (DateTime object, string or timestamp)
412 */
413 class UFC_Dead implements UserFilterCondition
414 {
415 private $comparison;
416 private $date;
417
418 public function __construct($comparison = null, $date = null)
419 {
420 $this->comparison = $comparison;
421 $this->date = make_datetime($date);
422 }
423
424 public function buildCondition(PlFilter &$uf)
425 {
426 $uf->requireProfiles();
427 $str = 'p.deathdate IS NOT NULL';
428 if (!is_null($this->comparison)) {
429 $str .= ' AND p.deathdate ' . $this->comparison . ' ' . XDB::format('{?}', $this->date->format('Y-m-d'));
430 }
431 return $str;
432 }
433 }
434 // }}}
435
436 // {{{ class UFC_Registered
437 /** Filters users based on registration state
438 * @param $active Whether we want to use only "active" users (i.e with a valid redirection)
439 * @param $comparison Comparison operator
440 * @param $date Date to which users registration date should be compared
441 */
442 class UFC_Registered implements UserFilterCondition
443 {
444 private $active;
445 private $comparison;
446 private $date;
447
448 public function __construct($active = false, $comparison = null, $date = null)
449 {
450 $this->active = $active;
451 $this->comparison = $comparison;
452 $this->date = make_datetime($date);
453 }
454
455 public function buildCondition(PlFilter &$uf)
456 {
457 $uf->requireAccounts();
458 if ($this->active) {
459 $date = '$UID IS NOT NULL AND a.state = \'active\'';
460 } else {
461 $date = '$UID IS NOT NULL AND a.state != \'pending\'';
462 }
463 if (!is_null($this->comparison)) {
464 $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'));
465 }
466 return $date;
467 }
468 }
469 // }}}
470
471 // {{{ class UFC_ProfileUpdated
472 /** Filters users based on profile update date
473 * @param $comparison Comparison operator
474 * @param $date Date to which profile update date must be compared
475 */
476 class UFC_ProfileUpdated implements UserFilterCondition
477 {
478 private $comparison;
479 private $date;
480
481 public function __construct($comparison = null, $date = null)
482 {
483 $this->comparison = $comparison;
484 $this->date = $date;
485 }
486
487 public function buildCondition(PlFilter &$uf)
488 {
489 $uf->requireProfiles();
490 return 'p.last_change ' . $this->comparison . XDB::format(' {?}', date('Y-m-d H:i:s', $this->date));
491 }
492 }
493 // }}}
494
495 // {{{ class UFC_Birthday
496 /** Filters users based on next birthday date
497 * @param $comparison Comparison operator
498 * @param $date Date to which users next birthday date should be compared
499 */
500 class UFC_Birthday implements UserFilterCondition
501 {
502 private $comparison;
503 private $date;
504
505 public function __construct($comparison = null, $date = null)
506 {
507 $this->comparison = $comparison;
508 $this->date = $date;
509 }
510
511 public function buildCondition(PlFilter &$uf)
512 {
513 $uf->requireProfiles();
514 return 'p.next_birthday ' . $this->comparison . XDB::format(' {?}', date('Y-m-d', $this->date));
515 }
516 }
517 // }}}
518
519 // {{{ class UFC_Sex
520 /** Filters users based on sex
521 * @parm $sex One of User::GENDER_MALE or User::GENDER_FEMALE, for selecting users
522 */
523 class UFC_Sex implements UserFilterCondition
524 {
525 private $sex;
526 public function __construct($sex)
527 {
528 $this->sex = $sex;
529 }
530
531 public function buildCondition(PlFilter &$uf)
532 {
533 if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) {
534 return self::COND_FALSE;
535 } else {
536 $uf->requireProfiles();
537 return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male');
538 }
539 }
540 }
541 // }}}
542
543 // {{{ class UFC_Group
544 /** Filters users based on group membership
545 * @param $group Group whose members we are selecting
546 * @param $anim Whether to restrict selection to animators of that group
547 */
548 class UFC_Group implements UserFilterCondition
549 {
550 private $group;
551 private $anim;
552 public function __construct($group, $anim = false)
553 {
554 $this->group = $group;
555 $this->anim = $anim;
556 }
557
558 public function buildCondition(PlFilter &$uf)
559 {
560 $sub = $uf->addGroupFilter($this->group);
561 $where = 'gpm' . $sub . '.perms IS NOT NULL';
562 if ($this->anim) {
563 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
564 }
565 return $where;
566 }
567 }
568 // }}}
569
570 // {{{ class UFC_Binet
571 /** Selects users based on their belonging to a given (list of) binet
572 * @param $binet either a binet_id or an array of binet_ids
573 */
574 class UFC_Binet implements UserFilterCondition
575 {
576 private $val;
577
578 public function __construct()
579 {
580 $this->val = pl_flatten(func_get_args());
581 }
582
583 public function buildCondition(PlFilter &$uf)
584 {
585 $sub = $uf->addBinetsFilter();
586 return XDB::format($sub . '.binet_id IN {?}', $this->val);
587 }
588 }
589 // }}}
590
591 // {{{ class UFC_Section
592 /** Selects users based on section
593 * @param $section ID of the section
594 */
595 class UFC_Section implements UserFilterCondition
596 {
597 private $section;
598
599 public function __construct()
600 {
601 $this->section = pl_flatten(func_get_args());
602 }
603
604 public function buildCondition(PlFilter &$uf)
605 {
606 $uf->requireProfiles();
607 return XDB::format('p.section IN {?}', $this->section);
608 }
609 }
610 // }}}
611
612 // {{{ class UFC_Email
613 /** Filters users based on an email or a list of emails
614 * @param $emails List of emails whose owner must be selected
615 */
616 class UFC_Email implements UserFilterCondition
617 {
618 private $emails;
619 public function __construct()
620 {
621 $this->emails = pl_flatten(func_get_args());
622 }
623
624 public function buildCondition(PlFilter &$uf)
625 {
626 $foreign = array();
627 $virtual = array();
628 $aliases = array();
629 $cond = array();
630
631 if (count($this->emails) == 0) {
632 return PlFilterCondition::COND_TRUE;
633 }
634
635 foreach ($this->emails as $entry) {
636 if (User::isForeignEmailAddress($entry)) {
637 $foreign[] = $entry;
638 } else if (User::isVirtualEmailAddress($entry)) {
639 $virtual[] = $entry;
640 } else {
641 @list($user, $domain) = explode('@', $entry);
642 $aliases[] = $user;
643 }
644 }
645
646 if (count($foreign) > 0) {
647 $sub = $uf->addEmailRedirectFilter($foreign);
648 $cond[] = XDB::format('e' . $sub . '.email IS NOT NULL OR a.email IN {?}', $foreign);
649 }
650 if (count($virtual) > 0) {
651 $sub = $uf->addVirtualEmailFilter($virtual);
652 $cond[] = 'vr' . $sub . '.redirect IS NOT NULL';
653 }
654 if (count($aliases) > 0) {
655 $sub = $uf->addAliasFilter($aliases);
656 $cond[] = 'al' . $sub . '.alias IS NOT NULL';
657 }
658 return '(' . implode(') OR (', $cond) . ')';
659 }
660 }
661 // }}}
662
663 // {{{ class UFC_Address
664 abstract class UFC_Address implements UserFilterCondition
665 {
666 /** Valid address type ('hq' is reserved for company addresses)
667 */
668 const TYPE_HOME = 1;
669 const TYPE_PRO = 2;
670 const TYPE_ANY = 3;
671
672 /** Text for these types
673 */
674 protected static $typetexts = array(
675 self::TYPE_HOME => 'home',
676 self::TYPE_PRO => 'pro',
677 );
678
679 protected $type;
680
681 /** Flags for addresses
682 */
683 const FLAG_CURRENT = 0x0001;
684 const FLAG_TEMP = 0x0002;
685 const FLAG_SECOND = 0x0004;
686 const FLAG_MAIL = 0x0008;
687 const FLAG_CEDEX = 0x0010;
688
689 // Binary OR of those flags
690 const FLAG_ANY = 0x001F;
691
692 /** Text of these flags
693 */
694 protected static $flagtexts = array(
695 self::FLAG_CURRENT => 'current',
696 self::FLAG_TEMP => 'temporary',
697 self::FLAG_SECOND => 'secondary',
698 self::FLAG_MAIL => 'mail',
699 self::FLAG_CEDEX => 'cedex',
700 );
701
702 protected $flags;
703
704 public function __construct($type = null, $flags = null)
705 {
706 $this->type = $type;
707 $this->flags = $flags;
708 }
709
710 protected function initConds($sub)
711 {
712 $conds = array();
713 $types = array();
714 foreach (self::$typetexts as $flag => $type) {
715 if ($flag & $this->type) {
716 $types[] = $type;
717 }
718 }
719 if (count($types)) {
720 $conds[] = XDB::format($sub . '.type IN {?}', $types);
721 }
722
723 if ($this->flags != self::FLAG_ANY) {
724 foreach(self::$flagtexts as $flag => $text) {
725 if ($flag & $this->flags) {
726 $conds[] = 'FIND_IN_SET(' . XDB::format('{?}', $text) . ', ' . $sub . '.flags)';
727 }
728 }
729 }
730 return $conds;
731 }
732
733 }
734 // }}}
735
736 // {{{ class UFC_AddressText
737 /** Select users based on their address, using full text search
738 * @param $text Text for filter in fulltext search
739 * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
740 * @param $type Filter on address type
741 * @param $flags Filter on address flags
742 * @param $country Filter on address country
743 * @param $locality Filter on address locality
744 */
745 class UFC_AddressText extends UFC_Address
746 {
747
748 private $text;
749 private $textSearchMode;
750
751 public function __construct($text = null, $textSearchMode = XDB::WILDCARD_CONTAINS,
752 $type = null, $flags = self::FLAG_ANY, $country = null, $locality = null)
753 {
754 parent::__construct($type, $flags);
755 $this->text = $text;
756 $this->textSearchMode = $textSearchMode;
757 $this->country = $country;
758 $this->locality = $locality;
759 }
760
761 private function mkMatch($txt)
762 {
763 return XDB::formatWildcards($this->textSearchMode, $txt);
764 }
765
766 public function buildCondition(PlFilter &$uf)
767 {
768 $sub = $uf->addAddressFilter();
769 $conds = $this->initConds($sub);
770 if ($this->text != null) {
771 $conds[] = $sub . '.text' . $this->mkMatch($this->text);
772 }
773
774 if ($this->country != null) {
775 $subc = $uf->addAddressCountryFilter();
776 $subconds = array();
777 $subconds[] = $subc . '.country' . $this->mkMatch($this->country);
778 $subconds[] = $subc . '.countryFR' . $this->mkMatch($this->country);
779 $conds[] = implode(' OR ', $subconds);
780 }
781
782 if ($this->locality != null) {
783 $subl = $uf->addAddressLocalityFilter();
784 $conds[] = $subl . '.name' . $this->mkMatch($this->locality);
785 }
786
787 return implode(' AND ', $conds);
788 }
789 }
790 // }}}
791
792 // {{{ class UFC_AddressField
793 /** Filters users based on their address,
794 * @param $val Either a code for one of the fields, or an array of such codes
795 * @param $fieldtype The type of field to look for
796 * @param $type Filter on address type
797 * @param $flags Filter on address flags
798 */
799 class UFC_AddressField extends UFC_Address
800 {
801 const FIELD_COUNTRY = 1;
802 const FIELD_ADMAREA = 2;
803 const FIELD_SUBADMAREA = 3;
804 const FIELD_LOCALITY = 4;
805 const FIELD_ZIPCODE = 5;
806
807 /** Data of the filter
808 */
809 private $val;
810 private $fieldtype;
811
812 public function __construct($val, $fieldtype, $type = null, $flags = self::FLAG_ANY)
813 {
814 parent::__construct($type, $flags);
815
816 if (!is_array($val)) {
817 $val = array($val);
818 }
819 $this->val = $val;
820 $this->fieldtype = $fieldtype;
821 }
822
823 public function buildCondition(PlFilter &$uf)
824 {
825 $sub = $uf->addAddressFilter();
826 $conds = $this->initConds($sub);
827
828 switch ($this->fieldtype) {
829 case self::FIELD_COUNTRY:
830 $field = 'countryId';
831 break;
832 case self::FIELD_ADMAREA:
833 $field = 'administrativeAreaId';
834 break;
835 case self::FIELD_SUBADMAREA:
836 $field = 'subAdministrativeAreaId';
837 break;
838 case self::FIELD_LOCALITY:
839 $field = 'localityId';
840 break;
841 case self::FIELD_ZIPCODE:
842 $field = 'postalCode';
843 break;
844 default:
845 Platal::page()->killError('Invalid address field type: ' . $this->fieldtype);
846 }
847 $conds[] = XDB::format($sub . '.' . $field . ' IN {?}', $this->val);
848
849 return implode(' AND ', $conds);
850 }
851 }
852 // }}}
853
854 // {{{ class UFC_Corps
855 /** Filters users based on the corps they belong to
856 * @param $corps Corps we are looking for (abbreviation)
857 * @param $type Whether we search for original or current corps
858 */
859 class UFC_Corps implements UserFilterCondition
860 {
861 const CURRENT = 1;
862 const ORIGIN = 2;
863
864 private $corps;
865 private $type;
866
867 public function __construct($corps, $type = self::CURRENT)
868 {
869 $this->corps = $corps;
870 $this->type = $type;
871 }
872
873 public function buildCondition(PlFilter &$uf)
874 {
875 /** Tables shortcuts:
876 * pc for profile_corps,
877 * pceo for profile_corps_enum - orginal
878 * pcec for profile_corps_enum - current
879 */
880 $sub = $uf->addCorpsFilter($this->type);
881 $cond = $sub . '.abbreviation = ' . $corps;
882 return $cond;
883 }
884 }
885 // }}}
886
887 // {{{ class UFC_Corps_Rank
888 /** Filters users based on their rank in the corps
889 * @param $rank Rank we are looking for (abbreviation)
890 */
891 class UFC_Corps_Rank implements UserFilterCondition
892 {
893 private $rank;
894 public function __construct($rank)
895 {
896 $this->rank = $rank;
897 }
898
899 public function buildCondition(PlFilter &$uf)
900 {
901 /** Tables shortcuts:
902 * pcr for profile_corps_rank
903 */
904 $sub = $uf->addCorpsRankFilter();
905 $cond = $sub . '.abbreviation = ' . $rank;
906 return $cond;
907 }
908 }
909 // }}}
910
911 // {{{ class UFC_Job_Company
912 /** Filters users based on the company they belong to
913 * @param $type The field being searched (self::JOBID, self::JOBNAME or self::JOBACRONYM)
914 * @param $value The searched value
915 */
916 class UFC_Job_Company implements UserFilterCondition
917 {
918 const JOBID = 'id';
919 const JOBNAME = 'name';
920 const JOBACRONYM = 'acronym';
921
922 private $type;
923 private $value;
924
925 public function __construct($type, $value)
926 {
927 $this->assertType($type);
928 $this->type = $type;
929 $this->value = $value;
930 }
931
932 private function assertType($type)
933 {
934 if ($type != self::JOBID && $type != self::JOBNAME && $type != self::JOBACRONYM) {
935 Platal::page()->killError("Type de recherche non valide.");
936 }
937 }
938
939 public function buildCondition(PlFilter &$uf)
940 {
941 $sub = $uf->addJobCompanyFilter();
942 $cond = $sub . '.' . $this->type . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value);
943 return $cond;
944 }
945 }
946 // }}}
947
948 // {{{ class UFC_Job_Sectorization
949 /** Filters users based on the ((sub)sub)sector they work in
950 * @param $val The ID of the sector, or an array of such IDs
951 * @param $type The kind of search (subsubsector/subsector/sector)
952 */
953 class UFC_Job_Sectorization implements UserFilterCondition
954 {
955 private $val;
956 private $type;
957
958 public function __construct($val, $type = UserFilter::JOB_SECTOR)
959 {
960 self::assertType($type);
961 if (!is_array($val)) {
962 $val = array($val);
963 }
964 $this->val = $val;
965 $this->type = $type;
966 }
967
968 private static function assertType($type)
969 {
970 if ($type != UserFilter::JOB_SECTOR && $type != UserFilter::JOB_SUBSECTOR && $type != UserFilter::JOB_SUBSUBSECTOR) {
971 Platal::page()->killError("Type de secteur non valide.");
972 }
973 }
974
975 public function buildCondition(PlFilter &$uf)
976 {
977 $sub = $uf->addJobSectorizationFilter($this->type);
978 return $sub . '.id = ' . XDB::format('{?}', $this->val);
979 }
980 }
981 // }}}
982
983 // {{{ class UFC_Job_Description
984 /** Filters users based on their job description
985 * @param $description The text being searched for
986 * @param $fields The fields to search for (user-defined, ((sub|)sub|)sector)
987 */
988 class UFC_Job_Description implements UserFilterCondition
989 {
990
991 private $description;
992 private $fields;
993
994 public function __construct($description, $fields)
995 {
996 $this->fields = $fields;
997 $this->description = $description;
998 }
999
1000 public function buildCondition(PlFilter &$uf)
1001 {
1002 $conds = array();
1003 if ($this->fields & UserFilter::JOB_USERDEFINED) {
1004 $sub = $uf->addJobFilter();
1005 $conds[] = $sub . '.description ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
1006 }
1007 if ($this->fields & UserFilter::JOB_CV) {
1008 $uf->requireProfiles();
1009 $conds[] = 'p.cv ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
1010 }
1011 if ($this->fields & UserFilter::JOB_SECTOR) {
1012 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SECTOR);
1013 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
1014 }
1015 if ($this->fields & UserFilter::JOB_SUBSECTOR) {
1016 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSECTOR);
1017 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
1018 }
1019 if ($this->fields & UserFilter::JOB_SUBSUBSECTOR) {
1020 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSUBSECTOR);
1021 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
1022 $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_ALTERNATES);
1023 $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
1024 }
1025 return implode(' OR ', $conds);
1026 }
1027 }
1028 // }}}
1029
1030 // {{{ class UFC_Networking
1031 /** Filters users based on network identity (IRC, ...)
1032 * @param $type Type of network (-1 for any)
1033 * @param $value Value to search
1034 */
1035 class UFC_Networking implements UserFilterCondition
1036 {
1037 private $type;
1038 private $value;
1039
1040 public function __construct($type, $value)
1041 {
1042 $this->type = $type;
1043 $this->value = $value;
1044 }
1045
1046 public function buildCondition(PlFilter &$uf)
1047 {
1048 $sub = $uf->addNetworkingFilter();
1049 $conds = array();
1050 $conds[] = $sub . '.address ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value);
1051 if ($this->type != -1) {
1052 $conds[] = $sub . '.nwid = ' . XDB::format('{?}', $this->type);
1053 }
1054 return implode(' AND ', $conds);
1055 }
1056 }
1057 // }}}
1058
1059 // {{{ class UFC_Phone
1060 /** Filters users based on their phone number
1061 * @param $num_type Type of number (pro/user/home)
1062 * @param $phone_type Type of phone (fixed/mobile/fax)
1063 * @param $number Phone number
1064 */
1065 class UFC_Phone implements UserFilterCondition
1066 {
1067 const NUM_PRO = 'pro';
1068 const NUM_USER = 'user';
1069 const NUM_HOME = 'address';
1070 const NUM_ANY = 'any';
1071
1072 const PHONE_FIXED = 'fixed';
1073 const PHONE_MOBILE = 'mobile';
1074 const PHONE_FAX = 'fax';
1075 const PHONE_ANY = 'any';
1076
1077 private $num_type;
1078 private $phone_type;
1079 private $number;
1080
1081 public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY)
1082 {
1083 $phone = new Phone('display' => $number);
1084 $phone->format();
1085 $this->number = $phone->search();
1086 $this->num_type = $num_type;
1087 $this->phone_type = $phone_type;
1088 }
1089
1090 public function buildCondition(PlFilter &$uf)
1091 {
1092 $sub = $uf->addPhoneFilter();
1093 $conds = array();
1094 $conds[] = $sub . '.search_tel = ' . XDB::format('{?}', $this->number);
1095 if ($this->num_type != self::NUM_ANY) {
1096 $conds[] = $sub . '.link_type = ' . XDB::format('{?}', $this->num_type);
1097 }
1098 if ($this->phone_type != self::PHONE_ANY) {
1099 $conds[] = $sub . '.tel_type = ' . XDB::format('{?}', $this->phone_type);
1100 }
1101 return implode(' AND ', $conds);
1102 }
1103 }
1104 // }}}
1105
1106 // {{{ class UFC_Medal
1107 /** Filters users based on their medals
1108 * @param $medal ID of the medal
1109 * @param $grade Grade of the medal (null for 'any')
1110 */
1111 class UFC_Medal implements UserFilterCondition
1112 {
1113 private $medal;
1114 private $grade;
1115
1116 public function __construct($medal, $grade = null)
1117 {
1118 $this->medal = $medal;
1119 $this->grade = $grade;
1120 }
1121
1122 public function buildCondition(PlFilter &$uf)
1123 {
1124 $conds = array();
1125 $sub = $uf->addMedalFilter();
1126 $conds[] = $sub . '.mid = ' . XDB::format('{?}', $this->medal);
1127 if ($this->grade != null) {
1128 $conds[] = $sub . '.gid = ' . XDB::format('{?}', $this->grade);
1129 }
1130 return implode(' AND ', $conds);
1131 }
1132 }
1133 // }}}
1134
1135 // {{{ class UFC_Photo
1136 /** Filters profiles with photo
1137 */
1138 class UFC_Photo implements UserFilterCondition
1139 {
1140 public function buildCondition(PlFilter &$uf)
1141 {
1142 $uf->addPhotoFilter();
1143 return 'photo.attach IS NOT NULL';
1144 }
1145 }
1146 // }}}
1147
1148 // {{{ class UFC_Mentor_Expertise
1149 /** Filters users by mentoring expertise
1150 * @param $expertise Domain of expertise
1151 */
1152 class UFC_Mentor_Expertise implements UserFilterCondition
1153 {
1154 private $expertise;
1155
1156 public function __construct($expertise)
1157 {
1158 $this->expertise = $expertise;
1159 }
1160
1161 public function buildCondition(PlFilter &$uf)
1162 {
1163 $sub = $uf->addMentorFilter(UserFilter::MENTOR_EXPERTISE);
1164 return $sub . '.expertise ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->expertise);
1165 }
1166 }
1167 // }}}
1168
1169 // {{{ class UFC_Mentor_Country
1170 /** Filters users by mentoring country
1171 * @param $country Two-letters code of country being searched
1172 */
1173 class UFC_Mentor_Country implements UserFilterCondition
1174 {
1175 private $country;
1176
1177 public function __construct()
1178 {
1179 $this->country = pl_flatten(func_get_args());
1180 }
1181
1182 public function buildCondition(PlFilter &$uf)
1183 {
1184 $sub = $uf->addMentorFilter(UserFilter::MENTOR_COUNTRY);
1185 return $sub . '.country IN ' . XDB::format('{?}', $this->country);
1186 }
1187 }
1188 // }}}
1189
1190 // {{{ class UFC_Mentor_Sectorization
1191 /** Filters users based on mentoring (sub|)sector
1192 * @param $sector ID of (sub)sector
1193 * @param $type Whether we are looking for a sector or a subsector
1194 */
1195 class UFC_Mentor_Sectorization implements UserFilterCondition
1196 {
1197 const SECTOR = 1;
1198 const SUBSECTOR = 2;
1199 private $sector;
1200 private $type;
1201
1202 public function __construct($sector, $type = self::SECTOR)
1203 {
1204 $this->sector = $sector;
1205 $this->type = $type;
1206 }
1207
1208 public function buildCondition(PlFilter &$uf)
1209 {
1210 $sub = $uf->addMentorFilter(UserFilter::MENTOR_SECTOR);
1211 if ($this->type == self::SECTOR) {
1212 $field = 'sectorid';
1213 } else {
1214 $field = 'subsectorid';
1215 }
1216 return $sub . '.' . $field . ' = ' . XDB::format('{?}', $this->sector);
1217 }
1218 }
1219 // }}}
1220
1221 // {{{ class UFC_UserRelated
1222 /** Filters users based on a relation toward a user
1223 * @param $user User to which searched users are related
1224 */
1225 abstract class UFC_UserRelated implements UserFilterCondition
1226 {
1227 protected $user;
1228 public function __construct(PlUser &$user)
1229 {
1230 $this->user =& $user;
1231 }
1232 }
1233 // }}}
1234
1235 // {{{ class UFC_Contact
1236 /** Filters users who belong to selected user's contacts
1237 */
1238 class UFC_Contact extends UFC_UserRelated
1239 {
1240 public function buildCondition(PlFilter &$uf)
1241 {
1242 $sub = $uf->addContactFilter($this->user->id());
1243 return 'c' . $sub . '.contact IS NOT NULL';
1244 }
1245 }
1246 // }}}
1247
1248 // {{{ class UFC_WatchRegistration
1249 /** Filters users being watched by selected user
1250 */
1251 class UFC_WatchRegistration extends UFC_UserRelated
1252 {
1253 public function buildCondition(PlFilter &$uf)
1254 {
1255 if (!$this->user->watchType('registration')) {
1256 return PlFilterCondition::COND_FALSE;
1257 }
1258 $uids = $this->user->watchUsers();
1259 if (count($uids) == 0) {
1260 return PlFilterCondition::COND_FALSE;
1261 } else {
1262 return XDB::format('$UID IN {?}', $uids);
1263 }
1264 }
1265 }
1266 // }}}
1267
1268 // {{{ class UFC_WatchPromo
1269 /** Filters users belonging to a promo watched by selected user
1270 * @param $user Selected user (the one watching promo)
1271 * @param $grade Formation the user is watching
1272 */
1273 class UFC_WatchPromo extends UFC_UserRelated
1274 {
1275 private $grade;
1276 public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING)
1277 {
1278 parent::__construct($user);
1279 $this->grade = $grade;
1280 }
1281
1282 public function buildCondition(PlFilter &$uf)
1283 {
1284 $promos = $this->user->watchPromos();
1285 if (count($promos) == 0) {
1286 return PlFilterCondition::COND_FALSE;
1287 } else {
1288 $sube = $uf->addEducationFilter(true, $this->grade);
1289 $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
1290 return XDB::format($field . ' IN {?}', $promos);
1291 }
1292 }
1293 }
1294 // }}}
1295
1296 // {{{ class UFC_WatchContact
1297 /** Filters users watched by selected user
1298 */
1299 class UFC_WatchContact extends UFC_Contact
1300 {
1301 public function buildCondition(PlFilter &$uf)
1302 {
1303 if (!$this->user->watchContacts()) {
1304 return PlFilterCondition::COND_FALSE;
1305 }
1306 return parent::buildCondition($uf);
1307 }
1308 }
1309 // }}}
1310
1311 // {{{ class UFC_MarketingHash
1312 /** Filters users using the hash generated
1313 * to send marketing emails to him.
1314 */
1315 class UFC_MarketingHash implements UserFilterCondition
1316 {
1317 private $hash;
1318
1319 public function __construct($hash)
1320 {
1321 $this->hash = $hash;
1322 }
1323
1324 public function buildCondition(PlFilter &$uf)
1325 {
1326 $table = $uf->addMarketingHash();
1327 return XDB::format('rm.hash = {?}', $this->hash);
1328 }
1329 }
1330 // }}}
1331
1332 /******************
1333 * ORDERS
1334 ******************/
1335
1336 // {{{ class UserFilterOrder
1337 /** Base class for ordering results of a query.
1338 * Parameters for the ordering must be given to the constructor ($desc for a
1339 * descending order).
1340 * The getSortTokens function is used to get actual ordering part of the query.
1341 */
1342 abstract class UserFilterOrder extends PlFilterOrder
1343 {
1344 /** This function must return the tokens to use for ordering
1345 * @param &$uf The UserFilter whose results must be ordered
1346 * @return The name of the field to use for ordering results
1347 */
1348 // abstract protected function getSortTokens(UserFilter &$uf);
1349 }
1350 // }}}
1351
1352 // {{{ class UFO_Promo
1353 /** Orders users by promotion
1354 * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
1355 * @param $desc Whether sort is descending
1356 */
1357 class UFO_Promo extends UserFilterOrder
1358 {
1359 private $grade;
1360
1361 public function __construct($grade = null, $desc = false)
1362 {
1363 parent::__construct($desc);
1364 $this->grade = $grade;
1365 }
1366
1367 protected function getSortTokens(PlFilter &$uf)
1368 {
1369 if (UserFilter::isGrade($this->grade)) {
1370 $sub = $uf->addEducationFilter($this->grade);
1371 return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade);
1372 } else {
1373 $sub = $uf->addDisplayFilter();
1374 return 'pd' . $sub . '.promo';
1375 }
1376 }
1377 }
1378 // }}}
1379
1380 // {{{ class UFO_Name
1381 /** Sorts users by name
1382 * @param $type Type of name on which to sort (firstname...)
1383 * @param $variant Variant of that name to use (marital, ordinary...)
1384 * @param $particle Set to true if particles should be included in the sorting order
1385 * @param $desc If sort order should be descending
1386 */
1387 class UFO_Name extends UserFilterOrder
1388 {
1389 private $type;
1390 private $variant;
1391 private $particle;
1392
1393 public function __construct($type, $variant = null, $particle = false, $desc = false)
1394 {
1395 parent::__construct($desc);
1396 $this->type = $type;
1397 $this->variant = $variant;
1398 $this->particle = $particle;
1399 }
1400
1401 protected function getSortTokens(PlFilter &$uf)
1402 {
1403 if (Profile::isDisplayName($this->type)) {
1404 $sub = $uf->addDisplayFilter();
1405 $token = 'pd' . $sub . '.' . $this->type;
1406 if ($uf->accountsRequired()) {
1407 $account_token = Profile::getAccountEquivalentName($this->type);
1408 return 'IFNULL(' . $token . ', a.' . $account_token . ')';
1409 } else {
1410 return $token;
1411 }
1412 } else {
1413 $sub = $uf->addNameFilter($this->type, $this->variant);
1414 if ($this->particle) {
1415 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
1416 } else {
1417 return 'pn' . $sub . '.name';
1418 }
1419 }
1420 }
1421 }
1422 // }}}
1423
1424 // {{{ class UFO_Score
1425 class UFO_Score extends UserFilterOrder
1426 {
1427 protected function getSortTokens(PlFilter &$uf)
1428 {
1429 $toks = $uf->getNameTokens();
1430 $scores = array();
1431
1432 // If there weren't any sort tokens, we shouldn't sort by score, sort by NULL instead
1433 if (count($toks) == 0) {
1434 return 'NULL';
1435 }
1436
1437 foreach ($toks as $sub => $token) {
1438 $scores[] = XDB::format('SUM(' . $sub . '.score + IF (' . $sub . '.token = {?}, 5, 0) )', $token);
1439 }
1440 return implode(' + ', $scores);
1441 }
1442 }
1443 // }}}
1444
1445 // {{{ class UFO_Registration
1446 /** Sorts users based on registration date
1447 */
1448 class UFO_Registration extends UserFilterOrder
1449 {
1450 protected function getSortTokens(PlFilter &$uf)
1451 {
1452 $uf->requireAccounts();
1453 return 'a.registration_date';
1454 }
1455 }
1456 // }}}
1457
1458 // {{{ class UFO_Birthday
1459 /** Sorts users based on next birthday date
1460 */
1461 class UFO_Birthday extends UserFilterOrder
1462 {
1463 protected function getSortTokens(PlFilter &$uf)
1464 {
1465 $uf->requireProfiles();
1466 return 'p.next_birthday';
1467 }
1468 }
1469 // }}}
1470
1471 // {{{ class UFO_ProfileUpdate
1472 /** Sorts users based on last profile update
1473 */
1474 class UFO_ProfileUpdate extends UserFilterOrder
1475 {
1476 protected function getSortTokens(PlFilter &$uf)
1477 {
1478 $uf->requireProfiles();
1479 return 'p.last_change';
1480 }
1481 }
1482 // }}}
1483
1484 // {{{ class UFO_Death
1485 /** Sorts users based on death date
1486 */
1487 class UFO_Death extends UserFilterOrder
1488 {
1489 protected function getSortTokens(PlFilter &$uf)
1490 {
1491 $uf->requireProfiles();
1492 return 'p.deathdate';
1493 }
1494 }
1495 // }}}
1496
1497 // {{{ class UFO_Uid
1498 /** Sorts users based on their uid
1499 */
1500 class UFO_Uid extends UserFilterOrder
1501 {
1502 protected function getSortTokens(PlFilter &$uf)
1503 {
1504 $uf->requireAccounts();
1505 return '$UID';
1506 }
1507 }
1508 // }}}
1509
1510 // {{{ class UFO_Hruid
1511 /** Sorts users based on their hruid
1512 */
1513 class UFO_Hruid extends UserFilterOrder
1514 {
1515 protected function getSortTokens(PlFilter &$uf)
1516 {
1517 $uf->requireAccounts();
1518 return 'a.hruid';
1519 }
1520 }
1521 // }}}
1522
1523 // {{{ class UFO_Pid
1524 /** Sorts users based on their pid
1525 */
1526 class UFO_Pid extends UserFilterOrder
1527 {
1528 protected function getSortTokens(PlFilter &$uf)
1529 {
1530 $uf->requireProfiles();
1531 return '$PID';
1532 }
1533 }
1534 // }}}
1535
1536 // {{{ class UFO_Hrpid
1537 /** Sorts users based on their hrpid
1538 */
1539 class UFO_Hrpid extends UserFilterOrder
1540 {
1541 protected function getSortTokens(PlFilter &$uf)
1542 {
1543 $uf->requireProfiles();
1544 return 'p.hrpid';
1545 }
1546 }
1547 // }}}
1548
1549
1550 /***********************************
1551 *********************************
1552 USER FILTER CLASS
1553 *********************************
1554 ***********************************/
1555
1556 // {{{ class UserFilter
1557 /** This class provides a convenient and centralized way of filtering users.
1558 *
1559 * Usage:
1560 * $uf = new UserFilter(new UFC_Blah($x, $y), new UFO_Coin($z, $t));
1561 *
1562 * Resulting UserFilter can be used to:
1563 * - get a list of User objects matching the filter
1564 * - get a list of UIDs matching the filter
1565 * - get the number of users matching the filter
1566 * - check whether a given User matches the filter
1567 * - filter a list of User objects depending on whether they match the filter
1568 *
1569 * Usage for UFC and UFO objects:
1570 * A UserFilter will call all private functions named XXXJoins.
1571 * These functions must return an array containing the list of join
1572 * required by the various UFC and UFO associated to the UserFilter.
1573 * Entries in those returned array are of the following form:
1574 * 'join_tablealias' => array('join_type', 'joined_table', 'join_criter')
1575 * which will be translated into :
1576 * join_type JOIN joined_table AS join_tablealias ON (join_criter)
1577 * in the final query.
1578 *
1579 * In the join_criter text, $ME is replaced with 'join_tablealias', $PID with
1580 * profile.pid, and $UID with accounts.uid.
1581 *
1582 * For each kind of "JOIN" needed, a function named addXXXFilter() should be defined;
1583 * its parameter will be used to set various private vars of the UserFilter describing
1584 * the required joins ; such a function shall return the "join_tablealias" to use
1585 * when referring to the joined table.
1586 *
1587 * For example, if data from profile_job must be available to filter results,
1588 * the UFC object will call $uf-addJobFilter(), which will set the 'with_pj' var and
1589 * return 'pj', the short name to use when referring to profile_job; when building
1590 * the query, calling the jobJoins function will return an array containing a single
1591 * row:
1592 * 'pj' => array('left', 'profile_job', '$ME.pid = $UID');
1593 *
1594 * The 'register_optional' function can be used to generate unique table aliases when
1595 * the same table has to be joined several times with different aliases.
1596 */
1597 class UserFilter extends PlFilter
1598 {
1599 protected $joinMethods = array();
1600
1601 protected $joinMetas = array(
1602 '$PID' => 'p.pid',
1603 '$UID' => 'a.uid',
1604 );
1605
1606 private $root;
1607 private $sort = array();
1608 private $query = null;
1609 private $orderby = null;
1610
1611 private $lastusercount = null;
1612 private $lastprofilecount = null;
1613
1614 public function __construct($cond = null, $sort = null)
1615 {
1616 if (empty($this->joinMethods)) {
1617 $class = new ReflectionClass('UserFilter');
1618 foreach ($class->getMethods() as $method) {
1619 $name = $method->getName();
1620 if (substr($name, -5) == 'Joins' && $name != 'buildJoins') {
1621 $this->joinMethods[] = $name;
1622 }
1623 }
1624 }
1625 if (!is_null($cond)) {
1626 if ($cond instanceof PlFilterCondition) {
1627 $this->setCondition($cond);
1628 }
1629 }
1630 if (!is_null($sort)) {
1631 if ($sort instanceof UserFilterOrder) {
1632 $this->addSort($sort);
1633 } else if (is_array($sort)) {
1634 foreach ($sort as $s) {
1635 $this->addSort($s);
1636 }
1637 }
1638 }
1639 }
1640
1641 private function buildQuery()
1642 {
1643 // The root condition is built first because some orders need info
1644 // available only once all UFC have set their conditions (UFO_Score)
1645 if (is_null($this->query)) {
1646 $where = $this->root->buildCondition($this);
1647 $where = str_replace(array_keys($this->joinMetas),
1648 $this->joinMetas,
1649 $where);
1650 }
1651 if (is_null($this->orderby)) {
1652 $orders = array();
1653 foreach ($this->sort as $sort) {
1654 $orders = array_merge($orders, $sort->buildSort($this));
1655 }
1656 if (count($orders) == 0) {
1657 $this->orderby = '';
1658 } else {
1659 $this->orderby = 'ORDER BY ' . implode(', ', $orders);
1660 }
1661 $this->orderby = str_replace(array_keys($this->joinMetas),
1662 $this->joinMetas,
1663 $this->orderby);
1664 }
1665 if (is_null($this->query)) {
1666 if ($this->with_accounts) {
1667 $from = 'accounts AS a';
1668 } else {
1669 $this->requireProfiles();
1670 $from = 'profiles AS p';
1671 }
1672 $joins = $this->buildJoins();
1673 $this->query = 'FROM ' . $from . '
1674 ' . $joins . '
1675 WHERE (' . $where . ')';
1676 }
1677 }
1678
1679 private function getUIDList($uids = null, PlLimit &$limit)
1680 {
1681 $this->requireAccounts();
1682 $this->buildQuery();
1683 $lim = $limit->getSql();
1684 $cond = '';
1685 if (!empty($uids)) {
1686 $cond = XDB::format(' AND a.uid IN {?}', $uids);
1687 }
1688 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
1689 ' . $this->query . $cond . '
1690 GROUP BY a.uid
1691 ' . $this->orderby . '
1692 ' . $lim);
1693 $this->lastusercount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
1694 return $fetched;
1695 }
1696
1697 private function getPIDList($pids = null, PlLimit &$limit)
1698 {
1699 $this->requireProfiles();
1700 $this->buildQuery();
1701 $lim = $limit->getSql();
1702 $cond = '';
1703 if (!is_null($pids)) {
1704 $cond = XDB::format(' AND p.pid IN {?}', $pids);
1705 }
1706 $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS p.pid
1707 ' . $this->query . $cond . '
1708 GROUP BY p.pid
1709 ' . $this->orderby . '
1710 ' . $lim);
1711 $this->lastprofilecount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()');
1712 return $fetched;
1713 }
1714
1715 private static function defaultLimit($limit) {
1716 if ($limit == null) {
1717 return new PlLimit();
1718 } else {
1719 return $limit;
1720 }
1721 }
1722
1723 /** Check that the user match the given rule.
1724 */
1725 public function checkUser(PlUser &$user)
1726 {
1727 $this->requireAccounts();
1728 $this->buildQuery();
1729 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1730 ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id()));
1731 return $count == 1;
1732 }
1733
1734 /** Check that the profile match the given rule.
1735 */
1736 public function checkProfile(Profile &$profile)
1737 {
1738 $this->requireProfiles();
1739 $this->buildQuery();
1740 $count = (int)XDB::fetchOneCell('SELECT COUNT(*)
1741 ' . $this->query . XDB::format(' AND p.pid = {?}', $profile->id()));
1742 return $count == 1;
1743 }
1744
1745 /** Default filter is on users
1746 */
1747 public function filter(array $users, $limit = null)
1748 {
1749 return $this->filterUsers($users, self::defaultLimit($limit));
1750 }
1751
1752 /** Filter a list of users to extract the users matching the rule.
1753 */
1754 public function filterUsers(array $users, $limit = null)
1755 {
1756 $limit = self::defaultLimit($limit);
1757 $this->requireAccounts();
1758 $this->buildQuery();
1759 $table = array();
1760 $uids = array();
1761 foreach ($users as $user) {
1762 if ($user instanceof PlUser) {
1763 $uid = $user->id();
1764 } else {
1765 $uid = $user;
1766 }
1767 $uids[] = $uid;
1768 $table[$uid] = $user;
1769 }
1770 $fetched = $this->getUIDList($uids, $limit);
1771 $output = array();
1772 foreach ($fetched as $uid) {
1773 $output[] = $table[$uid];
1774 }
1775 return $output;
1776 }
1777
1778 /** Filter a list of profiles to extract the users matching the rule.
1779 */
1780 public function filterProfiles(array $profiles, $limit = null)
1781 {
1782 $limit = self::defaultLimit($limit);
1783 $this->requireProfiles();
1784 $this->buildQuery();
1785 $table = array();
1786 $pids = array();
1787 foreach ($profiles as $profile) {
1788 if ($profile instanceof Profile) {
1789 $pid = $profile->id();
1790 } else {
1791 $pid = $profile;
1792 }
1793 $pids[] = $pid;
1794 $table[$pid] = $profile;
1795 }
1796 $fetched = $this->getPIDList($pids, $limit);
1797 $output = array();
1798 foreach ($fetched as $pid) {
1799 $output[] = $table[$pid];
1800 }
1801 return $output;
1802 }
1803
1804 public function getUIDs($limit = null)
1805 {
1806 $limit = self::defaultLimit($limit);
1807 return $this->getUIDList(null, $limit);
1808 }
1809
1810 public function getUID($pos = 0)
1811 {
1812 $uids =$this->getUIDList(null, new PlLimit(1, $pos));
1813 if (count($uids) == 0) {
1814 return null;
1815 } else {
1816 return $uids[0];
1817 }
1818 }
1819
1820 public function getPIDs($limit = null)
1821 {
1822 $limit = self::defaultLimit($limit);
1823 return $this->getPIDList(null, $limit);
1824 }
1825
1826 public function getPID($pos = 0)
1827 {
1828 $pids =$this->getPIDList(null, new PlLimit(1, $pos));
1829 if (count($pids) == 0) {
1830 return null;
1831 } else {
1832 return $pids[0];
1833 }
1834 }
1835
1836 public function getUsers($limit = null)
1837 {
1838 return User::getBulkUsersWithUIDs($this->getUIDs($limit));
1839 }
1840
1841 public function getUser($pos = 0)
1842 {
1843 $uid = $this->getUID($pos);
1844 if ($uid == null) {
1845 return null;
1846 } else {
1847 return User::getWithUID($uid);
1848 }
1849 }
1850
1851 public function iterUsers($limit = null)
1852 {
1853 return User::iterOverUIDs($this->getUIDs($limit));
1854 }
1855
1856 public function getProfiles($limit = null, $fields = 0x0000, $visibility = null)
1857 {
1858 return Profile::getBulkProfilesWithPIDs($this->getPIDs($limit), $fields, $visibility);
1859 }
1860
1861 public function getProfile($pos = 0, $fields = 0x0000, $visibility = null)
1862 {
1863 $pid = $this->getPID($pos);
1864 if ($pid == null) {
1865 return null;
1866 } else {
1867 return Profile::get($pid, $fields, $visibility);
1868 }
1869 }
1870
1871 public function iterProfiles($limit = null, $fields = 0x0000, $visibility = null)
1872 {
1873 return Profile::iterOverPIDs($this->getPIDs($limit), true, $fields, $visibility);
1874 }
1875
1876 public function get($limit = null)
1877 {
1878 return $this->getUsers($limit);
1879 }
1880
1881
1882 public function getTotalCount()
1883 {
1884 return $this->getTotalUserCount();
1885 }
1886
1887 public function getTotalUserCount()
1888 {
1889 if (is_null($this->lastusercount)) {
1890 $this->requireAccounts();
1891 $this->buildQuery();
1892 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT a.uid)
1893 ' . $this->query);
1894 } else {
1895 return $this->lastusercount;
1896 }
1897 }
1898
1899 public function getTotalProfileCount()
1900 {
1901 if (is_null($this->lastprofilecount)) {
1902 $this->requireProfiles();
1903 $this->buildQuery();
1904 return (int)XDB::fetchOneCell('SELECT COUNT(DISTINCT p.pid)
1905 ' . $this->query);
1906 } else {
1907 return $this->lastprofilecount;
1908 }
1909 }
1910
1911 public function setCondition(PlFilterCondition &$cond)
1912 {
1913 $this->root =& $cond;
1914 $this->query = null;
1915 }
1916
1917 public function addSort(PlFilterOrder &$sort)
1918 {
1919 $this->sort[] = $sort;
1920 $this->orderby = null;
1921 }
1922
1923 static public function getLegacy($promo_min, $promo_max)
1924 {
1925 if ($promo_min != 0) {
1926 $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
1927 } else {
1928 $min = new PFC_True();
1929 }
1930 if ($promo_max != 0) {
1931 $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
1932 } else {
1933 $max = new PFC_True();
1934 }
1935 return new UserFilter(new PFC_And($min, $max));
1936 }
1937
1938 static public function sortByName()
1939 {
1940 return array(new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME));
1941 }
1942
1943 static public function sortByPromo()
1944 {
1945 return array(new UFO_Promo(), new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME));
1946 }
1947
1948 static private function getDBSuffix($string)
1949 {
1950 if (is_array($string)) {
1951 if (count($string) == 1) {
1952 return self::getDBSuffix(array_pop($string));
1953 }
1954 return md5(implode('|', $string));
1955 } else {
1956 return preg_replace('/[^a-z0-9]/i', '', $string);
1957 }
1958 }
1959
1960
1961 /** Stores a new (and unique) table alias in the &$table table
1962 * @param &$table Array in which the table alias must be stored
1963 * @param $val Value which will then be used to build the join
1964 * @return Name of the newly created alias
1965 */
1966 private $option = 0;
1967 private function register_optional(array &$table, $val)
1968 {
1969 if (is_null($val)) {
1970 $sub = $this->option++;
1971 $index = null;
1972 } else {
1973 $sub = self::getDBSuffix($val);
1974 $index = $val;
1975 }
1976 $sub = '_' . $sub;
1977 $table[$sub] = $index;
1978 return $sub;
1979 }
1980
1981 /** PROFILE VS ACCOUNT
1982 */
1983 private $with_profiles = false;
1984 private $with_accounts = false;
1985 public function requireAccounts()
1986 {
1987 $this->with_accounts = true;
1988 }
1989
1990 public function accountsRequired()
1991 {
1992 return $this->with_accounts;
1993 }
1994
1995 public function requireProfiles()
1996 {
1997 $this->with_profiles = true;
1998 }
1999
2000 public function profilesRequired()
2001 {
2002 return $this->with_profiles;
2003 }
2004
2005 protected function accountJoins()
2006 {
2007 $joins = array();
2008 if ($this->with_profiles && $this->with_accounts) {
2009 $joins['ap'] = PlSqlJoin::left('account_profiles', '$ME.uid = $UID AND FIND_IN_SET(\'owner\', ap.perms)');
2010 $joins['p'] = PlSqlJoin::left('profiles', '$PID = ap.pid');
2011 }
2012 return $joins;
2013 }
2014
2015 /** DISPLAY
2016 */
2017 const DISPLAY = 'display';
2018 private $pd = false;
2019 public function addDisplayFilter()
2020 {
2021 $this->requireProfiles();
2022 $this->pd = true;
2023 return '';
2024 }
2025
2026 protected function displayJoins()
2027 {
2028 if ($this->pd) {
2029 return array('pd' => PlSqlJoin::left('profile_display', '$ME.pid = $PID'));
2030 } else {
2031 return array();
2032 }
2033 }
2034
2035 /** LOGGER
2036 */
2037
2038 private $with_logger = false;
2039 public function addLoggerFilter()
2040 {
2041 $this->with_logger = true;
2042 $this->requireAccounts();
2043 return 'ls';
2044 }
2045 protected function loggerJoins()
2046 {
2047 $joins = array();
2048 if ($this->with_logger) {
2049 $joins['ls'] = PlSqlJoin::left('log_sessions', '$ME.uid = $UID');
2050 }
2051 return $joins;
2052 }
2053
2054 /** NAMES
2055 */
2056
2057 static public function assertName($name)
2058 {
2059 if (!DirEnum::getID(DirEnum::NAMETYPES, $name)) {
2060 Platal::page()->kill('Invalid name type: ' . $name);
2061 }
2062 }
2063
2064 private $pn = array();
2065 public function addNameFilter($type, $variant = null)
2066 {
2067 $this->requireProfiles();
2068 if (!is_null($variant)) {
2069 $ft = $type . '_' . $variant;
2070 } else {
2071 $ft = $type;
2072 }
2073 $sub = '_' . $ft;
2074 self::assertName($ft);
2075
2076 if (!is_null($variant) && $variant == 'other') {
2077 $sub .= $this->option++;
2078 }
2079 $this->pn[$sub] = DirEnum::getID(DirEnum::NAMETYPES, $ft);
2080 return $sub;
2081 }
2082
2083 protected function nameJoins()
2084 {
2085 $joins = array();
2086 foreach ($this->pn as $sub => $type) {
2087 $joins['pn' . $sub] = PlSqlJoin::left('profile_name', '$ME.pid = $PID AND $ME.typeid = {?}', $type);
2088 }
2089 return $joins;
2090 }
2091
2092 /** NAMETOKENS
2093 */
2094 private $name_tokens = array();
2095 private $nb_tokens = 0;
2096
2097 public function addNameTokensFilter($token)
2098 {
2099 $this->requireProfiles();
2100 $sub = 'sn' . (1 + $this->nb_tokens);
2101 $this->nb_tokens++;
2102 $this->name_tokens[$sub] = $token;
2103 return $sub;
2104 }
2105
2106 protected function nameTokensJoins()
2107 {
2108 /* We don't return joins, since with_sn forces the SELECT to run on search_name first */
2109 $joins = array();
2110 foreach ($this->name_tokens as $sub => $token) {
2111 $joins[$sub] = PlSqlJoin::left('search_name', '$ME.pid = $PID');
2112 }
2113 return $joins;
2114 }
2115
2116 public function getNameTokens()
2117 {
2118 return $this->name_tokens;
2119 }
2120
2121 /** NATIONALITY
2122 */
2123
2124 private $with_nat = false;
2125 public function addNationalityFilter()
2126 {
2127 $this->with_nat = true;
2128 return 'ngc';
2129 }
2130
2131 protected function nationalityJoins()
2132 {
2133 $joins = array();
2134 if ($this->with_nat) {
2135 $joins['ngc'] = PlSqlJoin::left('geoloc_countries', '$ME.iso_3166_1_a2 = p.nationality1 OR $ME.iso_3166_1_a2 = p.nationality2 OR $ME.iso_3166_1_a2 = p.nationality3');
2136 }
2137 return $joins;
2138 }
2139
2140 /** EDUCATION
2141 */
2142 const GRADE_ING = 'Ing.';
2143 const GRADE_PHD = 'PhD';
2144 const GRADE_MST = 'M%';
2145 static public function isGrade($grade)
2146 {
2147 return ($grade !== 0) && ($grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST);
2148 }
2149
2150 static public function assertGrade($grade)
2151 {
2152 if (!self::isGrade($grade)) {
2153 Platal::page()->killError("DiplĂ´me non valide: $grade");
2154 }
2155 }
2156
2157 static public function promoYear($grade)
2158 {
2159 // XXX: Definition of promotion for phds and masters might change in near future.
2160 return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year';
2161 }
2162
2163 private $pepe = array();
2164 private $with_pee = false;
2165 public function addEducationFilter($x = false, $grade = null)
2166 {
2167 $this->requireProfiles();
2168 if (!$x) {
2169 $index = $this->option;
2170 $sub = $this->option++;
2171 } else {
2172 self::assertGrade($grade);
2173 $index = $grade;
2174 $sub = $grade[0];
2175 $this->with_pee = true;
2176 }
2177 $sub = '_' . $sub;
2178 $this->pepe[$index] = $sub;
2179 return $sub;
2180 }
2181
2182 protected function educationJoins()
2183 {
2184 $joins = array();
2185 if ($this->with_pee) {
2186 $joins['pee'] = PlSqlJoin::inner('profile_education_enum', 'pee.abbreviation = \'X\'');
2187 }
2188 foreach ($this->pepe as $grade => $sub) {
2189 if ($this->isGrade($grade)) {
2190 $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID');
2191 $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE {?}', $grade);
2192 } else {
2193 $joins['pe' . $sub] = PlSqlJoin::left('profile_education', '$ME.pid = $PID');
2194 $joins['pee' . $sub] = PlSqlJoin::inner('profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
2195 $joins['pede' . $sub] = PlSqlJoin::inner('profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
2196 }
2197 }
2198 return $joins;
2199 }
2200
2201
2202 /** GROUPS
2203 */
2204 private $gpm = array();
2205 public function addGroupFilter($group = null)
2206 {
2207 $this->requireAccounts();
2208 if (!is_null($group)) {
2209 if (is_int($group) || ctype_digit($group)) {
2210 $index = $sub = $group;
2211 } else {
2212 $index = $group;
2213 $sub = self::getDBSuffix($group);
2214 }
2215 } else {
2216 $sub = 'group_' . $this->option++;
2217 $index = null;
2218 }
2219 $sub = '_' . $sub;
2220 $this->gpm[$sub] = $index;
2221 return $sub;
2222 }
2223
2224 protected function groupJoins()
2225 {
2226 $joins = array();
2227 foreach ($this->gpm as $sub => $key) {
2228 if (is_null($key)) {
2229 $joins['gpa' . $sub] = PlSqlJoin::inner('groups');
2230 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
2231 } else if (is_int($key) || ctype_digit($key)) {
2232 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = ' . $key);
2233 } else {
2234 $joins['gpa' . $sub] = PlSqlJoin::inner('groups', '$ME.diminutif = {?}', $key);
2235 $joins['gpm' . $sub] = PlSqlJoin::left('group_members', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id');
2236 }
2237 }
2238 return $joins;
2239 }
2240
2241 /** BINETS
2242 */
2243
2244 private $with_bi = false;
2245 private $with_bd = false;
2246 public function addBinetsFilter($with_enum = false)
2247 {
2248 $this->requireProfiles();
2249 $this->with_bi = true;
2250 if ($with_enum) {
2251 $this->with_bd = true;
2252 return 'bd';
2253 } else {
2254 return 'bi';
2255 }
2256 }
2257
2258 protected function binetsJoins()
2259 {
2260 $joins = array();
2261 if ($this->with_bi) {
2262 $joins['bi'] = PlSqlJoin::left('profile_binets', '$ME.pid = $PID');
2263 }
2264 if ($this->with_bd) {
2265 $joins['bd'] = PlSqlJoin::left('profile_binet_enum', '$ME.id = bi.binet_id');
2266 }
2267 return $joins;
2268 }
2269
2270 /** EMAILS
2271 */
2272 private $e = array();
2273 public function addEmailRedirectFilter($email = null)
2274 {
2275 $this->requireAccounts();
2276 return $this->register_optional($this->e, $email);
2277 }
2278
2279 private $ve = array();
2280 public function addVirtualEmailFilter($email = null)
2281 {
2282 $this->addAliasFilter(self::ALIAS_FORLIFE);
2283 return $this->register_optional($this->ve, $email);
2284 }
2285
2286 const ALIAS_BEST = 'bestalias';
2287 const ALIAS_FORLIFE = 'forlife';
2288 private $al = array();
2289 public function addAliasFilter($alias = null)
2290 {
2291 $this->requireAccounts();
2292 return $this->register_optional($this->al, $alias);
2293 }
2294
2295 protected function emailJoins()
2296 {
2297 global $globals;
2298 $joins = array();
2299 foreach ($this->e as $sub=>$key) {
2300 if (is_null($key)) {
2301 $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'');
2302 } else {
2303 if (!is_array($key)) {
2304 $key = array($key);
2305 }
2306 $joins['e' . $sub] = PlSqlJoin::left('emails', '$ME.uid = $UID AND $ME.flags != \'filter\'
2307 AND $ME.email IN {?}', $key);
2308 }
2309 }
2310 foreach ($this->al as $sub=>$key) {
2311 if (is_null($key)) {
2312 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
2313 } else if ($key == self::ALIAS_BEST) {
2314 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)');
2315 } else if ($key == self::ALIAS_FORLIFE) {
2316 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type = \'a_vie\'');
2317 } else {
2318 if (!is_array($key)) {
2319 $key = array($key);
2320 }
2321 $joins['al' . $sub] = PlSqlJoin::left('aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')
2322 AND $ME.alias IN {?}', $key);
2323 }
2324 }
2325 foreach ($this->ve as $sub=>$key) {
2326 if (is_null($key)) {
2327 $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\'');
2328 } else {
2329 if (!is_array($key)) {
2330 $key = array($key);
2331 }
2332 $joins['v' . $sub] = PlSqlJoin::left('virtual', '$ME.type = \'user\' AND $ME.alias IN {?}', $key);
2333 }
2334 $joins['vr' . $sub] = PlSqlJoin::left('virtual_redirect',
2335 '$ME.vid = v' . $sub . '.vid
2336 AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}),
2337 CONCAT(al_forlife.alias, \'@\', {?}),
2338 a.email))',
2339 $globals->mail->domain, $globals->mail->domain2);
2340 }
2341 return $joins;
2342 }
2343
2344
2345 /** ADDRESSES
2346 */
2347 private $with_pa = false;
2348 public function addAddressFilter()
2349 {
2350 $this->requireProfiles();
2351 $this->with_pa = true;
2352 return 'pa';
2353 }
2354
2355 private $with_pac = false;
2356 public function addAddressCountryFilter()
2357 {
2358 $this->requireProfiles();
2359 $this->addAddressFilter();
2360 $this->with_pac = true;
2361 return 'gc';
2362 }
2363
2364 private $with_pal = false;
2365 public function addAddressLocalityFilter()
2366 {
2367 $this->requireProfiles();
2368 $this->addAddressFilter();
2369 $this->with_pal = true;
2370 return 'gl';
2371 }
2372
2373 protected function addressJoins()
2374 {
2375 $joins = array();
2376 if ($this->with_pa) {
2377 $joins['pa'] = PlSqlJoin::left('profile_addresses', '$ME.pid = $PID');
2378 }
2379 if ($this->with_pac) {
2380 $joins['gc'] = PlSqlJoin::left('geoloc_countries', '$ME.iso_3166_1_a2 = pa.countryID');
2381 }
2382 if ($this->with_pal) {
2383 $joins['gl'] = PlSqlJoin::left('geoloc_localities', '$ME.id = pa.localityID');
2384 }
2385 return $joins;
2386 }
2387
2388
2389 /** CORPS
2390 */
2391
2392 private $pc = false;
2393 private $pce = array();
2394 private $pcr = false;
2395 public function addCorpsFilter($type)
2396 {
2397 $this->requireProfiles();
2398 $this->pc = true;
2399 if ($type == UFC_Corps::CURRENT) {
2400 $pce['pcec'] = 'current_corpsid';
2401 return 'pcec';
2402 } else if ($type == UFC_Corps::ORIGIN) {
2403 $pce['pceo'] = 'original_corpsid';
2404 return 'pceo';
2405 }
2406 }
2407
2408 public function addCorpsRankFilter()
2409 {
2410 $this->requireProfiles();
2411 $this->pc = true;
2412 $this->pcr = true;
2413 return 'pcr';
2414 }
2415
2416 protected function corpsJoins()
2417 {
2418 $joins = array();
2419 if ($this->pc) {
2420 $joins['pc'] = PlSqlJoin::left('profile_corps', '$ME.pid = $PID');
2421 }
2422 if ($this->pcr) {
2423 $joins['pcr'] = PlSqlJoin::left('profile_corps_rank_enum', '$ME.id = pc.rankid');
2424 }
2425 foreach($this->pce as $sub => $field) {
2426 $joins[$sub] = PlSqlJoin::left('profile_corps_enum', '$ME.id = pc.' . $field);
2427 }
2428 return $joins;
2429 }
2430
2431 /** JOBS
2432 */
2433
2434 const JOB_SECTOR = 0x0001;
2435 const JOB_SUBSECTOR = 0x0002;
2436 const JOB_SUBSUBSECTOR = 0x0004;
2437 const JOB_ALTERNATES = 0x0008;
2438 const JOB_USERDEFINED = 0x0010;
2439 const JOB_CV = 0x0020;
2440
2441 const JOB_SECTORIZATION = 0x000F;
2442 const JOB_ANY = 0x003F;
2443
2444 /** Joins :
2445 * pj => profile_job
2446 * pje => profile_job_enum
2447 * pjse => profile_job_sector_enum
2448 * pjsse => profile_job_subsector_enum
2449 * pjssse => profile_job_subsubsector_enum
2450 * pja => profile_job_alternates
2451 */
2452 private $with_pj = false;
2453 private $with_pje = false;
2454 private $with_pjse = false;
2455 private $with_pjsse = false;
2456 private $with_pjssse = false;
2457 private $with_pja = false;
2458
2459 public function addJobFilter()
2460 {
2461 $this->requireProfiles();
2462 $this->with_pj = true;
2463 return 'pj';
2464 }
2465
2466 public function addJobCompanyFilter()
2467 {
2468 $this->addJobFilter();
2469 $this->with_pje = true;
2470 return 'pje';
2471 }
2472
2473 public function addJobSectorizationFilter($type)
2474 {
2475 $this->addJobFilter();
2476 if ($type == self::JOB_SECTOR) {
2477 $this->with_pjse = true;
2478 return 'pjse';
2479 } else if ($type == self::JOB_SUBSECTOR) {
2480 $this->with_pjsse = true;
2481 return 'pjsse';
2482 } else if ($type == self::JOB_SUBSUBSECTOR) {
2483 $this->with_pjssse = true;
2484 return 'pjssse';
2485 } else if ($type == self::JOB_ALTERNATES) {
2486 $this->with_pja = true;
2487 return 'pja';
2488 }
2489 }
2490
2491 protected function jobJoins()
2492 {
2493 $joins = array();
2494 if ($this->with_pj) {
2495 $joins['pj'] = PlSqlJoin::left('profile_job', '$ME.pid = $PID');
2496 }
2497 if ($this->with_pje) {
2498 $joins['pje'] = PlSqlJoin::left('profile_job_enum', '$ME.id = pj.jobid');
2499 }
2500 if ($this->with_pjse) {
2501 $joins['pjse'] = PlSqlJoin::left('profile_job_sector_enum', '$ME.id = pj.sectorid');
2502 }
2503 if ($this->with_pjsse) {
2504 $joins['pjsse'] = PlSqlJoin::left('profile_job_subsector_enum', '$ME.id = pj.subsectorid');
2505 }
2506 if ($this->with_pjssse) {
2507 $joins['pjssse'] = PlSqlJoin::left('profile_job_subsubsector_enum', '$ME.id = pj.subsubsectorid');
2508 }
2509 if ($this->with_pja) {
2510 $joins['pja'] = PlSqlJoin::left('profile_job_alternates', '$ME.subsubsectorid = pj.subsubsectorid');
2511 }
2512 return $joins;
2513 }
2514
2515 /** NETWORKING
2516 */
2517
2518 private $with_pnw = false;
2519 public function addNetworkingFilter()
2520 {
2521 $this->requireAccounts();
2522 $this->with_pnw = true;
2523 return 'pnw';
2524 }
2525
2526 protected function networkingJoins()
2527 {
2528 $joins = array();
2529 if ($this->with_pnw) {
2530 $joins['pnw'] = PlSqlJoin::left('profile_networking', '$ME.pid = $PID');
2531 }
2532 return $joins;
2533 }
2534
2535 /** PHONE
2536 */
2537
2538 private $with_ptel = false;
2539
2540 public function addPhoneFilter()
2541 {
2542 $this->requireAccounts();
2543 $this->with_ptel = true;
2544 return 'ptel';
2545 }
2546
2547 protected function phoneJoins()
2548 {
2549 $joins = array();
2550 if ($this->with_ptel) {
2551 $joins['ptel'] = PlSqlJoin::left('profile_phones', '$ME.pid = $PID');
2552 }
2553 return $joins;
2554 }
2555
2556 /** MEDALS
2557 */
2558
2559 private $with_pmed = false;
2560 public function addMedalFilter()
2561 {
2562 $this->requireProfiles();
2563 $this->with_pmed = true;
2564 return 'pmed';
2565 }
2566
2567 protected function medalJoins()
2568 {
2569 $joins = array();
2570 if ($this->with_pmed) {
2571 $joins['pmed'] = PlSqlJoin::left('profile_medals', '$ME.pid = $PID');
2572 }
2573 return $joins;
2574 }
2575
2576 /** MENTORING
2577 */
2578
2579 private $pms = array();
2580 const MENTOR_EXPERTISE = 1;
2581 const MENTOR_COUNTRY = 2;
2582 const MENTOR_SECTOR = 3;
2583
2584 public function addMentorFilter($type)
2585 {
2586 $this->requireAccounts();
2587 switch($type) {
2588 case self::MENTOR_EXPERTISE:
2589 $this->pms['pme'] = 'profile_mentor';
2590 return 'pme';
2591 case self::MENTOR_COUNTRY:
2592 $this->pms['pmc'] = 'profile_mentor_country';
2593 return 'pmc';
2594 case self::MENTOR_SECTOR:
2595 $this->pms['pms'] = 'profile_mentor_sector';
2596 return 'pms';
2597 default:
2598 Platal::page()->killError("Undefined mentor filter.");
2599 }
2600 }
2601
2602 protected function mentorJoins()
2603 {
2604 $joins = array();
2605 foreach ($this->pms as $sub => $tab) {
2606 $joins[$sub] = PlSqlJoin::left($tab, '$ME.pid = $PID');
2607 }
2608 return $joins;
2609 }
2610
2611 /** CONTACTS
2612 */
2613 private $cts = array();
2614 public function addContactFilter($uid = null)
2615 {
2616 $this->requireProfiles();
2617 return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid);
2618 }
2619
2620 protected function contactJoins()
2621 {
2622 $joins = array();
2623 foreach ($this->cts as $sub=>$key) {
2624 if (is_null($key)) {
2625 $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.contact = $PID');
2626 } else {
2627 $joins['c' . $sub] = PlSqlJoin::left('contacts', '$ME.uid = {?} AND $ME.contact = $PID', substr($key, 5));
2628 }
2629 }
2630 return $joins;
2631 }
2632
2633
2634 /** CARNET
2635 */
2636 private $wn = array();
2637 public function addWatchRegistrationFilter($uid = null)
2638 {
2639 $this->requireAccounts();
2640 return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid);
2641 }
2642
2643 private $wp = array();
2644 public function addWatchPromoFilter($uid = null)
2645 {
2646 $this->requireAccounts();
2647 return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid);
2648 }
2649
2650 private $w = array();
2651 public function addWatchFilter($uid = null)
2652 {
2653 $this->requireAccounts();
2654 return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid);
2655 }
2656
2657 protected function watchJoins()
2658 {
2659 $joins = array();
2660 foreach ($this->w as $sub=>$key) {
2661 if (is_null($key)) {
2662 $joins['w' . $sub] = PlSqlJoin::left('watch');
2663 } else {
2664 $joins['w' . $sub] = PlSqlJoin::left('watch', '$ME.uid = {?}', substr($key, 5));
2665 }
2666 }
2667 foreach ($this->wn as $sub=>$key) {
2668 if (is_null($key)) {
2669 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID');
2670 } else {
2671 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
2672 }
2673 }
2674 foreach ($this->wn as $sub=>$key) {
2675 if (is_null($key)) {
2676 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.ni_id = $UID');
2677 } else {
2678 $joins['wn' . $sub] = PlSqlJoin::left('watch_nonins', '$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5));
2679 }
2680 }
2681 foreach ($this->wp as $sub=>$key) {
2682 if (is_null($key)) {
2683 $joins['wp' . $sub] = PlSqlJoin::left('watch_promo');
2684 } else {
2685 $joins['wp' . $sub] = PlSqlJoin::left('watch_promo', '$ME.uid = {?}', substr($key, 5));
2686 }
2687 }
2688 return $joins;
2689 }
2690
2691
2692 /** PHOTOS
2693 */
2694 private $with_photo;
2695 public function addPhotoFilter()
2696 {
2697 $this->requireProfiles();
2698 $this->with_photo = true;
2699 }
2700
2701 protected function photoJoins()
2702 {
2703 if ($this->with_photo) {
2704 return array('photo' => PlSqlJoin::left('profile_photos', '$ME.pid = $PID'));
2705 } else {
2706 return array();
2707 }
2708 }
2709
2710
2711 /** MARKETING
2712 */
2713 private $with_rm;
2714 public function addMarketingHash()
2715 {
2716 $this->requireAccounts();
2717 $this->with_rm = true;
2718 }
2719
2720 protected function marketingJoins()
2721 {
2722 if ($this->with_rm) {
2723 return array('rm' => PlSqlJoin::left('register_marketing', '$ME.uid = $UID'));
2724 } else {
2725 return array();
2726 }
2727 }
2728 }
2729 // }}}
2730
2731 // {{{ class ProfileFilter
2732 class ProfileFilter extends UserFilter
2733 {
2734 public function get($limit = null)
2735 {
2736 return $this->getProfiles($limit);
2737 }
2738
2739 public function filter(array $profiles, $limit = null)
2740 {
2741 return $this->filterProfiles($profiles, self::defaultLimit($limit));
2742 }
2743
2744 public function getTotalCount()
2745 {
2746 return $this->getTotalProfileCount();
2747 }
2748 }
2749 // }}}
2750
2751 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
2752 ?>