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