2 /***************************************************************************
3 * Copyright (C) 2003-2006 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
, '', NO_AUTH
),
30 'search/ajax/grade' => $this->make_hook('grade', AUTH_COOKIE
, '', NO_AUTH
),
31 'advanced_search.php' => $this->make_hook('redir_advanced', AUTH_PUBLIC
),
35 function handler_redir_advanced(&$page, $mode = null
)
37 pl_redirect('search/adv');
41 function on_subscribe($forlife, $uid, $promo, $pass)
43 require_once 'user.func.inc.php';
47 function get_quick($offset, $limit, $order)
50 $qSearch = new QuickSearch('quick');
51 $fields = new SFieldGroup(true
, array($qSearch));
53 if ($qSearch->isempty()) {
54 new ThrowError('Recherche trop générale.');
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' : '')
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);
87 function form_prepare()
91 $page->assign('formulaire',1);
92 $page->assign('choix_nats',
93 XDB
::iterator('SELECT a2 AS id,IF(nat=\'\',pays,nat) AS text
94 FROM geoloc_pays ORDER BY text'));
95 $page->assign('choix_postes',
96 XDB
::iterator('SELECT id,fonction_fr FROM fonctions_def
97 ORDER BY fonction_fr'));
98 $page->assign('choix_binets',
99 XDB
::iterator('SELECT id,text FROM binets_def ORDER BY text'));
100 $page->assign('choix_groupesx',
101 XDB
::iterator('SELECT id,text FROM groupesx_def ORDER BY text'));
102 $page->assign('choix_sections',
103 XDB
::iterator('SELECT id,text FROM sections ORDER BY text'));
104 $page->assign('choix_schools',
105 XDB
::iterator('SELECT id,text FROM applis_def ORDER BY text'));
106 $page->assign('choix_secteurs',
107 XDB
::iterator('SELECT id,label FROM emploi_secteur ORDER BY label'));
108 $this->get_diplomas();
111 function get_diplomas($school = null
)
113 if (is_null($school) && Env
::has('school')) {
114 $school = Env
::i('school');
117 if (!is_null($school)) {
118 $sql = 'SELECT type FROM applis_def WHERE id=' . $school;
120 $sql = 'DESCRIBE applis_def type';
123 $res = XDB
::query($sql);
124 $row = $res->fetchOneRow();
125 if (!is_null($school)) {
128 $types = explode('(',$row[1]);
129 $types = str_replace("'","",substr($types[1],0,-1));
132 $page->assign('choix_diplomas', explode(',',$types));
135 function get_advanced($offset, $limit, $order)
137 $fields = new SFieldGroup(true
, advancedSearchFromInput());
138 if ($fields->too_large()) {
139 $this->form_prepare();
140 new ThrowError('Recherche trop générale.');
142 global $globals, $page;
144 $page->assign('search_vars', $fields->get_url());
146 $where = $fields->get_where_statement();
148 $where = "WHERE $where";
150 $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT
152 '.$globals->search
->result_fields
.'
155 FROM auth_user_md5 AS u
156 LEFT JOIN auth_user_quick AS q USING(user_id)
157 '.$fields->get_select_statement().'
158 '.(Env
::has('only_referent') ?
' INNER JOIN mentor AS m ON (m.uid = u.user_id)' : '').'
159 LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie")
160 LEFT JOIN contacts AS c ON (c.uid='.S
::v('uid').'
161 AND c.contact=u.user_id)
162 LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id
163 AND w.uid='.S
::v('uid').')
164 '.$globals->search
->result_where_statement
."
167 ORDER BY ".($order?
($order.', '):'')
168 .implode(',',array_filter(array($fields->get_order_statement(),
169 'promo DESC, NomSortKey, prenom'))).'
170 LIMIT '.($offset * $limit).','.$limit;
171 $liste = XDB
::iterator($sql);
172 $res = XDB
::query("SELECT FOUND_ROWS()");
173 $nb_tot = $res->fetchOneCell();
174 return Array($liste, $nb_tot);
177 function handler_quick(&$page)
181 require_once dirname(__FILE__
).'/search/search.inc.php';
183 $page->changeTpl('search/index.tpl');
185 $page->assign('xorg_title','Polytechnique.org - Annuaire');
186 require_once("applis.func.inc.php");
187 require_once("geoloc.inc.php");
189 $page->assign('baseurl', $globals->baseurl
);
191 if (Env
::has('quick')) {
192 $page->assign('formulaire', 0);
194 $search = new XOrgSearch(array($this, 'get_quick'));
195 $search->setNbLines($globals->search
->per_page
);
196 $search->addOrder('score', 'score', false
, 'pertinence', AUTH_PUBLIC
, true
);
198 $nb_tot = $search->show();
200 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
201 new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
202 } elseif ($nb_tot > $globals->search
->private_max
) {
203 new ThrowError('Recherche trop générale');
204 } elseif (empty($nb_tot)) {
205 new ThrowError('il n\'existe personne correspondant à ces critères dans la base !');
208 $page->assign('formulaire',1);
211 $page->register_modifier('display_lines', 'display_lines');
214 function handler_advanced(&$page, $mode = null
)
218 require_once dirname(__FILE__
).'/search/search.inc.php';
219 require_once 'applis.func.inc.php';
220 require_once 'geoloc.inc.php';
223 $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE
: SKINNED
);
225 $page->assign('advanced',1);
226 $page->assign('public_directory',0);
228 if (!Env
::has('rechercher')) {
229 $this->form_prepare();
232 $search = new XOrgSearch(array($this, 'get_advanced'));
233 $search->setNbLines($globals->search
->per_page
);
235 $page->assign('url_search_form', $search->make_url(Array('rechercher'=>0)));
236 if (Env
::has('with_soundex')) {
237 $page->assign('with_soundex', $search->make_url(Array())."&with_soundex=1");
240 $nb_tot = $search->show();
242 if ($nb_tot > $globals->search
->private_max
) {
243 $this->form_prepare();
244 new ThrowError('Recherche trop générale');
249 $page->addJsLink('ajax.js');
250 $page->register_modifier('display_lines', 'display_lines');
253 function handler_region(&$page, $country = null
)
255 header('Content-Type: text/html; charset="iso-8859-15"');
256 require_once("geoloc.inc.php");
257 $page->ChangeTpl('search/adv.region.form.tpl', NO_SKIN
);
258 $page->assign('region', "");
259 $page->assign('country', $country);
262 function handler_grade(&$page, $school = null
)
264 header('Content-Type: text/html; charset="iso-8859-15"');
265 $page->ChangeTpl('search/adv.grade.form.tpl', NO_SKIN
);
266 $page->assign('grade', '');
267 $this->get_diplomas($school);