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 (S
::has('__DE_' . $type)) {
65 self
::$enumerations[$type] = S
::v('__DE_' . $type);
67 $cls = "DE_" . ucfirst($type);
69 self
::$enumerations[$type] = $obj;
70 if ($obj->capabilities
& DirEnumeration
::SAVE_IN_SESSION
) {
71 S
::set('__DE_' . $type, $obj);
76 /** Retrieves all options for a given type
77 * @param $type Type of enum for which options are requested
78 * @return Array of the results
80 static public function getOptions($type)
82 if (!array_key_exists($type, self
::$enumerations)) {
85 $obj = self
::$enumerations[$type];
86 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
87 return call_user_func(array($obj, 'getOptions'));
93 /** Retrieves all options for a given type
94 * @param $type Type of enum for which options are requested
95 * @return PlIterator over the results
97 static public function getOptionsIter($type)
99 if (!array_key_exists($type, self
::$enumerations)) {
102 $obj = self
::$enumerations[$type];
103 $args = func_get_args();
105 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
106 return call_user_func_array(array($obj, 'getOptionsIter'), $args);
108 return PlIteratorUtils
::fromArray(array());
112 /** Retrieves all options with number of profiles for autocompletion
113 * @param $type Type of enum for which options are requested
114 * @param $text Text to autocomplete
115 * @return PlIterator over the results
117 static public function getAutoComplete($type, $text)
119 if (!array_key_exists($type, self
::$enumerations)) {
122 $obj = self
::$enumerations[$type];
123 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
124 return call_user_func(array($obj, 'getAutoComplete'), $text);
126 return PlIteratorUtils
::fromArray(array());
130 /** Retrieves a list of IDs for a given type
131 * @param $type Type of enum for which IDs are requested
132 * @param $text Text to search in enum valuees
133 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
135 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
137 if (!array_key_exists($type, self
::$enumerations)) {
140 $obj = self
::$enumerations[$type];
141 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
142 return call_user_func(array($obj, 'getIDs'), $text, $mode);
148 /** Retrieves a single ID for a given type.
149 * @param $type Type of the enum for which an ID is requested
150 * @param $text Text to search in enum values
151 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
153 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
155 $ids = self
::getIDs($type, $text, $mode);
156 return array_shift($ids);
161 // {{{ class DirEnumeration
162 abstract class DirEnumeration
164 const AUTOCOMPLETE_LIMIT
= 11;
166 const HAS_OPTIONS
= 0x001;
167 const HAS_AUTOCOMP
= 0x002;
168 const SAVE_IN_SESSION
= 0x004;
170 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
172 /** An internal array of ID => optionTxt
174 protected $options = null
;
176 /** Description of the MySQL storage of the fields
178 protected $idfield = 'id';
179 protected $valfield = 'text';
180 protected $valfield2 = null
;
182 protected $join = '';
183 protected $where = '';
185 /** Fields for autocompletion
187 protected $ac_join = ''; // Additional joins
188 protected $ac_where = null
; // Additional where
189 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
190 protected $ac_unique; // Which field is to be taken as unique
191 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
192 protected $ac_withid = true
; // Do we want to fetch id too ?
194 protected function _fetchOptions()
196 if (is_null($this->options
)) {
197 $this->loadOptions();
201 public function getOptions()
203 $this->_fetchOptions();
204 return $this->options
;
207 public function getOptionsIter()
209 $options = $this->getOptions();
210 $options = self
::expandArray($options);
211 return PlIteratorUtils
::fromArray($options, 1, true
);
214 // {{{ function getIDs
215 /** Retrieves possible IDs for given text
216 * @param $text Text to search for IDs
217 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
218 * @return An array of matching IDs ; if empty, input should be considered invalid
220 public function getIDs($text, $mode)
222 if ($mode == XDB
::WILDCARD_EXACT
) {
223 $options = $this->getOptions();
224 return array_keys($options, $text);
226 if ($this->where
== null
) {
229 $where = $this->where
. ' AND ';
232 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
233 if ($this->valfield2
!= null
) {
234 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
236 $where .= '(' . implode(' OR ', $conds) . ')';
238 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
239 FROM ' . $this->from
. '
242 GROUP BY ' . $this->idfield
);
247 /** Builds a list of query parts for searching @$text in @$field :
248 * field LIKE 'text%', field LIKE '% text%', field LIKE '%-text%'
250 private function mkTests($field, $text)
253 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
254 if (!$this->ac_beginwith
) {
255 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
256 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
261 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
264 foreach ($tab as $key => $val) {
273 // {{{ function getAutoComplete
274 public function getAutoComplete($text)
276 $text = str_replace(array('%', '_'), '', $text);
278 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
281 $where = $this->ac_where
. ' AND ';
284 $tests = $this->mkTests($this->valfield
, $text);
285 if (!is_null($this->valfield2
)) {
286 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
289 $where .= '(' . implode(' OR ', $tests) . ')';
291 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
292 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
293 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
294 FROM ' . $this->from
. '
295 ' . $this->ac_join
. '
297 GROUP BY ' . $this->valfield
. '
298 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
299 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
303 // {{{ function loadOptions
304 /** The function used to load options
306 protected function loadOptions()
308 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
309 ' . $this->idfield
. ' AS id
310 FROM ' . $this->from
. '
313 GROUP BY ' . $this->valfield
. '
314 ORDER BY ' . $this->valfield
);
320 // {{{ class DE_WithSuboption
321 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
323 abstract class DE_WithSuboption
extends DirEnumeration
327 protected $suboptions = null
;
329 protected function loadOptions()
331 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
332 ' . $this->optfield
. ' AS subid,
333 ' . $this->idfield
. ' AS id
334 FROM ' . $this->from
. '
337 GROUP BY ' . $this->valfield
. '
338 ORDER BY ' . $this->valfield
);
339 $this->options
= array();
340 $this->suboptions
= array();
341 foreach ($opts as $id => $opt) {
342 $this->options
[$id] = $opt['field'];
343 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
344 $this->suboptions
[$opt['subid']] = array();
346 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
350 public function getOptions($subid = null
)
352 $this->_fetchOptions();
353 if ($subid == null
) {
354 return $this->options
;
355 } else if (array_key_exists($subid, $this->suboptions
)) {
356 return $this->suboptions
[$subid];
362 public function getOptionsIter($subid = null
)
364 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
367 public function getIDs($text, $mode, $subid = null
)
369 if ($mode == XDB
::WILDCARD_EXACT
) {
370 $options = $this->getOptions($subid);
371 return array_keys($options, $text);
373 if ($this->where
== null
) {
376 $where = $this->where
. ' AND ';
378 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
379 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
383 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
384 if ($this->valfield2
!= null
) {
385 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
387 $where .= '(' . implode(' OR ', $conds) . ')';
389 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
390 FROM ' . $this->from
. '
393 GROUP BY ' . $this->idfield
);
397 public function getAutoComplete($text, $subid = null
)
399 $text = str_replace(array('%', '_'), '', $text);
401 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
404 $where = $this->ac_where
. ' AND ';
407 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
408 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
411 $tests = $this->mkTests($this->valfield
, $text);
412 if (!is_null($this->valfield2
)) {
413 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
416 $where .= '(' . implode(' OR ', $tests) . ')';
418 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
419 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
420 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
421 FROM ' . $this->from
. '
422 ' . $this->ac_join
. '
424 GROUP BY ' . $this->valfield
. '
425 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
426 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
431 // {{{ class DE_NameTypes
432 // returns 'system' names ('lastname', 'lastname_marital', ...)
433 class DE_NameTypes
extends DirEnumeration
435 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
437 protected $from = 'profile_name_enum';
438 protected $valfield = 'type';
442 // {{{ class DE_Names
443 // returns 'system' names ('lastname', 'lastname_marital', ...)
444 class DE_Names
extends DirEnumeration
446 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
448 protected $from = 'profile_name_enum';
449 protected $idfield = 'type';
450 protected $valfield = 'name';
456 // {{{ class DE_Binets
457 class DE_Binets
extends DirEnumeration
459 protected $from = 'profile_binet_enum';
461 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
462 protected $ac_unique = 'profile_binets.pid';
466 // {{{ class DE_Sections
467 class DE_Sections
extends DirEnumeration
469 protected $from = 'profile_section_enum';
471 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
472 protected $ac_unique = 'profiles.pid';
476 // {{{ class DE_GroupesX
477 class DE_GroupesX
extends DirEnumeration
479 protected $idfield = 'groups.id';
480 protected $valfield = 'groups.nom';
481 protected $valfield2 = 'groups.diminutif';
482 protected $from = 'groups';
483 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
485 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
486 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
487 AND groups.pub = 'public')";
488 protected $ac_unique = 'group_members.uid';
494 // {{{ class DE_EducationSchools
495 class DE_EducationSchools
extends DirEnumeration
497 protected $idfield = 'profile_education_enum.id';
498 protected $valfield = 'profile_education_enum.name';
499 protected $valfield2 = 'profile_education_enum.abbreviation';
500 protected $from = 'profile_education_enum';
502 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
503 protected $ac_unique = 'profile_education.pid';
507 // {{{ class DE_EducationDegrees
508 class DE_EducationDegrees
extends DirEnumeration
510 public $capabilities = self
::HAS_OPTIONS
;
512 protected $idfield = 'profile_education_degree.degreeid';
513 protected $optfield = 'profile_education_degree.eduid';
514 protected $valfield = 'profile_education_degree_enum.degree';
515 protected $from = 'profile_education_degree_enum';
516 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
521 // {{{ class DE_EducationFields
522 class DE_EducationFields
extends DirEnumeration
524 protected $valfield = 'profile_education_field_enum.field';
525 protected $from = 'profile_education_field_enum';
527 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
528 protected $ac_unique = 'profile_education.pid';
532 // {{{ class DE_Corps
533 class DE_Corps
extends DirEnumeration
535 protected $idfield = 'profile_corps_enum.id';
536 protected $valfield = 'profile_corps_enum.name';
537 protected $valfield2 = 'profile_corps_enum.abbrev';
538 protected $from = 'profile_corps_enum';
540 protected $ac_unique = 'profile_corps.pid';
541 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.current_corpsid = profile_corps_enum.id)';
545 // {{{ class DE_CorpsRanks
546 class DE_CorpsRanks
extends DirEnumeration
548 protected $idfield = 'profile_corps_rank_enum.id';
549 protected $valfield = 'profile_corps_rank_enum.name';
550 protected $valfield2 = 'profile_corps_rank_enum.abbrev';
551 protected $from = 'profile_corps_rank_enum';
553 protected $ac_unique = 'profile_corps.pid';
554 protected $ac_join = 'INNER JOIN profile_corps ON (profile_corps.rankid = profile_corps_rank_enum.id)';
560 // {{{ class DE_Nationalities
561 class DE_Nationalities
extends DirEnumeration
563 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
564 protected $valfield = 'geoloc_countries.nationalityFR';
565 protected $valfield2 = 'geoloc_countries.nationality';
566 protected $from = 'geoloc_countries';
567 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
569 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
570 protected $ac_unique = 'profiles.pid';
574 // {{{ class DE_Countries
575 class DE_Countries
extends DirEnumeration
577 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
578 protected $valfield = 'geoloc_countries.countryFR';
579 protected $valfield2 = 'geoloc_countries.country';
580 protected $from = 'geoloc_countries';
582 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
583 protected $ac_unique = 'profile_addresses.pid';
587 // {{{ class DE_AdminAreas
588 class DE_AdminAreas
extends DE_WithSuboption
590 protected $idfield = 'geoloc_administrativeareas.id';
591 protected $optfield = 'geoloc_administrativeareas.country';
592 protected $valfield = 'geoloc_administrativeareas.name';
593 protected $from = 'geoloc_administrativeareas';
595 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
596 protected $ac_unique = 'profile_addresses.pid';
600 // {{{ class DE_Localities
601 class DE_Localities
extends DirEnumeration
603 protected $idfield = 'geoloc_localities.id';
604 protected $valfield = 'geoloc_localities.name';
605 protected $from = 'geoloc_localities';
607 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
608 protected $ac_unique = 'profile_addresses.pid';
614 // {{{ class DE_Companies
615 class DE_Companies
extends DirEnumeration
617 protected $idfield = 'profile_job_enum.id';
618 protected $valfield = 'profile_job_enum.name';
619 protected $valfield2 = 'profile_job_enum.acronym';
620 protected $from = 'profile_job_enum';
622 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
623 protected $ac_unique = 'profile_job.pid';
627 // {{{ class DE_Sectors
628 class DE_Sectors
extends DirEnumeration
630 protected $idfield = 'profile_job_sector_enum.id';
631 protected $valfield = 'profile_job_sector_enum.name';
632 protected $from = 'profile_job_sector_enum';
634 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
635 protected $ac_unique = 'profile_job.pid';
639 // {{{ class DE_JobDescription
640 class DE_JobDescription
extends DirEnumeration
642 protected $valfield = 'profile_job.description';
643 protected $from = 'profile_job';
644 protected $idfield = 'profile_job.pid';
646 protected $ac_unique = 'profile_job.pid';
650 // {{{ class DE_JobTerms
651 class DE_JobTerms
extends DirEnumeration
653 // {{{ function getAutoComplete
654 public function getAutoComplete($text)
656 $tokens = JobTerms
::tokenize($text.'%');
657 if (count($tokens) == 0) {
658 return PlIteratorUtils
::fromArray(array());
660 $token_join = JobTerms
::token_join_query($tokens, 'e');
661 return XDB
::iterator('SELECT e.jtid AS id, e.full_name AS field, COUNT(DISTINCT p.pid) AS nb
662 FROM profile_job_term_enum AS e
663 INNER JOIN profile_job_term_relation AS r ON (r.jtid_1 = e.jtid)
664 INNER JOIN profile_job_term AS p ON (r.jtid_2 = p.jtid)
667 ORDER BY nb DESC, field
668 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
676 // {{{ class DE_Networking
677 class DE_Networking
extends DirEnumeration
679 protected $idfield = 'profile_networking_enum.nwid';
680 protected $valfield = 'profile_networking_enum.name';
681 protected $from = 'profile_networking_enum';
684 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
685 protected $ac_unique = 'profile_networking.pid';
691 // {{{ class DE_Medals
692 class DE_Medals
extends DirEnumeration
694 protected $from = 'profile_medal_enum';
696 protected $ac_join = 'INNER JOIN profile_medals ON (profile_medals.mid = profile_medal_enum.id)';
697 protected $ac_unique = 'profile_medals.pid';
701 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: