Move paiement db in x4dat (tables payment*).
[platal.git] / include / directory.enums.inc.php
CommitLineData
e39e72a6
RB
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
98925aab 22// {{{ class DirEnum
e39e72a6
RB
23/** This class stores all data for the different kinds of fields.
24 * It is only a dispatcher for the various DirEnum_XXX classes.
25 */
26class DirEnum
27{
28 /** Name of availables Enumerations
29 * Each of these consts contains the basename of the class (its full name
30 * being DE_$basename).
31 */
21b67462 32 const BINETS = 'binets';
5ab3ef57 33 const GROUPESX = 'groupesx';
21b67462 34 const SECTIONS = 'sections';
5ab3ef57 35
fb3b6547
RB
36 const EDUSCHOOLS = 'educationschools';
37 const EDUDEGREES = 'educationdegrees';
38 const EDUFIELDS = 'educationfields';
5ab3ef57 39
21b67462
RB
40 const NATIONALITIES = 'nationalities';
41 const COUNTRIES = 'countries';
5ab3ef57
RB
42 const ADMINAREAS = 'adminareas';
43 const LOCALITIES = 'localities';
44
45 const COMPANIES = 'companies';
21b67462 46 const SECTORS = 'sectors';
5ab3ef57
RB
47 const JOBDESCRIPTION = 'jobdescription';
48
21b67462 49 const NETWORKS = 'networking';
e39e72a6
RB
50
51 static private $enumerations = array();
52
53 static private function init($type)
54 {
55 $cls = "DE_" . ucfirst($type);
56 self::$enumerations[$type] = new $cls();
57 }
58
59 /** Retrieves all options for a given type
60 * @param $type Type of enum for which options are requested
61 * @return XorgDbIterator over the results
e39e72a6
RB
62 */
63 static public function getOptions()
64 {
65 $args = func_get_args();
66 $type = array_shift($args);
67 if (!array_key_exists($type, self::$enumerations)) {
68 self::init($type);
69 }
70 $obj = self::$enumerations[$type];
71 return call_user_func_array(array($obj, 'getOptions'), $args);
72 }
21b67462 73
a137e407
RB
74 /** Retrieves all options for a given type
75 * @param $type Type of enum for which options are requested
76 * @return Array of the results the results
77 */
78 static public function getOptionsArray()
79 {
80 $args = func_get_args();
81 $type = array_shift($args);
82 if (!array_key_exists($type, self::$enumerations)) {
83 self::init($type);
84 }
85 $obj = self::$enumerations[$type];
86 return call_user_func_array(array($obj, 'getOptionsArray'), $args);
87 }
88
30b9ca46
RB
89 /** Retrieves all options with number of profiles for autocompletion
90 * @param $type Type of enum for which options are requested
91 * @param $text Text to autocomplete
92 * @return XorgDbIterator over the results
93 */
94 static public function getAutoComplete()
95 {
96 $args = func_get_args();
97 $type = array_shift($args);
98 if (!array_key_exists($type, self::$enumerations)) {
99 self::init($type);
100 }
101 $obj = self::$enumerations[$type];
102 return call_user_func_array(array($obj, 'getAutoComplete'), $args);
103 }
104
98925aab
RB
105 /** Retrieves a list of IDs for a given type
106 * @param $type Type of enum for which IDs are requested
107 * @param $text Text to search in enum valuees
108 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
109 */
21b67462
RB
110 static public function getIDs()
111 {
112 $args = func_get_args();
113 $type = array_shift($args);
114 if (!array_key_exists($type, self::$enumerations)) {
115 self::init($type);
116 }
117 $obj = self::$enumerations[$type];
118 return call_user_func_array(array($obj, 'getIDs'), $args);
119 }
e39e72a6 120}
98925aab 121// }}}
e39e72a6 122
98925aab 123// {{{ class DirEnumeration
e39e72a6
RB
124abstract class DirEnumeration
125{
21b67462
RB
126 /** Modes for LIKE searches
127 */
128 const MODE_EXACT = 0x000;
129 const MODE_PREFIX = 0x001;
130 const MODE_SUFFIX = 0x002;
131 const MODE_CONTAINS = 0x003;
132
e39e72a6
RB
133 /** An internal array of ID => optionTxt
134 */
9ed7f5bc 135 protected $options = null;
e39e72a6 136
21b67462
RB
137 /** Description of the MySQL storage of the fields
138 */
e39e72a6
RB
139 protected $idfield = 'id';
140 protected $valfield = 'text';
30b9ca46 141 protected $valfield2 = null;
e39e72a6 142 protected $from;
21b67462 143 protected $join = '';
e39e72a6
RB
144 protected $where = '';
145
30b9ca46
RB
146 /** Fields for autocompletion
147 */
148 protected $ac_join = ''; // Additional joins
149 protected $ac_where = null; // Additional where
150 protected $ac_beginwith = true; // Whether to search for 'x%' or for '%x%'
151 protected $ac_unique; // Which field is to be taken as unique
152 protected $ac_distinct = true; // Whether we want to keep only distinct valfield value
153 protected $ac_withid = true; // Do we want to fetch id too ?
154
9ed7f5bc
RB
155 protected function _fetchOptions()
156 {
157 if (is_null($this->options)) {
158 $this->loadOptions();
159 }
e39e72a6
RB
160 }
161
162 public function getOptions()
163 {
9ed7f5bc 164 $this->_fetchOptions();
e39e72a6
RB
165 return $this->options;
166 }
167
a137e407
RB
168 public function getOptionsArray()
169 {
170 $this->_fetchOptions();
171 $options = array();
172 while ($row = $this->options->next()) {
173 $options[$row['id']] = $row['field'];
174 }
175 return $options;
176 }
177
30b9ca46 178 // {{{ function getIDs
21b67462
RB
179 /** Retrieves possible IDs for given text
180 * @param $text Text to search for IDs
181 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
182 * @return An array of matching IDs ; if empty, input should be considered invalid
183 */
184 public function getIDs($text, $mode)
185 {
186 if ($mode == self::MODE_EXACT) {
187 $options = $this->getOptions();
188 return array_keys($options, $text);
189 } else {
190 if ($this->where == null) {
191 $where = 'WHERE ';
192 } else {
193 $where = $this->where . ' AND ';
194 }
30b9ca46
RB
195 $conds = array();
196 $conds[] = $this->valfield . self::makeSqlConcat($text, $mode);
197 if ($this->valfield2 != null) {
198 $conds[] = $this->valfield2 . self::makeSqlConcat($text, $mode);
199 }
200 $where .= '(' . implode(' OR ', $conds) . ')';
201
21b67462
RB
202 return XDB::fetchColumn('SELECT ' . $this->idfield . '
203 FROM ' . $this->from . '
204 ' . $this->join . '
30b9ca46 205 ' . $where . '
21b67462
RB
206 GROUP BY ' . $this->idfield);
207 }
208 }
30b9ca46 209 // }}}
21b67462 210
30b9ca46
RB
211 private function mkTests($field, $text)
212 {
213 $tests = array();
214 $tests[] = $field . self::makeSqlConcat($text, self::MODE_PREFIX);
215 if (!$this->ac_beginwith) {
216 $tests[] = $field . self::makeSqlConcat(' ' . $text, self::MODE_CONTAINS);
217 $tests[] = $field . self::makeSqlConcat('-' . $text, self::MODE_CONTAINS);
218 }
219 return $tests;
220 }
221
222 // {{{ function getAutoComplete
223 public function getAutoComplete($text)
224 {
225 $text = str_replace(array('%', '_'), '', $text);
226
227 if (is_null($this->ac_where) || $this->ac_where == '') {
228 $where = '';
229 } else {
230 $where = $this->ac_where . ' AND ';
231 }
232
233 $tests = $this->mkTests($this->valfield, $text);
234 if (!is_null($this->valfield2)) {
235 $tests = array_merge($tests, $this->mkTests($this->valfield2, $text));
236 }
237
238 $where .= '(' . implode(' OR ', $tests) . ')';
239
240 return XDB::iterator('SELECT ' . $this->valfield . ' AS field'
241 . ($this->ac_distinct ? (', COUNT(DISTINCT ' . $this->ac_unique . ') AS nb') : '')
242 . ($this->ac_withid ? (', ' . $this->idfield . ' AS id') : '') . '
243 FROM ' . $this->from . '
244 ' . $this->ac_join . '
245 WHERE ' . $where . '
246 GROUP BY ' . $this->valfield . '
247 ORDER BY ' . ($this->ac_distinct ? 'nb DESC' : $this->valfield) . '
248 LIMIT 11');
249 }
250 // }}}
251
252 // {{{ function makeSqlConcat
21b67462
RB
253 static protected function makeSqlConcat($text, $mode)
254 {
255 if ($mode == self::MODE_EXACT) {
256 return ' = ' . XDB::format('{?}', $text);
257 }
30b9ca46 258 if ($mode == self::MODE_PREFIX) {
21b67462 259 $right = XDB::format('CONCAT({?}, \'%\')', $text);
30b9ca46 260 } else if ($mode == self::MODE_SUFFIX) {
21b67462
RB
261 $right = XDB::format('CONCAT(\'%\', {?})', $text);
262 } else {
263 $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $text);
264 }
265 return ' LIKE ' . $right;
266 }
30b9ca46 267 // }}}
21b67462 268
30b9ca46 269 // {{{ function loadOptions
e39e72a6
RB
270 /** The function used to load options
271 */
272 protected function loadOptions()
273 {
274 $this->options = XDB::iterator('SELECT ' . $this->valfield . ' AS field,
275 ' . $this->idfield . ' AS id
276 FROM ' . $this->from . '
21b67462 277 ' . $this->join . '
e39e72a6
RB
278 ' . $this->where . '
279 GROUP BY ' . $this->valfield . '
280 ORDER BY ' . $this->valfield);
281 }
30b9ca46 282 // }}}
e39e72a6 283}
98925aab 284// }}}
e39e72a6 285
5ab3ef57
RB
286/** GROUPS
287 */
98925aab 288// {{{ class DE_Binets
e39e72a6
RB
289class DE_Binets extends DirEnumeration
290{
291 protected $from = 'binets_def';
30b9ca46
RB
292
293 protected $ac_join = 'INNER JOIN binets_ins ON (binets_def.id = binets_ins.binet_id)';
294 protected $ac_unique = 'binets_ins.user_id';
e39e72a6 295}
98925aab
RB
296// }}}
297
298// {{{ class DE_Sections
e39e72a6
RB
299class DE_Sections extends DirEnumeration
300{
301 protected $from = 'sections';
30b9ca46
RB
302
303 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = sections.id)';
304 protected $ac_unique = 'profiles.pid';
e39e72a6 305}
98925aab
RB
306// }}}
307
5ab3ef57
RB
308// {{{ class DE_GroupesX
309class DE_GroupesX extends DirEnumeration
310{
311 protected $idfield = 'asso.id';
312 protected $valfield = 'asso.nom';
313 protected $valfield2 = 'asso.diminutif';
314 protected $from = '#groupex#.asso AS asso';
315 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
316
317 protected $ac_join = "INNER JOIN #groupex#.membres AS memb ON (asso.id = memb.asso_id
318 AND (asso.cat = 'GroupesX' OR asso.cat = 'Institutions')
319 AND asso.pub = 'public')";
320 protected $ac_unique = 'memb.uid';
321}
322// }}}
323
324/** EDUCATION
325 */
fb3b6547
RB
326// {{{ class DE_EducationSchools
327class DE_EducationSchools extends DirEnumeration
e39e72a6 328{
30b9ca46
RB
329 protected $valfield = 'name';
330 protected $valfield2 = 'abbreviation';
331 protected $from = 'profile_education_enum';
332
333 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
334 protected $ac_unique = 'profile_education.uid';
e39e72a6 335}
98925aab
RB
336// }}}
337
fb3b6547
RB
338// {{{ class DE_EducationDegrees
339class DE_EducationDegrees extends DirEnumeration
e39e72a6 340{
30b9ca46
RB
341 protected $from = 'profile_education_degree_enum';
342 protected $valfield = 'degree';
343
e39e72a6
RB
344 protected $suboptions = array();
345
346 protected function loadOptions()
347 {
348 $res = XDB::query('SELECT ped.eduid, ped.degreeid, pede.degree
349 FROM profile_education_enum AS pee
350 LEFT JOIN profile_education_degree AS ped ON (pee.id = ped.eduid)
351 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
352 ORDER BY pede.degree');
353 foreach($res->fetchAllRow() as $row) {
354 list($eduid, $degreeid, $name) = $row;
355 $this->options[$degreeid] = array('id' => $degreeid, 'field' => $name);
356 if (!array_key_exists($eduid, $this->suboptions)) {
357 $this->suboptions[$eduid] = array();
358 }
359 $this->suboptions[$eduid][] = array('id' => $degreeid, 'field' => $name);
360 }
361 }
362
363 public function getOptions($eduid = null)
364 {
9ed7f5bc 365 $this->_fetchOptions();
e39e72a6
RB
366 if ($eduid == null) {
367 return PlIteratorUtils::fromArray($this->options, 1, true);
368 }
369 if (array_key_exists($eduid, $this->suboptions)) {
370 return PlIteratorUtils::fromArray($this->suboptions[$eduid], 1, true);
371 } else {
372 return array();
373 }
374 }
21b67462
RB
375
376 public function getIDs($text, $mode, $eduid = null)
377 {
378 if ($eduid == null) {
379 return XDB::fetchColumn('SELECT id
380 FROM profile_education_degree_enum
381 WHERE degree ' . self::makeSqlConcat($text, $mode));
382 } else {
383 return XDB::fetchColumn('SELECT pede.id
384 FROM profile_education_degree AS ped
385 LEFT JOIN profile_education_degree_enum AS pede ON (ped.degreeid = pede.id)
386 WHERE ped.eduid = {?} AND pede.degree ' . self::makeSqlConcat($text, $mode), $eduid);
387 }
388 }
389}
98925aab
RB
390// }}}
391
fb3b6547
RB
392// {{{ class DE_EducationFields
393class DE_EducationFields extends DirEnumeration
21b67462
RB
394{
395 protected $valfield = 'field';
30b9ca46
RB
396 protected $from = 'profile_education_field_enum';
397
398 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
399 protected $ac_unique = 'profile_education.uid';
e39e72a6 400}
98925aab
RB
401// }}}
402
5ab3ef57
RB
403/** GEOLOC
404 */
98925aab 405// {{{ class DE_Nationalities
e39e72a6
RB
406class DE_Nationalities extends DirEnumeration
407{
30b9ca46
RB
408 protected $idfield = 'iso_3166_1_a2';
409 protected $valfield = 'nationalityFR';
410 protected $valfield2 = 'nationality';
411 protected $from = 'geoloc_countries AS gc';
412 protected $join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
413
414 protected $ac_join = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
415 protected $ac_unique = 'profiles.pid';
e39e72a6 416}
98925aab
RB
417// }}}
418
419// {{{ class DE_Countries
e39e72a6
RB
420class DE_Countries extends DirEnumeration
421{
30b9ca46
RB
422 protected $idfield = 'iso_3166_1_a2';
423 protected $valfield = 'countryFR';
424 protected $valfield2 = 'country';
425 protected $from = 'geoloc_countries';
426
427 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryFR';
428 protected $ac_unique = 'profile_addresses.pid';
e39e72a6 429}
98925aab
RB
430// }}}
431
432// {{{ class DE_AdminAreas
e39e72a6
RB
433class DE_AdminAreas extends DirEnumeration
434{
435 protected $suboptions = array();
436
437 protected function loadOptions()
438 {
439 $res = XDB::query('SELECT id, name AS field, country
440 FROM geoloc_administrativeareas
441 GROUP BY name
442 ORDER BY name');
443 foreach($res->fetchAllRow() as $row) {
444 list($id, $field, $country) = $row;
445 $this->options[] = array('id' => $id, 'field' => $field);
446 if (!array_key_exists($country, $this->suboptions)) {
447 $this->suboptions[$country] = array();
448 }
449 $this->suboptions[$country][] = array('id' => $id, 'field' => $field);
450 }
451 }
452
453 public function getOptions($country = null)
454 {
9ed7f5bc
RB
455 $this->_fetchOptions();
456
e39e72a6
RB
457 if ($country == null) {
458 return PlIteratorUtils::fromArray($this->options, 1, true);
459 }
460 if (array_key_exists($country, $this->suboptions)) {
461 return PlIteratorUtils::fromArray($this->suboptions[$country], 1, true);
462 } else {
463 return array();
464 }
465 }
21b67462
RB
466
467 public function getIDs($text, $mode, $country = null)
468 {
469 if ($country == null) {
470 return XDB::fetchColumn('SELECT id
471 FROM geoloc_administrativeareas
472 WHERE name ' . self::makeSqlConcat($text, $mode));
473 } else {
474 return XDB::fetchColumn('SELECT id
475 FROM geoloc_administrativeareas
476 WHERE country = {?} AND name' . self::makeSqlConcat($text, $mode), $country);
477 }
478 }
e39e72a6 479}
98925aab
RB
480// }}}
481
5ab3ef57
RB
482// {{{ class DE_Localities
483class DE_Localities extends DirEnumeration
e39e72a6 484{
5ab3ef57
RB
485 protected $valfield = 'gl.name';
486 protected $from = 'geoloc_localities AS gl';
30b9ca46 487
5ab3ef57
RB
488 protected $ac_join = 'profile_addresses AS pa ON (pa.localityID = gl.id)';
489 protected $ac_unique = 'pa.pid';
490}
491// }}}
492
493/** JOBS
494 */
495// {{{ class DE_Companies
496class DE_Companies extends DirEnumeration
497{
498 protected $valfield = 'pje.name';
499 protected $valfield2 = 'pje.acronym';
500 protected $from = 'profile_job_enum AS pje';
501
502 protected $ac_join = 'INNER JOIN profile_job AS pj ON (pj.jobid = pje.id)';
503 protected $ac_unique = 'pj.uid';
e39e72a6 504}
98925aab
RB
505// }}}
506
507// {{{ class DE_Sectors
e39e72a6
RB
508class DE_Sectors extends DirEnumeration
509{
30b9ca46
RB
510 protected $valfield = 'name';
511 protected $from = 'profile_job_sector_enum';
512
513 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
514 protected $ac_unique = 'profile_job.uid';
e39e72a6 515}
98925aab
RB
516// }}}
517
5ab3ef57
RB
518// {{{ class DE_JobDescription
519class DE_JobDescription
520{
521 protected $valfield = 'pj.description';
522 protected $from = 'profile_job AS pj';
523 protected $idfield = 'pj.pid';
524
525 protected $ac_unique = 'pj.pid';
526}
527// }}}
528
529/** NETWORKING
530 */
98925aab 531// {{{ class DE_Networking
e39e72a6
RB
532class DE_Networking extends DirEnumeration
533{
30b9ca46
RB
534 protected $idfield = 'profile_networking_enum.network_type';
535 protected $valfield = 'profile_networking_enum.name';
e39e72a6 536 protected $from = 'profile_networking_enum';
30b9ca46
RB
537
538
539 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.network_type = profile_networking_enum.network_type';
540 protected $ac_unique = 'profile_networking.uid';
e39e72a6 541}
98925aab 542// }}}
e39e72a6 543?>