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), | |
09824164 | 32 | ); |
33 | } | |
34 | ||
ba2afb88 | 35 | function handler_redir_advanced(&$page, $mode = null) |
36 | { | |
37 | pl_redirect('search/adv'); | |
38 | exit; | |
39 | } | |
40 | ||
09824164 | 41 | function form_prepare() |
42 | { | |
d7610c35 | 43 | Platal::page()->assign('formulaire',1); |
3c640222 | 44 | } |
45 | ||
8eb6931f RB |
46 | /** |
47 | * $model: The way of presenting the results: minifiche, trombi, geoloc. | |
48 | * $byletter: Show only names beginning with this letter | |
49 | */ | |
50 | function handler_quick(&$page, $model = null, $byletter = null) | |
09824164 | 51 | { |
52 | global $globals; | |
53 | ||
8eb6931f | 54 | if (Env::has('quick') || $model == 'geoloc') { |
dd23c027 | 55 | $quick = Env::t('quick'); |
85bb670b | 56 | if (S::logged() && !Env::has('page')) { |
732e5855 | 57 | S::logger()->log('search', 'quick=' . $quick); |
e31c1c3e | 58 | } |
dd23c027 SJ |
59 | |
60 | if ($quick == '') { | |
61 | $page->trigWarning('Aucun critère de recherche n\'est spécifié.'); | |
62 | $page->changeTpl('search/index.tpl'); | |
63 | $page->setTitle('Annuaire'); | |
64 | $page->assign('formulaire', 1); | |
65 | $page->addJsLink('ajax.js'); | |
66 | return; | |
67 | } | |
68 | ||
383bcdec | 69 | $list = 'profile|prf|fiche|fic|referent|ref|mentor'; |
dd70cd28 | 70 | if (S::admin()) { |
383bcdec | 71 | $list .= '|admin|adm|ax'; |
72 | } | |
11647113 | 73 | if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.\d{2,4})?)?)$/', replace_accent($quick), $matches)) { |
4514bea3 | 74 | $login = $matches[2]; |
383bcdec | 75 | switch($matches[1]) { |
76 | case 'admin': case 'adm': | |
77 | $base = 'admin/user/'; | |
78 | break; | |
79 | case 'ax': | |
80 | $base = 'profile/ax/'; | |
81 | break; | |
82 | case 'profile': case 'prf': case 'fiche': case 'fic': | |
83 | $base = 'profile/'; | |
84 | break; | |
85 | case 'referent': case 'ref': case 'mentor': | |
86 | $base = 'referent/'; | |
87 | break; | |
88 | } | |
7511200d | 89 | |
4514bea3 VZ |
90 | $user = User::getSilent($login); |
91 | if ($user) { | |
92 | pl_redirect($base . $user->login()); | |
7511200d | 93 | } |
90c614cd VZ |
94 | $_REQUEST['quick'] = $login; |
95 | $_GET['quick'] = $login; | |
8fee4fbd | 96 | } elseif (strpos($quick, 'doc:') === 0) { |
97 | $url = 'Docs/Recherche?'; | |
98 | $url .= 'action=search&q=' . urlencode(substr($quick, 4)); | |
7511200d | 99 | $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review'); |
8fee4fbd | 100 | pl_redirect($url); |
85b45717 FB |
101 | } elseif (strpos($quick, 'trombi:') === 0) { |
102 | $promo = substr($quick, 7); | |
103 | $res = XDB::query("SELECT diminutif | |
104 | FROM groups | |
105 | WHERE cat = 'Promotions' AND diminutif = {?}", | |
106 | $promo); | |
107 | if ($res->numRows() == 0) { | |
108 | $page->trigWarning("La promotion demandée n'est pas valide: $promo"); | |
109 | } else { | |
110 | http_redirect('http://www.polytechnique.net/login/' . $promo . '/annuaire/trombi'); | |
111 | } | |
383bcdec | 112 | } |
8fee4fbd | 113 | |
09824164 | 114 | $page->assign('formulaire', 0); |
115 | ||
8c4a0c30 | 116 | require_once 'userset.inc.php'; |
8eb6931f RB |
117 | $view = new SearchSet(true); |
118 | $view->addMod('minifiche', 'Mini-fiches', true, array('with_score' => true, 'starts_with' => $byletter)); | |
35fa92e8 | 119 | if (S::logged() && !Env::i('nonins')) { |
120 | $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true, 'with_score' => true)); | |
1fe46b8f SJ |
121 | // TODO: Reactivate when the new map is completed. |
122 | // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv')); | |
35fa92e8 | 123 | } |
8eb6931f | 124 | $view->apply('search', $page, $model); |
09824164 | 125 | |
8c4a0c30 | 126 | $nb_tot = $view->count(); |
e35882be | 127 | $page->assign('search_results_nb', $nb_tot); |
cab08090 | 128 | if (!S::logged() && $nb_tot > $globals->search->public_max) { |
825a13a4 | 129 | $page->trigError('Votre recherche a généré trop de résultats pour un affichage public.'); |
09824164 | 130 | } elseif ($nb_tot > $globals->search->private_max) { |
825a13a4 | 131 | $page->trigError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.'); |
09824164 | 132 | } elseif (empty($nb_tot)) { |
825a13a4 | 133 | $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !'); |
09824164 | 134 | } |
135 | } else { | |
136 | $page->assign('formulaire',1); | |
e654517d | 137 | $page->addJsLink('ajax.js'); |
09824164 | 138 | } |
139 | ||
eaf30d86 | 140 | $page->changeTpl('search/index.tpl'); |
46f272fe | 141 | $page->setTitle('Annuaire'); |
09824164 | 142 | } |
143 | ||
8eb6931f RB |
144 | /** $model is the way of presenting the results: minifiche, trombi, geoloc. |
145 | */ | |
146 | function handler_advanced(&$page, $model = null, $byletter = null) | |
09824164 | 147 | { |
148 | global $globals; | |
c7c2a181 | 149 | require_once 'geocoding.inc.php'; |
bc67c37c | 150 | $page->assign('advanced',1); |
4b4b4b67 | 151 | $page->addJsLink('jquery.autocomplete.js'); |
bc67c37c | 152 | |
8eb6931f | 153 | if (!Env::has('rechercher') && $model != 'geoloc') { |
09824164 | 154 | $this->form_prepare(); |
155 | } else { | |
e31c1c3e | 156 | if (!Env::has('page')) { |
732e5855 | 157 | S::logger()->log('search', 'adv=' . var_export($_GET, true)); |
e31c1c3e | 158 | } |
137e819f | 159 | |
8c4a0c30 | 160 | require_once 'userset.inc.php'; |
8eb6931f RB |
161 | $view = new SearchSet(false); |
162 | $view->addMod('minifiche', 'Mini-fiches', true, array('starts_with' => $byletter)); | |
35fa92e8 | 163 | $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true)); |
1fe46b8f SJ |
164 | // TODO: Reactivate when the new map is completed. |
165 | // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv')); | |
8eb6931f | 166 | $view->apply('search/adv', $page, $model); |
eaf30d86 | 167 | |
8c4a0c30 | 168 | $nb_tot = $view->count(); |
09824164 | 169 | if ($nb_tot > $globals->search->private_max) { |
170 | $this->form_prepare(); | |
825a13a4 | 171 | $page->trigError('Recherche trop générale.'); |
6a48a49a SJ |
172 | } else if ($nb_tot == 0) { |
173 | $this->form_prepare(); | |
174 | $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !'); | |
09824164 | 175 | } |
09824164 | 176 | } |
177 | ||
8eb6931f | 178 | $page->changeTpl('search/index.tpl', $model == 'mini' ? SIMPLE : SKINNED); |
3c640222 | 179 | $page->addJsLink('ajax.js'); |
8c4a0c30 | 180 | $page->assign('public_directory',0); |
09824164 | 181 | } |
3c640222 | 182 | |
838cc16a | 183 | function handler_autocomplete(&$page, $type = null) |
184 | { | |
185 | // Autocompletion : according to type required, return | |
186 | // a list of results matching with the number of matches. | |
187 | // The output format is : | |
188 | // result1|nb1 | |
189 | // result2|nb2 | |
190 | // ... | |
3cb500d5 | 191 | pl_content_headers("text/plain"); |
ff3eb9b7 | 192 | $q = preg_replace(array('/\*+$/', // always look for $q* |
193 | '/([\^\$\[\]])/', // escape special regexp char | |
194 | '/\*/'), // replace joker by regexp joker | |
195 | array('', | |
196 | '\\\\\1', | |
197 | '.*'), | |
78e4b1f6 | 198 | Env::s('q')); |
838cc16a | 199 | if (!$q) exit(); |
c15afc4e | 200 | |
eaf30d86 | 201 | // try to look in cached results |
78e4b1f6 FB |
202 | $cache = XDB::query('SELECT result |
203 | FROM search_autocomplete | |
204 | WHERE name = {?} AND | |
205 | query = {?} AND | |
206 | generated > NOW() - INTERVAL 1 DAY', | |
ff3eb9b7 | 207 | $type, $q); |
c15afc4e | 208 | if ($res = $cache->fetchOneCell()) { |
ff3eb9b7 | 209 | echo $res; |
210 | die(); | |
c15afc4e | 211 | } |
eaf30d86 | 212 | |
8e720253 RB |
213 | $enums = array( |
214 | 'binetTxt' => DirEnum::BINETS, | |
215 | 'groupexTxt' => DirEnum::GROUPESX, | |
216 | 'sectionTxt' => DirEnum::SECTIONS, | |
217 | 'networking_typeTxt' => DirEnum::NETWORKS, | |
218 | 'city' => DirEnum::LOCALITIES, | |
219 | 'countryTxt' => DirEnum::COUNTRIES, | |
220 | 'entreprise' => DirEnum::COMPANIES, | |
221 | 'secteurTxt' => DirEnum::SECTORS, | |
222 | 'description' => DirEnum::JOBDESCRIPTION, | |
223 | 'nationaliteTxt' => DirEnum::NATIONALITIES, | |
fb3b6547 | 224 | 'schoolTxt' => DirEnum::EDUSCHOOLS, |
8e720253 | 225 | ); |
9d44d2c7 | 226 | if (!array_key_exists($type, $enums)) { |
8e720253 | 227 | exit(); |
838cc16a | 228 | } |
229 | ||
8e720253 | 230 | $enum = $enums[$type]; |
f781871c | 231 | |
8e720253 | 232 | $list = DirEnum::getAutoComplete($enum, $q); |
838cc16a | 233 | $nbResults = 0; |
c15afc4e | 234 | $res = ""; |
2ab7a09f | 235 | while ($result = $list->next()) { |
838cc16a | 236 | $nbResults++; |
237 | if ($nbResults == 11) { | |
b3ec63d5 | 238 | $res .= $q."|-1\n"; |
838cc16a | 239 | } else { |
f6818108 | 240 | $res .= $result['field'].'|'; |
c7139c07 SJ |
241 | if (isset($result['nb'])) { |
242 | $res .= $result['nb']; | |
243 | } | |
ff3eb9b7 | 244 | if (isset($result['id'])) { |
245 | $res .= '|'.$result['id']; | |
246 | } | |
247 | $res .= "\n"; | |
838cc16a | 248 | } |
249 | } | |
78e4b1f6 | 250 | XDB::query('REPLACE INTO search_autocomplete |
ff3eb9b7 | 251 | VALUES ({?}, {?}, {?}, NOW())', |
252 | $type, $q, $res); | |
c15afc4e | 253 | echo $res; |
838cc16a | 254 | exit(); |
255 | } | |
eaf30d86 | 256 | |
2ab7a09f | 257 | function handler_list(&$page, $type = null, $idVal = null) |
258 | { | |
ffd70398 PC |
259 | $page->assign('name', $type); |
260 | $page->assign('with_text_value', true); | |
261 | $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text"); | |
262 | ||
ff3eb9b7 | 263 | // Give the list of all values possible of type and builds a select input for it |
8e720253 | 264 | $ids = null; |
ff3eb9b7 | 265 | |
266 | switch ($type) { | |
8e720253 | 267 | case 'binet': |
2998edf1 | 268 | $ids = DirEnum::getOptionsIter(DirEnum::BINETS); |
ff3eb9b7 | 269 | break; |
92c3f9e5 | 270 | case 'networking_type': |
2998edf1 | 271 | $ids = DirEnum::getOptionsIter(DirEnum::NETWORKS); |
92c3f9e5 | 272 | break; |
ff3eb9b7 | 273 | case 'country': |
2998edf1 | 274 | $ids = DirEnum::getOptionsIter(DirEnum::COUNTRIES); |
ff3eb9b7 | 275 | $page->assign('onchange', 'changeCountry(this.value)'); |
276 | break; | |
ff3eb9b7 | 277 | case 'diploma': |
8e720253 | 278 | if (Env::has('school') && Env::i('school') != 0) { |
2998edf1 | 279 | $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES, Env::i('school')); |
8e720253 | 280 | } else { |
2998edf1 | 281 | $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES); |
8e720253 RB |
282 | } |
283 | break; | |
ff3eb9b7 | 284 | case 'groupex': |
2998edf1 | 285 | $ids = DirEnum::getOptionsIter(DirEnum::GROUPESX); |
ff3eb9b7 | 286 | break; |
287 | case 'nationalite': | |
2998edf1 | 288 | $ids = DirEnum::getOptionsIter(DirEnum::NATIONALITIES); |
ff3eb9b7 | 289 | break; |
ffd70398 PC |
290 | case 'region': |
291 | if (Env::has('country')) { | |
292 | $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS, Env::v('country')); | |
8e720253 | 293 | } else { |
2998edf1 | 294 | $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS); |
f6818108 | 295 | } |
ff3eb9b7 | 296 | break; |
297 | case 'school': | |
2998edf1 | 298 | $ids = DirEnum::getOptionsIter(DirEnum::EDUSCHOOLS); |
ff3eb9b7 | 299 | $page->assign('onchange', 'changeSchool(this.value)'); |
300 | break; | |
301 | case 'section': | |
2998edf1 | 302 | $ids = DirEnum::getOptionsIter(DirEnum::SECTIONS); |
ff3eb9b7 | 303 | break; |
304 | case 'secteur': | |
2998edf1 | 305 | $ids = DirEnum::getOptionsIter(DirEnum::SECTORS); |
ff3eb9b7 | 306 | break; |
307 | default: exit(); | |
308 | } | |
309 | if (isset($idVal)) { | |
3cb500d5 | 310 | pl_content_headers("text/plain"); |
8e720253 | 311 | echo $ids[$idVal]; |
ff3eb9b7 | 312 | exit(); |
313 | } | |
3cb500d5 | 314 | pl_content_headers("text/xml"); |
05cb05c0 | 315 | $page->changeTpl('include/field.select.tpl', NO_SKIN); |
8e720253 | 316 | $page->assign('list', $ids); |
2ab7a09f | 317 | } |
09824164 | 318 | } |
319 | ||
a7de4ef7 | 320 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
09824164 | 321 | ?> |