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 const MEDALS
= 'medals';
56 static private $enumerations = array();
58 static private function init($type)
60 if (S
::has('__DE_' . $type)) {
61 self
::$enumerations[$type] = S
::v('__DE_' . $type);
63 $cls = "DE_" . ucfirst($type);
65 self
::$enumerations[$type] = $obj;
66 if ($obj->capabilities
& DirEnumeration
::SAVE_IN_SESSION
) {
67 S
::set('__DE_' . $type, $obj);
72 /** Retrieves all options for a given type
73 * @param $type Type of enum for which options are requested
74 * @return Array of the results
76 static public function getOptions($type)
78 if (!array_key_exists($type, self
::$enumerations)) {
81 $obj = self
::$enumerations[$type];
82 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
83 return call_user_func(array($obj, 'getOptions'));
89 /** Retrieves all options for a given type
90 * @param $type Type of enum for which options are requested
91 * @return PlIterator over the results
93 static public function getOptionsIter($type)
95 if (!array_key_exists($type, self
::$enumerations)) {
98 $obj = self
::$enumerations[$type];
99 $args = func_get_args();
101 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
102 return call_user_func_array(array($obj, 'getOptionsIter'), $args);
104 return PlIteratorUtils
::fromArray(array());
108 /** Retrieves all options with number of profiles for autocompletion
109 * @param $type Type of enum for which options are requested
110 * @param $text Text to autocomplete
111 * @return PlIterator over the results
113 static public function getAutoComplete($type, $text)
115 if (!array_key_exists($type, self
::$enumerations)) {
118 $obj = self
::$enumerations[$type];
119 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
120 return call_user_func(array($obj, 'getAutoComplete'), $text);
122 return PlIteratorUtils
::fromArray(array());
126 /** Retrieves a list of IDs for a given type
127 * @param $type Type of enum for which IDs are requested
128 * @param $text Text to search in enum valuees
129 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
131 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
133 if (!array_key_exists($type, self
::$enumerations)) {
136 $obj = self
::$enumerations[$type];
137 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
138 return call_user_func(array($obj, 'getIDs'), $text, $mode);
144 /** Retrieves a single ID for a given type.
145 * @param $type Type of the enum for which an ID is requested
146 * @param $text Text to search in enum values
147 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
149 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
151 $ids = self
::getIDs($type, $text, $mode);
152 return array_shift($ids);
157 // {{{ class DirEnumeration
158 abstract class DirEnumeration
160 const AUTOCOMPLETE_LIMIT
= 11;
162 const HAS_OPTIONS
= 0x001;
163 const HAS_AUTOCOMP
= 0x002;
164 const SAVE_IN_SESSION
= 0x004;
166 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
168 /** An internal array of ID => optionTxt
170 protected $options = null
;
172 /** Description of the MySQL storage of the fields
174 protected $idfield = 'id';
175 protected $valfield = 'text';
176 protected $valfield2 = null
;
178 protected $join = '';
179 protected $where = '';
181 /** Fields for autocompletion
183 protected $ac_join = ''; // Additional joins
184 protected $ac_where = null
; // Additional where
185 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
186 protected $ac_unique; // Which field is to be taken as unique
187 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
188 protected $ac_withid = true
; // Do we want to fetch id too ?
190 protected function _fetchOptions()
192 if (is_null($this->options
)) {
193 $this->loadOptions();
197 public function getOptions()
199 $this->_fetchOptions();
200 return $this->options
;
203 public function getOptionsIter()
205 $options = $this->getOptions();
206 $options = self
::expandArray($options);
207 return PlIteratorUtils
::fromArray($options, 1, true
);
210 // {{{ function getIDs
211 /** Retrieves possible IDs for given text
212 * @param $text Text to search for IDs
213 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
214 * @return An array of matching IDs ; if empty, input should be considered invalid
216 public function getIDs($text, $mode)
218 if ($mode == XDB
::WILDCARD_EXACT
) {
219 $options = $this->getOptions();
220 return array_keys($options, $text);
222 if ($this->where
== null
) {
225 $where = $this->where
. ' AND ';
228 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
229 if ($this->valfield2
!= null
) {
230 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
232 $where .= '(' . implode(' OR ', $conds) . ')';
234 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
235 FROM ' . $this->from
. '
238 GROUP BY ' . $this->idfield
);
243 /** Builds a list of query parts for searching @$text in @$field :
244 * field LIKE 'text%', field LIKE '% text%', field LIKE '%-text%'
246 private function mkTests($field, $text)
249 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
250 if (!$this->ac_beginwith
) {
251 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
252 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
257 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
260 foreach ($tab as $key => $val) {
269 // {{{ function getAutoComplete
270 public function getAutoComplete($text)
272 $text = str_replace(array('%', '_'), '', $text);
274 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
277 $where = $this->ac_where
. ' AND ';
280 $tests = $this->mkTests($this->valfield
, $text);
281 if (!is_null($this->valfield2
)) {
282 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
285 $where .= '(' . implode(' OR ', $tests) . ')';
287 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
288 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
289 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
290 FROM ' . $this->from
. '
291 ' . $this->ac_join
. '
293 GROUP BY ' . $this->valfield
. '
294 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
295 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
299 // {{{ function loadOptions
300 /** The function used to load options
302 protected function loadOptions()
304 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
305 ' . $this->idfield
. ' AS id
306 FROM ' . $this->from
. '
309 GROUP BY ' . $this->valfield
. '
310 ORDER BY ' . $this->valfield
);
316 // {{{ class DE_WithSuboption
317 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
319 abstract class DE_WithSuboption
extends DirEnumeration
323 protected $suboptions = null
;
325 protected function loadOptions()
327 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
328 ' . $this->optfield
. ' AS subid,
329 ' . $this->idfield
. ' AS id
330 FROM ' . $this->from
. '
333 GROUP BY ' . $this->valfield
. '
334 ORDER BY ' . $this->valfield
);
335 $this->options
= array();
336 $this->suboptions
= array();
337 foreach ($opts as $id => $opt) {
338 $this->options
[$id] = $opt['field'];
339 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
340 $this->suboptions
[$opt['subid']] = array();
342 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
346 public function getOptions($subid = null
)
348 $this->_fetchOptions();
349 if ($subid == null
) {
350 return $this->options
;
351 } else if (array_key_exists($subid, $this->suboptions
)) {
352 return $this->suboptions
[$subid];
358 public function getOptionsIter($subid = null
)
360 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
363 public function getIDs($text, $mode, $subid = null
)
365 if ($mode == XDB
::WILDCARD_EXACT
) {
366 $options = $this->getOptions($subid);
367 return array_keys($options, $text);
369 if ($this->where
== null
) {
372 $where = $this->where
. ' AND ';
374 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
375 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
379 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
380 if ($this->valfield2
!= null
) {
381 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
383 $where .= '(' . implode(' OR ', $conds) . ')';
385 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
386 FROM ' . $this->from
. '
389 GROUP BY ' . $this->idfield
);
393 public function getAutoComplete($text, $subid = null
)
395 $text = str_replace(array('%', '_'), '', $text);
397 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
400 $where = $this->ac_where
. ' AND ';
403 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
404 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
407 $tests = $this->mkTests($this->valfield
, $text);
408 if (!is_null($this->valfield2
)) {
409 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
412 $where .= '(' . implode(' OR ', $tests) . ')';
414 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
415 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
416 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
417 FROM ' . $this->from
. '
418 ' . $this->ac_join
. '
420 GROUP BY ' . $this->valfield
. '
421 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
422 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
427 // {{{ class DE_NameTypes
428 // returns 'system' names ('lastname', 'lastname_marital', ...)
429 class DE_NameTypes
extends DirEnumeration
431 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
433 protected $from = 'profile_name_enum';
434 protected $valfield = 'type';
438 // {{{ class DE_Names
439 // returns 'system' names ('lastname', 'lastname_marital', ...)
440 class DE_Names
extends DirEnumeration
442 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
444 protected $from = 'profile_name_enum';
445 protected $idfield = 'type';
446 protected $valfield = 'name';
452 // {{{ class DE_Binets
453 class DE_Binets
extends DirEnumeration
455 protected $from = 'profile_binet_enum';
457 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
458 protected $ac_unique = 'profile_binets.pid';
462 // {{{ class DE_Sections
463 class DE_Sections
extends DirEnumeration
465 protected $from = 'profile_section_enum';
467 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
468 protected $ac_unique = 'profiles.pid';
472 // {{{ class DE_GroupesX
473 class DE_GroupesX
extends DirEnumeration
475 protected $idfield = 'groups.id';
476 protected $valfield = 'groups.nom';
477 protected $valfield2 = 'groups.diminutif';
478 protected $from = 'groups';
479 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
481 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
482 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
483 AND groups.pub = 'public')";
484 protected $ac_unique = 'group_members.uid';
490 // {{{ class DE_EducationSchools
491 class DE_EducationSchools
extends DirEnumeration
493 protected $idfield = 'profile_education_enum.id';
494 protected $valfield = 'profile_education_enum.name';
495 protected $valfield2 = 'profile_education_enum.abbreviation';
496 protected $from = 'profile_education_enum';
498 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
499 protected $ac_unique = 'profile_education.pid';
503 // {{{ class DE_EducationDegrees
504 class DE_EducationDegrees
extends DirEnumeration
506 public $capabilities = self
::HAS_OPTIONS
;
508 protected $idfield = 'profile_education_degree.degreeid';
509 protected $optfield = 'profile_education_degree.eduid';
510 protected $valfield = 'profile_education_degree_enum.degree';
511 protected $from = 'profile_education_degree_enum';
512 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
517 // {{{ class DE_EducationFields
518 class DE_EducationFields
extends DirEnumeration
520 protected $valfield = 'profile_education_field_enum.field';
521 protected $from = 'profile_education_field_enum';
523 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
524 protected $ac_unique = 'profile_education.pid';
530 // {{{ class DE_Nationalities
531 class DE_Nationalities
extends DirEnumeration
533 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
534 protected $valfield = 'geoloc_countries.nationalityFR';
535 protected $valfield2 = 'geoloc_countries.nationality';
536 protected $from = 'geoloc_countries';
537 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
539 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
540 protected $ac_unique = 'profiles.pid';
544 // {{{ class DE_Countries
545 class DE_Countries
extends DirEnumeration
547 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
548 protected $valfield = 'geoloc_countries.countryFR';
549 protected $valfield2 = 'geoloc_countries.country';
550 protected $from = 'geoloc_countries';
552 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
553 protected $ac_unique = 'profile_addresses.pid';
557 // {{{ class DE_AdminAreas
558 class DE_AdminAreas
extends DE_WithSuboption
560 protected $idfield = 'geoloc_administrativeareas.id';
561 protected $optfield = 'geoloc_administrativeareas.country';
562 protected $valfield = 'geoloc_administrativeareas.name';
563 protected $from = 'geoloc_administrativeareas';
565 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
566 protected $ac_unique = 'profile_addresses.pid';
570 // {{{ class DE_Localities
571 class DE_Localities
extends DirEnumeration
573 protected $idfield = 'geoloc_localities.id';
574 protected $valfield = 'geoloc_localities.name';
575 protected $from = 'geoloc_localities';
577 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
578 protected $ac_unique = 'profile_addresses.pid';
584 // {{{ class DE_Companies
585 class DE_Companies
extends DirEnumeration
587 protected $idfield = 'profile_job_enum.id';
588 protected $valfield = 'profile_job_enum.name';
589 protected $valfield2 = 'profile_job_enum.acronym';
590 protected $from = 'profile_job_enum';
592 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
593 protected $ac_unique = 'profile_job.pid';
597 // {{{ class DE_Sectors
598 class DE_Sectors
extends DirEnumeration
600 protected $idfield = 'profile_job_sector_enum.id';
601 protected $valfield = 'profile_job_sector_enum.name';
602 protected $from = 'profile_job_sector_enum';
604 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
605 protected $ac_unique = 'profile_job.pid';
609 // {{{ class DE_JobDescription
610 class DE_JobDescription
extends DirEnumeration
612 protected $valfield = 'profile_job.description';
613 protected $from = 'profile_job';
614 protected $idfield = 'profile_job.pid';
616 protected $ac_unique = 'profile_job.pid';
622 // {{{ class DE_Networking
623 class DE_Networking
extends DirEnumeration
625 protected $idfield = 'profile_networking_enum.nwid';
626 protected $valfield = 'profile_networking_enum.name';
627 protected $from = 'profile_networking_enum';
630 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
631 protected $ac_unique = 'profile_networking.pid';
637 // {{{ class DE_Medals
638 class DE_Medals
extends DirEnumeration
640 protected $from = 'profile_medal_enum';
642 protected $ac_join = 'INNER JOIN profile_medals ON (profile_medals.mid = profile_medal_enum.id)';
643 protected $ac_unique = 'profile_medals.pid';
647 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: