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 SUBLOCALITIES
= 'sublocalities';
49 const LOCALITIES
= 'localities';
50 const ADMNISTRATIVEAREAS3
= 'admnistrativeareas3';
51 const ADMNISTRATIVEAREAS2
= 'admnistrativeareas2';
52 const ADMNISTRATIVEAREAS1
= 'admnistrativeareas1';
53 const COUNTRIES
= 'countries';
55 const COMPANIES
= 'companies';
56 const JOBDESCRIPTION
= 'jobdescription';
57 const JOBTERMS
= 'jobterms';
59 const NETWORKS
= 'networking';
61 const MEDALS
= 'medals';
63 const ACCOUNTTYPES
= 'accounttypes';
64 const SKINS
= 'skins';
66 static private $enumerations = array();
68 static private function init($type)
70 if (Platal
::globals()->cacheEnabled() && S
::has('__DE_' . $type)) {
71 self
::$enumerations[$type] = S
::v('__DE_' . $type);
73 $cls = "DE_" . ucfirst($type);
75 self
::$enumerations[$type] = $obj;
76 if (Platal
::globals()->cacheEnabled()
77 && $obj->capabilities
& DirEnumeration
::SAVE_IN_SESSION
) {
78 S
::set('__DE_' . $type, $obj);
83 /** Retrieves all options for a given type
84 * @param $type Type of enum for which options are requested
85 * @return Array of the results
87 static public function getOptions($type)
89 if (!array_key_exists($type, self
::$enumerations)) {
92 $obj = self
::$enumerations[$type];
93 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
94 return call_user_func(array($obj, 'getOptions'));
100 /** Retrieves all options for a given type
101 * @param $type Type of enum for which options are requested
102 * @return PlIterator over the results
104 static public function getOptionsIter($type)
106 if (!array_key_exists($type, self
::$enumerations)) {
109 $obj = self
::$enumerations[$type];
110 $args = func_get_args();
112 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
113 return call_user_func_array(array($obj, 'getOptionsIter'), $args);
115 return PlIteratorUtils
::fromArray(array());
119 /** Retrieves all options with number of profiles for autocompletion
120 * @param $type Type of enum for which options are requested
121 * @param $text Text to autocomplete
122 * @return PlIterator over the results
124 static public function getAutoComplete($type, $text)
126 if (!array_key_exists($type, self
::$enumerations)) {
129 $obj = self
::$enumerations[$type];
130 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
131 return call_user_func(array($obj, 'getAutoComplete'), $text);
133 return PlIteratorUtils
::fromArray(array());
137 /** Retrieves a list of IDs for a given type
138 * @param $type Type of enum for which IDs are requested
139 * @param $text Text to search in enum valuees
140 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
142 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
144 if (!array_key_exists($type, self
::$enumerations)) {
147 $obj = self
::$enumerations[$type];
148 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
149 return call_user_func(array($obj, 'getIDs'), $text, $mode);
155 /** Retrieves a single ID for a given type.
156 * @param $type Type of the enum for which an ID is requested
157 * @param $text Text to search in enum values
158 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
160 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
162 $ids = self
::getIDs($type, $text, $mode);
163 return array_shift($ids);
168 // {{{ class DirEnumeration
169 abstract class DirEnumeration
171 const AUTOCOMPLETE_LIMIT
= 11;
173 const HAS_OPTIONS
= 0x001;
174 const HAS_AUTOCOMP
= 0x002;
175 const SAVE_IN_SESSION
= 0x004;
177 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
179 /** An internal array of ID => optionTxt
181 protected $options = null
;
183 /** Description of the MySQL storage of the fields
185 protected $idfield = 'id';
186 protected $valfield = 'text';
187 protected $valfield2 = null
;
189 protected $join = '';
190 protected $where = '';
192 /** Fields for autocompletion
194 protected $ac_join = ''; // Additional joins
195 protected $ac_where = null
; // Additional where
196 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
197 protected $ac_unique; // Which field is to be taken as unique
198 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
199 protected $ac_withid = true
; // Do we want to fetch id too ?
201 protected function _fetchOptions()
203 if (is_null($this->options
)) {
204 $this->loadOptions();
208 public function getOptions()
210 $this->_fetchOptions();
211 return $this->options
;
214 public function getOptionsIter()
216 $options = $this->getOptions();
217 $options = self
::expandArray($options);
218 return PlIteratorUtils
::fromArray($options, 1, true
);
221 // {{{ function getIDs
222 /** Retrieves possible IDs for given text
223 * @param $text Text to search for IDs
224 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
225 * @return An array of matching IDs ; if empty, input should be considered invalid
227 public function getIDs($text, $mode)
229 if ($mode == XDB
::WILDCARD_EXACT
) {
230 $options = $this->getOptions();
231 return array_keys($options, $text);
233 if ($this->where
== null
) {
236 $where = $this->where
. ' AND ';
239 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
240 if ($this->valfield2
!= null
) {
241 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
243 $where .= '(' . implode(' OR ', $conds) . ')';
245 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
246 FROM ' . $this->from
. '
249 GROUP BY ' . $this->idfield
);
254 /** Builds a list of query parts for searching @$text in @$field :
255 * field LIKE 'text%', field LIKE '% text%', field LIKE '%-text%'
257 private function mkTests($field, $text)
260 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
261 if (!$this->ac_beginwith
) {
262 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
263 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
268 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
271 foreach ($tab as $key => $val) {
280 // {{{ function getAutoComplete
281 public function getAutoComplete($text)
283 $text = str_replace(array('%', '_'), '', $text);
285 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
288 $where = $this->ac_where
. ' AND ';
291 $tests = $this->mkTests($this->valfield
, $text);
292 if (!is_null($this->valfield2
)) {
293 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
296 $where .= '(' . implode(' OR ', $tests) . ')';
298 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
299 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
300 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
301 FROM ' . $this->from
. '
302 ' . $this->ac_join
. '
304 GROUP BY ' . $this->valfield
. '
305 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
306 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
310 // {{{ function loadOptions
311 /** The function used to load options
313 protected function loadOptions()
315 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
316 ' . $this->idfield
. ' AS id
317 FROM ' . $this->from
. '
320 GROUP BY ' . $this->valfield
. '
321 ORDER BY ' . $this->valfield
);
327 // {{{ class DE_WithSuboption
328 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
330 abstract class DE_WithSuboption
extends DirEnumeration
334 protected $suboptions = null
;
336 protected function loadOptions()
338 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
339 ' . $this->optfield
. ' AS subid,
340 ' . $this->idfield
. ' AS id
341 FROM ' . $this->from
. '
344 GROUP BY ' . $this->valfield
. '
345 ORDER BY ' . $this->valfield
);
346 $this->options
= array();
347 $this->suboptions
= array();
348 foreach ($opts as $id => $opt) {
349 $this->options
[$id] = $opt['field'];
350 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
351 $this->suboptions
[$opt['subid']] = array();
353 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
357 public function getOptions($subid = null
)
359 $this->_fetchOptions();
360 if ($subid == null
) {
361 return $this->options
;
362 } else if (array_key_exists($subid, $this->suboptions
)) {
363 return $this->suboptions
[$subid];
369 public function getOptionsIter($subid = null
)
371 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
374 public function getIDs($text, $mode, $subid = null
)
376 if ($mode == XDB
::WILDCARD_EXACT
) {
377 $options = $this->getOptions($subid);
378 return array_keys($options, $text);
380 if ($this->where
== null
) {
383 $where = $this->where
. ' AND ';
385 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
386 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
390 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
391 if ($this->valfield2
!= null
) {
392 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
394 $where .= '(' . implode(' OR ', $conds) . ')';
396 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
397 FROM ' . $this->from
. '
400 GROUP BY ' . $this->idfield
);
404 public function getAutoComplete($text, $subid = null
)
406 $text = str_replace(array('%', '_'), '', $text);
408 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
411 $where = $this->ac_where
. ' AND ';
414 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
415 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
418 $tests = $this->mkTests($this->valfield
, $text);
419 if (!is_null($this->valfield2
)) {
420 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
423 $where .= '(' . implode(' OR ', $tests) . ')';
425 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
426 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
427 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
428 FROM ' . $this->from
. '
429 ' . $this->ac_join
. '
431 GROUP BY ' . $this->valfield
. '
432 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
433 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
438 // {{{ class DE_NameTypes
439 // returns 'system' names ('lastname', 'lastname_marital', ...)
440 class DE_NameTypes
extends DirEnumeration
442 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
444 protected $from = 'profile_name_enum';
445 protected $valfield = 'type';
449 // {{{ class DE_Names
450 // returns 'system' names ('lastname', 'lastname_marital', ...)
451 class DE_Names
extends DirEnumeration
453 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
455 protected $from = 'profile_name_enum';
456 protected $idfield = 'type';
457 protected $valfield = 'name';
463 // {{{ class DE_Binets
464 class DE_Binets
extends DirEnumeration
466 protected $from = 'profile_binet_enum';
468 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
469 protected $ac_unique = 'profile_binets.pid';
473 // {{{ class DE_Sections
474 class DE_Sections
extends DirEnumeration
476 protected $from = 'profile_section_enum';
478 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
479 protected $ac_unique = 'profiles.pid';
483 // {{{ class DE_GroupesX
484 class DE_GroupesX
extends DirEnumeration
486 protected $idfield = 'groups.id';
487 protected $valfield = 'groups.nom';
488 protected $valfield2 = 'groups.diminutif';
489 protected $from = 'groups';
490 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
492 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
493 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
494 AND groups.pub = 'public')";
495 protected $ac_unique = 'group_members.uid';
501 // {{{ class DE_EducationSchools
502 class DE_EducationSchools
extends DirEnumeration
504 protected $idfield = 'profile_education_enum.id';
505 protected $valfield = 'profile_education_enum.name';
506 protected $valfield2 = 'profile_education_enum.abbreviation';
507 protected $from = 'profile_education_enum';
509 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
510 protected $ac_unique = 'profile_education.pid';
514 // {{{ class DE_EducationDegrees
515 class DE_EducationDegrees
extends DirEnumeration
517 public $capabilities = self
::HAS_OPTIONS
;
519 protected $idfield = 'profile_education_degree.degreeid';
520 protected $optfield = 'profile_education_degree.eduid';
521 protected $valfield = 'profile_education_degree_enum.degree';
522 protected $from = 'profile_education_degree_enum';
523 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
528 // {{{ class DE_EducationFields
529 class DE_EducationFields
extends DirEnumeration
531 protected $valfield = 'profile_education_field_enum.field';
532 protected $from = 'profile_education_field_enum';
534 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
535 protected $ac_unique = 'profile_education.pid';
539 // {{{ class DE_CurrentCorps
540 class DE_CurrentCorps
extends DirEnumeration
542 protected $idfield = 'profile_corps_enum.id';
543 protected $valfield = 'profile_corps_enum.name';
544 protected $valfield2 = 'profile_corps_enum.abbrev';
545 protected $from = 'profile_corps_enum';
546 protected $where = 'WHERE profile_corps_enum.still_exists = 1';
548 protected $ac_unique = 'profile_corps.pid';
549 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.current_corpsid = profile_corps_enum.id)';
553 // {{{ class DE_OriginCorps
554 class DE_OriginCorps
extends DirEnumeration
556 protected $idfield = 'profile_corps_enum.id';
557 protected $valfield = 'profile_corps_enum.name';
558 protected $valfield2 = 'profile_corps_enum.abbrev';
559 protected $from = 'profile_corps_enum';
561 protected $ac_unique = 'profile_corps.pid';
562 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.original_corpsid = profile_corps_enum.id)';
566 // {{{ class DE_CorpsRanks
567 class DE_CorpsRanks
extends DirEnumeration
569 protected $idfield = 'profile_corps_rank_enum.id';
570 protected $valfield = 'profile_corps_rank_enum.name';
571 protected $valfield2 = 'profile_corps_rank_enum.abbrev';
572 protected $from = 'profile_corps_rank_enum';
574 protected $ac_unique = 'profile_corps.pid';
575 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.rankid = profile_corps_rank_enum.id)';
581 // {{{ class DE_Nationalities
582 class DE_Nationalities
extends DirEnumeration
584 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
585 protected $valfield = 'geoloc_countries.nationality';
586 protected $valfield2 = 'geoloc_countries.nationalityEn';
587 protected $from = 'geoloc_countries';
588 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
590 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
591 protected $ac_unique = 'profiles.pid';
595 // {{{ class DE_AddressesComponents
596 class DE_AddressesComponents
extends DirEnumeration
598 protected $idfield = 'profile_addresses_components_enum.id';
599 protected $valfield = 'profile_addresses_components_enum.long_name';
600 protected $from = 'profile_addresses_components_enum';
602 protected $ac_join = 'INNER JOIN profile_addresses_components ON (profile_addresses_components.component_id = profile_addresses_components_enum.id)';
603 protected $ac_unique = 'profile_addresses_components.pid';
604 protected $ac_where = 'profile_addresses_components.type = \'home\'';
607 // {{{ class DE_AddressesComponents extensions
608 class DE_Countries
extends DE_AddressesComponents
610 protected $where = 'WHERE FIND_IN_SET(\'country\', profile_addresses_components_enum.types)';
613 class DE_Admnistrativeareas1
extends DE_AddressesComponents
615 protected $where = 'WHERE FIND_IN_SET(\'admnistrative_area_1\', profile_addresses_components_enum.types)';
618 class DE_Admnistrativeareas2
extends DE_AddressesComponents
620 protected $where = 'WHERE FIND_IN_SET(\'admnistrative_area_2\', profile_addresses_components_enum.types)';
623 class DE_Admnistrativeareas3
extends DE_AddressesComponents
625 protected $where = 'WHERE FIND_IN_SET(\'admnistrative_area_3\', profile_addresses_components_enum.types)';
628 class DE_Localities
extends DE_AddressesComponents
630 protected $where = 'WHERE FIND_IN_SET(\'locality\', profile_addresses_components_enum.types)';
633 class DE_Sublocalities
extends DE_AddressesComponents
635 protected $where = 'WHERE FIND_IN_SET(\'sublocality\', profile_addresses_components_enum.types)';
642 // {{{ class DE_Companies
643 class DE_Companies
extends DirEnumeration
645 protected $idfield = 'profile_job_enum.id';
646 protected $valfield = 'profile_job_enum.name';
647 protected $valfield2 = 'profile_job_enum.acronym';
648 protected $from = 'profile_job_enum';
650 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
651 protected $ac_unique = 'profile_job.pid';
655 // {{{ class DE_JobDescription
656 class DE_JobDescription
extends DirEnumeration
658 protected $valfield = 'profile_job.description';
659 protected $from = 'profile_job';
660 protected $idfield = 'profile_job.pid';
662 protected $ac_unique = 'profile_job.pid';
666 // {{{ class DE_JobTerms
667 class DE_JobTerms
extends DirEnumeration
669 // {{{ function getAutoComplete
670 public function getAutoComplete($text)
672 $tokens = JobTerms
::tokenize($text.'%');
673 if (count($tokens) == 0) {
674 return PlIteratorUtils
::fromArray(array());
676 $token_join = JobTerms
::token_join_query($tokens, 'e');
677 return XDB
::iterator('SELECT e.jtid AS id, e.full_name AS field, COUNT(DISTINCT p.pid) AS nb
678 FROM profile_job_term_enum AS e
679 INNER JOIN profile_job_term_relation AS r ON (r.jtid_1 = e.jtid)
680 INNER JOIN profile_job_term AS p ON (r.jtid_2 = p.jtid)
683 ORDER BY nb DESC, field
684 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
692 // {{{ class DE_Networking
693 class DE_Networking
extends DirEnumeration
695 protected $idfield = 'profile_networking_enum.nwid';
696 protected $valfield = 'profile_networking_enum.name';
697 protected $from = 'profile_networking_enum';
700 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
701 protected $ac_unique = 'profile_networking.pid';
707 // {{{ class DE_Medals
708 class DE_Medals
extends DirEnumeration
710 protected $from = 'profile_medal_enum';
712 protected $ac_join = 'INNER JOIN profile_medals ON (profile_medals.mid = profile_medal_enum.id)';
713 protected $ac_unique = 'profile_medals.pid';
719 // {{{ class DE_AccountTypes
720 class DE_AccountTypes
extends DirEnumeration
722 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
724 protected $from = 'account_types';
725 protected $valfield = 'perms';
726 protected $idfield = 'type';
730 // {{{ class DE_Skins
731 class DE_Skins
extends DirEnumeration
733 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
735 protected $from = 'skins';
736 protected $valfield = 'name';
737 protected $idfield = 'skin_tpl';
741 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: