Adds iterators on both group admins and members, renames filtering functions.
[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
78e4b1f6 50 $res = XDB::query("SELECT MIN(diminutif), MAX(diminutif)
eb41eda9 51 FROM groups
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) {
825a13a4 114 $page->trigError('Votre recherche a généré trop de résultats pour un affichage public.');
09824164 115 } elseif ($nb_tot > $globals->search->private_max) {
825a13a4 116 $page->trigError('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)) {
825a13a4 118 $page->trigError('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
eaf30d86 125 $page->changeTpl('search/index.tpl');
46f272fe 126 $page->setTitle('Annuaire');
09824164 127 }
128
90ccb062 129 function handler_advanced(&$page, $action = null, $subaction = null)
09824164 130 {
131 global $globals;
c7c2a181 132 require_once 'geocoding.inc.php';
bc67c37c 133 $page->assign('advanced',1);
4b4b4b67 134 $page->addJsLink('jquery.autocomplete.js');
bc67c37c 135
90ccb062 136 if (!Env::has('rechercher') && $action != 'geoloc') {
09824164 137 $this->form_prepare();
138 } else {
e31c1c3e 139 if (!Env::has('page')) {
732e5855 140 S::logger()->log('search', 'adv=' . var_export($_GET, true));
e31c1c3e 141 }
137e819f 142
8c4a0c30 143 require_once 'userset.inc.php';
144 $view = new SearchSet(false, $action == 'geoloc' && substr($subaction, -3) == 'swf');
1fae7605 145 $view->addMod('minifiche', 'Mini-fiches', true);
35fa92e8 146 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true));
1fe46b8f
SJ
147 // TODO: Reactivate when the new map is completed.
148 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
86b5c8f0 149 $view->apply('search/adv', $page, $action, $subaction);
eaf30d86 150
8c4a0c30 151 if ($subaction) {
152 return;
09824164 153 }
8c4a0c30 154 $nb_tot = $view->count();
09824164 155 if ($nb_tot > $globals->search->private_max) {
156 $this->form_prepare();
825a13a4 157 $page->trigError('Recherche trop générale.');
09824164 158 }
09824164 159 }
160
90ccb062 161 $page->changeTpl('search/index.tpl', $action == 'mini' ? SIMPLE : SKINNED);
3c640222 162 $page->addJsLink('ajax.js');
8c4a0c30 163 $page->assign('public_directory',0);
09824164 164 }
3c640222 165
838cc16a 166 function handler_autocomplete(&$page, $type = null)
167 {
168 // Autocompletion : according to type required, return
169 // a list of results matching with the number of matches.
170 // The output format is :
171 // result1|nb1
172 // result2|nb2
173 // ...
3cb500d5 174 pl_content_headers("text/plain");
ff3eb9b7 175 $q = preg_replace(array('/\*+$/', // always look for $q*
176 '/([\^\$\[\]])/', // escape special regexp char
177 '/\*/'), // replace joker by regexp joker
178 array('',
179 '\\\\\1',
180 '.*'),
78e4b1f6 181 Env::s('q'));
838cc16a 182 if (!$q) exit();
c15afc4e 183
eaf30d86 184 // try to look in cached results
78e4b1f6
FB
185 $cache = XDB::query('SELECT result
186 FROM search_autocomplete
187 WHERE name = {?} AND
188 query = {?} AND
189 generated > NOW() - INTERVAL 1 DAY',
ff3eb9b7 190 $type, $q);
c15afc4e 191 if ($res = $cache->fetchOneCell()) {
ff3eb9b7 192 echo $res;
193 die();
c15afc4e 194 }
eaf30d86 195
8e720253
RB
196 $enums = array(
197 'binetTxt' => DirEnum::BINETS,
198 'groupexTxt' => DirEnum::GROUPESX,
199 'sectionTxt' => DirEnum::SECTIONS,
200 'networking_typeTxt' => DirEnum::NETWORKS,
201 'city' => DirEnum::LOCALITIES,
202 'countryTxt' => DirEnum::COUNTRIES,
203 'entreprise' => DirEnum::COMPANIES,
204 'secteurTxt' => DirEnum::SECTORS,
205 'description' => DirEnum::JOBDESCRIPTION,
206 'nationaliteTxt' => DirEnum::NATIONALITIES,
fb3b6547 207 'schoolTxt' => DirEnum::EDUSCHOOLS,
8e720253 208 );
9d44d2c7 209 if (!array_key_exists($type, $enums)) {
8e720253 210 exit();
838cc16a 211 }
212
8e720253 213 $enum = $enums[$type];
f781871c 214
8e720253 215 $list = DirEnum::getAutoComplete($enum, $q);
838cc16a 216 $nbResults = 0;
c15afc4e 217 $res = "";
2ab7a09f 218 while ($result = $list->next()) {
838cc16a 219 $nbResults++;
220 if ($nbResults == 11) {
b3ec63d5 221 $res .= $q."|-1\n";
838cc16a 222 } else {
f6818108 223 $res .= $result['field'].'|';
c7139c07
SJ
224 if (isset($result['nb'])) {
225 $res .= $result['nb'];
226 }
ff3eb9b7 227 if (isset($result['id'])) {
228 $res .= '|'.$result['id'];
229 }
230 $res .= "\n";
838cc16a 231 }
232 }
78e4b1f6 233 XDB::query('REPLACE INTO search_autocomplete
ff3eb9b7 234 VALUES ({?}, {?}, {?}, NOW())',
235 $type, $q, $res);
c15afc4e 236 echo $res;
838cc16a 237 exit();
238 }
eaf30d86 239
2ab7a09f 240 function handler_list(&$page, $type = null, $idVal = null)
241 {
ff3eb9b7 242 // Give the list of all values possible of type and builds a select input for it
8e720253 243 $ids = null;
ff3eb9b7 244
245 switch ($type) {
8e720253 246 case 'binet':
2998edf1 247 $ids = DirEnum::getOptionsIter(DirEnum::BINETS);
ff3eb9b7 248 break;
92c3f9e5 249 case 'networking_type':
2998edf1 250 $ids = DirEnum::getOptionsIter(DirEnum::NETWORKS);
92c3f9e5 251 break;
ff3eb9b7 252 case 'country':
2998edf1 253 $ids = DirEnum::getOptionsIter(DirEnum::COUNTRIES);
ff3eb9b7 254 $page->assign('onchange', 'changeCountry(this.value)');
255 break;
ff3eb9b7 256 case 'diploma':
8e720253 257 if (Env::has('school') && Env::i('school') != 0) {
2998edf1 258 $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES, Env::i('school'));
8e720253 259 } else {
2998edf1 260 $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES);
8e720253
RB
261 }
262 break;
ff3eb9b7 263 case 'groupex':
2998edf1 264 $ids = DirEnum::getOptionsIter(DirEnum::GROUPESX);
ff3eb9b7 265 break;
266 case 'nationalite':
2998edf1 267 $ids = DirEnum::getOptionsIter(DirEnum::NATIONALITIES);
ff3eb9b7 268 break;
8e720253
RB
269 case 'region':
270 if ($isset($_REQUEST['country'])) {
2998edf1 271 $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS, $_REQUEST['country']);
8e720253 272 } else {
2998edf1 273 $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS);
f6818108 274 }
ff3eb9b7 275 break;
276 case 'school':
2998edf1 277 $ids = DirEnum::getOptionsIter(DirEnum::EDUSCHOOLS);
ff3eb9b7 278 $page->assign('onchange', 'changeSchool(this.value)');
279 break;
280 case 'section':
2998edf1 281 $ids = DirEnum::getOptionsIter(DirEnum::SECTIONS);
ff3eb9b7 282 break;
283 case 'secteur':
2998edf1 284 $ids = DirEnum::getOptionsIter(DirEnum::SECTORS);
ff3eb9b7 285 break;
286 default: exit();
287 }
288 if (isset($idVal)) {
3cb500d5 289 pl_content_headers("text/plain");
8e720253 290 echo $ids[$idVal];
ff3eb9b7 291 exit();
292 }
3cb500d5 293 pl_content_headers("text/xml");
05cb05c0 294 $page->changeTpl('include/field.select.tpl', NO_SKIN);
f6818108 295 $page->assign('name', $type);
8e720253 296 $page->assign('list', $ids);
b0691e10
FB
297 $page->assign('with_text_value', true);
298 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
2ab7a09f 299 }
09824164 300}
301
a7de4ef7 302// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
09824164 303?>