Fixes region selection when choosing country from list. Fixes also for schools. Close...
[platal.git] / modules / search.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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(
27 'search' => $this->make_hook('quick', AUTH_PUBLIC),
28 'search/adv' => $this->make_hook('advanced', AUTH_COOKIE),
29 'advanced_search.php' => $this->make_hook('redir_advanced', AUTH_PUBLIC),
30 'search/autocomplete' => $this->make_hook('autocomplete', AUTH_COOKIE, 'user', NO_AUTH),
31 'search/list' => $this->make_hook('list', AUTH_COOKIE, 'user', NO_AUTH),
32 );
33 }
34
35 function handler_redir_advanced(&$page, $mode = null)
36 {
37 pl_redirect('search/adv');
38 exit;
39 }
40
41 function form_prepare()
42 {
43 Platal::page()->assign('formulaire',1);
44 }
45
46 function handler_quick(&$page, $action = null, $subaction = null)
47 {
48 global $globals;
49
50 if (Env::has('quick') || $action == 'geoloc') {
51 $quick = trim(Env::t('quick'));
52 if (S::logged() && !Env::has('page')) {
53 S::logger()->log('search', 'quick=' . $quick);
54 }
55 $list = 'profile|prf|fiche|fic|referent|ref|mentor';
56 if (S::admin()) {
57 $list .= '|admin|adm|ax';
58 }
59 if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.\d{2,4})?)?)$/', replace_accent($quick), $matches)) {
60 $login = $matches[2];
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 }
75
76 $user = User::getSilent($login);
77 if ($user) {
78 pl_redirect($base . $user->login());
79 }
80 $_REQUEST['quick'] = $login;
81 $_GET['quick'] = $login;
82 } elseif (strpos($quick, 'doc:') === 0) {
83 $url = 'Docs/Recherche?';
84 $url .= 'action=search&q=' . urlencode(substr($quick, 4));
85 $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review');
86 pl_redirect($url);
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 }
98 }
99
100 $page->assign('formulaire', 0);
101
102 require_once 'userset.inc.php';
103 $view = new SearchSet(true, $action == 'geoloc' && substr($subaction, -3) == 'swf');
104 $view->addMod('minifiche', 'Mini-fiches', true, array('with_score' => true));
105 if (S::logged() && !Env::i('nonins')) {
106 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true, 'with_score' => true));
107 // TODO: Reactivate when the new map is completed.
108 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
109 }
110 $view->apply('search', $page, $action, $subaction);
111
112 $nb_tot = $view->count();
113 $page->assign('search_results_nb', $nb_tot);
114 if ($subaction) {
115 return;
116 }
117 if (!S::logged() && $nb_tot > $globals->search->public_max) {
118 $page->trigError('Votre recherche a généré trop de résultats pour un affichage public.');
119 } elseif ($nb_tot > $globals->search->private_max) {
120 $page->trigError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.');
121 } elseif (empty($nb_tot)) {
122 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
123 }
124 } else {
125 $page->assign('formulaire',1);
126 $page->addJsLink('ajax.js');
127 }
128
129 $page->changeTpl('search/index.tpl');
130 $page->setTitle('Annuaire');
131 }
132
133 function handler_advanced(&$page, $action = null, $subaction = null)
134 {
135 global $globals;
136 require_once 'geocoding.inc.php';
137 $page->assign('advanced',1);
138 $page->addJsLink('jquery.autocomplete.js');
139
140 if (!Env::has('rechercher') && $action != 'geoloc') {
141 $this->form_prepare();
142 } else {
143 if (!Env::has('page')) {
144 S::logger()->log('search', 'adv=' . var_export($_GET, true));
145 }
146
147 require_once 'userset.inc.php';
148 $view = new SearchSet(false, $action == 'geoloc' && substr($subaction, -3) == 'swf');
149 $view->addMod('minifiche', 'Mini-fiches', true);
150 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true));
151 // TODO: Reactivate when the new map is completed.
152 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
153 $view->apply('search/adv', $page, $action, $subaction);
154
155 if ($subaction) {
156 return;
157 }
158 $nb_tot = $view->count();
159 if ($nb_tot > $globals->search->private_max) {
160 $this->form_prepare();
161 $page->trigError('Recherche trop générale.');
162 }
163 }
164
165 $page->changeTpl('search/index.tpl', $action == 'mini' ? SIMPLE : SKINNED);
166 $page->addJsLink('ajax.js');
167 $page->assign('public_directory',0);
168 }
169
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 // ...
178 pl_content_headers("text/plain");
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 '.*'),
185 Env::s('q'));
186 if (!$q) exit();
187
188 // try to look in cached results
189 $cache = XDB::query('SELECT result
190 FROM search_autocomplete
191 WHERE name = {?} AND
192 query = {?} AND
193 generated > NOW() - INTERVAL 1 DAY',
194 $type, $q);
195 if ($res = $cache->fetchOneCell()) {
196 echo $res;
197 die();
198 }
199
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,
211 'schoolTxt' => DirEnum::EDUSCHOOLS,
212 );
213 if (!array_key_exists($type, $enums)) {
214 exit();
215 }
216
217 $enum = $enums[$type];
218
219 $list = DirEnum::getAutoComplete($enum, $q);
220 $nbResults = 0;
221 $res = "";
222 while ($result = $list->next()) {
223 $nbResults++;
224 if ($nbResults == 11) {
225 $res .= $q."|-1\n";
226 } else {
227 $res .= $result['field'].'|';
228 if (isset($result['nb'])) {
229 $res .= $result['nb'];
230 }
231 if (isset($result['id'])) {
232 $res .= '|'.$result['id'];
233 }
234 $res .= "\n";
235 }
236 }
237 XDB::query('REPLACE INTO search_autocomplete
238 VALUES ({?}, {?}, {?}, NOW())',
239 $type, $q, $res);
240 echo $res;
241 exit();
242 }
243
244 function handler_list(&$page, $type = null, $idVal = null)
245 {
246 $page->assign('name', $type);
247 $page->assign('with_text_value', true);
248 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
249
250 // Give the list of all values possible of type and builds a select input for it
251 $ids = null;
252
253 switch ($type) {
254 case 'binet':
255 $ids = DirEnum::getOptionsIter(DirEnum::BINETS);
256 break;
257 case 'networking_type':
258 $ids = DirEnum::getOptionsIter(DirEnum::NETWORKS);
259 break;
260 case 'country':
261 $ids = DirEnum::getOptionsIter(DirEnum::COUNTRIES);
262 $page->assign('onchange', 'changeCountry(this.value)');
263 break;
264 case 'diploma':
265 if (Env::has('school') && Env::i('school') != 0) {
266 $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES, Env::i('school'));
267 } else {
268 $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES);
269 }
270 break;
271 case 'groupex':
272 $ids = DirEnum::getOptionsIter(DirEnum::GROUPESX);
273 break;
274 case 'nationalite':
275 $ids = DirEnum::getOptionsIter(DirEnum::NATIONALITIES);
276 break;
277 case 'region':
278 if (Env::has('country')) {
279 $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS, Env::v('country'));
280 } else {
281 $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS);
282 }
283 break;
284 case 'school':
285 $ids = DirEnum::getOptionsIter(DirEnum::EDUSCHOOLS);
286 $page->assign('onchange', 'changeSchool(this.value)');
287 break;
288 case 'section':
289 $ids = DirEnum::getOptionsIter(DirEnum::SECTIONS);
290 break;
291 case 'secteur':
292 $ids = DirEnum::getOptionsIter(DirEnum::SECTORS);
293 break;
294 default: exit();
295 }
296 if (isset($idVal)) {
297 pl_content_headers("text/plain");
298 echo $ids[$idVal];
299 exit();
300 }
301 pl_content_headers("text/xml");
302 $page->changeTpl('include/field.select.tpl', NO_SKIN);
303 $page->assign('list', $ids);
304 }
305 }
306
307 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
308 ?>