Add comments and folding to DirEnumeration
[platal.git] / include / directory.enums.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 // {{{ class DirEnum
23 /** This class stores all data for the different kinds of fields.
24 * It is only a dispatcher for the various DirEnum_XXX classes.
25 */
26 class DirEnum
27 {
28 /** Name of availables Enumerations
29 * Each of these consts contains the basename of the class (its full name
30 * being DE_$basename).
31 */
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
44 static private $enumerations = array();
45
46 static private function init($type)
47 {
48 $cls = "DE_" . ucfirst($type);
49 self::$enumerations[$type] = new $cls();
50 }
51
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
56 */
57 static public function getOptions()
58 {
59 $args = func_get_args();
60 $type = array_shift($args);
61 if (!array_key_exists($type, self::$enumerations)) {
62 self::init($type);
63 }
64 $obj = self::$enumerations[$type];
65 return call_user_func_array(array($obj, 'getOptions'), $args);
66 }
67
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)
72 */
73 static public function getIDs()
74 {
75 $args = func_get_args();
76 $type = array_shift($args);
77 if (!array_key_exists($type, self::$enumerations)) {
78 self::init($type);
79 }
80 $obj = self::$enumerations[$type];
81 return call_user_func_array(array($obj, 'getIDs'), $args);
82 }
83 }
84 // }}}
85
86 // {{{ class DirEnumeration
87 abstract class DirEnumeration
88 {
89 /** Modes for LIKE searches
90 */
91 const MODE_EXACT = 0x000;
92 const MODE_PREFIX = 0x001;
93 const MODE_SUFFIX = 0x002;
94 const MODE_CONTAINS = 0x003;
95
96 /** An internal array of ID => optionTxt
97 */
98 protected $options;
99
100 /** Description of the MySQL storage of the fields
101 */
102 protected $idfield = 'id';
103 protected $valfield = 'text';
104 protected $from;
105 protected $join = '';
106 protected $where = '';
107
108 public function __construct() {
109 $this->loadOptions();
110 }
111
112 public function getOptions()
113 {
114 return $this->options;
115 }
116
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
121 */
122 public function getIDs($text, $mode)
123 {
124 if ($mode == self::MODE_EXACT) {
125 $options = $this->getOptions();
126 return array_keys($options, $text);
127 } else {
128 if ($this->where == null) {
129 $where = 'WHERE ';
130 } else {
131 $where = $this->where . ' AND ';
132 }
133 return XDB::fetchColumn('SELECT ' . $this->idfield . '
134 FROM ' . $this->from . '
135 ' . $this->join . '
136 ' . $where . $this->valfield . self::makeSqlConcat($text, $mode) . '
137 GROUP BY ' . $this->idfield);
138 }
139 }
140
141 static protected function makeSqlConcat($text, $mode)
142 {
143 if ($mode == self::MODE_EXACT) {
144 return ' = ' . XDB::format('{?}', $text);
145 }
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);
150 } else {
151 $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $text);
152 }
153 return ' LIKE ' . $right;
154 }
155
156 /** The function used to load options
157 */
158 protected function loadOptions()
159 {
160 $this->options = XDB::iterator('SELECT ' . $this->valfield . ' AS field,
161 ' . $this->idfield . ' AS id
162 FROM ' . $this->from . '
163 ' . $this->join . '
164 ' . $this->where . '
165 GROUP BY ' . $this->valfield . '
166 ORDER BY ' . $this->valfield);
167 }
168 }
169 // }}}
170
171 // {{{ class DE_Binets
172 class DE_Binets extends DirEnumeration
173 {
174 protected $from = 'binets_def';
175 }
176 // }}}
177
178 // {{{ class DE_Sections
179 class DE_Sections extends DirEnumeration
180 {
181 protected $from = 'sections';
182 }
183 // }}}
184
185 // {{{ class DE_Schools
186 class DE_Schools extends DirEnumeration
187 {
188 protected $valfield = 'name';
189 protected $from = 'profile_education_enum';
190 }
191 // }}}
192
193 // {{{ class DE_Degrees
194 class DE_Degrees extends DirEnumeration
195 {
196 protected $suboptions = array();
197
198 protected function loadOptions()
199 {
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();
210 }
211 $this->suboptions[$eduid][] = array('id' => $degreeid, 'field' => $name);
212 }
213 }
214
215 public function getOptions($eduid = null)
216 {
217 if ($eduid == null) {
218 return PlIteratorUtils::fromArray($this->options, 1, true);
219 }
220 if (array_key_exists($eduid, $this->suboptions)) {
221 return PlIteratorUtils::fromArray($this->suboptions[$eduid], 1, true);
222 } else {
223 return array();
224 }
225 }
226
227 public function getIDs($text, $mode, $eduid = null)
228 {
229 if ($eduid == null) {
230 return XDB::fetchColumn('SELECT id
231 FROM profile_education_degree_enum
232 WHERE degree ' . self::makeSqlConcat($text, $mode));
233 } else {
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);
238 }
239 }
240 }
241 // }}}
242
243 // {{{ class DE_StudiesSector
244 class DE_StudiesSector extends DirEnumeration
245 {
246 protected $valfield = 'field';
247 protected $from = 'profile_education_field_enum';
248 }
249 // }}}
250
251 // {{{ class DE_Nationalities
252 class DE_Nationalities extends DirEnumeration
253 {
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))';
258 }
259 // }}}
260
261 // {{{ class DE_Countries
262 class DE_Countries extends DirEnumeration
263 {
264 protected $idfield = 'iso_3166_1_a2';
265 protected $valfield = 'countryFR';
266 protected $from = 'geoloc_countries';
267 }
268 // }}}
269
270 // {{{ class DE_AdminAreas
271 class DE_AdminAreas extends DirEnumeration
272 {
273 protected $suboptions = array();
274
275 protected function loadOptions()
276 {
277 $res = XDB::query('SELECT id, name AS field, country
278 FROM geoloc_administrativeareas
279 GROUP BY name
280 ORDER BY name');
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();
286 }
287 $this->suboptions[$country][] = array('id' => $id, 'field' => $field);
288 }
289 }
290
291 public function getOptions($country = null)
292 {
293 if ($country == null) {
294 return PlIteratorUtils::fromArray($this->options, 1, true);
295 }
296 if (array_key_exists($country, $this->suboptions)) {
297 return PlIteratorUtils::fromArray($this->suboptions[$country], 1, true);
298 } else {
299 return array();
300 }
301 }
302
303 public function getIDs($text, $mode, $country = null)
304 {
305 if ($country == null) {
306 return XDB::fetchColumn('SELECT id
307 FROM geoloc_administrativeareas
308 WHERE name ' . self::makeSqlConcat($text, $mode));
309 } else {
310 return XDB::fetchColumn('SELECT id
311 FROM geoloc_administrativeareas
312 WHERE country = {?} AND name' . self::makeSqlConcat($text, $mode), $country);
313 }
314 }
315 }
316 // }}}
317
318 // {{{ class DE_GroupesX
319 class DE_GroupesX extends DirEnumeration
320 {
321 protected $valfield = 'nom';
322 protected $from = '#groupex#.asso';
323 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
324 }
325 // }}}
326
327 // {{{ class DE_Sectors
328 class DE_Sectors extends DirEnumeration
329 {
330 protected $valfield = 'name';
331 protected $from = 'profile_job_sector_enum';
332 }
333 // }}}
334
335 // {{{ class DE_Networking
336 class DE_Networking extends DirEnumeration
337 {
338 protected $idfield = 'network_type';
339 protected $valfield = 'name';
340 protected $from = 'profile_networking_enum';
341 }
342 // }}}
343 ?>