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