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), |
838cc16a |
32 | 'search/autocomplete' => $this->make_hook('autocomplete', AUTH_PUBLIC), |
09824164 |
33 | ); |
34 | } |
35 | |
ba2afb88 |
36 | function handler_redir_advanced(&$page, $mode = null) |
37 | { |
38 | pl_redirect('search/adv'); |
39 | exit; |
40 | } |
41 | |
8d8f7607 |
42 | function on_subscribe($forlife, $uid, $promo, $pass) |
43 | { |
44 | require_once 'user.func.inc.php'; |
45 | user_reindex($uid); |
46 | } |
47 | |
09824164 |
48 | function get_quick($offset, $limit, $order) |
49 | { |
50 | global $globals; |
393137f9 |
51 | if (!S::logged()) { |
52 | Env::kill('with_soundex'); |
53 | } |
09824164 |
54 | $qSearch = new QuickSearch('quick'); |
55 | $fields = new SFieldGroup(true, array($qSearch)); |
56 | |
57 | if ($qSearch->isempty()) { |
a7de4ef7 |
58 | new ThrowError('Recherche trop générale.'); |
09824164 |
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") |
6b590724 |
71 | LEFT JOIN contacts AS c ON (c.uid='.S::i('uid', -1).' |
09824164 |
72 | AND c.contact=u.user_id) |
73 | LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id |
6b590724 |
74 | AND w.uid='.S::i('uid', -1).') |
09824164 |
75 | '.$globals->search->result_where_statement.' |
76 | WHERE '.$fields->get_where_statement() |
cab08090 |
77 | .(S::logged() && Env::has('nonins') ? ' AND u.perms="pending" AND u.deces=0' : '') |
09824164 |
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; |
08cce2ff |
85 | $list = XDB::iterator($sql); |
86 | $res = XDB::query("SELECT FOUND_ROWS()"); |
09824164 |
87 | $nb_tot = $res->fetchOneCell(); |
88 | return array($list, $nb_tot); |
89 | } |
90 | |
91 | function form_prepare() |
92 | { |
d25e0f0b |
93 | global $page; |
94 | |
09824164 |
95 | $page->assign('formulaire',1); |
96 | $page->assign('choix_nats', |
b536b5ae |
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')); |
09824164 |
102 | $page->assign('choix_postes', |
08cce2ff |
103 | XDB::iterator('SELECT id,fonction_fr FROM fonctions_def |
09824164 |
104 | ORDER BY fonction_fr')); |
105 | $page->assign('choix_binets', |
08cce2ff |
106 | XDB::iterator('SELECT id,text FROM binets_def ORDER BY text')); |
09824164 |
107 | $page->assign('choix_groupesx', |
08cce2ff |
108 | XDB::iterator('SELECT id,text FROM groupesx_def ORDER BY text')); |
09824164 |
109 | $page->assign('choix_sections', |
08cce2ff |
110 | XDB::iterator('SELECT id,text FROM sections ORDER BY text')); |
09824164 |
111 | $page->assign('choix_schools', |
08cce2ff |
112 | XDB::iterator('SELECT id,text FROM applis_def ORDER BY text')); |
09824164 |
113 | $page->assign('choix_secteurs', |
08cce2ff |
114 | XDB::iterator('SELECT id,label FROM emploi_secteur ORDER BY label')); |
3c640222 |
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 | } |
09824164 |
123 | |
3c640222 |
124 | if (!is_null($school)) { |
125 | $sql = 'SELECT type FROM applis_def WHERE id=' . $school; |
09824164 |
126 | } else { |
127 | $sql = 'DESCRIBE applis_def type'; |
128 | } |
129 | |
08cce2ff |
130 | $res = XDB::query($sql); |
09824164 |
131 | $row = $res->fetchOneRow(); |
3c640222 |
132 | if (!is_null($school)) { |
09824164 |
133 | $types = $row[0]; |
134 | } else { |
135 | $types = explode('(',$row[1]); |
136 | $types = str_replace("'","",substr($types[1],0,-1)); |
137 | } |
3c640222 |
138 | global $page; |
09824164 |
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(); |
a7de4ef7 |
147 | new ThrowError('Recherche trop générale.'); |
09824164 |
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") |
cab08090 |
167 | LEFT JOIN contacts AS c ON (c.uid='.S::v('uid').' |
09824164 |
168 | AND c.contact=u.user_id) |
169 | LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id |
cab08090 |
170 | AND w.uid='.S::v('uid').') |
09824164 |
171 | '.$globals->search->result_where_statement." |
172 | $where |
1f17ab93 |
173 | GROUP BY u.user_id |
09824164 |
174 | ORDER BY ".($order?($order.', '):'') |
175 | .implode(',',array_filter(array($fields->get_order_statement(), |
176 | 'promo DESC, NomSortKey, prenom'))).' |
177 | LIMIT '.($offset * $limit).','.$limit; |
08cce2ff |
178 | $liste = XDB::iterator($sql); |
179 | $res = XDB::query("SELECT FOUND_ROWS()"); |
09824164 |
180 | $nb_tot = $res->fetchOneCell(); |
181 | return Array($liste, $nb_tot); |
182 | } |
183 | |
184 | function handler_quick(&$page) |
185 | { |
186 | global $globals; |
187 | |
abc68084 |
188 | require_once dirname(__FILE__).'/search/search.inc.php'; |
09824164 |
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 | |
cab08090 |
207 | if (!S::logged() && $nb_tot > $globals->search->public_max) { |
a7de4ef7 |
208 | new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.'); |
09824164 |
209 | } elseif ($nb_tot > $globals->search->private_max) { |
a7de4ef7 |
210 | new ThrowError('Recherche trop générale'); |
09824164 |
211 | } elseif (empty($nb_tot)) { |
a7de4ef7 |
212 | new ThrowError('il n\'existe personne correspondant à ces critères dans la base !'); |
09824164 |
213 | } |
214 | } else { |
70a55e68 |
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); |
09824164 |
221 | $page->assign('formulaire',1); |
e654517d |
222 | $page->addJsLink('ajax.js'); |
09824164 |
223 | } |
224 | |
225 | $page->register_modifier('display_lines', 'display_lines'); |
09824164 |
226 | } |
227 | |
228 | function handler_advanced(&$page, $mode = null) |
229 | { |
230 | global $globals; |
231 | |
abc68084 |
232 | require_once dirname(__FILE__).'/search/search.inc.php'; |
09824164 |
233 | require_once 'applis.func.inc.php'; |
234 | require_once 'geoloc.inc.php'; |
235 | |
236 | |
62a66dfc |
237 | $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE : SKINNED); |
09824164 |
238 | |
239 | $page->assign('advanced',1); |
240 | $page->assign('public_directory',0); |
09824164 |
241 | |
242 | if (!Env::has('rechercher')) { |
243 | $this->form_prepare(); |
244 | } else { |
d095003a |
245 | $search = new XOrgSearch(array($this, 'get_advanced')); |
09824164 |
246 | $search->setNbLines($globals->search->per_page); |
247 | |
248 | $page->assign('url_search_form', $search->make_url(Array('rechercher'=>0))); |
b16476f7 |
249 | if (!Env::i('with_soundex')) { |
250 | $page->assign('with_soundex', $search->make_url(Array()) . "&with_soundex=1"); |
09824164 |
251 | } |
09824164 |
252 | $nb_tot = $search->show(); |
253 | |
254 | if ($nb_tot > $globals->search->private_max) { |
255 | $this->form_prepare(); |
a7de4ef7 |
256 | new ThrowError('Recherche trop générale'); |
09824164 |
257 | } |
258 | |
259 | } |
260 | |
3c640222 |
261 | $page->addJsLink('ajax.js'); |
09824164 |
262 | $page->register_modifier('display_lines', 'display_lines'); |
09824164 |
263 | } |
3c640222 |
264 | |
265 | function handler_region(&$page, $country = null) |
266 | { |
493b6abe |
267 | header('Content-Type: text/html; charset="UTF-8"'); |
3c640222 |
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 | { |
493b6abe |
276 | header('Content-Type: text/html; charset="UTF-8"'); |
3c640222 |
277 | $page->ChangeTpl('search/adv.grade.form.tpl', NO_SKIN); |
278 | $page->assign('grade', ''); |
279 | $this->get_diplomas($school); |
280 | } |
838cc16a |
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 | } |
09824164 |
316 | } |
317 | |
a7de4ef7 |
318 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
09824164 |
319 | ?> |