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 | |
3f42a6ad FB |
400 | class UFC_Contact implements UserFilterCondition |
401 | { | |
402 | private $uid; | |
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 | } | |
415 | ||
416 | public function buildCondition(UserFilter &$uf) | |
417 | { | |
418 | $sub = $uf->addContactFilter($this->uid); | |
419 | return 'c' . $sub . '.contact IS NOT NULL'; | |
420 | } | |
421 | } | |
422 | ||
d865c296 FB |
423 | /****************** |
424 | * ORDERS | |
425 | ******************/ | |
426 | ||
427 | abstract class UserFilterOrder | |
428 | { | |
429 | protected $desc = false; | |
430 | ||
431 | public function buildSort(UserFilter &$uf) | |
432 | { | |
433 | $sel = $this->getSortTokens($uf); | |
434 | if (!is_array($sel)) { | |
435 | $sel = array($sel); | |
436 | } | |
437 | if ($this->desc) { | |
438 | foreach ($sel as $k=>$s) { | |
439 | $sel[$k] = $s . ' DESC'; | |
440 | } | |
441 | } | |
442 | return $sel; | |
443 | } | |
444 | ||
445 | abstract protected function getSortTokens(UserFilter &$uf); | |
446 | } | |
447 | ||
448 | class UFO_Promo extends UserFilterOrder | |
449 | { | |
450 | private $grade; | |
451 | ||
452 | public function __construct($grade = null, $desc = false) | |
453 | { | |
454 | $this->grade = $grade; | |
455 | $this->desc = $desc; | |
456 | } | |
457 | ||
458 | protected function getSortTokens(UserFilter &$uf) | |
459 | { | |
460 | if (UserFilter::isGrade($this->grade)) { | |
461 | $sub = $uf->addEducationFilter($this->grade); | |
462 | return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade); | |
463 | } else { | |
464 | $sub = $uf->addDisplayFilter(); | |
465 | return 'pd' . $sub . '.promo'; | |
466 | } | |
467 | } | |
468 | } | |
469 | ||
470 | class UFO_Name extends UserFilterOrder | |
471 | { | |
472 | private $type; | |
473 | private $variant; | |
474 | private $particle; | |
475 | ||
476 | public function __construct($type, $variant = null, $particle = false, $desc = false) | |
477 | { | |
478 | $this->type = $type; | |
479 | $this->variant = $variant; | |
480 | $this->particle = $particle; | |
481 | $this->desc = $desc; | |
482 | } | |
483 | ||
484 | protected function getSortTokens(UserFilter &$uf) | |
485 | { | |
486 | if (UserFilter::isDisplayName($this->type)) { | |
487 | $sub = $uf->addDisplayFilter(); | |
488 | return 'pd' . $sub . '.' . $this->type; | |
489 | } else { | |
490 | $sub = $uf->addNameFilter($this->type, $this->variant); | |
491 | if ($this->particle) { | |
492 | return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)'; | |
493 | } else { | |
494 | return 'pn' . $sub . '.name'; | |
495 | } | |
496 | } | |
497 | } | |
498 | } | |
499 | ||
38c6fe96 FB |
500 | class UFO_Registration extends UserFilterOrder |
501 | { | |
502 | public function __construct($desc = false) | |
503 | { | |
504 | $this->desc = $desc; | |
505 | } | |
506 | ||
507 | protected function getSortTokens(UserFilter &$uf) | |
508 | { | |
509 | return 'a.registration_date'; | |
510 | } | |
511 | } | |
512 | ||
d865c296 FB |
513 | /*********************************** |
514 | ********************************* | |
515 | USER FILTER CLASS | |
516 | ********************************* | |
517 | ***********************************/ | |
518 | ||
a087cc8d FB |
519 | class UserFilter |
520 | { | |
d865c296 FB |
521 | static private $joinMethods = array(); |
522 | ||
a087cc8d | 523 | private $root; |
24e08e33 | 524 | private $sort = array(); |
784745ce | 525 | private $query = null; |
24e08e33 | 526 | private $orderby = null; |
784745ce | 527 | |
aa21c568 | 528 | private $lastcount = null; |
d865c296 | 529 | |
24e08e33 | 530 | public function __construct($cond = null, $sort = null) |
5dd9d823 | 531 | { |
d865c296 FB |
532 | if (empty(self::$joinMethods)) { |
533 | $class = new ReflectionClass('UserFilter'); | |
534 | foreach ($class->getMethods() as $method) { | |
535 | $name = $method->getName(); | |
536 | if (substr($name, -5) == 'Joins' && $name != 'buildJoins') { | |
537 | self::$joinMethods[] = $name; | |
538 | } | |
539 | } | |
540 | } | |
5dd9d823 FB |
541 | if (!is_null($cond)) { |
542 | if ($cond instanceof UserFilterCondition) { | |
543 | $this->setCondition($cond); | |
544 | } | |
545 | } | |
24e08e33 FB |
546 | if (!is_null($sort)) { |
547 | if ($sort instanceof UserFilterOrder) { | |
548 | $this->addSort($sort); | |
d865c296 FB |
549 | } else if (is_array($sort)) { |
550 | foreach ($sort as $s) { | |
551 | $this->addSort($s); | |
552 | } | |
24e08e33 FB |
553 | } |
554 | } | |
5dd9d823 FB |
555 | } |
556 | ||
784745ce FB |
557 | private function buildQuery() |
558 | { | |
d865c296 FB |
559 | if (is_null($this->orderby)) { |
560 | $orders = array(); | |
561 | foreach ($this->sort as $sort) { | |
562 | $orders = array_merge($orders, $sort->buildSort($this)); | |
563 | } | |
564 | if (count($orders) == 0) { | |
565 | $this->orderby = ''; | |
566 | } else { | |
567 | $this->orderby = 'ORDER BY ' . implode(', ', $orders); | |
568 | } | |
569 | } | |
784745ce FB |
570 | if (is_null($this->query)) { |
571 | $where = $this->root->buildCondition($this); | |
572 | $joins = $this->buildJoins(); | |
573 | $this->query = 'FROM accounts AS a | |
574 | INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms)) | |
575 | INNER JOIN profiles AS p ON (p.pid = ap.pid) | |
576 | ' . $joins . ' | |
577 | WHERE (' . $where . ')'; | |
578 | } | |
579 | } | |
580 | ||
581 | private function formatJoin(array $joins) | |
582 | { | |
583 | $str = ''; | |
584 | foreach ($joins as $key => $infos) { | |
585 | $mode = $infos[0]; | |
586 | $table = $infos[1]; | |
587 | if ($mode == 'inner') { | |
588 | $str .= 'INNER JOIN '; | |
589 | } else if ($mode == 'left') { | |
590 | $str .= 'LEFT JOIN '; | |
591 | } else { | |
592 | Platal::page()->kill("Join mode error"); | |
593 | } | |
594 | $str .= $table . ' AS ' . $key; | |
595 | if (isset($infos[2])) { | |
4927ee54 | 596 | $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')'; |
784745ce FB |
597 | } |
598 | $str .= "\n"; | |
599 | } | |
600 | return $str; | |
601 | } | |
602 | ||
603 | private function buildJoins() | |
604 | { | |
d865c296 FB |
605 | $joins = array(); |
606 | foreach (self::$joinMethods as $method) { | |
607 | $joins = array_merge($joins, $this->$method()); | |
608 | } | |
784745ce FB |
609 | return $this->formatJoin($joins); |
610 | } | |
a087cc8d | 611 | |
d865c296 FB |
612 | private function getUIDList($uids = null, $count = null, $offset = null) |
613 | { | |
614 | $this->buildQuery(); | |
615 | $limit = ''; | |
616 | if (!is_null($count)) { | |
617 | if (!is_null($offset)) { | |
618 | $limit = XDB::format('LIMIT {?}, {?}', $offset, $count); | |
619 | } else { | |
620 | $limit = XDB::format('LIMIT {?}', $count); | |
621 | } | |
622 | } | |
623 | $cond = ''; | |
624 | if (!is_null($uids)) { | |
625 | $cond = ' AND a.uid IN (' . implode(', ', $uids) . ')'; | |
626 | } | |
627 | $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid | |
628 | ' . $this->query . $cond . ' | |
629 | GROUP BY a.uid | |
630 | ' . $this->orderby . ' | |
631 | ' . $limit); | |
632 | $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()'); | |
633 | return $fetched; | |
634 | } | |
635 | ||
a087cc8d FB |
636 | /** Check that the user match the given rule. |
637 | */ | |
638 | public function checkUser(PlUser &$user) | |
639 | { | |
784745ce FB |
640 | $this->buildQuery(); |
641 | $count = (int)XDB::fetchOneCell('SELECT COUNT(*) | |
642 | ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id())); | |
643 | return $count == 1; | |
a087cc8d FB |
644 | } |
645 | ||
646 | /** Filter a list of user to extract the users matching the rule. | |
647 | */ | |
d865c296 | 648 | public function filter(array $users, $count = null, $offset = null) |
a087cc8d | 649 | { |
4927ee54 FB |
650 | $this->buildQuery(); |
651 | $table = array(); | |
652 | $uids = array(); | |
653 | foreach ($users as $user) { | |
654 | $uids[] = $user->id(); | |
655 | $table[$user->id()] = $user; | |
656 | } | |
d865c296 | 657 | $fetched = $this->getUIDList($uids, $count, $offset); |
a087cc8d | 658 | $output = array(); |
4927ee54 FB |
659 | foreach ($fetched as $uid) { |
660 | $output[] = $table[$uid]; | |
a087cc8d FB |
661 | } |
662 | return $output; | |
663 | } | |
664 | ||
d865c296 | 665 | public function getUIDs($count = null, $offset = null) |
4927ee54 | 666 | { |
d865c296 FB |
667 | return $this->getUIDList(null, $count, $offset); |
668 | } | |
669 | ||
670 | public function getUsers($count = null, $offset = null) | |
671 | { | |
672 | return User::getBulkUsersWithUIDs($this->getUIDs($count, $offset)); | |
4927ee54 FB |
673 | } |
674 | ||
d865c296 | 675 | public function getTotalCount() |
4927ee54 | 676 | { |
38c6fe96 | 677 | if (is_null($this->lastcount)) { |
aa21c568 | 678 | $this->buildQuery(); |
38c6fe96 FB |
679 | return (int)XDB::fetchOneCell('SELECT COUNT(*) |
680 | ' . $this->query . ' | |
681 | GROUP BY a.uid'); | |
682 | } else { | |
683 | return $this->lastcount; | |
684 | } | |
4927ee54 FB |
685 | } |
686 | ||
a087cc8d FB |
687 | public function setCondition(UserFilterCondition &$cond) |
688 | { | |
689 | $this->root =& $cond; | |
784745ce | 690 | $this->query = null; |
a087cc8d FB |
691 | } |
692 | ||
24e08e33 FB |
693 | public function addSort(UserFilterOrder &$sort) |
694 | { | |
d865c296 FB |
695 | $this->sort[] = $sort; |
696 | $this->orderby = null; | |
24e08e33 FB |
697 | } |
698 | ||
a087cc8d FB |
699 | static public function getLegacy($promo_min, $promo_max) |
700 | { | |
a087cc8d | 701 | if ($promo_min != 0) { |
784745ce | 702 | $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min)); |
5dd9d823 FB |
703 | } else { |
704 | $min = new UFC_True(); | |
a087cc8d | 705 | } |
a087cc8d | 706 | if ($promo_max != 0) { |
784745ce | 707 | $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max)); |
a087cc8d | 708 | } else { |
5dd9d823 | 709 | $max = new UFC_True(); |
a087cc8d | 710 | } |
5dd9d823 | 711 | return new UserFilter(new UFC_And($min, $max)); |
a087cc8d | 712 | } |
784745ce | 713 | |
aa21c568 FB |
714 | static private function getDBSuffix($string) |
715 | { | |
716 | return preg_replace('/[^a-z0-9]/i', '', $string); | |
717 | } | |
718 | ||
719 | ||
720 | private $option = 0; | |
721 | private function register_optional(array &$table, $val) | |
722 | { | |
723 | if (is_null($val)) { | |
724 | $sub = $this->option++; | |
725 | $index = null; | |
726 | } else { | |
727 | $sub = self::getDBSuffix($val); | |
728 | $index = $val; | |
729 | } | |
730 | $sub = '_' . $sub; | |
731 | $table[$sub] = $index; | |
732 | return $sub; | |
733 | } | |
784745ce | 734 | |
d865c296 FB |
735 | /** DISPLAY |
736 | */ | |
38c6fe96 | 737 | const DISPLAY = 'display'; |
d865c296 FB |
738 | private $pd = false; |
739 | public function addDisplayFilter() | |
740 | { | |
741 | $this->pd = true; | |
742 | return ''; | |
743 | } | |
744 | ||
745 | private function displayJoins() | |
746 | { | |
747 | if ($this->pd) { | |
748 | return array('pd' => array('left', 'profile_display', '$ME.pid = $PID')); | |
749 | } else { | |
750 | return array(); | |
751 | } | |
752 | } | |
753 | ||
784745ce FB |
754 | /** NAMES |
755 | */ | |
d865c296 | 756 | /* name tokens */ |
784745ce FB |
757 | const LASTNAME = 'lastname'; |
758 | const FIRSTNAME = 'firstname'; | |
759 | const NICKNAME = 'nickname'; | |
760 | const PSEUDONYM = 'pseudonym'; | |
761 | const NAME = 'name'; | |
d865c296 | 762 | /* name variants */ |
784745ce FB |
763 | const VN_MARITAL = 'marital'; |
764 | const VN_ORDINARY = 'ordinary'; | |
765 | const VN_OTHER = 'other'; | |
766 | const VN_INI = 'ini'; | |
d865c296 FB |
767 | /* display names */ |
768 | const DN_FULL = 'directory_name'; | |
769 | const DN_DISPLAY = 'yourself'; | |
770 | const DN_YOURSELF = 'yourself'; | |
771 | const DN_DIRECTORY = 'directory_name'; | |
772 | const DN_PRIVATE = 'private_name'; | |
773 | const DN_PUBLIC = 'public_name'; | |
774 | const DN_SHORT = 'short_name'; | |
775 | const DN_SORT = 'sort_name'; | |
784745ce FB |
776 | |
777 | static public $name_variants = array( | |
778 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), | |
d865c296 FB |
779 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) |
780 | ); | |
784745ce FB |
781 | |
782 | static public function assertName($name) | |
783 | { | |
784 | if (!Profile::getNameTypeId($name)) { | |
785 | Platal::page()->kill('Invalid name type'); | |
786 | } | |
787 | } | |
788 | ||
d865c296 FB |
789 | static public function isDisplayName($name) |
790 | { | |
791 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
792 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
793 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
794 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
795 | } | |
796 | ||
784745ce | 797 | private $pn = array(); |
784745ce FB |
798 | public function addNameFilter($type, $variant = null) |
799 | { | |
800 | if (!is_null($variant)) { | |
801 | $ft = $type . '_' . $variant; | |
802 | } else { | |
803 | $ft = $type; | |
804 | } | |
805 | $sub = '_' . $ft; | |
806 | self::assertName($ft); | |
807 | ||
808 | if (!is_null($variant) && $variant == 'other') { | |
aa21c568 | 809 | $sub .= $this->option++; |
784745ce FB |
810 | } |
811 | $this->pn[$sub] = Profile::getNameTypeId($ft); | |
812 | return $sub; | |
813 | } | |
814 | ||
815 | private function nameJoins() | |
816 | { | |
817 | $joins = array(); | |
818 | foreach ($this->pn as $sub => $type) { | |
819 | $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type); | |
820 | } | |
821 | return $joins; | |
822 | } | |
823 | ||
824 | ||
825 | /** EDUCATION | |
826 | */ | |
827 | const GRADE_ING = 'Ing.'; | |
828 | const GRADE_PHD = 'PhD'; | |
829 | const GRADE_MST = 'M%'; | |
830 | static public function isGrade($grade) | |
831 | { | |
38c6fe96 | 832 | return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST; |
784745ce FB |
833 | } |
834 | ||
835 | static public function assertGrade($grade) | |
836 | { | |
837 | if (!self::isGrade($grade)) { | |
838 | Platal::page()->killError("DiplĂ´me non valide"); | |
839 | } | |
840 | } | |
841 | ||
d865c296 FB |
842 | static public function promoYear($grade) |
843 | { | |
844 | // XXX: Definition of promotion for phds and masters might change in near future. | |
845 | return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year'; | |
846 | } | |
847 | ||
784745ce FB |
848 | private $pepe = array(); |
849 | private $with_pee = false; | |
784745ce FB |
850 | public function addEducationFilter($x = false, $grade = null) |
851 | { | |
852 | if (!$x) { | |
aa21c568 FB |
853 | $index = $this->option; |
854 | $sub = $this->option++; | |
784745ce FB |
855 | } else { |
856 | self::assertGrade($grade); | |
857 | $index = $grade; | |
858 | $sub = $grade[0]; | |
859 | $this->with_pee = true; | |
860 | } | |
861 | $sub = '_' . $sub; | |
862 | $this->pepe[$index] = $sub; | |
863 | return $sub; | |
864 | } | |
865 | ||
866 | private function educationJoins() | |
867 | { | |
868 | $joins = array(); | |
869 | if ($this->with_pee) { | |
870 | $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\''); | |
871 | } | |
872 | foreach ($this->pepe as $grade => $sub) { | |
873 | if ($this->isGrade($grade)) { | |
874 | $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID'); | |
875 | $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' . | |
876 | XDB::format('{?}', $grade)); | |
877 | } else { | |
878 | $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID'); | |
879 | $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid'); | |
880 | $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid'); | |
881 | } | |
882 | } | |
883 | return $joins; | |
884 | } | |
4927ee54 FB |
885 | |
886 | ||
887 | /** GROUPS | |
888 | */ | |
889 | private $gpm = array(); | |
4927ee54 FB |
890 | public function addGroupFilter($group = null) |
891 | { | |
892 | if (!is_null($group)) { | |
893 | if (ctype_digit($group)) { | |
894 | $index = $sub = $group; | |
895 | } else { | |
896 | $index = $group; | |
aa21c568 | 897 | $sub = self::getDBSuffix($group); |
4927ee54 FB |
898 | } |
899 | } else { | |
aa21c568 | 900 | $sub = 'group_' . $this->option++; |
4927ee54 FB |
901 | $index = null; |
902 | } | |
903 | $sub = '_' . $sub; | |
904 | $this->gpm[$sub] = $index; | |
905 | return $sub; | |
906 | } | |
907 | ||
908 | private function groupJoins() | |
909 | { | |
910 | $joins = array(); | |
911 | foreach ($this->gpm as $sub => $key) { | |
912 | if (is_null($key)) { | |
913 | $joins['gpa' . $sub] = array('inner', 'groupex.asso'); | |
914 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
915 | } else if (ctype_digit($key)) { | |
916 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key); | |
917 | } else { | |
918 | $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key)); | |
919 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
920 | } | |
921 | } | |
922 | return $joins; | |
923 | } | |
aa21c568 FB |
924 | |
925 | /** EMAILS | |
926 | */ | |
927 | private $e = array(); | |
928 | public function addEmailRedirectFilter($email = null) | |
929 | { | |
930 | return $this->register_optional($this->e, $email); | |
931 | } | |
932 | ||
933 | private $ve = array(); | |
934 | public function addVirtualEmailFilter($email = null) | |
935 | { | |
21401768 | 936 | $this->addAliasFilter(self::ALIAS_FORLIFE); |
aa21c568 FB |
937 | return $this->register_optional($this->ve, $email); |
938 | } | |
939 | ||
21401768 FB |
940 | const ALIAS_BEST = 'bestalias'; |
941 | const ALIAS_FORLIFE = 'forlife'; | |
aa21c568 FB |
942 | private $al = array(); |
943 | public function addAliasFilter($alias = null) | |
944 | { | |
945 | return $this->register_optional($this->al, $alias); | |
946 | } | |
947 | ||
948 | private function emailJoins() | |
949 | { | |
950 | global $globals; | |
951 | $joins = array(); | |
952 | foreach ($this->e as $sub=>$key) { | |
953 | if (is_null($key)) { | |
954 | $joins['e' . $sub] = array('left', 'emails', '$ME.uid = $UID AND $ME.flags != \'filter\''); | |
955 | } else { | |
956 | $joins['e' . $sub] = array('left', 'emails', XDB::format('$ME.uid = $UID AND $ME.flags != \'filter\' AND $ME.email = {?}', $key)); | |
957 | } | |
958 | } | |
21401768 | 959 | foreach ($this->al as $sub=>$key) { |
aa21c568 | 960 | if (is_null($key)) { |
21401768 FB |
961 | $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')'); |
962 | } else if ($key == self::ALIAS_BEST) { | |
963 | $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND FIND_IN_SET(\'bestalias\', $ME.flags)'); | |
964 | } else if ($key == self::ALIAS_FORLIFE) { | |
965 | $joins['al' . $sub] = array('left', 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\''); | |
aa21c568 | 966 | } else { |
21401768 | 967 | $joins['al' . $sub] = array('left', 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key)); |
aa21c568 | 968 | } |
aa21c568 | 969 | } |
21401768 | 970 | foreach ($this->ve as $sub=>$key) { |
aa21c568 | 971 | if (is_null($key)) { |
21401768 | 972 | $joins['v' . $sub] = array('left', 'virtual', '$ME.type = \'user\''); |
aa21c568 | 973 | } else { |
21401768 | 974 | $joins['v' . $sub] = array('left', 'virtual', XDB::format('$ME.type = \'user\' AND $ME.alias = {?}', $key)); |
aa21c568 | 975 | } |
21401768 FB |
976 | $joins['vr' . $sub] = array('left', 'virtual_redirect', XDB::format('$ME.vid = v' . $sub . '.vid |
977 | AND ($ME.redirect IN (CONCAT(al_forlife.alias, \'@\', {?}), | |
978 | CONCAT(al_forlife.alias, \'@\', {?}), | |
979 | a.email))', | |
980 | $globals->mail->domain, $globals->mail->domain2)); | |
aa21c568 FB |
981 | } |
982 | return $joins; | |
983 | } | |
3f42a6ad FB |
984 | |
985 | ||
986 | /** CONTACTS | |
987 | */ | |
988 | private $cts = array(); | |
989 | public function addContactFilter($uid = null) | |
990 | { | |
991 | return $this->register_optional($this->cts, is_null($uid) ? null : 'user_' . $uid); | |
992 | } | |
993 | ||
994 | private function contactJoins() | |
995 | { | |
996 | $joins = array(); | |
997 | foreach ($this->cts as $sub=>$key) { | |
998 | if (is_null($key)) { | |
999 | $joins['c' . $sub] = array('left', 'contacts', '$ME.contact = $UID'); | |
1000 | } else { | |
1001 | $joins['c' . $sub] = array('left', 'contacts', XDB::format('$ME.uid = {?} AND $ME.contact = $UID', substr($key, 5))); | |
1002 | } | |
1003 | } | |
1004 | return $joins; | |
1005 | } | |
a087cc8d FB |
1006 | } |
1007 | ||
3f42a6ad | 1008 | |
a087cc8d FB |
1009 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
1010 | ?> |