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