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