Fix autocompletion.
[platal.git] / classes / direnum.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 NAMETYPES = 'nametypes';
33
34 const BINETS = 'binets';
35 const GROUPESX = 'groupesx';
36 const SECTIONS = 'sections';
37
38 const EDUSCHOOLS = 'educationschools';
39 const EDUDEGREES = 'educationdegrees';
40 const EDUFIELDS = 'educationfields';
41
42 const NATIONALITIES = 'nationalities';
43 const COUNTRIES = 'countries';
44 const ADMINAREAS = 'adminareas';
45 const LOCALITIES = 'localities';
46
47 const COMPANIES = 'companies';
48 const SECTORS = 'sectors';
49 const JOBDESCRIPTION = 'jobdescription';
50
51 const NETWORKS = 'networking';
52
53 static private $enumerations = array();
54
55 static private function init($type)
56 {
57 if (S::has('__DE_' . $type)) {
58 self::$enumerations[$type] = S::v('__DE_' . $type);
59 } else {
60 $cls = "DE_" . ucfirst($type);
61 $obj = new $cls();
62 self::$enumerations[$type] = $obj;
63 if ($obj->capabilities & DirEnumeration::SAVE_IN_SESSION) {
64 S::set('__DE_' . $type, $obj);
65 }
66 }
67 }
68
69 /** Retrieves all options for a given type
70 * @param $type Type of enum for which options are requested
71 * @return Array of the results
72 */
73 static public function getOptions($type)
74 {
75 if (!array_key_exists($type, self::$enumerations)) {
76 self::init($type);
77 }
78 $obj = self::$enumerations[$type];
79 if ($obj->capabilities & DirEnumeration::HAS_OPTIONS) {
80 return call_user_func(array($obj, 'getOptions'));
81 } else {
82 return array();
83 }
84 }
85
86 /** Retrieves all options for a given type
87 * @param $type Type of enum for which options are requested
88 * @return PlIterator over the results
89 */
90 static public function getOptionsIter($type)
91 {
92 if (!array_key_exists($type, self::$enumerations)) {
93 self::init($type);
94 }
95 $obj = self::$enumerations[$type];
96 if ($obj->capabilities & DirEnumeration::HAS_OPTIONS) {
97 return call_user_func(array($obj, 'getOptionsIter'));
98 } else {
99 return PlIteratorUtils::fromArray(array());
100 }
101 }
102
103 /** Retrieves all options with number of profiles for autocompletion
104 * @param $type Type of enum for which options are requested
105 * @param $text Text to autocomplete
106 * @return PlIterator over the results
107 */
108 static public function getAutoComplete($type, $text)
109 {
110 if (!array_key_exists($type, self::$enumerations)) {
111 self::init($type);
112 }
113 $obj = self::$enumerations[$type];
114 if ($obj->capabilities & DirEnumeration::HAS_AUTOCOMP) {
115 return call_user_func(array($obj, 'getAutoComplete'), $text);
116 } else {
117 return PlIteratorUtils::fromArray(array());
118 }
119 }
120
121 /** Retrieves a list of IDs for a given type
122 * @param $type Type of enum for which IDs are requested
123 * @param $text Text to search in enum valuees
124 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
125 */
126 static public function getIDs($type, $text, $mode = XDB::WILDCARD_EXACT)
127 {
128 if (!array_key_exists($type, self::$enumerations)) {
129 self::init($type);
130 }
131 $obj = self::$enumerations[$type];
132 if ($obj->capabilities & DirEnumeration::HAS_OPTIONS) {
133 return call_user_func(array($obj, 'getIDs'), $text, $mode);
134 } else {
135 return array();
136 }
137 }
138
139 /** Retrieves a single ID for a given type.
140 * @param $type Type of the enum for which an ID is requested
141 * @param $text Text to search in enum values
142 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
143 */
144 static public function getID($type, $text, $mode = XDB::WILDCARD_EXACT)
145 {
146 $ids = self::getIDs($type, $text, $mode);
147 return array_shift($ids);
148 }
149 }
150 // }}}
151
152 // {{{ class DirEnumeration
153 abstract class DirEnumeration
154 {
155 const AUTOCOMPLETE_LIMIT = 11;
156
157 const HAS_OPTIONS = 0x001;
158 const HAS_AUTOCOMP = 0x002;
159 const SAVE_IN_SESSION = 0x004;
160
161 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
162
163 /** An internal array of ID => optionTxt
164 */
165 protected $options = null;
166
167 /** Description of the MySQL storage of the fields
168 */
169 protected $idfield = 'id';
170 protected $valfield = 'text';
171 protected $valfield2 = null;
172 protected $from;
173 protected $join = '';
174 protected $where = '';
175
176 /** Fields for autocompletion
177 */
178 protected $ac_join = ''; // Additional joins
179 protected $ac_where = null; // Additional where
180 protected $ac_beginwith = true; // Whether to search for 'x%' or for '%x%'
181 protected $ac_unique; // Which field is to be taken as unique
182 protected $ac_distinct = true; // Whether we want to keep only distinct valfield value
183 protected $ac_withid = true; // Do we want to fetch id too ?
184
185 protected function _fetchOptions()
186 {
187 if (is_null($this->options)) {
188 $this->loadOptions();
189 }
190 }
191
192 public function getOptions()
193 {
194 $this->_fetchOptions();
195 return $this->options;
196 }
197
198 public function getOptionsIter()
199 {
200 return PlIteratorUtils::fromArray(self::expandArray($this->getOptions()), 1, true);
201 }
202
203 // {{{ function getIDs
204 /** Retrieves possible IDs for given text
205 * @param $text Text to search for IDs
206 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
207 * @return An array of matching IDs ; if empty, input should be considered invalid
208 */
209 public function getIDs($text, $mode)
210 {
211 if ($mode == XDB::WILDCARD_EXACT) {
212 $options = $this->getOptions();
213 return array_keys($options, $text);
214 } else {
215 if ($this->where == null) {
216 $where = 'WHERE ';
217 } else {
218 $where = $this->where . ' AND ';
219 }
220 $conds = array();
221 $conds[] = $this->valfield . XDB::formatWildcards($mode, $text);
222 if ($this->valfield2 != null) {
223 $conds[] = $this->valfield2 . XDB::formatWildcards($mode, $text);
224 }
225 $where .= '(' . implode(' OR ', $conds) . ')';
226
227 return XDB::fetchColumn('SELECT ' . $this->idfield . '
228 FROM ' . $this->from . '
229 ' . $this->join . '
230 ' . $where . '
231 GROUP BY ' . $this->idfield);
232 }
233 }
234 // }}}
235
236 private function mkTests($field, $text)
237 {
238 $tests = array();
239 $tests[] = $field . XDB::formatWildcards(XDB::WILDCARD_PREFIX, $text);
240 if (!$this->ac_beginwith) {
241 $tests[] = $field . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, ' ' . $text);
242 $tests[] = $field . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, '-' . $text);
243 }
244 return $tests;
245 }
246
247 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
248 {
249 $res = array();
250 foreach ($tab as $key => $val) {
251 $res[$key] = array(
252 $keyname => $key,
253 $valname => $val,
254 );
255 }
256 return $res;
257 }
258
259 // {{{ function getAutoComplete
260 public function getAutoComplete($text)
261 {
262 $text = str_replace(array('%', '_'), '', $text);
263
264 if (is_null($this->ac_where) || $this->ac_where == '') {
265 $where = '';
266 } else {
267 $where = $this->ac_where . ' AND ';
268 }
269
270 $tests = $this->mkTests($this->valfield, $text);
271 if (!is_null($this->valfield2)) {
272 $tests = array_merge($tests, $this->mkTests($this->valfield2, $text));
273 }
274
275 $where .= '(' . implode(' OR ', $tests) . ')';
276
277 return XDB::iterator('SELECT ' . $this->valfield . ' AS field'
278 . ($this->ac_distinct ? (', COUNT(DISTINCT ' . $this->ac_unique . ') AS nb') : '')
279 . ($this->ac_withid ? (', ' . $this->from . '.' . $this->idfield . ' AS id') : '') . '
280 FROM ' . $this->from . '
281 ' . $this->ac_join . '
282 WHERE ' . $where . '
283 GROUP BY ' . $this->valfield . '
284 ORDER BY ' . ($this->ac_distinct ? 'nb DESC' : $this->valfield) . '
285 LIMIT ' . self::AUTOCOMPLETE_LIMIT);
286 }
287 // }}}
288
289 // {{{ function loadOptions
290 /** The function used to load options
291 */
292 protected function loadOptions()
293 {
294 $this->options = XDB::fetchAllAssoc('id', 'SELECT ' . $this->valfield . ' AS field,
295 ' . $this->idfield . ' AS id
296 FROM ' . $this->from . '
297 ' . $this->join . '
298 ' . $this->where . '
299 GROUP BY ' . $this->valfield . '
300 ORDER BY ' . $this->valfield);
301 }
302 // }}}
303 }
304 // }}}
305
306 // {{{ class DE_WithSuboption
307 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
308 */
309 abstract class DE_WithSuboption extends DirEnumeration
310 {
311 protected $optfield;
312
313 protected $suboptions = null;
314
315 protected function loadOptions()
316 {
317 $opts = XDB::fetchAllAssoc('id', 'SELECT ' . $this->valfield . ' AS field,
318 ' . $this->optfield . ' AS subid,
319 ' . $this->idfield . ' AS id
320 FROM ' . $this->from . '
321 ' . $this->join . '
322 ' . $this->where . '
323 GROUP BY ' . $this->valfield . '
324 ORDER BY ' . $this->valfield);
325 $this->options = array();
326 $this->suboptions = array();
327 foreach ($opts as $id => $opt) {
328 $this->options[$id] = $opt['field'];
329 if (!array_key_exists($opt['subid'], $this->suboptions)) {
330 $this->suboptions[$opt['subid']] = array();
331 }
332 $this->suboptions[$opt['subid']][$id] = $opt['field'];
333 }
334 }
335
336 public function getOptions($subid = null)
337 {
338 $this->_fetchOptions();
339 if ($subid == null) {
340 return $this->options;
341 } else if (array_key_exists($subid, $this->suboptions)) {
342 return $this->suboptions[$subid];
343 } else {
344 return false;
345 }
346 }
347
348 public function getOptionsIter($subid = null)
349 {
350 return PlIteratorUtils::fromArray(self::expandArray($this->getOptions($subid)), 1, true);
351 }
352
353 public function getIDs($text, $mode, $subid = null)
354 {
355 if ($mode == XDB::WILDCARD_EXACT) {
356 $options = $this->getOptions($subid);
357 return array_keys($options, $text);
358 } else {
359 if ($this->where == null) {
360 $where = 'WHERE ';
361 } else {
362 $where = $this->where . ' AND ';
363 }
364 if ($subid != null && array_key_exists($subid, $this->suboptions)) {
365 $where .= XDB::format($this->optfield . ' = {?} AND ', $subid);
366 }
367
368 $conds = array();
369 $conds[] = $this->valfield . XDB::formatWildcards($mode, $text);
370 if ($this->valfield2 != null) {
371 $conds[] = $this->valfield2 . XDB::formatWildcards($mode, $text);
372 }
373 $where .= '(' . implode(' OR ', $conds) . ')';
374
375 return XDB::fetchColumn('SELECT ' . $this->idfield . '
376 FROM ' . $this->from . '
377 ' . $this->join . '
378 ' . $where . '
379 GROUP BY ' . $this->idfield);
380 }
381 }
382
383 public function getAutoComplete($text, $subid = null)
384 {
385 $text = str_replace(array('%', '_'), '', $text);
386
387 if (is_null($this->ac_where) || $this->ac_where == '') {
388 $where = '';
389 } else {
390 $where = $this->ac_where . ' AND ';
391 }
392
393 if ($subid != null && array_key_exists($subid, $this->suboptions)) {
394 $where .= XDB::format($this->optfield . ' = {?} AND ', $subid);
395 }
396
397 $tests = $this->mkTests($this->valfield, $text);
398 if (!is_null($this->valfield2)) {
399 $tests = array_merge($tests, $this->mkTests($this->valfield2, $text));
400 }
401
402 $where .= '(' . implode(' OR ', $tests) . ')';
403
404 return XDB::iterator('SELECT ' . $this->valfield . ' AS field'
405 . ($this->ac_distinct ? (', COUNT(DISTINCT ' . $this->ac_unique . ') AS nb') : '')
406 . ($this->ac_withid ? (', ' . $this->idfield . ' AS id') : '') . '
407 FROM ' . $this->from . '
408 ' . $this->ac_join . '
409 WHERE ' . $where . '
410 GROUP BY ' . $this->valfield . '
411 ORDER BY ' . ($this->ac_distinct ? 'nb DESC' : $this->valfield) . '
412 LIMIT ' . self::AUTOCOMPLETE_LIMIT);
413 }
414 }
415 // }}}
416
417 // {{{ class DE_NameTypes
418 // returns 'system' names ('lastname', 'lastname_marital', ...)
419 class DE_NameTypes extends DirEnumeration
420 {
421 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
422
423 protected $from = 'profile_name_enum';
424 protected $valfield = 'type';
425 }
426 // }}}
427
428 /** GROUPS
429 */
430 // {{{ class DE_Binets
431 class DE_Binets extends DirEnumeration
432 {
433 protected $from = 'profile_binet_enum';
434
435 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
436 protected $ac_unique = 'profile_binets.pid';
437 }
438 // }}}
439
440 // {{{ class DE_Sections
441 class DE_Sections extends DirEnumeration
442 {
443 protected $from = 'profile_section_enum';
444
445 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
446 protected $ac_unique = 'profiles.pid';
447 }
448 // }}}
449
450 // {{{ class DE_GroupesX
451 class DE_GroupesX extends DirEnumeration
452 {
453 protected $idfield = 'groups.id';
454 protected $valfield = 'groups.nom';
455 protected $valfield2 = 'groups.diminutif';
456 protected $from = 'groups';
457 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
458
459 protected $ac_join = "INNER JOIN group_members ON (groups.id = memb.asso_id
460 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
461 AND groups.pub = 'public')";
462 protected $ac_unique = 'group_members.uid';
463 }
464 // }}}
465
466 /** EDUCATION
467 */
468 // {{{ class DE_EducationSchools
469 class DE_EducationSchools extends DirEnumeration
470 {
471 protected $valfield = 'profile_education_enum.name';
472 protected $valfield2 = 'profile_education_enum.abbreviation';
473 protected $from = 'profile_education_enum';
474
475 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
476 protected $ac_unique = 'profile_education.pid';
477 }
478 // }}}
479
480 // {{{ class DE_EducationDegrees
481 class DE_EducationDegrees extends DirEnumeration
482 {
483 public $capabilities = self::HAS_OPTIONS;
484
485 protected $idfield = 'profile_education_degree.degreeid';
486 protected $optfield = 'profile_education_degree.eduid';
487 protected $valfield = 'profile_education_degree_enum.degree';
488 protected $from = 'profile_education_degree_enum';
489 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
490
491 }
492 // }}}
493
494 // {{{ class DE_EducationFields
495 class DE_EducationFields extends DirEnumeration
496 {
497 protected $valfield = 'profile_education_field_enum.field';
498 protected $from = 'profile_education_field_enum';
499
500 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
501 protected $ac_unique = 'profile_education.pid';
502 }
503 // }}}
504
505 /** GEOLOC
506 */
507 // {{{ class DE_Nationalities
508 class DE_Nationalities extends DirEnumeration
509 {
510 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
511 protected $valfield = 'geoloc_countries.nationalityFR';
512 protected $valfield2 = 'geoloc_countries.nationality';
513 protected $from = 'geoloc_countries';
514 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
515
516 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
517 protected $ac_unique = 'profiles.pid';
518 }
519 // }}}
520
521 // {{{ class DE_Countries
522 class DE_Countries extends DirEnumeration
523 {
524 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
525 protected $valfield = 'geoloc_countries.countryFR';
526 protected $valfield2 = 'geoloc_countries.country';
527 protected $from = 'geoloc_countries';
528
529 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryFR';
530 protected $ac_unique = 'profile_addresses.pid';
531 }
532 // }}}
533
534 // {{{ class DE_AdminAreas
535 class DE_AdminAreas extends DE_WithSuboption
536 {
537 protected $idfield = 'geoloc_administrativeareas.id';
538 protected $optfield = 'geoloc_administrativeareas.country';
539 protected $valfield = 'geoloc_administrativeareas.name';
540 protected $from = 'geoloc_administrativeareas';
541
542 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
543 protected $ac_unique = 'profile_addresses.pid';
544 }
545 // }}}
546
547 // {{{ class DE_Localities
548 class DE_Localities extends DirEnumeration
549 {
550 protected $valfield = 'geoloc_localities.name';
551 protected $from = 'geoloc_localities';
552
553 protected $ac_join = 'profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
554 protected $ac_unique = 'profile_addresses.pid';
555 }
556 // }}}
557
558 /** JOBS
559 */
560 // {{{ class DE_Companies
561 class DE_Companies extends DirEnumeration
562 {
563 protected $valfield = 'profile_job_enum.name';
564 protected $valfield2 = 'profile_job_enum.acronym';
565 protected $from = 'profile_job_enum';
566
567 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
568 protected $ac_unique = 'profile_job.pid';
569 }
570 // }}}
571
572 // {{{ class DE_Sectors
573 class DE_Sectors extends DirEnumeration
574 {
575 protected $valfield = 'profile_job_sector_enum.name';
576 protected $from = 'profile_job_sector_enum';
577
578 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
579 protected $ac_unique = 'profile_job.pid';
580 }
581 // }}}
582
583 // {{{ class DE_JobDescription
584 class DE_JobDescription
585 {
586 protected $valfield = 'profile_job.description';
587 protected $from = 'profile_job';
588 protected $idfield = 'profile_job.pid';
589
590 protected $ac_unique = 'profile_job.pid';
591 }
592 // }}}
593
594 /** NETWORKING
595 */
596 // {{{ class DE_Networking
597 class DE_Networking extends DirEnumeration
598 {
599 protected $idfield = 'profile_networking_enum.network_type';
600 protected $valfield = 'profile_networking_enum.name';
601 protected $from = 'profile_networking_enum';
602
603
604 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.network_type = profile_networking_enum.network_type';
605 protected $ac_unique = 'profile_networking.pid';
606 }
607 // }}}
608
609 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
610 ?>