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 $suffixes = array_keys(DirEnum
::getOptions(DirEnum
::ACCOUNTTYPES
));
78 $suffixes = implode('|', $suffixes);
79 if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.(?:[md]?\d{2,4}|' . $suffixes . '))?)?)$/', replace_accent($quick), $matches)) {
82 case 'admin': case 'adm':
83 $base = 'admin/user/';
86 $base = 'profile/ax/';
88 case 'profile': case 'prf': case 'fiche': case 'fic':
91 case 'referent': case 'ref': case 'mentor':
96 $user = User
::getSilent($login);
98 pl_redirect($base . $user->login());
100 $_REQUEST['quick'] = $login;
101 $_GET['quick'] = $login;
102 } elseif (strpos($quick, 'doc:') === 0) {
103 $url = 'Docs/Recherche?';
104 $url .= 'action=search&q=' . urlencode(substr($quick, 4));
105 $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review');
107 } elseif (strpos($quick, 'trombi:') === 0) {
108 $promo = substr($quick, 7);
109 $res = XDB
::query("SELECT diminutif
111 WHERE cat = 'Promotions' AND diminutif = {?}",
113 if ($res->numRows() == 0) {
114 $page->trigWarning("La promotion demandée n'est pas valide: $promo");
116 http_redirect('http://www.polytechnique.net/login/' . $promo . '/annuaire/trombi');
120 $page->assign('formulaire', 0);
122 require_once 'userset.inc.php';
123 $view = new QuickSearchSet();
124 $view->addMod('minifiche', 'Mini-fiches', true
, array('with_score' => true
, 'starts_with' => $byletter));
125 if (S
::logged() && !Env
::i('nonins')) {
126 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
, 'with_score' => true
));
127 // TODO: Reactivate when the new map is completed.
128 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
130 $view->apply('search', $page, $model);
132 $nb_tot = $view->count();
133 $page->assign('search_results_nb', $nb_tot);
134 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
135 $page->trigError('Votre recherche a généré trop de résultats pour un affichage public.');
136 } elseif ($nb_tot > $globals->search
->private_max
) {
137 $page->trigError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.');
138 } elseif (empty($nb_tot)) {
139 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
142 $page->assign('formulaire',1);
143 $page->addJsLink('ajax.js');
146 $page->changeTpl('search/index.tpl');
147 $page->setTitle('Annuaire');
150 /** $model is the way of presenting the results: minifiche, trombi, geoloc.
152 function handler_advanced(&$page, $model = null
, $byletter = null
)
155 $page->assign('advanced',1);
156 $page->addJsLink('jquery.autocomplete.js');
158 $networks = DirEnum
::getOptions(DirEnum
::NETWORKS
);
159 $networks[-1] = 'Tous types';
162 $page->assign('networking_types', $networks);
164 if (!Env
::has('rechercher') && $model != 'geoloc') {
165 $this->form_prepare();
167 if (!Env
::has('page')) {
168 S
::logger()->log('search', 'adv=' . var_export($_GET, true
));
171 require_once 'userset.inc.php';
172 // Enable X.org fields for X.org admins, and AX fields for AX secretaries.
173 $view = new AdvancedSearchSet(S
::admin(),
174 S
::user()->checkPerms(User
::PERM_EDIT_DIRECTORY
));
176 if (!$view->isValid()) {
177 $this->form_prepare();
178 $page->trigError('Recherche invalide.');
180 $view->addMod('minifiche', 'Mini-fiches', true
, array('starts_with' => $byletter));
181 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
));
182 // TODO: Reactivate when the new map is completed.
183 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
184 if (S
::user()->checkPerms(User
::PERM_EDIT_DIRECTORY
) || S
::admin()) {
185 $view->addMod('addresses', 'Adresses postales', false
);
187 $view->apply('search/adv', $page, $model);
189 $nb_tot = $view->count();
190 if ($nb_tot > $globals->search
->private_max
) {
191 $this->form_prepare();
192 if ($model != 'addresses' && (S
::user()->checkPerms(User
::PERM_EDIT_DIRECTORY
) || S
::admin())) {
193 $page->assign('suggestAddresses', true
);
195 $page->trigError('Recherche trop générale.');
196 } else if ($nb_tot == 0) {
197 $this->form_prepare();
198 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
203 $page->changeTpl('search/index.tpl', $model == 'mini' ? SIMPLE
: SKINNED
);
204 $page->addJsLink('ajax.js');
205 $page->assign('public_directory',0);
208 function handler_autocomplete(&$page, $type = null
)
210 // Autocompletion : according to type required, return
211 // a list of results matching with the number of matches.
212 // The output format is :
216 pl_content_headers("text/plain");
217 $q = preg_replace(array('/\*+$/', // always look for $q*
218 '/([\^\$\[\]])/', // escape special regexp char
219 '/\*/'), // replace joker by regexp joker
226 // try to look in cached results
227 $cache = XDB
::query('SELECT result
228 FROM search_autocomplete
231 generated > NOW() - INTERVAL 1 DAY',
233 if ($res = $cache->fetchOneCell()) {
239 'binetTxt' => DirEnum
::BINETS
,
240 'groupexTxt' => DirEnum
::GROUPESX
,
241 'sectionTxt' => DirEnum
::SECTIONS
,
242 'networking_typeTxt' => DirEnum
::NETWORKS
,
243 'city' => DirEnum
::LOCALITIES
,
244 'countryTxt' => DirEnum
::COUNTRIES
,
245 'entreprise' => DirEnum
::COMPANIES
,
246 'jobtermTxt' => DirEnum
::JOBTERMS
,
247 'description' => DirEnum
::JOBDESCRIPTION
,
248 'nationaliteTxt' => DirEnum
::NATIONALITIES
,
249 'schoolTxt' => DirEnum
::EDUSCHOOLS
,
251 if (!array_key_exists($type, $enums)) {
255 $enum = $enums[$type];
257 $list = DirEnum
::getAutoComplete($enum, $q);
260 while ($result = $list->next()) {
262 if ($nbResults == 11) {
265 $res .= $result['field'].'|';
266 if (isset($result['nb'])) {
267 $res .= $result['nb'];
269 if (isset($result['id'])) {
270 $res .= '|'.$result['id'];
275 if ($nbResults == 0) {
278 XDB
::query('INSERT INTO search_autocomplete (name, query, result, generated)
279 VALUES ({?}, {?}, {?}, NOW())
280 ON DUPLICATE KEY UPDATE result = VALUES(result), generated = VALUES(generated)',
286 function handler_list(&$page, $type = null
, $idVal = null
)
288 $page->assign('name', $type);
289 $page->assign('with_text_value', true
);
290 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
292 // Give the list of all values possible of type and builds a select input for it
297 $ids = DirEnum
::getOptionsIter(DirEnum
::BINETS
);
299 case 'networking_type':
300 $ids = DirEnum
::getOptionsIter(DirEnum
::NETWORKS
);
303 $ids = DirEnum
::getOptionsIter(DirEnum
::COUNTRIES
);
304 $page->assign('onchange', 'changeCountry(this.value)');
307 if (Env
::has('school') && Env
::i('school') != 0) {
308 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
, Env
::i('school'));
310 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
);
314 $ids = DirEnum
::getOptionsIter(DirEnum
::GROUPESX
);
317 $ids = DirEnum
::getOptionsIter(DirEnum
::NATIONALITIES
);
320 if (Env
::has('country')) {
321 $ids = DirEnum
::getOptionsIter(DirEnum
::ADMINAREAS
, Env
::v('country'));
323 $ids = DirEnum
::getOptionsIter(DirEnum
::ADMINAREAS
);
327 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUSCHOOLS
);
328 $page->assign('onchange', 'changeSchool(this.value)');
331 $ids = DirEnum
::getOptionsIter(DirEnum
::SECTIONS
);
334 if (Env
::has('jtid')) {
335 JobTerms
::ajaxGetBranch(&$page, JobTerms
::ONLY_JOBS
);
338 pl_content_headers('text/xml');
339 echo '<div>'; // global container so that response is valid xml
340 echo '<input name="jobtermTxt" type="text" style="display:none" size="32" />';
341 echo '<input name="jobterm" type="hidden"/>';
342 echo '<div class="term_tree"></div>'; // container where to create the tree
343 echo '<script type="text/javascript" src="javascript/jquery.jstree.js"></script>';
344 echo '<script type="text/javascript" src="javascript/jobtermstree.js"></script>';
345 echo '<script type="text/javascript">createJobTermsTree(".term_tree", "search/list/jobterm", "search", "searchForJobTerm");</script>';
352 pl_content_headers("text/plain");
356 pl_content_headers("text/xml");
357 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
358 $page->assign('list', $ids);
361 function handler_referent(&$page, $action = null
, $subaction = null
)
365 $wp = new PlWikiPage('Docs.Emploi');
368 $page->setTitle('Emploi et Carrières');
371 $res = XDB
::query("SELECT count(distinct pid) FROM profile_mentor_term");
372 $page->assign('mentors_number', $res->fetchOneCell());
374 $page->addJsLink('jquery.autocomplete.js');
376 // Search for mentors matching filters
377 require_once 'ufbuilder.inc.php';
378 $ufb = new UFB_MentorSearch();
379 if (!$ufb->isEmpty()) {
380 require_once 'userset.inc.php';
381 $ufc = $ufb->getUFC();
382 $set = new ProfileSet($ufc);
383 $set->addMod('mentor', 'Référents');
384 $set->apply('referent/search', $page, $action, $subaction);
385 $nb_tot = $set->count();
386 if ($nb_tot > $globals->search
->private_max
) {
387 $this->form_prepare();
388 $page->trigError('Recherche trop générale.');
389 $page->assign('plset_count', 0);
390 } else if ($nb_tot == 0) {
391 $this->form_prepare();
392 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base.');
396 $page->changeTpl('search/referent.tpl');
400 * Builds a select field to choose among countries that referents
401 * know about. Only referents linked to term (jtid) are displayed.
402 * @param $jtid id of job term to restrict referents
404 function handler_referent_countries(&$page, $jtid = null
)
406 pl_content_headers("text/xml");
407 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
408 $page->assign('name', 'country');
409 $it = XDB
::iterator("SELECT gc.iso_3166_1_a2 AS id, gc.country AS field
410 FROM geoloc_countries AS gc
411 INNER JOIN profile_mentor_country AS mp ON (mp.country = gc.iso_3166_1_a2)
412 INNER JOIN profile_mentor_term AS mt ON (mt.pid = mp.pid)
413 INNER JOIN profile_job_term_relation AS jtr ON (jtr.jtid_2 = mt.jtid)
414 WHERE jtr.jtid_1 = {?}
415 GROUP BY iso_3166_1_a2
416 ORDER BY country", $jtid);
417 $page->assign('list', $it);
421 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: