Add class DirEnum, to access all enumerations used in the site
[platal.git] / include / directory.enums.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 /** This class stores all data for the different kinds of fields.
23 * It is only a dispatcher for the various DirEnum_XXX classes.
24 */
25 class DirEnum
26 {
27 /** Name of availables Enumerations
28 * Each of these consts contains the basename of the class (its full name
29 * being DE_$basename).
30 */
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';
41
42 static private $enumerations = array();
43
44 static private function init($type)
45 {
46 $cls = "DE_" . ucfirst($type);
47 self::$enumerations[$type] = new $cls();
48 }
49
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
55 */
56 static public function getOptions()
57 {
58 $args = func_get_args();
59 $type = array_shift($args);
60 if (!array_key_exists($type, self::$enumerations)) {
61 self::init($type);
62 }
63 $obj = self::$enumerations[$type];
64 return call_user_func_array(array($obj, 'getOptions'), $args);
65 }
66 }
67
68 abstract class DirEnumeration
69 {
70 /** An internal array of ID => optionTxt
71 */
72 protected $options;
73
74 protected $idfield = 'id';
75 protected $valfield = 'text';
76 protected $from;
77 protected $where = '';
78
79 public function __construct() {
80 $this->loadOptions();
81 }
82
83 public function getOptions()
84 {
85 return $this->options;
86 }
87
88 /** The function used to load options
89 */
90 protected function loadOptions()
91 {
92 $this->options = XDB::iterator('SELECT ' . $this->valfield . ' AS field,
93 ' . $this->idfield . ' AS id
94 FROM ' . $this->from . '
95 ' . $this->where . '
96 GROUP BY ' . $this->valfield . '
97 ORDER BY ' . $this->valfield);
98 }
99 }
100
101 class DE_Binets extends DirEnumeration
102 {
103 protected $from = 'binets_def';
104 }
105 class DE_Sections extends DirEnumeration
106 {
107 protected $from = 'sections';
108 }
109 class DE_Schools extends DirEnumeration
110 {
111 protected $valfield = 'name';
112 protected $from = 'profile_education_enum';
113 }
114 class DE_Degrees extends DirEnumeration
115 {
116 protected $suboptions = array();
117
118 protected function loadOptions()
119 {
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();
130 }
131 $this->suboptions[$eduid][] = array('id' => $degreeid, 'field' => $name);
132 }
133 }
134
135 public function getOptions($eduid = null)
136 {
137 if ($eduid == null) {
138 return PlIteratorUtils::fromArray($this->options, 1, true);
139 }
140 if (array_key_exists($eduid, $this->suboptions)) {
141 return PlIteratorUtils::fromArray($this->suboptions[$eduid], 1, true);
142 } else {
143 return array();
144 }
145 }
146 }
147 class DE_Nationalities extends DirEnumeration
148 {
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))';
153 }
154 class DE_Countries extends DirEnumeration
155 {
156 protected $idfield = 'iso_3166_1_a2';
157 protected $valfield = 'countryFR';
158 protected $from = 'geoloc_countries';
159 }
160 class DE_AdminAreas extends DirEnumeration
161 {
162 protected $suboptions = array();
163
164 protected function loadOptions()
165 {
166 $res = XDB::query('SELECT id, name AS field, country
167 FROM geoloc_administrativeareas
168 GROUP BY name
169 ORDER BY name');
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();
175 }
176 $this->suboptions[$country][] = array('id' => $id, 'field' => $field);
177 }
178 }
179
180 public function getOptions($country = null)
181 {
182 if ($country == null) {
183 return PlIteratorUtils::fromArray($this->options, 1, true);
184 }
185 if (array_key_exists($country, $this->suboptions)) {
186 return PlIteratorUtils::fromArray($this->suboptions[$country], 1, true);
187 } else {
188 return array();
189 }
190 }
191 }
192 class DE_GroupesX extends DirEnumeration
193 {
194 protected $valfield = 'nom';
195 protected $from = '#groupex#.asso';
196 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
197 }
198 class DE_Sectors extends DirEnumeration
199 {
200 protected $valfield = 'name';
201 protected $from = 'profile_job_sector_enum';
202 }
203 class DE_Networking extends DirEnumeration
204 {
205 protected $idfield = 'network_type';
206 protected $valfield = 'name';
207 protected $from = 'profile_networking_enum';
208 }
209 ?>