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