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, $action = null
, $subaction = null
)
172 if (Env
::has('quick') ||
$action == 'geoloc') {
173 $page->assign('formulaire', 0);
175 require_once 'userset.inc.php';
176 $view = new SearchSet(true
, $action == 'geoloc' && substr($subaction, -3) == 'swf');
177 $view->addMod('minifiche', 'Minifiches', true
);
178 $view->addMod('trombi', 'Trombinoscope');
179 $view->addMod('geoloc', 'Planishpère');
180 $view->apply('search', $page, $action, $subaction);
182 $nb_tot = $view->count();
186 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
187 new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
188 } elseif ($nb_tot > $globals->search
->private_max
) {
189 new ThrowError('Recherche trop générale');
190 } elseif (empty($nb_tot)) {
191 new ThrowError('il n\'existe personne correspondant à ces critères dans la base !');
194 $res = XDB
::query("SELECT MIN(diminutif), MAX(diminutif)
196 WHERE cat = 'Promotions'");
197 list($min, $max) = $res->fetchOneRow();
198 $page->assign('promo_min', $min);
199 $page->assign('promo_max', $max);
200 $page->assign('formulaire',1);
201 $page->addJsLink('ajax.js');
204 $page->changeTpl('search/index.tpl');
205 $page->assign('xorg_title','Polytechnique.org - Annuaire');
206 $page->assign('baseurl', $globals->baseurl
);
209 function handler_advanced(&$page, $mode = null
)
212 if (!Env
::has('rechercher')) {
213 $this->form_prepare();
215 require_once 'userset.inc.php';
216 $view = new SearchSet(false
, $action == 'geoloc' && substr($subaction, -3) == 'swf');
217 $view->addMod('minifiche', 'Minifiches', true
);
218 $view->addMod('trombi', 'Trombinoscope');
219 $view->addMod('geoloc', 'Planishpère');
220 $view->apply('search', $page, $action, $subaction);
225 $nb_tot = $view->count();
226 if ($nb_tot > $globals->search
->private_max
) {
227 $this->form_prepare();
228 new ThrowError('Recherche trop générale');
232 $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE
: SKINNED
);
233 $page->addJsLink('ajax.js');
234 $page->assign('advanced',1);
235 $page->assign('public_directory',0);
238 function handler_region(&$page, $country = null
)
240 header('Content-Type: text/html; charset="UTF-8"');
241 require_once("geoloc.inc.php");
242 $page->ChangeTpl('search/adv.region.form.tpl', NO_SKIN
);
243 $page->assign('region', "");
244 $page->assign('country', $country);
247 function handler_grade(&$page, $school = null
)
249 header('Content-Type: text/html; charset="UTF-8"');
250 $page->ChangeTpl('search/adv.grade.form.tpl', NO_SKIN
);
251 $page->assign('grade', '');
252 $this->get_diplomas($school);
255 function handler_autocomplete(&$page, $type = null
)
257 // Autocompletion : according to type required, return
258 // a list of results matching with the number of matches.
259 // The output format is :
263 header('Content-Type: text/plain; charset="UTF-8"');
268 $db = 'auth_user_md5';
274 $db = 'binets_def INNER JOIN binets_ins ON(binets_def.id = binets_ins.binet_id)';
275 $field='binets_def.text';
278 $realid = 'binets_def.id';
280 case 'city': $db = 'geoloc_city INNER JOIN adresses ON(geoloc_city.id = adresses.cityid)'; $unique='uid'; $field='geoloc_city.name'; break;
281 case 'entreprise': $db = 'entreprises'; $field = 'entreprise'; $unique='uid'; break;
282 case 'firstname': $field = 'prenom'; break;
284 $db = 'fonctions_def INNER JOIN entreprises ON(entreprises.fonction = fonctions_def.id)';
285 $field = 'fonction_fr';
287 $realid = 'fonctions_def.id';
290 $db = 'groupesx_def INNER JOIN groupesx_ins ON(groupesx_def.id = groupesx_ins.gid)';
291 $field='groupesx_def.text';
294 $realid = 'groupesx_def.id';
297 case 'name': $field = 'nom'; break;
298 case 'nationaliteTxt':
299 $db = 'geoloc_pays INNER JOIN auth_user_md5 ON(geoloc_pays.a2 = auth_user_md5.nationalite)';
300 $field = 'IF(geoloc_pays.nat=\'\', geoloc_pays.pays, geoloc_pays.nat)';
301 $realid = 'geoloc_pays.a2';
303 case 'nickname': $field = 'profile_nick'; $db = 'auth_user_quick'; break;
304 case 'poste': $db = 'entreprises'; $field = 'poste'; $unique='uid'; break;
306 $db = 'emploi_secteur INNER JOIN entreprises ON(entreprises.secteur = emploi_secteur.id)';
307 $field = 'emploi_secteur.label';
308 $realid = 'emploi_secteur.id';
312 $db = 'sections INNER JOIN auth_user_md5 ON(auth_user_md5.section = sections.id)';
313 $field = 'sections.text';
314 $realid = 'sections.id';
319 $list = XDB
::iterator('
322 COUNT(DISTINCT '.$unique.') AS nb
323 '.($realid?
(', '.$realid.' AS id'):'').'
325 WHERE '.$field.' LIKE {?}
329 ($contains?
'%':'').str_replace('*','%',$q).'%');
331 while ($result = $list->next()) {
333 if ($nbResults == 11) {
336 echo $result['field'].'|'.$result['nb'].(isset($result['id'])?
('|'.$result['id']):'')."\n";
343 function handler_list(&$page, $type = null
, $idVal = null
)
345 // Give the list of all values possible of type and builds a select input for it
353 $db = 'fonctions_def';
354 $field = 'fonction_fr';
357 $db = 'groupesx_def';
361 $field = 'IF(nat=\'\', pays, nat)';
368 $db = 'emploi_secteur';
374 header('Content-Type: text/plain; charset="UTF-8"');
375 $result = XDB
::query('SELECT '.$field.' AS field FROM '.$db.' WHERE '.$id.' = {?} LIMIT 1',$idVal);
376 echo $result->fetchOneCell();
378 header('Content-Type: text/xml; charset="UTF-8"');
379 $list = XDB
::iterator('
385 echo '<select name="'.$type.'">';
386 while ($result = $list->next()) {
387 echo '<option value="'.$result['id'].'">'.htmlspecialchars($result['field']).'</option>';
396 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: