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