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 | ||
d865c296 FB |
334 | |
335 | ||
336 | /****************** | |
337 | * ORDERS | |
338 | ******************/ | |
339 | ||
340 | abstract class UserFilterOrder | |
341 | { | |
342 | protected $desc = false; | |
343 | ||
344 | public function buildSort(UserFilter &$uf) | |
345 | { | |
346 | $sel = $this->getSortTokens($uf); | |
347 | if (!is_array($sel)) { | |
348 | $sel = array($sel); | |
349 | } | |
350 | if ($this->desc) { | |
351 | foreach ($sel as $k=>$s) { | |
352 | $sel[$k] = $s . ' DESC'; | |
353 | } | |
354 | } | |
355 | return $sel; | |
356 | } | |
357 | ||
358 | abstract protected function getSortTokens(UserFilter &$uf); | |
359 | } | |
360 | ||
361 | class UFO_Promo extends UserFilterOrder | |
362 | { | |
363 | private $grade; | |
364 | ||
365 | public function __construct($grade = null, $desc = false) | |
366 | { | |
367 | $this->grade = $grade; | |
368 | $this->desc = $desc; | |
369 | } | |
370 | ||
371 | protected function getSortTokens(UserFilter &$uf) | |
372 | { | |
373 | if (UserFilter::isGrade($this->grade)) { | |
374 | $sub = $uf->addEducationFilter($this->grade); | |
375 | return 'pe' . $sub . '.' . UserFilter::promoYear($this->grade); | |
376 | } else { | |
377 | $sub = $uf->addDisplayFilter(); | |
378 | return 'pd' . $sub . '.promo'; | |
379 | } | |
380 | } | |
381 | } | |
382 | ||
383 | class UFO_Name extends UserFilterOrder | |
384 | { | |
385 | private $type; | |
386 | private $variant; | |
387 | private $particle; | |
388 | ||
389 | public function __construct($type, $variant = null, $particle = false, $desc = false) | |
390 | { | |
391 | $this->type = $type; | |
392 | $this->variant = $variant; | |
393 | $this->particle = $particle; | |
394 | $this->desc = $desc; | |
395 | } | |
396 | ||
397 | protected function getSortTokens(UserFilter &$uf) | |
398 | { | |
399 | if (UserFilter::isDisplayName($this->type)) { | |
400 | $sub = $uf->addDisplayFilter(); | |
401 | return 'pd' . $sub . '.' . $this->type; | |
402 | } else { | |
403 | $sub = $uf->addNameFilter($this->type, $this->variant); | |
404 | if ($this->particle) { | |
405 | return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)'; | |
406 | } else { | |
407 | return 'pn' . $sub . '.name'; | |
408 | } | |
409 | } | |
410 | } | |
411 | } | |
412 | ||
38c6fe96 FB |
413 | class UFO_Registration extends UserFilterOrder |
414 | { | |
415 | public function __construct($desc = false) | |
416 | { | |
417 | $this->desc = $desc; | |
418 | } | |
419 | ||
420 | protected function getSortTokens(UserFilter &$uf) | |
421 | { | |
422 | return 'a.registration_date'; | |
423 | } | |
424 | } | |
425 | ||
d865c296 FB |
426 | /*********************************** |
427 | ********************************* | |
428 | USER FILTER CLASS | |
429 | ********************************* | |
430 | ***********************************/ | |
431 | ||
a087cc8d FB |
432 | class UserFilter |
433 | { | |
d865c296 FB |
434 | static private $joinMethods = array(); |
435 | ||
a087cc8d | 436 | private $root; |
24e08e33 | 437 | private $sort = array(); |
784745ce | 438 | private $query = null; |
24e08e33 | 439 | private $orderby = null; |
784745ce | 440 | |
d865c296 FB |
441 | private $lastcount = 0; |
442 | ||
24e08e33 | 443 | public function __construct($cond = null, $sort = null) |
5dd9d823 | 444 | { |
d865c296 FB |
445 | if (empty(self::$joinMethods)) { |
446 | $class = new ReflectionClass('UserFilter'); | |
447 | foreach ($class->getMethods() as $method) { | |
448 | $name = $method->getName(); | |
449 | if (substr($name, -5) == 'Joins' && $name != 'buildJoins') { | |
450 | self::$joinMethods[] = $name; | |
451 | } | |
452 | } | |
453 | } | |
5dd9d823 FB |
454 | if (!is_null($cond)) { |
455 | if ($cond instanceof UserFilterCondition) { | |
456 | $this->setCondition($cond); | |
457 | } | |
458 | } | |
24e08e33 FB |
459 | if (!is_null($sort)) { |
460 | if ($sort instanceof UserFilterOrder) { | |
461 | $this->addSort($sort); | |
d865c296 FB |
462 | } else if (is_array($sort)) { |
463 | foreach ($sort as $s) { | |
464 | $this->addSort($s); | |
465 | } | |
24e08e33 FB |
466 | } |
467 | } | |
5dd9d823 FB |
468 | } |
469 | ||
784745ce FB |
470 | private function buildQuery() |
471 | { | |
d865c296 FB |
472 | if (is_null($this->orderby)) { |
473 | $orders = array(); | |
474 | foreach ($this->sort as $sort) { | |
475 | $orders = array_merge($orders, $sort->buildSort($this)); | |
476 | } | |
477 | if (count($orders) == 0) { | |
478 | $this->orderby = ''; | |
479 | } else { | |
480 | $this->orderby = 'ORDER BY ' . implode(', ', $orders); | |
481 | } | |
482 | } | |
784745ce FB |
483 | if (is_null($this->query)) { |
484 | $where = $this->root->buildCondition($this); | |
485 | $joins = $this->buildJoins(); | |
486 | $this->query = 'FROM accounts AS a | |
487 | INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms)) | |
488 | INNER JOIN profiles AS p ON (p.pid = ap.pid) | |
489 | ' . $joins . ' | |
490 | WHERE (' . $where . ')'; | |
491 | } | |
492 | } | |
493 | ||
494 | private function formatJoin(array $joins) | |
495 | { | |
496 | $str = ''; | |
497 | foreach ($joins as $key => $infos) { | |
498 | $mode = $infos[0]; | |
499 | $table = $infos[1]; | |
500 | if ($mode == 'inner') { | |
501 | $str .= 'INNER JOIN '; | |
502 | } else if ($mode == 'left') { | |
503 | $str .= 'LEFT JOIN '; | |
504 | } else { | |
505 | Platal::page()->kill("Join mode error"); | |
506 | } | |
507 | $str .= $table . ' AS ' . $key; | |
508 | if (isset($infos[2])) { | |
4927ee54 | 509 | $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')'; |
784745ce FB |
510 | } |
511 | $str .= "\n"; | |
512 | } | |
513 | return $str; | |
514 | } | |
515 | ||
516 | private function buildJoins() | |
517 | { | |
d865c296 FB |
518 | $joins = array(); |
519 | foreach (self::$joinMethods as $method) { | |
520 | $joins = array_merge($joins, $this->$method()); | |
521 | } | |
784745ce FB |
522 | return $this->formatJoin($joins); |
523 | } | |
a087cc8d | 524 | |
d865c296 FB |
525 | private function getUIDList($uids = null, $count = null, $offset = null) |
526 | { | |
527 | $this->buildQuery(); | |
528 | $limit = ''; | |
529 | if (!is_null($count)) { | |
530 | if (!is_null($offset)) { | |
531 | $limit = XDB::format('LIMIT {?}, {?}', $offset, $count); | |
532 | } else { | |
533 | $limit = XDB::format('LIMIT {?}', $count); | |
534 | } | |
535 | } | |
536 | $cond = ''; | |
537 | if (!is_null($uids)) { | |
538 | $cond = ' AND a.uid IN (' . implode(', ', $uids) . ')'; | |
539 | } | |
540 | $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid | |
541 | ' . $this->query . $cond . ' | |
542 | GROUP BY a.uid | |
543 | ' . $this->orderby . ' | |
544 | ' . $limit); | |
545 | $this->lastcount = (int)XDB::fetchOneCell('SELECT FOUND_ROWS()'); | |
546 | return $fetched; | |
547 | } | |
548 | ||
a087cc8d FB |
549 | /** Check that the user match the given rule. |
550 | */ | |
551 | public function checkUser(PlUser &$user) | |
552 | { | |
784745ce FB |
553 | $this->buildQuery(); |
554 | $count = (int)XDB::fetchOneCell('SELECT COUNT(*) | |
555 | ' . $this->query . XDB::format(' AND a.uid = {?}', $user->id())); | |
556 | return $count == 1; | |
a087cc8d FB |
557 | } |
558 | ||
559 | /** Filter a list of user to extract the users matching the rule. | |
560 | */ | |
d865c296 | 561 | public function filter(array $users, $count = null, $offset = null) |
a087cc8d | 562 | { |
4927ee54 FB |
563 | $this->buildQuery(); |
564 | $table = array(); | |
565 | $uids = array(); | |
566 | foreach ($users as $user) { | |
567 | $uids[] = $user->id(); | |
568 | $table[$user->id()] = $user; | |
569 | } | |
d865c296 | 570 | $fetched = $this->getUIDList($uids, $count, $offset); |
a087cc8d | 571 | $output = array(); |
4927ee54 FB |
572 | foreach ($fetched as $uid) { |
573 | $output[] = $table[$uid]; | |
a087cc8d FB |
574 | } |
575 | return $output; | |
576 | } | |
577 | ||
d865c296 | 578 | public function getUIDs($count = null, $offset = null) |
4927ee54 | 579 | { |
d865c296 FB |
580 | return $this->getUIDList(null, $count, $offset); |
581 | } | |
582 | ||
583 | public function getUsers($count = null, $offset = null) | |
584 | { | |
585 | return User::getBulkUsersWithUIDs($this->getUIDs($count, $offset)); | |
4927ee54 FB |
586 | } |
587 | ||
d865c296 | 588 | public function getTotalCount() |
4927ee54 | 589 | { |
38c6fe96 FB |
590 | if (is_null($this->lastcount)) { |
591 | return (int)XDB::fetchOneCell('SELECT COUNT(*) | |
592 | ' . $this->query . ' | |
593 | GROUP BY a.uid'); | |
594 | } else { | |
595 | return $this->lastcount; | |
596 | } | |
4927ee54 FB |
597 | } |
598 | ||
a087cc8d FB |
599 | public function setCondition(UserFilterCondition &$cond) |
600 | { | |
601 | $this->root =& $cond; | |
784745ce | 602 | $this->query = null; |
a087cc8d FB |
603 | } |
604 | ||
24e08e33 FB |
605 | public function addSort(UserFilterOrder &$sort) |
606 | { | |
d865c296 FB |
607 | $this->sort[] = $sort; |
608 | $this->orderby = null; | |
24e08e33 FB |
609 | } |
610 | ||
a087cc8d FB |
611 | static public function getLegacy($promo_min, $promo_max) |
612 | { | |
a087cc8d | 613 | if ($promo_min != 0) { |
784745ce | 614 | $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min)); |
5dd9d823 FB |
615 | } else { |
616 | $min = new UFC_True(); | |
a087cc8d | 617 | } |
a087cc8d | 618 | if ($promo_max != 0) { |
784745ce | 619 | $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max)); |
a087cc8d | 620 | } else { |
5dd9d823 | 621 | $max = new UFC_True(); |
a087cc8d | 622 | } |
5dd9d823 | 623 | return new UserFilter(new UFC_And($min, $max)); |
a087cc8d | 624 | } |
784745ce FB |
625 | |
626 | ||
d865c296 FB |
627 | /** DISPLAY |
628 | */ | |
38c6fe96 | 629 | const DISPLAY = 'display'; |
d865c296 FB |
630 | private $pd = false; |
631 | public function addDisplayFilter() | |
632 | { | |
633 | $this->pd = true; | |
634 | return ''; | |
635 | } | |
636 | ||
637 | private function displayJoins() | |
638 | { | |
639 | if ($this->pd) { | |
640 | return array('pd' => array('left', 'profile_display', '$ME.pid = $PID')); | |
641 | } else { | |
642 | return array(); | |
643 | } | |
644 | } | |
645 | ||
784745ce FB |
646 | /** NAMES |
647 | */ | |
d865c296 | 648 | /* name tokens */ |
784745ce FB |
649 | const LASTNAME = 'lastname'; |
650 | const FIRSTNAME = 'firstname'; | |
651 | const NICKNAME = 'nickname'; | |
652 | const PSEUDONYM = 'pseudonym'; | |
653 | const NAME = 'name'; | |
d865c296 | 654 | /* name variants */ |
784745ce FB |
655 | const VN_MARITAL = 'marital'; |
656 | const VN_ORDINARY = 'ordinary'; | |
657 | const VN_OTHER = 'other'; | |
658 | const VN_INI = 'ini'; | |
d865c296 FB |
659 | /* display names */ |
660 | const DN_FULL = 'directory_name'; | |
661 | const DN_DISPLAY = 'yourself'; | |
662 | const DN_YOURSELF = 'yourself'; | |
663 | const DN_DIRECTORY = 'directory_name'; | |
664 | const DN_PRIVATE = 'private_name'; | |
665 | const DN_PUBLIC = 'public_name'; | |
666 | const DN_SHORT = 'short_name'; | |
667 | const DN_SORT = 'sort_name'; | |
784745ce FB |
668 | |
669 | static public $name_variants = array( | |
670 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), | |
d865c296 FB |
671 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) |
672 | ); | |
784745ce FB |
673 | |
674 | static public function assertName($name) | |
675 | { | |
676 | if (!Profile::getNameTypeId($name)) { | |
677 | Platal::page()->kill('Invalid name type'); | |
678 | } | |
679 | } | |
680 | ||
d865c296 FB |
681 | static public function isDisplayName($name) |
682 | { | |
683 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
684 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
685 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
686 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
687 | } | |
688 | ||
784745ce FB |
689 | private $pn = array(); |
690 | private $pno = 0; | |
691 | public function addNameFilter($type, $variant = null) | |
692 | { | |
693 | if (!is_null($variant)) { | |
694 | $ft = $type . '_' . $variant; | |
695 | } else { | |
696 | $ft = $type; | |
697 | } | |
698 | $sub = '_' . $ft; | |
699 | self::assertName($ft); | |
700 | ||
701 | if (!is_null($variant) && $variant == 'other') { | |
702 | $sub .= $this->pno++; | |
703 | } | |
704 | $this->pn[$sub] = Profile::getNameTypeId($ft); | |
705 | return $sub; | |
706 | } | |
707 | ||
708 | private function nameJoins() | |
709 | { | |
710 | $joins = array(); | |
711 | foreach ($this->pn as $sub => $type) { | |
712 | $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type); | |
713 | } | |
714 | return $joins; | |
715 | } | |
716 | ||
717 | ||
718 | /** EDUCATION | |
719 | */ | |
720 | const GRADE_ING = 'Ing.'; | |
721 | const GRADE_PHD = 'PhD'; | |
722 | const GRADE_MST = 'M%'; | |
723 | static public function isGrade($grade) | |
724 | { | |
38c6fe96 | 725 | return $grade == self::GRADE_ING || $grade == self::GRADE_PHD || $grade == self::GRADE_MST; |
784745ce FB |
726 | } |
727 | ||
728 | static public function assertGrade($grade) | |
729 | { | |
730 | if (!self::isGrade($grade)) { | |
731 | Platal::page()->killError("DiplĂ´me non valide"); | |
732 | } | |
733 | } | |
734 | ||
d865c296 FB |
735 | static public function promoYear($grade) |
736 | { | |
737 | // XXX: Definition of promotion for phds and masters might change in near future. | |
738 | return ($grade == UserFilter::GRADE_ING) ? 'entry_year' : 'grad_year'; | |
739 | } | |
740 | ||
784745ce FB |
741 | private $pepe = array(); |
742 | private $with_pee = false; | |
743 | private $pe_g = 0; | |
744 | public function addEducationFilter($x = false, $grade = null) | |
745 | { | |
746 | if (!$x) { | |
747 | $index = $this->pe_g; | |
748 | $sub = $this->pe_g++; | |
749 | } else { | |
750 | self::assertGrade($grade); | |
751 | $index = $grade; | |
752 | $sub = $grade[0]; | |
753 | $this->with_pee = true; | |
754 | } | |
755 | $sub = '_' . $sub; | |
756 | $this->pepe[$index] = $sub; | |
757 | return $sub; | |
758 | } | |
759 | ||
760 | private function educationJoins() | |
761 | { | |
762 | $joins = array(); | |
763 | if ($this->with_pee) { | |
764 | $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\''); | |
765 | } | |
766 | foreach ($this->pepe as $grade => $sub) { | |
767 | if ($this->isGrade($grade)) { | |
768 | $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID'); | |
769 | $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' . | |
770 | XDB::format('{?}', $grade)); | |
771 | } else { | |
772 | $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID'); | |
773 | $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid'); | |
774 | $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid'); | |
775 | } | |
776 | } | |
777 | return $joins; | |
778 | } | |
4927ee54 FB |
779 | |
780 | ||
781 | /** GROUPS | |
782 | */ | |
783 | private $gpm = array(); | |
784 | private $gpm_o = 0; | |
785 | public function addGroupFilter($group = null) | |
786 | { | |
787 | if (!is_null($group)) { | |
788 | if (ctype_digit($group)) { | |
789 | $index = $sub = $group; | |
790 | } else { | |
791 | $index = $group; | |
792 | $sub = preg_replace('/[^a-z0-9]/i', '', $group); | |
793 | } | |
794 | } else { | |
795 | $sub = 'group_' . $this->gpm_o++; | |
796 | $index = null; | |
797 | } | |
798 | $sub = '_' . $sub; | |
799 | $this->gpm[$sub] = $index; | |
800 | return $sub; | |
801 | } | |
802 | ||
803 | private function groupJoins() | |
804 | { | |
805 | $joins = array(); | |
806 | foreach ($this->gpm as $sub => $key) { | |
807 | if (is_null($key)) { | |
808 | $joins['gpa' . $sub] = array('inner', 'groupex.asso'); | |
809 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
810 | } else if (ctype_digit($key)) { | |
811 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = ' . $key); | |
812 | } else { | |
813 | $joins['gpa' . $sub] = array('inner', 'groupex.asso', XDB::format('$ME.diminutif = {?}', $key)); | |
814 | $joins['gpm' . $sub] = array('left', 'groupex.membres', '$ME.uid = $UID AND $ME.asso_id = gpa' . $sub . '.id'); | |
815 | } | |
816 | } | |
817 | return $joins; | |
818 | } | |
a087cc8d FB |
819 | } |
820 | ||
821 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
822 | ?> |