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