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), |
09824164 |
32 | ); |
33 | } |
34 | |
ba2afb88 |
35 | function handler_redir_advanced(&$page, $mode = null) |
36 | { |
37 | pl_redirect('search/adv'); |
38 | exit; |
39 | } |
40 | |
8d8f7607 |
41 | function on_subscribe($forlife, $uid, $promo, $pass) |
42 | { |
43 | require_once 'user.func.inc.php'; |
44 | user_reindex($uid); |
45 | } |
46 | |
09824164 |
47 | function get_quick($offset, $limit, $order) |
48 | { |
49 | global $globals; |
393137f9 |
50 | if (!S::logged()) { |
51 | Env::kill('with_soundex'); |
52 | } |
09824164 |
53 | $qSearch = new QuickSearch('quick'); |
54 | $fields = new SFieldGroup(true, array($qSearch)); |
55 | |
56 | if ($qSearch->isempty()) { |
a7de4ef7 |
57 | new ThrowError('Recherche trop générale.'); |
09824164 |
58 | } |
59 | |
60 | $sql = 'SELECT SQL_CALC_FOUND_ROWS |
61 | UPPER(IF(u.nom!="",u.nom,u.nom_ini)) AS nom, |
62 | IF(u.prenom!="",u.prenom,u.prenom_ini) AS prenom, |
63 | '.$globals->search->result_fields.' |
64 | c.uid AS contact, w.ni_id AS watch, |
65 | '.$qSearch->get_score_statement().' |
66 | FROM auth_user_md5 AS u |
67 | '.$fields->get_select_statement().' |
68 | LEFT JOIN auth_user_quick AS q ON (u.user_id = q.user_id) |
69 | LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie") |
6b590724 |
70 | LEFT JOIN contacts AS c ON (c.uid='.S::i('uid', -1).' |
09824164 |
71 | AND c.contact=u.user_id) |
72 | LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id |
6b590724 |
73 | AND w.uid='.S::i('uid', -1).') |
09824164 |
74 | '.$globals->search->result_where_statement.' |
75 | WHERE '.$fields->get_where_statement() |
cab08090 |
76 | .(S::logged() && Env::has('nonins') ? ' AND u.perms="pending" AND u.deces=0' : '') |
09824164 |
77 | .' |
78 | GROUP BY u.user_id |
79 | ORDER BY '.($order?($order.', '):'') |
80 | .implode(',',array_filter(array($fields->get_order_statement(), |
81 | 'u.promo DESC, NomSortKey, prenom'))).' |
82 | LIMIT '.$offset * $globals->search->per_page.',' |
83 | .$globals->search->per_page; |
08cce2ff |
84 | $list = XDB::iterator($sql); |
85 | $res = XDB::query("SELECT FOUND_ROWS()"); |
09824164 |
86 | $nb_tot = $res->fetchOneCell(); |
87 | return array($list, $nb_tot); |
88 | } |
89 | |
90 | function form_prepare() |
91 | { |
d25e0f0b |
92 | global $page; |
93 | |
09824164 |
94 | $page->assign('formulaire',1); |
95 | $page->assign('choix_nats', |
b536b5ae |
96 | XDB::iterator('SELECT g.a2 AS id, IF(nat=\'\', g.pays, g.nat) AS text |
97 | FROM geoloc_pays AS g |
98 | INNER JOIN auth_user_md5 AS u ON (u.nationalite = g.a2) |
99 | GROUP BY g.a2 |
100 | ORDER BY text')); |
09824164 |
101 | $page->assign('choix_postes', |
08cce2ff |
102 | XDB::iterator('SELECT id,fonction_fr FROM fonctions_def |
09824164 |
103 | ORDER BY fonction_fr')); |
104 | $page->assign('choix_binets', |
08cce2ff |
105 | XDB::iterator('SELECT id,text FROM binets_def ORDER BY text')); |
09824164 |
106 | $page->assign('choix_groupesx', |
08cce2ff |
107 | XDB::iterator('SELECT id,text FROM groupesx_def ORDER BY text')); |
09824164 |
108 | $page->assign('choix_sections', |
08cce2ff |
109 | XDB::iterator('SELECT id,text FROM sections ORDER BY text')); |
09824164 |
110 | $page->assign('choix_schools', |
08cce2ff |
111 | XDB::iterator('SELECT id,text FROM applis_def ORDER BY text')); |
09824164 |
112 | $page->assign('choix_secteurs', |
08cce2ff |
113 | XDB::iterator('SELECT id,label FROM emploi_secteur ORDER BY label')); |
3c640222 |
114 | $this->get_diplomas(); |
115 | } |
116 | |
117 | function get_diplomas($school = null) |
118 | { |
119 | if (is_null($school) && Env::has('school')) { |
120 | $school = Env::i('school'); |
121 | } |
09824164 |
122 | |
3c640222 |
123 | if (!is_null($school)) { |
124 | $sql = 'SELECT type FROM applis_def WHERE id=' . $school; |
09824164 |
125 | } else { |
126 | $sql = 'DESCRIBE applis_def type'; |
127 | } |
128 | |
08cce2ff |
129 | $res = XDB::query($sql); |
09824164 |
130 | $row = $res->fetchOneRow(); |
3c640222 |
131 | if (!is_null($school)) { |
09824164 |
132 | $types = $row[0]; |
133 | } else { |
134 | $types = explode('(',$row[1]); |
135 | $types = str_replace("'","",substr($types[1],0,-1)); |
136 | } |
3c640222 |
137 | global $page; |
09824164 |
138 | $page->assign('choix_diplomas', explode(',',$types)); |
139 | } |
140 | |
141 | function get_advanced($offset, $limit, $order) |
142 | { |
143 | $fields = new SFieldGroup(true, advancedSearchFromInput()); |
144 | if ($fields->too_large()) { |
145 | $this->form_prepare(); |
a7de4ef7 |
146 | new ThrowError('Recherche trop générale.'); |
09824164 |
147 | } |
148 | global $globals, $page; |
149 | |
150 | $page->assign('search_vars', $fields->get_url()); |
151 | |
152 | $where = $fields->get_where_statement(); |
153 | if ($where) { |
154 | $where = "WHERE $where"; |
155 | } |
156 | $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT |
157 | u.nom, u.prenom, |
158 | '.$globals->search->result_fields.' |
159 | c.uid AS contact, |
160 | w.ni_id AS watch |
161 | FROM auth_user_md5 AS u |
162 | LEFT JOIN auth_user_quick AS q USING(user_id) |
163 | '.$fields->get_select_statement().' |
164 | '.(Env::has('only_referent') ? ' INNER JOIN mentor AS m ON (m.uid = u.user_id)' : '').' |
165 | LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie") |
cab08090 |
166 | LEFT JOIN contacts AS c ON (c.uid='.S::v('uid').' |
09824164 |
167 | AND c.contact=u.user_id) |
168 | LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id |
cab08090 |
169 | AND w.uid='.S::v('uid').') |
09824164 |
170 | '.$globals->search->result_where_statement." |
171 | $where |
1f17ab93 |
172 | GROUP BY u.user_id |
09824164 |
173 | ORDER BY ".($order?($order.', '):'') |
174 | .implode(',',array_filter(array($fields->get_order_statement(), |
175 | 'promo DESC, NomSortKey, prenom'))).' |
176 | LIMIT '.($offset * $limit).','.$limit; |
08cce2ff |
177 | $liste = XDB::iterator($sql); |
178 | $res = XDB::query("SELECT FOUND_ROWS()"); |
09824164 |
179 | $nb_tot = $res->fetchOneCell(); |
180 | return Array($liste, $nb_tot); |
181 | } |
182 | |
183 | function handler_quick(&$page) |
184 | { |
185 | global $globals; |
186 | |
abc68084 |
187 | require_once dirname(__FILE__).'/search/search.inc.php'; |
09824164 |
188 | |
189 | $page->changeTpl('search/index.tpl'); |
190 | |
191 | $page->assign('xorg_title','Polytechnique.org - Annuaire'); |
192 | require_once("applis.func.inc.php"); |
193 | require_once("geoloc.inc.php"); |
194 | |
195 | $page->assign('baseurl', $globals->baseurl); |
196 | |
197 | if (Env::has('quick')) { |
198 | $page->assign('formulaire', 0); |
199 | |
200 | $search = new XOrgSearch(array($this, 'get_quick')); |
201 | $search->setNbLines($globals->search->per_page); |
202 | $search->addOrder('score', 'score', false, 'pertinence', AUTH_PUBLIC, true); |
203 | |
204 | $nb_tot = $search->show(); |
205 | |
cab08090 |
206 | if (!S::logged() && $nb_tot > $globals->search->public_max) { |
a7de4ef7 |
207 | new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.'); |
09824164 |
208 | } elseif ($nb_tot > $globals->search->private_max) { |
a7de4ef7 |
209 | new ThrowError('Recherche trop générale'); |
09824164 |
210 | } elseif (empty($nb_tot)) { |
a7de4ef7 |
211 | new ThrowError('il n\'existe personne correspondant à ces critères dans la base !'); |
09824164 |
212 | } |
213 | } else { |
214 | $page->assign('formulaire',1); |
215 | } |
216 | |
217 | $page->register_modifier('display_lines', 'display_lines'); |
09824164 |
218 | } |
219 | |
220 | function handler_advanced(&$page, $mode = null) |
221 | { |
222 | global $globals; |
223 | |
abc68084 |
224 | require_once dirname(__FILE__).'/search/search.inc.php'; |
09824164 |
225 | require_once 'applis.func.inc.php'; |
226 | require_once 'geoloc.inc.php'; |
227 | |
228 | |
62a66dfc |
229 | $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE : SKINNED); |
09824164 |
230 | |
231 | $page->assign('advanced',1); |
232 | $page->assign('public_directory',0); |
09824164 |
233 | |
234 | if (!Env::has('rechercher')) { |
235 | $this->form_prepare(); |
236 | } else { |
d095003a |
237 | $search = new XOrgSearch(array($this, 'get_advanced')); |
09824164 |
238 | $search->setNbLines($globals->search->per_page); |
239 | |
240 | $page->assign('url_search_form', $search->make_url(Array('rechercher'=>0))); |
b16476f7 |
241 | if (!Env::i('with_soundex')) { |
242 | $page->assign('with_soundex', $search->make_url(Array()) . "&with_soundex=1"); |
09824164 |
243 | } |
09824164 |
244 | $nb_tot = $search->show(); |
245 | |
246 | if ($nb_tot > $globals->search->private_max) { |
247 | $this->form_prepare(); |
a7de4ef7 |
248 | new ThrowError('Recherche trop générale'); |
09824164 |
249 | } |
250 | |
251 | } |
252 | |
3c640222 |
253 | $page->addJsLink('ajax.js'); |
09824164 |
254 | $page->register_modifier('display_lines', 'display_lines'); |
09824164 |
255 | } |
3c640222 |
256 | |
257 | function handler_region(&$page, $country = null) |
258 | { |
493b6abe |
259 | header('Content-Type: text/html; charset="UTF-8"'); |
3c640222 |
260 | require_once("geoloc.inc.php"); |
261 | $page->ChangeTpl('search/adv.region.form.tpl', NO_SKIN); |
262 | $page->assign('region', ""); |
263 | $page->assign('country', $country); |
264 | } |
265 | |
266 | function handler_grade(&$page, $school = null) |
267 | { |
493b6abe |
268 | header('Content-Type: text/html; charset="UTF-8"'); |
3c640222 |
269 | $page->ChangeTpl('search/adv.grade.form.tpl', NO_SKIN); |
270 | $page->assign('grade', ''); |
271 | $this->get_diplomas($school); |
272 | } |
09824164 |
273 | } |
274 | |
a7de4ef7 |
275 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
09824164 |
276 | ?> |