1dc226cdfb57ad1b404bc7c4f3e23691e8b10803
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 'search/list/count' => $this->make_hook('list_count', AUTH_COOKIE
, 'directory_ax', NO_AUTH
),
33 'jobs' => $this->make_hook('referent', AUTH_COOKIE
),
34 'emploi' => $this->make_hook('referent', AUTH_COOKIE
),
35 'referent/search' => $this->make_hook('referent', AUTH_COOKIE
),
36 'search/referent/countries' => $this->make_hook('referent_countries', AUTH_COOKIE
),
40 function handler_redir_advanced($page, $mode = null
)
42 pl_redirect('search/adv');
46 function form_prepare()
48 Platal
::page()->assign('formulaire',1);
52 * $model: The way of presenting the results: minifiche, trombi, geoloc.
53 * $byletter: Show only names beginning with this letter
55 function handler_quick($page, $model = null
, $byletter = null
)
59 if (Env
::has('quick') ||
$model == 'geoloc') {
60 $quick = Env
::t('quick');
61 if (S
::logged() && !Env
::has('page')) {
62 S
::logger()->log('search', 'quick=' . $quick);
66 $page->trigWarning('Aucun critère de recherche n\'est spécifié.');
67 $page->changeTpl('search/index.tpl');
68 $page->setTitle('Annuaire');
69 $page->assign('formulaire', 1);
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 Get
::set('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 QuickSearchSet();
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);
155 $networks = DirEnum
::getOptions(DirEnum
::NETWORKS
);
156 $networks[-1] = 'Tous types';
159 $page->assign('networking_types', $networks);
160 $origin_corps_list = DirEnum
::getOptions(DirEnum
::CURRENTCORPS
);
161 $current_corps_list = DirEnum
::getOptions(DirEnum
::ORIGINCORPS
);
162 $corps_rank_list = DirEnum
::getOptions(DirEnum
::CORPSRANKS
);
163 $origin_corps_list[0] = '-';
164 $current_corps_list[0] = '-';
165 $corps_rank_list[0] = '-';
166 ksort($origin_corps_list);
167 ksort($current_corps_list);
168 ksort($corps_rank_list);
169 $page->assign('origin_corps_list', $origin_corps_list);
170 $page->assign('current_corps_list', $current_corps_list);
171 $page->assign('corps_rank_list', $corps_rank_list);
173 if (!Env
::has('rechercher') && $model != 'geoloc') {
174 $this->form_prepare();
176 if (!Env
::has('page')) {
177 S
::logger()->log('search', 'adv=' . var_export($_GET, true
));
180 require_once 'userset.inc.php';
181 // Enable X.org fields for X.org admins, and AX fields for AX secretaries.
182 $view = new AdvancedSearchSet(S
::admin(),
183 S
::user()->checkPerms(User
::PERM_EDIT_DIRECTORY
));
185 if (!$view->isValid()) {
186 $this->form_prepare();
187 $page->trigError('Recherche invalide.');
189 $view->addMod('minifiche', 'Mini-fiches', true
, array('starts_with' => $byletter));
190 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
));
191 // TODO: Reactivate when the new map is completed.
192 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
193 if (S
::user()->checkPerms(User
::PERM_EDIT_DIRECTORY
) || S
::admin()) {
194 $view->addMod('addresses', 'Adresses postales', false
);
196 $view->apply('search/adv', $page, $model);
198 $nb_tot = $view->count();
199 if ($nb_tot > $globals->search
->private_max
) {
200 $this->form_prepare();
201 if ($model != 'addresses' && (S
::user()->checkPerms(User
::PERM_EDIT_DIRECTORY
) || S
::admin())) {
202 $page->assign('suggestAddresses', true
);
204 $page->trigError('Recherche trop générale.');
205 } else if ($nb_tot == 0) {
206 $this->form_prepare();
207 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
212 $page->changeTpl('search/index.tpl', $model == 'mini' ? SIMPLE
: SKINNED
);
213 $page->assign('public_directory',0);
216 function handler_autocomplete($page, $type = null
)
218 // Autocompletion : according to type required, return
219 // a list of results matching with the number of matches.
220 // The output format is :
224 pl_content_headers("text/plain");
225 $q = preg_replace(array('/\*+$/', // always look for $q*
226 '/([\^\$\[\]])/', // escape special regexp char
227 '/\*/'), // replace joker by regexp joker
234 // try to look in cached results
235 $cache = XDB
::query('SELECT result
236 FROM search_autocomplete
239 generated > NOW() - INTERVAL 1 DAY',
241 if ($res = $cache->fetchOneCell()) {
247 'binetTxt' => DirEnum
::BINETS
,
248 'groupexTxt' => DirEnum
::GROUPESX
,
249 'sectionTxt' => DirEnum
::SECTIONS
,
250 'networking_typeTxt' => DirEnum
::NETWORKS
,
251 'localityTxt' => DirEnum
::LOCALITIES
,
252 'countryTxt' => DirEnum
::COUNTRIES
,
253 'entreprise' => DirEnum
::COMPANIES
,
254 'jobtermTxt' => DirEnum
::JOBTERMS
,
255 'description' => DirEnum
::JOBDESCRIPTION
,
256 'nationaliteTxt' => DirEnum
::NATIONALITIES
,
257 'schoolTxt' => DirEnum
::EDUSCHOOLS
,
259 if (!array_key_exists($type, $enums)) {
263 $enum = $enums[$type];
265 $list = DirEnum
::getAutoComplete($enum, $q);
268 while ($result = $list->next()) {
270 if ($nbResults == 11) {
273 $res .= $result['field'].'|';
274 if (isset($result['nb'])) {
275 $res .= $result['nb'];
277 if (isset($result['id'])) {
278 $res .= '|'.$result['id'];
283 if ($nbResults == 0) {
286 XDB
::query('INSERT INTO search_autocomplete (name, query, result, generated)
287 VALUES ({?}, {?}, {?}, NOW())
288 ON DUPLICATE KEY UPDATE result = VALUES(result), generated = VALUES(generated)',
294 function handler_list($page, $type = null
, $idVal = null
)
296 $page->assign('name', $type);
297 $page->assign('with_text_value', true
);
298 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
300 // Give the list of all values possible of type and builds a select input for it
305 $ids = DirEnum
::getOptionsIter(DirEnum
::BINETS
);
307 case 'networking_type':
308 $ids = DirEnum
::getOptionsIter(DirEnum
::NETWORKS
);
311 $ids = DirEnum
::getOptionsIter(DirEnum
::COUNTRIES
);
312 $page->assign('onchange', 'changeAddressComponents(\'' . $type . '\', this.value)');
314 case 'administrative_area_level_1':
315 case 'administrative_area_level_2':
316 case 'administrative_area_level_3':
318 $page->assign('onchange', 'changeAddressComponents(\'' . $type . '\', this.value)');
320 $ids = XDB
::iterator("SELECT pace1.id, pace1.long_name AS field
321 FROM profile_addresses_components_enum AS pace1
322 INNER JOIN profile_addresses_components AS pac1 ON (pac1.component_id = pace1.id)
323 INNER JOIN profile_addresses_components AS pac2 ON (pac1.pid = pac2.pid AND pac1.jobid = pac2.jobid AND pac1.id = pac2.id
324 AND pac1.groupid = pac2.groupid AND pac1.type = pac2.type)
325 INNER JOIN profile_addresses_components_enum AS pace2 ON (pac2.component_id = pace2.id AND FIND_IN_SET({?}, pace2.types))
326 WHERE pace2.id = {?} AND FIND_IN_SET({?}, pace1.types) AND pac1.type = 'home'
327 GROUP BY pace1.long_name",
328 Env
::v('previous'), Env
::v('value'), $type);
331 if (Env
::has('school') && Env
::i('school') != 0) {
332 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
, Env
::i('school'));
334 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
);
338 $ids = DirEnum
::getOptionsIter(DirEnum
::GROUPESX
);
341 $ids = DirEnum
::getOptionsIter(DirEnum
::NATIONALITIES
);
344 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUSCHOOLS
);
345 $page->assign('onchange', 'changeSchool(this.value)');
348 $ids = DirEnum
::getOptionsIter(DirEnum
::SECTIONS
);
351 if (Env
::has('jtid')) {
352 JobTerms
::ajaxGetBranch($page, JobTerms
::ONLY_JOBS
);
355 pl_content_headers('text/xml');
356 echo '<div>'; // global container so that response is valid xml
357 echo '<input name="jobtermTxt" type="text" style="display:none" size="32" />';
358 echo '<input name="jobterm" type="hidden"/>';
359 echo '<div class="term_tree"></div>'; // container where to create the tree
360 echo '<script type="text/javascript" src="javascript/jquery.jstree.js"></script>';
361 echo '<script type="text/javascript" src="javascript/jobtermstree.js"></script>';
362 echo '<script type="text/javascript">createJobTermsTree(".term_tree", "search/list/jobterm", "search", "searchForJobTerm");</script>';
369 pl_content_headers("text/plain");
373 pl_content_headers("text/xml");
374 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
375 $page->assign('list', $ids);
378 function handler_referent($page, $action = null
, $subaction = null
)
382 $wp = new PlWikiPage('Docs.Emploi');
385 $page->setTitle('Emploi et Carrières');
388 $res = XDB
::query("SELECT count(distinct pid) FROM profile_mentor_term");
389 $page->assign('mentors_number', $res->fetchOneCell());
391 // Search for mentors matching filters
392 require_once 'ufbuilder.inc.php';
393 $ufb = new UFB_MentorSearch();
394 if (!$ufb->isEmpty()) {
395 require_once 'userset.inc.php';
396 $ufc = $ufb->getUFC();
397 $set = new ProfileSet($ufc);
398 $set->addMod('mentor', 'Référents');
399 $set->apply('referent/search', $page, $action, $subaction);
400 $nb_tot = $set->count();
401 if ($nb_tot > $globals->search
->private_max
) {
402 $this->form_prepare();
403 $page->trigError('Recherche trop générale.');
404 $page->assign('plset_count', 0);
405 } else if ($nb_tot == 0) {
406 $this->form_prepare();
407 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base.');
411 $page->changeTpl('search/referent.tpl');
415 * Builds a select field to choose among countries that referents
416 * know about. Only referents linked to term (jtid) are displayed.
417 * @param $jtid id of job term to restrict referents
419 function handler_referent_countries($page, $jtid = null
)
421 pl_content_headers("text/xml");
422 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
423 $page->assign('name', 'country');
424 $it = XDB
::iterator("SELECT gc.iso_3166_1_a2 AS id, gc.country AS field
425 FROM geoloc_countries AS gc
426 INNER JOIN profile_mentor_country AS mp ON (mp.country = gc.iso_3166_1_a2)
427 INNER JOIN profile_mentor_term AS mt ON (mt.pid = mp.pid)
428 INNER JOIN profile_job_term_relation AS jtr ON (jtr.jtid_2 = mt.jtid)
429 WHERE jtr.jtid_1 = {?}
430 GROUP BY gc.iso_3166_1_a2
431 ORDER BY gc.country", $jtid);
432 $page->assign('list', $it);
436 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: