07786b3d94ae630f0b642900b2c286d7991b434d
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 GROUPESX
= 'groupesx';
34 const SECTIONS
= 'sections';
36 const SCHOOLS
= 'schools';
37 const DEGREES
= 'degrees';
38 const STUDIESDOMAINS
= 'studiesdomains';
40 const NATIONALITIES
= 'nationalities';
41 const COUNTRIES
= 'countries';
42 const ADMINAREAS
= 'adminareas';
43 const LOCALITIES
= 'localities';
45 const COMPANIES
= 'companies';
46 const SECTORS
= 'sectors';
47 const JOBDESCRIPTION
= 'jobdescription';
49 const NETWORKS
= 'networking';
51 static private $enumerations = array();
53 static private function init($type)
55 $cls = "DE_" . ucfirst($type);
56 self
::$enumerations[$type] = new $cls();
59 /** Retrieves all options for a given type
60 * @param $type Type of enum for which options are requested
61 * @return XorgDbIterator over the results
62 * TODO : Find a way to get either an array, or the adequate PlIterator
64 static public function getOptions()
66 $args = func_get_args();
67 $type = array_shift($args);
68 if (!array_key_exists($type, self
::$enumerations)) {
71 $obj = self
::$enumerations[$type];
72 return call_user_func_array(array($obj, 'getOptions'), $args);
75 /** Retrieves all options with number of profiles for autocompletion
76 * @param $type Type of enum for which options are requested
77 * @param $text Text to autocomplete
78 * @return XorgDbIterator over the results
80 static public function getAutoComplete()
82 $args = func_get_args();
83 $type = array_shift($args);
84 if (!array_key_exists($type, self
::$enumerations)) {
87 $obj = self
::$enumerations[$type];
88 return call_user_func_array(array($obj, 'getAutoComplete'), $args);
91 /** Retrieves a list of IDs for a given type
92 * @param $type Type of enum for which IDs are requested
93 * @param $text Text to search in enum valuees
94 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
96 static public function getIDs()
98 $args = func_get_args();
99 $type = array_shift($args);
100 if (!array_key_exists($type, self
::$enumerations)) {
103 $obj = self
::$enumerations[$type];
104 return call_user_func_array(array($obj, 'getIDs'), $args);
109 // {{{ class DirEnumeration
110 abstract class DirEnumeration
112 /** Modes for LIKE searches
114 const MODE_EXACT
= 0x000;
115 const MODE_PREFIX
= 0x001;
116 const MODE_SUFFIX
= 0x002;
117 const MODE_CONTAINS
= 0x003;
119 /** An internal array of ID => optionTxt
121 protected $options = null
;
123 /** Description of the MySQL storage of the fields
125 protected $idfield = 'id';
126 protected $valfield = 'text';
127 protected $valfield2 = null
;
129 protected $join = '';
130 protected $where = '';
132 /** Fields for autocompletion
134 protected $ac_join = ''; // Additional joins
135 protected $ac_where = null
; // Additional where
136 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
137 protected $ac_unique; // Which field is to be taken as unique
138 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
139 protected $ac_withid = true
; // Do we want to fetch id too ?
141 protected function _fetchOptions()
143 if (is_null($this->options
)) {
144 $this->loadOptions();
148 public function getOptions()
150 $this->_fetchOptions();
151 return $this->options
;
154 // {{{ function getIDs
155 /** Retrieves possible IDs for given text
156 * @param $text Text to search for IDs
157 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
158 * @return An array of matching IDs ; if empty, input should be considered invalid
160 public function getIDs($text, $mode)
162 if ($mode == self
::MODE_EXACT
) {
163 $options = $this->getOptions();
164 return array_keys($options, $text);
166 if ($this->where
== null
) {
169 $where = $this->where
. ' AND ';
172 $conds[] = $this->valfield
. self
::makeSqlConcat($text, $mode);
173 if ($this->valfield2
!= null
) {
174 $conds[] = $this->valfield2
. self
::makeSqlConcat($text, $mode);
176 $where .= '(' . implode(' OR ', $conds) . ')';
178 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
179 FROM ' . $this->from
. '
182 GROUP BY ' . $this->idfield
);
187 private function mkTests($field, $text)
190 $tests[] = $field . self
::makeSqlConcat($text, self
::MODE_PREFIX
);
191 if (!$this->ac_beginwith
) {
192 $tests[] = $field . self
::makeSqlConcat(' ' . $text, self
::MODE_CONTAINS
);
193 $tests[] = $field . self
::makeSqlConcat('-' . $text, self
::MODE_CONTAINS
);
198 // {{{ function getAutoComplete
199 public function getAutoComplete($text)
201 $text = str_replace(array('%', '_'), '', $text);
203 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
206 $where = $this->ac_where
. ' AND ';
209 $tests = $this->mkTests($this->valfield
, $text);
210 if (!is_null($this->valfield2
)) {
211 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
214 $where .= '(' . implode(' OR ', $tests) . ')';
216 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
217 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
218 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
219 FROM ' . $this->from
. '
220 ' . $this->ac_join
. '
222 GROUP BY ' . $this->valfield
. '
223 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
228 // {{{ function makeSqlConcat
229 static protected function makeSqlConcat($text, $mode)
231 if ($mode == self
::MODE_EXACT
) {
232 return ' = ' . XDB
::format('{?}', $text);
234 if ($mode == self
::MODE_PREFIX
) {
235 $right = XDB
::format('CONCAT({?}, \'%\')', $text);
236 } else if ($mode == self
::MODE_SUFFIX
) {
237 $right = XDB
::format('CONCAT(\'%\', {?})', $text);
239 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $text);
241 return ' LIKE ' . $right;
245 // {{{ function loadOptions
246 /** The function used to load options
248 protected function loadOptions()
250 $this->options
= XDB
::iterator('SELECT ' . $this->valfield
. ' AS field,
251 ' . $this->idfield
. ' AS id
252 FROM ' . $this->from
. '
255 GROUP BY ' . $this->valfield
. '
256 ORDER BY ' . $this->valfield
);
264 // {{{ class DE_Binets
265 class DE_Binets
extends DirEnumeration
267 protected $from = 'binets_def';
269 protected $ac_join = 'INNER JOIN binets_ins ON (binets_def.id = binets_ins.binet_id)';
270 protected $ac_unique = 'binets_ins.user_id';
274 // {{{ class DE_Sections
275 class DE_Sections
extends DirEnumeration
277 protected $from = 'sections';
279 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = sections.id)';
280 protected $ac_unique = 'profiles.pid';
284 // {{{ class DE_GroupesX
285 class DE_GroupesX
extends DirEnumeration
287 protected $idfield = 'asso.id';
288 protected $valfield = 'asso.nom';
289 protected $valfield2 = 'asso.diminutif';
290 protected $from = '#groupex#.asso AS asso';
291 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
293 protected $ac_join = "INNER JOIN #groupex#.membres AS memb ON (asso.id = memb.asso_id
294 AND (asso.cat = 'GroupesX' OR asso.cat = 'Institutions')
295 AND asso.pub = 'public')";
296 protected $ac_unique = 'memb.uid';
302 // {{{ class DE_Schools
303 class DE_Schools
extends DirEnumeration
305 protected $valfield = 'name';
306 protected $valfield2 = 'abbreviation';
307 protected $from = 'profile_education_enum';
309 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
310 protected $ac_unique = 'profile_education.uid';
314 // {{{ class DE_Degrees
315 class DE_Degrees
extends DirEnumeration
317 protected $from = 'profile_education_degree_enum';
318 protected $valfield = 'degree';
320 protected $suboptions = array();
322 protected function loadOptions()
324 $res = XDB
::query('SELECT ped.eduid, ped.degreeid, pede.degree
325 FROM profile_education_enum AS pee
326 LEFT JOIN profile_education_degree AS ped ON (pee.id = ped.eduid)
327 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
328 ORDER BY pede.degree');
329 foreach($res->fetchAllRow() as $row) {
330 list($eduid, $degreeid, $name) = $row;
331 $this->options
[$degreeid] = array('id' => $degreeid, 'field' => $name);
332 if (!array_key_exists($eduid, $this->suboptions
)) {
333 $this->suboptions
[$eduid] = array();
335 $this->suboptions
[$eduid][] = array('id' => $degreeid, 'field' => $name);
339 public function getOptions($eduid = null
)
341 $this->_fetchOptions();
342 if ($eduid == null
) {
343 return PlIteratorUtils
::fromArray($this->options
, 1, true
);
345 if (array_key_exists($eduid, $this->suboptions
)) {
346 return PlIteratorUtils
::fromArray($this->suboptions
[$eduid], 1, true
);
352 public function getIDs($text, $mode, $eduid = null
)
354 if ($eduid == null
) {
355 return XDB
::fetchColumn('SELECT id
356 FROM profile_education_degree_enum
357 WHERE degree ' . self
::makeSqlConcat($text, $mode));
359 return XDB
::fetchColumn('SELECT pede.id
360 FROM profile_education_degree AS ped
361 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
362 WHERE ped.eduid = {?} AND pede.degree ' . self
::makeSqlConcat($text, $mode), $eduid);
368 // {{{ class DE_StudiesSector
369 class DE_StudiesSector
extends DirEnumeration
371 protected $valfield = 'field';
372 protected $from = 'profile_education_field_enum';
374 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
375 protected $ac_unique = 'profile_education.uid';
381 // {{{ class DE_Nationalities
382 class DE_Nationalities
extends DirEnumeration
384 protected $idfield = 'iso_3166_1_a2';
385 protected $valfield = 'nationalityFR';
386 protected $valfield2 = 'nationality';
387 protected $from = 'geoloc_countries AS gc';
388 protected $join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
390 protected $ac_join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
391 protected $ac_unique = 'profiles.pid';
395 // {{{ class DE_Countries
396 class DE_Countries
extends DirEnumeration
398 protected $idfield = 'iso_3166_1_a2';
399 protected $valfield = 'countryFR';
400 protected $valfield2 = 'country';
401 protected $from = 'geoloc_countries';
403 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryFR';
404 protected $ac_unique = 'profile_addresses.pid';
408 // {{{ class DE_AdminAreas
409 class DE_AdminAreas
extends DirEnumeration
411 protected $suboptions = array();
413 protected function loadOptions()
415 $res = XDB
::query('SELECT id, name AS field, country
416 FROM geoloc_administrativeareas
419 foreach($res->fetchAllRow() as $row) {
420 list($id, $field, $country) = $row;
421 $this->options
[] = array('id' => $id, 'field' => $field);
422 if (!array_key_exists($country, $this->suboptions
)) {
423 $this->suboptions
[$country] = array();
425 $this->suboptions
[$country][] = array('id' => $id, 'field' => $field);
429 public function getOptions($country = null
)
431 $this->_fetchOptions();
433 if ($country == null
) {
434 return PlIteratorUtils
::fromArray($this->options
, 1, true
);
436 if (array_key_exists($country, $this->suboptions
)) {
437 return PlIteratorUtils
::fromArray($this->suboptions
[$country], 1, true
);
443 public function getIDs($text, $mode, $country = null
)
445 if ($country == null
) {
446 return XDB
::fetchColumn('SELECT id
447 FROM geoloc_administrativeareas
448 WHERE name ' . self
::makeSqlConcat($text, $mode));
450 return XDB
::fetchColumn('SELECT id
451 FROM geoloc_administrativeareas
452 WHERE country = {?} AND name' . self
::makeSqlConcat($text, $mode), $country);
458 // {{{ class DE_Localities
459 class DE_Localities
extends DirEnumeration
461 protected $valfield = 'gl.name';
462 protected $from = 'geoloc_localities AS gl';
464 protected $ac_join = 'profile_addresses AS pa ON (pa.localityID = gl.id)';
465 protected $ac_unique = 'pa.pid';
471 // {{{ class DE_Companies
472 class DE_Companies
extends DirEnumeration
474 protected $valfield = 'pje.name';
475 protected $valfield2 = 'pje.acronym';
476 protected $from = 'profile_job_enum AS pje';
478 protected $ac_join = 'INNER JOIN profile_job AS pj ON (pj.jobid = pje.id)';
479 protected $ac_unique = 'pj.uid';
483 // {{{ class DE_Sectors
484 class DE_Sectors
extends DirEnumeration
486 protected $valfield = 'name';
487 protected $from = 'profile_job_sector_enum';
489 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
490 protected $ac_unique = 'profile_job.uid';
494 // {{{ class DE_JobDescription
495 class DE_JobDescription
497 protected $valfield = 'pj.description';
498 protected $from = 'profile_job AS pj';
499 protected $idfield = 'pj.pid';
501 protected $ac_unique = 'pj.pid';
507 // {{{ class DE_Networking
508 class DE_Networking
extends DirEnumeration
510 protected $idfield = 'profile_networking_enum.network_type';
511 protected $valfield = 'profile_networking_enum.name';
512 protected $from = 'profile_networking_enum';
515 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.network_type = profile_networking_enum.network_type';
516 protected $ac_unique = 'profile_networking.uid';