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 ***************************************************************************/
22 /** This class stores all data for the different kinds of fields.
23 * It is only a dispatcher for the various DirEnum_XXX classes.
27 /** Name of availables Enumerations
28 * Each of these consts contains the basename of the class (its full name
29 * being DE_$basename).
31 const BINETS
= 'binets';
32 const SECTIONS
= 'sections';
33 const SCHOOLS
= 'schools';
34 const DEGREES
= 'degrees';
35 const NATIONALITIES
= 'nationalities';
36 const COUNTRIES
= 'countries';
37 const GROUPESX
= 'groupesx';
38 const SECTORS
= 'sectors';
39 const NETWORKS
= 'networking';
40 const ADMINAREAS
= 'adminareas';
42 static private $enumerations = array();
44 static private function init($type)
46 $cls = "DE_" . ucfirst($type);
47 self
::$enumerations[$type] = new $cls();
50 /** Retrieves all options for a given type
51 * @param $type Type of enum for which options are requested
52 * @return XorgDbIterator over the results
53 * TODO : add support for optional parameters transmitted to enumerations
54 * TODO : Find a way to get either an array, or the adequate PlIterator
56 static public function getOptions()
58 $args = func_get_args();
59 $type = array_shift($args);
60 if (!array_key_exists($type, self
::$enumerations)) {
63 $obj = self
::$enumerations[$type];
64 return call_user_func_array(array($obj, 'getOptions'), $args);
68 abstract class DirEnumeration
70 /** An internal array of ID => optionTxt
74 protected $idfield = 'id';
75 protected $valfield = 'text';
77 protected $where = '';
79 public function __construct() {
83 public function getOptions()
85 return $this->options
;
88 /** The function used to load options
90 protected function loadOptions()
92 $this->options
= XDB
::iterator('SELECT ' . $this->valfield
. ' AS field,
93 ' . $this->idfield
. ' AS id
94 FROM ' . $this->from
. '
96 GROUP BY ' . $this->valfield
. '
97 ORDER BY ' . $this->valfield
);
101 class DE_Binets
extends DirEnumeration
103 protected $from = 'binets_def';
105 class DE_Sections
extends DirEnumeration
107 protected $from = 'sections';
109 class DE_Schools
extends DirEnumeration
111 protected $valfield = 'name';
112 protected $from = 'profile_education_enum';
114 class DE_Degrees
extends DirEnumeration
116 protected $suboptions = array();
118 protected function loadOptions()
120 $res = XDB
::query('SELECT ped.eduid, ped.degreeid, pede.degree
121 FROM profile_education_enum AS pee
122 LEFT JOIN profile_education_degree AS ped ON (pee.id = ped.eduid)
123 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
124 ORDER BY pede.degree');
125 foreach($res->fetchAllRow() as $row) {
126 list($eduid, $degreeid, $name) = $row;
127 $this->options
[$degreeid] = array('id' => $degreeid, 'field' => $name);
128 if (!array_key_exists($eduid, $this->suboptions
)) {
129 $this->suboptions
[$eduid] = array();
131 $this->suboptions
[$eduid][] = array('id' => $degreeid, 'field' => $name);
135 public function getOptions($eduid = null
)
137 if ($eduid == null
) {
138 return PlIteratorUtils
::fromArray($this->options
, 1, true
);
140 if (array_key_exists($eduid, $this->suboptions
)) {
141 return PlIteratorUtils
::fromArray($this->suboptions
[$eduid], 1, true
);
147 class DE_Nationalities
extends DirEnumeration
149 protected $idfield = 'iso_3166_1_a2';
150 protected $valfield = 'nationalityFR';
151 protected $from = 'geoloc_countries AS gc';
152 protected $where = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
154 class DE_Countries
extends DirEnumeration
156 protected $idfield = 'iso_3166_1_a2';
157 protected $valfield = 'countryFR';
158 protected $from = 'geoloc_countries';
160 class DE_AdminAreas
extends DirEnumeration
162 protected $suboptions = array();
164 protected function loadOptions()
166 $res = XDB
::query('SELECT id, name AS field, country
167 FROM geoloc_administrativeareas
170 foreach($res->fetchAllRow() as $row) {
171 list($id, $field, $country) = $row;
172 $this->options
[] = array('id' => $id, 'field' => $field);
173 if (!array_key_exists($country, $this->suboptions
)) {
174 $this->suboptions
[$country] = array();
176 $this->suboptions
[$country][] = array('id' => $id, 'field' => $field);
180 public function getOptions($country = null
)
182 if ($country == null
) {
183 return PlIteratorUtils
::fromArray($this->options
, 1, true
);
185 if (array_key_exists($country, $this->suboptions
)) {
186 return PlIteratorUtils
::fromArray($this->suboptions
[$country], 1, true
);
192 class DE_GroupesX
extends DirEnumeration
194 protected $valfield = 'nom';
195 protected $from = '#groupex#.asso';
196 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
198 class DE_Sectors
extends DirEnumeration
200 protected $valfield = 'name';
201 protected $from = 'profile_job_sector_enum';
203 class DE_Networking
extends DirEnumeration
205 protected $idfield = 'network_type';
206 protected $valfield = 'name';
207 protected $from = 'profile_networking_enum';