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 private function mkTests($field, $text)
244 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
245 if (!$this->ac_beginwith
) {
246 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
247 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
252 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
255 foreach ($tab as $key => $val) {
264 // {{{ function getAutoComplete
265 public function getAutoComplete($text)
267 $text = str_replace(array('%', '_'), '', $text);
269 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
272 $where = $this->ac_where
. ' AND ';
275 $tests = $this->mkTests($this->valfield
, $text);
276 if (!is_null($this->valfield2
)) {
277 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
280 $where .= '(' . implode(' OR ', $tests) . ')';
282 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
283 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
284 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
285 FROM ' . $this->from
. '
286 ' . $this->ac_join
. '
288 GROUP BY ' . $this->valfield
. '
289 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
290 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
294 // {{{ function loadOptions
295 /** The function used to load options
297 protected function loadOptions()
299 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
300 ' . $this->idfield
. ' AS id
301 FROM ' . $this->from
. '
304 GROUP BY ' . $this->valfield
. '
305 ORDER BY ' . $this->valfield
);
311 // {{{ class DE_WithSuboption
312 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
314 abstract class DE_WithSuboption
extends DirEnumeration
318 protected $suboptions = null
;
320 protected function loadOptions()
322 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
323 ' . $this->optfield
. ' AS subid,
324 ' . $this->idfield
. ' AS id
325 FROM ' . $this->from
. '
328 GROUP BY ' . $this->valfield
. '
329 ORDER BY ' . $this->valfield
);
330 $this->options
= array();
331 $this->suboptions
= array();
332 foreach ($opts as $id => $opt) {
333 $this->options
[$id] = $opt['field'];
334 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
335 $this->suboptions
[$opt['subid']] = array();
337 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
341 public function getOptions($subid = null
)
343 $this->_fetchOptions();
344 if ($subid == null
) {
345 return $this->options
;
346 } else if (array_key_exists($subid, $this->suboptions
)) {
347 return $this->suboptions
[$subid];
353 public function getOptionsIter($subid = null
)
355 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
358 public function getIDs($text, $mode, $subid = null
)
360 if ($mode == XDB
::WILDCARD_EXACT
) {
361 $options = $this->getOptions($subid);
362 return array_keys($options, $text);
364 if ($this->where
== null
) {
367 $where = $this->where
. ' AND ';
369 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
370 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
374 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
375 if ($this->valfield2
!= null
) {
376 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
378 $where .= '(' . implode(' OR ', $conds) . ')';
380 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
381 FROM ' . $this->from
. '
384 GROUP BY ' . $this->idfield
);
388 public function getAutoComplete($text, $subid = null
)
390 $text = str_replace(array('%', '_'), '', $text);
392 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
395 $where = $this->ac_where
. ' AND ';
398 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
399 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
402 $tests = $this->mkTests($this->valfield
, $text);
403 if (!is_null($this->valfield2
)) {
404 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
407 $where .= '(' . implode(' OR ', $tests) . ')';
409 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
410 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
411 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
412 FROM ' . $this->from
. '
413 ' . $this->ac_join
. '
415 GROUP BY ' . $this->valfield
. '
416 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
417 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
422 // {{{ class DE_NameTypes
423 // returns 'system' names ('lastname', 'lastname_marital', ...)
424 class DE_NameTypes
extends DirEnumeration
426 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
428 protected $from = 'profile_name_enum';
429 protected $valfield = 'type';
433 // {{{ class DE_Names
434 // returns 'system' names ('lastname', 'lastname_marital', ...)
435 class DE_Names
extends DirEnumeration
437 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
439 protected $from = 'profile_name_enum';
440 protected $idfield = 'type';
441 protected $valfield = 'name';
447 // {{{ class DE_Binets
448 class DE_Binets
extends DirEnumeration
450 protected $from = 'profile_binet_enum';
452 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
453 protected $ac_unique = 'profile_binets.pid';
457 // {{{ class DE_Sections
458 class DE_Sections
extends DirEnumeration
460 protected $from = 'profile_section_enum';
462 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
463 protected $ac_unique = 'profiles.pid';
467 // {{{ class DE_GroupesX
468 class DE_GroupesX
extends DirEnumeration
470 protected $idfield = 'groups.id';
471 protected $valfield = 'groups.nom';
472 protected $valfield2 = 'groups.diminutif';
473 protected $from = 'groups';
474 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
476 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
477 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
478 AND groups.pub = 'public')";
479 protected $ac_unique = 'group_members.uid';
485 // {{{ class DE_EducationSchools
486 class DE_EducationSchools
extends DirEnumeration
488 protected $idfield = 'profile_education_enum.id';
489 protected $valfield = 'profile_education_enum.name';
490 protected $valfield2 = 'profile_education_enum.abbreviation';
491 protected $from = 'profile_education_enum';
493 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
494 protected $ac_unique = 'profile_education.pid';
498 // {{{ class DE_EducationDegrees
499 class DE_EducationDegrees
extends DirEnumeration
501 public $capabilities = self
::HAS_OPTIONS
;
503 protected $idfield = 'profile_education_degree.degreeid';
504 protected $optfield = 'profile_education_degree.eduid';
505 protected $valfield = 'profile_education_degree_enum.degree';
506 protected $from = 'profile_education_degree_enum';
507 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
512 // {{{ class DE_EducationFields
513 class DE_EducationFields
extends DirEnumeration
515 protected $valfield = 'profile_education_field_enum.field';
516 protected $from = 'profile_education_field_enum';
518 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
519 protected $ac_unique = 'profile_education.pid';
525 // {{{ class DE_Nationalities
526 class DE_Nationalities
extends DirEnumeration
528 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
529 protected $valfield = 'geoloc_countries.nationalityFR';
530 protected $valfield2 = 'geoloc_countries.nationality';
531 protected $from = 'geoloc_countries';
532 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
534 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
535 protected $ac_unique = 'profiles.pid';
539 // {{{ class DE_Countries
540 class DE_Countries
extends DirEnumeration
542 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
543 protected $valfield = 'geoloc_countries.countryFR';
544 protected $valfield2 = 'geoloc_countries.country';
545 protected $from = 'geoloc_countries';
547 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
548 protected $ac_unique = 'profile_addresses.pid';
552 // {{{ class DE_AdminAreas
553 class DE_AdminAreas
extends DE_WithSuboption
555 protected $idfield = 'geoloc_administrativeareas.id';
556 protected $optfield = 'geoloc_administrativeareas.country';
557 protected $valfield = 'geoloc_administrativeareas.name';
558 protected $from = 'geoloc_administrativeareas';
560 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
561 protected $ac_unique = 'profile_addresses.pid';
565 // {{{ class DE_Localities
566 class DE_Localities
extends DirEnumeration
568 protected $idfield = 'geoloc_localities.id';
569 protected $valfield = 'geoloc_localities.name';
570 protected $from = 'geoloc_localities';
572 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
573 protected $ac_unique = 'profile_addresses.pid';
579 // {{{ class DE_Companies
580 class DE_Companies
extends DirEnumeration
582 protected $idfield = 'profile_job_enum.id';
583 protected $valfield = 'profile_job_enum.name';
584 protected $valfield2 = 'profile_job_enum.acronym';
585 protected $from = 'profile_job_enum';
587 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
588 protected $ac_unique = 'profile_job.pid';
592 // {{{ class DE_Sectors
593 class DE_Sectors
extends DirEnumeration
595 protected $idfield = 'profile_job_sector_enum.id';
596 protected $valfield = 'profile_job_sector_enum.name';
597 protected $from = 'profile_job_sector_enum';
599 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
600 protected $ac_unique = 'profile_job.pid';
604 // {{{ class DE_JobDescription
605 class DE_JobDescription
extends DirEnumeration
607 protected $valfield = 'profile_job.description';
608 protected $from = 'profile_job';
609 protected $idfield = 'profile_job.pid';
611 protected $ac_unique = 'profile_job.pid';
617 // {{{ class DE_Networking
618 class DE_Networking
extends DirEnumeration
620 protected $idfield = 'profile_networking_enum.nwid';
621 protected $valfield = 'profile_networking_enum.name';
622 protected $from = 'profile_networking_enum';
625 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
626 protected $ac_unique = 'profile_networking.pid';
630 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: