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