Commit | Line | Data |
---|---|---|
09824164 | 1 | <?php |
2 | /*************************************************************************** | |
9f5bd98e | 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
09824164 | 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 SearchModule extends PLModule | |
23 | { | |
24 | function handlers() | |
25 | { | |
26 | return array( | |
abed2826 | 27 | 'search' => $this->make_hook('quick', AUTH_PUBLIC), |
20f36c54 | 28 | 'search/adv' => $this->make_hook('advanced', AUTH_COOKIE, 'directory_ax'), |
ba2afb88 | 29 | 'advanced_search.php' => $this->make_hook('redir_advanced', AUTH_PUBLIC), |
20f36c54 FB |
30 | 'search/autocomplete' => $this->make_hook('autocomplete', AUTH_COOKIE, 'directory_ax', NO_AUTH), |
31 | 'search/list' => $this->make_hook('list', AUTH_COOKIE, 'directory_ax', NO_AUTH), | |
4ec03752 RB |
32 | 'jobs' => $this->make_hook('referent', AUTH_COOKIE), |
33 | 'emploi' => $this->make_hook('referent', AUTH_COOKIE), | |
34 | 'referent/search' => $this->make_hook('referent', AUTH_COOKIE), | |
459e6f81 | 35 | 'search/referent/countries' => $this->make_hook('referent_countries', AUTH_COOKIE), |
09824164 | 36 | ); |
37 | } | |
38 | ||
ba2afb88 | 39 | function handler_redir_advanced(&$page, $mode = null) |
40 | { | |
41 | pl_redirect('search/adv'); | |
42 | exit; | |
43 | } | |
44 | ||
09824164 | 45 | function form_prepare() |
46 | { | |
d7610c35 | 47 | Platal::page()->assign('formulaire',1); |
3c640222 | 48 | } |
49 | ||
8eb6931f RB |
50 | /** |
51 | * $model: The way of presenting the results: minifiche, trombi, geoloc. | |
52 | * $byletter: Show only names beginning with this letter | |
53 | */ | |
54 | function handler_quick(&$page, $model = null, $byletter = null) | |
09824164 | 55 | { |
56 | global $globals; | |
57 | ||
8eb6931f | 58 | if (Env::has('quick') || $model == 'geoloc') { |
dd23c027 | 59 | $quick = Env::t('quick'); |
85bb670b | 60 | if (S::logged() && !Env::has('page')) { |
732e5855 | 61 | S::logger()->log('search', 'quick=' . $quick); |
e31c1c3e | 62 | } |
dd23c027 SJ |
63 | |
64 | if ($quick == '') { | |
65 | $page->trigWarning('Aucun critère de recherche n\'est spécifié.'); | |
66 | $page->changeTpl('search/index.tpl'); | |
67 | $page->setTitle('Annuaire'); | |
68 | $page->assign('formulaire', 1); | |
dd23c027 SJ |
69 | return; |
70 | } | |
71 | ||
383bcdec | 72 | $list = 'profile|prf|fiche|fic|referent|ref|mentor'; |
dd70cd28 | 73 | if (S::admin()) { |
383bcdec | 74 | $list .= '|admin|adm|ax'; |
75 | } | |
251ac585 FB |
76 | $suffixes = array_keys(DirEnum::getOptions(DirEnum::ACCOUNTTYPES)); |
77 | $suffixes = implode('|', $suffixes); | |
78 | if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.(?:[md]?\d{2,4}|' . $suffixes . '))?)?)$/', replace_accent($quick), $matches)) { | |
4514bea3 | 79 | $login = $matches[2]; |
383bcdec | 80 | switch($matches[1]) { |
81 | case 'admin': case 'adm': | |
82 | $base = 'admin/user/'; | |
83 | break; | |
84 | case 'ax': | |
85 | $base = 'profile/ax/'; | |
86 | break; | |
87 | case 'profile': case 'prf': case 'fiche': case 'fic': | |
88 | $base = 'profile/'; | |
89 | break; | |
90 | case 'referent': case 'ref': case 'mentor': | |
91 | $base = 'referent/'; | |
92 | break; | |
93 | } | |
7511200d | 94 | |
4514bea3 VZ |
95 | $user = User::getSilent($login); |
96 | if ($user) { | |
97 | pl_redirect($base . $user->login()); | |
7511200d | 98 | } |
90c614cd VZ |
99 | $_REQUEST['quick'] = $login; |
100 | $_GET['quick'] = $login; | |
8fee4fbd | 101 | } elseif (strpos($quick, 'doc:') === 0) { |
102 | $url = 'Docs/Recherche?'; | |
103 | $url .= 'action=search&q=' . urlencode(substr($quick, 4)); | |
7511200d | 104 | $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review'); |
8fee4fbd | 105 | pl_redirect($url); |
85b45717 FB |
106 | } elseif (strpos($quick, 'trombi:') === 0) { |
107 | $promo = substr($quick, 7); | |
108 | $res = XDB::query("SELECT diminutif | |
109 | FROM groups | |
110 | WHERE cat = 'Promotions' AND diminutif = {?}", | |
111 | $promo); | |
112 | if ($res->numRows() == 0) { | |
113 | $page->trigWarning("La promotion demandée n'est pas valide: $promo"); | |
114 | } else { | |
115 | http_redirect('http://www.polytechnique.net/login/' . $promo . '/annuaire/trombi'); | |
116 | } | |
383bcdec | 117 | } |
8fee4fbd | 118 | |
09824164 | 119 | $page->assign('formulaire', 0); |
120 | ||
8c4a0c30 | 121 | require_once 'userset.inc.php'; |
78a47eb4 | 122 | $view = new QuickSearchSet(); |
8eb6931f | 123 | $view->addMod('minifiche', 'Mini-fiches', true, array('with_score' => true, 'starts_with' => $byletter)); |
35fa92e8 | 124 | if (S::logged() && !Env::i('nonins')) { |
125 | $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true, 'with_score' => true)); | |
1fe46b8f SJ |
126 | // TODO: Reactivate when the new map is completed. |
127 | // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv')); | |
35fa92e8 | 128 | } |
8eb6931f | 129 | $view->apply('search', $page, $model); |
09824164 | 130 | |
8c4a0c30 | 131 | $nb_tot = $view->count(); |
e35882be | 132 | $page->assign('search_results_nb', $nb_tot); |
cab08090 | 133 | if (!S::logged() && $nb_tot > $globals->search->public_max) { |
825a13a4 | 134 | $page->trigError('Votre recherche a généré trop de résultats pour un affichage public.'); |
09824164 | 135 | } elseif ($nb_tot > $globals->search->private_max) { |
825a13a4 | 136 | $page->trigError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.'); |
09824164 | 137 | } elseif (empty($nb_tot)) { |
825a13a4 | 138 | $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !'); |
09824164 | 139 | } |
140 | } else { | |
141 | $page->assign('formulaire',1); | |
142 | } | |
143 | ||
eaf30d86 | 144 | $page->changeTpl('search/index.tpl'); |
46f272fe | 145 | $page->setTitle('Annuaire'); |
09824164 | 146 | } |
147 | ||
8eb6931f RB |
148 | /** $model is the way of presenting the results: minifiche, trombi, geoloc. |
149 | */ | |
150 | function handler_advanced(&$page, $model = null, $byletter = null) | |
09824164 | 151 | { |
152 | global $globals; | |
bc67c37c | 153 | $page->assign('advanced',1); |
4b4b4b67 | 154 | $page->addJsLink('jquery.autocomplete.js'); |
bc67c37c | 155 | |
1d364832 RB |
156 | $networks = DirEnum::getOptions(DirEnum::NETWORKS); |
157 | $networks[-1] = 'Tous types'; | |
158 | $networks[0] = '-'; | |
159 | ksort($networks); | |
160 | $page->assign('networking_types', $networks); | |
161 | ||
8eb6931f | 162 | if (!Env::has('rechercher') && $model != 'geoloc') { |
09824164 | 163 | $this->form_prepare(); |
164 | } else { | |
e31c1c3e | 165 | if (!Env::has('page')) { |
732e5855 | 166 | S::logger()->log('search', 'adv=' . var_export($_GET, true)); |
e31c1c3e | 167 | } |
137e819f | 168 | |
8c4a0c30 | 169 | require_once 'userset.inc.php'; |
0f567f55 RB |
170 | // Enable X.org fields for X.org admins, and AX fields for AX secretaries. |
171 | $view = new AdvancedSearchSet(S::admin(), | |
172 | S::user()->checkPerms(User::PERM_EDIT_DIRECTORY)); | |
173 | ||
51fcda84 | 174 | if (!$view->isValid()) { |
09824164 | 175 | $this->form_prepare(); |
51fcda84 RB |
176 | $page->trigError('Recherche invalide.'); |
177 | } else { | |
178 | $view->addMod('minifiche', 'Mini-fiches', true, array('starts_with' => $byletter)); | |
179 | $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true)); | |
180 | // TODO: Reactivate when the new map is completed. | |
181 | // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv')); | |
39394cbf | 182 | if (S::user()->checkPerms(User::PERM_EDIT_DIRECTORY) || S::admin()) { |
c442573a | 183 | $view->addMod('addresses', 'Adresses postales', false); |
39394cbf | 184 | } |
51fcda84 RB |
185 | $view->apply('search/adv', $page, $model); |
186 | ||
187 | $nb_tot = $view->count(); | |
188 | if ($nb_tot > $globals->search->private_max) { | |
189 | $this->form_prepare(); | |
39394cbf SJ |
190 | if ($model != 'addresses' && (S::user()->checkPerms(User::PERM_EDIT_DIRECTORY) || S::admin())) { |
191 | $page->assign('suggestAddresses', true); | |
192 | } | |
51fcda84 RB |
193 | $page->trigError('Recherche trop générale.'); |
194 | } else if ($nb_tot == 0) { | |
195 | $this->form_prepare(); | |
196 | $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !'); | |
197 | } | |
09824164 | 198 | } |
09824164 | 199 | } |
200 | ||
8eb6931f | 201 | $page->changeTpl('search/index.tpl', $model == 'mini' ? SIMPLE : SKINNED); |
8c4a0c30 | 202 | $page->assign('public_directory',0); |
09824164 | 203 | } |
3c640222 | 204 | |
838cc16a | 205 | function handler_autocomplete(&$page, $type = null) |
206 | { | |
207 | // Autocompletion : according to type required, return | |
208 | // a list of results matching with the number of matches. | |
209 | // The output format is : | |
210 | // result1|nb1 | |
211 | // result2|nb2 | |
212 | // ... | |
3cb500d5 | 213 | pl_content_headers("text/plain"); |
ff3eb9b7 | 214 | $q = preg_replace(array('/\*+$/', // always look for $q* |
215 | '/([\^\$\[\]])/', // escape special regexp char | |
216 | '/\*/'), // replace joker by regexp joker | |
217 | array('', | |
218 | '\\\\\1', | |
219 | '.*'), | |
78e4b1f6 | 220 | Env::s('q')); |
838cc16a | 221 | if (!$q) exit(); |
c15afc4e | 222 | |
eaf30d86 | 223 | // try to look in cached results |
78e4b1f6 FB |
224 | $cache = XDB::query('SELECT result |
225 | FROM search_autocomplete | |
226 | WHERE name = {?} AND | |
227 | query = {?} AND | |
228 | generated > NOW() - INTERVAL 1 DAY', | |
ff3eb9b7 | 229 | $type, $q); |
c15afc4e | 230 | if ($res = $cache->fetchOneCell()) { |
ff3eb9b7 | 231 | echo $res; |
232 | die(); | |
c15afc4e | 233 | } |
eaf30d86 | 234 | |
8e720253 RB |
235 | $enums = array( |
236 | 'binetTxt' => DirEnum::BINETS, | |
237 | 'groupexTxt' => DirEnum::GROUPESX, | |
238 | 'sectionTxt' => DirEnum::SECTIONS, | |
239 | 'networking_typeTxt' => DirEnum::NETWORKS, | |
240 | 'city' => DirEnum::LOCALITIES, | |
241 | 'countryTxt' => DirEnum::COUNTRIES, | |
242 | 'entreprise' => DirEnum::COMPANIES, | |
3ac45f10 | 243 | 'jobtermTxt' => DirEnum::JOBTERMS, |
8e720253 RB |
244 | 'description' => DirEnum::JOBDESCRIPTION, |
245 | 'nationaliteTxt' => DirEnum::NATIONALITIES, | |
fb3b6547 | 246 | 'schoolTxt' => DirEnum::EDUSCHOOLS, |
8e720253 | 247 | ); |
9d44d2c7 | 248 | if (!array_key_exists($type, $enums)) { |
8e720253 | 249 | exit(); |
838cc16a | 250 | } |
251 | ||
8e720253 | 252 | $enum = $enums[$type]; |
f781871c | 253 | |
8e720253 | 254 | $list = DirEnum::getAutoComplete($enum, $q); |
838cc16a | 255 | $nbResults = 0; |
c15afc4e | 256 | $res = ""; |
2ab7a09f | 257 | while ($result = $list->next()) { |
838cc16a | 258 | $nbResults++; |
259 | if ($nbResults == 11) { | |
b3ec63d5 | 260 | $res .= $q."|-1\n"; |
838cc16a | 261 | } else { |
f6818108 | 262 | $res .= $result['field'].'|'; |
c7139c07 SJ |
263 | if (isset($result['nb'])) { |
264 | $res .= $result['nb']; | |
265 | } | |
ff3eb9b7 | 266 | if (isset($result['id'])) { |
267 | $res .= '|'.$result['id']; | |
268 | } | |
269 | $res .= "\n"; | |
838cc16a | 270 | } |
271 | } | |
fb6dc875 PC |
272 | if ($nbResults == 0) { |
273 | $res = $q."|-2\n"; | |
274 | } | |
00ba8a74 SJ |
275 | XDB::query('INSERT INTO search_autocomplete (name, query, result, generated) |
276 | VALUES ({?}, {?}, {?}, NOW()) | |
a245a3e1 | 277 | ON DUPLICATE KEY UPDATE result = VALUES(result), generated = VALUES(generated)', |
00ba8a74 | 278 | $type, $q, $res); |
c15afc4e | 279 | echo $res; |
838cc16a | 280 | exit(); |
281 | } | |
eaf30d86 | 282 | |
2ab7a09f | 283 | function handler_list(&$page, $type = null, $idVal = null) |
284 | { | |
ffd70398 PC |
285 | $page->assign('name', $type); |
286 | $page->assign('with_text_value', true); | |
287 | $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text"); | |
288 | ||
ff3eb9b7 | 289 | // Give the list of all values possible of type and builds a select input for it |
8e720253 | 290 | $ids = null; |
ff3eb9b7 | 291 | |
292 | switch ($type) { | |
8e720253 | 293 | case 'binet': |
2998edf1 | 294 | $ids = DirEnum::getOptionsIter(DirEnum::BINETS); |
ff3eb9b7 | 295 | break; |
92c3f9e5 | 296 | case 'networking_type': |
2998edf1 | 297 | $ids = DirEnum::getOptionsIter(DirEnum::NETWORKS); |
92c3f9e5 | 298 | break; |
ff3eb9b7 | 299 | case 'country': |
2998edf1 | 300 | $ids = DirEnum::getOptionsIter(DirEnum::COUNTRIES); |
ff3eb9b7 | 301 | $page->assign('onchange', 'changeCountry(this.value)'); |
302 | break; | |
ff3eb9b7 | 303 | case 'diploma': |
8e720253 | 304 | if (Env::has('school') && Env::i('school') != 0) { |
2998edf1 | 305 | $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES, Env::i('school')); |
8e720253 | 306 | } else { |
2998edf1 | 307 | $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES); |
8e720253 RB |
308 | } |
309 | break; | |
ff3eb9b7 | 310 | case 'groupex': |
2998edf1 | 311 | $ids = DirEnum::getOptionsIter(DirEnum::GROUPESX); |
ff3eb9b7 | 312 | break; |
313 | case 'nationalite': | |
2998edf1 | 314 | $ids = DirEnum::getOptionsIter(DirEnum::NATIONALITIES); |
ff3eb9b7 | 315 | break; |
e429cd03 | 316 | case 'administrativearea': |
ffd70398 PC |
317 | if (Env::has('country')) { |
318 | $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS, Env::v('country')); | |
8e720253 | 319 | } else { |
2998edf1 | 320 | $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS); |
f6818108 | 321 | } |
e429cd03 SJ |
322 | $page->assign('onchange', 'changeAdministrativeArea(this.value)'); |
323 | break; | |
324 | case 'subadministrativearea': | |
325 | if (Env::has('administrativearea')) { | |
326 | $ids = DirEnum::getOptionsIter(DirEnum::SUBADMINAREAS, Env::v('administrativearea')); | |
327 | } else { | |
328 | $ids = DirEnum::getOptionsIter(DirEnum::SUBADMINAREAS); | |
329 | } | |
ff3eb9b7 | 330 | break; |
331 | case 'school': | |
2998edf1 | 332 | $ids = DirEnum::getOptionsIter(DirEnum::EDUSCHOOLS); |
ff3eb9b7 | 333 | $page->assign('onchange', 'changeSchool(this.value)'); |
334 | break; | |
335 | case 'section': | |
2998edf1 | 336 | $ids = DirEnum::getOptionsIter(DirEnum::SECTIONS); |
ff3eb9b7 | 337 | break; |
3ac45f10 PC |
338 | case 'jobterm': |
339 | if (Env::has('jtid')) { | |
340 | JobTerms::ajaxGetBranch(&$page, JobTerms::ONLY_JOBS); | |
341 | return; | |
342 | } else { | |
343 | pl_content_headers('text/xml'); | |
344 | echo '<div>'; // global container so that response is valid xml | |
345 | echo '<input name="jobtermTxt" type="text" style="display:none" size="32" />'; | |
346 | echo '<input name="jobterm" type="hidden"/>'; | |
347 | echo '<div class="term_tree"></div>'; // container where to create the tree | |
348 | echo '<script type="text/javascript" src="javascript/jquery.jstree.js"></script>'; | |
349 | echo '<script type="text/javascript" src="javascript/jobtermstree.js"></script>'; | |
350 | echo '<script type="text/javascript">createJobTermsTree(".term_tree", "search/list/jobterm", "search", "searchForJobTerm");</script>'; | |
351 | echo '</div>'; | |
352 | exit(); | |
353 | } | |
ff3eb9b7 | 354 | default: exit(); |
355 | } | |
356 | if (isset($idVal)) { | |
3cb500d5 | 357 | pl_content_headers("text/plain"); |
8e720253 | 358 | echo $ids[$idVal]; |
ff3eb9b7 | 359 | exit(); |
360 | } | |
3cb500d5 | 361 | pl_content_headers("text/xml"); |
05cb05c0 | 362 | $page->changeTpl('include/field.select.tpl', NO_SKIN); |
8e720253 | 363 | $page->assign('list', $ids); |
2ab7a09f | 364 | } |
9dc65790 PC |
365 | |
366 | function handler_referent(&$page, $action = null, $subaction = null) | |
367 | { | |
459e6f81 PC |
368 | global $globals; |
369 | ||
9dc65790 PC |
370 | $wp = new PlWikiPage('Docs.Emploi'); |
371 | $wp->buildCache(); | |
372 | ||
373 | $page->setTitle('Emploi et Carrières'); | |
374 | ||
4ec03752 | 375 | // Count mentors |
9dc65790 PC |
376 | $res = XDB::query("SELECT count(distinct pid) FROM profile_mentor_term"); |
377 | $page->assign('mentors_number', $res->fetchOneCell()); | |
378 | ||
379 | $page->addJsLink('jquery.autocomplete.js'); | |
380 | ||
4ec03752 | 381 | // Search for mentors matching filters |
9dc65790 PC |
382 | require_once 'ufbuilder.inc.php'; |
383 | $ufb = new UFB_MentorSearch(); | |
384 | if (!$ufb->isEmpty()) { | |
385 | require_once 'userset.inc.php'; | |
386 | $ufc = $ufb->getUFC(); | |
387 | $set = new ProfileSet($ufc); | |
388 | $set->addMod('mentor', 'Référents'); | |
389 | $set->apply('referent/search', $page, $action, $subaction); | |
459e6f81 PC |
390 | $nb_tot = $set->count(); |
391 | if ($nb_tot > $globals->search->private_max) { | |
392 | $this->form_prepare(); | |
393 | $page->trigError('Recherche trop générale.'); | |
394 | $page->assign('plset_count', 0); | |
395 | } else if ($nb_tot == 0) { | |
396 | $this->form_prepare(); | |
397 | $page->trigError('Il n\'existe personne correspondant à ces critères dans la base.'); | |
9dc65790 PC |
398 | } |
399 | } | |
400 | ||
401 | $page->changeTpl('search/referent.tpl'); | |
402 | } | |
403 | ||
459e6f81 PC |
404 | /** |
405 | * Builds a select field to choose among countries that referents | |
406 | * know about. Only referents linked to term (jtid) are displayed. | |
407 | * @param $jtid id of job term to restrict referents | |
408 | */ | |
409 | function handler_referent_countries(&$page, $jtid = null) | |
410 | { | |
411 | pl_content_headers("text/xml"); | |
412 | $page->changeTpl('include/field.select.tpl', NO_SKIN); | |
413 | $page->assign('name', 'country'); | |
1c305d4c | 414 | $it = XDB::iterator("SELECT gc.iso_3166_1_a2 AS id, gc.country AS field |
459e6f81 PC |
415 | FROM geoloc_countries AS gc |
416 | INNER JOIN profile_mentor_country AS mp ON (mp.country = gc.iso_3166_1_a2) | |
417 | INNER JOIN profile_mentor_term AS mt ON (mt.pid = mp.pid) | |
418 | INNER JOIN profile_job_term_relation AS jtr ON (jtr.jtid_2 = mt.jtid) | |
419 | WHERE jtr.jtid_1 = {?} | |
420 | GROUP BY iso_3166_1_a2 | |
1c305d4c | 421 | ORDER BY country", $jtid); |
459e6f81 PC |
422 | $page->assign('list', $it); |
423 | } | |
09824164 | 424 | } |
425 | ||
a7de4ef7 | 426 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
09824164 | 427 | ?> |