2 /***************************************************************************
3 * Copyright (C) 2003-2010 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
),
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
),
35 function handler_redir_advanced(&$page, $mode = null
)
37 pl_redirect('search/adv');
41 function form_prepare()
43 Platal
::page()->assign('formulaire',1);
46 function handler_quick(&$page, $action = null
, $subaction = null
)
50 if (Env
::has('quick') ||
$action == 'geoloc') {
51 $quick = trim(Env
::t('quick'));
52 if (S
::logged() && !Env
::has('page')) {
53 S
::logger()->log('search', 'quick=' . $quick);
55 $list = 'profile|prf|fiche|fic|referent|ref|mentor';
57 $list .= '|admin|adm|ax';
59 if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.\d{2,4})?)?)$/', replace_accent($quick), $matches)) {
62 case 'admin': case 'adm':
63 $base = 'admin/user/';
66 $base = 'profile/ax/';
68 case 'profile': case 'prf': case 'fiche': case 'fic':
71 case 'referent': case 'ref': case 'mentor':
76 $user = User
::getSilent($login);
78 pl_redirect($base . $user->login());
80 $_REQUEST['quick'] = $login;
81 $_GET['quick'] = $login;
82 } elseif (strpos($quick, 'doc:') === 0) {
83 $url = 'Docs/Recherche?';
84 $url .= 'action=search&q=' . urlencode(substr($quick, 4));
85 $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review');
87 } elseif (strpos($quick, 'trombi:') === 0) {
88 $promo = substr($quick, 7);
89 $res = XDB
::query("SELECT diminutif
91 WHERE cat = 'Promotions' AND diminutif = {?}",
93 if ($res->numRows() == 0) {
94 $page->trigWarning("La promotion demandée n'est pas valide: $promo");
96 http_redirect('http://www.polytechnique.net/login/' . $promo . '/annuaire/trombi');
100 $page->assign('formulaire', 0);
102 require_once 'userset.inc.php';
103 $view = new SearchSet(true
, $action == 'geoloc' && substr($subaction, -3) == 'swf');
104 $view->addMod('minifiche', 'Mini-fiches', true
, array('with_score' => true
));
105 if (S
::logged() && !Env
::i('nonins')) {
106 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
, 'with_score' => true
));
107 // TODO: Reactivate when the new map is completed.
108 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
110 $view->apply('search', $page, $action, $subaction);
112 $nb_tot = $view->count();
113 $page->assign('search_results_nb', $nb_tot);
117 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
118 $page->trigError('Votre recherche a généré trop de résultats pour un affichage public.');
119 } elseif ($nb_tot > $globals->search
->private_max
) {
120 $page->trigError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.');
121 } elseif (empty($nb_tot)) {
122 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
125 $page->assign('formulaire',1);
126 $page->addJsLink('ajax.js');
129 $page->changeTpl('search/index.tpl');
130 $page->setTitle('Annuaire');
133 function handler_advanced(&$page, $action = null
, $subaction = null
)
136 require_once 'geocoding.inc.php';
137 $page->assign('advanced',1);
138 $page->addJsLink('jquery.autocomplete.js');
140 if (!Env
::has('rechercher') && $action != 'geoloc') {
141 $this->form_prepare();
143 if (!Env
::has('page')) {
144 S
::logger()->log('search', 'adv=' . var_export($_GET, true
));
147 require_once 'userset.inc.php';
148 $view = new SearchSet(false
, $action == 'geoloc' && substr($subaction, -3) == 'swf');
149 $view->addMod('minifiche', 'Mini-fiches', true
);
150 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
));
151 // TODO: Reactivate when the new map is completed.
152 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
153 $view->apply('search/adv', $page, $action, $subaction);
158 $nb_tot = $view->count();
159 if ($nb_tot > $globals->search
->private_max
) {
160 $this->form_prepare();
161 $page->trigError('Recherche trop générale.');
165 $page->changeTpl('search/index.tpl', $action == 'mini' ? SIMPLE
: SKINNED
);
166 $page->addJsLink('ajax.js');
167 $page->assign('public_directory',0);
170 function handler_autocomplete(&$page, $type = null
)
172 // Autocompletion : according to type required, return
173 // a list of results matching with the number of matches.
174 // The output format is :
178 pl_content_headers("text/plain");
179 $q = preg_replace(array('/\*+$/', // always look for $q*
180 '/([\^\$\[\]])/', // escape special regexp char
181 '/\*/'), // replace joker by regexp joker
188 // try to look in cached results
189 $cache = XDB
::query('SELECT result
190 FROM search_autocomplete
193 generated > NOW() - INTERVAL 1 DAY',
195 if ($res = $cache->fetchOneCell()) {
201 'binetTxt' => DirEnum
::BINETS
,
202 'groupexTxt' => DirEnum
::GROUPESX
,
203 'sectionTxt' => DirEnum
::SECTIONS
,
204 'networking_typeTxt' => DirEnum
::NETWORKS
,
205 'city' => DirEnum
::LOCALITIES
,
206 'countryTxt' => DirEnum
::COUNTRIES
,
207 'entreprise' => DirEnum
::COMPANIES
,
208 'secteurTxt' => DirEnum
::SECTORS
,
209 'description' => DirEnum
::JOBDESCRIPTION
,
210 'nationaliteTxt' => DirEnum
::NATIONALITIES
,
211 'schoolTxt' => DirEnum
::EDUSCHOOLS
,
213 if (!array_key_exists($type, $enums)) {
217 $enum = $enums[$type];
219 $list = DirEnum
::getAutoComplete($enum, $q);
222 while ($result = $list->next()) {
224 if ($nbResults == 11) {
227 $res .= $result['field'].'|';
228 if (isset($result['nb'])) {
229 $res .= $result['nb'];
231 if (isset($result['id'])) {
232 $res .= '|'.$result['id'];
237 XDB
::query('REPLACE INTO search_autocomplete
238 VALUES ({?}, {?}, {?}, NOW())',
244 function handler_list(&$page, $type = null
, $idVal = null
)
246 $page->assign('name', $type);
247 $page->assign('with_text_value', true
);
248 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
250 // Give the list of all values possible of type and builds a select input for it
255 $ids = DirEnum
::getOptionsIter(DirEnum
::BINETS
);
257 case 'networking_type':
258 $ids = DirEnum
::getOptionsIter(DirEnum
::NETWORKS
);
261 $ids = DirEnum
::getOptionsIter(DirEnum
::COUNTRIES
);
262 $page->assign('onchange', 'changeCountry(this.value)');
265 if (Env
::has('school') && Env
::i('school') != 0) {
266 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
, Env
::i('school'));
268 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
);
272 $ids = DirEnum
::getOptionsIter(DirEnum
::GROUPESX
);
275 $ids = DirEnum
::getOptionsIter(DirEnum
::NATIONALITIES
);
278 if (Env
::has('country')) {
279 $ids = DirEnum
::getOptionsIter(DirEnum
::ADMINAREAS
, Env
::v('country'));
281 $ids = DirEnum
::getOptionsIter(DirEnum
::ADMINAREAS
);
285 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUSCHOOLS
);
286 $page->assign('onchange', 'changeSchool(this.value)');
289 $ids = DirEnum
::getOptionsIter(DirEnum
::SECTIONS
);
292 $ids = DirEnum
::getOptionsIter(DirEnum
::SECTORS
);
297 pl_content_headers("text/plain");
301 pl_content_headers("text/xml");
302 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
303 $page->assign('list', $ids);
307 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: