autocompletion for advanced search
[platal.git] / modules / search.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 '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),
31 'advanced_search.php' => $this->make_hook('redir_advanced', AUTH_PUBLIC),
32 'search/autocomplete' => $this->make_hook('autocomplete', AUTH_PUBLIC),
33 );
34 }
35
36 function handler_redir_advanced(&$page, $mode = null)
37 {
38 pl_redirect('search/adv');
39 exit;
40 }
41
42 function on_subscribe($forlife, $uid, $promo, $pass)
43 {
44 require_once 'user.func.inc.php';
45 user_reindex($uid);
46 }
47
48 function get_quick($offset, $limit, $order)
49 {
50 global $globals;
51 if (!S::logged()) {
52 Env::kill('with_soundex');
53 }
54 $qSearch = new QuickSearch('quick');
55 $fields = new SFieldGroup(true, array($qSearch));
56
57 if ($qSearch->isempty()) {
58 new ThrowError('Recherche trop générale.');
59 }
60
61 $sql = 'SELECT SQL_CALC_FOUND_ROWS
62 UPPER(IF(u.nom!="",u.nom,u.nom_ini)) AS nom,
63 IF(u.prenom!="",u.prenom,u.prenom_ini) AS prenom,
64 '.$globals->search->result_fields.'
65 c.uid AS contact, w.ni_id AS watch,
66 '.$qSearch->get_score_statement().'
67 FROM auth_user_md5 AS u
68 '.$fields->get_select_statement().'
69 LEFT JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
70 LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie")
71 LEFT JOIN contacts AS c ON (c.uid='.S::i('uid', -1).'
72 AND c.contact=u.user_id)
73 LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id
74 AND w.uid='.S::i('uid', -1).')
75 '.$globals->search->result_where_statement.'
76 WHERE '.$fields->get_where_statement()
77 .(S::logged() && Env::has('nonins') ? ' AND u.perms="pending" AND u.deces=0' : '')
78 .'
79 GROUP BY u.user_id
80 ORDER BY '.($order?($order.', '):'')
81 .implode(',',array_filter(array($fields->get_order_statement(),
82 'u.promo DESC, NomSortKey, prenom'))).'
83 LIMIT '.$offset * $globals->search->per_page.','
84 .$globals->search->per_page;
85 $list = XDB::iterator($sql);
86 $res = XDB::query("SELECT FOUND_ROWS()");
87 $nb_tot = $res->fetchOneCell();
88 return array($list, $nb_tot);
89 }
90
91 function form_prepare()
92 {
93 global $page;
94
95 $page->assign('formulaire',1);
96 $page->assign('choix_nats',
97 XDB::iterator('SELECT g.a2 AS id, IF(nat=\'\', g.pays, g.nat) AS text
98 FROM geoloc_pays AS g
99 INNER JOIN auth_user_md5 AS u ON (u.nationalite = g.a2)
100 GROUP BY g.a2
101 ORDER BY text'));
102 $page->assign('choix_postes',
103 XDB::iterator('SELECT id,fonction_fr FROM fonctions_def
104 ORDER BY fonction_fr'));
105 $page->assign('choix_binets',
106 XDB::iterator('SELECT id,text FROM binets_def ORDER BY text'));
107 $page->assign('choix_groupesx',
108 XDB::iterator('SELECT id,text FROM groupesx_def ORDER BY text'));
109 $page->assign('choix_sections',
110 XDB::iterator('SELECT id,text FROM sections ORDER BY text'));
111 $page->assign('choix_schools',
112 XDB::iterator('SELECT id,text FROM applis_def ORDER BY text'));
113 $page->assign('choix_secteurs',
114 XDB::iterator('SELECT id,label FROM emploi_secteur ORDER BY label'));
115 $this->get_diplomas();
116 }
117
118 function get_diplomas($school = null)
119 {
120 if (is_null($school) && Env::has('school')) {
121 $school = Env::i('school');
122 }
123
124 if (!is_null($school)) {
125 $sql = 'SELECT type FROM applis_def WHERE id=' . $school;
126 } else {
127 $sql = 'DESCRIBE applis_def type';
128 }
129
130 $res = XDB::query($sql);
131 $row = $res->fetchOneRow();
132 if (!is_null($school)) {
133 $types = $row[0];
134 } else {
135 $types = explode('(',$row[1]);
136 $types = str_replace("'","",substr($types[1],0,-1));
137 }
138 global $page;
139 $page->assign('choix_diplomas', explode(',',$types));
140 }
141
142 function get_advanced($offset, $limit, $order)
143 {
144 $fields = new SFieldGroup(true, advancedSearchFromInput());
145 if ($fields->too_large()) {
146 $this->form_prepare();
147 new ThrowError('Recherche trop générale.');
148 }
149 global $globals, $page;
150
151 $page->assign('search_vars', $fields->get_url());
152
153 $where = $fields->get_where_statement();
154 if ($where) {
155 $where = "WHERE $where";
156 }
157 $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT
158 u.nom, u.prenom,
159 '.$globals->search->result_fields.'
160 c.uid AS contact,
161 w.ni_id AS watch
162 FROM auth_user_md5 AS u
163 LEFT JOIN auth_user_quick AS q USING(user_id)
164 '.$fields->get_select_statement().'
165 '.(Env::has('only_referent') ? ' INNER JOIN mentor AS m ON (m.uid = u.user_id)' : '').'
166 LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie")
167 LEFT JOIN contacts AS c ON (c.uid='.S::v('uid').'
168 AND c.contact=u.user_id)
169 LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id
170 AND w.uid='.S::v('uid').')
171 '.$globals->search->result_where_statement."
172 $where
173 GROUP BY u.user_id
174 ORDER BY ".($order?($order.', '):'')
175 .implode(',',array_filter(array($fields->get_order_statement(),
176 'promo DESC, NomSortKey, prenom'))).'
177 LIMIT '.($offset * $limit).','.$limit;
178 $liste = XDB::iterator($sql);
179 $res = XDB::query("SELECT FOUND_ROWS()");
180 $nb_tot = $res->fetchOneCell();
181 return Array($liste, $nb_tot);
182 }
183
184 function handler_quick(&$page)
185 {
186 global $globals;
187
188 require_once dirname(__FILE__).'/search/search.inc.php';
189
190 $page->changeTpl('search/index.tpl');
191
192 $page->assign('xorg_title','Polytechnique.org - Annuaire');
193 require_once("applis.func.inc.php");
194 require_once("geoloc.inc.php");
195
196 $page->assign('baseurl', $globals->baseurl);
197
198 if (Env::has('quick')) {
199 $page->assign('formulaire', 0);
200
201 $search = new XOrgSearch(array($this, 'get_quick'));
202 $search->setNbLines($globals->search->per_page);
203 $search->addOrder('score', 'score', false, 'pertinence', AUTH_PUBLIC, true);
204
205 $nb_tot = $search->show();
206
207 if (!S::logged() && $nb_tot > $globals->search->public_max) {
208 new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
209 } elseif ($nb_tot > $globals->search->private_max) {
210 new ThrowError('Recherche trop générale');
211 } elseif (empty($nb_tot)) {
212 new ThrowError('il n\'existe personne correspondant à ces critères dans la base !');
213 }
214 } else {
215 $res = XDB::query("SELECT MIN(diminutif), MAX(diminutif)
216 FROM groupex.asso
217 WHERE cat = 'Promotions'");
218 list($min, $max) = $res->fetchOneRow();
219 $page->assign('promo_min', $min);
220 $page->assign('promo_max', $max);
221 $page->assign('formulaire',1);
222 $page->addJsLink('ajax.js');
223 }
224
225 $page->register_modifier('display_lines', 'display_lines');
226 }
227
228 function handler_advanced(&$page, $mode = null)
229 {
230 global $globals;
231
232 require_once dirname(__FILE__).'/search/search.inc.php';
233 require_once 'applis.func.inc.php';
234 require_once 'geoloc.inc.php';
235
236
237 $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE : SKINNED);
238
239 $page->assign('advanced',1);
240 $page->assign('public_directory',0);
241
242 if (!Env::has('rechercher')) {
243 $this->form_prepare();
244 } else {
245 $search = new XOrgSearch(array($this, 'get_advanced'));
246 $search->setNbLines($globals->search->per_page);
247
248 $page->assign('url_search_form', $search->make_url(Array('rechercher'=>0)));
249 if (!Env::i('with_soundex')) {
250 $page->assign('with_soundex', $search->make_url(Array()) . "&with_soundex=1");
251 }
252 $nb_tot = $search->show();
253
254 if ($nb_tot > $globals->search->private_max) {
255 $this->form_prepare();
256 new ThrowError('Recherche trop générale');
257 }
258
259 }
260
261 $page->addJsLink('ajax.js');
262 $page->register_modifier('display_lines', 'display_lines');
263 }
264
265 function handler_region(&$page, $country = null)
266 {
267 header('Content-Type: text/html; charset="UTF-8"');
268 require_once("geoloc.inc.php");
269 $page->ChangeTpl('search/adv.region.form.tpl', NO_SKIN);
270 $page->assign('region', "");
271 $page->assign('country', $country);
272 }
273
274 function handler_grade(&$page, $school = null)
275 {
276 header('Content-Type: text/html; charset="UTF-8"');
277 $page->ChangeTpl('search/adv.grade.form.tpl', NO_SKIN);
278 $page->assign('grade', '');
279 $this->get_diplomas($school);
280 }
281
282 function handler_autocomplete(&$page, $type = null)
283 {
284 // Autocompletion : according to type required, return
285 // a list of results matching with the number of matches.
286 // The output format is :
287 // result1|nb1
288 // result2|nb2
289 // ...
290 header('Content-Type: text/plain; charset="UTF-8"');
291 $q = $_REQUEST['q'];
292 if (!$q) exit();
293 $unique = 'user_id';
294 $db = 'auth_user_md5';
295 switch ($type) {
296 case 'firstname': $field = 'prenom'; break;
297 case 'name': $field = 'nom'; break;
298 case 'nickname': $field = 'profile_nick'; break;
299 case 'entreprise': $db = 'entreprises'; $field = 'entreprise'; $unique='uid'; break;
300 default: exit();
301 }
302
303 $liste = XDB::iterator('SELECT '.$field.' AS field, COUNT(DISTINCT '.$unique.') AS nb FROM '.$db.' WHERE '.$field.' LIKE {?} GROUP BY '.$field.' LIMIT 11', $q.'%');
304 $nbResults = 0;
305 while ($result = $liste->next()) {
306 $nbResults++;
307 if ($nbResults == 11) {
308 echo '...|1'."\n";
309 } else {
310 echo $result['field'].'|'.$result['nb']."\n";
311 }
312 }
313
314 exit();
315 }
316 }
317
318 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
319 ?>