2cd0f410bdeb649689e2b1e20b552ab52f30e3b1
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 if (S
::has('__DE_' . $type)) {
58 self
::$enumerations[$type] = S
::v('__DE_' . $type);
60 $cls = "DE_" . ucfirst($type);
62 self
::$enumerations[$type] = $obj;
63 if ($obj->capabilities
& DirEnumeration
::SAVE_IN_SESSION
) {
64 S
::set('__DE_' . $type, $obj);
69 /** Retrieves all options for a given type
70 * @param $type Type of enum for which options are requested
71 * @return Array of the results
73 static public function getOptions($type)
75 if (!array_key_exists($type, self
::$enumerations)) {
78 $obj = self
::$enumerations[$type];
79 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
80 return call_user_func(array($obj, 'getOptions'));
86 /** Retrieves all options for a given type
87 * @param $type Type of enum for which options are requested
88 * @return PlIterator over the results
90 static public function getOptionsIter($type)
92 if (!array_key_exists($type, self
::$enumerations)) {
95 $obj = self
::$enumerations[$type];
96 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
97 return call_user_func(array($obj, 'getOptionsIter'));
99 return PlIteratorUtils
::fromArray(array());
103 /** Retrieves all options with number of profiles for autocompletion
104 * @param $type Type of enum for which options are requested
105 * @param $text Text to autocomplete
106 * @return PlIterator over the results
108 static public function getAutoComplete($type, $text)
110 if (!array_key_exists($type, self
::$enumerations)) {
113 $obj = self
::$enumerations[$type];
114 if ($obj->capabilities
& DirEnumeration
::HAS_AUTOCOMP
) {
115 return call_user_func(array($obj, 'getAutoComplete'), $text);
117 return PlIteratorUtils
::fromArray(array());
121 /** Retrieves a list of IDs for a given type
122 * @param $type Type of enum for which IDs are requested
123 * @param $text Text to search in enum valuees
124 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
126 static public function getIDs($type, $text, $mode = XDB
::WILDCARD_EXACT
)
128 if (!array_key_exists($type, self
::$enumerations)) {
131 $obj = self
::$enumerations[$type];
132 if ($obj->capabilities
& DirEnumeration
::HAS_OPTIONS
) {
133 return call_user_func(array($obj, 'getIDs'), $text, $mode);
139 /** Retrieves a single ID for a given type.
140 * @param $type Type of the enum for which an ID is requested
141 * @param $text Text to search in enum values
142 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
144 static public function getID($type, $text, $mode = XDB
::WILDCARD_EXACT
)
146 $ids = self
::getIDs($type, $text, $mode);
147 return array_shift($ids);
152 // {{{ class DirEnumeration
153 abstract class DirEnumeration
155 const AUTOCOMPLETE_LIMIT
= 11;
157 const HAS_OPTIONS
= 0x001;
158 const HAS_AUTOCOMP
= 0x002;
159 const SAVE_IN_SESSION
= 0x004;
161 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
163 /** An internal array of ID => optionTxt
165 protected $options = null
;
167 /** Description of the MySQL storage of the fields
169 protected $idfield = 'id';
170 protected $valfield = 'text';
171 protected $valfield2 = null
;
173 protected $join = '';
174 protected $where = '';
176 /** Fields for autocompletion
178 protected $ac_join = ''; // Additional joins
179 protected $ac_where = null
; // Additional where
180 protected $ac_beginwith = true
; // Whether to search for 'x%' or for '%x%'
181 protected $ac_unique; // Which field is to be taken as unique
182 protected $ac_distinct = true
; // Whether we want to keep only distinct valfield value
183 protected $ac_withid = true
; // Do we want to fetch id too ?
185 protected function _fetchOptions()
187 if (is_null($this->options
)) {
188 $this->loadOptions();
192 public function getOptions()
194 $this->_fetchOptions();
195 return $this->options
;
198 public function getOptionsIter()
200 $options = $this->getOptions();
201 $options = self
::expandArray($options);
202 return PlIteratorUtils
::fromArray($options, 1, true
);
205 // {{{ function getIDs
206 /** Retrieves possible IDs for given text
207 * @param $text Text to search for IDs
208 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
209 * @return An array of matching IDs ; if empty, input should be considered invalid
211 public function getIDs($text, $mode)
213 if ($mode == XDB
::WILDCARD_EXACT
) {
214 $options = $this->getOptions();
215 return array_keys($options, $text);
217 if ($this->where
== null
) {
220 $where = $this->where
. ' AND ';
223 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
224 if ($this->valfield2
!= null
) {
225 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
227 $where .= '(' . implode(' OR ', $conds) . ')';
229 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
230 FROM ' . $this->from
. '
233 GROUP BY ' . $this->idfield
);
238 private function mkTests($field, $text)
241 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_PREFIX
, $text);
242 if (!$this->ac_beginwith
) {
243 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, ' ' . $text);
244 $tests[] = $field . XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, '-' . $text);
249 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
252 foreach ($tab as $key => $val) {
261 // {{{ function getAutoComplete
262 public function getAutoComplete($text)
264 $text = str_replace(array('%', '_'), '', $text);
266 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
269 $where = $this->ac_where
. ' AND ';
272 $tests = $this->mkTests($this->valfield
, $text);
273 if (!is_null($this->valfield2
)) {
274 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
277 $where .= '(' . implode(' OR ', $tests) . ')';
279 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
280 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
281 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
282 FROM ' . $this->from
. '
283 ' . $this->ac_join
. '
285 GROUP BY ' . $this->valfield
. '
286 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
287 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
291 // {{{ function loadOptions
292 /** The function used to load options
294 protected function loadOptions()
296 $this->options
= XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
297 ' . $this->idfield
. ' AS id
298 FROM ' . $this->from
. '
301 GROUP BY ' . $this->valfield
. '
302 ORDER BY ' . $this->valfield
);
308 // {{{ class DE_WithSuboption
309 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
311 abstract class DE_WithSuboption
extends DirEnumeration
315 protected $suboptions = null
;
317 protected function loadOptions()
319 $opts = XDB
::fetchAllAssoc('id', 'SELECT ' . $this->valfield
. ' AS field,
320 ' . $this->optfield
. ' AS subid,
321 ' . $this->idfield
. ' AS id
322 FROM ' . $this->from
. '
325 GROUP BY ' . $this->valfield
. '
326 ORDER BY ' . $this->valfield
);
327 $this->options
= array();
328 $this->suboptions
= array();
329 foreach ($opts as $id => $opt) {
330 $this->options
[$id] = $opt['field'];
331 if (!array_key_exists($opt['subid'], $this->suboptions
)) {
332 $this->suboptions
[$opt['subid']] = array();
334 $this->suboptions
[$opt['subid']][$id] = $opt['field'];
338 public function getOptions($subid = null
)
340 $this->_fetchOptions();
341 if ($subid == null
) {
342 return $this->options
;
343 } else if (array_key_exists($subid, $this->suboptions
)) {
344 return $this->suboptions
[$subid];
350 public function getOptionsIter($subid = null
)
352 return PlIteratorUtils
::fromArray(self
::expandArray($this->getOptions($subid)), 1, true
);
355 public function getIDs($text, $mode, $subid = null
)
357 if ($mode == XDB
::WILDCARD_EXACT
) {
358 $options = $this->getOptions($subid);
359 return array_keys($options, $text);
361 if ($this->where
== null
) {
364 $where = $this->where
. ' AND ';
366 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
367 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
371 $conds[] = $this->valfield
. XDB
::formatWildcards($mode, $text);
372 if ($this->valfield2
!= null
) {
373 $conds[] = $this->valfield2
. XDB
::formatWildcards($mode, $text);
375 $where .= '(' . implode(' OR ', $conds) . ')';
377 return XDB
::fetchColumn('SELECT ' . $this->idfield
. '
378 FROM ' . $this->from
. '
381 GROUP BY ' . $this->idfield
);
385 public function getAutoComplete($text, $subid = null
)
387 $text = str_replace(array('%', '_'), '', $text);
389 if (is_null($this->ac_where
) ||
$this->ac_where
== '') {
392 $where = $this->ac_where
. ' AND ';
395 if ($subid != null
&& array_key_exists($subid, $this->suboptions
)) {
396 $where .= XDB
::format($this->optfield
. ' = {?} AND ', $subid);
399 $tests = $this->mkTests($this->valfield
, $text);
400 if (!is_null($this->valfield2
)) {
401 $tests = array_merge($tests, $this->mkTests($this->valfield2
, $text));
404 $where .= '(' . implode(' OR ', $tests) . ')';
406 return XDB
::iterator('SELECT ' . $this->valfield
. ' AS field'
407 . ($this->ac_distinct ?
(', COUNT(DISTINCT ' . $this->ac_unique
. ') AS nb') : '')
408 . ($this->ac_withid ?
(', ' . $this->idfield
. ' AS id') : '') . '
409 FROM ' . $this->from
. '
410 ' . $this->ac_join
. '
412 GROUP BY ' . $this->valfield
. '
413 ORDER BY ' . ($this->ac_distinct ?
'nb DESC' : $this->valfield
) . '
414 LIMIT ' . self
::AUTOCOMPLETE_LIMIT
);
419 // {{{ class DE_NameTypes
420 // returns 'system' names ('lastname', 'lastname_marital', ...)
421 class DE_NameTypes
extends DirEnumeration
423 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
425 protected $from = 'profile_name_enum';
426 protected $valfield = 'type';
432 // {{{ class DE_Binets
433 class DE_Binets
extends DirEnumeration
435 protected $from = 'profile_binet_enum';
437 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
438 protected $ac_unique = 'profile_binets.pid';
442 // {{{ class DE_Sections
443 class DE_Sections
extends DirEnumeration
445 protected $from = 'profile_section_enum';
447 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
448 protected $ac_unique = 'profiles.pid';
452 // {{{ class DE_GroupesX
453 class DE_GroupesX
extends DirEnumeration
455 protected $idfield = 'groups.id';
456 protected $valfield = 'groups.nom';
457 protected $valfield2 = 'groups.diminutif';
458 protected $from = 'groups';
459 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
461 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
462 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
463 AND groups.pub = 'public')";
464 protected $ac_unique = 'group_members.uid';
470 // {{{ class DE_EducationSchools
471 class DE_EducationSchools
extends DirEnumeration
473 protected $idfield = 'profile_education_enum.id';
474 protected $valfield = 'profile_education_enum.name';
475 protected $valfield2 = 'profile_education_enum.abbreviation';
476 protected $from = 'profile_education_enum';
478 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
479 protected $ac_unique = 'profile_education.pid';
483 // {{{ class DE_EducationDegrees
484 class DE_EducationDegrees
extends DirEnumeration
486 public $capabilities = self
::HAS_OPTIONS
;
488 protected $idfield = 'profile_education_degree.degreeid';
489 protected $optfield = 'profile_education_degree.eduid';
490 protected $valfield = 'profile_education_degree_enum.degree';
491 protected $from = 'profile_education_degree_enum';
492 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
497 // {{{ class DE_EducationFields
498 class DE_EducationFields
extends DirEnumeration
500 protected $valfield = 'profile_education_field_enum.field';
501 protected $from = 'profile_education_field_enum';
503 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
504 protected $ac_unique = 'profile_education.pid';
510 // {{{ class DE_Nationalities
511 class DE_Nationalities
extends DirEnumeration
513 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
514 protected $valfield = 'geoloc_countries.nationalityFR';
515 protected $valfield2 = 'geoloc_countries.nationality';
516 protected $from = 'geoloc_countries';
517 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
519 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
520 protected $ac_unique = 'profiles.pid';
524 // {{{ class DE_Countries
525 class DE_Countries
extends DirEnumeration
527 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
528 protected $valfield = 'geoloc_countries.countryFR';
529 protected $valfield2 = 'geoloc_countries.country';
530 protected $from = 'geoloc_countries';
532 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
533 protected $ac_unique = 'profile_addresses.pid';
537 // {{{ class DE_AdminAreas
538 class DE_AdminAreas
extends DE_WithSuboption
540 protected $idfield = 'geoloc_administrativeareas.id';
541 protected $optfield = 'geoloc_administrativeareas.country';
542 protected $valfield = 'geoloc_administrativeareas.name';
543 protected $from = 'geoloc_administrativeareas';
545 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
546 protected $ac_unique = 'profile_addresses.pid';
550 // {{{ class DE_Localities
551 class DE_Localities
extends DirEnumeration
553 protected $idfield = 'geoloc_localities.id';
554 protected $valfield = 'geoloc_localities.name';
555 protected $from = 'geoloc_localities';
557 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
558 protected $ac_unique = 'profile_addresses.pid';
564 // {{{ class DE_Companies
565 class DE_Companies
extends DirEnumeration
567 protected $idfield = 'profile_job_enum.id';
568 protected $valfield = 'profile_job_enum.name';
569 protected $valfield2 = 'profile_job_enum.acronym';
570 protected $from = 'profile_job_enum';
572 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
573 protected $ac_unique = 'profile_job.pid';
577 // {{{ class DE_Sectors
578 class DE_Sectors
extends DirEnumeration
580 protected $idfield = 'profile_job_sector_enum.id';
581 protected $valfield = 'profile_job_sector_enum.name';
582 protected $from = 'profile_job_sector_enum';
584 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
585 protected $ac_unique = 'profile_job.pid';
589 // {{{ class DE_JobDescription
590 class DE_JobDescription
extends DirEnumeration
592 protected $valfield = 'profile_job.description';
593 protected $from = 'profile_job';
594 protected $idfield = 'profile_job.pid';
596 protected $ac_unique = 'profile_job.pid';
602 // {{{ class DE_Networking
603 class DE_Networking
extends DirEnumeration
605 protected $idfield = 'profile_networking_enum.network_type';
606 protected $valfield = 'profile_networking_enum.name';
607 protected $from = 'profile_networking_enum';
610 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.network_type = profile_networking_enum.network_type)';
611 protected $ac_unique = 'profile_networking.pid';
615 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: