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 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
98 return call_user_func(array($obj, 'getOptionsIter'));
100 return PlIteratorUtils
::fromArray(array());
104 /** Retrieves all options with number of profiles for autocompletion
105 * @param $type Type of enum for which options are requested
106 * @param $text Text to autocomplete
107 * @return PlIterator over the results
109 static public function getAutoComplete($type, $text)
111 if (!array_key_exists($type, self
::$enumerations)) {
114 $obj = self
::$enumerations[$type];
115 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
116 return call_user_func(array($obj, 'getAutoComplete'), $text);
118 return PlIteratorUtils
::fromArray(array());
122 /** Retrieves a list of IDs for a given type
123 * @param $type Type of enum for which IDs are requested
124 * @param $text Text to search in enum valuees
125 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
127 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
129 if (!array_key_exists($type, self
::$enumerations)) {
132 $obj = self
::$enumerations[$type];
133 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
134 return call_user_func(array($obj, 'getIDs'), $text, $mode);
140 /** Retrieves a single ID for a given type.
141 * @param $type Type of the enum for which an ID is requested
142 * @param $text Text to search in enum values
143 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
145 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
147 $ids = self
::getIDs($type, $text, $mode);
148 return array_shift($ids);
153 // {{{ class DirEnumeration
154 abstract class DirEnumeration
156 const AUTOCOMPLETE_LIMIT
= 11;
158 const HAS_OPTIONS
= 0x001;
159 const HAS_AUTOCOMP
= 0x002;
160 const SAVE_IN_SESSION
= 0x004;
162 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
164 /** An internal array of ID => optionTxt
166 protected $options = null
;
168 /** Description of the MySQL storage of the fields
170 protected $idfield = 'id';
171 protected $valfield = 'text';
172 protected $valfield2 = null
;
174 protected $join = '';
175 protected $where = '';
177 /** Fields for autocompletion
179 protected $ac_join = ''; // Additional joins
180 protected $ac_where = null
; // Additional where
181 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
182 protected $ac_unique; // Which field is to be taken as unique
183 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
184 protected $ac_withid = true
; // Do we want to fetch id too ?
186 protected function _fetchOptions()
188 if (is_null($this->options
)) {
189 $this->loadOptions();
193 public function getOptions()
195 $this->_fetchOptions();
196 return $this->options
;
199 public function getOptionsIter()
201 $options = $this->getOptions();
202 $options = self
::expandArray($options);
203 return PlIteratorUtils
::fromArray($options, 1, true
);
206 // {{{ function getIDs
207 /** Retrieves possible IDs for given text
208 * @param $text Text to search for IDs
209 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
210 * @return An array of matching IDs ; if empty, input should be considered invalid
212 public function getIDs($text, $mode)
214 if ($mode == XDB
::WILDCARD_EXACT
) {
215 $options = $this->getOptions();
216 return array_keys($options, $text);
218 if ($this->where
== null
) {
221 $where = $this->where
. ' AND ';
224 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
225 if ($this->valfield2
!= null
) {
226 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
228 $where .= '(' . implode(' OR ', $conds) . ')';
230 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
231 FROM ' . $this->from
. '
234 GROUP BY ' . $this->idfield
);
239 private function mkTests($field, $text)
242 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
243 if (!$this->ac_beginwith
) {
244 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
245 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
250 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
253 foreach ($tab as $key => $val) {
262 // {{{ function getAutoComplete
263 public function getAutoComplete($text)
265 $text = str_replace(array('%', '_'), '', $text);
267 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
270 $where = $this->ac_where
. ' AND ';
273 $tests = $this->mkTests($this->valfield
, $text);
274 if (!is_null($this->valfield2
)) {
275 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
278 $where .= '(' . implode(' OR ', $tests) . ')';
280 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
281 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
282 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
283 FROM ' . $this->from
. '
284 ' . $this->ac_join
. '
286 GROUP BY ' . $this->valfield
. '
287 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
288 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
292 // {{{ function loadOptions
293 /** The function used to load options
295 protected function loadOptions()
297 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
298 ' . $this->idfield
. ' AS id
299 FROM ' . $this->from
. '
302 GROUP BY ' . $this->valfield
. '
303 ORDER BY ' . $this->valfield
);
309 // {{{ class DE_WithSuboption
310 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
312 abstract class DE_WithSuboption
extends DirEnumeration
316 protected $suboptions = null
;
318 protected function loadOptions()
320 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
321 ' . $this->optfield
. ' AS subid,
322 ' . $this->idfield
. ' AS id
323 FROM ' . $this->from
. '
326 GROUP BY ' . $this->valfield
. '
327 ORDER BY ' . $this->valfield
);
328 $this->options
= array();
329 $this->suboptions
= array();
330 foreach ($opts as $id => $opt) {
331 $this->options
[$id] = $opt['field'];
332 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
333 $this->suboptions
[$opt['subid']] = array();
335 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
339 public function getOptions($subid = null
)
341 $this->_fetchOptions();
342 if ($subid == null
) {
343 return $this->options
;
344 } else if (array_key_exists($subid, $this->suboptions
)) {
345 return $this->suboptions
[$subid];
351 public function getOptionsIter($subid = null
)
353 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
356 public function getIDs($text, $mode, $subid = null
)
358 if ($mode == XDB
::WILDCARD_EXACT
) {
359 $options = $this->getOptions($subid);
360 return array_keys($options, $text);
362 if ($this->where
== null
) {
365 $where = $this->where
. ' AND ';
367 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
368 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
372 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
373 if ($this->valfield2
!= null
) {
374 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
376 $where .= '(' . implode(' OR ', $conds) . ')';
378 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
379 FROM ' . $this->from
. '
382 GROUP BY ' . $this->idfield
);
386 public function getAutoComplete($text, $subid = null
)
388 $text = str_replace(array('%', '_'), '', $text);
390 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
393 $where = $this->ac_where
. ' AND ';
396 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
397 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
400 $tests = $this->mkTests($this->valfield
, $text);
401 if (!is_null($this->valfield2
)) {
402 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
405 $where .= '(' . implode(' OR ', $tests) . ')';
407 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
408 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
409 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
410 FROM ' . $this->from
. '
411 ' . $this->ac_join
. '
413 GROUP BY ' . $this->valfield
. '
414 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
415 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
420 // {{{ class DE_NameTypes
421 // returns 'system' names ('lastname', 'lastname_marital', ...)
422 class DE_NameTypes
extends DirEnumeration
424 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
426 protected $from = 'profile_name_enum';
427 protected $valfield = 'type';
431 // {{{ class DE_Names
432 // returns 'system' names ('lastname', 'lastname_marital', ...)
433 class DE_Names
extends DirEnumeration
435 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
437 protected $from = 'profile_name_enum';
438 protected $idfield = 'type';
439 protected $valfield = 'name';
445 // {{{ class DE_Binets
446 class DE_Binets
extends DirEnumeration
448 protected $from = 'profile_binet_enum';
450 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
451 protected $ac_unique = 'profile_binets.pid';
455 // {{{ class DE_Sections
456 class DE_Sections
extends DirEnumeration
458 protected $from = 'profile_section_enum';
460 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
461 protected $ac_unique = 'profiles.pid';
465 // {{{ class DE_GroupesX
466 class DE_GroupesX
extends DirEnumeration
468 protected $idfield = 'groups.id';
469 protected $valfield = 'groups.nom';
470 protected $valfield2 = 'groups.diminutif';
471 protected $from = 'groups';
472 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
474 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
475 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
476 AND groups.pub = 'public')";
477 protected $ac_unique = 'group_members.uid';
483 // {{{ class DE_EducationSchools
484 class DE_EducationSchools
extends DirEnumeration
486 protected $idfield = 'profile_education_enum.id';
487 protected $valfield = 'profile_education_enum.name';
488 protected $valfield2 = 'profile_education_enum.abbreviation';
489 protected $from = 'profile_education_enum';
491 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
492 protected $ac_unique = 'profile_education.pid';
496 // {{{ class DE_EducationDegrees
497 class DE_EducationDegrees
extends DirEnumeration
499 public $capabilities = self
::HAS_OPTIONS
;
501 protected $idfield = 'profile_education_degree.degreeid';
502 protected $optfield = 'profile_education_degree.eduid';
503 protected $valfield = 'profile_education_degree_enum.degree';
504 protected $from = 'profile_education_degree_enum';
505 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
510 // {{{ class DE_EducationFields
511 class DE_EducationFields
extends DirEnumeration
513 protected $valfield = 'profile_education_field_enum.field';
514 protected $from = 'profile_education_field_enum';
516 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
517 protected $ac_unique = 'profile_education.pid';
523 // {{{ class DE_Nationalities
524 class DE_Nationalities
extends DirEnumeration
526 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
527 protected $valfield = 'geoloc_countries.nationalityFR';
528 protected $valfield2 = 'geoloc_countries.nationality';
529 protected $from = 'geoloc_countries';
530 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
532 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
533 protected $ac_unique = 'profiles.pid';
537 // {{{ class DE_Countries
538 class DE_Countries
extends DirEnumeration
540 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
541 protected $valfield = 'geoloc_countries.countryFR';
542 protected $valfield2 = 'geoloc_countries.country';
543 protected $from = 'geoloc_countries';
545 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
546 protected $ac_unique = 'profile_addresses.pid';
550 // {{{ class DE_AdminAreas
551 class DE_AdminAreas
extends DE_WithSuboption
553 protected $idfield = 'geoloc_administrativeareas.id';
554 protected $optfield = 'geoloc_administrativeareas.country';
555 protected $valfield = 'geoloc_administrativeareas.name';
556 protected $from = 'geoloc_administrativeareas';
558 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
559 protected $ac_unique = 'profile_addresses.pid';
563 // {{{ class DE_Localities
564 class DE_Localities
extends DirEnumeration
566 protected $idfield = 'geoloc_localities.id';
567 protected $valfield = 'geoloc_localities.name';
568 protected $from = 'geoloc_localities';
570 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
571 protected $ac_unique = 'profile_addresses.pid';
577 // {{{ class DE_Companies
578 class DE_Companies
extends DirEnumeration
580 protected $idfield = 'profile_job_enum.id';
581 protected $valfield = 'profile_job_enum.name';
582 protected $valfield2 = 'profile_job_enum.acronym';
583 protected $from = 'profile_job_enum';
585 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
586 protected $ac_unique = 'profile_job.pid';
590 // {{{ class DE_Sectors
591 class DE_Sectors
extends DirEnumeration
593 protected $idfield = 'profile_job_sector_enum.id';
594 protected $valfield = 'profile_job_sector_enum.name';
595 protected $from = 'profile_job_sector_enum';
597 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
598 protected $ac_unique = 'profile_job.pid';
602 // {{{ class DE_JobDescription
603 class DE_JobDescription
extends DirEnumeration
605 protected $valfield = 'profile_job.description';
606 protected $from = 'profile_job';
607 protected $idfield = 'profile_job.pid';
609 protected $ac_unique = 'profile_job.pid';
615 // {{{ class DE_Networking
616 class DE_Networking
extends DirEnumeration
618 protected $idfield = 'profile_networking_enum.network_type';
619 protected $valfield = 'profile_networking_enum.name';
620 protected $from = 'profile_networking_enum';
623 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.network_type = profile_networking_enum.network_type)';
624 protected $ac_unique = 'profile_networking.pid';
628 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: