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