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');
341 foreach($res->fetchAllRow() as $row) {
342 list($eduid, $degreeid, $name) = $row;
343 $options[$degreeid] = array('id' => $degreeid, 'field' => $name);
344 if (!array_key_exists($eduid, $this->suboptions
)) {
345 $this->suboptions
[$eduid] = array();
347 $this->suboptions
[$eduid][] = array('id' => $degreeid, 'field' => $name);
349 $this->options
= PlIteratorUtils
::fromArray($options, 1, true
);
352 public function getOptions($eduid = null
)
354 $this->_fetchOptions();
355 if ($eduid == null
) {
356 return $this->options
;
358 if (array_key_exists($eduid, $this->suboptions
)) {
359 return PlIteratorUtils
::fromArray($this->suboptions
[$eduid], 1, true
);
365 public function getOptionsArray($eduid = null
)
367 $it = $this->getOptions($eduid);
369 while ($row = $it->next()) {
370 $options[$row['id']] = $row['field'];
375 public function getIDs($text, $mode, $eduid = null
)
377 if ($eduid == null
) {
378 return XDB
::fetchColumn('SELECT id
379 FROM profile_education_degree_enum
380 WHERE degree ' . XDB
::formatWildcards($mode, $text));
382 return XDB
::fetchColumn('SELECT pede.id
383 FROM profile_education_degree AS ped
384 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
385 WHERE ped.eduid = {?} AND pede.degree ' . XDB
::formatWildcards($mode, $text), $eduid);
391 // {{{ class DE_EducationFields
392 class DE_EducationFields
extends DirEnumeration
394 protected $valfield = 'field';
395 protected $from = 'profile_education_field_enum';
397 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
398 protected $ac_unique = 'profile_education.uid';
404 // {{{ class DE_Nationalities
405 class DE_Nationalities
extends DirEnumeration
407 protected $idfield = 'iso_3166_1_a2';
408 protected $valfield = 'nationalityFR';
409 protected $valfield2 = 'nationality';
410 protected $from = 'geoloc_countries AS gc';
411 protected $join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
413 protected $ac_join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
414 protected $ac_unique = 'profiles.pid';
418 // {{{ class DE_Countries
419 class DE_Countries
extends DirEnumeration
421 protected $idfield = 'iso_3166_1_a2';
422 protected $valfield = 'countryFR';
423 protected $valfield2 = 'country';
424 protected $from = 'geoloc_countries';
426 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryFR';
427 protected $ac_unique = 'profile_addresses.pid';
431 // {{{ class DE_AdminAreas
432 class DE_AdminAreas
extends DirEnumeration
434 protected $suboptions = array();
436 protected function loadOptions()
438 $res = XDB
::query('SELECT id, name AS field, country
439 FROM geoloc_administrativeareas
443 foreach($res->fetchAllRow() as $row) {
444 list($id, $field, $country) = $row;
445 $options[$id] = array('id' => $id, 'field' => $field);
446 if (!array_key_exists($country, $this->suboptions
)) {
447 $this->suboptions
[$country] = array();
449 $this->suboptions
[$country][] = array('id' => $id, 'field' => $field);
451 $this->options
= PlIteratorUtils
::fromArray($options, 1, true
);
454 public function getOptions($country = null
)
456 $this->_fetchOptions();
458 if ($country == null
) {
459 return $this->options
;
461 if (array_key_exists($country, $this->suboptions
)) {
462 return PlIteratorUtils
::fromArray($this->suboptions
[$country], 1, true
);
468 public function getOptionsArray($country = null
)
470 $it = $this->getOptions($eduid);
472 while ($row = $it->next()) {
473 $options[$row['id']] = $row['field'];
478 public function getIDs($text, $mode, $country = null
)
480 if ($country == null
) {
481 return XDB
::fetchColumn('SELECT id
482 FROM geoloc_administrativeareas
483 WHERE name ' . XDB
::formatWildcards($mode, $text));
485 return XDB
::fetchColumn('SELECT id
486 FROM geoloc_administrativeareas
487 WHERE country = {?} AND name' . XDB
::formatWildcards($mode, $text), $country);
493 // {{{ class DE_Localities
494 class DE_Localities
extends DirEnumeration
496 protected $valfield = 'gl.name';
497 protected $from = 'geoloc_localities AS gl';
499 protected $ac_join = 'profile_addresses AS pa ON (pa.localityID = gl.id)';
500 protected $ac_unique = 'pa.pid';
506 // {{{ class DE_Companies
507 class DE_Companies
extends DirEnumeration
509 protected $valfield = 'pje.name';
510 protected $valfield2 = 'pje.acronym';
511 protected $from = 'profile_job_enum AS pje';
513 protected $ac_join = 'INNER JOIN profile_job AS pj ON (pj.jobid = pje.id)';
514 protected $ac_unique = 'pj.uid';
518 // {{{ class DE_Sectors
519 class DE_Sectors
extends DirEnumeration
521 protected $valfield = 'name';
522 protected $from = 'profile_job_sector_enum';
524 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
525 protected $ac_unique = 'profile_job.uid';
529 // {{{ class DE_JobDescription
530 class DE_JobDescription
532 protected $valfield = 'pj.description';
533 protected $from = 'profile_job AS pj';
534 protected $idfield = 'pj.pid';
536 protected $ac_unique = 'pj.pid';
542 // {{{ class DE_Networking
543 class DE_Networking
extends DirEnumeration
545 protected $idfield = 'profile_networking_enum.network_type';
546 protected $valfield = 'profile_networking_enum.name';
547 protected $from = 'profile_networking_enum';
550 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.network_type = profile_networking_enum.network_type';
551 protected $ac_unique = 'profile_networking.uid';