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