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