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);
69 $page->addJsLink('ajax.js');
73 $list = 'profile|prf|fiche|fic|referent|ref|mentor';
75 $list .= '|admin|adm|ax';
77 if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.\d{2,4})?)?)$/', replace_accent($quick), $matches)) {
80 case 'admin': case 'adm':
81 $base = 'admin/user/';
84 $base = 'profile/ax/';
86 case 'profile': case 'prf': case 'fiche': case 'fic':
89 case 'referent': case 'ref': case 'mentor':
94 $user = User
::getSilent($login);
96 pl_redirect($base . $user->login());
98 $_REQUEST['quick'] = $login;
99 $_GET['quick'] = $login;
100 } elseif (strpos($quick, 'doc:') === 0) {
101 $url = 'Docs/Recherche?';
102 $url .= 'action=search&q=' . urlencode(substr($quick, 4));
103 $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review');
105 } elseif (strpos($quick, 'trombi:') === 0) {
106 $promo = substr($quick, 7);
107 $res = XDB
::query("SELECT diminutif
109 WHERE cat = 'Promotions' AND diminutif = {?}",
111 if ($res->numRows() == 0) {
112 $page->trigWarning("La promotion demandée n'est pas valide: $promo");
114 http_redirect('http://www.polytechnique.net/login/' . $promo . '/annuaire/trombi');
118 $page->assign('formulaire', 0);
120 require_once 'userset.inc.php';
121 $view = new SearchSet(true
);
122 $view->addMod('minifiche', 'Mini-fiches', true
, array('with_score' => true
, 'starts_with' => $byletter));
123 if (S
::logged() && !Env
::i('nonins')) {
124 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
, 'with_score' => true
));
125 // TODO: Reactivate when the new map is completed.
126 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
128 $view->apply('search', $page, $model);
130 $nb_tot = $view->count();
131 $page->assign('search_results_nb', $nb_tot);
132 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
133 $page->trigError('Votre recherche a généré trop de résultats pour un affichage public.');
134 } elseif ($nb_tot > $globals->search
->private_max
) {
135 $page->trigError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.');
136 } elseif (empty($nb_tot)) {
137 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
140 $page->assign('formulaire',1);
141 $page->addJsLink('ajax.js');
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 require_once 'geocoding.inc.php';
154 $page->assign('advanced',1);
155 $page->addJsLink('jquery.autocomplete.js');
157 if (!Env
::has('rechercher') && $model != 'geoloc') {
158 $this->form_prepare();
160 if (!Env
::has('page')) {
161 S
::logger()->log('search', 'adv=' . var_export($_GET, true
));
164 require_once 'userset.inc.php';
165 $view = new SearchSet(false
);
166 $view->addMod('minifiche', 'Mini-fiches', true
, array('starts_with' => $byletter));
167 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
));
168 // TODO: Reactivate when the new map is completed.
169 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
170 $view->apply('search/adv', $page, $model);
172 $nb_tot = $view->count();
173 if ($nb_tot > $globals->search
->private_max
) {
174 $this->form_prepare();
175 $page->trigError('Recherche trop générale.');
176 } else if ($nb_tot == 0) {
177 $this->form_prepare();
178 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
182 $page->changeTpl('search/index.tpl', $model == 'mini' ? SIMPLE
: SKINNED
);
183 $page->addJsLink('ajax.js');
184 $page->assign('public_directory',0);
187 function handler_autocomplete(&$page, $type = null
)
189 // Autocompletion : according to type required, return
190 // a list of results matching with the number of matches.
191 // The output format is :
195 pl_content_headers("text/plain");
196 $q = preg_replace(array('/\*+$/', // always look for $q*
197 '/([\^\$\[\]])/', // escape special regexp char
198 '/\*/'), // replace joker by regexp joker
205 // try to look in cached results
206 $cache = XDB
::query('SELECT result
207 FROM search_autocomplete
210 generated > NOW() - INTERVAL 1 DAY',
212 if ($res = $cache->fetchOneCell()) {
218 'binetTxt' => DirEnum
::BINETS
,
219 'groupexTxt' => DirEnum
::GROUPESX
,
220 'sectionTxt' => DirEnum
::SECTIONS
,
221 'networking_typeTxt' => DirEnum
::NETWORKS
,
222 'city' => DirEnum
::LOCALITIES
,
223 'countryTxt' => DirEnum
::COUNTRIES
,
224 'entreprise' => DirEnum
::COMPANIES
,
225 'jobtermTxt' => DirEnum
::JOBTERMS
,
226 'description' => DirEnum
::JOBDESCRIPTION
,
227 'nationaliteTxt' => DirEnum
::NATIONALITIES
,
228 'schoolTxt' => DirEnum
::EDUSCHOOLS
,
230 if (!array_key_exists($type, $enums)) {
234 $enum = $enums[$type];
236 $list = DirEnum
::getAutoComplete($enum, $q);
239 while ($result = $list->next()) {
241 if ($nbResults == 11) {
244 $res .= $result['field'].'|';
245 if (isset($result['nb'])) {
246 $res .= $result['nb'];
248 if (isset($result['id'])) {
249 $res .= '|'.$result['id'];
254 XDB
::query('REPLACE INTO search_autocomplete
255 VALUES ({?}, {?}, {?}, NOW())',
261 function handler_list(&$page, $type = null
, $idVal = null
)
263 $page->assign('name', $type);
264 $page->assign('with_text_value', true
);
265 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
267 // Give the list of all values possible of type and builds a select input for it
272 $ids = DirEnum
::getOptionsIter(DirEnum
::BINETS
);
274 case 'networking_type':
275 $ids = DirEnum
::getOptionsIter(DirEnum
::NETWORKS
);
278 $ids = DirEnum
::getOptionsIter(DirEnum
::COUNTRIES
);
279 $page->assign('onchange', 'changeCountry(this.value)');
282 if (Env
::has('school') && Env
::i('school') != 0) {
283 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
, Env
::i('school'));
285 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
);
289 $ids = DirEnum
::getOptionsIter(DirEnum
::GROUPESX
);
292 $ids = DirEnum
::getOptionsIter(DirEnum
::NATIONALITIES
);
295 if (Env
::has('country')) {
296 $ids = DirEnum
::getOptionsIter(DirEnum
::ADMINAREAS
, Env
::v('country'));
298 $ids = DirEnum
::getOptionsIter(DirEnum
::ADMINAREAS
);
302 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUSCHOOLS
);
303 $page->assign('onchange', 'changeSchool(this.value)');
306 $ids = DirEnum
::getOptionsIter(DirEnum
::SECTIONS
);
309 $ids = DirEnum
::getOptionsIter(DirEnum
::SECTORS
);
312 if (Env
::has('jtid')) {
313 JobTerms
::ajaxGetBranch(&$page, JobTerms
::ONLY_JOBS
);
316 pl_content_headers('text/xml');
317 echo '<div>'; // global container so that response is valid xml
318 echo '<input name="jobtermTxt" type="text" style="display:none" size="32" />';
319 echo '<input name="jobterm" type="hidden"/>';
320 echo '<div class="term_tree"></div>'; // container where to create the tree
321 echo '<script type="text/javascript" src="javascript/jquery.jstree.js"></script>';
322 echo '<script type="text/javascript" src="javascript/jobtermstree.js"></script>';
323 echo '<script type="text/javascript">createJobTermsTree(".term_tree", "search/list/jobterm", "search", "searchForJobTerm");</script>';
330 pl_content_headers("text/plain");
334 pl_content_headers("text/xml");
335 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
336 $page->assign('list', $ids);
339 function handler_referent(&$page, $action = null
, $subaction = null
)
343 $wp = new PlWikiPage('Docs.Emploi');
346 $page->setTitle('Emploi et Carrières');
349 $res = XDB
::query("SELECT count(distinct pid) FROM profile_mentor_term");
350 $page->assign('mentors_number', $res->fetchOneCell());
352 $page->addJsLink('jquery.autocomplete.js');
354 // On vient d'un formulaire
355 require_once 'ufbuilder.inc.php';
356 $ufb = new UFB_MentorSearch();
357 if (!$ufb->isEmpty()) {
358 require_once 'userset.inc.php';
359 $ufc = $ufb->getUFC();
360 $set = new ProfileSet($ufc);
361 $set->addMod('mentor', 'Référents');
362 $set->apply('referent/search', $page, $action, $subaction);
363 $nb_tot = $set->count();
364 if ($nb_tot > $globals->search
->private_max
) {
365 $this->form_prepare();
366 $page->trigError('Recherche trop générale.');
367 $page->assign('plset_count', 0);
368 } else if ($nb_tot == 0) {
369 $this->form_prepare();
370 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base.');
374 $page->changeTpl('search/referent.tpl');
378 * Builds a select field to choose among countries that referents
379 * know about. Only referents linked to term (jtid) are displayed.
380 * @param $jtid id of job term to restrict referents
382 function handler_referent_countries(&$page, $jtid = null
)
384 pl_content_headers("text/xml");
385 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
386 $page->assign('name', 'country');
387 $it = XDB
::iterator("SELECT gc.iso_3166_1_a2 AS id, gc.countryFR AS field
388 FROM geoloc_countries AS gc
389 INNER JOIN profile_mentor_country AS mp ON (mp.country = gc.iso_3166_1_a2)
390 INNER JOIN profile_mentor_term AS mt ON (mt.pid = mp.pid)
391 INNER JOIN profile_job_term_relation AS jtr ON (jtr.jtid_2 = mt.jtid)
392 WHERE jtr.jtid_1 = {?}
393 GROUP BY iso_3166_1_a2
394 ORDER BY countryFR", $jtid);
395 $page->assign('list', $it);
399 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: