Continue #448: fix search and order with PlSet
[platal.git] / modules / search.php
CommitLineData
09824164 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 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(
27 'search' => $this->make_hook('quick', AUTH_PUBLIC),
28 'search/adv' => $this->make_hook('advanced', AUTH_COOKIE),
9ed396c0 29 'search/ajax/region' => $this->make_hook('region', AUTH_COOKIE, 'user', NO_AUTH),
30 'search/ajax/grade' => $this->make_hook('grade', AUTH_COOKIE, 'user', NO_AUTH),
ba2afb88 31 'advanced_search.php' => $this->make_hook('redir_advanced', AUTH_PUBLIC),
2ab7a09f 32 'search/autocomplete' => $this->make_hook('autocomplete', AUTH_COOKIE, 'user', NO_AUTH),
33 'search/list' => $this->make_hook('list', AUTH_COOKIE, 'user', NO_AUTH),
09824164 34 );
35 }
36
ba2afb88 37 function handler_redir_advanced(&$page, $mode = null)
38 {
39 pl_redirect('search/adv');
40 exit;
41 }
42
8d8f7607 43 function on_subscribe($forlife, $uid, $promo, $pass)
44 {
45 require_once 'user.func.inc.php';
46 user_reindex($uid);
47 }
48
09824164 49
50 function form_prepare()
51 {
d25e0f0b 52 global $page;
53
09824164 54 $page->assign('formulaire',1);
09824164 55 $page->assign('choix_schools',
08cce2ff 56 XDB::iterator('SELECT id,text FROM applis_def ORDER BY text'));
3c640222 57 $this->get_diplomas();
58 }
59
60 function get_diplomas($school = null)
61 {
62 if (is_null($school) && Env::has('school')) {
63 $school = Env::i('school');
64 }
09824164 65
3c640222 66 if (!is_null($school)) {
67 $sql = 'SELECT type FROM applis_def WHERE id=' . $school;
09824164 68 } else {
69 $sql = 'DESCRIBE applis_def type';
70 }
71
08cce2ff 72 $res = XDB::query($sql);
09824164 73 $row = $res->fetchOneRow();
3c640222 74 if (!is_null($school)) {
09824164 75 $types = $row[0];
76 } else {
77 $types = explode('(',$row[1]);
78 $types = str_replace("'","",substr($types[1],0,-1));
79 }
3c640222 80 global $page;
09824164 81 $page->assign('choix_diplomas', explode(',',$types));
82 }
83
8c4a0c30 84 function handler_quick(&$page, $action = null, $subaction = null)
09824164 85 {
86 global $globals;
87
8c4a0c30 88 if (Env::has('quick') || $action == 'geoloc') {
09824164 89 $page->assign('formulaire', 0);
90
8c4a0c30 91 require_once 'userset.inc.php';
92 $view = new SearchSet(true, $action == 'geoloc' && substr($subaction, -3) == 'swf');
35fa92e8 93 $view->addMod('minifiche', 'Minifiches', true, array('with_score' => true));
94 if (S::logged() && !Env::i('nonins')) {
95 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true, 'with_score' => true));
96 $view->addMod('geoloc', 'Planisphère');
97 }
8c4a0c30 98 $view->apply('search', $page, $action, $subaction);
09824164 99
8c4a0c30 100 $nb_tot = $view->count();
101 if ($subaction) {
102 return;
103 }
cab08090 104 if (!S::logged() && $nb_tot > $globals->search->public_max) {
a7de4ef7 105 new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
09824164 106 } elseif ($nb_tot > $globals->search->private_max) {
a7de4ef7 107 new ThrowError('Recherche trop générale');
09824164 108 } elseif (empty($nb_tot)) {
a7de4ef7 109 new ThrowError('il n\'existe personne correspondant à ces critères dans la base !');
09824164 110 }
111 } else {
70a55e68 112 $res = XDB::query("SELECT MIN(diminutif), MAX(diminutif)
113 FROM groupex.asso
114 WHERE cat = 'Promotions'");
115 list($min, $max) = $res->fetchOneRow();
116 $page->assign('promo_min', $min);
117 $page->assign('promo_max', $max);
09824164 118 $page->assign('formulaire',1);
e654517d 119 $page->addJsLink('ajax.js');
09824164 120 }
121
90ccb062 122 require_once dirname(__FILE__) . '/search/search.inc.php';
8c4a0c30 123 $page->changeTpl('search/index.tpl');
124 $page->assign('xorg_title','Polytechnique.org - Annuaire');
125 $page->assign('baseurl', $globals->baseurl);
90ccb062 126 $page->register_modifier('display_lines', 'display_lines');
09824164 127 }
128
90ccb062 129 function handler_advanced(&$page, $action = null, $subaction = null)
09824164 130 {
131 global $globals;
90ccb062 132 if (!Env::has('rechercher') && $action != 'geoloc') {
09824164 133 $this->form_prepare();
134 } else {
8c4a0c30 135 require_once 'userset.inc.php';
136 $view = new SearchSet(false, $action == 'geoloc' && substr($subaction, -3) == 'swf');
137 $view->addMod('minifiche', 'Minifiches', true);
35fa92e8 138 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true));
8c4a0c30 139 $view->addMod('geoloc', 'Planishpère');
140 $view->apply('search', $page, $action, $subaction);
141
142 if ($subaction) {
143 return;
09824164 144 }
8c4a0c30 145 $nb_tot = $view->count();
09824164 146 if ($nb_tot > $globals->search->private_max) {
147 $this->form_prepare();
a7de4ef7 148 new ThrowError('Recherche trop générale');
09824164 149 }
09824164 150 }
151
90ccb062 152 require_once 'geoloc.inc.php';
153 require_once dirname(__FILE__) . '/search/search.inc.php';
154 $page->changeTpl('search/index.tpl', $action == 'mini' ? SIMPLE : SKINNED);
3c640222 155 $page->addJsLink('ajax.js');
8c4a0c30 156 $page->assign('advanced',1);
157 $page->assign('public_directory',0);
90ccb062 158 $page->register_modifier('display_lines', 'display_lines');
09824164 159 }
3c640222 160
161 function handler_region(&$page, $country = null)
162 {
493b6abe 163 header('Content-Type: text/html; charset="UTF-8"');
3c640222 164 require_once("geoloc.inc.php");
165 $page->ChangeTpl('search/adv.region.form.tpl', NO_SKIN);
166 $page->assign('region', "");
167 $page->assign('country', $country);
168 }
169
170 function handler_grade(&$page, $school = null)
171 {
493b6abe 172 header('Content-Type: text/html; charset="UTF-8"');
3c640222 173 $page->ChangeTpl('search/adv.grade.form.tpl', NO_SKIN);
174 $page->assign('grade', '');
175 $this->get_diplomas($school);
176 }
838cc16a 177
178 function handler_autocomplete(&$page, $type = null)
179 {
180 // Autocompletion : according to type required, return
181 // a list of results matching with the number of matches.
182 // The output format is :
183 // result1|nb1
184 // result2|nb2
185 // ...
186 header('Content-Type: text/plain; charset="UTF-8"');
c15afc4e 187 $q = preg_replace('/\*+$/','',$_REQUEST['q']);
838cc16a 188 if (!$q) exit();
c15afc4e 189
190 // try to look in cached results
191 $cache = XDB::query('SELECT result FROM search_autocomplete WHERE name = {?} AND query = {?} AND generated > NOW() - INTERVAL 1 DAY',
192 $type, $q);
193 if ($res = $cache->fetchOneCell()) {
194 echo $res;
195 die();
196 }
197
2ab7a09f 198 // default search
838cc16a 199 $unique = 'user_id';
200 $db = 'auth_user_md5';
2ab7a09f 201 $realid = false;
202 $contains = false;
203
838cc16a 204 switch ($type) {
2ab7a09f 205 case 'binetTxt':
206 $db = 'binets_def INNER JOIN binets_ins ON(binets_def.id = binets_ins.binet_id)';
207 $field='binets_def.text';
208 if (strlen($q) > 2)
209 $contains = true;
210 $realid = 'binets_def.id';
211 break;
212 case 'city': $db = 'geoloc_city INNER JOIN adresses ON(geoloc_city.id = adresses.cityid)'; $unique='uid'; $field='geoloc_city.name'; break;
213 case 'entreprise': $db = 'entreprises'; $field = 'entreprise'; $unique='uid'; break;
838cc16a 214 case 'firstname': $field = 'prenom'; break;
2ab7a09f 215 case 'fonctionTxt':
216 $db = 'fonctions_def INNER JOIN entreprises ON(entreprises.fonction = fonctions_def.id)';
217 $field = 'fonction_fr';
218 $unique = 'uid';
219 $realid = 'fonctions_def.id';
220 break;
221 case 'groupexTxt':
222 $db = 'groupesx_def INNER JOIN groupesx_ins ON(groupesx_def.id = groupesx_ins.gid)';
223 $field='groupesx_def.text';
224 if (strlen($q) > 2)
225 $contains = true;
226 $realid = 'groupesx_def.id';
227 $unique = 'guid';
228 break;
838cc16a 229 case 'name': $field = 'nom'; break;
2ab7a09f 230 case 'nationaliteTxt':
231 $db = 'geoloc_pays INNER JOIN auth_user_md5 ON(geoloc_pays.a2 = auth_user_md5.nationalite)';
232 $field = 'IF(geoloc_pays.nat=\'\', geoloc_pays.pays, geoloc_pays.nat)';
233 $realid = 'geoloc_pays.a2';
234 break;
baa8a594 235 case 'nickname': $field = 'profile_nick'; $db = 'auth_user_quick'; break;
df87518a 236 case 'poste': $db = 'entreprises'; $field = 'poste'; $unique='uid'; break;
2ab7a09f 237 case 'secteurTxt':
238 $db = 'emploi_secteur INNER JOIN entreprises ON(entreprises.secteur = emploi_secteur.id)';
239 $field = 'emploi_secteur.label';
240 $realid = 'emploi_secteur.id';
241 $unique = 'uid';
242 break;
243 case 'sectionTxt':
244 $db = 'sections INNER JOIN auth_user_md5 ON(auth_user_md5.section = sections.id)';
245 $field = 'sections.text';
246 $realid = 'sections.id';
247 break;
838cc16a 248 default: exit();
249 }
250
2ab7a09f 251 $list = XDB::iterator('
252 SELECT
253 '.$field.' AS field,
254 COUNT(DISTINCT '.$unique.') AS nb
255 '.($realid?(', '.$realid.' AS id'):'').'
256 FROM '.$db.'
257 WHERE '.$field.' LIKE {?}
258 GROUP BY '.$field.'
259 ORDER BY nb DESC
260 LIMIT 11',
261 ($contains?'%':'').str_replace('*','%',$q).'%');
838cc16a 262 $nbResults = 0;
c15afc4e 263 $res = "";
2ab7a09f 264 while ($result = $list->next()) {
838cc16a 265 $nbResults++;
266 if ($nbResults == 11) {
c15afc4e 267 $res .= '...|1'."\n";
838cc16a 268 } else {
c15afc4e 269 $res .= $result['field'].'|'.$result['nb'].(isset($result['id'])?('|'.$result['id']):'')."\n";
838cc16a 270 }
271 }
c15afc4e 272 XDB::query('REPLACE INTO search_autocomplete VALUES ({?}, {?}, {?}, NOW())',
273 $type, $q, $res);
274 echo $res;
838cc16a 275 exit();
276 }
2ab7a09f 277
278 function handler_list(&$page, $type = null, $idVal = null)
279 {
280 // Give the list of all values possible of type and builds a select input for it
281 $field = 'text';
282 $id = 'id';
283 switch ($type) {
284 case 'binet':
285 $db = 'binets_def';
286 break;
287 case 'fonction':
288 $db = 'fonctions_def';
289 $field = 'fonction_fr';
290 break;
291 case 'groupex':
292 $db = 'groupesx_def';
293 break;
294 case 'nationalite':
295 $db = 'geoloc_pays';
296 $field = 'IF(nat=\'\', pays, nat)';
297 $id = 'a2';
298 break;
299 case 'section':
300 $db = 'sections';
301 break;
302 case 'secteur':
303 $db = 'emploi_secteur';
304 $field = 'label';
305 break;
306 default: exit();
307 }
308 if (isset($idVal)) {
309 header('Content-Type: text/plain; charset="UTF-8"');
310 $result = XDB::query('SELECT '.$field.' AS field FROM '.$db.' WHERE '.$id.' = {?} LIMIT 1',$idVal);
311 echo $result->fetchOneCell();
312 } else {
313 header('Content-Type: text/xml; charset="UTF-8"');
314 $list = XDB::iterator('
315 SELECT
316 '.$field.' AS field,
317 '.$id.' AS id
318 FROM '.$db.'
319 ORDER BY '.$field);
320 echo '<select name="'.$type.'">';
321 while ($result = $list->next()) {
322 echo '<option value="'.$result['id'].'">'.htmlspecialchars($result['field']).'</option>';
323 }
324 echo '</select>';
325 }
326
327 exit();
328 }
09824164 329}
330
a7de4ef7 331// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
09824164 332?>