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