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