567f4de1314bac4c22dc3953be246691e691a384
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';
54 const JOBTERMS
= 'jobterms';
56 const NETWORKS
= 'networking';
58 const MEDALS
= 'medals';
60 static private $enumerations = array();
62 static private function init($type)
64 if (Platal
::globals()->cacheEnabled() && S
::has('__DE_' . $type)) {
65 self
::$enumerations[$type] = S
::v('__DE_' . $type);
67 $cls = "DE_" . ucfirst($type);
69 self
::$enumerations[$type] = $obj;
70 if (Platal
::globals()->cacheEnabled()
71 && $obj->capabilities
& DirEnumeration
::SAVE_IN_SESSION
) {
72 S
::set('__DE_' . $type, $obj);
77 /** Retrieves all options for a given type
78 * @param $type Type of enum for which options are requested
79 * @return Array of the results
81 static public function getOptions($type)
83 if (!array_key_exists($type, self
::$enumerations)) {
86 $obj = self
::$enumerations[$type];
87 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
88 return call_user_func(array($obj, 'getOptions'));
94 /** Retrieves all options for a given type
95 * @param $type Type of enum for which options are requested
96 * @return PlIterator over the results
98 static public function getOptionsIter($type)
100 if (!array_key_exists($type, self
::$enumerations)) {
103 $obj = self
::$enumerations[$type];
104 $args = func_get_args();
106 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
107 return call_user_func_array(array($obj, 'getOptionsIter'), $args);
109 return PlIteratorUtils
::fromArray(array());
113 /** Retrieves all options with number of profiles for autocompletion
114 * @param $type Type of enum for which options are requested
115 * @param $text Text to autocomplete
116 * @return PlIterator over the results
118 static public function getAutoComplete($type, $text)
120 if (!array_key_exists($type, self
::$enumerations)) {
123 $obj = self
::$enumerations[$type];
124 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
125 return call_user_func(array($obj, 'getAutoComplete'), $text);
127 return PlIteratorUtils
::fromArray(array());
131 /** Retrieves a list of IDs for a given type
132 * @param $type Type of enum for which IDs are requested
133 * @param $text Text to search in enum valuees
134 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
136 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
138 if (!array_key_exists($type, self
::$enumerations)) {
141 $obj = self
::$enumerations[$type];
142 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
143 return call_user_func(array($obj, 'getIDs'), $text, $mode);
149 /** Retrieves a single ID for a given type.
150 * @param $type Type of the enum for which an ID is requested
151 * @param $text Text to search in enum values
152 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
154 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
156 $ids = self
::getIDs($type, $text, $mode);
157 return array_shift($ids);
162 // {{{ class DirEnumeration
163 abstract class DirEnumeration
165 const AUTOCOMPLETE_LIMIT
= 11;
167 const HAS_OPTIONS
= 0x001;
168 const HAS_AUTOCOMP
= 0x002;
169 const SAVE_IN_SESSION
= 0x004;
171 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
173 /** An internal array of ID => optionTxt
175 protected $options = null
;
177 /** Description of the MySQL storage of the fields
179 protected $idfield = 'id';
180 protected $valfield = 'text';
181 protected $valfield2 = null
;
183 protected $join = '';
184 protected $where = '';
186 /** Fields for autocompletion
188 protected $ac_join = ''; // Additional joins
189 protected $ac_where = null
; // Additional where
190 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
191 protected $ac_unique; // Which field is to be taken as unique
192 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
193 protected $ac_withid = true
; // Do we want to fetch id too ?
195 protected function _fetchOptions()
197 if (is_null($this->options
)) {
198 $this->loadOptions();
202 public function getOptions()
204 $this->_fetchOptions();
205 return $this->options
;
208 public function getOptionsIter()
210 $options = $this->getOptions();
211 $options = self
::expandArray($options);
212 return PlIteratorUtils
::fromArray($options, 1, true
);
215 // {{{ function getIDs
216 /** Retrieves possible IDs for given text
217 * @param $text Text to search for IDs
218 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
219 * @return An array of matching IDs ; if empty, input should be considered invalid
221 public function getIDs($text, $mode)
223 if ($mode == XDB
::WILDCARD_EXACT
) {
224 $options = $this->getOptions();
225 return array_keys($options, $text);
227 if ($this->where
== null
) {
230 $where = $this->where
. ' AND ';
233 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
234 if ($this->valfield2
!= null
) {
235 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
237 $where .= '(' . implode(' OR ', $conds) . ')';
239 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
240 FROM ' . $this->from
. '
243 GROUP BY ' . $this->idfield
);
248 /** Builds a list of query parts for searching @$text in @$field :
249 * field LIKE 'text%', field LIKE '% text%', field LIKE '%-text%'
251 private function mkTests($field, $text)
254 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
255 if (!$this->ac_beginwith
) {
256 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
257 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
262 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
265 foreach ($tab as $key => $val) {
274 // {{{ function getAutoComplete
275 public function getAutoComplete($text)
277 $text = str_replace(array('%', '_'), '', $text);
279 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
282 $where = $this->ac_where
. ' AND ';
285 $tests = $this->mkTests($this->valfield
, $text);
286 if (!is_null($this->valfield2
)) {
287 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
290 $where .= '(' . implode(' OR ', $tests) . ')';
292 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
293 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
294 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
295 FROM ' . $this->from
. '
296 ' . $this->ac_join
. '
298 GROUP BY ' . $this->valfield
. '
299 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
300 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
304 // {{{ function loadOptions
305 /** The function used to load options
307 protected function loadOptions()
309 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
310 ' . $this->idfield
. ' AS id
311 FROM ' . $this->from
. '
314 GROUP BY ' . $this->valfield
. '
315 ORDER BY ' . $this->valfield
);
321 // {{{ class DE_WithSuboption
322 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
324 abstract class DE_WithSuboption
extends DirEnumeration
328 protected $suboptions = null
;
330 protected function loadOptions()
332 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
333 ' . $this->optfield
. ' AS subid,
334 ' . $this->idfield
. ' AS id
335 FROM ' . $this->from
. '
338 GROUP BY ' . $this->valfield
. '
339 ORDER BY ' . $this->valfield
);
340 $this->options
= array();
341 $this->suboptions
= array();
342 foreach ($opts as $id => $opt) {
343 $this->options
[$id] = $opt['field'];
344 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
345 $this->suboptions
[$opt['subid']] = array();
347 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
351 public function getOptions($subid = null
)
353 $this->_fetchOptions();
354 if ($subid == null
) {
355 return $this->options
;
356 } else if (array_key_exists($subid, $this->suboptions
)) {
357 return $this->suboptions
[$subid];
363 public function getOptionsIter($subid = null
)
365 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
368 public function getIDs($text, $mode, $subid = null
)
370 if ($mode == XDB
::WILDCARD_EXACT
) {
371 $options = $this->getOptions($subid);
372 return array_keys($options, $text);
374 if ($this->where
== null
) {
377 $where = $this->where
. ' AND ';
379 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
380 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
384 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
385 if ($this->valfield2
!= null
) {
386 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
388 $where .= '(' . implode(' OR ', $conds) . ')';
390 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
391 FROM ' . $this->from
. '
394 GROUP BY ' . $this->idfield
);
398 public function getAutoComplete($text, $subid = null
)
400 $text = str_replace(array('%', '_'), '', $text);
402 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
405 $where = $this->ac_where
. ' AND ';
408 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
409 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
412 $tests = $this->mkTests($this->valfield
, $text);
413 if (!is_null($this->valfield2
)) {
414 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
417 $where .= '(' . implode(' OR ', $tests) . ')';
419 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
420 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
421 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
422 FROM ' . $this->from
. '
423 ' . $this->ac_join
. '
425 GROUP BY ' . $this->valfield
. '
426 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
427 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
432 // {{{ class DE_NameTypes
433 // returns 'system' names ('lastname', 'lastname_marital', ...)
434 class DE_NameTypes
extends DirEnumeration
436 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
438 protected $from = 'profile_name_enum';
439 protected $valfield = 'type';
443 // {{{ class DE_Names
444 // returns 'system' names ('lastname', 'lastname_marital', ...)
445 class DE_Names
extends DirEnumeration
447 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
449 protected $from = 'profile_name_enum';
450 protected $idfield = 'type';
451 protected $valfield = 'name';
457 // {{{ class DE_Binets
458 class DE_Binets
extends DirEnumeration
460 protected $from = 'profile_binet_enum';
462 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
463 protected $ac_unique = 'profile_binets.pid';
467 // {{{ class DE_Sections
468 class DE_Sections
extends DirEnumeration
470 protected $from = 'profile_section_enum';
472 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
473 protected $ac_unique = 'profiles.pid';
477 // {{{ class DE_GroupesX
478 class DE_GroupesX
extends DirEnumeration
480 protected $idfield = 'groups.id';
481 protected $valfield = 'groups.nom';
482 protected $valfield2 = 'groups.diminutif';
483 protected $from = 'groups';
484 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
486 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
487 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
488 AND groups.pub = 'public')";
489 protected $ac_unique = 'group_members.uid';
495 // {{{ class DE_EducationSchools
496 class DE_EducationSchools
extends DirEnumeration
498 protected $idfield = 'profile_education_enum.id';
499 protected $valfield = 'profile_education_enum.name';
500 protected $valfield2 = 'profile_education_enum.abbreviation';
501 protected $from = 'profile_education_enum';
503 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
504 protected $ac_unique = 'profile_education.pid';
508 // {{{ class DE_EducationDegrees
509 class DE_EducationDegrees
extends DirEnumeration
511 public $capabilities = self
::HAS_OPTIONS
;
513 protected $idfield = 'profile_education_degree.degreeid';
514 protected $optfield = 'profile_education_degree.eduid';
515 protected $valfield = 'profile_education_degree_enum.degree';
516 protected $from = 'profile_education_degree_enum';
517 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
522 // {{{ class DE_EducationFields
523 class DE_EducationFields
extends DirEnumeration
525 protected $valfield = 'profile_education_field_enum.field';
526 protected $from = 'profile_education_field_enum';
528 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
529 protected $ac_unique = 'profile_education.pid';
533 // {{{ class DE_Corps
534 class DE_Corps
extends DirEnumeration
536 protected $idfield = 'profile_corps_enum.id';
537 protected $valfield = 'profile_corps_enum.name';
538 protected $valfield2 = 'profile_corps_enum.abbrev';
539 protected $from = 'profile_corps_enum';
541 protected $ac_unique = 'profile_corps.pid';
542 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.current_corpsid = profile_corps_enum.id)';
546 // {{{ class DE_CorpsRanks
547 class DE_CorpsRanks
extends DirEnumeration
549 protected $idfield = 'profile_corps_rank_enum.id';
550 protected $valfield = 'profile_corps_rank_enum.name';
551 protected $valfield2 = 'profile_corps_rank_enum.abbrev';
552 protected $from = 'profile_corps_rank_enum';
554 protected $ac_unique = 'profile_corps.pid';
555 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.rankid = profile_corps_rank_enum.id)';
561 // {{{ class DE_Nationalities
562 class DE_Nationalities
extends DirEnumeration
564 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
565 protected $valfield = 'geoloc_countries.nationalityFR';
566 protected $valfield2 = 'geoloc_countries.nationality';
567 protected $from = 'geoloc_countries';
568 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
570 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
571 protected $ac_unique = 'profiles.pid';
575 // {{{ class DE_Countries
576 class DE_Countries
extends DirEnumeration
578 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
579 protected $valfield = 'geoloc_countries.countryFR';
580 protected $valfield2 = 'geoloc_countries.country';
581 protected $from = 'geoloc_countries';
583 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
584 protected $ac_unique = 'profile_addresses.pid';
588 // {{{ class DE_AdminAreas
589 class DE_AdminAreas
extends DE_WithSuboption
591 protected $idfield = 'geoloc_administrativeareas.id';
592 protected $optfield = 'geoloc_administrativeareas.country';
593 protected $valfield = 'geoloc_administrativeareas.name';
594 protected $from = 'geoloc_administrativeareas';
596 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
597 protected $ac_unique = 'profile_addresses.pid';
601 // {{{ class DE_Localities
602 class DE_Localities
extends DirEnumeration
604 protected $idfield = 'geoloc_localities.id';
605 protected $valfield = 'geoloc_localities.name';
606 protected $from = 'geoloc_localities';
608 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
609 protected $ac_unique = 'profile_addresses.pid';
615 // {{{ class DE_Companies
616 class DE_Companies
extends DirEnumeration
618 protected $idfield = 'profile_job_enum.id';
619 protected $valfield = 'profile_job_enum.name';
620 protected $valfield2 = 'profile_job_enum.acronym';
621 protected $from = 'profile_job_enum';
623 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
624 protected $ac_unique = 'profile_job.pid';
628 // {{{ class DE_Sectors
629 class DE_Sectors
extends DirEnumeration
631 protected $idfield = 'profile_job_sector_enum.id';
632 protected $valfield = 'profile_job_sector_enum.name';
633 protected $from = 'profile_job_sector_enum';
635 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
636 protected $ac_unique = 'profile_job.pid';
640 // {{{ class DE_JobDescription
641 class DE_JobDescription
extends DirEnumeration
643 protected $valfield = 'profile_job.description';
644 protected $from = 'profile_job';
645 protected $idfield = 'profile_job.pid';
647 protected $ac_unique = 'profile_job.pid';
651 // {{{ class DE_JobTerms
652 class DE_JobTerms
extends DirEnumeration
654 // {{{ function getAutoComplete
655 public function getAutoComplete($text)
657 $tokens = JobTerms
::tokenize($text.'%');
658 if (count($tokens) == 0) {
659 return PlIteratorUtils
::fromArray(array());
661 $token_join = JobTerms
::token_join_query($tokens, 'e');
662 return XDB
::iterator('SELECT e.jtid AS id, e.full_name AS field, COUNT(DISTINCT p.pid) AS nb
663 FROM profile_job_term_enum AS e
664 INNER JOIN profile_job_term_relation AS r ON (r.jtid_1 = e.jtid)
665 INNER JOIN profile_job_term AS p ON (r.jtid_2 = p.jtid)
668 ORDER BY nb DESC, field
669 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
677 // {{{ class DE_Networking
678 class DE_Networking
extends DirEnumeration
680 protected $idfield = 'profile_networking_enum.nwid';
681 protected $valfield = 'profile_networking_enum.name';
682 protected $from = 'profile_networking_enum';
685 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
686 protected $ac_unique = 'profile_networking.pid';
692 // {{{ class DE_Medals
693 class DE_Medals
extends DirEnumeration
695 protected $from = 'profile_medal_enum';
697 protected $ac_join = 'INNER JOIN profile_medals ON (profile_medals.mid = profile_medal_enum.id)';
698 protected $ac_unique = 'profile_medals.pid';
702 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: