Cleanup and fix email/send.
[platal.git] / modules / search.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 'search/autocomplete' => $this->make_hook('autocomplete', AUTH_COOKIE, 'user', NO_AUTH),
31 'search/list' => $this->make_hook('list', AUTH_COOKIE, 'user', NO_AUTH),
32 );
33 }
34
35 function handler_redir_advanced(&$page, $mode = null)
36 {
37 pl_redirect('search/adv');
38 exit;
39 }
40
41 function form_prepare()
42 {
43 Platal::page()->assign('formulaire',1);
44 }
45
46 function handler_quick(&$page, $action = null, $subaction = null)
47 {
48 global $globals;
49
50 $res = XDB::query("SELECT MIN(diminutif), MAX(diminutif)
51 FROM groups
52 WHERE cat = 'Promotions'");
53 list($min, $max) = $res->fetchOneRow();
54 $page->assign('promo_min', $min);
55 $page->assign('promo_max', $max);
56
57 if (Env::has('quick') || $action == 'geoloc') {
58 $quick = trim(Env::v('quick'));
59 if (S::logged() && !Env::has('page')) {
60 S::logger()->log('search', 'quick=' . $quick);
61 }
62 $list = 'profile|prf|fiche|fic|referent|ref|mentor';
63 if (S::admin()) {
64 $list .= '|admin|adm|ax';
65 }
66 if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.\d{2,4})?)?)$/', replace_accent($quick), $matches)) {
67 $login = $matches[2];
68 switch($matches[1]) {
69 case 'admin': case 'adm':
70 $base = 'admin/user/';
71 break;
72 case 'ax':
73 $base = 'profile/ax/';
74 break;
75 case 'profile': case 'prf': case 'fiche': case 'fic':
76 $base = 'profile/';
77 break;
78 case 'referent': case 'ref': case 'mentor':
79 $base = 'referent/';
80 break;
81 }
82
83 $user = User::getSilent($login);
84 if ($user) {
85 pl_redirect($base . $user->login());
86 }
87 $_REQUEST['quick'] = $login;
88 $_GET['quick'] = $login;
89 } elseif (strpos($quick, 'doc:') === 0) {
90 $url = 'Docs/Recherche?';
91 $url .= 'action=search&q=' . urlencode(substr($quick, 4));
92 $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review');
93 pl_redirect($url);
94 }
95
96 $page->assign('formulaire', 0);
97
98 require_once 'userset.inc.php';
99 $view = new SearchSet(true, $action == 'geoloc' && substr($subaction, -3) == 'swf');
100 $view->addMod('minifiche', 'Mini-fiches', true, array('with_score' => true));
101 if (S::logged() && !Env::i('nonins')) {
102 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true, 'with_score' => true));
103 // TODO: Reactivate when the new map is completed.
104 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
105 }
106 $view->apply('search', $page, $action, $subaction);
107
108 $nb_tot = $view->count();
109 $page->assign('search_results_nb', $nb_tot);
110 if ($subaction) {
111 return;
112 }
113 if (!S::logged() && $nb_tot > $globals->search->public_max) {
114 $page->trigError('Votre recherche a généré trop de résultats pour un affichage public.');
115 } elseif ($nb_tot > $globals->search->private_max) {
116 $page->trigError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.');
117 } elseif (empty($nb_tot)) {
118 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
119 }
120 } else {
121 $page->assign('formulaire',1);
122 $page->addJsLink('ajax.js');
123 }
124
125 $page->changeTpl('search/index.tpl');
126 $page->setTitle('Annuaire');
127 }
128
129 function handler_advanced(&$page, $action = null, $subaction = null)
130 {
131 global $globals;
132 require_once 'geocoding.inc.php';
133 $page->assign('advanced',1);
134 $page->addJsLink('jquery.autocomplete.js');
135
136 if (!Env::has('rechercher') && $action != 'geoloc') {
137 $this->form_prepare();
138 } else {
139 if (!Env::has('page')) {
140 S::logger()->log('search', 'adv=' . var_export($_GET, true));
141 }
142
143 require_once 'userset.inc.php';
144 $view = new SearchSet(false, $action == 'geoloc' && substr($subaction, -3) == 'swf');
145 $view->addMod('minifiche', 'Mini-fiches', true);
146 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true));
147 // TODO: Reactivate when the new map is completed.
148 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
149 $view->apply('search/adv', $page, $action, $subaction);
150
151 if ($subaction) {
152 return;
153 }
154 $nb_tot = $view->count();
155 if ($nb_tot > $globals->search->private_max) {
156 $this->form_prepare();
157 $page->trigError('Recherche trop générale.');
158 }
159 }
160
161 $page->changeTpl('search/index.tpl', $action == 'mini' ? SIMPLE : SKINNED);
162 $page->addJsLink('ajax.js');
163 $page->assign('public_directory',0);
164 }
165
166 function handler_autocomplete(&$page, $type = null)
167 {
168 // Autocompletion : according to type required, return
169 // a list of results matching with the number of matches.
170 // The output format is :
171 // result1|nb1
172 // result2|nb2
173 // ...
174 pl_content_headers("text/plain");
175 $q = preg_replace(array('/\*+$/', // always look for $q*
176 '/([\^\$\[\]])/', // escape special regexp char
177 '/\*/'), // replace joker by regexp joker
178 array('',
179 '\\\\\1',
180 '.*'),
181 Env::s('q'));
182 if (!$q) exit();
183
184 // try to look in cached results
185 $cache = XDB::query('SELECT result
186 FROM search_autocomplete
187 WHERE name = {?} AND
188 query = {?} AND
189 generated > NOW() - INTERVAL 1 DAY',
190 $type, $q);
191 if ($res = $cache->fetchOneCell()) {
192 echo $res;
193 die();
194 }
195
196 require_once 'directory.enums.inc.php';
197 $enums = array(
198 'binetTxt' => DirEnum::BINETS,
199 'groupexTxt' => DirEnum::GROUPESX,
200 'sectionTxt' => DirEnum::SECTIONS,
201 'networking_typeTxt' => DirEnum::NETWORKS,
202 'city' => DirEnum::LOCALITIES,
203 'countryTxt' => DirEnum::COUNTRIES,
204 'entreprise' => DirEnum::COMPANIES,
205 'secteurTxt' => DirEnum::SECTORS,
206 'description' => DirEnum::JOBDESCRIPTION,
207 'nationaliteTxt' => DirEnum::NATIONALITIES,
208 'schoolTxt' => DirEnum::EDUSCHOOLS,
209 );
210 if (!array_key_exists($enums, $type)) {
211 exit();
212 }
213
214 $enum = $enums[$type];
215
216 $list = DirEnum::getAutoComplete($enum, $q);
217 $nbResults = 0;
218 $res = "";
219 while ($result = $list->next()) {
220 $nbResults++;
221 if ($nbResults == 11) {
222 $res .= $q."|-1\n";
223 } else {
224 $res .= $result['field'].'|';
225 if (isset($result['nb'])) {
226 $res .= $result['nb'];
227 }
228 if (isset($result['id'])) {
229 $res .= '|'.$result['id'];
230 }
231 $res .= "\n";
232 }
233 }
234 XDB::query('REPLACE INTO search_autocomplete
235 VALUES ({?}, {?}, {?}, NOW())',
236 $type, $q, $res);
237 echo $res;
238 exit();
239 }
240
241 function handler_list(&$page, $type = null, $idVal = null)
242 {
243 // Give the list of all values possible of type and builds a select input for it
244 $ids = null;
245 require_once 'directory.enums.inc.php';
246
247 switch ($type) {
248 case 'binet':
249 $ids = DirEnum::getOptionsIter(DirEnum::BINETS);
250 break;
251 case 'networking_type':
252 $ids = DirEnum::getOptionsIter(DirEnum::NETWORKS);
253 break;
254 case 'country':
255 $ids = DirEnum::getOptionsIter(DirEnum::COUNTRIES);
256 $page->assign('onchange', 'changeCountry(this.value)');
257 break;
258 case 'diploma':
259 if (Env::has('school') && Env::i('school') != 0) {
260 $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES, Env::i('school'));
261 } else {
262 $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES);
263 }
264 break;
265 case 'groupex':
266 $ids = DirEnum::getOptionsIter(DirEnum::GROUPESX);
267 break;
268 case 'nationalite':
269 $ids = DirEnum::getOptionsIter(DirEnum::NATIONALITIES);
270 break;
271 case 'region':
272 if ($isset($_REQUEST['country'])) {
273 $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS, $_REQUEST['country']);
274 } else {
275 $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS);
276 }
277 break;
278 case 'school':
279 $ids = DirEnum::getOptionsIter(DirEnum::EDUSCHOOLS);
280 $page->assign('onchange', 'changeSchool(this.value)');
281 break;
282 case 'section':
283 $ids = DirEnum::getOptionsIter(DirEnum::SECTIONS);
284 break;
285 case 'secteur':
286 $ids = DirEnum::getOptionsIter(DirEnum::SECTORS);
287 break;
288 default: exit();
289 }
290 if (isset($idVal)) {
291 pl_content_headers("text/plain");
292 echo $ids[$idVal];
293 exit();
294 }
295 pl_content_headers("text/xml");
296 $page->changeTpl('include/field.select.tpl', NO_SKIN);
297 $page->assign('name', $type);
298 $page->assign('list', $ids);
299 $page->assign('with_text_value', true);
300 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
301 }
302 }
303
304 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
305 ?>