37a40c58533f128bec8f8d494f34f979ca829f23
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';
44 static private $enumerations = array();
46 static private function init($type)
48 $cls = "DE_" . ucfirst($type);
49 self
::$enumerations[$type] = new $cls();
52 /** Retrieves all options for a given type
53 * @param $type Type of enum for which options are requested
54 * @return XorgDbIterator over the results
55 * TODO : Find a way to get either an array, or the adequate PlIterator
57 static public function getOptions()
59 $args = func_get_args();
60 $type = array_shift($args);
61 if (!array_key_exists($type, self
::$enumerations)) {
64 $obj = self
::$enumerations[$type];
65 return call_user_func_array(array($obj, 'getOptions'), $args);
68 /** Retrieves a list of IDs for a given type
69 * @param $type Type of enum for which IDs are requested
70 * @param $text Text to search in enum valuees
71 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
73 static public function getIDs()
75 $args = func_get_args();
76 $type = array_shift($args);
77 if (!array_key_exists($type, self
::$enumerations)) {
80 $obj = self
::$enumerations[$type];
81 return call_user_func_array(array($obj, 'getIDs'), $args);
86 // {{{ class DirEnumeration
87 abstract class DirEnumeration
89 /** Modes for LIKE searches
91 const MODE_EXACT
= 0x000;
92 const MODE_PREFIX
= 0x001;
93 const MODE_SUFFIX
= 0x002;
94 const MODE_CONTAINS
= 0x003;
96 /** An internal array of ID => optionTxt
100 /** Description of the MySQL storage of the fields
102 protected $idfield = 'id';
103 protected $valfield = 'text';
105 protected $join = '';
106 protected $where = '';
108 public function __construct() {
109 $this->loadOptions();
112 public function getOptions()
114 return $this->options
;
117 /** Retrieves possible IDs for given text
118 * @param $text Text to search for IDs
119 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
120 * @return An array of matching IDs ; if empty, input should be considered invalid
122 public function getIDs($text, $mode)
124 if ($mode == self
::MODE_EXACT
) {
125 $options = $this->getOptions();
126 return array_keys($options, $text);
128 if ($this->where
== null
) {
131 $where = $this->where
. ' AND ';
133 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
134 FROM ' . $this->from
. '
136 ' . $where . $this->valfield
. self
::makeSqlConcat($text, $mode) . '
137 GROUP BY ' . $this->idfield
);
141 static protected function makeSqlConcat($text, $mode)
143 if ($mode == self
::MODE_EXACT
) {
144 return ' = ' . XDB
::format('{?}', $text);
146 if (($mode & self
::MODE_CONTAINS
) == self
::MODE_PREFIX
) {
147 $right = XDB
::format('CONCAT({?}, \'%\')', $text);
148 } else if (($mode & self
::MODE_CONTAINS
) == self
::MODE_SUFFIX
) {
149 $right = XDB
::format('CONCAT(\'%\', {?})', $text);
151 $right = XDB
::format('CONCAT(\'%\', {?}, \'%\')', $text);
153 return ' LIKE ' . $right;
156 /** The function used to load options
158 protected function loadOptions()
160 $this->options
= XDB
::iterator('SELECT ' . $this->valfield
. ' AS field,
161 ' . $this->idfield
. ' AS id
162 FROM ' . $this->from
. '
165 GROUP BY ' . $this->valfield
. '
166 ORDER BY ' . $this->valfield
);
171 // {{{ class DE_Binets
172 class DE_Binets
extends DirEnumeration
174 protected $from = 'binets_def';
178 // {{{ class DE_Sections
179 class DE_Sections
extends DirEnumeration
181 protected $from = 'sections';
185 // {{{ class DE_Schools
186 class DE_Schools
extends DirEnumeration
188 protected $valfield = 'name';
189 protected $from = 'profile_education_enum';
193 // {{{ class DE_Degrees
194 class DE_Degrees
extends DirEnumeration
196 protected $suboptions = array();
198 protected function loadOptions()
200 $res = XDB
::query('SELECT ped.eduid, ped.degreeid, pede.degree
201 FROM profile_education_enum AS pee
202 LEFT JOIN profile_education_degree AS ped ON (pee.id = ped.eduid)
203 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
204 ORDER BY pede.degree');
205 foreach($res->fetchAllRow() as $row) {
206 list($eduid, $degreeid, $name) = $row;
207 $this->options
[$degreeid] = array('id' => $degreeid, 'field' => $name);
208 if (!array_key_exists($eduid, $this->suboptions
)) {
209 $this->suboptions
[$eduid] = array();
211 $this->suboptions
[$eduid][] = array('id' => $degreeid, 'field' => $name);
215 public function getOptions($eduid = null
)
217 if ($eduid == null
) {
218 return PlIteratorUtils
::fromArray($this->options
, 1, true
);
220 if (array_key_exists($eduid, $this->suboptions
)) {
221 return PlIteratorUtils
::fromArray($this->suboptions
[$eduid], 1, true
);
227 public function getIDs($text, $mode, $eduid = null
)
229 if ($eduid == null
) {
230 return XDB
::fetchColumn('SELECT id
231 FROM profile_education_degree_enum
232 WHERE degree ' . self
::makeSqlConcat($text, $mode));
234 return XDB
::fetchColumn('SELECT pede.id
235 FROM profile_education_degree AS ped
236 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
237 WHERE ped.eduid = {?} AND pede.degree ' . self
::makeSqlConcat($text, $mode), $eduid);
243 // {{{ class DE_StudiesSector
244 class DE_StudiesSector
extends DirEnumeration
246 protected $valfield = 'field';
247 protected $from = 'profile_education_field_enum';
251 // {{{ class DE_Nationalities
252 class DE_Nationalities
extends DirEnumeration
254 protected $idfield = 'iso_3166_1_a2';
255 protected $valfield = 'nationalityFR';
256 protected $from = 'geoloc_countries AS gc';
257 protected $join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
261 // {{{ class DE_Countries
262 class DE_Countries
extends DirEnumeration
264 protected $idfield = 'iso_3166_1_a2';
265 protected $valfield = 'countryFR';
266 protected $from = 'geoloc_countries';
270 // {{{ class DE_AdminAreas
271 class DE_AdminAreas
extends DirEnumeration
273 protected $suboptions = array();
275 protected function loadOptions()
277 $res = XDB
::query('SELECT id, name AS field, country
278 FROM geoloc_administrativeareas
281 foreach($res->fetchAllRow() as $row) {
282 list($id, $field, $country) = $row;
283 $this->options
[] = array('id' => $id, 'field' => $field);
284 if (!array_key_exists($country, $this->suboptions
)) {
285 $this->suboptions
[$country] = array();
287 $this->suboptions
[$country][] = array('id' => $id, 'field' => $field);
291 public function getOptions($country = null
)
293 if ($country == null
) {
294 return PlIteratorUtils
::fromArray($this->options
, 1, true
);
296 if (array_key_exists($country, $this->suboptions
)) {
297 return PlIteratorUtils
::fromArray($this->suboptions
[$country], 1, true
);
303 public function getIDs($text, $mode, $country = null
)
305 if ($country == null
) {
306 return XDB
::fetchColumn('SELECT id
307 FROM geoloc_administrativeareas
308 WHERE name ' . self
::makeSqlConcat($text, $mode));
310 return XDB
::fetchColumn('SELECT id
311 FROM geoloc_administrativeareas
312 WHERE country = {?} AND name' . self
::makeSqlConcat($text, $mode), $country);
318 // {{{ class DE_GroupesX
319 class DE_GroupesX
extends DirEnumeration
321 protected $valfield = 'nom';
322 protected $from = '#groupex#.asso';
323 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
327 // {{{ class DE_Sectors
328 class DE_Sectors
extends DirEnumeration
330 protected $valfield = 'name';
331 protected $from = 'profile_job_sector_enum';
335 // {{{ class DE_Networking
336 class DE_Networking
extends DirEnumeration
338 protected $idfield = 'network_type';
339 protected $valfield = 'name';
340 protected $from = 'profile_networking_enum';