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