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
, 'directory_ax'),
29 'advanced_search.php' => $this->make_hook('redir_advanced', AUTH_PUBLIC
),
30 'search/autocomplete' => $this->make_hook('autocomplete', AUTH_COOKIE
, 'directory_ax', NO_AUTH
),
31 'search/list' => $this->make_hook('list', AUTH_COOKIE
, 'directory_ax', 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);
47 * $model: The way of presenting the results: minifiche, trombi, geoloc.
48 * $byletter: Show only names beginning with this letter
50 function handler_quick(&$page, $model = null
, $byletter = null
)
54 if (Env
::has('quick') ||
$model == 'geoloc') {
55 $quick = Env
::t('quick');
56 if (S
::logged() && !Env
::has('page')) {
57 S
::logger()->log('search', 'quick=' . $quick);
61 $page->trigWarning('Aucun critère de recherche n\'est spécifié.');
62 $page->changeTpl('search/index.tpl');
63 $page->setTitle('Annuaire');
64 $page->assign('formulaire', 1);
65 $page->addJsLink('ajax.js');
69 $list = 'profile|prf|fiche|fic|referent|ref|mentor';
71 $list .= '|admin|adm|ax';
73 if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.\d{2,4})?)?)$/', replace_accent($quick), $matches)) {
76 case 'admin': case 'adm':
77 $base = 'admin/user/';
80 $base = 'profile/ax/';
82 case 'profile': case 'prf': case 'fiche': case 'fic':
85 case 'referent': case 'ref': case 'mentor':
90 $user = User
::getSilent($login);
92 pl_redirect($base . $user->login());
94 $_REQUEST['quick'] = $login;
95 $_GET['quick'] = $login;
96 } elseif (strpos($quick, 'doc:') === 0) {
97 $url = 'Docs/Recherche?';
98 $url .= 'action=search&q=' . urlencode(substr($quick, 4));
99 $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review');
101 } elseif (strpos($quick, 'trombi:') === 0) {
102 $promo = substr($quick, 7);
103 $res = XDB
::query("SELECT diminutif
105 WHERE cat = 'Promotions' AND diminutif = {?}",
107 if ($res->numRows() == 0) {
108 $page->trigWarning("La promotion demandée n'est pas valide: $promo");
110 http_redirect('http://www.polytechnique.net/login/' . $promo . '/annuaire/trombi');
114 $page->assign('formulaire', 0);
116 require_once 'userset.inc.php';
117 $view = new SearchSet(true
);
118 $view->addMod('minifiche', 'Mini-fiches', true
, array('with_score' => true
, 'starts_with' => $byletter));
119 if (S
::logged() && !Env
::i('nonins')) {
120 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
, 'with_score' => true
));
121 // TODO: Reactivate when the new map is completed.
122 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
124 $view->apply('search', $page, $model);
126 $nb_tot = $view->count();
127 $page->assign('search_results_nb', $nb_tot);
128 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
129 $page->trigError('Votre recherche a généré trop de résultats pour un affichage public.');
130 } elseif ($nb_tot > $globals->search
->private_max
) {
131 $page->trigError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.');
132 } elseif (empty($nb_tot)) {
133 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
136 $page->assign('formulaire',1);
137 $page->addJsLink('ajax.js');
140 $page->changeTpl('search/index.tpl');
141 $page->setTitle('Annuaire');
144 /** $model is the way of presenting the results: minifiche, trombi, geoloc.
146 function handler_advanced(&$page, $model = null
, $byletter = null
)
149 require_once 'geocoding.inc.php';
150 $page->assign('advanced',1);
151 $page->addJsLink('jquery.autocomplete.js');
153 if (!Env
::has('rechercher') && $model != 'geoloc') {
154 $this->form_prepare();
156 if (!Env
::has('page')) {
157 S
::logger()->log('search', 'adv=' . var_export($_GET, true
));
160 require_once 'userset.inc.php';
161 $view = new SearchSet(false
);
162 $view->addMod('minifiche', 'Mini-fiches', true
, array('starts_with' => $byletter));
163 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
));
164 // TODO: Reactivate when the new map is completed.
165 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
166 $view->apply('search/adv', $page, $model);
168 $nb_tot = $view->count();
169 if ($nb_tot > $globals->search
->private_max
) {
170 $this->form_prepare();
171 $page->trigError('Recherche trop générale.');
172 } else if ($nb_tot == 0) {
173 $this->form_prepare();
174 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
178 $page->changeTpl('search/index.tpl', $model == 'mini' ? SIMPLE
: SKINNED
);
179 $page->addJsLink('ajax.js');
180 $page->assign('public_directory',0);
183 function handler_autocomplete(&$page, $type = null
)
185 // Autocompletion : according to type required, return
186 // a list of results matching with the number of matches.
187 // The output format is :
191 pl_content_headers("text/plain");
192 $q = preg_replace(array('/\*+$/', // always look for $q*
193 '/([\^\$\[\]])/', // escape special regexp char
194 '/\*/'), // replace joker by regexp joker
201 // try to look in cached results
202 $cache = XDB
::query('SELECT result
203 FROM search_autocomplete
206 generated > NOW() - INTERVAL 1 DAY',
208 if ($res = $cache->fetchOneCell()) {
214 'binetTxt' => DirEnum
::BINETS
,
215 'groupexTxt' => DirEnum
::GROUPESX
,
216 'sectionTxt' => DirEnum
::SECTIONS
,
217 'networking_typeTxt' => DirEnum
::NETWORKS
,
218 'city' => DirEnum
::LOCALITIES
,
219 'countryTxt' => DirEnum
::COUNTRIES
,
220 'entreprise' => DirEnum
::COMPANIES
,
221 'secteurTxt' => DirEnum
::SECTORS
,
222 'description' => DirEnum
::JOBDESCRIPTION
,
223 'nationaliteTxt' => DirEnum
::NATIONALITIES
,
224 'schoolTxt' => DirEnum
::EDUSCHOOLS
,
226 if (!array_key_exists($type, $enums)) {
230 $enum = $enums[$type];
232 $list = DirEnum
::getAutoComplete($enum, $q);
235 while ($result = $list->next()) {
237 if ($nbResults == 11) {
240 $res .= $result['field'].'|';
241 if (isset($result['nb'])) {
242 $res .= $result['nb'];
244 if (isset($result['id'])) {
245 $res .= '|'.$result['id'];
250 XDB
::query('REPLACE INTO search_autocomplete
251 VALUES ({?}, {?}, {?}, NOW())',
257 function handler_list(&$page, $type = null
, $idVal = null
)
259 $page->assign('name', $type);
260 $page->assign('with_text_value', true
);
261 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
263 // Give the list of all values possible of type and builds a select input for it
268 $ids = DirEnum
::getOptionsIter(DirEnum
::BINETS
);
270 case 'networking_type':
271 $ids = DirEnum
::getOptionsIter(DirEnum
::NETWORKS
);
274 $ids = DirEnum
::getOptionsIter(DirEnum
::COUNTRIES
);
275 $page->assign('onchange', 'changeCountry(this.value)');
278 if (Env
::has('school') && Env
::i('school') != 0) {
279 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
, Env
::i('school'));
281 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
);
285 $ids = DirEnum
::getOptionsIter(DirEnum
::GROUPESX
);
288 $ids = DirEnum
::getOptionsIter(DirEnum
::NATIONALITIES
);
291 if (Env
::has('country')) {
292 $ids = DirEnum
::getOptionsIter(DirEnum
::ADMINAREAS
, Env
::v('country'));
294 $ids = DirEnum
::getOptionsIter(DirEnum
::ADMINAREAS
);
298 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUSCHOOLS
);
299 $page->assign('onchange', 'changeSchool(this.value)');
302 $ids = DirEnum
::getOptionsIter(DirEnum
::SECTIONS
);
305 $ids = DirEnum
::getOptionsIter(DirEnum
::SECTORS
);
310 pl_content_headers("text/plain");
314 pl_content_headers("text/xml");
315 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
316 $page->assign('list', $ids);
320 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: