2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
27 interface UserFilterCondition
29 const COND_TRUE
= 'TRUE';
30 const COND_FALSE
= 'FALSE';
32 /** Check that the given user matches the rule.
34 public function buildCondition(UserFilter
&$uf);
37 abstract class UFC_OneChild
implements UserFilterCondition
41 public function __construct($child = null
)
43 if (!is_null($child) && ($child instanceof UserFilterCondition
)) {
44 $this->setChild($child);
48 public function setChild(UserFilterCondition
&$cond)
50 $this->child
=& $cond;
54 abstract class UFC_NChildren
implements UserFilterCondition
56 protected $children = array();
58 public function __construct()
60 $children = func_get_args();
61 foreach ($children as &$child) {
62 if (!is_null($child) && ($child instanceof UserFilterCondition
)) {
63 $this->addChild($child);
68 public function addChild(UserFilterCondition
&$cond)
70 $this->children
[] =& $cond;
73 protected function catConds(array $cond, $op, $fallback)
75 if (count($cond) == 0) {
77 } else if (count($cond) == 1) {
80 return '(' . implode(') ' . $op . ' (', $cond) . ')';
85 class UFC_True
implements UserFilterCondition
87 public function buildCondition(UserFilter
&$uf)
89 return self
::COND_TRUE
;
93 class UFC_False
implements UserFilterCondition
95 public function buildCondition(UserFilter
&$uf)
97 return self
::COND_FALSE
;
101 class UFC_Not
extends UFC_OneChild
103 public function buildCondition(UserFilter
&$uf)
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
;
111 return 'NOT (' . $val . ')';
116 class UFC_And
extends UFC_NChildren
118 public function buildCondition(UserFilter
&$uf)
120 if (empty($this->children
)) {
121 return self
::COND_FALSE
;
123 $true = self
::COND_FALSE
;
125 foreach ($this->children
as &$child) {
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
;
135 return $this->catConds($conds, 'AND', $true);
140 class UFC_Or
extends UFC_NChildren
142 public function buildCondition(UserFilter
&$uf)
144 if (empty($this->children
)) {
145 return self
::COND_TRUE
;
147 $true = self
::COND_TRUE
;
149 foreach ($this->children
as &$child) {
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
;
159 return $this->catConds($conds, 'OR', $true);
164 class UFC_Promo
implements UserFilterCondition
171 public function __construct($comparison, $grade, $promo)
173 $this->grade
= $grade;
174 $this->comparison
= $comparison;
175 $this->promo
= $promo;
176 if ($this->grade
!= UserFilter
::DISPLAY
) {
177 UserFilter
::assertGrade($this->grade
);
181 public function buildCondition(UserFilter
&$uf)
183 if ($this->grade
== UserFilter
::DISPLAY
) {
184 $sub = $uf->addDisplayFilter();
185 return XDB
::format('pd' . $sub . '.promo = {?}', $this->promo
);
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
);
194 class UFC_Name
implements UserFilterCondition
206 public function __construct($type, $text, $mode)
213 private function buildNameQuery($type, $variant, $where, UserFilter
&$uf)
215 $sub = $uf->addNameFilter($type, $variant);
216 return str_replace('$ME', 'pn' . $sub, $where);
219 public function buildCondition(UserFilter
&$uf)
223 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
224 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
226 if (($this->mode
& self
::CONTAINS
) == 0) {
227 $right = XDB
::format('{?}', $this->text
);
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
);
234 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->text
);
236 $cond = $left . $op . $right;
237 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
238 if (($this->mode
& self
::VARIANTS
) != 0 && isset(UserFilter
::$name_variants[$this->type
])) {
239 foreach (UserFilter
::$name_variants[$this->type
] as $var) {
240 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
243 return implode(' OR ', $conds);
247 class UFC_Dead
implements UserFilterCondition
252 public function __construct($comparison = null
, $date = null
)
254 $this->comparison
= $comparison;
258 public function buildCondition(UserFilter
&$uf)
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
));
268 class UFC_Registered
implements UserFilterCondition
274 public function __construct($active = false
, $comparison = null
, $date = null
)
276 $this->only_active
= $active;
277 $this->comparison
= $comparison;
281 public function buildCondition(UserFilter
&$uf)
284 $date = 'a.uid IS NOT NULL AND a.state = \'active\'';
286 $date = 'a.uid IS NOT NULL AND a.state != \'pending\'';
288 if (!is_null($this->comparison
)) {
289 $date .= ' AND a.registration_date ' . $this->comparison
. ' ' . XDB
::format('{?}', date('Y-m-d', $this->date
));
295 class UFC_Sex
implements UserFilterCondition
298 public function __construct($sex)
303 public function buildCondition(UserFilter
&$uf)
305 if ($this->sex
!= User
::GENDER_MALE
&& $this->sex
!= User
::GENDER_FEMALE
) {
306 return self
::COND_FALSE
;
308 return XDB
::format('p.sex = {?}', $this->sex
== User
::GENDER_FEMALE ?
'female' : 'male');
313 class UFC_Group
implements UserFilterCondition
317 public function __construct($group, $admin = false
)
319 $this->group
= $group;
320 $this->admin
= $admin;
323 public function buildCondition(UserFilter
&$uf)
325 $sub = $uf->addGroupFilter($this->group
);
326 $where = 'gpm' . $sub . '.perms IS NOT NULL';
328 $where .= ' AND gpm' . $sub . '.perms = \'admin\'';
340 abstract class UserFilterOrder
342 protected $desc = false
;
344 public function buildSort(UserFilter
&$uf)
346 $sel = $this->getSortTokens($uf);
347 if (!is_array($sel)) {
351 foreach ($sel as $k=>$s) {
352 $sel[$k] = $s . ' DESC';
358 abstract protected function getSortTokens(UserFilter
&$uf);
361 class UFO_Promo
extends UserFilterOrder
365 public function __construct($grade = null
, $desc = false
)
367 $this->grade
= $grade;
371 protected function getSortTokens(UserFilter
&$uf)
373 if (UserFilter
::isGrade($this->grade
)) {
374 $sub = $uf->addEducationFilter($this->grade
);
375 return 'pe' . $sub . '.' . UserFilter
::promoYear($this->grade
);
377 $sub = $uf->addDisplayFilter();
378 return 'pd' . $sub . '.promo';
383 class UFO_Name
extends UserFilterOrder
389 public function __construct($type, $variant = null
, $particle = false
, $desc = false
)
392 $this->variant
= $variant;
393 $this->particle
= $particle;
397 protected function getSortTokens(UserFilter
&$uf)
399 if (UserFilter
::isDisplayName($this->type
)) {
400 $sub = $uf->addDisplayFilter();
401 return 'pd' . $sub . '.' . $this->type
;
403 $sub = $uf->addNameFilter($this->type
, $this->variant
);
404 if ($this->particle
) {
405 return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
407 return 'pn' . $sub . '.name';
413 class UFO_Registration
extends UserFilterOrder
415 public function __construct($desc = false
)
420 protected function getSortTokens(UserFilter
&$uf)
422 return 'a.registration_date';
426 /***********************************
427 *********************************
429 *********************************
430 ***********************************/
434 static private $joinMethods = array();
437 private $sort = array();
438 private $query = null
;
439 private $orderby = null
;
441 private $lastcount = 0;
443 public function __construct($cond = null
, $sort = null
)
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;
454 if (!is_null($cond)) {
455 if ($cond instanceof UserFilterCondition
) {
456 $this->setCondition($cond);
459 if (!is_null($sort)) {
460 if ($sort instanceof UserFilterOrder
) {
461 $this->addSort($sort);
462 } else if (is_array($sort)) {
463 foreach ($sort as $s) {
470 private function buildQuery()
472 if (is_null($this->orderby
)) {
474 foreach ($this->sort
as $sort) {
475 $orders = array_merge($orders, $sort->buildSort($this));
477 if (count($orders) == 0) {
480 $this->orderby
= 'ORDER BY ' . implode(', ', $orders);
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)
490 WHERE (' . $where . ')';
494 private function formatJoin(array $joins)
497 foreach ($joins as $key => $infos) {
500 if ($mode == 'inner') {
501 $str .= 'INNER JOIN ';
502 } else if ($mode == 'left') {
503 $str .= 'LEFT JOIN ';
505 Platal
::page()->kill("Join mode error");
507 $str .= $table . ' AS ' . $key;
508 if (isset($infos[2])) {
509 $str .= ' ON (' . str_replace(array('$ME', '$PID', '$UID'), array($key, 'p.pid', 'a.uid'), $infos[2]) . ')';
516 private function buildJoins()
519 foreach (self
::$joinMethods as $method) {
520 $joins = array_merge($joins, $this->$method());
522 return $this->formatJoin($joins);
525 private function getUIDList($uids = null
, $count = null
, $offset = null
)
529 if (!is_null($count)) {
530 if (!is_null($offset)) {
531 $limit = XDB
::format('LIMIT {?}, {?}', $offset, $count);
533 $limit = XDB
::format('LIMIT {?}', $count);
537 if (!is_null($uids)) {
538 $cond = ' AND a.uid IN (' . implode(', ', $uids) . ')';
540 $fetched = XDB
::fetchColumn('SELECT SQL_CALC_FOUND_ROWS a.uid
541 ' . $this->query
. $cond . '
543 ' . $this->orderby
. '
545 $this->lastcount
= (int)XDB
::fetchOneCell('SELECT FOUND_ROWS()');
549 /** Check that the user match the given rule.
551 public function checkUser(PlUser
&$user)
554 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
555 ' . $this->query
. XDB
::format(' AND a.uid = {?}', $user->id()));
559 /** Filter a list of user to extract the users matching the rule.
561 public function filter(array $users, $count = null
, $offset = null
)
566 foreach ($users as $user) {
567 $uids[] = $user->id();
568 $table[$user->id()] = $user;
570 $fetched = $this->getUIDList($uids, $count, $offset);
572 foreach ($fetched as $uid) {
573 $output[] = $table[$uid];
578 public function getUIDs($count = null
, $offset = null
)
580 return $this->getUIDList(null
, $count, $offset);
583 public function getUsers($count = null
, $offset = null
)
585 return User
::getBulkUsersWithUIDs($this->getUIDs($count, $offset));
588 public function getTotalCount()
590 if (is_null($this->lastcount
)) {
591 return (int)XDB
::fetchOneCell('SELECT COUNT(*)
595 return $this->lastcount
;
599 public function setCondition(UserFilterCondition
&$cond)
601 $this->root
=& $cond;
605 public function addSort(UserFilterOrder
&$sort)
607 $this->sort
[] = $sort;
608 $this->orderby
= null
;
611 static public function getLegacy($promo_min, $promo_max)
613 if ($promo_min != 0) {
614 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
616 $min = new UFC_True();
618 if ($promo_max != 0) {
619 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
621 $max = new UFC_True();
623 return new UserFilter(new UFC_And($min, $max));
629 const DISPLAY
= 'display';
631 public function addDisplayFilter()
637 private function displayJoins()
640 return array('pd' => array('left', 'profile_display', '$ME.pid = $PID'));
649 const LASTNAME
= 'lastname';
650 const FIRSTNAME
= 'firstname';
651 const NICKNAME
= 'nickname';
652 const PSEUDONYM
= 'pseudonym';
655 const VN_MARITAL
= 'marital';
656 const VN_ORDINARY
= 'ordinary';
657 const VN_OTHER
= 'other';
658 const VN_INI
= 'ini';
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';
669 static public $name_variants = array(
670 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
671 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
674 static public function assertName($name)
676 if (!Profile
::getNameTypeId($name)) {
677 Platal
::page()->kill('Invalid name type');
681 static public function isDisplayName($name)
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
;
689 private $pn = array();
691 public function addNameFilter($type, $variant = null
)
693 if (!is_null($variant)) {
694 $ft = $type . '_' . $variant;
699 self
::assertName($ft);
701 if (!is_null($variant) && $variant == 'other') {
702 $sub .= $this->pno++
;
704 $this->pn
[$sub] = Profile
::getNameTypeId($ft);
708 private function nameJoins()
711 foreach ($this->pn
as $sub => $type) {
712 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
720 const GRADE_ING
= 'Ing.';
721 const GRADE_PHD
= 'PhD';
722 const GRADE_MST
= 'M%';
723 static public function isGrade($grade)
725 return $grade == self
::GRADE_ING ||
$grade == self
::GRADE_PHD ||
$grade == self
::GRADE_MST
;
728 static public function assertGrade($grade)
730 if (!self
::isGrade($grade)) {
731 Platal
::page()->killError("DiplĂ´me non valide");
735 static public function promoYear($grade)
737 // XXX: Definition of promotion for phds and masters might change in near future.
738 return ($grade == UserFilter
::GRADE_ING
) ?
'entry_year' : 'grad_year';
741 private $pepe = array();
742 private $with_pee = false
;
744 public function addEducationFilter($x = false
, $grade = null
)
747 $index = $this->pe_g
;
748 $sub = $this->pe_g++
;
750 self
::assertGrade($grade);
753 $this->with_pee
= true
;
756 $this->pepe
[$index] = $sub;
760 private function educationJoins()
763 if ($this->with_pee
) {
764 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
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));
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');
783 private $gpm = array();
785 public function addGroupFilter($group = null
)
787 if (!is_null($group)) {
788 if (ctype_digit($group)) {
789 $index = $sub = $group;
792 $sub = preg_replace('/[^a-z0-9]/i', '', $group);
795 $sub = 'group_' . $this->gpm_o++
;
799 $this->gpm
[$sub] = $index;
803 private function groupJoins()
806 foreach ($this->gpm
as $sub => $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);
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');
821 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: