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 NAMETYPES
= 'nametypes';
34 const BINETS
= 'binets';
35 const GROUPESX
= 'groupesx';
36 const SECTIONS
= 'sections';
38 const EDUSCHOOLS
= 'educationschools';
39 const EDUDEGREES
= 'educationdegrees';
40 const EDUFIELDS
= 'educationfields';
42 const NATIONALITIES
= 'nationalities';
43 const COUNTRIES
= 'countries';
44 const ADMINAREAS
= 'adminareas';
45 const LOCALITIES
= 'localities';
47 const COMPANIES
= 'companies';
48 const SECTORS
= 'sectors';
49 const JOBDESCRIPTION
= 'jobdescription';
51 const NETWORKS
= 'networking';
53 static private $enumerations = array();
55 static private function init($type)
57 $cls = "DE_" . ucfirst($type);
58 self
::$enumerations[$type] = new $cls();
61 /** Retrieves all options for a given type
62 * @param $type Type of enum for which options are requested
63 * @return XorgDbIterator over the results
65 static public function getOptions()
67 $args = func_get_args();
68 $type = array_shift($args);
69 if (!array_key_exists($type, self
::$enumerations)) {
72 $obj = self
::$enumerations[$type];
73 return call_user_func_array(array($obj, 'getOptions'), $args);
76 /** Retrieves all options for a given type
77 * @param $type Type of enum for which options are requested
78 * @return Array of the results the results
80 static public function getOptionsArray()
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, 'getOptionsArray'), $args);
91 /** Retrieves all options with number of profiles for autocompletion
92 * @param $type Type of enum for which options are requested
93 * @param $text Text to autocomplete
94 * @return XorgDbIterator over the results
96 static public function getAutoComplete()
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, 'getAutoComplete'), $args);
107 /** Retrieves a list of IDs for a given type
108 * @param $type Type of enum for which IDs are requested
109 * @param $text Text to search in enum valuees
110 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
112 static public function getIDs()
114 $args = func_get_args();
115 $type = array_shift($args);
116 if (!array_key_exists($type, self
::$enumerations)) {
119 $obj = self
::$enumerations[$type];
120 return call_user_func_array(array($obj, 'getIDs'), $args);
125 // {{{ class DirEnumeration
126 abstract class DirEnumeration
128 /** An internal array of ID => optionTxt
130 protected $options = null
;
132 /** Description of the MySQL storage of the fields
134 protected $idfield = 'id';
135 protected $valfield = 'text';
136 protected $valfield2 = null
;
138 protected $join = '';
139 protected $where = '';
141 /** Fields for autocompletion
143 protected $ac_join = ''; // Additional joins
144 protected $ac_where = null
; // Additional where
145 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
146 protected $ac_unique; // Which field is to be taken as unique
147 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
148 protected $ac_withid = true
; // Do we want to fetch id too ?
150 protected function _fetchOptions()
152 if (is_null($this->options
)) {
153 $this->loadOptions();
157 public function getOptions()
159 $this->_fetchOptions();
160 return $this->options
;
163 public function getOptionsArray()
165 $this->_fetchOptions();
167 while ($row = $this->options
->next()) {
168 $options[$row['id']] = $row['field'];
173 // {{{ function getIDs
174 /** Retrieves possible IDs for given text
175 * @param $text Text to search for IDs
176 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
177 * @return An array of matching IDs ; if empty, input should be considered invalid
179 public function getIDs($text, $mode)
181 if ($mode == XDB
::WILDCARD_EXACT
) {
182 $options = $this->getOptions();
183 return array_keys($options, $text);
185 if ($this->where
== null
) {
188 $where = $this->where
. ' AND ';
191 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
192 if ($this->valfield2
!= null
) {
193 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
195 $where .= '(' . implode(' OR ', $conds) . ')';
197 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
198 FROM ' . $this->from
. '
201 GROUP BY ' . $this->idfield
);
206 private function mkTests($field, $text)
209 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
210 if (!$this->ac_beginwith
) {
211 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
212 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
217 // {{{ function getAutoComplete
218 public function getAutoComplete($text)
220 $text = str_replace(array('%', '_'), '', $text);
222 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
225 $where = $this->ac_where
. ' AND ';
228 $tests = $this->mkTests($this->valfield
, $text);
229 if (!is_null($this->valfield2
)) {
230 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
233 $where .= '(' . implode(' OR ', $tests) . ')';
235 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
236 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
237 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
238 FROM ' . $this->from
. '
239 ' . $this->ac_join
. '
241 GROUP BY ' . $this->valfield
. '
242 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
247 // {{{ function loadOptions
248 /** The function used to load options
250 protected function loadOptions()
252 $this->options
= XDB
::iterator('SELECT ' . $this->valfield
. ' AS field,
253 ' . $this->idfield
. ' AS id
254 FROM ' . $this->from
. '
257 GROUP BY ' . $this->valfield
. '
258 ORDER BY ' . $this->valfield
);
264 // {{{ class DE_NameTypes
265 // returns 'system' names ('lastname', 'lastname_marital', ...)
266 class DE_NameTypes
extends DirEnumeration
268 protected $from = 'profile_name_enum';
269 protected $valfield = 'type';
275 // {{{ class DE_Binets
276 class DE_Binets
extends DirEnumeration
278 protected $from = 'binets_def';
280 protected $ac_join = 'INNER JOIN binets_ins ON (binets_def.id = binets_ins.binet_id)';
281 protected $ac_unique = 'binets_ins.user_id';
285 // {{{ class DE_Sections
286 class DE_Sections
extends DirEnumeration
288 protected $from = 'sections';
290 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = sections.id)';
291 protected $ac_unique = 'profiles.pid';
295 // {{{ class DE_GroupesX
296 class DE_GroupesX
extends DirEnumeration
298 protected $idfield = 'asso.id';
299 protected $valfield = 'asso.nom';
300 protected $valfield2 = 'asso.diminutif';
301 protected $from = 'groups AS asso';
302 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
304 protected $ac_join = "INNER JOIN group_members AS memb ON (asso.id = memb.asso_id
305 AND (asso.cat = 'GroupesX' OR asso.cat = 'Institutions')
306 AND asso.pub = 'public')";
307 protected $ac_unique = 'memb.uid';
313 // {{{ class DE_EducationSchools
314 class DE_EducationSchools
extends DirEnumeration
316 protected $valfield = 'name';
317 protected $valfield2 = 'abbreviation';
318 protected $from = 'profile_education_enum';
320 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
321 protected $ac_unique = 'profile_education.uid';
325 // {{{ class DE_EducationDegrees
326 class DE_EducationDegrees
extends DirEnumeration
328 protected $from = 'profile_education_degree_enum';
329 protected $valfield = 'degree';
331 protected $suboptions = array();
333 protected function loadOptions()
335 $res = XDB
::query('SELECT ped.eduid, ped.degreeid, pede.degree
336 FROM profile_education_enum AS pee
337 LEFT JOIN profile_education_degree AS ped ON (pee.id = ped.eduid)
338 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
339 ORDER BY pede.degree');
340 foreach($res->fetchAllRow() as $row) {
341 list($eduid, $degreeid, $name) = $row;
342 $this->options
[$degreeid] = array('id' => $degreeid, 'field' => $name);
343 if (!array_key_exists($eduid, $this->suboptions
)) {
344 $this->suboptions
[$eduid] = array();
346 $this->suboptions
[$eduid][] = array('id' => $degreeid, 'field' => $name);
350 public function getOptions($eduid = null
)
352 $this->_fetchOptions();
353 if ($eduid == null
) {
354 return PlIteratorUtils
::fromArray($this->options
, 1, true
);
356 if (array_key_exists($eduid, $this->suboptions
)) {
357 return PlIteratorUtils
::fromArray($this->suboptions
[$eduid], 1, true
);
363 public function getIDs($text, $mode, $eduid = null
)
365 if ($eduid == null
) {
366 return XDB
::fetchColumn('SELECT id
367 FROM profile_education_degree_enum
368 WHERE degree ' . XDB
::formatWildcards($mode, $text));
370 return XDB
::fetchColumn('SELECT pede.id
371 FROM profile_education_degree AS ped
372 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
373 WHERE ped.eduid = {?} AND pede.degree ' . XDB
::formatWildcards($mode, $text), $eduid);
379 // {{{ class DE_EducationFields
380 class DE_EducationFields
extends DirEnumeration
382 protected $valfield = 'field';
383 protected $from = 'profile_education_field_enum';
385 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
386 protected $ac_unique = 'profile_education.uid';
392 // {{{ class DE_Nationalities
393 class DE_Nationalities
extends DirEnumeration
395 protected $idfield = 'iso_3166_1_a2';
396 protected $valfield = 'nationalityFR';
397 protected $valfield2 = 'nationality';
398 protected $from = 'geoloc_countries AS gc';
399 protected $join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
401 protected $ac_join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
402 protected $ac_unique = 'profiles.pid';
406 // {{{ class DE_Countries
407 class DE_Countries
extends DirEnumeration
409 protected $idfield = 'iso_3166_1_a2';
410 protected $valfield = 'countryFR';
411 protected $valfield2 = 'country';
412 protected $from = 'geoloc_countries';
414 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryFR';
415 protected $ac_unique = 'profile_addresses.pid';
419 // {{{ class DE_AdminAreas
420 class DE_AdminAreas
extends DirEnumeration
422 protected $suboptions = array();
424 protected function loadOptions()
426 $res = XDB
::query('SELECT id, name AS field, country
427 FROM geoloc_administrativeareas
430 foreach($res->fetchAllRow() as $row) {
431 list($id, $field, $country) = $row;
432 $this->options
[] = array('id' => $id, 'field' => $field);
433 if (!array_key_exists($country, $this->suboptions
)) {
434 $this->suboptions
[$country] = array();
436 $this->suboptions
[$country][] = array('id' => $id, 'field' => $field);
440 public function getOptions($country = null
)
442 $this->_fetchOptions();
444 if ($country == null
) {
445 return PlIteratorUtils
::fromArray($this->options
, 1, true
);
447 if (array_key_exists($country, $this->suboptions
)) {
448 return PlIteratorUtils
::fromArray($this->suboptions
[$country], 1, true
);
454 public function getIDs($text, $mode, $country = null
)
456 if ($country == null
) {
457 return XDB
::fetchColumn('SELECT id
458 FROM geoloc_administrativeareas
459 WHERE name ' . XDB
::formatWildcards($mode, $text));
461 return XDB
::fetchColumn('SELECT id
462 FROM geoloc_administrativeareas
463 WHERE country = {?} AND name' . XDB
::formatWildcards($mode, $text), $country);
469 // {{{ class DE_Localities
470 class DE_Localities
extends DirEnumeration
472 protected $valfield = 'gl.name';
473 protected $from = 'geoloc_localities AS gl';
475 protected $ac_join = 'profile_addresses AS pa ON (pa.localityID = gl.id)';
476 protected $ac_unique = 'pa.pid';
482 // {{{ class DE_Companies
483 class DE_Companies
extends DirEnumeration
485 protected $valfield = 'pje.name';
486 protected $valfield2 = 'pje.acronym';
487 protected $from = 'profile_job_enum AS pje';
489 protected $ac_join = 'INNER JOIN profile_job AS pj ON (pj.jobid = pje.id)';
490 protected $ac_unique = 'pj.uid';
494 // {{{ class DE_Sectors
495 class DE_Sectors
extends DirEnumeration
497 protected $valfield = 'name';
498 protected $from = 'profile_job_sector_enum';
500 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
501 protected $ac_unique = 'profile_job.uid';
505 // {{{ class DE_JobDescription
506 class DE_JobDescription
508 protected $valfield = 'pj.description';
509 protected $from = 'profile_job AS pj';
510 protected $idfield = 'pj.pid';
512 protected $ac_unique = 'pj.pid';
518 // {{{ class DE_Networking
519 class DE_Networking
extends DirEnumeration
521 protected $idfield = 'profile_networking_enum.network_type';
522 protected $valfield = 'profile_networking_enum.name';
523 protected $from = 'profile_networking_enum';
526 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.network_type = profile_networking_enum.network_type';
527 protected $ac_unique = 'profile_networking.uid';