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
),
32 function get_quick($offset, $limit, $order)
35 $qSearch = new QuickSearch('quick');
36 $fields = new SFieldGroup(true
, array($qSearch));
38 if ($qSearch->isempty()) {
39 new ThrowError('Recherche trop générale.');
42 $sql = 'SELECT SQL_CALC_FOUND_ROWS
43 UPPER(IF(u.nom!="",u.nom,u.nom_ini)) AS nom,
44 IF(u.prenom!="",u.prenom,u.prenom_ini) AS prenom,
45 '.$globals->search
->result_fields
.'
46 c.uid AS contact, w.ni_id AS watch,
47 '.$qSearch->get_score_statement().'
48 FROM auth_user_md5 AS u
49 '.$fields->get_select_statement().'
50 LEFT JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
51 LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie")
52 LEFT JOIN contacts AS c ON (c.uid='.S
::v('uid').'
53 AND c.contact=u.user_id)
54 LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id
55 AND w.uid='.S
::v('uid').')
56 '.$globals->search
->result_where_statement
.'
57 WHERE '.$fields->get_where_statement()
58 .(S
::logged() && Env
::has('nonins') ?
' AND u.perms="pending" AND u.deces=0' : '')
61 ORDER BY '.($order?
($order.', '):'')
62 .implode(',',array_filter(array($fields->get_order_statement(),
63 'u.promo DESC, NomSortKey, prenom'))).'
64 LIMIT '.$offset * $globals->search
->per_page
.','
65 .$globals->search
->per_page
;
66 $list = XDB
::iterator($sql);
67 $res = XDB
::query("SELECT FOUND_ROWS()");
68 $nb_tot = $res->fetchOneCell();
69 return array($list, $nb_tot);
72 function form_prepare()
74 $page->assign('formulaire',1);
75 $page->assign('choix_nats',
76 XDB
::iterator('SELECT a2 AS id,IF(nat=\'\',pays,nat) AS text
77 FROM geoloc_pays ORDER BY text'));
78 $page->assign('choix_postes',
79 XDB
::iterator('SELECT id,fonction_fr FROM fonctions_def
80 ORDER BY fonction_fr'));
81 $page->assign('choix_binets',
82 XDB
::iterator('SELECT id,text FROM binets_def ORDER BY text'));
83 $page->assign('choix_groupesx',
84 XDB
::iterator('SELECT id,text FROM groupesx_def ORDER BY text'));
85 $page->assign('choix_sections',
86 XDB
::iterator('SELECT id,text FROM sections ORDER BY text'));
87 $page->assign('choix_schools',
88 XDB
::iterator('SELECT id,text FROM applis_def ORDER BY text'));
89 $page->assign('choix_secteurs',
90 XDB
::iterator('SELECT id,label FROM emploi_secteur ORDER BY label'));
92 if (Env
::has('school')) {
93 $sql = 'SELECT type FROM applis_def WHERE id='.Env
::getInt('school');
95 $sql = 'DESCRIBE applis_def type';
98 $res = XDB
::query($sql);
99 $row = $res->fetchOneRow();
100 if (Env
::has('school')) {
103 $types = explode('(',$row[1]);
104 $types = str_replace("'","",substr($types[1],0,-1));
106 $page->assign('choix_diplomas', explode(',',$types));
109 function get_advanced($offset, $limit, $order)
111 $fields = new SFieldGroup(true
, advancedSearchFromInput());
112 if ($fields->too_large()) {
113 $this->form_prepare();
114 new ThrowError('Recherche trop générale.');
116 global $globals, $page;
118 $page->assign('search_vars', $fields->get_url());
120 $where = $fields->get_where_statement();
122 $where = "WHERE $where";
124 $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT
126 '.$globals->search
->result_fields
.'
129 FROM auth_user_md5 AS u
130 LEFT JOIN auth_user_quick AS q USING(user_id)
131 '.$fields->get_select_statement().'
132 '.(Env
::has('only_referent') ?
' INNER JOIN mentor AS m ON (m.uid = u.user_id)' : '').'
133 LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie")
134 LEFT JOIN contacts AS c ON (c.uid='.S
::v('uid').'
135 AND c.contact=u.user_id)
136 LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id
137 AND w.uid='.S
::v('uid').')
138 '.$globals->search
->result_where_statement
."
140 ORDER BY ".($order?
($order.', '):'')
141 .implode(',',array_filter(array($fields->get_order_statement(),
142 'promo DESC, NomSortKey, prenom'))).'
143 LIMIT '.($offset * $limit).','.$limit;
144 $liste = XDB
::iterator($sql);
145 $res = XDB
::query("SELECT FOUND_ROWS()");
146 $nb_tot = $res->fetchOneCell();
147 return Array($liste, $nb_tot);
150 function handler_quick(&$page)
154 require_once 'search.inc.php';
156 $page->changeTpl('search/index.tpl');
158 $page->assign('xorg_title','Polytechnique.org - Annuaire');
159 require_once("applis.func.inc.php");
160 require_once("geoloc.inc.php");
162 $page->assign('baseurl', $globals->baseurl
);
164 if (Env
::has('quick')) {
165 $page->assign('formulaire', 0);
167 $search = new XOrgSearch(array($this, 'get_quick'));
168 $search->setNbLines($globals->search
->per_page
);
169 $search->addOrder('score', 'score', false
, 'pertinence', AUTH_PUBLIC
, true
);
171 $nb_tot = $search->show();
173 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
174 new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
175 } elseif ($nb_tot > $globals->search
->private_max
) {
176 new ThrowError('Recherche trop générale');
177 } elseif (empty($nb_tot)) {
178 new ThrowError('il n\'existe personne correspondant à ces critères dans la base !');
181 $page->assign('formulaire',1);
184 $page->register_modifier('display_lines', 'display_lines');
187 function handler_advanced(&$page, $mode = null
)
191 require_once 'search.inc.php' ;
192 require_once 'applis.func.inc.php';
193 require_once 'geoloc.inc.php';
196 $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE
: SKINNED
);
198 $page->assign('advanced',1);
199 $page->assign('public_directory',0);
200 $page->assign('use_map', $globals->geoloc
->use_map());
202 if (!Env
::has('rechercher')) {
203 $this->form_prepare();
206 $search = new XOrgSearch(array($this, 'get_advanced'));
207 $search->setNbLines($globals->search
->per_page
);
209 $page->assign('url_search_form', $search->make_url(Array('rechercher'=>0)));
210 if (Env
::has('with_soundex')) {
211 $page->assign('with_soundex', $search->make_url(Array())."&with_soundex=1");
214 $nb_tot = $search->show();
216 if ($nb_tot > $globals->search
->private_max
) {
217 $this->form_prepare();
218 new ThrowError('Recherche trop générale');
223 $page->register_modifier('display_lines', 'display_lines');