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