2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
23 /** This class stores all data for the different kinds of fields.
24 * It is only a dispatcher for the various DirEnum_XXX classes.
28 /** Name of availables Enumerations
29 * Each of these consts contains the basename of the class (its full name
30 * being DE_$basename).
32 const NAMETYPES
= 'nametypes';
33 const NAMES
= 'names';
35 const BINETS
= 'binets';
36 const GROUPESX
= 'groupesx';
37 const SECTIONS
= 'sections';
39 const EDUSCHOOLS
= 'educationschools';
40 const EDUDEGREES
= 'educationdegrees';
41 const EDUFIELDS
= 'educationfields';
43 const CORPS
= 'corps';
44 const CORPSRANKS
= 'corpsranks';
46 const NATIONALITIES
= 'nationalities';
47 const COUNTRIES
= 'countries';
48 const ADMINAREAS
= 'adminareas';
49 const LOCALITIES
= 'localities';
51 const COMPANIES
= 'companies';
52 const SECTORS
= 'sectors';
53 const JOBDESCRIPTION
= 'jobdescription';
55 const NETWORKS
= 'networking';
57 const MEDALS
= 'medals';
59 static private $enumerations = array();
61 static private function init($type)
63 if (S
::has('__DE_' . $type)) {
64 self
::$enumerations[$type] = S
::v('__DE_' . $type);
66 $cls = "DE_" . ucfirst($type);
68 self
::$enumerations[$type] = $obj;
69 if ($obj->capabilities
& DirEnumeration
::SAVE_IN_SESSION
) {
70 S
::set('__DE_' . $type, $obj);
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
79 static public function getOptions($type)
81 if (!array_key_exists($type, self
::$enumerations)) {
84 $obj = self
::$enumerations[$type];
85 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
86 return call_user_func(array($obj, 'getOptions'));
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
96 static public function getOptionsIter($type)
98 if (!array_key_exists($type, self
::$enumerations)) {
101 $obj = self
::$enumerations[$type];
102 $args = func_get_args();
104 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
105 return call_user_func_array(array($obj, 'getOptionsIter'), $args);
107 return PlIteratorUtils
::fromArray(array());
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
116 static public function getAutoComplete($type, $text)
118 if (!array_key_exists($type, self
::$enumerations)) {
121 $obj = self
::$enumerations[$type];
122 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
123 return call_user_func(array($obj, 'getAutoComplete'), $text);
125 return PlIteratorUtils
::fromArray(array());
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)
134 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
136 if (!array_key_exists($type, self
::$enumerations)) {
139 $obj = self
::$enumerations[$type];
140 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
141 return call_user_func(array($obj, 'getIDs'), $text, $mode);
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)
152 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
154 $ids = self
::getIDs($type, $text, $mode);
155 return array_shift($ids);
160 // {{{ class DirEnumeration
161 abstract class DirEnumeration
163 const AUTOCOMPLETE_LIMIT
= 11;
165 const HAS_OPTIONS
= 0x001;
166 const HAS_AUTOCOMP
= 0x002;
167 const SAVE_IN_SESSION
= 0x004;
169 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
171 /** An internal array of ID => optionTxt
173 protected $options = null
;
175 /** Description of the MySQL storage of the fields
177 protected $idfield = 'id';
178 protected $valfield = 'text';
179 protected $valfield2 = null
;
181 protected $join = '';
182 protected $where = '';
184 /** Fields for autocompletion
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 ?
193 protected function _fetchOptions()
195 if (is_null($this->options
)) {
196 $this->loadOptions();
200 public function getOptions()
202 $this->_fetchOptions();
203 return $this->options
;
206 public function getOptionsIter()
208 $options = $this->getOptions();
209 $options = self
::expandArray($options);
210 return PlIteratorUtils
::fromArray($options, 1, true
);
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
219 public function getIDs($text, $mode)
221 if ($mode == XDB
::WILDCARD_EXACT
) {
222 $options = $this->getOptions();
223 return array_keys($options, $text);
225 if ($this->where
== null
) {
228 $where = $this->where
. ' AND ';
231 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
232 if ($this->valfield2
!= null
) {
233 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
235 $where .= '(' . implode(' OR ', $conds) . ')';
237 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
238 FROM ' . $this->from
. '
241 GROUP BY ' . $this->idfield
);
246 /** Builds a list of query parts for searching @$text in @$field :
247 * field LIKE 'text%', field LIKE '% text%', field LIKE '%-text%'
249 private function mkTests($field, $text)
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);
260 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
263 foreach ($tab as $key => $val) {
272 // {{{ function getAutoComplete
273 public function getAutoComplete($text)
275 $text = str_replace(array('%', '_'), '', $text);
277 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
280 $where = $this->ac_where
. ' AND ';
283 $tests = $this->mkTests($this->valfield
, $text);
284 if (!is_null($this->valfield2
)) {
285 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
288 $where .= '(' . implode(' OR ', $tests) . ')';
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
. '
296 GROUP BY ' . $this->valfield
. '
297 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
298 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
302 // {{{ function loadOptions
303 /** The function used to load options
305 protected function loadOptions()
307 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
308 ' . $this->idfield
. ' AS id
309 FROM ' . $this->from
. '
312 GROUP BY ' . $this->valfield
. '
313 ORDER BY ' . $this->valfield
);
319 // {{{ class DE_WithSuboption
320 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
322 abstract class DE_WithSuboption
extends DirEnumeration
326 protected $suboptions = null
;
328 protected function loadOptions()
330 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
331 ' . $this->optfield
. ' AS subid,
332 ' . $this->idfield
. ' AS id
333 FROM ' . $this->from
. '
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();
345 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
349 public function getOptions($subid = null
)
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];
361 public function getOptionsIter($subid = null
)
363 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
366 public function getIDs($text, $mode, $subid = null
)
368 if ($mode == XDB
::WILDCARD_EXACT
) {
369 $options = $this->getOptions($subid);
370 return array_keys($options, $text);
372 if ($this->where
== null
) {
375 $where = $this->where
. ' AND ';
377 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
378 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
382 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
383 if ($this->valfield2
!= null
) {
384 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
386 $where .= '(' . implode(' OR ', $conds) . ')';
388 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
389 FROM ' . $this->from
. '
392 GROUP BY ' . $this->idfield
);
396 public function getAutoComplete($text, $subid = null
)
398 $text = str_replace(array('%', '_'), '', $text);
400 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
403 $where = $this->ac_where
. ' AND ';
406 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
407 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
410 $tests = $this->mkTests($this->valfield
, $text);
411 if (!is_null($this->valfield2
)) {
412 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
415 $where .= '(' . implode(' OR ', $tests) . ')';
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
. '
423 GROUP BY ' . $this->valfield
. '
424 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
425 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
430 // {{{ class DE_NameTypes
431 // returns 'system' names ('lastname', 'lastname_marital', ...)
432 class DE_NameTypes
extends DirEnumeration
434 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
436 protected $from = 'profile_name_enum';
437 protected $valfield = 'type';
441 // {{{ class DE_Names
442 // returns 'system' names ('lastname', 'lastname_marital', ...)
443 class DE_Names
extends DirEnumeration
445 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
447 protected $from = 'profile_name_enum';
448 protected $idfield = 'type';
449 protected $valfield = 'name';
455 // {{{ class DE_Binets
456 class DE_Binets
extends DirEnumeration
458 protected $from = 'profile_binet_enum';
460 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
461 protected $ac_unique = 'profile_binets.pid';
465 // {{{ class DE_Sections
466 class DE_Sections
extends DirEnumeration
468 protected $from = 'profile_section_enum';
470 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
471 protected $ac_unique = 'profiles.pid';
475 // {{{ class DE_GroupesX
476 class DE_GroupesX
extends DirEnumeration
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\'';
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';
493 // {{{ class DE_EducationSchools
494 class DE_EducationSchools
extends DirEnumeration
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';
501 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
502 protected $ac_unique = 'profile_education.pid';
506 // {{{ class DE_EducationDegrees
507 class DE_EducationDegrees
extends DirEnumeration
509 public $capabilities = self
::HAS_OPTIONS
;
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)';
520 // {{{ class DE_EducationFields
521 class DE_EducationFields
extends DirEnumeration
523 protected $valfield = 'profile_education_field_enum.field';
524 protected $from = 'profile_education_field_enum';
526 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
527 protected $ac_unique = 'profile_education.pid';
531 // {{{ class DE_Corps
532 class DE_Corps
extends DirEnumeration
534 protected $idfield = 'corps_enum.id';
535 protected $valfield = 'corps_enum.name';
536 protected $valfield2 = 'corps_enum.abbrev';
537 protected $from = 'corps_enum';
539 protected $ac_unique = 'corps.pid';
540 protected $ac_join = 'INNER JOIN corps ON (corps.current_corpsid = corps_enum.id)';
544 // {{{ class DE_CorpsRanks
545 class DE_CorpsRanks
extends DirEnumeration
547 protected $idfield = 'corps_rank_enum.id';
548 protected $valfield = 'corps_rank_enum.name';
549 protected $valfield2 = 'corps_rank_enum.abbrev';
550 protected $from = 'corps_rank_enum';
552 protected $ac_unique = 'corps.pid';
553 protected $ac_join = 'INNER JOIN corps ON (corps.rankid = corps_rank_enum.id)';
559 // {{{ class DE_Nationalities
560 class DE_Nationalities
extends DirEnumeration
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))';
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';
573 // {{{ class DE_Countries
574 class DE_Countries
extends DirEnumeration
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';
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';
586 // {{{ class DE_AdminAreas
587 class DE_AdminAreas
extends DE_WithSuboption
589 protected $idfield = 'geoloc_administrativeareas.id';
590 protected $optfield = 'geoloc_administrativeareas.country';
591 protected $valfield = 'geoloc_administrativeareas.name';
592 protected $from = 'geoloc_administrativeareas';
594 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
595 protected $ac_unique = 'profile_addresses.pid';
599 // {{{ class DE_Localities
600 class DE_Localities
extends DirEnumeration
602 protected $idfield = 'geoloc_localities.id';
603 protected $valfield = 'geoloc_localities.name';
604 protected $from = 'geoloc_localities';
606 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
607 protected $ac_unique = 'profile_addresses.pid';
613 // {{{ class DE_Companies
614 class DE_Companies
extends DirEnumeration
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';
621 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
622 protected $ac_unique = 'profile_job.pid';
626 // {{{ class DE_Sectors
627 class DE_Sectors
extends DirEnumeration
629 protected $idfield = 'profile_job_sector_enum.id';
630 protected $valfield = 'profile_job_sector_enum.name';
631 protected $from = 'profile_job_sector_enum';
633 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
634 protected $ac_unique = 'profile_job.pid';
638 // {{{ class DE_JobDescription
639 class DE_JobDescription
extends DirEnumeration
641 protected $valfield = 'profile_job.description';
642 protected $from = 'profile_job';
643 protected $idfield = 'profile_job.pid';
645 protected $ac_unique = 'profile_job.pid';
651 // {{{ class DE_Networking
652 class DE_Networking
extends DirEnumeration
654 protected $idfield = 'profile_networking_enum.nwid';
655 protected $valfield = 'profile_networking_enum.name';
656 protected $from = 'profile_networking_enum';
659 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
660 protected $ac_unique = 'profile_networking.pid';
666 // {{{ class DE_Medals
667 class DE_Medals
extends DirEnumeration
669 protected $from = 'profile_medal_enum';
671 protected $ac_join = 'INNER JOIN profile_medals ON (profile_medals.mid = profile_medal_enum.id)';
672 protected $ac_unique = 'profile_medals.pid';
676 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: