2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 BINETS
= 'binets';
33 const GROUPESX
= 'groupesx';
34 const SECTIONS
= 'sections';
36 const EDUSCHOOLS
= 'educationschools';
37 const EDUDEGREES
= 'educationdegrees';
38 const EDUFIELDS
= 'educationfields';
40 const CURRENTCORPS
= 'currentcorps';
41 const ORIGINCORPS
= 'origincorps';
42 const CORPSRANKS
= 'corpsranks';
44 const NATIONALITIES
= 'nationalities';
45 const COUNTRIES
= 'countries';
46 const ADMINAREAS
= 'adminareas';
47 const SUBADMINAREAS
= 'subadminareas';
48 const LOCALITIES
= 'localities';
50 const COMPANIES
= 'companies';
51 const JOBDESCRIPTION
= 'jobdescription';
52 const JOBTERMS
= 'jobterms';
54 const NETWORKS
= 'networking';
56 const MEDALS
= 'medals';
58 const ACCOUNTTYPES
= 'accounttypes';
59 const SKINS
= 'skins';
61 static private $enumerations = array();
63 static private function init($type)
65 if (Platal
::globals()->cacheEnabled() && S
::has('__DE_' . $type)) {
66 self
::$enumerations[$type] = S
::v('__DE_' . $type);
68 $cls = "DE_" . ucfirst($type);
70 self
::$enumerations[$type] = $obj;
71 if (Platal
::globals()->cacheEnabled()
72 && $obj->capabilities
& DirEnumeration
::SAVE_IN_SESSION
) {
73 S
::set('__DE_' . $type, $obj);
78 /** Retrieves all options for a given type
79 * @param $type Type of enum for which options are requested
80 * @return Array of the results
82 static public function getOptions($type)
84 if (!array_key_exists($type, self
::$enumerations)) {
87 $obj = self
::$enumerations[$type];
88 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
89 return call_user_func(array($obj, 'getOptions'));
95 /** Retrieves all options for a given type
96 * @param $type Type of enum for which options are requested
97 * @return PlIterator over the results
99 static public function getOptionsIter($type)
101 if (!array_key_exists($type, self
::$enumerations)) {
104 $obj = self
::$enumerations[$type];
105 $args = func_get_args();
107 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
108 return call_user_func_array(array($obj, 'getOptionsIter'), $args);
110 return PlIteratorUtils
::fromArray(array());
114 /** Retrieves all options with number of profiles for autocompletion
115 * @param $type Type of enum for which options are requested
116 * @param $text Text to autocomplete
117 * @return PlIterator over the results
119 static public function getAutoComplete($type, $text)
121 if (!array_key_exists($type, self
::$enumerations)) {
124 $obj = self
::$enumerations[$type];
125 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
126 return call_user_func(array($obj, 'getAutoComplete'), $text);
128 return PlIteratorUtils
::fromArray(array());
132 /** Retrieves a list of IDs for a given type
133 * @param $type Type of enum for which IDs are requested
134 * @param $text Text to search in enum valuees
135 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
137 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
139 if (!array_key_exists($type, self
::$enumerations)) {
142 $obj = self
::$enumerations[$type];
143 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
144 return call_user_func(array($obj, 'getIDs'), $text, $mode);
150 /** Retrieves a single ID for a given type.
151 * @param $type Type of the enum for which an ID is requested
152 * @param $text Text to search in enum values
153 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
155 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
157 $ids = self
::getIDs($type, $text, $mode);
158 return array_shift($ids);
163 // {{{ class DirEnumeration
164 abstract class DirEnumeration
166 const AUTOCOMPLETE_LIMIT
= 11;
168 const HAS_OPTIONS
= 0x001;
169 const HAS_AUTOCOMP
= 0x002;
170 const SAVE_IN_SESSION
= 0x004;
172 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
174 /** An internal array of ID => optionTxt
176 protected $options = null
;
178 /** Description of the MySQL storage of the fields
180 protected $idfield = 'id';
181 protected $valfield = 'text';
182 protected $valfield2 = null
;
184 protected $join = '';
185 protected $where = '';
187 /** Fields for autocompletion
189 protected $ac_join = ''; // Additional joins
190 protected $ac_where = null
; // Additional where
191 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
192 protected $ac_unique; // Which field is to be taken as unique
193 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
194 protected $ac_withid = true
; // Do we want to fetch id too ?
196 protected function _fetchOptions()
198 if (is_null($this->options
)) {
199 $this->loadOptions();
203 public function getOptions()
205 $this->_fetchOptions();
206 return $this->options
;
209 public function getOptionsIter()
211 $options = $this->getOptions();
212 $options = self
::expandArray($options);
213 return PlIteratorUtils
::fromArray($options, 1, true
);
216 // {{{ function getIDs
217 /** Retrieves possible IDs for given text
218 * @param $text Text to search for IDs
219 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
220 * @return An array of matching IDs ; if empty, input should be considered invalid
222 public function getIDs($text, $mode)
224 if ($mode == XDB
::WILDCARD_EXACT
) {
225 $options = $this->getOptions();
226 return array_keys($options, $text);
228 if ($this->where
== null
) {
231 $where = $this->where
. ' AND ';
234 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
235 if ($this->valfield2
!= null
) {
236 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
238 $where .= '(' . implode(' OR ', $conds) . ')';
240 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
241 FROM ' . $this->from
. '
244 GROUP BY ' . $this->idfield
);
249 /** Builds a list of query parts for searching @$text in @$field :
250 * field LIKE 'text%', field LIKE '% text%', field LIKE '%-text%'
252 private function mkTests($field, $text)
255 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
256 if (!$this->ac_beginwith
) {
257 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
258 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
263 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
266 foreach ($tab as $key => $val) {
275 // {{{ function getAutoComplete
276 public function getAutoComplete($text)
278 $text = str_replace(array('%', '_'), '', $text);
280 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
283 $where = $this->ac_where
. ' AND ';
286 $tests = $this->mkTests($this->valfield
, $text);
287 if (!is_null($this->valfield2
)) {
288 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
291 $where .= '(' . implode(' OR ', $tests) . ')';
293 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
294 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
295 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
296 FROM ' . $this->from
. '
297 ' . $this->ac_join
. '
299 GROUP BY ' . $this->valfield
. '
300 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
301 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
305 // {{{ function loadOptions
306 /** The function used to load options
308 protected function loadOptions()
310 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
311 ' . $this->idfield
. ' AS id
312 FROM ' . $this->from
. '
315 GROUP BY ' . $this->valfield
. '
316 ORDER BY ' . $this->valfield
);
322 // {{{ class DE_WithSuboption
323 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
325 abstract class DE_WithSuboption
extends DirEnumeration
329 protected $suboptions = null
;
331 protected function loadOptions()
333 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
334 ' . $this->optfield
. ' AS subid,
335 ' . $this->idfield
. ' AS id
336 FROM ' . $this->from
. '
339 GROUP BY ' . $this->valfield
. '
340 ORDER BY ' . $this->valfield
);
341 $this->options
= array();
342 $this->suboptions
= array();
343 foreach ($opts as $id => $opt) {
344 $this->options
[$id] = $opt['field'];
345 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
346 $this->suboptions
[$opt['subid']] = array();
348 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
352 public function getOptions($subid = null
)
354 $this->_fetchOptions();
355 if ($subid == null
) {
356 return $this->options
;
357 } else if (array_key_exists($subid, $this->suboptions
)) {
358 return $this->suboptions
[$subid];
364 public function getOptionsIter($subid = null
)
366 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
369 public function getIDs($text, $mode, $subid = null
)
371 if ($mode == XDB
::WILDCARD_EXACT
) {
372 $options = $this->getOptions($subid);
373 return array_keys($options, $text);
375 if ($this->where
== null
) {
378 $where = $this->where
. ' AND ';
380 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
381 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
385 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
386 if ($this->valfield2
!= null
) {
387 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
389 $where .= '(' . implode(' OR ', $conds) . ')';
391 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
392 FROM ' . $this->from
. '
395 GROUP BY ' . $this->idfield
);
399 public function getAutoComplete($text, $subid = null
)
401 $text = str_replace(array('%', '_'), '', $text);
403 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
406 $where = $this->ac_where
. ' AND ';
409 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
410 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
413 $tests = $this->mkTests($this->valfield
, $text);
414 if (!is_null($this->valfield2
)) {
415 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
418 $where .= '(' . implode(' OR ', $tests) . ')';
420 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
421 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
422 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
423 FROM ' . $this->from
. '
424 ' . $this->ac_join
. '
426 GROUP BY ' . $this->valfield
. '
427 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
428 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
435 // {{{ class DE_Binets
436 class DE_Binets
extends DirEnumeration
438 protected $from = 'profile_binet_enum';
440 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
441 protected $ac_unique = 'profile_binets.pid';
445 // {{{ class DE_Sections
446 class DE_Sections
extends DirEnumeration
448 protected $from = 'profile_section_enum';
450 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
451 protected $ac_unique = 'profiles.pid';
455 // {{{ class DE_GroupesX
456 class DE_GroupesX
extends DirEnumeration
458 protected $idfield = 'groups.id';
459 protected $valfield = 'groups.nom';
460 protected $valfield2 = 'groups.diminutif';
461 protected $from = 'groups';
462 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
464 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
465 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
466 AND groups.pub = 'public')";
467 protected $ac_unique = 'group_members.uid';
473 // {{{ class DE_EducationSchools
474 class DE_EducationSchools
extends DirEnumeration
476 protected $ac_beginwith = false
;
477 protected $idfield = 'profile_education_enum.id';
478 protected $valfield = 'profile_education_enum.name';
479 protected $valfield2 = 'profile_education_enum.abbreviation';
480 protected $from = 'profile_education_enum';
482 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
483 protected $ac_unique = 'profile_education.pid';
487 // {{{ class DE_EducationDegrees
488 class DE_EducationDegrees
extends DirEnumeration
490 public $capabilities = self
::HAS_OPTIONS
;
492 protected $idfield = 'profile_education_degree.degreeid';
493 protected $optfield = 'profile_education_degree.eduid';
494 protected $valfield = 'profile_education_degree_enum.degree';
495 protected $from = 'profile_education_degree_enum';
496 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
501 // {{{ class DE_EducationFields
502 class DE_EducationFields
extends DirEnumeration
504 protected $valfield = 'profile_education_field_enum.field';
505 protected $from = 'profile_education_field_enum';
507 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
508 protected $ac_unique = 'profile_education.pid';
512 // {{{ class DE_CurrentCorps
513 class DE_CurrentCorps
extends DirEnumeration
515 protected $idfield = 'profile_corps_enum.id';
516 protected $valfield = 'profile_corps_enum.name';
517 protected $valfield2 = 'profile_corps_enum.abbrev';
518 protected $from = 'profile_corps_enum';
519 protected $where = 'WHERE profile_corps_enum.still_exists = 1';
521 protected $ac_unique = 'profile_corps.pid';
522 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.current_corpsid = profile_corps_enum.id)';
526 // {{{ class DE_OriginCorps
527 class DE_OriginCorps
extends DirEnumeration
529 protected $idfield = 'profile_corps_enum.id';
530 protected $valfield = 'profile_corps_enum.name';
531 protected $valfield2 = 'profile_corps_enum.abbrev';
532 protected $from = 'profile_corps_enum';
534 protected $ac_unique = 'profile_corps.pid';
535 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.original_corpsid = profile_corps_enum.id)';
539 // {{{ class DE_CorpsRanks
540 class DE_CorpsRanks
extends DirEnumeration
542 protected $idfield = 'profile_corps_rank_enum.id';
543 protected $valfield = 'profile_corps_rank_enum.name';
544 protected $valfield2 = 'profile_corps_rank_enum.abbrev';
545 protected $from = 'profile_corps_rank_enum';
547 protected $ac_unique = 'profile_corps.pid';
548 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.rankid = profile_corps_rank_enum.id)';
554 // {{{ class DE_Nationalities
555 class DE_Nationalities
extends DirEnumeration
557 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
558 protected $valfield = 'geoloc_countries.nationality';
559 protected $valfield2 = 'geoloc_countries.nationalityEn';
560 protected $from = 'geoloc_countries';
561 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
563 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
564 protected $ac_unique = 'profiles.pid';
568 // {{{ class DE_Countries
569 class DE_Countries
extends DirEnumeration
571 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
572 protected $valfield = 'geoloc_countries.country';
573 protected $valfield2 = 'geoloc_countries.countryEn';
574 protected $from = 'geoloc_countries';
576 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
577 protected $ac_unique = 'profile_addresses.pid';
578 protected $ac_where = 'profile_addresses.type = \'home\'';
582 // {{{ class DE_AdminAreas
583 class DE_AdminAreas
extends DE_WithSuboption
585 protected $idfield = 'geoloc_administrativeareas.id';
586 protected $optfield = 'geoloc_administrativeareas.country';
587 protected $valfield = 'geoloc_administrativeareas.name';
588 protected $from = 'geoloc_administrativeareas';
590 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
591 protected $ac_unique = 'profile_addresses.pid';
595 // {{{ class DE_SubAdminAreas
596 class DE_SubAdminAreas
extends DE_WithSuboption
598 protected $idfield = 'geoloc_subadministrativeareas.id';
599 protected $optfield = 'geoloc_subadministrativeareas.administrativearea';
600 protected $valfield = 'geoloc_subadministrativeareas.name';
601 protected $from = 'geoloc_subadministrativeareas';
603 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.subadministrativeAreaId = geoloc_subadministrativeareas.id)';
604 protected $ac_unique = 'profile_addresses.pid';
608 // {{{ class DE_Localities
609 class DE_Localities
extends DirEnumeration
611 protected $idfield = 'geoloc_localities.id';
612 protected $valfield = 'geoloc_localities.name';
613 protected $from = 'geoloc_localities';
615 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
616 protected $ac_unique = 'profile_addresses.pid';
622 // {{{ class DE_Companies
623 class DE_Companies
extends DirEnumeration
625 protected $idfield = 'profile_job_enum.id';
626 protected $valfield = 'profile_job_enum.name';
627 protected $valfield2 = 'profile_job_enum.acronym';
628 protected $from = 'profile_job_enum';
630 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
631 protected $ac_unique = 'profile_job.pid';
635 // {{{ class DE_JobDescription
636 class DE_JobDescription
extends DirEnumeration
638 protected $valfield = 'profile_job.description';
639 protected $from = 'profile_job';
640 protected $idfield = 'profile_job.pid';
642 protected $ac_unique = 'profile_job.pid';
646 // {{{ class DE_JobTerms
647 class DE_JobTerms
extends DirEnumeration
649 // {{{ function getAutoComplete
650 public function getAutoComplete($text)
652 $tokens = JobTerms
::tokenize($text.'%');
653 if (count($tokens) == 0) {
654 return PlIteratorUtils
::fromArray(array());
656 $token_join = JobTerms
::token_join_query($tokens, 'e');
657 return XDB
::iterator('SELECT e.jtid AS id, e.full_name AS field, COUNT(DISTINCT p.pid) AS nb
658 FROM profile_job_term_enum AS e
659 INNER JOIN profile_job_term_relation AS r ON (r.jtid_1 = e.jtid)
660 INNER JOIN profile_job_term AS p ON (r.jtid_2 = p.jtid)
663 ORDER BY nb DESC, field
664 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
672 // {{{ class DE_Networking
673 class DE_Networking
extends DirEnumeration
675 protected $idfield = 'profile_networking_enum.nwid';
676 protected $valfield = 'profile_networking_enum.name';
677 protected $from = 'profile_networking_enum';
680 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
681 protected $ac_unique = 'profile_networking.pid';
687 // {{{ class DE_Medals
688 class DE_Medals
extends DirEnumeration
690 protected $from = 'profile_medal_enum';
692 protected $ac_join = 'INNER JOIN profile_medals ON (profile_medals.mid = profile_medal_enum.id)';
693 protected $ac_unique = 'profile_medals.pid';
699 // {{{ class DE_AccountTypes
700 class DE_AccountTypes
extends DirEnumeration
702 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
704 protected $from = 'account_types';
705 protected $valfield = 'perms';
706 protected $idfield = 'type';
710 // {{{ class DE_Skins
711 class DE_Skins
extends DirEnumeration
713 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
715 protected $from = 'skins';
716 protected $valfield = 'name';
717 protected $idfield = 'skin_tpl';
721 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: