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