Fix networking with new network_type issues introduced by previous commit
[platal.git] / classes / direnum.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 // {{{ class DirEnum
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 */
26 class 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 */
32 const NAMETYPES = 'nametypes';
33 const NAMES = 'names';
34
35 const BINETS = 'binets';
36 const GROUPESX = 'groupesx';
37 const SECTIONS = 'sections';
38
39 const EDUSCHOOLS = 'educationschools';
40 const EDUDEGREES = 'educationdegrees';
41 const EDUFIELDS = 'educationfields';
42
43 const NATIONALITIES = 'nationalities';
44 const COUNTRIES = 'countries';
45 const ADMINAREAS = 'adminareas';
46 const LOCALITIES = 'localities';
47
48 const COMPANIES = 'companies';
49 const SECTORS = 'sectors';
50 const JOBDESCRIPTION = 'jobdescription';
51
52 const NETWORKS = 'networking';
53
54 static private $enumerations = array();
55
56 static private function init($type)
57 {
58 if (S::has('__DE_' . $type)) {
59 self::$enumerations[$type] = S::v('__DE_' . $type);
60 } else {
61 $cls = "DE_" . ucfirst($type);
62 $obj = new $cls();
63 self::$enumerations[$type] = $obj;
64 if ($obj->capabilities & DirEnumeration::SAVE_IN_SESSION) {
65 S::set('__DE_' . $type, $obj);
66 }
67 }
68 }
69
70 /** Retrieves all options for a given type
71 * @param $type Type of enum for which options are requested
72 * @return Array of the results
73 */
74 static public function getOptions($type)
75 {
76 if (!array_key_exists($type, self::$enumerations)) {
77 self::init($type);
78 }
79 $obj = self::$enumerations[$type];
80 if ($obj->capabilities & DirEnumeration::HAS_OPTIONS) {
81 return call_user_func(array($obj, 'getOptions'));
82 } else {
83 return array();
84 }
85 }
86
87 /** Retrieves all options for a given type
88 * @param $type Type of enum for which options are requested
89 * @return PlIterator over the results
90 */
91 static public function getOptionsIter($type)
92 {
93 if (!array_key_exists($type, self::$enumerations)) {
94 self::init($type);
95 }
96 $obj = self::$enumerations[$type];
97 $args = func_get_args();
98 array_shift($args);
99 if ($obj->capabilities & DirEnumeration::HAS_OPTIONS) {
100 return call_user_func_array(array($obj, 'getOptionsIter'), $args);
101 } else {
102 return PlIteratorUtils::fromArray(array());
103 }
104 }
105
106 /** Retrieves all options with number of profiles for autocompletion
107 * @param $type Type of enum for which options are requested
108 * @param $text Text to autocomplete
109 * @return PlIterator over the results
110 */
111 static public function getAutoComplete($type, $text)
112 {
113 if (!array_key_exists($type, self::$enumerations)) {
114 self::init($type);
115 }
116 $obj = self::$enumerations[$type];
117 if ($obj->capabilities & DirEnumeration::HAS_AUTOCOMP) {
118 return call_user_func(array($obj, 'getAutoComplete'), $text);
119 } else {
120 return PlIteratorUtils::fromArray(array());
121 }
122 }
123
124 /** Retrieves a list of IDs for a given type
125 * @param $type Type of enum for which IDs are requested
126 * @param $text Text to search in enum valuees
127 * @param $mode Mode of search for those IDs (prefix/suffix/infix)
128 */
129 static public function getIDs($type, $text, $mode = XDB::WILDCARD_EXACT)
130 {
131 if (!array_key_exists($type, self::$enumerations)) {
132 self::init($type);
133 }
134 $obj = self::$enumerations[$type];
135 if ($obj->capabilities & DirEnumeration::HAS_OPTIONS) {
136 return call_user_func(array($obj, 'getIDs'), $text, $mode);
137 } else {
138 return array();
139 }
140 }
141
142 /** Retrieves a single ID for a given type.
143 * @param $type Type of the enum for which an ID is requested
144 * @param $text Text to search in enum values
145 * @param $mode Mode of search of that ID (prefix/suffix/infix/exact)
146 */
147 static public function getID($type, $text, $mode = XDB::WILDCARD_EXACT)
148 {
149 $ids = self::getIDs($type, $text, $mode);
150 return array_shift($ids);
151 }
152 }
153 // }}}
154
155 // {{{ class DirEnumeration
156 abstract class DirEnumeration
157 {
158 const AUTOCOMPLETE_LIMIT = 11;
159
160 const HAS_OPTIONS = 0x001;
161 const HAS_AUTOCOMP = 0x002;
162 const SAVE_IN_SESSION = 0x004;
163
164 public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
165
166 /** An internal array of ID => optionTxt
167 */
168 protected $options = null;
169
170 /** Description of the MySQL storage of the fields
171 */
172 protected $idfield = 'id';
173 protected $valfield = 'text';
174 protected $valfield2 = null;
175 protected $from;
176 protected $join = '';
177 protected $where = '';
178
179 /** Fields for autocompletion
180 */
181 protected $ac_join = ''; // Additional joins
182 protected $ac_where = null; // Additional where
183 protected $ac_beginwith = true; // Whether to search for 'x%' or for '%x%'
184 protected $ac_unique; // Which field is to be taken as unique
185 protected $ac_distinct = true; // Whether we want to keep only distinct valfield value
186 protected $ac_withid = true; // Do we want to fetch id too ?
187
188 protected function _fetchOptions()
189 {
190 if (is_null($this->options)) {
191 $this->loadOptions();
192 }
193 }
194
195 public function getOptions()
196 {
197 $this->_fetchOptions();
198 return $this->options;
199 }
200
201 public function getOptionsIter()
202 {
203 $options = $this->getOptions();
204 $options = self::expandArray($options);
205 return PlIteratorUtils::fromArray($options, 1, true);
206 }
207
208 // {{{ function getIDs
209 /** Retrieves possible IDs for given text
210 * @param $text Text to search for IDs
211 * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
212 * @return An array of matching IDs ; if empty, input should be considered invalid
213 */
214 public function getIDs($text, $mode)
215 {
216 if ($mode == XDB::WILDCARD_EXACT) {
217 $options = $this->getOptions();
218 return array_keys($options, $text);
219 } else {
220 if ($this->where == null) {
221 $where = 'WHERE ';
222 } else {
223 $where = $this->where . ' AND ';
224 }
225 $conds = array();
226 $conds[] = $this->valfield . XDB::formatWildcards($mode, $text);
227 if ($this->valfield2 != null) {
228 $conds[] = $this->valfield2 . XDB::formatWildcards($mode, $text);
229 }
230 $where .= '(' . implode(' OR ', $conds) . ')';
231
232 return XDB::fetchColumn('SELECT ' . $this->idfield . '
233 FROM ' . $this->from . '
234 ' . $this->join . '
235 ' . $where . '
236 GROUP BY ' . $this->idfield);
237 }
238 }
239 // }}}
240
241 private function mkTests($field, $text)
242 {
243 $tests = array();
244 $tests[] = $field . XDB::formatWildcards(XDB::WILDCARD_PREFIX, $text);
245 if (!$this->ac_beginwith) {
246 $tests[] = $field . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, ' ' . $text);
247 $tests[] = $field . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, '-' . $text);
248 }
249 return $tests;
250 }
251
252 static protected function expandArray(array $tab, $keyname = 'id', $valname = 'field')
253 {
254 $res = array();
255 foreach ($tab as $key => $val) {
256 $res[$key] = array(
257 $keyname => $key,
258 $valname => $val,
259 );
260 }
261 return $res;
262 }
263
264 // {{{ function getAutoComplete
265 public function getAutoComplete($text)
266 {
267 $text = str_replace(array('%', '_'), '', $text);
268
269 if (is_null($this->ac_where) || $this->ac_where == '') {
270 $where = '';
271 } else {
272 $where = $this->ac_where . ' AND ';
273 }
274
275 $tests = $this->mkTests($this->valfield, $text);
276 if (!is_null($this->valfield2)) {
277 $tests = array_merge($tests, $this->mkTests($this->valfield2, $text));
278 }
279
280 $where .= '(' . implode(' OR ', $tests) . ')';
281
282 return XDB::iterator('SELECT ' . $this->valfield . ' AS field'
283 . ($this->ac_distinct ? (', COUNT(DISTINCT ' . $this->ac_unique . ') AS nb') : '')
284 . ($this->ac_withid ? (', ' . $this->idfield . ' AS id') : '') . '
285 FROM ' . $this->from . '
286 ' . $this->ac_join . '
287 WHERE ' . $where . '
288 GROUP BY ' . $this->valfield . '
289 ORDER BY ' . ($this->ac_distinct ? 'nb DESC' : $this->valfield) . '
290 LIMIT ' . self::AUTOCOMPLETE_LIMIT);
291 }
292 // }}}
293
294 // {{{ function loadOptions
295 /** The function used to load options
296 */
297 protected function loadOptions()
298 {
299 $this->options = XDB::fetchAllAssoc('id', 'SELECT ' . $this->valfield . ' AS field,
300 ' . $this->idfield . ' AS id
301 FROM ' . $this->from . '
302 ' . $this->join . '
303 ' . $this->where . '
304 GROUP BY ' . $this->valfield . '
305 ORDER BY ' . $this->valfield);
306 }
307 // }}}
308 }
309 // }}}
310
311 // {{{ class DE_WithSuboption
312 /** A class for DirEnum with possibility to select only suboptions for a given parameter (country, school, ...)
313 */
314 abstract class DE_WithSuboption extends DirEnumeration
315 {
316 protected $optfield;
317
318 protected $suboptions = null;
319
320 protected function loadOptions()
321 {
322 $opts = XDB::fetchAllAssoc('id', 'SELECT ' . $this->valfield . ' AS field,
323 ' . $this->optfield . ' AS subid,
324 ' . $this->idfield . ' AS id
325 FROM ' . $this->from . '
326 ' . $this->join . '
327 ' . $this->where . '
328 GROUP BY ' . $this->valfield . '
329 ORDER BY ' . $this->valfield);
330 $this->options = array();
331 $this->suboptions = array();
332 foreach ($opts as $id => $opt) {
333 $this->options[$id] = $opt['field'];
334 if (!array_key_exists($opt['subid'], $this->suboptions)) {
335 $this->suboptions[$opt['subid']] = array();
336 }
337 $this->suboptions[$opt['subid']][$id] = $opt['field'];
338 }
339 }
340
341 public function getOptions($subid = null)
342 {
343 $this->_fetchOptions();
344 if ($subid == null) {
345 return $this->options;
346 } else if (array_key_exists($subid, $this->suboptions)) {
347 return $this->suboptions[$subid];
348 } else {
349 return array();
350 }
351 }
352
353 public function getOptionsIter($subid = null)
354 {
355 return PlIteratorUtils::fromArray(self::expandArray($this->getOptions($subid)), 1, true);
356 }
357
358 public function getIDs($text, $mode, $subid = null)
359 {
360 if ($mode == XDB::WILDCARD_EXACT) {
361 $options = $this->getOptions($subid);
362 return array_keys($options, $text);
363 } else {
364 if ($this->where == null) {
365 $where = 'WHERE ';
366 } else {
367 $where = $this->where . ' AND ';
368 }
369 if ($subid != null && array_key_exists($subid, $this->suboptions)) {
370 $where .= XDB::format($this->optfield . ' = {?} AND ', $subid);
371 }
372
373 $conds = array();
374 $conds[] = $this->valfield . XDB::formatWildcards($mode, $text);
375 if ($this->valfield2 != null) {
376 $conds[] = $this->valfield2 . XDB::formatWildcards($mode, $text);
377 }
378 $where .= '(' . implode(' OR ', $conds) . ')';
379
380 return XDB::fetchColumn('SELECT ' . $this->idfield . '
381 FROM ' . $this->from . '
382 ' . $this->join . '
383 ' . $where . '
384 GROUP BY ' . $this->idfield);
385 }
386 }
387
388 public function getAutoComplete($text, $subid = null)
389 {
390 $text = str_replace(array('%', '_'), '', $text);
391
392 if (is_null($this->ac_where) || $this->ac_where == '') {
393 $where = '';
394 } else {
395 $where = $this->ac_where . ' AND ';
396 }
397
398 if ($subid != null && array_key_exists($subid, $this->suboptions)) {
399 $where .= XDB::format($this->optfield . ' = {?} AND ', $subid);
400 }
401
402 $tests = $this->mkTests($this->valfield, $text);
403 if (!is_null($this->valfield2)) {
404 $tests = array_merge($tests, $this->mkTests($this->valfield2, $text));
405 }
406
407 $where .= '(' . implode(' OR ', $tests) . ')';
408
409 return XDB::iterator('SELECT ' . $this->valfield . ' AS field'
410 . ($this->ac_distinct ? (', COUNT(DISTINCT ' . $this->ac_unique . ') AS nb') : '')
411 . ($this->ac_withid ? (', ' . $this->idfield . ' AS id') : '') . '
412 FROM ' . $this->from . '
413 ' . $this->ac_join . '
414 WHERE ' . $where . '
415 GROUP BY ' . $this->valfield . '
416 ORDER BY ' . ($this->ac_distinct ? 'nb DESC' : $this->valfield) . '
417 LIMIT ' . self::AUTOCOMPLETE_LIMIT);
418 }
419 }
420 // }}}
421
422 // {{{ class DE_NameTypes
423 // returns 'system' names ('lastname', 'lastname_marital', ...)
424 class DE_NameTypes extends DirEnumeration
425 {
426 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
427
428 protected $from = 'profile_name_enum';
429 protected $valfield = 'type';
430 }
431 // }}}
432
433 // {{{ class DE_Names
434 // returns 'system' names ('lastname', 'lastname_marital', ...)
435 class DE_Names extends DirEnumeration
436 {
437 public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
438
439 protected $from = 'profile_name_enum';
440 protected $idfield = 'type';
441 protected $valfield = 'name';
442 }
443 // }}}
444
445 /** GROUPS
446 */
447 // {{{ class DE_Binets
448 class DE_Binets extends DirEnumeration
449 {
450 protected $from = 'profile_binet_enum';
451
452 protected $ac_join = 'INNER JOIN profile_binets ON (profile_binet_enum.id = profile_binets.binet_id)';
453 protected $ac_unique = 'profile_binets.pid';
454 }
455 // }}}
456
457 // {{{ class DE_Sections
458 class DE_Sections extends DirEnumeration
459 {
460 protected $from = 'profile_section_enum';
461
462 protected $ac_join = 'INNER JOIN profiles ON (profiles.section = profile_section_enum.id)';
463 protected $ac_unique = 'profiles.pid';
464 }
465 // }}}
466
467 // {{{ class DE_GroupesX
468 class DE_GroupesX extends DirEnumeration
469 {
470 protected $idfield = 'groups.id';
471 protected $valfield = 'groups.nom';
472 protected $valfield2 = 'groups.diminutif';
473 protected $from = 'groups';
474 protected $where = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
475
476 protected $ac_join = "INNER JOIN group_members ON (groups.id = group_members.asso_id
477 AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
478 AND groups.pub = 'public')";
479 protected $ac_unique = 'group_members.uid';
480 }
481 // }}}
482
483 /** EDUCATION
484 */
485 // {{{ class DE_EducationSchools
486 class DE_EducationSchools extends DirEnumeration
487 {
488 protected $idfield = 'profile_education_enum.id';
489 protected $valfield = 'profile_education_enum.name';
490 protected $valfield2 = 'profile_education_enum.abbreviation';
491 protected $from = 'profile_education_enum';
492
493 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
494 protected $ac_unique = 'profile_education.pid';
495 }
496 // }}}
497
498 // {{{ class DE_EducationDegrees
499 class DE_EducationDegrees extends DirEnumeration
500 {
501 public $capabilities = self::HAS_OPTIONS;
502
503 protected $idfield = 'profile_education_degree.degreeid';
504 protected $optfield = 'profile_education_degree.eduid';
505 protected $valfield = 'profile_education_degree_enum.degree';
506 protected $from = 'profile_education_degree_enum';
507 protected $join = 'INNER JOIN profile_education_degree ON (profile_education_degree.degreeid = profile_education_degree_enum.id)';
508
509 }
510 // }}}
511
512 // {{{ class DE_EducationFields
513 class DE_EducationFields extends DirEnumeration
514 {
515 protected $valfield = 'profile_education_field_enum.field';
516 protected $from = 'profile_education_field_enum';
517
518 protected $ac_join = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
519 protected $ac_unique = 'profile_education.pid';
520 }
521 // }}}
522
523 /** GEOLOC
524 */
525 // {{{ class DE_Nationalities
526 class DE_Nationalities extends DirEnumeration
527 {
528 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
529 protected $valfield = 'geoloc_countries.nationalityFR';
530 protected $valfield2 = 'geoloc_countries.nationality';
531 protected $from = 'geoloc_countries';
532 protected $join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
533
534 protected $ac_join = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
535 protected $ac_unique = 'profiles.pid';
536 }
537 // }}}
538
539 // {{{ class DE_Countries
540 class DE_Countries extends DirEnumeration
541 {
542 protected $idfield = 'geoloc_countries.iso_3166_1_a2';
543 protected $valfield = 'geoloc_countries.countryFR';
544 protected $valfield2 = 'geoloc_countries.country';
545 protected $from = 'geoloc_countries';
546
547 protected $ac_join = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
548 protected $ac_unique = 'profile_addresses.pid';
549 }
550 // }}}
551
552 // {{{ class DE_AdminAreas
553 class DE_AdminAreas extends DE_WithSuboption
554 {
555 protected $idfield = 'geoloc_administrativeareas.id';
556 protected $optfield = 'geoloc_administrativeareas.country';
557 protected $valfield = 'geoloc_administrativeareas.name';
558 protected $from = 'geoloc_administrativeareas';
559
560 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
561 protected $ac_unique = 'profile_addresses.pid';
562 }
563 // }}}
564
565 // {{{ class DE_Localities
566 class DE_Localities extends DirEnumeration
567 {
568 protected $idfield = 'geoloc_localities.id';
569 protected $valfield = 'geoloc_localities.name';
570 protected $from = 'geoloc_localities';
571
572 protected $ac_join = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
573 protected $ac_unique = 'profile_addresses.pid';
574 }
575 // }}}
576
577 /** JOBS
578 */
579 // {{{ class DE_Companies
580 class DE_Companies extends DirEnumeration
581 {
582 protected $idfield = 'profile_job_enum.id';
583 protected $valfield = 'profile_job_enum.name';
584 protected $valfield2 = 'profile_job_enum.acronym';
585 protected $from = 'profile_job_enum';
586
587 protected $ac_join = 'INNER JOIN profile_job ON (profile_job.jobid = profile_job_enum.id)';
588 protected $ac_unique = 'profile_job.pid';
589 }
590 // }}}
591
592 // {{{ class DE_Sectors
593 class DE_Sectors extends DirEnumeration
594 {
595 protected $idfield = 'profile_job_sector_enum.id';
596 protected $valfield = 'profile_job_sector_enum.name';
597 protected $from = 'profile_job_sector_enum';
598
599 protected $ac_join = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
600 protected $ac_unique = 'profile_job.pid';
601 }
602 // }}}
603
604 // {{{ class DE_JobDescription
605 class DE_JobDescription extends DirEnumeration
606 {
607 protected $valfield = 'profile_job.description';
608 protected $from = 'profile_job';
609 protected $idfield = 'profile_job.pid';
610
611 protected $ac_unique = 'profile_job.pid';
612 }
613 // }}}
614
615 /** NETWORKING
616 */
617 // {{{ class DE_Networking
618 class DE_Networking extends DirEnumeration
619 {
620 protected $idfield = 'profile_networking_enum.nwid';
621 protected $valfield = 'profile_networking_enum.name';
622 protected $from = 'profile_networking_enum';
623
624
625 protected $ac_join = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
626 protected $ac_unique = 'profile_networking.pid';
627 }
628 // }}}
629
630 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
631 ?>