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 NATIONALITIES
= 'nationalities';
44 const COUNTRIES
= 'countries';
45 const ADMINAREAS
= 'adminareas';
46 const LOCALITIES
= 'localities';
48 const COMPANIES
= 'companies';
49 const SECTORS
= 'sectors';
50 const JOBDESCRIPTION
= 'jobdescription';
52 const NETWORKS
= 'networking';
54 static private $enumerations = array();
56 static private function init($type)
58 if (S
::has('__DE_' . $type)) {
59 self
::$enumerations[$type] = S
::v('__DE_' . $type);
61 $cls = "DE_" . ucfirst($type);
63 self
::$enumerations[$type] = $obj;
64 if ($obj->capabilities
& DirEnumeration
::SAVE_IN_SESSION
) {
65 S
::set('__DE_' . $type, $obj);
70 /** Retrieves all options for a given type
71 * @param $type Type of enum for which options are requested
72 * @return Array of the results
74 static public function getOptions($type)
76 if (!array_key_exists($type, self
::$enumerations)) {
79 $obj = self
::$enumerations[$type];
80 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
81 return call_user_func(array($obj, 'getOptions'));
87 /** Retrieves all options for a given type
88 * @param $type Type of enum for which options are requested
89 * @return PlIterator over the results
91 static public function getOptionsIter($type)
93 if (!array_key_exists($type, self
::$enumerations)) {
96 $obj = self
::$enumerations[$type];
97 $args = func_get_args();
99 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
100 return call_user_func_array(array($obj, 'getOptionsIter'), $args);
102 return PlIteratorUtils
::fromArray(array());
106 /** Retrieves all options with number of profiles for autocompletion
107 * @param $type Type of enum for which options are requested
108 * @param $text Text to autocomplete
109 * @return PlIterator over the results
111 static public function getAutoComplete($type, $text)
113 if (!array_key_exists($type, self
::$enumerations)) {
116 $obj = self
::$enumerations[$type];
117 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
118 return call_user_func(array($obj, 'getAutoComplete'), $text);
120 return PlIteratorUtils
::fromArray(array());
124 /** Retrieves a list of IDs for a given type
125 * @param $type Type of enum for which IDs are requested
126 * @param $text Text to search in enum valuees
127 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
129 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
131 if (!array_key_exists($type, self
::$enumerations)) {
134 $obj = self
::$enumerations[$type];
135 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
136 return call_user_func(array($obj, 'getIDs'), $text, $mode);
142 /** Retrieves a single ID for a given type.
143 * @param $type Type of the enum for which an ID is requested
144 * @param $text Text to search in enum values
145 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
147 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
149 $ids = self
::getIDs($type, $text, $mode);
150 return array_shift($ids);
155 // {{{ class DirEnumeration
156 abstract class DirEnumeration
158 const AUTOCOMPLETE_LIMIT
= 11;
160 const HAS_OPTIONS
= 0x001;
161 const HAS_AUTOCOMP
= 0x002;
162 const SAVE_IN_SESSION
= 0x004;
164 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
166 /** An internal array of ID => optionTxt
168 protected $options = null
;
170 /** Description of the MySQL storage of the fields
172 protected $idfield = 'id';
173 protected $valfield = 'text';
174 protected $valfield2 = null
;
176 protected $join = '';
177 protected $where = '';
179 /** Fields for autocompletion
181 protected $ac_join = ''; // Additional joins
182 protected $ac_where = null
; // Additional where
183 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
184 protected $ac_unique; // Which field is to be taken as unique
185 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
186 protected $ac_withid = true
; // Do we want to fetch id too ?
188 protected function _fetchOptions()
190 if (is_null($this->options
)) {
191 $this->loadOptions();
195 public function getOptions()
197 $this->_fetchOptions();
198 return $this->options
;
201 public function getOptionsIter()
203 $options = $this->getOptions();
204 $options = self
::expandArray($options);
205 return PlIteratorUtils
::fromArray($options, 1, true
);
208 // {{{ function getIDs
209 /** Retrieves possible IDs for given text
210 * @param $text Text to search for IDs
211 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
212 * @return An array of matching IDs ; if empty, input should be considered invalid
214 public function getIDs($text, $mode)
216 if ($mode == XDB
::WILDCARD_EXACT
) {
217 $options = $this->getOptions();
218 return array_keys($options, $text);
220 if ($this->where
== null
) {
223 $where = $this->where
. ' AND ';
226 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
227 if ($this->valfield2
!= null
) {
228 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
230 $where .= '(' . implode(' OR ', $conds) . ')';
232 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
233 FROM ' . $this->from
. '
236 GROUP BY ' . $this->idfield
);
241 /** Builds a list of query parts for searching @$text in @$field :
242 * field LIKE 'text%', field LIKE '% text%', field LIKE '%-text%'
244 private function mkTests($field, $text)
247 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
248 if (!$this->ac_beginwith
) {
249 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
250 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
255 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
258 foreach ($tab as $key => $val) {
267 // {{{ function getAutoComplete
268 public function getAutoComplete($text)
270 $text = str_replace(array('%', '_'), '', $text);
272 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
275 $where = $this->ac_where
. ' AND ';
278 $tests = $this->mkTests($this->valfield
, $text);
279 if (!is_null($this->valfield2
)) {
280 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
283 $where .= '(' . implode(' OR ', $tests) . ')';
285 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
286 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
287 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
288 FROM ' . $this->from
. '
289 ' . $this->ac_join
. '
291 GROUP BY ' . $this->valfield
. '
292 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
293 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
297 // {{{ function loadOptions
298 /** The function used to load options
300 protected function loadOptions()
302 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
303 ' . $this->idfield
. ' AS id
304 FROM ' . $this->from
. '
307 GROUP BY ' . $this->valfield
. '
308 ORDER BY ' . $this->valfield
);
314 // {{{ class DE_WithSuboption
315 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
317 abstract class DE_WithSuboption
extends DirEnumeration
321 protected $suboptions = null
;
323 protected function loadOptions()
325 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
326 ' . $this->optfield
. ' AS subid,
327 ' . $this->idfield
. ' AS id
328 FROM ' . $this->from
. '
331 GROUP BY ' . $this->valfield
. '
332 ORDER BY ' . $this->valfield
);
333 $this->options
= array();
334 $this->suboptions
= array();
335 foreach ($opts as $id => $opt) {
336 $this->options
[$id] = $opt['field'];
337 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
338 $this->suboptions
[$opt['subid']] = array();
340 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
344 public function getOptions($subid = null
)
346 $this->_fetchOptions();
347 if ($subid == null
) {
348 return $this->options
;
349 } else if (array_key_exists($subid, $this->suboptions
)) {
350 return $this->suboptions
[$subid];
356 public function getOptionsIter($subid = null
)
358 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
361 public function getIDs($text, $mode, $subid = null
)
363 if ($mode == XDB
::WILDCARD_EXACT
) {
364 $options = $this->getOptions($subid);
365 return array_keys($options, $text);
367 if ($this->where
== null
) {
370 $where = $this->where
. ' AND ';
372 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
373 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
377 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
378 if ($this->valfield2
!= null
) {
379 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
381 $where .= '(' . implode(' OR ', $conds) . ')';
383 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
384 FROM ' . $this->from
. '
387 GROUP BY ' . $this->idfield
);
391 public function getAutoComplete($text, $subid = null
)
393 $text = str_replace(array('%', '_'), '', $text);
395 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
398 $where = $this->ac_where
. ' AND ';
401 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
402 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
405 $tests = $this->mkTests($this->valfield
, $text);
406 if (!is_null($this->valfield2
)) {
407 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
410 $where .= '(' . implode(' OR ', $tests) . ')';
412 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
413 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
414 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
415 FROM ' . $this->from
. '
416 ' . $this->ac_join
. '
418 GROUP BY ' . $this->valfield
. '
419 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
420 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
425 // {{{ class DE_NameTypes
426 // returns 'system' names ('lastname', 'lastname_marital', ...)
427 class DE_NameTypes
extends DirEnumeration
429 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
431 protected $from = 'profile_name_enum';
432 protected $valfield = 'type';
436 // {{{ class DE_Names
437 // returns 'system' names ('lastname', 'lastname_marital', ...)
438 class DE_Names
extends DirEnumeration
440 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
442 protected $from = 'profile_name_enum';
443 protected $idfield = 'type';
444 protected $valfield = 'name';
450 // {{{ class DE_Binets
451 class DE_Binets
extends DirEnumeration
453 protected $from = 'profile_binet_enum';
455 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
456 protected $ac_unique = 'profile_binets.pid';
460 // {{{ class DE_Sections
461 class DE_Sections
extends DirEnumeration
463 protected $from = 'profile_section_enum';
465 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
466 protected $ac_unique = 'profiles.pid';
470 // {{{ class DE_GroupesX
471 class DE_GroupesX
extends DirEnumeration
473 protected $idfield = 'groups.id';
474 protected $valfield = 'groups.nom';
475 protected $valfield2 = 'groups.diminutif';
476 protected $from = 'groups';
477 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
479 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
480 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
481 AND groups.pub = 'public')";
482 protected $ac_unique = 'group_members.uid';
488 // {{{ class DE_EducationSchools
489 class DE_EducationSchools
extends DirEnumeration
491 protected $idfield = 'profile_education_enum.id';
492 protected $valfield = 'profile_education_enum.name';
493 protected $valfield2 = 'profile_education_enum.abbreviation';
494 protected $from = 'profile_education_enum';
496 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
497 protected $ac_unique = 'profile_education.pid';
501 // {{{ class DE_EducationDegrees
502 class DE_EducationDegrees
extends DirEnumeration
504 public $capabilities = self
::HAS_OPTIONS
;
506 protected $idfield = 'profile_education_degree.degreeid';
507 protected $optfield = 'profile_education_degree.eduid';
508 protected $valfield = 'profile_education_degree_enum.degree';
509 protected $from = 'profile_education_degree_enum';
510 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
515 // {{{ class DE_EducationFields
516 class DE_EducationFields
extends DirEnumeration
518 protected $valfield = 'profile_education_field_enum.field';
519 protected $from = 'profile_education_field_enum';
521 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
522 protected $ac_unique = 'profile_education.pid';
528 // {{{ class DE_Nationalities
529 class DE_Nationalities
extends DirEnumeration
531 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
532 protected $valfield = 'geoloc_countries.nationalityFR';
533 protected $valfield2 = 'geoloc_countries.nationality';
534 protected $from = 'geoloc_countries';
535 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
537 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
538 protected $ac_unique = 'profiles.pid';
542 // {{{ class DE_Countries
543 class DE_Countries
extends DirEnumeration
545 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
546 protected $valfield = 'geoloc_countries.countryFR';
547 protected $valfield2 = 'geoloc_countries.country';
548 protected $from = 'geoloc_countries';
550 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
551 protected $ac_unique = 'profile_addresses.pid';
555 // {{{ class DE_AdminAreas
556 class DE_AdminAreas
extends DE_WithSuboption
558 protected $idfield = 'geoloc_administrativeareas.id';
559 protected $optfield = 'geoloc_administrativeareas.country';
560 protected $valfield = 'geoloc_administrativeareas.name';
561 protected $from = 'geoloc_administrativeareas';
563 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
564 protected $ac_unique = 'profile_addresses.pid';
568 // {{{ class DE_Localities
569 class DE_Localities
extends DirEnumeration
571 protected $idfield = 'geoloc_localities.id';
572 protected $valfield = 'geoloc_localities.name';
573 protected $from = 'geoloc_localities';
575 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
576 protected $ac_unique = 'profile_addresses.pid';
582 // {{{ class DE_Companies
583 class DE_Companies
extends DirEnumeration
585 protected $idfield = 'profile_job_enum.id';
586 protected $valfield = 'profile_job_enum.name';
587 protected $valfield2 = 'profile_job_enum.acronym';
588 protected $from = 'profile_job_enum';
590 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
591 protected $ac_unique = 'profile_job.pid';
595 // {{{ class DE_Sectors
596 class DE_Sectors
extends DirEnumeration
598 protected $idfield = 'profile_job_sector_enum.id';
599 protected $valfield = 'profile_job_sector_enum.name';
600 protected $from = 'profile_job_sector_enum';
602 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
603 protected $ac_unique = 'profile_job.pid';
607 // {{{ class DE_JobDescription
608 class DE_JobDescription
extends DirEnumeration
610 protected $valfield = 'profile_job.description';
611 protected $from = 'profile_job';
612 protected $idfield = 'profile_job.pid';
614 protected $ac_unique = 'profile_job.pid';
620 // {{{ class DE_Networking
621 class DE_Networking
extends DirEnumeration
623 protected $idfield = 'profile_networking_enum.nwid';
624 protected $valfield = 'profile_networking_enum.name';
625 protected $from = 'profile_networking_enum';
628 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
629 protected $ac_unique = 'profile_networking.pid';
635 // {{{ class DE_Medals
636 class DE_Medals
extends DirEnumeration
638 protected $from = 'profile_medal_enum';
640 protected $ac_join = 'INNER JOIN profile_medals ON (profile_medals.mid = profile_medal_enum.id)';
641 protected $ac_unique = 'profile_medals.pid';
645 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: