Table editor fills the new entry form with the default values of the fields
[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 '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),
32 );
33 }
34
35 function handler_redir_advanced(&$page, $mode = null)
36 {
37 pl_redirect('search/adv');
38 exit;
39 }
40
41 function on_subscribe($forlife, $uid, $promo, $pass)
42 {
43 require_once 'user.func.inc.php';
44 user_reindex($uid);
45 }
46
47 function get_quick($offset, $limit, $order)
48 {
49 global $globals;
50 $qSearch = new QuickSearch('quick');
51 $fields = new SFieldGroup(true, array($qSearch));
52
53 if ($qSearch->isempty()) {
54 new ThrowError('Recherche trop générale.');
55 }
56
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' : '')
74 .'
75 GROUP BY u.user_id
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);
85 }
86
87 function form_prepare()
88 {
89 global $page;
90
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();
109 }
110
111 function get_diplomas($school = null)
112 {
113 if (is_null($school) && Env::has('school')) {
114 $school = Env::i('school');
115 }
116
117 if (!is_null($school)) {
118 $sql = 'SELECT type FROM applis_def WHERE id=' . $school;
119 } else {
120 $sql = 'DESCRIBE applis_def type';
121 }
122
123 $res = XDB::query($sql);
124 $row = $res->fetchOneRow();
125 if (!is_null($school)) {
126 $types = $row[0];
127 } else {
128 $types = explode('(',$row[1]);
129 $types = str_replace("'","",substr($types[1],0,-1));
130 }
131 global $page;
132 $page->assign('choix_diplomas', explode(',',$types));
133 }
134
135 function get_advanced($offset, $limit, $order)
136 {
137 $fields = new SFieldGroup(true, advancedSearchFromInput());
138 if ($fields->too_large()) {
139 $this->form_prepare();
140 new ThrowError('Recherche trop générale.');
141 }
142 global $globals, $page;
143
144 $page->assign('search_vars', $fields->get_url());
145
146 $where = $fields->get_where_statement();
147 if ($where) {
148 $where = "WHERE $where";
149 }
150 $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT
151 u.nom, u.prenom,
152 '.$globals->search->result_fields.'
153 c.uid AS contact,
154 w.ni_id AS watch
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."
165 $where
166 GROUP BY u.user_id
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);
175 }
176
177 function handler_quick(&$page)
178 {
179 global $globals;
180
181 require_once dirname(__FILE__).'/search/search.inc.php';
182
183 $page->changeTpl('search/index.tpl');
184
185 $page->assign('xorg_title','Polytechnique.org - Annuaire');
186 require_once("applis.func.inc.php");
187 require_once("geoloc.inc.php");
188
189 $page->assign('baseurl', $globals->baseurl);
190
191 if (Env::has('quick')) {
192 $page->assign('formulaire', 0);
193
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);
197
198 $nb_tot = $search->show();
199
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 !');
206 }
207 } else {
208 $page->assign('formulaire',1);
209 }
210
211 $page->register_modifier('display_lines', 'display_lines');
212 }
213
214 function handler_advanced(&$page, $mode = null)
215 {
216 global $globals;
217
218 require_once dirname(__FILE__).'/search/search.inc.php';
219 require_once 'applis.func.inc.php';
220 require_once 'geoloc.inc.php';
221
222
223 $page->changeTpl('search/index.tpl', $mode == 'mini' ? SIMPLE : SKINNED);
224
225 $page->assign('advanced',1);
226 $page->assign('public_directory',0);
227
228 if (!Env::has('rechercher')) {
229 $this->form_prepare();
230 } else {
231
232 $search = new XOrgSearch(array($this, 'get_advanced'));
233 $search->setNbLines($globals->search->per_page);
234
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");
238 }
239
240 $nb_tot = $search->show();
241
242 if ($nb_tot > $globals->search->private_max) {
243 $this->form_prepare();
244 new ThrowError('Recherche trop générale');
245 }
246
247 }
248
249 $page->addJsLink('ajax.js');
250 $page->register_modifier('display_lines', 'display_lines');
251 }
252
253 function handler_region(&$page, $country = null)
254 {
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);
260 }
261
262 function handler_grade(&$page, $school = null)
263 {
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);
268 }
269 }
270
271 ?>