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 JOBDESCRIPTION
= 'jobdescription';
53 const JOBTERMS
= 'jobterms';
55 const NETWORKS
= 'networking';
57 const MEDALS
= 'medals';
59 const ACCOUNTTYPES
= 'accounttypes';
60 const SKINS
= 'skins';
62 static private $enumerations = array();
64 static private function init($type)
66 if (Platal
::globals()->cacheEnabled() && S
::has('__DE_' . $type)) {
67 self
::$enumerations[$type] = S
::v('__DE_' . $type);
69 $cls = "DE_" . ucfirst($type);
71 self
::$enumerations[$type] = $obj;
72 if (Platal
::globals()->cacheEnabled()
73 && $obj->capabilities
& DirEnumeration
::SAVE_IN_SESSION
) {
74 S
::set('__DE_' . $type, $obj);
79 /** Retrieves all options for a given type
80 * @param $type Type of enum for which options are requested
81 * @return Array of the results
83 static public function getOptions($type)
85 if (!array_key_exists($type, self
::$enumerations)) {
88 $obj = self
::$enumerations[$type];
89 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
90 return call_user_func(array($obj, 'getOptions'));
96 /** Retrieves all options for a given type
97 * @param $type Type of enum for which options are requested
98 * @return PlIterator over the results
100 static public function getOptionsIter($type)
102 if (!array_key_exists($type, self
::$enumerations)) {
105 $obj = self
::$enumerations[$type];
106 $args = func_get_args();
108 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
109 return call_user_func_array(array($obj, 'getOptionsIter'), $args);
111 return PlIteratorUtils
::fromArray(array());
115 /** Retrieves all options with number of profiles for autocompletion
116 * @param $type Type of enum for which options are requested
117 * @param $text Text to autocomplete
118 * @return PlIterator over the results
120 static public function getAutoComplete($type, $text)
122 if (!array_key_exists($type, self
::$enumerations)) {
125 $obj = self
::$enumerations[$type];
126 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
127 return call_user_func(array($obj, 'getAutoComplete'), $text);
129 return PlIteratorUtils
::fromArray(array());
133 /** Retrieves a list of IDs for a given type
134 * @param $type Type of enum for which IDs are requested
135 * @param $text Text to search in enum valuees
136 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
138 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
140 if (!array_key_exists($type, self
::$enumerations)) {
143 $obj = self
::$enumerations[$type];
144 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
145 return call_user_func(array($obj, 'getIDs'), $text, $mode);
151 /** Retrieves a single ID for a given type.
152 * @param $type Type of the enum for which an ID is requested
153 * @param $text Text to search in enum values
154 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
156 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
158 $ids = self
::getIDs($type, $text, $mode);
159 return array_shift($ids);
164 // {{{ class DirEnumeration
165 abstract class DirEnumeration
167 const AUTOCOMPLETE_LIMIT
= 11;
169 const HAS_OPTIONS
= 0x001;
170 const HAS_AUTOCOMP
= 0x002;
171 const SAVE_IN_SESSION
= 0x004;
173 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
175 /** An internal array of ID => optionTxt
177 protected $options = null
;
179 /** Description of the MySQL storage of the fields
181 protected $idfield = 'id';
182 protected $valfield = 'text';
183 protected $valfield2 = null
;
185 protected $join = '';
186 protected $where = '';
188 /** Fields for autocompletion
190 protected $ac_join = ''; // Additional joins
191 protected $ac_where = null
; // Additional where
192 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
193 protected $ac_unique; // Which field is to be taken as unique
194 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
195 protected $ac_withid = true
; // Do we want to fetch id too ?
197 protected function _fetchOptions()
199 if (is_null($this->options
)) {
200 $this->loadOptions();
204 public function getOptions()
206 $this->_fetchOptions();
207 return $this->options
;
210 public function getOptionsIter()
212 $options = $this->getOptions();
213 $options = self
::expandArray($options);
214 return PlIteratorUtils
::fromArray($options, 1, true
);
217 // {{{ function getIDs
218 /** Retrieves possible IDs for given text
219 * @param $text Text to search for IDs
220 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
221 * @return An array of matching IDs ; if empty, input should be considered invalid
223 public function getIDs($text, $mode)
225 if ($mode == XDB
::WILDCARD_EXACT
) {
226 $options = $this->getOptions();
227 return array_keys($options, $text);
229 if ($this->where
== null
) {
232 $where = $this->where
. ' AND ';
235 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
236 if ($this->valfield2
!= null
) {
237 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
239 $where .= '(' . implode(' OR ', $conds) . ')';
241 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
242 FROM ' . $this->from
. '
245 GROUP BY ' . $this->idfield
);
250 /** Builds a list of query parts for searching @$text in @$field :
251 * field LIKE 'text%', field LIKE '% text%', field LIKE '%-text%'
253 private function mkTests($field, $text)
256 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
257 if (!$this->ac_beginwith
) {
258 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
259 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
264 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
267 foreach ($tab as $key => $val) {
276 // {{{ function getAutoComplete
277 public function getAutoComplete($text)
279 $text = str_replace(array('%', '_'), '', $text);
281 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
284 $where = $this->ac_where
. ' AND ';
287 $tests = $this->mkTests($this->valfield
, $text);
288 if (!is_null($this->valfield2
)) {
289 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
292 $where .= '(' . implode(' OR ', $tests) . ')';
294 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
295 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
296 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
297 FROM ' . $this->from
. '
298 ' . $this->ac_join
. '
300 GROUP BY ' . $this->valfield
. '
301 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
302 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
306 // {{{ function loadOptions
307 /** The function used to load options
309 protected function loadOptions()
311 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
312 ' . $this->idfield
. ' AS id
313 FROM ' . $this->from
. '
316 GROUP BY ' . $this->valfield
. '
317 ORDER BY ' . $this->valfield
);
323 // {{{ class DE_WithSuboption
324 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
326 abstract class DE_WithSuboption
extends DirEnumeration
330 protected $suboptions = null
;
332 protected function loadOptions()
334 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
335 ' . $this->optfield
. ' AS subid,
336 ' . $this->idfield
. ' AS id
337 FROM ' . $this->from
. '
340 GROUP BY ' . $this->valfield
. '
341 ORDER BY ' . $this->valfield
);
342 $this->options
= array();
343 $this->suboptions
= array();
344 foreach ($opts as $id => $opt) {
345 $this->options
[$id] = $opt['field'];
346 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
347 $this->suboptions
[$opt['subid']] = array();
349 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
353 public function getOptions($subid = null
)
355 $this->_fetchOptions();
356 if ($subid == null
) {
357 return $this->options
;
358 } else if (array_key_exists($subid, $this->suboptions
)) {
359 return $this->suboptions
[$subid];
365 public function getOptionsIter($subid = null
)
367 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
370 public function getIDs($text, $mode, $subid = null
)
372 if ($mode == XDB
::WILDCARD_EXACT
) {
373 $options = $this->getOptions($subid);
374 return array_keys($options, $text);
376 if ($this->where
== null
) {
379 $where = $this->where
. ' AND ';
381 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
382 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
386 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
387 if ($this->valfield2
!= null
) {
388 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
390 $where .= '(' . implode(' OR ', $conds) . ')';
392 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
393 FROM ' . $this->from
. '
396 GROUP BY ' . $this->idfield
);
400 public function getAutoComplete($text, $subid = null
)
402 $text = str_replace(array('%', '_'), '', $text);
404 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
407 $where = $this->ac_where
. ' AND ';
410 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
411 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
414 $tests = $this->mkTests($this->valfield
, $text);
415 if (!is_null($this->valfield2
)) {
416 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
419 $where .= '(' . implode(' OR ', $tests) . ')';
421 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
422 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
423 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
424 FROM ' . $this->from
. '
425 ' . $this->ac_join
. '
427 GROUP BY ' . $this->valfield
. '
428 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
429 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
434 // {{{ class DE_NameTypes
435 // returns 'system' names ('lastname', 'lastname_marital', ...)
436 class DE_NameTypes
extends DirEnumeration
438 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
440 protected $from = 'profile_name_enum';
441 protected $valfield = 'type';
445 // {{{ class DE_Names
446 // returns 'system' names ('lastname', 'lastname_marital', ...)
447 class DE_Names
extends DirEnumeration
449 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
451 protected $from = 'profile_name_enum';
452 protected $idfield = 'type';
453 protected $valfield = 'name';
459 // {{{ class DE_Binets
460 class DE_Binets
extends DirEnumeration
462 protected $from = 'profile_binet_enum';
464 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
465 protected $ac_unique = 'profile_binets.pid';
469 // {{{ class DE_Sections
470 class DE_Sections
extends DirEnumeration
472 protected $from = 'profile_section_enum';
474 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
475 protected $ac_unique = 'profiles.pid';
479 // {{{ class DE_GroupesX
480 class DE_GroupesX
extends DirEnumeration
482 protected $idfield = 'groups.id';
483 protected $valfield = 'groups.nom';
484 protected $valfield2 = 'groups.diminutif';
485 protected $from = 'groups';
486 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
488 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
489 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
490 AND groups.pub = 'public')";
491 protected $ac_unique = 'group_members.uid';
497 // {{{ class DE_EducationSchools
498 class DE_EducationSchools
extends DirEnumeration
500 protected $idfield = 'profile_education_enum.id';
501 protected $valfield = 'profile_education_enum.name';
502 protected $valfield2 = 'profile_education_enum.abbreviation';
503 protected $from = 'profile_education_enum';
505 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
506 protected $ac_unique = 'profile_education.pid';
510 // {{{ class DE_EducationDegrees
511 class DE_EducationDegrees
extends DirEnumeration
513 public $capabilities = self
::HAS_OPTIONS
;
515 protected $idfield = 'profile_education_degree.degreeid';
516 protected $optfield = 'profile_education_degree.eduid';
517 protected $valfield = 'profile_education_degree_enum.degree';
518 protected $from = 'profile_education_degree_enum';
519 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
524 // {{{ class DE_EducationFields
525 class DE_EducationFields
extends DirEnumeration
527 protected $valfield = 'profile_education_field_enum.field';
528 protected $from = 'profile_education_field_enum';
530 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
531 protected $ac_unique = 'profile_education.pid';
535 // {{{ class DE_Corps
536 class DE_Corps
extends DirEnumeration
538 protected $idfield = 'profile_corps_enum.id';
539 protected $valfield = 'profile_corps_enum.name';
540 protected $valfield2 = 'profile_corps_enum.abbrev';
541 protected $from = 'profile_corps_enum';
543 protected $ac_unique = 'profile_corps.pid';
544 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.current_corpsid = profile_corps_enum.id)';
548 // {{{ class DE_CorpsRanks
549 class DE_CorpsRanks
extends DirEnumeration
551 protected $idfield = 'profile_corps_rank_enum.id';
552 protected $valfield = 'profile_corps_rank_enum.name';
553 protected $valfield2 = 'profile_corps_rank_enum.abbrev';
554 protected $from = 'profile_corps_rank_enum';
556 protected $ac_unique = 'profile_corps.pid';
557 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.rankid = profile_corps_rank_enum.id)';
563 // {{{ class DE_Nationalities
564 class DE_Nationalities
extends DirEnumeration
566 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
567 protected $valfield = 'geoloc_countries.nationalityFR';
568 protected $valfield2 = 'geoloc_countries.nationality';
569 protected $from = 'geoloc_countries';
570 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
572 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
573 protected $ac_unique = 'profiles.pid';
577 // {{{ class DE_Countries
578 class DE_Countries
extends DirEnumeration
580 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
581 protected $valfield = 'geoloc_countries.countryFR';
582 protected $valfield2 = 'geoloc_countries.country';
583 protected $from = 'geoloc_countries';
585 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
586 protected $ac_unique = 'profile_addresses.pid';
590 // {{{ class DE_AdminAreas
591 class DE_AdminAreas
extends DE_WithSuboption
593 protected $idfield = 'geoloc_administrativeareas.id';
594 protected $optfield = 'geoloc_administrativeareas.country';
595 protected $valfield = 'geoloc_administrativeareas.name';
596 protected $from = 'geoloc_administrativeareas';
598 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
599 protected $ac_unique = 'profile_addresses.pid';
603 // {{{ class DE_Localities
604 class DE_Localities
extends DirEnumeration
606 protected $idfield = 'geoloc_localities.id';
607 protected $valfield = 'geoloc_localities.name';
608 protected $from = 'geoloc_localities';
610 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
611 protected $ac_unique = 'profile_addresses.pid';
617 // {{{ class DE_Companies
618 class DE_Companies
extends DirEnumeration
620 protected $idfield = 'profile_job_enum.id';
621 protected $valfield = 'profile_job_enum.name';
622 protected $valfield2 = 'profile_job_enum.acronym';
623 protected $from = 'profile_job_enum';
625 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
626 protected $ac_unique = 'profile_job.pid';
630 // {{{ class DE_JobDescription
631 class DE_JobDescription
extends DirEnumeration
633 protected $valfield = 'profile_job.description';
634 protected $from = 'profile_job';
635 protected $idfield = 'profile_job.pid';
637 protected $ac_unique = 'profile_job.pid';
641 // {{{ class DE_JobTerms
642 class DE_JobTerms
extends DirEnumeration
644 // {{{ function getAutoComplete
645 public function getAutoComplete($text)
647 $tokens = JobTerms
::tokenize($text.'%');
648 if (count($tokens) == 0) {
649 return PlIteratorUtils
::fromArray(array());
651 $token_join = JobTerms
::token_join_query($tokens, 'e');
652 return XDB
::iterator('SELECT e.jtid AS id, e.full_name AS field, COUNT(DISTINCT p.pid) AS nb
653 FROM profile_job_term_enum AS e
654 INNER JOIN profile_job_term_relation AS r ON (r.jtid_1 = e.jtid)
655 INNER JOIN profile_job_term AS p ON (r.jtid_2 = p.jtid)
658 ORDER BY nb DESC, field
659 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
667 // {{{ class DE_Networking
668 class DE_Networking
extends DirEnumeration
670 protected $idfield = 'profile_networking_enum.nwid';
671 protected $valfield = 'profile_networking_enum.name';
672 protected $from = 'profile_networking_enum';
675 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
676 protected $ac_unique = 'profile_networking.pid';
682 // {{{ class DE_Medals
683 class DE_Medals
extends DirEnumeration
685 protected $from = 'profile_medal_enum';
687 protected $ac_join = 'INNER JOIN profile_medals ON (profile_medals.mid = profile_medal_enum.id)';
688 protected $ac_unique = 'profile_medals.pid';
694 // {{{ class DE_AccountTypes
695 class DE_AccountTypes
extends DirEnumeration
697 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
699 protected $from = 'account_types';
700 protected $valfield = 'perms';
701 protected $idfield = 'type';
705 // {{{ class DE_Skins
706 class DE_Skins
extends DirEnumeration
708 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
710 protected $from = 'skins';
711 protected $valfield = 'name';
712 protected $idfield = 'skin_tpl';
716 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: