Add support for autocompletion in DirEnum
[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 const LOCALITIES = 'localities'; // TODO
44 const COMPANIES = 'companies'; // TODO
45 const JOBDESCRIPTION = 'jobdescription'; // TODO
46
47 static private $enumerations = array();
48
49 static private function init($type)
50 {
51 $cls = "DE_" . ucfirst($type);
52 self::$enumerations[$type] = new $cls();
53 }
54
55 /** Retrieves all options for a given type
56 * @param $type Type of enum for which options are requested
57 * @return XorgDbIterator over the results
58 * TODO : Find a way to get either an array, or the adequate PlIterator
59 */
60 static public function getOptions()
61 {
62 $args = func_get_args();
63 $type = array_shift($args);
64 if (!array_key_exists($type, self::$enumerations)) {
65 self::init($type);
66 }
67 $obj = self::$enumerations[$type];
68 return call_user_func_array(array($obj, 'getOptions'), $args);
69 }
70
71 /** Retrieves all options with number of profiles for autocompletion
72 * @param $type Type of enum for which options are requested
73 * @param $text Text to autocomplete
74 * @return XorgDbIterator over the results
75 */
76 static public function getAutoComplete()
77 {
78 $args = func_get_args();
79 $type = array_shift($args);
80 if (!array_key_exists($type, self::$enumerations)) {
81 self::init($type);
82 }
83 $obj = self::$enumerations[$type];
84 return call_user_func_array(array($obj, 'getAutoComplete'), $args);
85 }
86
87 /** Retrieves a list of IDs for a given type
88 * @param $type Type of enum for which IDs are requested
89 * @param $text Text to search in enum valuees
90 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
91 */
92 static public function getIDs()
93 {
94 $args = func_get_args();
95 $type = array_shift($args);
96 if (!array_key_exists($type, self::$enumerations)) {
97 self::init($type);
98 }
99 $obj = self::$enumerations[$type];
100 return call_user_func_array(array($obj, 'getIDs'), $args);
101 }
102 }
103 // }}}
104
105 // {{{ class DirEnumeration
106 abstract class DirEnumeration
107 {
108 /** Modes for LIKE searches
109 */
110 const MODE_EXACT = 0x000;
111 const MODE_PREFIX = 0x001;
112 const MODE_SUFFIX = 0x002;
113 const MODE_CONTAINS = 0x003;
114
115 /** An internal array of ID => optionTxt
116 */
117 protected $options;
118
119 /** Description of the MySQL storage of the fields
120 */
121 protected $idfield = 'id';
122 protected $valfield = 'text';
123 protected $valfield2 = null;
124 protected $from;
125 protected $join = '';
126 protected $where = '';
127
128 /** Fields for autocompletion
129 */
130 protected $ac_join = ''; // Additional joins
131 protected $ac_where = null; // Additional where
132 protected $ac_beginwith = true; // Whether to search for 'x%' or for '%x%'
133 protected $ac_unique; // Which field is to be taken as unique
134 protected $ac_distinct = true; // Whether we want to keep only distinct valfield value
135 protected $ac_withid = true; // Do we want to fetch id too ?
136
137 public function __construct() {
138 $this->loadOptions();
139 }
140
141 public function getOptions()
142 {
143 return $this->options;
144 }
145
146 // {{{ function getIDs
147 /** Retrieves possible IDs for given text
148 * @param $text Text to search for IDs
149 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
150 * @return An array of matching IDs ; if empty, input should be considered invalid
151 */
152 public function getIDs($text, $mode)
153 {
154 if ($mode == self::MODE_EXACT) {
155 $options = $this->getOptions();
156 return array_keys($options, $text);
157 } else {
158 if ($this->where == null) {
159 $where = 'WHERE ';
160 } else {
161 $where = $this->where . ' AND ';
162 }
163 $conds = array();
164 $conds[] = $this->valfield . self::makeSqlConcat($text, $mode);
165 if ($this->valfield2 != null) {
166 $conds[] = $this->valfield2 . self::makeSqlConcat($text, $mode);
167 }
168 $where .= '(' . implode(' OR ', $conds) . ')';
169
170 return XDB::fetchColumn('SELECT ' . $this->idfield . '
171 FROM ' . $this->from . '
172 ' . $this->join . '
173 ' . $where . '
174 GROUP BY ' . $this->idfield);
175 }
176 }
177 // }}}
178
179 private function mkTests($field, $text)
180 {
181 $tests = array();
182 $tests[] = $field . self::makeSqlConcat($text, self::MODE_PREFIX);
183 if (!$this->ac_beginwith) {
184 $tests[] = $field . self::makeSqlConcat(' ' . $text, self::MODE_CONTAINS);
185 $tests[] = $field . self::makeSqlConcat('-' . $text, self::MODE_CONTAINS);
186 }
187 return $tests;
188 }
189
190 // {{{ function getAutoComplete
191 public function getAutoComplete($text)
192 {
193 $text = str_replace(array('%', '_'), '', $text);
194
195 if (is_null($this->ac_where) || $this->ac_where == '') {
196 $where = '';
197 } else {
198 $where = $this->ac_where . ' AND ';
199 }
200
201 $tests = $this->mkTests($this->valfield, $text);
202 if (!is_null($this->valfield2)) {
203 $tests = array_merge($tests, $this->mkTests($this->valfield2, $text));
204 }
205
206 $where .= '(' . implode(' OR ', $tests) . ')';
207
208 return XDB::iterator('SELECT ' . $this->valfield . ' AS field'
209 . ($this->ac_distinct ? (', COUNT(DISTINCT ' . $this->ac_unique . ') AS nb') : '')
210 . ($this->ac_withid ? (', ' . $this->idfield . ' AS id') : '') . '
211 FROM ' . $this->from . '
212 ' . $this->ac_join . '
213 WHERE ' . $where . '
214 GROUP BY ' . $this->valfield . '
215 ORDER BY ' . ($this->ac_distinct ? 'nb DESC' : $this->valfield) . '
216 LIMIT 11');
217 }
218 // }}}
219
220 // {{{ function makeSqlConcat
221 static protected function makeSqlConcat($text, $mode)
222 {
223 if ($mode == self::MODE_EXACT) {
224 return ' = ' . XDB::format('{?}', $text);
225 }
226 if ($mode == self::MODE_PREFIX) {
227 $right = XDB::format('CONCAT({?}, \'%\')', $text);
228 } else if ($mode == self::MODE_SUFFIX) {
229 $right = XDB::format('CONCAT(\'%\', {?})', $text);
230 } else {
231 $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $text);
232 }
233 return ' LIKE ' . $right;
234 }
235 // }}}
236
237 // {{{ function loadOptions
238 /** The function used to load options
239 */
240 protected function loadOptions()
241 {
242 $this->options = XDB::iterator('SELECT ' . $this->valfield . ' AS field,
243 ' . $this->idfield . ' AS id
244 FROM ' . $this->from . '
245 ' . $this->join . '
246 ' . $this->where . '
247 GROUP BY ' . $this->valfield . '
248 ORDER BY ' . $this->valfield);
249 }
250 // }}}
251 }
252 // }}}
253
254 // {{{ class DE_Binets
255 class DE_Binets extends DirEnumeration
256 {
257 protected $from = 'binets_def';
258
259 protected $ac_join = 'INNER JOIN binets_ins ON (binets_def.id = binets_ins.binet_id)';
260 protected $ac_unique = 'binets_ins.user_id';
261 }
262 // }}}
263
264 // {{{ class DE_Sections
265 class DE_Sections extends DirEnumeration
266 {
267 protected $from = 'sections';
268
269 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = sections.id)';
270 protected $ac_unique = 'profiles.pid';
271 }
272 // }}}
273
274 // {{{ class DE_Schools
275 class DE_Schools extends DirEnumeration
276 {
277 protected $valfield = 'name';
278 protected $valfield2 = 'abbreviation';
279 protected $from = 'profile_education_enum';
280
281 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
282 protected $ac_unique = 'profile_education.uid';
283 }
284 // }}}
285
286 // {{{ class DE_Degrees
287 class DE_Degrees extends DirEnumeration
288 {
289 protected $from = 'profile_education_degree_enum';
290 protected $valfield = 'degree';
291
292 protected $suboptions = array();
293
294 protected function loadOptions()
295 {
296 $res = XDB::query('SELECT ped.eduid, ped.degreeid, pede.degree
297 FROM profile_education_enum AS pee
298 LEFT JOIN profile_education_degree AS ped ON (pee.id = ped.eduid)
299 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
300 ORDER BY pede.degree');
301 foreach($res->fetchAllRow() as $row) {
302 list($eduid, $degreeid, $name) = $row;
303 $this->options[$degreeid] = array('id' => $degreeid, 'field' => $name);
304 if (!array_key_exists($eduid, $this->suboptions)) {
305 $this->suboptions[$eduid] = array();
306 }
307 $this->suboptions[$eduid][] = array('id' => $degreeid, 'field' => $name);
308 }
309 }
310
311 public function getOptions($eduid = null)
312 {
313 if ($eduid == null) {
314 return PlIteratorUtils::fromArray($this->options, 1, true);
315 }
316 if (array_key_exists($eduid, $this->suboptions)) {
317 return PlIteratorUtils::fromArray($this->suboptions[$eduid], 1, true);
318 } else {
319 return array();
320 }
321 }
322
323 public function getIDs($text, $mode, $eduid = null)
324 {
325 if ($eduid == null) {
326 return XDB::fetchColumn('SELECT id
327 FROM profile_education_degree_enum
328 WHERE degree ' . self::makeSqlConcat($text, $mode));
329 } else {
330 return XDB::fetchColumn('SELECT pede.id
331 FROM profile_education_degree AS ped
332 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
333 WHERE ped.eduid = {?} AND pede.degree ' . self::makeSqlConcat($text, $mode), $eduid);
334 }
335 }
336 }
337 // }}}
338
339 // {{{ class DE_StudiesSector
340 class DE_StudiesSector extends DirEnumeration
341 {
342 protected $valfield = 'field';
343 protected $from = 'profile_education_field_enum';
344
345 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
346 protected $ac_unique = 'profile_education.uid';
347 }
348 // }}}
349
350 // {{{ class DE_Nationalities
351 class DE_Nationalities extends DirEnumeration
352 {
353 protected $idfield = 'iso_3166_1_a2';
354 protected $valfield = 'nationalityFR';
355 protected $valfield2 = 'nationality';
356 protected $from = 'geoloc_countries AS gc';
357 protected $join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
358
359 protected $ac_join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
360 protected $ac_unique = 'profiles.pid';
361 }
362 // }}}
363
364 // {{{ class DE_Countries
365 class DE_Countries extends DirEnumeration
366 {
367 protected $idfield = 'iso_3166_1_a2';
368 protected $valfield = 'countryFR';
369 protected $valfield2 = 'country';
370 protected $from = 'geoloc_countries';
371
372 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryFR';
373 protected $ac_unique = 'profile_addresses.pid';
374 }
375 // }}}
376
377 // {{{ class DE_AdminAreas
378 class DE_AdminAreas extends DirEnumeration
379 {
380 protected $suboptions = array();
381
382 protected function loadOptions()
383 {
384 $res = XDB::query('SELECT id, name AS field, country
385 FROM geoloc_administrativeareas
386 GROUP BY name
387 ORDER BY name');
388 foreach($res->fetchAllRow() as $row) {
389 list($id, $field, $country) = $row;
390 $this->options[] = array('id' => $id, 'field' => $field);
391 if (!array_key_exists($country, $this->suboptions)) {
392 $this->suboptions[$country] = array();
393 }
394 $this->suboptions[$country][] = array('id' => $id, 'field' => $field);
395 }
396 }
397
398 public function getOptions($country = null)
399 {
400 if ($country == null) {
401 return PlIteratorUtils::fromArray($this->options, 1, true);
402 }
403 if (array_key_exists($country, $this->suboptions)) {
404 return PlIteratorUtils::fromArray($this->suboptions[$country], 1, true);
405 } else {
406 return array();
407 }
408 }
409
410 public function getIDs($text, $mode, $country = null)
411 {
412 if ($country == null) {
413 return XDB::fetchColumn('SELECT id
414 FROM geoloc_administrativeareas
415 WHERE name ' . self::makeSqlConcat($text, $mode));
416 } else {
417 return XDB::fetchColumn('SELECT id
418 FROM geoloc_administrativeareas
419 WHERE country = {?} AND name' . self::makeSqlConcat($text, $mode), $country);
420 }
421 }
422 }
423 // }}}
424
425 // {{{ class DE_GroupesX
426 class DE_GroupesX extends DirEnumeration
427 {
428 protected $idfield = 'asso.id';
429 protected $valfield = 'asso.nom';
430 protected $valfield2 = 'asso.diminutif';
431 protected $from = '#groupex#.asso AS asso';
432 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
433
434 protected $ac_join = "INNER JOIN #groupex#.membres AS memb ON (asso.id = memb.asso_id
435 AND (asso.cat = 'GroupesX' OR asso.cat = 'Institutions')
436 AND asso.pub = 'public')";
437 protected $ac_unique = 'memb.uid';
438 }
439 // }}}
440
441 // {{{ class DE_Sectors
442 class DE_Sectors extends DirEnumeration
443 {
444 protected $valfield = 'name';
445 protected $from = 'profile_job_sector_enum';
446
447 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
448 protected $ac_unique = 'profile_job.uid';
449 }
450 // }}}
451
452 // {{{ class DE_Networking
453 class DE_Networking extends DirEnumeration
454 {
455 protected $idfield = 'profile_networking_enum.network_type';
456 protected $valfield = 'profile_networking_enum.name';
457 protected $from = 'profile_networking_enum';
458
459
460 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.network_type = profile_networking_enum.network_type';
461 protected $ac_unique = 'profile_networking.uid';
462 }
463 // }}}
464 ?>