2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 ***************************************************************************/
23 /** This class stores all data for the different kinds of fields.
24 * It is only a dispatcher for the various DirEnum_XXX classes.
28 /** Name of availables Enumerations
29 * Each of these consts contains the basename of the class (its full name
30 * being DE_$basename).
32 const BINETS
= 'binets';
33 const SECTIONS
= 'sections';
34 const SCHOOLS
= 'schools';
35 const DEGREES
= 'degrees';
36 const STUDIESDOMAINS
= 'studiesdomains';
37 const NATIONALITIES
= 'nationalities';
38 const COUNTRIES
= 'countries';
39 const GROUPESX
= 'groupesx';
40 const SECTORS
= 'sectors';
41 const NETWORKS
= 'networking';
42 const ADMINAREAS
= 'adminareas';
43 const LOCALITIES
= 'localities'; // TODO
44 const COMPANIES
= 'companies'; // TODO
45 const JOBDESCRIPTION
= 'jobdescription'; // TODO
47 static private $enumerations = array();
49 static private function init($type)
51 $cls = "DE_" . ucfirst($type);
52 self
::$enumerations[$type] = new $cls();
55 /** Retrieves all options for a given type
56 * @param $type Type of enum for which options are requested
57 * @return XorgDbIterator over the results
58 * TODO : Find a way to get either an array, or the adequate PlIterator
60 static public function getOptions()
62 $args = func_get_args();
63 $type = array_shift($args);
64 if (!array_key_exists($type, self
::$enumerations)) {
67 $obj = self
::$enumerations[$type];
68 return call_user_func_array(array($obj, 'getOptions'), $args);
71 /** Retrieves all options with number of profiles for autocompletion
72 * @param $type Type of enum for which options are requested
73 * @param $text Text to autocomplete
74 * @return XorgDbIterator over the results
76 static public function getAutoComplete()
78 $args = func_get_args();
79 $type = array_shift($args);
80 if (!array_key_exists($type, self
::$enumerations)) {
83 $obj = self
::$enumerations[$type];
84 return call_user_func_array(array($obj, 'getAutoComplete'), $args);
87 /** Retrieves a list of IDs for a given type
88 * @param $type Type of enum for which IDs are requested
89 * @param $text Text to search in enum valuees
90 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
92 static public function getIDs()
94 $args = func_get_args();
95 $type = array_shift($args);
96 if (!array_key_exists($type, self
::$enumerations)) {
99 $obj = self
::$enumerations[$type];
100 return call_user_func_array(array($obj, 'getIDs'), $args);
105 // {{{ class DirEnumeration
106 abstract class DirEnumeration
108 /** Modes for LIKE searches
110 const MODE_EXACT
= 0x000;
111 const MODE_PREFIX
= 0x001;
112 const MODE_SUFFIX
= 0x002;
113 const MODE_CONTAINS
= 0x003;
115 /** An internal array of ID => optionTxt
117 protected $options = null
;
119 /** Description of the MySQL storage of the fields
121 protected $idfield = 'id';
122 protected $valfield = 'text';
123 protected $valfield2 = null
;
125 protected $join = '';
126 protected $where = '';
128 /** Fields for autocompletion
130 protected $ac_join = ''; // Additional joins
131 protected $ac_where = null
; // Additional where
132 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
133 protected $ac_unique; // Which field is to be taken as unique
134 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
135 protected $ac_withid = true
; // Do we want to fetch id too ?
137 protected function _fetchOptions()
139 if (is_null($this->options
)) {
140 $this->loadOptions();
144 public function getOptions()
146 $this->_fetchOptions();
147 return $this->options
;
150 // {{{ function getIDs
151 /** Retrieves possible IDs for given text
152 * @param $text Text to search for IDs
153 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
154 * @return An array of matching IDs ; if empty, input should be considered invalid
156 public function getIDs($text, $mode)
158 if ($mode == self
::MODE_EXACT
) {
159 $options = $this->getOptions();
160 return array_keys($options, $text);
162 if ($this->where
== null
) {
165 $where = $this->where
. ' AND ';
168 $conds[] = $this->valfield
. self
::makeSqlConcat($text, $mode);
169 if ($this->valfield2
!= null
) {
170 $conds[] = $this->valfield2
. self
::makeSqlConcat($text, $mode);
172 $where .= '(' . implode(' OR ', $conds) . ')';
174 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
175 FROM ' . $this->from
. '
178 GROUP BY ' . $this->idfield
);
183 private function mkTests($field, $text)
186 $tests[] = $field . self
::makeSqlConcat($text, self
::MODE_PREFIX
);
187 if (!$this->ac_beginwith
) {
188 $tests[] = $field . self
::makeSqlConcat(' ' . $text, self
::MODE_CONTAINS
);
189 $tests[] = $field . self
::makeSqlConcat('-' . $text, self
::MODE_CONTAINS
);
194 // {{{ function getAutoComplete
195 public function getAutoComplete($text)
197 $text = str_replace(array('%', '_'), '', $text);
199 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
202 $where = $this->ac_where
. ' AND ';
205 $tests = $this->mkTests($this->valfield
, $text);
206 if (!is_null($this->valfield2
)) {
207 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
210 $where .= '(' . implode(' OR ', $tests) . ')';
212 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
213 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
214 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
215 FROM ' . $this->from
. '
216 ' . $this->ac_join
. '
218 GROUP BY ' . $this->valfield
. '
219 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
224 // {{{ function makeSqlConcat
225 static protected function makeSqlConcat($text, $mode)
227 if ($mode == self
::MODE_EXACT
) {
228 return ' = ' . XDB
::format('{?}', $text);
230 if ($mode == self
::MODE_PREFIX
) {
231 $right = XDB
::format('CONCAT({?}, \'%\')', $text);
232 } else if ($mode == self
::MODE_SUFFIX
) {
233 $right = XDB
::format('CONCAT(\'%\', {?})', $text);
235 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $text);
237 return ' LIKE ' . $right;
241 // {{{ function loadOptions
242 /** The function used to load options
244 protected function loadOptions()
246 $this->options
= XDB
::iterator('SELECT ' . $this->valfield
. ' AS field,
247 ' . $this->idfield
. ' AS id
248 FROM ' . $this->from
. '
251 GROUP BY ' . $this->valfield
. '
252 ORDER BY ' . $this->valfield
);
258 // {{{ class DE_Binets
259 class DE_Binets
extends DirEnumeration
261 protected $from = 'binets_def';
263 protected $ac_join = 'INNER JOIN binets_ins ON (binets_def.id = binets_ins.binet_id)';
264 protected $ac_unique = 'binets_ins.user_id';
268 // {{{ class DE_Sections
269 class DE_Sections
extends DirEnumeration
271 protected $from = 'sections';
273 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = sections.id)';
274 protected $ac_unique = 'profiles.pid';
278 // {{{ class DE_Schools
279 class DE_Schools
extends DirEnumeration
281 protected $valfield = 'name';
282 protected $valfield2 = 'abbreviation';
283 protected $from = 'profile_education_enum';
285 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
286 protected $ac_unique = 'profile_education.uid';
290 // {{{ class DE_Degrees
291 class DE_Degrees
extends DirEnumeration
293 protected $from = 'profile_education_degree_enum';
294 protected $valfield = 'degree';
296 protected $suboptions = array();
298 protected function loadOptions()
300 $res = XDB
::query('SELECT ped.eduid, ped.degreeid, pede.degree
301 FROM profile_education_enum AS pee
302 LEFT JOIN profile_education_degree AS ped ON (pee.id = ped.eduid)
303 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
304 ORDER BY pede.degree');
305 foreach($res->fetchAllRow() as $row) {
306 list($eduid, $degreeid, $name) = $row;
307 $this->options
[$degreeid] = array('id' => $degreeid, 'field' => $name);
308 if (!array_key_exists($eduid, $this->suboptions
)) {
309 $this->suboptions
[$eduid] = array();
311 $this->suboptions
[$eduid][] = array('id' => $degreeid, 'field' => $name);
315 public function getOptions($eduid = null
)
317 $this->_fetchOptions();
318 if ($eduid == null
) {
319 return PlIteratorUtils
::fromArray($this->options
, 1, true
);
321 if (array_key_exists($eduid, $this->suboptions
)) {
322 return PlIteratorUtils
::fromArray($this->suboptions
[$eduid], 1, true
);
328 public function getIDs($text, $mode, $eduid = null
)
330 if ($eduid == null
) {
331 return XDB
::fetchColumn('SELECT id
332 FROM profile_education_degree_enum
333 WHERE degree ' . self
::makeSqlConcat($text, $mode));
335 return XDB
::fetchColumn('SELECT pede.id
336 FROM profile_education_degree AS ped
337 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
338 WHERE ped.eduid = {?} AND pede.degree ' . self
::makeSqlConcat($text, $mode), $eduid);
344 // {{{ class DE_StudiesSector
345 class DE_StudiesSector
extends DirEnumeration
347 protected $valfield = 'field';
348 protected $from = 'profile_education_field_enum';
350 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
351 protected $ac_unique = 'profile_education.uid';
355 // {{{ class DE_Nationalities
356 class DE_Nationalities
extends DirEnumeration
358 protected $idfield = 'iso_3166_1_a2';
359 protected $valfield = 'nationalityFR';
360 protected $valfield2 = 'nationality';
361 protected $from = 'geoloc_countries AS gc';
362 protected $join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
364 protected $ac_join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
365 protected $ac_unique = 'profiles.pid';
369 // {{{ class DE_Countries
370 class DE_Countries
extends DirEnumeration
372 protected $idfield = 'iso_3166_1_a2';
373 protected $valfield = 'countryFR';
374 protected $valfield2 = 'country';
375 protected $from = 'geoloc_countries';
377 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryFR';
378 protected $ac_unique = 'profile_addresses.pid';
382 // {{{ class DE_AdminAreas
383 class DE_AdminAreas
extends DirEnumeration
385 protected $suboptions = array();
387 protected function loadOptions()
389 $res = XDB
::query('SELECT id, name AS field, country
390 FROM geoloc_administrativeareas
393 foreach($res->fetchAllRow() as $row) {
394 list($id, $field, $country) = $row;
395 $this->options
[] = array('id' => $id, 'field' => $field);
396 if (!array_key_exists($country, $this->suboptions
)) {
397 $this->suboptions
[$country] = array();
399 $this->suboptions
[$country][] = array('id' => $id, 'field' => $field);
403 public function getOptions($country = null
)
405 $this->_fetchOptions();
407 if ($country == null
) {
408 return PlIteratorUtils
::fromArray($this->options
, 1, true
);
410 if (array_key_exists($country, $this->suboptions
)) {
411 return PlIteratorUtils
::fromArray($this->suboptions
[$country], 1, true
);
417 public function getIDs($text, $mode, $country = null
)
419 if ($country == null
) {
420 return XDB
::fetchColumn('SELECT id
421 FROM geoloc_administrativeareas
422 WHERE name ' . self
::makeSqlConcat($text, $mode));
424 return XDB
::fetchColumn('SELECT id
425 FROM geoloc_administrativeareas
426 WHERE country = {?} AND name' . self
::makeSqlConcat($text, $mode), $country);
432 // {{{ class DE_GroupesX
433 class DE_GroupesX
extends DirEnumeration
435 protected $idfield = 'asso.id';
436 protected $valfield = 'asso.nom';
437 protected $valfield2 = 'asso.diminutif';
438 protected $from = '#groupex#.asso AS asso';
439 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
441 protected $ac_join = "INNER JOIN #groupex#.membres AS memb ON (asso.id = memb.asso_id
442 AND (asso.cat = 'GroupesX' OR asso.cat = 'Institutions')
443 AND asso.pub = 'public')";
444 protected $ac_unique = 'memb.uid';
448 // {{{ class DE_Sectors
449 class DE_Sectors
extends DirEnumeration
451 protected $valfield = 'name';
452 protected $from = 'profile_job_sector_enum';
454 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
455 protected $ac_unique = 'profile_job.uid';
459 // {{{ class DE_Networking
460 class DE_Networking
extends DirEnumeration
462 protected $idfield = 'profile_networking_enum.network_type';
463 protected $valfield = 'profile_networking_enum.name';
464 protected $from = 'profile_networking_enum';
467 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.network_type = profile_networking_enum.network_type';
468 protected $ac_unique = 'profile_networking.uid';