b76196e7f29558d9a4299f18758456daaddd4b4a
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
),
32 'jobs' => $this->make_hook('referent', AUTH_COOKIE
),
33 'emploi' => $this->make_hook('referent', AUTH_COOKIE
),
34 'referent/search' => $this->make_hook('referent', AUTH_COOKIE
),
35 'search/referent/countries' => $this->make_hook('referent_countries', AUTH_COOKIE
),
39 function handler_redir_advanced(&$page, $mode = null
)
41 pl_redirect('search/adv');
45 function form_prepare()
47 Platal
::page()->assign('formulaire',1);
51 * $model: The way of presenting the results: minifiche, trombi, geoloc.
52 * $byletter: Show only names beginning with this letter
54 function handler_quick(&$page, $model = null
, $byletter = null
)
58 if (Env
::has('quick') ||
$model == 'geoloc') {
59 $quick = Env
::t('quick');
60 if (S
::logged() && !Env
::has('page')) {
61 S
::logger()->log('search', 'quick=' . $quick);
65 $page->trigWarning('Aucun critère de recherche n\'est spécifié.');
66 $page->changeTpl('search/index.tpl');
67 $page->setTitle('Annuaire');
68 $page->assign('formulaire', 1);
72 $list = 'profile|prf|fiche|fic|referent|ref|mentor';
74 $list .= '|admin|adm|ax';
76 $suffixes = array_keys(DirEnum
::getOptions(DirEnum
::ACCOUNTTYPES
));
77 $suffixes = implode('|', $suffixes);
78 if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.(?:[md]?\d{2,4}|' . $suffixes . '))?)?)$/', replace_accent($quick), $matches)) {
81 case 'admin': case 'adm':
82 $base = 'admin/user/';
85 $base = 'profile/ax/';
87 case 'profile': case 'prf': case 'fiche': case 'fic':
90 case 'referent': case 'ref': case 'mentor':
95 $user = User
::getSilent($login);
97 pl_redirect($base . $user->login());
99 $_REQUEST['quick'] = $login;
100 $_GET['quick'] = $login;
101 } elseif (strpos($quick, 'doc:') === 0) {
102 $url = 'Docs/Recherche?';
103 $url .= 'action=search&q=' . urlencode(substr($quick, 4));
104 $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review');
106 } elseif (strpos($quick, 'trombi:') === 0) {
107 $promo = substr($quick, 7);
108 $res = XDB
::query("SELECT diminutif
110 WHERE cat = 'Promotions' AND diminutif = {?}",
112 if ($res->numRows() == 0) {
113 $page->trigWarning("La promotion demandée n'est pas valide: $promo");
115 http_redirect('http://www.polytechnique.net/login/' . $promo . '/annuaire/trombi');
119 $page->assign('formulaire', 0);
121 require_once 'userset.inc.php';
122 $view = new SearchSet(true
);
123 $view->addMod('minifiche', 'Mini-fiches', true
, array('with_score' => true
, 'starts_with' => $byletter));
124 if (S
::logged() && !Env
::i('nonins')) {
125 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
, 'with_score' => true
));
126 // TODO: Reactivate when the new map is completed.
127 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
129 $view->apply('search', $page, $model);
131 $nb_tot = $view->count();
132 $page->assign('search_results_nb', $nb_tot);
133 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
134 $page->trigError('Votre recherche a généré trop de résultats pour un affichage public.');
135 } elseif ($nb_tot > $globals->search
->private_max
) {
136 $page->trigError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.');
137 } elseif (empty($nb_tot)) {
138 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
141 $page->assign('formulaire',1);
144 $page->changeTpl('search/index.tpl');
145 $page->setTitle('Annuaire');
148 /** $model is the way of presenting the results: minifiche, trombi, geoloc.
150 function handler_advanced(&$page, $model = null
, $byletter = null
)
153 $page->assign('advanced',1);
154 $page->addJsLink('jquery.autocomplete.js');
156 $networks = DirEnum
::getOptions(DirEnum
::NETWORKS
);
157 $networks[-1] = 'Tous types';
160 $page->assign('networking_types', $networks);
162 if (!Env
::has('rechercher') && $model != 'geoloc') {
163 $this->form_prepare();
165 if (!Env
::has('page')) {
166 S
::logger()->log('search', 'adv=' . var_export($_GET, true
));
169 require_once 'userset.inc.php';
170 $view = new SearchSet(false
);
171 if (!$view->isValid()) {
172 $this->form_prepare();
173 $page->trigError('Recherche invalide.');
175 $view->addMod('minifiche', 'Mini-fiches', true
, array('starts_with' => $byletter));
176 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
));
177 // TODO: Reactivate when the new map is completed.
178 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
179 if (S
::user()->checkPerms(User
::PERM_EDIT_DIRECTORY
) || S
::admin()) {
180 $view->addMod('addresses', 'Adresses postales', false
);
182 $view->apply('search/adv', $page, $model);
184 $nb_tot = $view->count();
185 if ($nb_tot > $globals->search
->private_max
) {
186 $this->form_prepare();
187 if ($model != 'addresses' && (S
::user()->checkPerms(User
::PERM_EDIT_DIRECTORY
) || S
::admin())) {
188 $page->assign('suggestAddresses', true
);
190 $page->trigError('Recherche trop générale.');
191 } else if ($nb_tot == 0) {
192 $this->form_prepare();
193 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
198 $page->changeTpl('search/index.tpl', $model == 'mini' ? SIMPLE
: SKINNED
);
199 $page->assign('public_directory',0);
202 function handler_autocomplete(&$page, $type = null
)
204 // Autocompletion : according to type required, return
205 // a list of results matching with the number of matches.
206 // The output format is :
210 pl_content_headers("text/plain");
211 $q = preg_replace(array('/\*+$/', // always look for $q*
212 '/([\^\$\[\]])/', // escape special regexp char
213 '/\*/'), // replace joker by regexp joker
220 // try to look in cached results
221 $cache = XDB
::query('SELECT result
222 FROM search_autocomplete
225 generated > NOW() - INTERVAL 1 DAY',
227 if ($res = $cache->fetchOneCell()) {
233 'binetTxt' => DirEnum
::BINETS
,
234 'groupexTxt' => DirEnum
::GROUPESX
,
235 'sectionTxt' => DirEnum
::SECTIONS
,
236 'networking_typeTxt' => DirEnum
::NETWORKS
,
237 'city' => DirEnum
::LOCALITIES
,
238 'countryTxt' => DirEnum
::COUNTRIES
,
239 'entreprise' => DirEnum
::COMPANIES
,
240 'jobtermTxt' => DirEnum
::JOBTERMS
,
241 'description' => DirEnum
::JOBDESCRIPTION
,
242 'nationaliteTxt' => DirEnum
::NATIONALITIES
,
243 'schoolTxt' => DirEnum
::EDUSCHOOLS
,
245 if (!array_key_exists($type, $enums)) {
249 $enum = $enums[$type];
251 $list = DirEnum
::getAutoComplete($enum, $q);
254 while ($result = $list->next()) {
256 if ($nbResults == 11) {
259 $res .= $result['field'].'|';
260 if (isset($result['nb'])) {
261 $res .= $result['nb'];
263 if (isset($result['id'])) {
264 $res .= '|'.$result['id'];
269 if ($nbResults == 0) {
272 XDB
::query('INSERT INTO search_autocomplete (name, query, result, generated)
273 VALUES ({?}, {?}, {?}, NOW())
274 ON DUPLICATE KEY UPDATE result = VALUES(result), generated = VALUES(generated)',
280 function handler_list(&$page, $type = null
, $idVal = null
)
282 $page->assign('name', $type);
283 $page->assign('with_text_value', true
);
284 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
286 // Give the list of all values possible of type and builds a select input for it
291 $ids = DirEnum
::getOptionsIter(DirEnum
::BINETS
);
293 case 'networking_type':
294 $ids = DirEnum
::getOptionsIter(DirEnum
::NETWORKS
);
297 $ids = DirEnum
::getOptionsIter(DirEnum
::COUNTRIES
);
298 $page->assign('onchange', 'changeCountry(this.value)');
301 if (Env
::has('school') && Env
::i('school') != 0) {
302 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
, Env
::i('school'));
304 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
);
308 $ids = DirEnum
::getOptionsIter(DirEnum
::GROUPESX
);
311 $ids = DirEnum
::getOptionsIter(DirEnum
::NATIONALITIES
);
314 if (Env
::has('country')) {
315 $ids = DirEnum
::getOptionsIter(DirEnum
::ADMINAREAS
, Env
::v('country'));
317 $ids = DirEnum
::getOptionsIter(DirEnum
::ADMINAREAS
);
321 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUSCHOOLS
);
322 $page->assign('onchange', 'changeSchool(this.value)');
325 $ids = DirEnum
::getOptionsIter(DirEnum
::SECTIONS
);
328 if (Env
::has('jtid')) {
329 JobTerms
::ajaxGetBranch(&$page, JobTerms
::ONLY_JOBS
);
332 pl_content_headers('text/xml');
333 echo '<div>'; // global container so that response is valid xml
334 echo '<input name="jobtermTxt" type="text" style="display:none" size="32" />';
335 echo '<input name="jobterm" type="hidden"/>';
336 echo '<div class="term_tree"></div>'; // container where to create the tree
337 echo '<script type="text/javascript" src="javascript/jquery.jstree.js"></script>';
338 echo '<script type="text/javascript" src="javascript/jobtermstree.js"></script>';
339 echo '<script type="text/javascript">createJobTermsTree(".term_tree", "search/list/jobterm", "search", "searchForJobTerm");</script>';
346 pl_content_headers("text/plain");
350 pl_content_headers("text/xml");
351 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
352 $page->assign('list', $ids);
355 function handler_referent(&$page, $action = null
, $subaction = null
)
359 $wp = new PlWikiPage('Docs.Emploi');
362 $page->setTitle('Emploi et Carrières');
365 $res = XDB
::query("SELECT count(distinct pid) FROM profile_mentor_term");
366 $page->assign('mentors_number', $res->fetchOneCell());
368 $page->addJsLink('jquery.autocomplete.js');
370 // Search for mentors matching filters
371 require_once 'ufbuilder.inc.php';
372 $ufb = new UFB_MentorSearch();
373 if (!$ufb->isEmpty()) {
374 require_once 'userset.inc.php';
375 $ufc = $ufb->getUFC();
376 $set = new ProfileSet($ufc);
377 $set->addMod('mentor', 'Référents');
378 $set->apply('referent/search', $page, $action, $subaction);
379 $nb_tot = $set->count();
380 if ($nb_tot > $globals->search
->private_max
) {
381 $this->form_prepare();
382 $page->trigError('Recherche trop générale.');
383 $page->assign('plset_count', 0);
384 } else if ($nb_tot == 0) {
385 $this->form_prepare();
386 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base.');
390 $page->changeTpl('search/referent.tpl');
394 * Builds a select field to choose among countries that referents
395 * know about. Only referents linked to term (jtid) are displayed.
396 * @param $jtid id of job term to restrict referents
398 function handler_referent_countries(&$page, $jtid = null
)
400 pl_content_headers("text/xml");
401 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
402 $page->assign('name', 'country');
403 $it = XDB
::iterator("SELECT gc.iso_3166_1_a2 AS id, gc.country AS field
404 FROM geoloc_countries AS gc
405 INNER JOIN profile_mentor_country AS mp ON (mp.country = gc.iso_3166_1_a2)
406 INNER JOIN profile_mentor_term AS mt ON (mt.pid = mp.pid)
407 INNER JOIN profile_job_term_relation AS jtr ON (jtr.jtid_2 = mt.jtid)
408 WHERE jtr.jtid_1 = {?}
409 GROUP BY iso_3166_1_a2
410 ORDER BY country", $jtid);
411 $page->assign('list', $it);
415 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: