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 ***************************************************************************/
22 interface UserFilterCondition
24 const COND_TRUE
= 'TRUE';
25 const COND_FALSE
= 'FALSE';
27 /** Check that the given user matches the rule.
29 public function buildCondition(UserFilter
&$uf);
32 abstract class UFC_OneChild
implements UserFilterCondition
36 public function __construct($child = null
)
38 if (!is_null($child) && ($child instanceof UserFilterCondition
)) {
39 $this->setChild($child);
43 public function setChild(UserFilterCondition
&$cond)
45 $this->child
=& $cond;
49 abstract class UFC_NChildren
implements UserFilterCondition
51 protected $children = array();
53 public function __construct()
55 $children = func_get_args();
56 foreach ($children as &$child) {
57 if (!is_null($child) && ($child instanceof UserFilterCondition
)) {
58 $this->addChild($child);
63 public function addChild(UserFilterCondition
&$cond)
65 $this->children
[] =& $cond;
68 protected function catConds(array $cond, $op, $fallback)
70 if (count($cond) == 0) {
72 } else if (count($cond) == 1) {
75 return '(' . implode(') ' . $op . ' (', $conds) . ')';
80 class UFC_True
implements UserFilterCondition
82 public function buildCondition(UserFilter
&$uf)
84 return self
::COND_TRUE
;
88 class UFC_False
implements UserFilterCondition
90 public function buildCondition(UserFilter
&$uf)
92 return self
::COND_FALSE
;
96 class UFC_Not
extends UFC_OneChild
98 public function buildCondition(UserFilter
&$uf)
100 $val = $this->child
->buildCondition($uf);
101 if ($val == self
::COND_TRUE
) {
102 return self
::COND_FALSE
;
103 } else if ($val == self
::COND_FALSE
) {
104 return self
::COND_TRUE
;
106 return 'NOT (' . $val . ')';
111 class UFC_And
extends UFC_NChildren
113 public function buildCondition(UserFilter
&$uf)
115 if (empty($this->children
)) {
116 return self
::COND_FALSE
;
118 $true = self
::COND_FALSE
;
120 foreach ($this->children
as &$child) {
121 $val = $child->buildCondition($uf);
122 if ($val == self
::COND_TRUE
) {
123 $true = self
::COND_TRUE
;
124 } else if ($val == self
::COND_FALSE
) {
125 return self
::COND_FALSE
;
130 return $this->catConds($conds, 'AND', $true);
135 class UFC_Or
extends UFC_NChildren
137 public function buildCondition(UserFilter
&$uf)
139 if (empty($this->children
)) {
140 return self
::COND_TRUE
;
142 $true = self
::COND_TRUE
;
144 foreach ($this->children
as &$child) {
145 $val = $child->buildCondition($uf);
146 if ($val == self
::COND_TRUE
) {
147 return self
::COND_TRUE
;
148 } else if ($val == self
::COND_FALSE
) {
149 $true = self
::COND_FALSE
;
154 return $this->catConds($conds, 'OR', $true);
159 class UFC_Promo
implements UserFilterCondition
166 public function __construct($comparison, $grade, $promo)
168 $this->grade
= $grade;
169 $this->comparison
= $comparison;
170 $this->promo
= $promo;
171 UserFilter
::assertGrade($this->grade
);
174 public function buildCondition(UserFilter
&$uf)
176 // XXX: Definition of promotion for phds and masters might change in near future.
177 if ($this->grade
== UserFilter
::GRADE_ING
) {
178 $promo_year = 'entry_year';
180 $promo_year = 'grad_year';
182 $sub = $uf->addEducationFilter(true
, $this->grade
);
183 $field = 'pe' . $sub . '.' . $promo_year;
184 return $field . ' IS NOT NULL AND ' . $field . ' ' . $this->comparison
. ' ' . XDB
::format('{?}', $this->promo
);
188 class UFC_Name
implements UserFilterCondition
200 public function __construct($type, $text, $mode)
207 private function buildNameQuery($type, $variant, $where, UserFilter
&$uf)
209 $sub = $uf->addNameFilter($type, $variant);
210 return str_replace('$ME', 'pn' . $sub, $where);
213 public function buildCondition(UserFilter
&$uf)
217 if (($this->mode
& self
::PARTICLE
) == self
::PARTICLE
) {
218 $left = 'CONCAT($ME.particle, \' \', $ME.name)';
220 if (($this->mode
& self
::CONTAINS
) == 0) {
221 $right = XDB
::format('{?}', $this->text
);
223 } else if (($this->mode
& self
::CONTAINS
) == self
::PREFIX
) {
224 $right = XDB
::format('CONCAT({?}, \'%\')', $this->text
);
225 } else if (($this->mode
& self
::CONTAINS
) == self
::SUFFIX
) {
226 $right = XDB
::format('CONCAT(\'%\', {?})', $this->text
);
228 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $this->text
);
230 $cond = $left . $op . $right;
231 $conds = array($this->buildNameQuery($this->type
, null
, $cond, $uf));
232 if (($this->mode
& self
::VARIANTS
) != 0) {
233 foreach (UserFilter
::$name_variants[$this->type
] as $var) {
234 $conds[] = $this->buildNameQuery($this->type
, $var, $cond, $uf);
237 return implode(' OR ', $conds);
244 private $query = null
;
246 public function __construct($cond = null
)
248 if (!is_null($cond)) {
249 if ($cond instanceof UserFilterCondition
) {
250 $this->setCondition($cond);
255 private function buildQuery()
257 if (is_null($this->query
)) {
258 $where = $this->root
->buildCondition($this);
259 $joins = $this->buildJoins();
260 $this->query
= 'FROM accounts AS a
261 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
262 INNER JOIN profiles AS p ON (p.pid = ap.pid)
264 WHERE (' . $where . ')';
268 private function formatJoin(array $joins)
271 foreach ($joins as $key => $infos) {
274 if ($mode == 'inner') {
275 $str .= 'INNER JOIN ';
276 } else if ($mode == 'left') {
277 $str .= 'LEFT JOIN ';
279 Platal
::page()->kill("Join mode error");
281 $str .= $table . ' AS ' . $key;
282 if (isset($infos[2])) {
283 $str .= ' ON (' . str_replace(array('$ME', '$PID'), array($key, 'p.pid'), $infos[2]) . ')';
290 private function buildJoins()
292 $joins = $this->educationJoins() +
$this->nameJoins();
293 return $this->formatJoin($joins);
296 /** Check that the user match the given rule.
298 public function checkUser(PlUser
&$user)
301 $count = (int)XDB
::fetchOneCell('SELECT COUNT(*)
302 ' . $this->query
. XDB
::format(' AND a.uid = {?}', $user->id()));
306 /** Filter a list of user to extract the users matching the rule.
308 public function filter(array $users)
311 foreach ($users as &$user) {
312 if ($this->checkUser($user)) {
319 public function setCondition(UserFilterCondition
&$cond)
321 $this->root
=& $cond;
325 static public function getLegacy($promo_min, $promo_max)
327 if ($promo_min != 0) {
328 $min = new UFC_Promo('>=', self
::GRADE_ING
, intval($promo_min));
330 $min = new UFC_True();
332 if ($promo_max != 0) {
333 $max = new UFC_Promo('<=', self
::GRADE_ING
, intval($promo_max));
335 $max = new UFC_True();
337 return new UserFilter(new UFC_And($min, $max));
343 const LASTNAME
= 'lastname';
344 const FIRSTNAME
= 'firstname';
345 const NICKNAME
= 'nickname';
346 const PSEUDONYM
= 'pseudonym';
348 const VN_MARITAL
= 'marital';
349 const VN_ORDINARY
= 'ordinary';
350 const VN_OTHER
= 'other';
351 const VN_INI
= 'ini';
353 static public $name_variants = array(
354 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
355 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
),
356 self
::NICKNAME
=> array(), self
::PSEUDONYM
=> array(),
357 self
::NAME
=> array());
359 static public function assertName($name)
361 if (!Profile
::getNameTypeId($name)) {
362 Platal
::page()->kill('Invalid name type');
366 private $pn = array();
368 public function addNameFilter($type, $variant = null
)
370 if (!is_null($variant)) {
371 $ft = $type . '_' . $variant;
376 self
::assertName($ft);
378 if (!is_null($variant) && $variant == 'other') {
379 $sub .= $this->pno++
;
381 $this->pn
[$sub] = Profile
::getNameTypeId($ft);
385 private function nameJoins()
388 foreach ($this->pn
as $sub => $type) {
389 $joins['pn' . $sub] = array('left', 'profile_name', '$ME.pid = $PID AND $ME.typeid = ' . $type);
397 const GRADE_ING
= 'Ing.';
398 const GRADE_PHD
= 'PhD';
399 const GRADE_MST
= 'M%';
400 static public function isGrade($grade)
402 return $grade == self
::GRADE_ING || self
::$grade == GRADE_PHD || self
::$grade == GRADE_MST
;
405 static public function assertGrade($grade)
407 if (!self
::isGrade($grade)) {
408 Platal
::page()->killError("DiplĂ´me non valide");
412 private $pepe = array();
413 private $with_pee = false
;
415 public function addEducationFilter($x = false
, $grade = null
)
418 $index = $this->pe_g
;
419 $sub = $this->pe_g++
;
421 self
::assertGrade($grade);
424 $this->with_pee
= true
;
427 $this->pepe
[$index] = $sub;
431 private function educationJoins()
434 if ($this->with_pee
) {
435 $joins['pee'] = array('inner', 'profile_education_enum', 'pee.abbreviation = \'X\'');
437 foreach ($this->pepe
as $grade => $sub) {
438 if ($this->isGrade($grade)) {
439 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
440 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
441 XDB
::format('{?}', $grade));
443 $joins['pe' . $sub] = array('left', 'profile_education', '$ME.uid = $PID');
444 $joins['pee' . $sub] = array('inner', 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
445 $joins['pede' . $sub] = array('inner', 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
452 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: