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_PUBLIC
),
36 function handler_redir_advanced(&$page, $mode = null
)
38 pl_redirect('search/adv');
42 function on_subscribe($forlife, $uid, $promo, $pass)
44 require_once 'user.func.inc.php';
48 function get_quick($offset, $limit, $order)
52 Env
::kill('with_soundex');
54 $qSearch = new QuickSearch('quick');
55 $fields = new SFieldGroup(true
, array($qSearch));
57 if ($qSearch->isempty()) {
58 new ThrowError('Recherche trop générale.');
61 $sql = 'SELECT SQL_CALC_FOUND_ROWS
62 UPPER(IF(u.nom!="",u.nom,u.nom_ini)) AS nom,
63 IF(u.prenom!="",u.prenom,u.prenom_ini) AS prenom,
64 '.$globals->search
->result_fields
.'
65 c.uid AS contact, w.ni_id AS watch,
66 '.$qSearch->get_score_statement().'
67 FROM auth_user_md5 AS u
68 '.$fields->get_select_statement().'
69 LEFT JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
70 LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie")
71 LEFT JOIN contacts AS c ON (c.uid='.S
::i('uid', -1).'
72 AND c.contact=u.user_id)
73 LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id
74 AND w.uid='.S
::i('uid', -1).')
75 '.$globals->search
->result_where_statement
.'
76 WHERE '.$fields->get_where_statement()
77 .(S
::logged() && Env
::has('nonins') ?
' AND u.perms="pending" AND u.deces=0' : '')
80 ORDER BY '.($order?
($order.', '):'')
81 .implode(',',array_filter(array($fields->get_order_statement(),
82 'u.promo DESC, NomSortKey, prenom'))).'
83 LIMIT '.$offset * $globals->search
->per_page
.','
84 .$globals->search
->per_page
;
85 $list = XDB
::iterator($sql);
86 $res = XDB
::query("SELECT FOUND_ROWS()");
87 $nb_tot = $res->fetchOneCell();
88 return array($list, $nb_tot);
91 function form_prepare()
95 $page->assign('formulaire',1);
96 $page->assign('choix_nats',
97 XDB
::iterator('SELECT g.a2 AS id, IF(nat=\'\', g.pays, g.nat) AS text
99 INNER JOIN auth_user_md5 AS u ON (u.nationalite = g.a2)
102 $page->assign('choix_postes',
103 XDB
::iterator('SELECT id,fonction_fr FROM fonctions_def
104 ORDER BY fonction_fr'));
105 $page->assign('choix_binets',
106 XDB
::iterator('SELECT id,text FROM binets_def ORDER BY text'));
107 $page->assign('choix_groupesx',
108 XDB
::iterator('SELECT id,text FROM groupesx_def ORDER BY text'));
109 $page->assign('choix_sections',
110 XDB
::iterator('SELECT id,text FROM sections ORDER BY text'));
111 $page->assign('choix_schools',
112 XDB
::iterator('SELECT id,text FROM applis_def ORDER BY text'));
113 $page->assign('choix_secteurs',
114 XDB
::iterator('SELECT id,label FROM emploi_secteur ORDER BY label'));
115 $this->get_diplomas();
118 function get_diplomas($school = null
)
120 if (is_null($school) && Env
::has('school')) {
121 $school = Env
::i('school');
124 if (!is_null($school)) {
125 $sql = 'SELECT type FROM applis_def WHERE id=' . $school;
127 $sql = 'DESCRIBE applis_def type';
130 $res = XDB
::query($sql);
131 $row = $res->fetchOneRow();
132 if (!is_null($school)) {
135 $types = explode('(',$row[1]);
136 $types = str_replace("'","",substr($types[1],0,-1));
139 $page->assign('choix_diplomas', explode(',',$types));
142 function get_advanced($offset, $limit, $order)
144 $fields = new SFieldGroup(true
, advancedSearchFromInput());
145 if ($fields->too_large()) {
146 $this->form_prepare();
147 new ThrowError('Recherche trop générale.');
149 global $globals, $page;
151 $page->assign('search_vars', $fields->get_url());
153 $where = $fields->get_where_statement();
155 $where = "WHERE $where";
157 $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT
159 '.$globals->search
->result_fields
.'
162 FROM auth_user_md5 AS u
163 LEFT JOIN auth_user_quick AS q USING(user_id)
164 '.$fields->get_select_statement().'
165 '.(Env
::has('only_referent') ?
' INNER JOIN mentor AS m ON (m.uid = u.user_id)' : '').'
166 LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie")
167 LEFT JOIN contacts AS c ON (c.uid='.S
::v('uid').'
168 AND c.contact=u.user_id)
169 LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id
170 AND w.uid='.S
::v('uid').')
171 '.$globals->search
->result_where_statement
."
174 ORDER BY ".($order?
($order.', '):'')
175 .implode(',',array_filter(array($fields->get_order_statement(),
176 'promo DESC, NomSortKey, prenom'))).'
177 LIMIT '.($offset * $limit).','.$limit;
178 $liste = XDB
::iterator($sql);
179 $res = XDB
::query("SELECT FOUND_ROWS()");
180 $nb_tot = $res->fetchOneCell();
181 return Array($liste, $nb_tot);
184 function handler_quick(&$page)
188 require_once dirname(__FILE__
).'/search/search.inc.php';
190 $page->changeTpl('search/index.tpl');
192 $page->assign('xorg_title','Polytechnique.org - Annuaire');
193 require_once("applis.func.inc.php");
194 require_once("geoloc.inc.php");
196 $page->assign('baseurl', $globals->baseurl
);
198 if (Env
::has('quick')) {
199 $page->assign('formulaire', 0);
201 $search = new XOrgSearch(array($this, 'get_quick'));
202 $search->setNbLines($globals->search
->per_page
);
203 $search->addOrder('score', 'score', false
, 'pertinence', AUTH_PUBLIC
, true
);
205 $nb_tot = $search->show();
207 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
208 new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
209 } elseif ($nb_tot > $globals->search
->private_max
) {
210 new ThrowError('Recherche trop générale');
211 } elseif (empty($nb_tot)) {
212 new ThrowError('il n\'existe personne correspondant à ces critères dans la base !');
215 $res = XDB
::query("SELECT MIN(diminutif), MAX(diminutif)
217 WHERE cat = 'Promotions'");
218 list($min, $max) = $res->fetchOneRow();
219 $page->assign('promo_min', $min);
220 $page->assign('promo_max', $max);
221 $page->assign('formulaire',1);
222 $page->addJsLink('ajax.js');
225 $page->register_modifier('display_lines', 'display_lines');
228 function handler_advanced(&$page, $mode = null
)
232 require_once dirname(__FILE__
).'/search/search.inc.php';
233 require_once 'applis.func.inc.php';
234 require_once 'geoloc.inc.php';
237 $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE
: SKINNED
);
239 $page->assign('advanced',1);
240 $page->assign('public_directory',0);
242 if (!Env
::has('rechercher')) {
243 $this->form_prepare();
245 $search = new XOrgSearch(array($this, 'get_advanced'));
246 $search->setNbLines($globals->search
->per_page
);
248 $page->assign('url_search_form', $search->make_url(Array('rechercher'=>0)));
249 if (!Env
::i('with_soundex')) {
250 $page->assign('with_soundex', $search->make_url(Array()) . "&with_soundex=1");
252 $nb_tot = $search->show();
254 if ($nb_tot > $globals->search
->private_max
) {
255 $this->form_prepare();
256 new ThrowError('Recherche trop générale');
261 $page->addJsLink('ajax.js');
262 $page->register_modifier('display_lines', 'display_lines');
265 function handler_region(&$page, $country = null
)
267 header('Content-Type: text/html; charset="UTF-8"');
268 require_once("geoloc.inc.php");
269 $page->ChangeTpl('search/adv.region.form.tpl', NO_SKIN
);
270 $page->assign('region', "");
271 $page->assign('country', $country);
274 function handler_grade(&$page, $school = null
)
276 header('Content-Type: text/html; charset="UTF-8"');
277 $page->ChangeTpl('search/adv.grade.form.tpl', NO_SKIN
);
278 $page->assign('grade', '');
279 $this->get_diplomas($school);
282 function handler_autocomplete(&$page, $type = null
)
284 // Autocompletion : according to type required, return
285 // a list of results matching with the number of matches.
286 // The output format is :
290 header('Content-Type: text/plain; charset="UTF-8"');
294 $db = 'auth_user_md5';
296 case 'firstname': $field = 'prenom'; break;
297 case 'name': $field = 'nom'; break;
298 case 'nickname': $field = 'profile_nick'; $db = 'auth_user_quick'; break;
299 case 'entreprise': $db = 'entreprises'; $field = 'entreprise'; $unique='uid'; break;
303 $liste = XDB
::iterator('SELECT '.$field.' AS field, COUNT(DISTINCT '.$unique.') AS nb FROM '.$db.' WHERE '.$field.' LIKE {?} GROUP BY '.$field.' LIMIT 11', $q.'%');
305 while ($result = $liste->next()) {
307 if ($nbResults == 11) {
310 echo $result['field'].'|'.$result['nb']."\n";
318 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: