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