2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class SearchModule
extends PLModule
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 'search/autocomplete' => $this->make_hook('autocomplete', AUTH_COOKIE
, 'user', NO_AUTH
),
33 'search/list' => $this->make_hook('list', AUTH_COOKIE
, 'user', NO_AUTH
),
37 function handler_redir_advanced(&$page, $mode = null
)
39 pl_redirect('search/adv');
43 function on_subscribe($forlife, $uid, $promo, $pass)
45 require_once 'user.func.inc.php';
49 function get_quick($offset, $limit, $order)
53 Env
::kill('with_soundex');
55 $qSearch = new QuickSearch('quick');
56 $fields = new SFieldGroup(true
, array($qSearch));
58 if ($qSearch->isempty()) {
59 new ThrowError('Recherche trop générale.');
62 $sql = 'SELECT SQL_CALC_FOUND_ROWS
63 UPPER(IF(u.nom!="",u.nom,u.nom_ini)) AS nom,
64 IF(u.prenom!="",u.prenom,u.prenom_ini) AS prenom,
65 '.$globals->search
->result_fields
.'
66 c.uid AS contact, w.ni_id AS watch,
67 '.$qSearch->get_score_statement().'
68 FROM auth_user_md5 AS u
69 '.$fields->get_select_statement().'
70 LEFT JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
71 LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie")
72 LEFT JOIN contacts AS c ON (c.uid='.S
::i('uid', -1).'
73 AND c.contact=u.user_id)
74 LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id
75 AND w.uid='.S
::i('uid', -1).')
76 '.$globals->search
->result_where_statement
.'
77 WHERE '.$fields->get_where_statement()
78 .(S
::logged() && Env
::has('nonins') ?
' AND u.perms="pending" AND u.deces=0' : '')
81 ORDER BY '.($order?
($order.', '):'')
82 .implode(',',array_filter(array($fields->get_order_statement(),
83 'u.promo DESC, NomSortKey, prenom'))).'
84 LIMIT '.$offset * $globals->search
->per_page
.','
85 .$globals->search
->per_page
;
86 $list = XDB
::iterator($sql);
87 $res = XDB
::query("SELECT FOUND_ROWS()");
88 $nb_tot = $res->fetchOneCell();
89 return array($list, $nb_tot);
92 function form_prepare()
96 $page->assign('formulaire',1);
97 $page->assign('choix_schools',
98 XDB
::iterator('SELECT id,text FROM applis_def ORDER BY text'));
99 $this->get_diplomas();
102 function get_diplomas($school = null
)
104 if (is_null($school) && Env
::has('school')) {
105 $school = Env
::i('school');
108 if (!is_null($school)) {
109 $sql = 'SELECT type FROM applis_def WHERE id=' . $school;
111 $sql = 'DESCRIBE applis_def type';
114 $res = XDB
::query($sql);
115 $row = $res->fetchOneRow();
116 if (!is_null($school)) {
119 $types = explode('(',$row[1]);
120 $types = str_replace("'","",substr($types[1],0,-1));
123 $page->assign('choix_diplomas', explode(',',$types));
126 function get_advanced($offset, $limit, $order)
128 $fields = new SFieldGroup(true
, advancedSearchFromInput());
129 if ($fields->too_large()) {
130 $this->form_prepare();
131 new ThrowError('Recherche trop générale.');
133 global $globals, $page;
135 $page->assign('search_vars', $fields->get_url());
137 $where = $fields->get_where_statement();
139 $where = "WHERE $where";
141 $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT
143 '.$globals->search
->result_fields
.'
146 FROM auth_user_md5 AS u
147 LEFT JOIN auth_user_quick AS q USING(user_id)
148 '.$fields->get_select_statement().'
149 '.(Env
::has('only_referent') ?
' INNER JOIN mentor AS m ON (m.uid = u.user_id)' : '').'
150 LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie")
151 LEFT JOIN contacts AS c ON (c.uid='.S
::v('uid').'
152 AND c.contact=u.user_id)
153 LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id
154 AND w.uid='.S
::v('uid').')
155 '.$globals->search
->result_where_statement
."
158 ORDER BY ".($order?
($order.', '):'')
159 .implode(',',array_filter(array($fields->get_order_statement(),
160 'promo DESC, NomSortKey, prenom'))).'
161 LIMIT '.($offset * $limit).','.$limit;
162 $liste = XDB
::iterator($sql);
163 $res = XDB
::query("SELECT FOUND_ROWS()");
164 $nb_tot = $res->fetchOneCell();
165 return Array($liste, $nb_tot);
168 function handler_quick(&$page)
172 require_once dirname(__FILE__
).'/search/search.inc.php';
174 $page->changeTpl('search/index.tpl');
176 $page->assign('xorg_title','Polytechnique.org - Annuaire');
177 require_once("applis.func.inc.php");
178 require_once("geoloc.inc.php");
180 $page->assign('baseurl', $globals->baseurl
);
182 if (Env
::has('quick')) {
183 $page->assign('formulaire', 0);
185 $search = new XOrgSearch(array($this, 'get_quick'));
186 $search->setNbLines($globals->search
->per_page
);
187 $search->addOrder('score', 'score', false
, 'pertinence', AUTH_PUBLIC
, true
);
189 $nb_tot = $search->show();
191 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
192 new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
193 } elseif ($nb_tot > $globals->search
->private_max
) {
194 new ThrowError('Recherche trop générale');
195 } elseif (empty($nb_tot)) {
196 new ThrowError('il n\'existe personne correspondant à ces critères dans la base !');
199 $res = XDB
::query("SELECT MIN(diminutif), MAX(diminutif)
201 WHERE cat = 'Promotions'");
202 list($min, $max) = $res->fetchOneRow();
203 $page->assign('promo_min', $min);
204 $page->assign('promo_max', $max);
205 $page->assign('formulaire',1);
206 $page->addJsLink('ajax.js');
209 $page->register_modifier('display_lines', 'display_lines');
212 function handler_advanced(&$page, $mode = null
)
216 require_once dirname(__FILE__
).'/search/search.inc.php';
217 require_once 'applis.func.inc.php';
218 require_once 'geoloc.inc.php';
221 $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE
: SKINNED
);
223 $page->assign('advanced',1);
224 $page->assign('public_directory',0);
226 if (!Env
::has('rechercher')) {
227 $this->form_prepare();
229 $search = new XOrgSearch(array($this, 'get_advanced'));
230 $search->setNbLines($globals->search
->per_page
);
232 $page->assign('url_search_form', $search->make_url(Array('rechercher'=>0)));
233 if (!Env
::i('with_soundex')) {
234 $page->assign('with_soundex', $search->make_url(Array()) . "&with_soundex=1");
236 $nb_tot = $search->show();
238 if ($nb_tot > $globals->search
->private_max
) {
239 $this->form_prepare();
240 new ThrowError('Recherche trop générale');
245 $page->addJsLink('ajax.js');
246 $page->register_modifier('display_lines', 'display_lines');
249 function handler_region(&$page, $country = null
)
251 header('Content-Type: text/html; charset="UTF-8"');
252 require_once("geoloc.inc.php");
253 $page->ChangeTpl('search/adv.region.form.tpl', NO_SKIN
);
254 $page->assign('region', "");
255 $page->assign('country', $country);
258 function handler_grade(&$page, $school = null
)
260 header('Content-Type: text/html; charset="UTF-8"');
261 $page->ChangeTpl('search/adv.grade.form.tpl', NO_SKIN
);
262 $page->assign('grade', '');
263 $this->get_diplomas($school);
266 function handler_autocomplete(&$page, $type = null
)
268 // Autocompletion : according to type required, return
269 // a list of results matching with the number of matches.
270 // The output format is :
274 header('Content-Type: text/plain; charset="UTF-8"');
279 $db = 'auth_user_md5';
285 $db = 'binets_def INNER JOIN binets_ins ON(binets_def.id = binets_ins.binet_id)';
286 $field='binets_def.text';
289 $realid = 'binets_def.id';
291 case 'city': $db = 'geoloc_city INNER JOIN adresses ON(geoloc_city.id = adresses.cityid)'; $unique='uid'; $field='geoloc_city.name'; break;
292 case 'entreprise': $db = 'entreprises'; $field = 'entreprise'; $unique='uid'; break;
293 case 'firstname': $field = 'prenom'; break;
295 $db = 'fonctions_def INNER JOIN entreprises ON(entreprises.fonction = fonctions_def.id)';
296 $field = 'fonction_fr';
298 $realid = 'fonctions_def.id';
301 $db = 'groupesx_def INNER JOIN groupesx_ins ON(groupesx_def.id = groupesx_ins.gid)';
302 $field='groupesx_def.text';
305 $realid = 'groupesx_def.id';
308 case 'name': $field = 'nom'; break;
309 case 'nationaliteTxt':
310 $db = 'geoloc_pays INNER JOIN auth_user_md5 ON(geoloc_pays.a2 = auth_user_md5.nationalite)';
311 $field = 'IF(geoloc_pays.nat=\'\', geoloc_pays.pays, geoloc_pays.nat)';
312 $realid = 'geoloc_pays.a2';
314 case 'nickname': $field = 'profile_nick'; $db = 'auth_user_quick'; break;
315 case 'poste': $db = 'entreprises'; $field = 'poste'; $unique='uid'; break;
317 $db = 'emploi_secteur INNER JOIN entreprises ON(entreprises.secteur = emploi_secteur.id)';
318 $field = 'emploi_secteur.label';
319 $realid = 'emploi_secteur.id';
323 $db = 'sections INNER JOIN auth_user_md5 ON(auth_user_md5.section = sections.id)';
324 $field = 'sections.text';
325 $realid = 'sections.id';
330 $list = XDB
::iterator('
333 COUNT(DISTINCT '.$unique.') AS nb
334 '.($realid?
(', '.$realid.' AS id'):'').'
336 WHERE '.$field.' LIKE {?}
340 ($contains?
'%':'').str_replace('*','%',$q).'%');
342 while ($result = $list->next()) {
344 if ($nbResults == 11) {
347 echo $result['field'].'|'.$result['nb'].(isset($result['id'])?
('|'.$result['id']):'')."\n";
354 function handler_list(&$page, $type = null
, $idVal = null
)
356 // Give the list of all values possible of type and builds a select input for it
364 $db = 'fonctions_def';
365 $field = 'fonction_fr';
368 $db = 'groupesx_def';
372 $field = 'IF(nat=\'\', pays, nat)';
379 $db = 'emploi_secteur';
385 header('Content-Type: text/plain; charset="UTF-8"');
386 $result = XDB
::query('SELECT '.$field.' AS field FROM '.$db.' WHERE '.$id.' = {?} LIMIT 1',$idVal);
387 echo $result->fetchOneCell();
389 header('Content-Type: text/xml; charset="UTF-8"');
390 $list = XDB
::iterator('
396 echo '<select name="'.$type.'">';
397 while ($result = $list->next()) {
398 echo '<option value="'.$result['id'].'">'.htmlspecialchars($result['field']).'</option>';
407 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: