be5d83bf4f2ac4cd4a7ecf015804a3ee97bafc03
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';
34 const BINETS
= 'binets';
35 const GROUPESX
= 'groupesx';
36 const SECTIONS
= 'sections';
38 const EDUSCHOOLS
= 'educationschools';
39 const EDUDEGREES
= 'educationdegrees';
40 const EDUFIELDS
= 'educationfields';
42 const NATIONALITIES
= 'nationalities';
43 const COUNTRIES
= 'countries';
44 const ADMINAREAS
= 'adminareas';
45 const LOCALITIES
= 'localities';
47 const COMPANIES
= 'companies';
48 const SECTORS
= 'sectors';
49 const JOBDESCRIPTION
= 'jobdescription';
51 const NETWORKS
= 'networking';
53 static private $enumerations = array();
55 static private function init($type)
57 $cls = "DE_" . ucfirst($type);
58 self
::$enumerations[$type] = new $cls();
61 /** Retrieves all options for a given type
62 * @param $type Type of enum for which options are requested
63 * @return Array of the results
65 static public function getOptions()
67 $args = func_get_args();
68 $type = array_shift($args);
69 if (!array_key_exists($type, self
::$enumerations)) {
72 $obj = self
::$enumerations[$type];
73 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
74 return call_user_func_array(array($obj, 'getOptions'), $args);
80 /** Retrieves all options for a given type
81 * @param $type Type of enum for which options are requested
82 * @return PlIterator over the results
84 static public function getOptionsIter()
86 $args = func_get_args();
87 $type = array_shift($args);
88 if (!array_key_exists($type, self
::$enumerations)) {
91 $obj = self
::$enumerations[$type];
92 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
93 return call_user_func_array(array($obj, 'getOptionsIter'), $args);
95 return PlIteratorUtils
::fromArray(array());
99 /** Retrieves all options with number of profiles for autocompletion
100 * @param $type Type of enum for which options are requested
101 * @param $text Text to autocomplete
102 * @return PlIterator over the results
104 static public function getAutoComplete()
106 $args = func_get_args();
107 $type = array_shift($args);
108 if (!array_key_exists($type, self
::$enumerations)) {
111 $obj = self
::$enumerations[$type];
112 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
113 return call_user_func_array(array($obj, 'getAutoComplete'), $args);
115 return PlIteratorUtils
::fromArray(array());
119 /** Retrieves a list of IDs for a given type
120 * @param $type Type of enum for which IDs are requested
121 * @param $text Text to search in enum valuees
122 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
124 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
126 if (!array_key_exists($type, self
::$enumerations)) {
129 $obj = self
::$enumerations[$type];
130 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
131 return call_user_func(array($obj, 'getIDs'), $text, $mode);
137 /** Retrieves a single ID for a given type.
138 * @param $type Type of the enum for which an ID is requested
139 * @param $text Text to search in enum values
140 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
142 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
144 return array_shift(self
::getIDs($type, $text, $mode));
149 // {{{ class DirEnumeration
150 abstract class DirEnumeration
152 const AUTOCOMPLETE_LIMIT
= 11;
154 const HAS_OPTIONS
= 0x001;
155 const HAS_AUTOCOMP
= 0x002;
157 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
159 /** An internal array of ID => optionTxt
161 protected $options = null
;
163 /** Description of the MySQL storage of the fields
165 protected $idfield = 'id';
166 protected $valfield = 'text';
167 protected $valfield2 = null
;
169 protected $join = '';
170 protected $where = '';
172 /** Fields for autocompletion
174 protected $ac_join = ''; // Additional joins
175 protected $ac_where = null
; // Additional where
176 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
177 protected $ac_unique; // Which field is to be taken as unique
178 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
179 protected $ac_withid = true
; // Do we want to fetch id too ?
181 protected function _fetchOptions()
183 if (is_null($this->options
)) {
184 $this->loadOptions();
188 public function getOptions()
190 $this->_fetchOptions();
191 return $this->options
;
194 public function getOptionsIter()
196 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions()), 1, true
);
199 // {{{ function getIDs
200 /** Retrieves possible IDs for given text
201 * @param $text Text to search for IDs
202 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
203 * @return An array of matching IDs ; if empty, input should be considered invalid
205 public function getIDs($text, $mode)
207 if ($mode == XDB
::WILDCARD_EXACT
) {
208 $options = $this->getOptions();
209 return array_keys($options, $text);
211 if ($this->where
== null
) {
214 $where = $this->where
. ' AND ';
217 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
218 if ($this->valfield2
!= null
) {
219 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
221 $where .= '(' . implode(' OR ', $conds) . ')';
223 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
224 FROM ' . $this->from
. '
227 GROUP BY ' . $this->idfield
);
232 private function mkTests($field, $text)
235 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
236 if (!$this->ac_beginwith
) {
237 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
238 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
243 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
246 foreach ($tab as $key => $val) {
255 // {{{ function getAutoComplete
256 public function getAutoComplete($text)
258 $text = str_replace(array('%', '_'), '', $text);
260 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
263 $where = $this->ac_where
. ' AND ';
266 $tests = $this->mkTests($this->valfield
, $text);
267 if (!is_null($this->valfield2
)) {
268 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
271 $where .= '(' . implode(' OR ', $tests) . ')';
273 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
274 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
275 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
276 FROM ' . $this->from
. '
277 ' . $this->ac_join
. '
279 GROUP BY ' . $this->valfield
. '
280 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
281 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
285 // {{{ function loadOptions
286 /** The function used to load options
288 protected function loadOptions()
290 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
291 ' . $this->idfield
. ' AS id
292 FROM ' . $this->from
. '
295 GROUP BY ' . $this->valfield
. '
296 ORDER BY ' . $this->valfield
);
302 // {{{ class DE_WithSuboption
303 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
305 abstract class DE_WithSuboption
extends DirEnumeration
309 protected $suboptions = null
;
311 protected function loadOptions()
313 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
314 ' . $this->optfield
. ' AS subid,
315 ' . $this->idfield
. ' AS id
316 FROM ' . $this->from
. '
319 GROUP BY ' . $this->valfield
. '
320 ORDER BY ' . $this->valfield
);
321 $this->options
= array();
322 $this->suboptions
= array();
323 foreach ($opts as $id => $opt) {
324 $this->options
[$id] = $opt['field'];
325 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
326 $this->suboptions
[$opt['subid']] = array();
328 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
332 public function getOptions($subid = null
)
334 $this->_fetchOptions();
335 if ($subid == null
) {
336 return $this->options
;
337 } else if (array_key_exists($subid, $this->suboptions
)) {
338 return $this->suboptions
[$subid];
344 public function getOptionsIter($subid = null
)
346 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
349 public function getIDs($text, $mode, $subid = null
)
351 if ($mode == XDB
::WILDCARD_EXACT
) {
352 $options = $this->getOptions($subid);
353 return array_keys($options, $text);
355 if ($this->where
== null
) {
358 $where = $this->where
. ' AND ';
360 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
361 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
365 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
366 if ($this->valfield2
!= null
) {
367 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
369 $where .= '(' . implode(' OR ', $conds) . ')';
371 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
372 FROM ' . $this->from
. '
375 GROUP BY ' . $this->idfield
);
379 public function getAutoComplete($text, $subid = null
)
381 $text = str_replace(array('%', '_'), '', $text);
383 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
386 $where = $this->ac_where
. ' AND ';
389 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
390 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
393 $tests = $this->mkTests($this->valfield
, $text);
394 if (!is_null($this->valfield2
)) {
395 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
398 $where .= '(' . implode(' OR ', $tests) . ')';
400 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
401 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
402 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
403 FROM ' . $this->from
. '
404 ' . $this->ac_join
. '
406 GROUP BY ' . $this->valfield
. '
407 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
408 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
413 // {{{ class DE_NameTypes
414 // returns 'system' names ('lastname', 'lastname_marital', ...)
415 class DE_NameTypes
extends DirEnumeration
417 public $capabilities = self
::HAS_OPTIONS
;
419 protected $from = 'profile_name_enum';
420 protected $valfield = 'type';
426 // {{{ class DE_Binets
427 class DE_Binets
extends DirEnumeration
429 protected $from = 'profile_binet_enum';
431 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
432 protected $ac_unique = 'profile_binets.pid';
436 // {{{ class DE_Sections
437 class DE_Sections
extends DirEnumeration
439 protected $from = 'profile_section_enum';
441 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
442 protected $ac_unique = 'profiles.pid';
446 // {{{ class DE_GroupesX
447 class DE_GroupesX
extends DirEnumeration
449 protected $idfield = 'groups.id';
450 protected $valfield = 'groups.nom';
451 protected $valfield2 = 'groups.diminutif';
452 protected $from = 'groups';
453 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
455 protected $ac_join = "INNER JOIN group_members ON (groups.id = memb.asso_id
456 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
457 AND groups.pub = 'public')";
458 protected $ac_unique = 'group_members.uid';
464 // {{{ class DE_EducationSchools
465 class DE_EducationSchools
extends DirEnumeration
467 protected $valfield = 'profile_education_enum.name';
468 protected $valfield2 = 'profile_education_enum.abbreviation';
469 protected $from = 'profile_education_enum';
471 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
472 protected $ac_unique = 'profile_education.pid';
476 // {{{ class DE_EducationDegrees
477 class DE_EducationDegrees
extends DirEnumeration
479 public $capabilities = self
::HAS_OPTIONS
;
481 protected $idfield = 'profile_education_degree.degreeid';
482 protected $optfield = 'profile_education_degree.eduid';
483 protected $valfield = 'profile_education_degree_enum.degree';
484 protected $from = 'profile_education_degree_enum';
485 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
490 // {{{ class DE_EducationFields
491 class DE_EducationFields
extends DirEnumeration
493 protected $valfield = 'profile_education_field_enum.field';
494 protected $from = 'profile_education_field_enum';
496 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
497 protected $ac_unique = 'profile_education.pid';
503 // {{{ class DE_Nationalities
504 class DE_Nationalities
extends DirEnumeration
506 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
507 protected $valfield = 'geoloc_countries.nationalityFR';
508 protected $valfield2 = 'geoloc_countries.nationality';
509 protected $from = 'geoloc_countries';
510 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
512 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
513 protected $ac_unique = 'profiles.pid';
517 // {{{ class DE_Countries
518 class DE_Countries
extends DirEnumeration
520 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
521 protected $valfield = 'geoloc_countries.countryFR';
522 protected $valfield2 = 'geoloc_countries.country';
523 protected $from = 'geoloc_countries';
525 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryFR';
526 protected $ac_unique = 'profile_addresses.pid';
530 // {{{ class DE_AdminAreas
531 class DE_AdminAreas
extends DE_WithSuboption
533 protected $idfield = 'geoloc_administrativeareas.id';
534 protected $optfield = 'geoloc_administrativeareas.country';
535 protected $valfield = 'geoloc_administrativeareas.name';
536 protected $from = 'geoloc_administrativeareas';
538 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
539 protected $ac_unique = 'profile_addresses.pid';
543 // {{{ class DE_Localities
544 class DE_Localities
extends DirEnumeration
546 protected $valfield = 'geoloc_localities.name';
547 protected $from = 'geoloc_localities';
549 protected $ac_join = 'profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
550 protected $ac_unique = 'profile_addresses.pid';
556 // {{{ class DE_Companies
557 class DE_Companies
extends DirEnumeration
559 protected $valfield = 'profile_job_enum.name';
560 protected $valfield2 = 'profile_job_enum.acronym';
561 protected $from = 'profile_job_enum';
563 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
564 protected $ac_unique = 'profile_job.pid';
568 // {{{ class DE_Sectors
569 class DE_Sectors
extends DirEnumeration
571 protected $valfield = 'profile_job_sector_enum.name';
572 protected $from = 'profile_job_sector_enum';
574 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
575 protected $ac_unique = 'profile_job.pid';
579 // {{{ class DE_JobDescription
580 class DE_JobDescription
582 protected $valfield = 'profile_job.description';
583 protected $from = 'profile_job';
584 protected $idfield = 'profile_job.pid';
586 protected $ac_unique = 'profile_job.pid';
592 // {{{ class DE_Networking
593 class DE_Networking
extends DirEnumeration
595 protected $idfield = 'profile_networking_enum.network_type';
596 protected $valfield = 'profile_networking_enum.name';
597 protected $from = 'profile_networking_enum';
600 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.network_type = profile_networking_enum.network_type';
601 protected $ac_unique = 'profile_networking.pid';
605 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: