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