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