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 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 CURRENTCORPS
= 'currentcorps';
44 const ORIGINCORPS
= 'origincorps';
45 const CORPSRANKS
= 'corpsranks';
47 const NATIONALITIES
= 'nationalities';
48 const COUNTRIES
= 'countries';
49 const ADMINAREAS
= 'adminareas';
50 const SUBADMINAREAS
= 'subadminareas';
51 const LOCALITIES
= 'localities';
53 const COMPANIES
= 'companies';
54 const JOBDESCRIPTION
= 'jobdescription';
55 const JOBTERMS
= 'jobterms';
57 const NETWORKS
= 'networking';
59 const MEDALS
= 'medals';
61 const ACCOUNTTYPES
= 'accounttypes';
62 const SKINS
= 'skins';
64 static private $enumerations = array();
66 static private function init($type)
68 if (Platal
::globals()->cacheEnabled() && S
::has('__DE_' . $type)) {
69 self
::$enumerations[$type] = S
::v('__DE_' . $type);
71 $cls = "DE_" . ucfirst($type);
73 self
::$enumerations[$type] = $obj;
74 if (Platal
::globals()->cacheEnabled()
75 && $obj->capabilities
& DirEnumeration
::SAVE_IN_SESSION
) {
76 S
::set('__DE_' . $type, $obj);
81 /** Retrieves all options for a given type
82 * @param $type Type of enum for which options are requested
83 * @return Array of the results
85 static public function getOptions($type)
87 if (!array_key_exists($type, self
::$enumerations)) {
90 $obj = self
::$enumerations[$type];
91 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
92 return call_user_func(array($obj, 'getOptions'));
98 /** Retrieves all options for a given type
99 * @param $type Type of enum for which options are requested
100 * @return PlIterator over the results
102 static public function getOptionsIter($type)
104 if (!array_key_exists($type, self
::$enumerations)) {
107 $obj = self
::$enumerations[$type];
108 $args = func_get_args();
110 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
111 return call_user_func_array(array($obj, 'getOptionsIter'), $args);
113 return PlIteratorUtils
::fromArray(array());
117 /** Retrieves all options with number of profiles for autocompletion
118 * @param $type Type of enum for which options are requested
119 * @param $text Text to autocomplete
120 * @return PlIterator over the results
122 static public function getAutoComplete($type, $text)
124 if (!array_key_exists($type, self
::$enumerations)) {
127 $obj = self
::$enumerations[$type];
128 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
129 return call_user_func(array($obj, 'getAutoComplete'), $text);
131 return PlIteratorUtils
::fromArray(array());
135 /** Retrieves a list of IDs for a given type
136 * @param $type Type of enum for which IDs are requested
137 * @param $text Text to search in enum valuees
138 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
140 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
142 if (!array_key_exists($type, self
::$enumerations)) {
145 $obj = self
::$enumerations[$type];
146 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
147 return call_user_func(array($obj, 'getIDs'), $text, $mode);
153 /** Retrieves a single ID for a given type.
154 * @param $type Type of the enum for which an ID is requested
155 * @param $text Text to search in enum values
156 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
158 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
160 $ids = self
::getIDs($type, $text, $mode);
161 return array_shift($ids);
166 // {{{ class DirEnumeration
167 abstract class DirEnumeration
169 const AUTOCOMPLETE_LIMIT
= 11;
171 const HAS_OPTIONS
= 0x001;
172 const HAS_AUTOCOMP
= 0x002;
173 const SAVE_IN_SESSION
= 0x004;
175 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
177 /** An internal array of ID => optionTxt
179 protected $options = null
;
181 /** Description of the MySQL storage of the fields
183 protected $idfield = 'id';
184 protected $valfield = 'text';
185 protected $valfield2 = null
;
187 protected $join = '';
188 protected $where = '';
190 /** Fields for autocompletion
192 protected $ac_join = ''; // Additional joins
193 protected $ac_where = null
; // Additional where
194 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
195 protected $ac_unique; // Which field is to be taken as unique
196 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
197 protected $ac_withid = true
; // Do we want to fetch id too ?
199 protected function _fetchOptions()
201 if (is_null($this->options
)) {
202 $this->loadOptions();
206 public function getOptions()
208 $this->_fetchOptions();
209 return $this->options
;
212 public function getOptionsIter()
214 $options = $this->getOptions();
215 $options = self
::expandArray($options);
216 return PlIteratorUtils
::fromArray($options, 1, true
);
219 // {{{ function getIDs
220 /** Retrieves possible IDs for given text
221 * @param $text Text to search for IDs
222 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
223 * @return An array of matching IDs ; if empty, input should be considered invalid
225 public function getIDs($text, $mode)
227 if ($mode == XDB
::WILDCARD_EXACT
) {
228 $options = $this->getOptions();
229 return array_keys($options, $text);
231 if ($this->where
== null
) {
234 $where = $this->where
. ' AND ';
237 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
238 if ($this->valfield2
!= null
) {
239 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
241 $where .= '(' . implode(' OR ', $conds) . ')';
243 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
244 FROM ' . $this->from
. '
247 GROUP BY ' . $this->idfield
);
252 /** Builds a list of query parts for searching @$text in @$field :
253 * field LIKE 'text%', field LIKE '% text%', field LIKE '%-text%'
255 private function mkTests($field, $text)
258 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
259 if (!$this->ac_beginwith
) {
260 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
261 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
266 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
269 foreach ($tab as $key => $val) {
278 // {{{ function getAutoComplete
279 public function getAutoComplete($text)
281 $text = str_replace(array('%', '_'), '', $text);
283 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
286 $where = $this->ac_where
. ' AND ';
289 $tests = $this->mkTests($this->valfield
, $text);
290 if (!is_null($this->valfield2
)) {
291 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
294 $where .= '(' . implode(' OR ', $tests) . ')';
296 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
297 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
298 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
299 FROM ' . $this->from
. '
300 ' . $this->ac_join
. '
302 GROUP BY ' . $this->valfield
. '
303 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
304 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
308 // {{{ function loadOptions
309 /** The function used to load options
311 protected function loadOptions()
313 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
314 ' . $this->idfield
. ' AS id
315 FROM ' . $this->from
. '
318 GROUP BY ' . $this->valfield
. '
319 ORDER BY ' . $this->valfield
);
325 // {{{ class DE_WithSuboption
326 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
328 abstract class DE_WithSuboption
extends DirEnumeration
332 protected $suboptions = null
;
334 protected function loadOptions()
336 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
337 ' . $this->optfield
. ' AS subid,
338 ' . $this->idfield
. ' AS id
339 FROM ' . $this->from
. '
342 GROUP BY ' . $this->valfield
. '
343 ORDER BY ' . $this->valfield
);
344 $this->options
= array();
345 $this->suboptions
= array();
346 foreach ($opts as $id => $opt) {
347 $this->options
[$id] = $opt['field'];
348 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
349 $this->suboptions
[$opt['subid']] = array();
351 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
355 public function getOptions($subid = null
)
357 $this->_fetchOptions();
358 if ($subid == null
) {
359 return $this->options
;
360 } else if (array_key_exists($subid, $this->suboptions
)) {
361 return $this->suboptions
[$subid];
367 public function getOptionsIter($subid = null
)
369 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
372 public function getIDs($text, $mode, $subid = null
)
374 if ($mode == XDB
::WILDCARD_EXACT
) {
375 $options = $this->getOptions($subid);
376 return array_keys($options, $text);
378 if ($this->where
== null
) {
381 $where = $this->where
. ' AND ';
383 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
384 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
388 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
389 if ($this->valfield2
!= null
) {
390 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
392 $where .= '(' . implode(' OR ', $conds) . ')';
394 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
395 FROM ' . $this->from
. '
398 GROUP BY ' . $this->idfield
);
402 public function getAutoComplete($text, $subid = null
)
404 $text = str_replace(array('%', '_'), '', $text);
406 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
409 $where = $this->ac_where
. ' AND ';
412 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
413 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
416 $tests = $this->mkTests($this->valfield
, $text);
417 if (!is_null($this->valfield2
)) {
418 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
421 $where .= '(' . implode(' OR ', $tests) . ')';
423 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
424 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
425 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
426 FROM ' . $this->from
. '
427 ' . $this->ac_join
. '
429 GROUP BY ' . $this->valfield
. '
430 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
431 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
436 // {{{ class DE_NameTypes
437 // returns 'system' names ('lastname', 'lastname_marital', ...)
438 class DE_NameTypes
extends DirEnumeration
440 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
442 protected $from = 'profile_name_enum';
443 protected $valfield = 'type';
447 // {{{ class DE_Names
448 // returns 'system' names ('lastname', 'lastname_marital', ...)
449 class DE_Names
extends DirEnumeration
451 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
453 protected $from = 'profile_name_enum';
454 protected $idfield = 'type';
455 protected $valfield = 'name';
461 // {{{ class DE_Binets
462 class DE_Binets
extends DirEnumeration
464 protected $from = 'profile_binet_enum';
466 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
467 protected $ac_unique = 'profile_binets.pid';
471 // {{{ class DE_Sections
472 class DE_Sections
extends DirEnumeration
474 protected $from = 'profile_section_enum';
476 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
477 protected $ac_unique = 'profiles.pid';
481 // {{{ class DE_GroupesX
482 class DE_GroupesX
extends DirEnumeration
484 protected $idfield = 'groups.id';
485 protected $valfield = 'groups.nom';
486 protected $valfield2 = 'groups.diminutif';
487 protected $from = 'groups';
488 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
490 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
491 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
492 AND groups.pub = 'public')";
493 protected $ac_unique = 'group_members.uid';
499 // {{{ class DE_EducationSchools
500 class DE_EducationSchools
extends DirEnumeration
502 protected $idfield = 'profile_education_enum.id';
503 protected $valfield = 'profile_education_enum.name';
504 protected $valfield2 = 'profile_education_enum.abbreviation';
505 protected $from = 'profile_education_enum';
507 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
508 protected $ac_unique = 'profile_education.pid';
512 // {{{ class DE_EducationDegrees
513 class DE_EducationDegrees
extends DirEnumeration
515 public $capabilities = self
::HAS_OPTIONS
;
517 protected $idfield = 'profile_education_degree.degreeid';
518 protected $optfield = 'profile_education_degree.eduid';
519 protected $valfield = 'profile_education_degree_enum.degree';
520 protected $from = 'profile_education_degree_enum';
521 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
526 // {{{ class DE_EducationFields
527 class DE_EducationFields
extends DirEnumeration
529 protected $valfield = 'profile_education_field_enum.field';
530 protected $from = 'profile_education_field_enum';
532 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
533 protected $ac_unique = 'profile_education.pid';
537 // {{{ class DE_CurrentCorps
538 class DE_CurrentCorps
extends DirEnumeration
540 protected $idfield = 'profile_corps_enum.id';
541 protected $valfield = 'profile_corps_enum.name';
542 protected $valfield2 = 'profile_corps_enum.abbrev';
543 protected $from = 'profile_corps_enum';
544 protected $where = 'WHERE profile_corps_enum.still_exists = 1';
546 protected $ac_unique = 'profile_corps.pid';
547 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.current_corpsid = profile_corps_enum.id)';
551 // {{{ class DE_OriginCorps
552 class DE_OriginCorps
extends DirEnumeration
554 protected $idfield = 'profile_corps_enum.id';
555 protected $valfield = 'profile_corps_enum.name';
556 protected $valfield2 = 'profile_corps_enum.abbrev';
557 protected $from = 'profile_corps_enum';
559 protected $ac_unique = 'profile_corps.pid';
560 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.original_corpsid = profile_corps_enum.id)';
564 // {{{ class DE_CorpsRanks
565 class DE_CorpsRanks
extends DirEnumeration
567 protected $idfield = 'profile_corps_rank_enum.id';
568 protected $valfield = 'profile_corps_rank_enum.name';
569 protected $valfield2 = 'profile_corps_rank_enum.abbrev';
570 protected $from = 'profile_corps_rank_enum';
572 protected $ac_unique = 'profile_corps.pid';
573 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.rankid = profile_corps_rank_enum.id)';
579 // {{{ class DE_Nationalities
580 class DE_Nationalities
extends DirEnumeration
582 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
583 protected $valfield = 'geoloc_countries.nationality';
584 protected $valfield2 = 'geoloc_countries.nationalityEn';
585 protected $from = 'geoloc_countries';
586 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
588 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
589 protected $ac_unique = 'profiles.pid';
593 // {{{ class DE_Countries
594 class DE_Countries
extends DirEnumeration
596 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
597 protected $valfield = 'geoloc_countries.country';
598 protected $valfield2 = 'geoloc_countries.countryEn';
599 protected $from = 'geoloc_countries';
601 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
602 protected $ac_unique = 'profile_addresses.pid';
603 protected $ac_where = 'profile_addresses.type = \'home\'';
607 // {{{ class DE_AdminAreas
608 class DE_AdminAreas
extends DE_WithSuboption
610 protected $idfield = 'geoloc_administrativeareas.id';
611 protected $optfield = 'geoloc_administrativeareas.country';
612 protected $valfield = 'geoloc_administrativeareas.name';
613 protected $from = 'geoloc_administrativeareas';
615 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
616 protected $ac_unique = 'profile_addresses.pid';
620 // {{{ class DE_SubAdminAreas
621 class DE_SubAdminAreas
extends DE_WithSuboption
623 protected $idfield = 'geoloc_subadministrativeareas.id';
624 protected $optfield = 'geoloc_subadministrativeareas.administrativearea';
625 protected $valfield = 'geoloc_subadministrativeareas.name';
626 protected $from = 'geoloc_subadministrativeareas';
628 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.subadministrativeAreaId = geoloc_subadministrativeareas.id)';
629 protected $ac_unique = 'profile_addresses.pid';
633 // {{{ class DE_Localities
634 class DE_Localities
extends DirEnumeration
636 protected $idfield = 'geoloc_localities.id';
637 protected $valfield = 'geoloc_localities.name';
638 protected $from = 'geoloc_localities';
640 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
641 protected $ac_unique = 'profile_addresses.pid';
647 // {{{ class DE_Companies
648 class DE_Companies
extends DirEnumeration
650 protected $idfield = 'profile_job_enum.id';
651 protected $valfield = 'profile_job_enum.name';
652 protected $valfield2 = 'profile_job_enum.acronym';
653 protected $from = 'profile_job_enum';
655 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
656 protected $ac_unique = 'profile_job.pid';
660 // {{{ class DE_JobDescription
661 class DE_JobDescription
extends DirEnumeration
663 protected $valfield = 'profile_job.description';
664 protected $from = 'profile_job';
665 protected $idfield = 'profile_job.pid';
667 protected $ac_unique = 'profile_job.pid';
671 // {{{ class DE_JobTerms
672 class DE_JobTerms
extends DirEnumeration
674 // {{{ function getAutoComplete
675 public function getAutoComplete($text)
677 $tokens = JobTerms
::tokenize($text.'%');
678 if (count($tokens) == 0) {
679 return PlIteratorUtils
::fromArray(array());
681 $token_join = JobTerms
::token_join_query($tokens, 'e');
682 return XDB
::iterator('SELECT e.jtid AS id, e.full_name AS field, COUNT(DISTINCT p.pid) AS nb
683 FROM profile_job_term_enum AS e
684 INNER JOIN profile_job_term_relation AS r ON (r.jtid_1 = e.jtid)
685 INNER JOIN profile_job_term AS p ON (r.jtid_2 = p.jtid)
688 ORDER BY nb DESC, field
689 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
697 // {{{ class DE_Networking
698 class DE_Networking
extends DirEnumeration
700 protected $idfield = 'profile_networking_enum.nwid';
701 protected $valfield = 'profile_networking_enum.name';
702 protected $from = 'profile_networking_enum';
705 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
706 protected $ac_unique = 'profile_networking.pid';
712 // {{{ class DE_Medals
713 class DE_Medals
extends DirEnumeration
715 protected $from = 'profile_medal_enum';
717 protected $ac_join = 'INNER JOIN profile_medals ON (profile_medals.mid = profile_medal_enum.id)';
718 protected $ac_unique = 'profile_medals.pid';
724 // {{{ class DE_AccountTypes
725 class DE_AccountTypes
extends DirEnumeration
727 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
729 protected $from = 'account_types';
730 protected $valfield = 'perms';
731 protected $idfield = 'type';
735 // {{{ class DE_Skins
736 class DE_Skins
extends DirEnumeration
738 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
740 protected $from = 'skins';
741 protected $valfield = 'name';
742 protected $idfield = 'skin_tpl';
746 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: