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 | |
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), |
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 | function get_quick($offset, $limit, $order) |
50 | { |
51 | global $globals; |
393137f9 |
52 | if (!S::logged()) { |
53 | Env::kill('with_soundex'); |
54 | } |
09824164 |
55 | $qSearch = new QuickSearch('quick'); |
56 | $fields = new SFieldGroup(true, array($qSearch)); |
57 | |
58 | if ($qSearch->isempty()) { |
a7de4ef7 |
59 | new ThrowError('Recherche trop générale.'); |
09824164 |
60 | } |
61 | |
62 | $sql = 'SELECT SQL_CALC_FOUND_ROWS |
63 | UPPER(IF(u.nom!="",u.nom,u.nom_ini)) AS nom, |
64 | IF(u.prenom!="",u.prenom,u.prenom_ini) AS prenom, |
65 | '.$globals->search->result_fields.' |
66 | c.uid AS contact, w.ni_id AS watch, |
67 | '.$qSearch->get_score_statement().' |
68 | FROM auth_user_md5 AS u |
69 | '.$fields->get_select_statement().' |
70 | LEFT JOIN auth_user_quick AS q ON (u.user_id = q.user_id) |
71 | LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie") |
6b590724 |
72 | LEFT JOIN contacts AS c ON (c.uid='.S::i('uid', -1).' |
09824164 |
73 | AND c.contact=u.user_id) |
74 | LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id |
6b590724 |
75 | AND w.uid='.S::i('uid', -1).') |
09824164 |
76 | '.$globals->search->result_where_statement.' |
77 | WHERE '.$fields->get_where_statement() |
cab08090 |
78 | .(S::logged() && Env::has('nonins') ? ' AND u.perms="pending" AND u.deces=0' : '') |
09824164 |
79 | .' |
80 | GROUP BY u.user_id |
81 | ORDER BY '.($order?($order.', '):'') |
82 | .implode(',',array_filter(array($fields->get_order_statement(), |
83 | 'u.promo DESC, NomSortKey, prenom'))).' |
84 | LIMIT '.$offset * $globals->search->per_page.',' |
85 | .$globals->search->per_page; |
08cce2ff |
86 | $list = XDB::iterator($sql); |
87 | $res = XDB::query("SELECT FOUND_ROWS()"); |
09824164 |
88 | $nb_tot = $res->fetchOneCell(); |
89 | return array($list, $nb_tot); |
90 | } |
91 | |
92 | function form_prepare() |
93 | { |
d25e0f0b |
94 | global $page; |
95 | |
09824164 |
96 | $page->assign('formulaire',1); |
09824164 |
97 | $page->assign('choix_schools', |
08cce2ff |
98 | XDB::iterator('SELECT id,text FROM applis_def ORDER BY text')); |
3c640222 |
99 | $this->get_diplomas(); |
100 | } |
101 | |
102 | function get_diplomas($school = null) |
103 | { |
104 | if (is_null($school) && Env::has('school')) { |
105 | $school = Env::i('school'); |
106 | } |
09824164 |
107 | |
3c640222 |
108 | if (!is_null($school)) { |
109 | $sql = 'SELECT type FROM applis_def WHERE id=' . $school; |
09824164 |
110 | } else { |
111 | $sql = 'DESCRIBE applis_def type'; |
112 | } |
113 | |
08cce2ff |
114 | $res = XDB::query($sql); |
09824164 |
115 | $row = $res->fetchOneRow(); |
3c640222 |
116 | if (!is_null($school)) { |
09824164 |
117 | $types = $row[0]; |
118 | } else { |
119 | $types = explode('(',$row[1]); |
120 | $types = str_replace("'","",substr($types[1],0,-1)); |
121 | } |
3c640222 |
122 | global $page; |
09824164 |
123 | $page->assign('choix_diplomas', explode(',',$types)); |
124 | } |
125 | |
126 | function get_advanced($offset, $limit, $order) |
127 | { |
128 | $fields = new SFieldGroup(true, advancedSearchFromInput()); |
129 | if ($fields->too_large()) { |
130 | $this->form_prepare(); |
a7de4ef7 |
131 | new ThrowError('Recherche trop générale.'); |
09824164 |
132 | } |
133 | global $globals, $page; |
134 | |
135 | $page->assign('search_vars', $fields->get_url()); |
136 | |
137 | $where = $fields->get_where_statement(); |
138 | if ($where) { |
139 | $where = "WHERE $where"; |
140 | } |
141 | $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT |
142 | u.nom, u.prenom, |
143 | '.$globals->search->result_fields.' |
144 | c.uid AS contact, |
145 | w.ni_id AS watch |
146 | FROM auth_user_md5 AS u |
147 | LEFT JOIN auth_user_quick AS q USING(user_id) |
148 | '.$fields->get_select_statement().' |
149 | '.(Env::has('only_referent') ? ' INNER JOIN mentor AS m ON (m.uid = u.user_id)' : '').' |
150 | LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie") |
cab08090 |
151 | LEFT JOIN contacts AS c ON (c.uid='.S::v('uid').' |
09824164 |
152 | AND c.contact=u.user_id) |
153 | LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id |
cab08090 |
154 | AND w.uid='.S::v('uid').') |
09824164 |
155 | '.$globals->search->result_where_statement." |
156 | $where |
1f17ab93 |
157 | GROUP BY u.user_id |
09824164 |
158 | ORDER BY ".($order?($order.', '):'') |
159 | .implode(',',array_filter(array($fields->get_order_statement(), |
160 | 'promo DESC, NomSortKey, prenom'))).' |
161 | LIMIT '.($offset * $limit).','.$limit; |
08cce2ff |
162 | $liste = XDB::iterator($sql); |
163 | $res = XDB::query("SELECT FOUND_ROWS()"); |
09824164 |
164 | $nb_tot = $res->fetchOneCell(); |
165 | return Array($liste, $nb_tot); |
166 | } |
167 | |
168 | function handler_quick(&$page) |
169 | { |
170 | global $globals; |
171 | |
abc68084 |
172 | require_once dirname(__FILE__).'/search/search.inc.php'; |
09824164 |
173 | |
174 | $page->changeTpl('search/index.tpl'); |
175 | |
176 | $page->assign('xorg_title','Polytechnique.org - Annuaire'); |
177 | require_once("applis.func.inc.php"); |
178 | require_once("geoloc.inc.php"); |
179 | |
180 | $page->assign('baseurl', $globals->baseurl); |
181 | |
182 | if (Env::has('quick')) { |
183 | $page->assign('formulaire', 0); |
184 | |
185 | $search = new XOrgSearch(array($this, 'get_quick')); |
186 | $search->setNbLines($globals->search->per_page); |
187 | $search->addOrder('score', 'score', false, 'pertinence', AUTH_PUBLIC, true); |
188 | |
189 | $nb_tot = $search->show(); |
190 | |
cab08090 |
191 | if (!S::logged() && $nb_tot > $globals->search->public_max) { |
a7de4ef7 |
192 | new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.'); |
09824164 |
193 | } elseif ($nb_tot > $globals->search->private_max) { |
a7de4ef7 |
194 | new ThrowError('Recherche trop générale'); |
09824164 |
195 | } elseif (empty($nb_tot)) { |
a7de4ef7 |
196 | new ThrowError('il n\'existe personne correspondant à ces critères dans la base !'); |
09824164 |
197 | } |
198 | } else { |
70a55e68 |
199 | $res = XDB::query("SELECT MIN(diminutif), MAX(diminutif) |
200 | FROM groupex.asso |
201 | WHERE cat = 'Promotions'"); |
202 | list($min, $max) = $res->fetchOneRow(); |
203 | $page->assign('promo_min', $min); |
204 | $page->assign('promo_max', $max); |
09824164 |
205 | $page->assign('formulaire',1); |
e654517d |
206 | $page->addJsLink('ajax.js'); |
09824164 |
207 | } |
208 | |
209 | $page->register_modifier('display_lines', 'display_lines'); |
09824164 |
210 | } |
211 | |
212 | function handler_advanced(&$page, $mode = null) |
213 | { |
214 | global $globals; |
215 | |
abc68084 |
216 | require_once dirname(__FILE__).'/search/search.inc.php'; |
09824164 |
217 | require_once 'applis.func.inc.php'; |
218 | require_once 'geoloc.inc.php'; |
219 | |
220 | |
62a66dfc |
221 | $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE : SKINNED); |
09824164 |
222 | |
223 | $page->assign('advanced',1); |
224 | $page->assign('public_directory',0); |
09824164 |
225 | |
226 | if (!Env::has('rechercher')) { |
227 | $this->form_prepare(); |
228 | } else { |
d095003a |
229 | $search = new XOrgSearch(array($this, 'get_advanced')); |
09824164 |
230 | $search->setNbLines($globals->search->per_page); |
231 | |
232 | $page->assign('url_search_form', $search->make_url(Array('rechercher'=>0))); |
b16476f7 |
233 | if (!Env::i('with_soundex')) { |
df87518a |
234 | $page->assign('with_soundex', $search->make_url(Array()) . "&with_soundex=1"); |
09824164 |
235 | } |
09824164 |
236 | $nb_tot = $search->show(); |
237 | |
238 | if ($nb_tot > $globals->search->private_max) { |
239 | $this->form_prepare(); |
a7de4ef7 |
240 | new ThrowError('Recherche trop générale'); |
09824164 |
241 | } |
242 | |
243 | } |
244 | |
3c640222 |
245 | $page->addJsLink('ajax.js'); |
09824164 |
246 | $page->register_modifier('display_lines', 'display_lines'); |
09824164 |
247 | } |
3c640222 |
248 | |
249 | function handler_region(&$page, $country = null) |
250 | { |
493b6abe |
251 | header('Content-Type: text/html; charset="UTF-8"'); |
3c640222 |
252 | require_once("geoloc.inc.php"); |
253 | $page->ChangeTpl('search/adv.region.form.tpl', NO_SKIN); |
254 | $page->assign('region', ""); |
255 | $page->assign('country', $country); |
256 | } |
257 | |
258 | function handler_grade(&$page, $school = null) |
259 | { |
493b6abe |
260 | header('Content-Type: text/html; charset="UTF-8"'); |
3c640222 |
261 | $page->ChangeTpl('search/adv.grade.form.tpl', NO_SKIN); |
262 | $page->assign('grade', ''); |
263 | $this->get_diplomas($school); |
264 | } |
838cc16a |
265 | |
266 | function handler_autocomplete(&$page, $type = null) |
267 | { |
268 | // Autocompletion : according to type required, return |
269 | // a list of results matching with the number of matches. |
270 | // The output format is : |
271 | // result1|nb1 |
272 | // result2|nb2 |
273 | // ... |
274 | header('Content-Type: text/plain; charset="UTF-8"'); |
275 | $q = $_REQUEST['q']; |
276 | if (!$q) exit(); |
2ab7a09f |
277 | // default search |
838cc16a |
278 | $unique = 'user_id'; |
279 | $db = 'auth_user_md5'; |
2ab7a09f |
280 | $realid = false; |
281 | $contains = false; |
282 | |
838cc16a |
283 | switch ($type) { |
2ab7a09f |
284 | case 'binetTxt': |
285 | $db = 'binets_def INNER JOIN binets_ins ON(binets_def.id = binets_ins.binet_id)'; |
286 | $field='binets_def.text'; |
287 | if (strlen($q) > 2) |
288 | $contains = true; |
289 | $realid = 'binets_def.id'; |
290 | break; |
291 | case 'city': $db = 'geoloc_city INNER JOIN adresses ON(geoloc_city.id = adresses.cityid)'; $unique='uid'; $field='geoloc_city.name'; break; |
292 | case 'entreprise': $db = 'entreprises'; $field = 'entreprise'; $unique='uid'; break; |
838cc16a |
293 | case 'firstname': $field = 'prenom'; break; |
2ab7a09f |
294 | case 'fonctionTxt': |
295 | $db = 'fonctions_def INNER JOIN entreprises ON(entreprises.fonction = fonctions_def.id)'; |
296 | $field = 'fonction_fr'; |
297 | $unique = 'uid'; |
298 | $realid = 'fonctions_def.id'; |
299 | break; |
300 | case 'groupexTxt': |
301 | $db = 'groupesx_def INNER JOIN groupesx_ins ON(groupesx_def.id = groupesx_ins.gid)'; |
302 | $field='groupesx_def.text'; |
303 | if (strlen($q) > 2) |
304 | $contains = true; |
305 | $realid = 'groupesx_def.id'; |
306 | $unique = 'guid'; |
307 | break; |
838cc16a |
308 | case 'name': $field = 'nom'; break; |
2ab7a09f |
309 | case 'nationaliteTxt': |
310 | $db = 'geoloc_pays INNER JOIN auth_user_md5 ON(geoloc_pays.a2 = auth_user_md5.nationalite)'; |
311 | $field = 'IF(geoloc_pays.nat=\'\', geoloc_pays.pays, geoloc_pays.nat)'; |
312 | $realid = 'geoloc_pays.a2'; |
313 | break; |
baa8a594 |
314 | case 'nickname': $field = 'profile_nick'; $db = 'auth_user_quick'; break; |
df87518a |
315 | case 'poste': $db = 'entreprises'; $field = 'poste'; $unique='uid'; break; |
2ab7a09f |
316 | case 'secteurTxt': |
317 | $db = 'emploi_secteur INNER JOIN entreprises ON(entreprises.secteur = emploi_secteur.id)'; |
318 | $field = 'emploi_secteur.label'; |
319 | $realid = 'emploi_secteur.id'; |
320 | $unique = 'uid'; |
321 | break; |
322 | case 'sectionTxt': |
323 | $db = 'sections INNER JOIN auth_user_md5 ON(auth_user_md5.section = sections.id)'; |
324 | $field = 'sections.text'; |
325 | $realid = 'sections.id'; |
326 | break; |
838cc16a |
327 | default: exit(); |
328 | } |
329 | |
2ab7a09f |
330 | $list = XDB::iterator(' |
331 | SELECT |
332 | '.$field.' AS field, |
333 | COUNT(DISTINCT '.$unique.') AS nb |
334 | '.($realid?(', '.$realid.' AS id'):'').' |
335 | FROM '.$db.' |
336 | WHERE '.$field.' LIKE {?} |
337 | GROUP BY '.$field.' |
338 | ORDER BY nb DESC |
339 | LIMIT 11', |
340 | ($contains?'%':'').str_replace('*','%',$q).'%'); |
838cc16a |
341 | $nbResults = 0; |
2ab7a09f |
342 | while ($result = $list->next()) { |
838cc16a |
343 | $nbResults++; |
344 | if ($nbResults == 11) { |
345 | echo '...|1'."\n"; |
346 | } else { |
2ab7a09f |
347 | echo $result['field'].'|'.$result['nb'].(isset($result['id'])?('|'.$result['id']):'')."\n"; |
838cc16a |
348 | } |
349 | } |
350 | |
351 | exit(); |
352 | } |
2ab7a09f |
353 | |
354 | function handler_list(&$page, $type = null, $idVal = null) |
355 | { |
356 | // Give the list of all values possible of type and builds a select input for it |
357 | $field = 'text'; |
358 | $id = 'id'; |
359 | switch ($type) { |
360 | case 'binet': |
361 | $db = 'binets_def'; |
362 | break; |
363 | case 'fonction': |
364 | $db = 'fonctions_def'; |
365 | $field = 'fonction_fr'; |
366 | break; |
367 | case 'groupex': |
368 | $db = 'groupesx_def'; |
369 | break; |
370 | case 'nationalite': |
371 | $db = 'geoloc_pays'; |
372 | $field = 'IF(nat=\'\', pays, nat)'; |
373 | $id = 'a2'; |
374 | break; |
375 | case 'section': |
376 | $db = 'sections'; |
377 | break; |
378 | case 'secteur': |
379 | $db = 'emploi_secteur'; |
380 | $field = 'label'; |
381 | break; |
382 | default: exit(); |
383 | } |
384 | if (isset($idVal)) { |
385 | header('Content-Type: text/plain; charset="UTF-8"'); |
386 | $result = XDB::query('SELECT '.$field.' AS field FROM '.$db.' WHERE '.$id.' = {?} LIMIT 1',$idVal); |
387 | echo $result->fetchOneCell(); |
388 | } else { |
389 | header('Content-Type: text/xml; charset="UTF-8"'); |
390 | $list = XDB::iterator(' |
391 | SELECT |
392 | '.$field.' AS field, |
393 | '.$id.' AS id |
394 | FROM '.$db.' |
395 | ORDER BY '.$field); |
396 | echo '<select name="'.$type.'">'; |
397 | while ($result = $list->next()) { |
398 | echo '<option value="'.$result['id'].'">'.htmlspecialchars($result['field']).'</option>'; |
399 | } |
400 | echo '</select>'; |
401 | } |
402 | |
403 | exit(); |
404 | } |
09824164 |
405 | } |
406 | |
a7de4ef7 |
407 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
09824164 |
408 | ?> |