Commit | Line | Data |
---|---|---|
a087cc8d FB |
1 | <?php |
2 | /*************************************************************************** | |
3 | * Copyright (C) 2003-2009 Polytechnique.org * | |
4 | * http://opensource.polytechnique.org/ * | |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
d865c296 FB |
22 | |
23 | /****************** | |
24 | * CONDITIONS | |
25 | ******************/ | |
26 | ||
a087cc8d FB |
27 | interface UserFilterCondition |
28 | { | |
5dd9d823 FB |
29 | const COND_TRUE = 'TRUE'; |
30 | const COND_FALSE = 'FALSE'; | |
31 | ||
a087cc8d FB |
32 | /** Check that the given user matches the rule. |
33 | */ | |
784745ce | 34 | public function buildCondition(UserFilter &$uf); |
a087cc8d FB |
35 | } |
36 | ||
37 | abstract class UFC_OneChild implements UserFilterCondition | |
38 | { | |
39 | protected $child; | |
40 | ||
5dd9d823 FB |
41 | public function __construct($child = null) |
42 | { | |
43 | if (!is_null($child) && ($child instanceof UserFilterCondition)) { | |
44 | $this->setChild($child); | |
45 | } | |
46 | } | |
47 | ||
a087cc8d FB |
48 | public function setChild(UserFilterCondition &$cond) |
49 | { | |
50 | $this->child =& $cond; | |
51 | } | |
52 | } | |
53 | ||
54 | abstract class UFC_NChildren implements UserFilterCondition | |
55 | { | |
56 | protected $children = array(); | |
57 | ||
5dd9d823 FB |
58 | public function __construct() |
59 | { | |
60 | $children = func_get_args(); | |
61 | foreach ($children as &$child) { | |
62 | if (!is_null($child) && ($child instanceof UserFilterCondition)) { | |
63 | $this->addChild($child); | |
64 | } | |
65 | } | |
66 | } | |
67 | ||
a087cc8d FB |
68 | public function addChild(UserFilterCondition &$cond) |
69 | { | |
70 | $this->children[] =& $cond; | |
71 | } | |
5dd9d823 FB |
72 | |
73 | protected function catConds(array $cond, $op, $fallback) | |
74 | { | |
75 | if (count($cond) == 0) { | |
76 | return $fallback; | |
77 | } else if (count($cond) == 1) { | |
78 | return $cond[0]; | |
79 | } else { | |
4927ee54 | 80 | return '(' . implode(') ' . $op . ' (', $cond) . ')'; |
5dd9d823 FB |
81 | } |
82 | } | |
a087cc8d FB |
83 | } |
84 | ||
85 | class UFC_True implements UserFilterCondition | |
86 | { | |
784745ce | 87 | public function buildCondition(UserFilter &$uf) |
a087cc8d | 88 | { |
5dd9d823 | 89 | return self::COND_TRUE; |
a087cc8d FB |
90 | } |
91 | } | |
92 | ||
93 | class UFC_False implements UserFilterCondition | |
94 | { | |
784745ce | 95 | public function buildCondition(UserFilter &$uf) |
a087cc8d | 96 | { |
5dd9d823 | 97 | return self::COND_FALSE; |
a087cc8d FB |
98 | } |
99 | } | |
100 | ||
101 | class UFC_Not extends UFC_OneChild | |
102 | { | |
784745ce | 103 | public function buildCondition(UserFilter &$uf) |
a087cc8d | 104 | { |
5dd9d823 FB |
105 | $val = $this->child->buildCondition($uf); |
106 | if ($val == self::COND_TRUE) { | |
107 | return self::COND_FALSE; | |
108 | } else if ($val == self::COND_FALSE) { | |
109 | return self::COND_TRUE; | |
110 | } else { | |
111 | return 'NOT (' . $val . ')'; | |
112 | } | |
a087cc8d FB |
113 | } |
114 | } | |
115 | ||
116 | class UFC_And extends UFC_NChildren | |
117 | { | |
784745ce | 118 | public function buildCondition(UserFilter &$uf) |
a087cc8d | 119 | { |
784745ce | 120 | if (empty($this->children)) { |
5dd9d823 | 121 | return self::COND_FALSE; |
784745ce | 122 | } else { |
5dd9d823 | 123 | $true = self::COND_FALSE; |
784745ce FB |
124 | $conds = array(); |
125 | foreach ($this->children as &$child) { | |
5dd9d823 FB |
126 | $val = $child->buildCondition($uf); |
127 | if ($val == self::COND_TRUE) { | |
128 | $true = self::COND_TRUE; | |
129 | } else if ($val == self::COND_FALSE) { | |
130 | return self::COND_FALSE; | |
131 | } else { | |
132 | $conds[] = $val; | |
133 | } | |
a087cc8d | 134 | } |
5dd9d823 | 135 | return $this->catConds($conds, 'AND', $true); |
a087cc8d | 136 | } |
a087cc8d FB |
137 | } |
138 | } | |
139 | ||
140 | class UFC_Or extends UFC_NChildren | |
141 | { | |
784745ce | 142 | public function buildCondition(UserFilter &$uf) |
a087cc8d | 143 | { |
784745ce | 144 | if (empty($this->children)) { |
5dd9d823 | 145 | return self::COND_TRUE; |
784745ce | 146 | } else { |
5dd9d823 | 147 | $true = self::COND_TRUE; |
784745ce FB |
148 | $conds = array(); |
149 | foreach ($this->children as &$child) { | |
5dd9d823 FB |
150 | $val = $child->buildCondition($uf); |
151 | if ($val == self::COND_TRUE) { | |
152 | return self::COND_TRUE; | |
153 | } else if ($val == self::COND_FALSE) { | |
154 | $true = self::COND_FALSE; | |
155 | } else { | |
156 | $conds[] = $val; | |
157 | } | |
a087cc8d | 158 | } |
5dd9d823 | 159 | return $this->catConds($conds, 'OR', $true); |
a087cc8d | 160 | } |
a087cc8d FB |
161 | } |
162 | } | |
163 | ||
164 | class UFC_Promo implements UserFilterCondition | |
165 | { | |
a087cc8d FB |
166 | |
167 | private $grade; | |
168 | private $promo; | |
169 | private $comparison; | |
170 | ||
171 | public function __construct($comparison, $grade, $promo) | |
172 | { | |
173 | $this->grade = $grade; | |
174 | $this->comparison = $comparison; | |
175 | $this->promo = $promo; | |
38c6fe96 FB |
176 | if ($this->grade != UserFilter::DISPLAY) { |
177 | UserFilter::assertGrade($this->grade); | |
178 | } | |
a087cc8d FB |
179 | } |
180 | ||
784745ce | 181 | public function buildCondition(UserFilter &$uf) |
a087cc8d | 182 | { |
38c6fe96 FB |
183 | if ($this->grade == UserFilter::DISPLAY) { |
184 | $sub = $uf->addDisplayFilter(); | |
185 | return XDB::format('pd' . $sub . '.promo = {?}', $this->promo); | |
186 | } else { | |
187 | $sub = $uf->addEducationFilter(true, $this->grade); | |
188 | $field = 'pe' . $sub . '.' . UserFilter::promoYear($this->grade); | |
189 | return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison . ' ' . XDB::format('{?}', $this->promo); | |
190 | } | |
784745ce FB |
191 | } |
192 | } | |
193 | ||
194 | class UFC_Name implements UserFilterCondition | |
195 | { | |
196 | const PREFIX = 1; | |
197 | const SUFFIX = 2; | |
198 | const PARTICLE = 7; | |
199 | const VARIANTS = 8; | |
200 | const CONTAINS = 3; | |
201 | ||
202 | private $type; | |
203 | private $text; | |
204 | private $mode; | |
205 | ||
206 | public function __construct($type, $text, $mode) | |
207 | { | |
208 | $this->type = $type; | |
209 | $this->text = $text; | |
210 | $this->mode = $mode; | |
211 | } | |
212 | ||
213 | private function buildNameQuery($type, $variant, $where, UserFilter &$uf) | |
214 | { | |
215 | $sub = $uf->addNameFilter($type, $variant); | |
216 | return str_replace('$ME', 'pn' . $sub, $where); | |
217 | } | |
218 | ||
219 | public function buildCondition(UserFilter &$uf) | |
220 | { | |
221 | $left = '$ME.name'; | |
222 | $op = ' LIKE '; | |
223 | if (($this->mode & self::PARTICLE) == self::PARTICLE) { | |
224 | $left = 'CONCAT($ME.particle, \' \', $ME.name)'; | |
225 | } | |
226 | if (($this->mode & self::CONTAINS) == 0) { | |
227 | $right = XDB::format('{?}', $this->text); | |
228 | $op = ' = '; | |
229 | } else if (($this->mode & self::CONTAINS) == self::PREFIX) { | |
230 | $right = XDB::format('CONCAT({?}, \'%\')', $this->text); | |
231 | } else if (($this->mode & self::CONTAINS) == self::SUFFIX) { | |
232 | $right = XDB::format('CONCAT(\'%\', {?})', $this->text); | |
233 | } else { | |
234 | $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text); | |
235 | } | |
236 | $cond = $left . $op . $right; | |
237 | $conds = array($this->buildNameQuery($this->type, null, $cond, $uf)); | |
d865c296 | 238 | if (($this->mode & self::VARIANTS) != 0 && isset(UserFilter::$name_variants[$this->type])) { |
784745ce FB |
239 | foreach (UserFilter::$name_variants[$this->type] as $var) { |
240 | $conds[] = $this->buildNameQuery($this->type, $var, $cond, $uf); | |
241 | } | |
242 | } | |
243 | return implode(' OR ', $conds); | |
a087cc8d FB |
244 | } |
245 | } | |
246 | ||
4927ee54 FB |
247 | class UFC_Dead implements UserFilterCondition |
248 | { | |
38c6fe96 FB |
249 | private $comparison; |
250 | private $date; | |
251 | ||
252 | public function __construct($comparison = null, $date = null) | |
4927ee54 | 253 | { |
38c6fe96 FB |
254 | $this->comparison = $comparison; |
255 | $this->date = $date; | |
4927ee54 FB |
256 | } |
257 | ||
258 | public function buildCondition(UserFilter &$uf) | |
259 | { | |
38c6fe96 FB |
260 | $str = 'p.deathdate IS NOT NULL'; |
261 | if (!is_null($this->comparison)) { | |
262 | $str .= ' AND p.deathdate ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date)); | |
4927ee54 | 263 | } |
38c6fe96 | 264 | return $str; |
4927ee54 FB |
265 | } |
266 | } | |
267 | ||
268 | class UFC_Registered implements UserFilterCondition | |
269 | { | |
270 | private $active; | |
38c6fe96 FB |
271 | private $comparison; |
272 | private $date; | |
273 | ||
274 | public function __construct($active = false, $comparison = null, $date = null) | |
4927ee54 FB |
275 | { |
276 | $this->only_active = $active; | |
38c6fe96 FB |
277 | $this->comparison = $comparison; |
278 | $this->date = $date; | |
4927ee54 FB |
279 | } |
280 | ||
281 | public function buildCondition(UserFilter &$uf) | |
282 | { | |
283 | if ($this->active) { | |
38c6fe96 | 284 | $date = 'a.uid IS NOT NULL AND a.state = \'active\''; |
4927ee54 | 285 | } else { |
38c6fe96 FB |
286 | $date = 'a.uid IS NOT NULL AND a.state != \'pending\''; |
287 | } | |
288 | if (!is_null($this->comparison)) { | |
289 | $date .= ' AND a.registration_date ' . $this->comparison . ' ' . XDB::format('{?}', date('Y-m-d', $this->date)); | |
4927ee54 | 290 | } |
38c6fe96 | 291 | return $date; |
4927ee54 FB |
292 | } |
293 | } | |
294 | ||
295 | class UFC_Sex implements UserFilterCondition | |
296 | { | |
297 | private $sex; | |
298 | public function __construct($sex) | |
299 | { | |
300 | $this->sex = $sex; | |
301 | } | |
302 | ||
303 | public function buildCondition(UserFilter &$uf) | |
304 | { | |
305 | if ($this->sex != User::GENDER_MALE && $this->sex != User::GENDER_FEMALE) { | |
306 | return self::COND_FALSE; | |
307 | } else { | |
24e08e33 | 308 | return XDB::format('p.sex = {?}', $this->sex == User::GENDER_FEMALE ? 'female' : 'male'); |
4927ee54 FB |
309 | } |
310 | } | |
311 | } | |
312 | ||
313 | class UFC_Group implements UserFilterCondition | |
314 | { | |
315 | private $group; | |
316 | private $admin; | |
317 | public function __construct($group, $admin = false) | |
318 | { | |
319 | $this->group = $group; | |
320 | $this->admin = $admin; | |
321 | } | |
322 | ||
323 | public function buildCondition(UserFilter &$uf) | |
324 | { | |
325 | $sub = $uf->addGroupFilter($this->group); | |
326 | $where = 'gpm' . $sub . '.perms IS NOT NULL'; | |
327 | if ($this->admin) { | |
328 | $where .= ' AND gpm' . $sub . '.perms = \'admin\''; | |
329 | } | |
330 | return $where; | |
331 | } | |
332 | } | |
333 | ||
aa21c568 FB |
334 | class UFC_Email implements UserFilterCondition |
335 | { | |
336 | private $email; | |
337 | public function __construct($email) | |
338 | { | |
339 | $this->email = $email; | |
340 | } | |
341 | ||
342 | public function buildCondition(UserFilter &$uf) | |
343 | { | |
344 | if (User::isForeignEmailAddress($this->email)) { | |
345 | $sub = $uf->addEmailRedirectFilter($this->email); | |
346 | return XDB::format('e' . $sub . '.email IS NOT NULL OR a.email = {?}', $this->email); | |
21401768 FB |
347 | } else if (User::isVirtualEmailAddress($this->email)) { |
348 | $sub = $uf->addVirtualEmailFilter($this->email); | |
349 | return 'vr' . $sub . '.redirect IS NOT NULL'; | |
aa21c568 | 350 | } else { |
21401768 FB |
351 | @list($user, $domain) = explode('@', $this->email); |
352 | $sub = $uf->addAliasFilter($user); | |
aa21c568 FB |
353 | return 'al' . $sub . '.alias IS NOT NULL'; |
354 | } | |
355 | } | |
356 | } | |
d865c296 | 357 | |
21401768 FB |
358 | class UFC_EmailList implements UserFilterCondition |
359 | { | |
360 | private $emails; | |
361 | public function __construct($emails) | |
362 | { | |
363 | $this->emails = $emails; | |
364 | } | |
365 | ||
366 | public function buildCondition(UserFilter &$uf) | |
367 | { | |
368 | $email = null; | |
369 | $virtual = null; | |
370 | $alias = null; | |
371 | $cond = array(); | |
372 | ||
373 | if (count($this->emails) == 0) { | |
374 | return UserFilterCondition::COND_TRUE; | |
375 | } | |
376 | ||
377 | foreach ($this->emails as $entry) { | |
378 | if (User::isForeignEmailAddress($entry)) { | |
379 | if (is_null($email)) { | |
380 | $email = $uf->addEmailRedirectFilter(); | |
381 | } | |
382 | $cond[] = XDB::format('e' . $email . '.email = {?} OR a.email = {?}', $entry, $entry); | |
383 | } else if (User::isVirtualEmailAddress($entry)) { | |
384 | if (is_null($virtual)) { | |
385 | $virtual = $uf->addVirtualEmailFilter(); | |
386 | } | |
387 | $cond[] = XDB::format('vr' . $virtual . '.redirect IS NOT NULL AND v' . $virtual . '.alias = {?}', $entry); | |
388 | } else { | |
389 | if (is_null($alias)) { | |
390 | $alias = $uf->addAliasFilter(); | |
391 | } | |
392 | @list($user, $domain) = explode('@', $entry); | |
393 | $cond[] = XDB::format('al' . $alias . '.alias = {?}', $user); | |
394 | } | |
395 | } | |
396 | return '(' . implode(') OR (', $cond) . ')'; | |
397 | } | |
398 | } | |
d865c296 | 399 | |
4e7bf1e0 | 400 | abstract class UFC_UserRelated implements UserFilterCondition |
3f42a6ad | 401 | { |
4e7bf1e0 | 402 | protected $uid; |
3f42a6ad FB |
403 | public function __construct($uid = null) |
404 | { | |
405 | if (is_null($uid)) { | |
406 | $this->uid = S::i('uid'); | |
407 | } else if ($uid instanceof PlUser) { | |
408 | $this->uid = $uid->id(); | |
409 | } else if (ctype_digit($uid)) { | |
410 | $this->uid = (int)$uid; | |
411 | } else { | |
412 | Platal::page()->kill("Invalid contact type"); | |
413 | } | |
414 | } | |
4e7bf1e0 | 415 | } |
3f42a6ad | 416 | |
4e7bf1e0 FB |
417 | class UFC_Contact extends UFC_UserRelated |
418 | { | |
3f42a6ad FB |
419 | public function buildCondition(UserFilter &$uf) |
420 | { | |
421 | $sub = $uf->addContactFilter($this->uid); | |
422 | return 'c' . $sub . '.contact IS NOT NULL'; | |
423 | } | |
424 | } | |
425 | ||
4e7bf1e0 FB |
426 | class UFC_WatchRegistration extends UFC_UserRelated |
427 | { | |
428 | public function buildCondition(UserFilter &$uf) | |
429 | { | |
430 | $sub = $uf->addWatchRegistrationFilter($this->uid); | |
431 | return 'wn' . $sub . '.uid IS NOT NULL'; | |
432 | } | |
433 | } | |
434 | ||
435 | class UFC_WatchPromo extends UFC_UserRelated | |
436 | { | |
437 | private $grade; | |
438 | public function __construct($uid = null, $grade = UserFilter::GRADE_ING) | |
439 | { | |
440 | parent::__construct($uid); | |
441 | $this->grade = $grade; | |
442 | } | |
443 | ||
444 | public function buildCondition(UserFilter &$uf) | |
445 | { | |
446 | $sube = $uf->addEducationFilter(true, $this->grade); | |
447 | $subw = $uf->addWatchPromoFilter($this->uid); | |
448 | $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade); | |
449 | return $field . ' IS NOT NULL AND ' . $field . ' = wp' . $subw . '.promo'; | |
450 | } | |
451 | } | |
452 | ||
453 | class UFC_WatchContacts extends UFC_Contact | |
454 | { | |
455 | public function buildCondition(UserFilter &$uf) | |
456 | { | |
457 | $sub = $uf->addWatchFilter($this->uid); | |
458 | return 'FIND_IN_SET(\'contacts\' w' . $sub . '.flags) AND ' . parent::buildCondition($uf); | |
459 | } | |
460 | } | |
461 | ||
462 | ||
d865c296 FB |
463 | /****************** |
464 | * ORDERS | |
465 | ******************/ | |
466 | ||
467 | abstract class UserFilterOrder | |
468 | { | |
469 | protected $desc = false; | |
470 | ||
471 | public function buildSort(UserFilter &$uf) | |
472 | { | |
473 | $sel = $this->getSortTokens($uf); | |
474 | if (!is_array($sel)) { | |
475 | $sel = array($sel); | |
476 | } | |
477 | if ($this->desc) { | |
478 | foreach ($sel as $k=>$s) { | |
479 | $sel[$k] = $s . ' DESC'; | |
480 | } | |
481 | } | |
482 | return $sel; | |
483 | } | |
484 | ||
485 | abstract protected function getSortTokens(UserFilter &$uf); | |
486 | } | |
487 | ||
488 | class UFO_Promo extends UserFilterOrder | |
489 | { | |
490 | private $grade; | |
491 | ||
492 | public function __construct($grade = null, $desc = false) | |
493 | { | |
494 | $this->grade = $grade; | |
495 | $this->desc = $desc; | |
496 | } | |
497 | ||
498 | protected function getSortTokens(UserFilter &$uf) | |
499 | { | |
500 | if (UserFilter::isGrade($this->grade)) { | |
501 | $sub = $uf->addEducationFilter($this->grade); | |
502 | return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade); | |
503 | } else { | |
504 | $sub = $uf->addDisplayFilter(); | |
505 | return 'pd' . $sub . '.promo'; | |
506 | } | |
507 | } | |
508 | } | |
509 | ||
510 | class UFO_Name extends UserFilterOrder | |
511 | { | |
512 | private $type; | |
513 | private $variant; | |
514 | private $particle; | |
515 | ||
516 | public function __construct($type, $variant = null, $particle = false, $desc = false) | |
517 | { | |
518 | $this->type = $type; | |
519 | $this->variant = $variant; | |
520 | $this->particle = $particle; | |
521 | $this->desc = $desc; | |
522 | } | |
523 | ||
524 | protected function getSortTokens(UserFilter &$uf) | |
525 | { | |
526 | if (UserFilter::isDisplayName($this->type)) { | |
527 | $sub = $uf->addDisplayFilter(); | |
528 | return 'pd' . $sub . '.' . $this->type; | |
529 | } else { | |
530 | $sub = $uf->addNameFilter($this->type, $this->variant); | |
531 | if ($this->particle) { | |
532 | return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)'; | |
533 | } else { | |
534 | return 'pn' . $sub . '.name'; | |
535 | } | |
536 | } | |
537 | } | |
538 | } | |
539 | ||
38c6fe96 FB |
540 | class UFO_Registration extends UserFilterOrder |
541 | { | |
542 | public function __construct($desc = false) | |
543 | { | |
544 | $this->desc = $desc; | |
545 | } | |
546 | ||
547 | protected function getSortTokens(UserFilter &$uf) | |
548 | { | |
549 | return 'a.registration_date'; | |
550 | } | |
551 | } | |
552 | ||
d865c296 FB |
553 | /*********************************** |
554 | ********************************* | |
555 | USER FILTER CLASS | |
556 | ********************************* | |
557 | ***********************************/ | |
558 | ||
a087cc8d FB |
559 | class UserFilter |
560 | { | |
d865c296 FB |
561 | static private $joinMethods = array(); |
562 | ||
a087cc8d | 563 | private $root; |
24e08e33 | 564 | private $sort = array(); |
784745ce | 565 | private $query = null; |
24e08e33 | 566 | private $orderby = null; |
784745ce | 567 | |
aa21c568 | 568 | private $lastcount = null; |
d865c296 | 569 | |
24e08e33 | 570 | public function __construct($cond = null, $sort = null) |
5dd9d823 | 571 | { |
d865c296 FB |
572 | if (empty(self::$joinMethods)) { |
573 | $class = new ReflectionClass('UserFilter'); | |
574 | foreach ($class->getMethods() as $method) { | |
575 | $name = $method->getName(); | |
576 | if (substr($name, -5) == 'Joins' && $name != 'buildJoins') { | |
577 | self::$joinMethods[] = $name; | |
578 | } | |
579 | } | |
580 | } | |
5dd9d823 FB |
581 | if (!is_null($cond)) { |
582 | if ($cond instanceof UserFilterCondition) { | |
583 | $this->setCondition($cond); | |
584 | } | |
585 | } | |
24e08e33 FB |
586 | if (!is_null($sort)) { |
587 | if ($sort instanceof UserFilterOrder) { | |
588 | $this->addSort($sort); | |
d865c296 FB |
589 | } else if (is_array($sort)) { |
590 | foreach ($sort as $s) { | |
591 | $this->addSort($s); | |
592 | } | |
24e08e33 FB |
593 | } |
594 | } | |
5dd9d823 FB |
595 | } |
596 | ||
784745ce FB |
597 | private function buildQuery() |
598 | { | |
d865c296 FB |
599 | if (is_null($this->orderby)) { |
600 | $orders = array(); | |
601 | foreach ($this->sort as $sort) { | |
602 | $orders = array_merge($orders, $sort->buildSort($this)); | |
603 | } | |
604 | if (count($orders) == 0) { | |
605 | $this->orderby = ''; | |
606 | } else { | |
607 | $this->orderby = 'ORDER BY ' . implode(', ', $orders); | |
608 | } | |
609 | } | |
784745ce FB |
610 | if (is_null($this->query)) { |
611 | $where = $this->root->buildCondition($this); | |
612 | $joins = $this->buildJoins(); | |
613 | $this->query = 'FROM accounts AS a | |
614 | INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms)) | |
615 | INNER JOIN profiles AS p ON (p.pid = ap.pid) | |
616 | ' . $joins . ' | |
617 | WHERE (' . $where . ')'; | |
618 | } | |
619 | } | |
620 | ||
621 | private function formatJoin(array $joins) | |
622 | { | |
623 | $str = ''; | |
624 | foreach ($joins as $key => $infos) { | |
625 | $mode = $infos[0]; | |
626 | $table = $infos[1]; | |
627 | if ($mode == 'inner') { | |
628 | $str .= 'INNER JOIN '; | |
629 | } else if ($mode == 'left') { | |
630 | $str .= 'LEFT JOIN '; | |
631 | } else { | |
632 | Platal::page()->kill("Join mode error"); | |
633 | } | |
634 | $str .= $table . ' AS ' . $key; | |
635 | if (isset($infos[2])) { | |
4927ee54 | 636 | $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')'; |
784745ce FB |
637 | } |
638 | $str .= "\n"; | |
639 | } | |
640 | return $str; | |
641 | } | |
642 | ||
643 | private function buildJoins() | |
644 | { | |
d865c296 FB |
645 | $joins = array(); |
646 | foreach (self::$joinMethods as $method) { | |
647 | $joins = array_merge($joins, $this->$method()); | |
648 | } | |
784745ce FB |
649 | return $this->formatJoin($joins); |
650 | } | |
a087cc8d | 651 | |
d865c296 FB |
652 | private function getUIDList($uids = null, $count = null, $offset = null) |
653 | { | |
654 | $this->buildQuery(); | |
655 | $limit = ''; | |
656 | if (!is_null($count)) { | |
657 | if (!is_null($offset)) { | |
658 | $limit = XDB::format('LIMIT {?}, {?}', $offset, $count); | |
659 | } else { | |
660 | $limit = XDB::format('LIMIT {?}', $count); | |
661 | } | |
662 | } | |
663 | $cond = ''; | |
664 | if (!is_null($uids)) { | |
665 | $cond = ' AND a.uid IN (' . implode(', ', $uids) . ')'; | |
666 | } | |
667 | $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid | |
668 | ' . $this->query . $cond . ' | |
669 | GROUP BY a.uid | |
670 | ' . $this->orderby . ' | |
671 | ' . $limit); | |
672 | $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()'); | |
673 | return $fetched; | |
674 | } | |
675 | ||
a087cc8d FB |
676 | /** Check that the user match the given rule. |
677 | */ | |
678 | public function checkUser(PlUser &$user) | |
679 | { | |
784745ce FB |
680 | $this->buildQuery(); |
681 | $count = (int)XDB::fetchOneCell('SELECT COUNT(*) | |
682 | ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id())); | |
683 | return $count == 1; | |
a087cc8d FB |
684 | } |
685 | ||
686 | /** Filter a list of user to extract the users matching the rule. | |
687 | */ | |
d865c296 | 688 | public function filter(array $users, $count = null, $offset = null) |
a087cc8d | 689 | { |
4927ee54 FB |
690 | $this->buildQuery(); |
691 | $table = array(); | |
692 | $uids = array(); | |
693 | foreach ($users as $user) { | |
694 | $uids[] = $user->id(); | |
695 | $table[$user->id()] = $user; | |
696 | } | |
d865c296 | 697 | $fetched = $this->getUIDList($uids, $count, $offset); |
a087cc8d | 698 | $output = array(); |
4927ee54 FB |
699 | foreach ($fetched as $uid) { |
700 | $output[] = $table[$uid]; | |
a087cc8d FB |
701 | } |
702 | return $output; | |
703 | } | |
704 | ||
d865c296 | 705 | public function getUIDs($count = null, $offset = null) |
4927ee54 | 706 | { |
d865c296 FB |
707 | return $this->getUIDList(null, $count, $offset); |
708 | } | |
709 | ||
710 | public function getUsers($count = null, $offset = null) | |
711 | { | |
712 | return User::getBulkUsersWithUIDs($this->getUIDs($count, $offset)); | |
4927ee54 FB |
713 | } |
714 | ||
d865c296 | 715 | public function getTotalCount() |
4927ee54 | 716 | { |
38c6fe96 | 717 | if (is_null($this->lastcount)) { |
aa21c568 | 718 | $this->buildQuery(); |
38c6fe96 FB |
719 | return (int)XDB::fetchOneCell('SELECT COUNT(*) |
720 | ' . $this->query . ' | |
721 | GROUP BY a.uid'); | |
722 | } else { | |
723 | return $this->lastcount; | |
724 | } | |
4927ee54 FB |
725 | } |
726 | ||
a087cc8d FB |
727 | public function setCondition(UserFilterCondition &$cond) |
728 | { | |
729 | $this->root =& $cond; | |
784745ce | 730 | $this->query = null; |
a087cc8d FB |
731 | } |
732 | ||
24e08e33 FB |
733 | public function addSort(UserFilterOrder &$sort) |
734 | { | |
d865c296 FB |
735 | $this->sort[] = $sort; |
736 | $this->orderby = null; | |
24e08e33 FB |
737 | } |
738 | ||
a087cc8d FB |
739 | static public function getLegacy($promo_min, $promo_max) |
740 | { | |
a087cc8d | 741 | if ($promo_min != 0) { |
784745ce | 742 | $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min)); |
5dd9d823 FB |
743 | } else { |
744 | $min = new UFC_True(); | |
a087cc8d | 745 | } |
a087cc8d | 746 | if ($promo_max != 0) { |
784745ce | 747 | $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max)); |
a087cc8d | 748 | } else { |
5dd9d823 | 749 | $max = new UFC_True(); |
a087cc8d | 750 | } |
5dd9d823 | 751 | return new UserFilter(new UFC_And($min, $max)); |
a087cc8d | 752 | } |
784745ce | 753 | |
aa21c568 FB |
754 | static private function getDBSuffix($string) |
755 | { | |
756 | return preg_replace('/[^a-z0-9]/i', '', $string); | |
757 | } | |
758 | ||
759 | ||
760 | private $option = 0; | |
761 | private function register_optional(array &$table, $val) | |
762 | { | |
763 | if (is_null($val)) { | |
764 | $sub = $this->option++; | |
765 | $index = null; | |
766 | } else { | |
767 | $sub = self::getDBSuffix($val); | |
768 | $index = $val; | |
769 | } | |
770 | $sub = '_' . $sub; | |
771 | $table[$sub] = $index; | |
772 | return $sub; | |
773 | } | |
784745ce | 774 | |
d865c296 FB |
775 | /** DISPLAY |
776 | */ | |
38c6fe96 | 777 | const DISPLAY = 'display'; |
d865c296 FB |
778 | private $pd = false; |
779 | public function addDisplayFilter() | |
780 | { | |
781 | $this->pd = true; | |
782 | return ''; | |
783 | } | |
784 | ||
785 | private function displayJoins() | |
786 | { | |
787 | if ($this->pd) { | |
788 | return array('pd' => array('left', 'profile_display', '$ME.pid = $PID')); | |
789 | } else { | |
790 | return array(); | |
791 | } | |
792 | } | |
793 | ||
784745ce FB |
794 | /** NAMES |
795 | */ | |
d865c296 | 796 | /* name tokens */ |
784745ce FB |
797 | const LASTNAME = 'lastname'; |
798 | const FIRSTNAME = 'firstname'; | |
799 | const NICKNAME = 'nickname'; | |
800 | const PSEUDONYM = 'pseudonym'; | |
801 | const NAME = 'name'; | |
d865c296 | 802 | /* name variants */ |
784745ce FB |
803 | const VN_MARITAL = 'marital'; |
804 | const VN_ORDINARY = 'ordinary'; | |
805 | const VN_OTHER = 'other'; | |
806 | const VN_INI = 'ini'; | |
d865c296 FB |
807 | /* display names */ |
808 | const DN_FULL = 'directory_name'; | |
809 | const DN_DISPLAY = 'yourself'; | |
810 | const DN_YOURSELF = 'yourself'; | |
811 | const DN_DIRECTORY = 'directory_name'; | |
812 | const DN_PRIVATE = 'private_name'; | |
813 | const DN_PUBLIC = 'public_name'; | |
814 | const DN_SHORT = 'short_name'; | |
815 | const DN_SORT = 'sort_name'; | |
784745ce FB |
816 | |
817 | static public $name_variants = array( | |
818 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), | |
d865c296 FB |
819 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) |
820 | ); | |
784745ce FB |
821 | |
822 | static public function assertName($name) | |
823 | { | |
824 | if (!Profile::getNameTypeId($name)) { | |
825 | Platal::page()->kill('Invalid name type'); | |
826 | } | |
827 | } | |
828 | ||
d865c296 FB |
829 | static public function isDisplayName($name) |
830 | { | |
831 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
832 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
833 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
834 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
835 | } | |
836 | ||
784745ce | 837 | private $pn = array(); |
784745ce FB |
838 | public function addNameFilter($type, $variant = null) |
839 | { | |
840 | if (!is_null($variant)) { | |
841 | $ft = $type . '_' . $variant; | |
842 | } else { | |
843 | $ft = $type; | |
844 | } | |
845 | $sub = '_' . $ft; | |
846 | self::assertName($ft); | |
847 | ||
848 | if (!is_null($variant) && $variant == 'other') { | |
aa21c568 | 849 | $sub .= $this->option++; |
784745ce FB |
850 | } |
851 | $this->pn[$sub] = Profile::getNameTypeId($ft); | |
852 | return $sub; | |
853 | } | |
854 | ||
855 | private function nameJoins() | |
856 | { | |
857 | $joins = array(); | |
858 | foreach ($this->pn as $sub => $type) { | |
859 | $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type); | |
860 | } | |
861 | return $joins; | |
862 | } | |
863 | ||
864 | ||
865 | /** EDUCATION | |
866 | */ | |
867 | const GRADE_ING = 'Ing.'; | |
868 | const GRADE_PHD = 'PhD'; | |
869 | const GRADE_MST = 'M%'; | |
870 | static public function isGrade($grade) | |
871 | { | |
38c6fe96 | 872 | return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST; |
784745ce FB |
873 | } |
874 | ||
875 | static public function assertGrade($grade) | |
876 | { | |
877 | if (!self::isGrade($grade)) { | |
878 | Platal::page()->killError("DiplĂ´me non valide"); | |
879 | } | |
880 | } | |
881 | ||
d865c296 FB |
882 | static public function promoYear($grade) |
883 | { | |
884 | // XXX: Definition of promotion for phds and masters might change in near future. | |
885 | return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year'; | |
886 | } | |
887 | ||
784745ce FB |
888 | private $pepe = array(); |
889 | private $with_pee = false; | |
784745ce FB |
890 | public function addEducationFilter($x = false, $grade = null) |
891 | { | |
892 | if (!$x) { | |
aa21c568 FB |
893 | $index = $this->option; |
894 | $sub = $this->option++; | |
784745ce FB |
895 | } else { |
896 | self::assertGrade($grade); | |
897 | $index = $grade; | |
898 | $sub = $grade[0]; | |
899 | $this->with_pee = true; | |
900 | } | |
901 | $sub = '_' . $sub; | |
902 | $this->pepe[$index] = $sub; | |
903 | return $sub; | |
904 | } | |
905 | ||
906 | private function educationJoins() | |
907 | { | |
908 | $joins = array(); | |
909 | if ($this->with_pee) { | |
910 | $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\''); | |
911 | } | |
912 | foreach ($this->pepe as $grade => $sub) { | |
913 | if ($this->isGrade($grade)) { | |
914 | $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID'); | |
915 | $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' . | |
916 | XDB::format('{?}', $grade)); | |
917 | } else { | |
918 | $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID'); | |
919 | $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid'); | |
920 | $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid'); | |
921 | } | |
922 | } | |
923 | return $joins; | |
924 | } | |
4927ee54 FB |
925 | |
926 | ||
927 | /** GROUPS | |
928 | */ | |
929 | private $gpm = array(); | |
4927ee54 FB |
930 | public function addGroupFilter($group = null) |
931 | { | |
932 | if (!is_null($group)) { | |
933 | if (ctype_digit($group)) { | |
934 | $index = $sub = $group; | |
935 | } else { | |
936 | $index = $group; | |
aa21c568 | 937 | $sub = self::getDBSuffix($group); |
4927ee54 FB |
938 | } |
939 | } else { | |
aa21c568 | 940 | $sub = 'group_' . $this->option++; |
4927ee54 FB |
941 | $index = null; |
942 | } | |
943 | $sub = '_' . $sub; | |
944 | $this->gpm[$sub] = $index; | |
945 | return $sub; | |
946 | } | |
947 | ||
948 | private function groupJoins() | |
949 | { | |
950 | $joins = array(); | |
951 | foreach ($this->gpm as $sub => $key) { | |
952 | if (is_null($key)) { | |
953 | $joins['gpa' . $sub] = array('inner', 'groupex.asso'); | |
954 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
955 | } else if (ctype_digit($key)) { | |
956 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key); | |
957 | } else { | |
958 | $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key)); | |
959 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
960 | } | |
961 | } | |
962 | return $joins; | |
963 | } | |
aa21c568 FB |
964 | |
965 | /** EMAILS | |
966 | */ | |
967 | private $e = array(); | |
968 | public function addEmailRedirectFilter($email = null) | |
969 | { | |
970 | return $this->register_optional($this->e, $email); | |
971 | } | |
972 | ||
973 | private $ve = array(); | |
974 | public function addVirtualEmailFilter($email = null) | |
975 | { | |
21401768 | 976 | $this->addAliasFilter(self::ALIAS_FORLIFE); |
aa21c568 FB |
977 | return $this->register_optional($this->ve, $email); |
978 | } | |
979 | ||
21401768 FB |
980 | const ALIAS_BEST = 'bestalias'; |
981 | const ALIAS_FORLIFE = 'forlife'; | |
aa21c568 FB |
982 | private $al = array(); |
983 | public function addAliasFilter($alias = null) | |
984 | { | |
985 | return $this->register_optional($this->al, $alias); | |
986 | } | |
987 | ||
988 | private function emailJoins() | |
989 | { | |
990 | global $globals; | |
991 | $joins = array(); | |
992 | foreach ($this->e as $sub=>$key) { | |
993 | if (is_null($key)) { | |
994 | $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\''); | |
995 | } else { | |
996 | $joins['e' . $sub] = array('left', 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key)); | |
997 | } | |
998 | } | |
21401768 | 999 | foreach ($this->al as $sub=>$key) { |
aa21c568 | 1000 | if (is_null($key)) { |
21401768 FB |
1001 | $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')'); |
1002 | } else if ($key == self::ALIAS_BEST) { | |
1003 | $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)'); | |
1004 | } else if ($key == self::ALIAS_FORLIFE) { | |
1005 | $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\''); | |
aa21c568 | 1006 | } else { |
21401768 | 1007 | $joins['al' . $sub] = array('left', 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key)); |
aa21c568 | 1008 | } |
aa21c568 | 1009 | } |
21401768 | 1010 | foreach ($this->ve as $sub=>$key) { |
aa21c568 | 1011 | if (is_null($key)) { |
21401768 | 1012 | $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\''); |
aa21c568 | 1013 | } else { |
21401768 | 1014 | $joins['v' . $sub] = array('left', 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key)); |
aa21c568 | 1015 | } |
21401768 FB |
1016 | $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid |
1017 | AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}), | |
1018 | CONCAT(al_forlife.alias, \'@\', {?}), | |
1019 | a.email))', | |
1020 | $globals->mail->domain, $globals->mail->domain2)); | |
aa21c568 FB |
1021 | } |
1022 | return $joins; | |
1023 | } | |
3f42a6ad FB |
1024 | |
1025 | ||
1026 | /** CONTACTS | |
1027 | */ | |
1028 | private $cts = array(); | |
1029 | public function addContactFilter($uid = null) | |
1030 | { | |
1031 | return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid); | |
1032 | } | |
1033 | ||
1034 | private function contactJoins() | |
1035 | { | |
1036 | $joins = array(); | |
1037 | foreach ($this->cts as $sub=>$key) { | |
1038 | if (is_null($key)) { | |
1039 | $joins['c' . $sub] = array('left', 'contacts', '$ME.contact = $UID'); | |
1040 | } else { | |
1041 | $joins['c' . $sub] = array('left', 'contacts', XDB::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5))); | |
1042 | } | |
1043 | } | |
1044 | return $joins; | |
1045 | } | |
4e7bf1e0 FB |
1046 | |
1047 | ||
1048 | /** CARNET | |
1049 | */ | |
1050 | private $wn = array(); | |
1051 | public function addWatchRegistrationFilter($uid = null) | |
1052 | { | |
1053 | return $this->register_optional($this->wn, is_null($uid) ? null : 'user_' . $uid); | |
1054 | } | |
1055 | ||
1056 | private $wp = array(); | |
1057 | public function addWatchPromoFilter($uid = null) | |
1058 | { | |
1059 | return $this->register_optional($this->wp, is_null($uid) ? null : 'user_' . $uid); | |
1060 | } | |
1061 | ||
1062 | private $w = array(); | |
1063 | public function addWatchFilter($uid = null) | |
1064 | { | |
1065 | return $this->register_optional($this->w, is_null($uid) ? null : 'user_' . $uid); | |
1066 | } | |
1067 | ||
1068 | private function watchJoins() | |
1069 | { | |
1070 | $joins = array(); | |
1071 | foreach ($this->w as $sub=>$key) { | |
1072 | if (is_null($key)) { | |
1073 | $joins['w' . $sub] = array('left', 'watch'); | |
1074 | } else { | |
1075 | $joins['w' . $sub] = array('left', 'watch', XDB::format('$ME.uid = {?}', substr($key, 5))); | |
1076 | } | |
1077 | } | |
1078 | foreach ($this->wn as $sub=>$key) { | |
1079 | if (is_null($key)) { | |
1080 | $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID'); | |
1081 | } else { | |
1082 | $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5))); | |
1083 | } | |
1084 | } | |
1085 | foreach ($this->wn as $sub=>$key) { | |
1086 | if (is_null($key)) { | |
1087 | $joins['wn' . $sub] = array('left', 'watch_nonins', '$ME.ni_id = $UID'); | |
1088 | } else { | |
1089 | $joins['wn' . $sub] = array('left', 'watch_nonins', XDB::format('$ME.uid = {?} AND $ME.ni_id = $UID', substr($key, 5))); | |
1090 | } | |
1091 | } | |
1092 | foreach ($this->wp as $sub=>$key) { | |
1093 | if (is_null($key)) { | |
1094 | $joins['wp' . $sub] = array('left', 'watch_promo'); | |
1095 | } else { | |
1096 | $joins['wp' . $sub] = array('left', 'watch_promo', XDB::format('$ME.uid = {?}', substr($key, 5))); | |
1097 | } | |
1098 | } | |
1099 | return $joins; | |
1100 | } | |
a087cc8d FB |
1101 | } |
1102 | ||
3f42a6ad | 1103 | |
a087cc8d FB |
1104 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
1105 | ?> |