less and less global includes \o/
[platal.git] / modules / search.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 class SearchModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
27 'search' => $this->make_hook('quick', AUTH_PUBLIC),
28 'search/adv' => $this->make_hook('advanced', AUTH_COOKIE),
29 'advanced_search.php' => $this->make_hook('redir_advanced', AUTH_PUBLIC),
30 );
31 }
32
33 function handler_redir_advanced(&$page, $mode = null)
34 {
35 pl_redirect('search/adv');
36 exit;
37 }
38
39 function on_subscribe($forlife, $uid, $promo, $pass)
40 {
41 require_once 'user.func.inc.php';
42 user_reindex($uid);
43 }
44
45 function get_quick($offset, $limit, $order)
46 {
47 global $globals;
48 $qSearch = new QuickSearch('quick');
49 $fields = new SFieldGroup(true, array($qSearch));
50
51 if ($qSearch->isempty()) {
52 new ThrowError('Recherche trop générale.');
53 }
54
55 $sql = 'SELECT SQL_CALC_FOUND_ROWS
56 UPPER(IF(u.nom!="",u.nom,u.nom_ini)) AS nom,
57 IF(u.prenom!="",u.prenom,u.prenom_ini) AS prenom,
58 '.$globals->search->result_fields.'
59 c.uid AS contact, w.ni_id AS watch,
60 '.$qSearch->get_score_statement().'
61 FROM auth_user_md5 AS u
62 '.$fields->get_select_statement().'
63 LEFT JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
64 LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie")
65 LEFT JOIN contacts AS c ON (c.uid='.S::i('uid', -1).'
66 AND c.contact=u.user_id)
67 LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id
68 AND w.uid='.S::i('uid', -1).')
69 '.$globals->search->result_where_statement.'
70 WHERE '.$fields->get_where_statement()
71 .(S::logged() && Env::has('nonins') ? ' AND u.perms="pending" AND u.deces=0' : '')
72 .'
73 GROUP BY u.user_id
74 ORDER BY '.($order?($order.', '):'')
75 .implode(',',array_filter(array($fields->get_order_statement(),
76 'u.promo DESC, NomSortKey, prenom'))).'
77 LIMIT '.$offset * $globals->search->per_page.','
78 .$globals->search->per_page;
79 $list = XDB::iterator($sql);
80 $res = XDB::query("SELECT FOUND_ROWS()");
81 $nb_tot = $res->fetchOneCell();
82 return array($list, $nb_tot);
83 }
84
85 function form_prepare()
86 {
87 global $page;
88
89 $page->assign('formulaire',1);
90 $page->assign('choix_nats',
91 XDB::iterator('SELECT a2 AS id,IF(nat=\'\',pays,nat) AS text
92 FROM geoloc_pays ORDER BY text'));
93 $page->assign('choix_postes',
94 XDB::iterator('SELECT id,fonction_fr FROM fonctions_def
95 ORDER BY fonction_fr'));
96 $page->assign('choix_binets',
97 XDB::iterator('SELECT id,text FROM binets_def ORDER BY text'));
98 $page->assign('choix_groupesx',
99 XDB::iterator('SELECT id,text FROM groupesx_def ORDER BY text'));
100 $page->assign('choix_sections',
101 XDB::iterator('SELECT id,text FROM sections ORDER BY text'));
102 $page->assign('choix_schools',
103 XDB::iterator('SELECT id,text FROM applis_def ORDER BY text'));
104 $page->assign('choix_secteurs',
105 XDB::iterator('SELECT id,label FROM emploi_secteur ORDER BY label'));
106
107 if (Env::has('school')) {
108 $sql = 'SELECT type FROM applis_def WHERE id='.Env::i('school');
109 } else {
110 $sql = 'DESCRIBE applis_def type';
111 }
112
113 $res = XDB::query($sql);
114 $row = $res->fetchOneRow();
115 if (Env::has('school')) {
116 $types = $row[0];
117 } else {
118 $types = explode('(',$row[1]);
119 $types = str_replace("'","",substr($types[1],0,-1));
120 }
121 $page->assign('choix_diplomas', explode(',',$types));
122 }
123
124 function get_advanced($offset, $limit, $order)
125 {
126 $fields = new SFieldGroup(true, advancedSearchFromInput());
127 if ($fields->too_large()) {
128 $this->form_prepare();
129 new ThrowError('Recherche trop générale.');
130 }
131 global $globals, $page;
132
133 $page->assign('search_vars', $fields->get_url());
134
135 $where = $fields->get_where_statement();
136 if ($where) {
137 $where = "WHERE $where";
138 }
139 $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT
140 u.nom, u.prenom,
141 '.$globals->search->result_fields.'
142 c.uid AS contact,
143 w.ni_id AS watch
144 FROM auth_user_md5 AS u
145 LEFT JOIN auth_user_quick AS q USING(user_id)
146 '.$fields->get_select_statement().'
147 '.(Env::has('only_referent') ? ' INNER JOIN mentor AS m ON (m.uid = u.user_id)' : '').'
148 LEFT JOIN aliases AS a ON (u.user_id = a.id AND a.type="a_vie")
149 LEFT JOIN contacts AS c ON (c.uid='.S::v('uid').'
150 AND c.contact=u.user_id)
151 LEFT JOIN watch_nonins AS w ON (w.ni_id=u.user_id
152 AND w.uid='.S::v('uid').')
153 '.$globals->search->result_where_statement."
154 $where
155 GROUP BY u.user_id
156 ORDER BY ".($order?($order.', '):'')
157 .implode(',',array_filter(array($fields->get_order_statement(),
158 'promo DESC, NomSortKey, prenom'))).'
159 LIMIT '.($offset * $limit).','.$limit;
160 $liste = XDB::iterator($sql);
161 $res = XDB::query("SELECT FOUND_ROWS()");
162 $nb_tot = $res->fetchOneCell();
163 return Array($liste, $nb_tot);
164 }
165
166 function handler_quick(&$page)
167 {
168 global $globals;
169
170 require_once dirname(__FILE__).'/search/search.inc.php';
171
172 $page->changeTpl('search/index.tpl');
173
174 $page->assign('xorg_title','Polytechnique.org - Annuaire');
175 require_once("applis.func.inc.php");
176 require_once("geoloc.inc.php");
177
178 $page->assign('baseurl', $globals->baseurl);
179
180 if (Env::has('quick')) {
181 $page->assign('formulaire', 0);
182
183 $search = new XOrgSearch(array($this, 'get_quick'));
184 $search->setNbLines($globals->search->per_page);
185 $search->addOrder('score', 'score', false, 'pertinence', AUTH_PUBLIC, true);
186
187 $nb_tot = $search->show();
188
189 if (!S::logged() && $nb_tot > $globals->search->public_max) {
190 new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
191 } elseif ($nb_tot > $globals->search->private_max) {
192 new ThrowError('Recherche trop générale');
193 } elseif (empty($nb_tot)) {
194 new ThrowError('il n\'existe personne correspondant à ces critères dans la base !');
195 }
196 } else {
197 $page->assign('formulaire',1);
198 }
199
200 $page->register_modifier('display_lines', 'display_lines');
201 }
202
203 function handler_advanced(&$page, $mode = null)
204 {
205 global $globals;
206
207 require_once dirname(__FILE__).'/search/search.inc.php';
208 require_once 'applis.func.inc.php';
209 require_once 'geoloc.inc.php';
210
211
212 $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE : SKINNED);
213
214 $page->assign('advanced',1);
215 $page->assign('public_directory',0);
216
217 if (!Env::has('rechercher')) {
218 $this->form_prepare();
219 } else {
220
221 $search = new XOrgSearch(array($this, 'get_advanced'));
222 $search->setNbLines($globals->search->per_page);
223
224 $page->assign('url_search_form', $search->make_url(Array('rechercher'=>0)));
225 if (Env::has('with_soundex')) {
226 $page->assign('with_soundex', $search->make_url(Array())."&with_soundex=1");
227 }
228
229 $nb_tot = $search->show();
230
231 if ($nb_tot > $globals->search->private_max) {
232 $this->form_prepare();
233 new ThrowError('Recherche trop générale');
234 }
235
236 }
237
238 $page->register_modifier('display_lines', 'display_lines');
239 }
240 }
241
242 ?>