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
, 'user'),
34 'emploi' => $this->make_hook('referent', AUTH_COOKIE
, 'user'),
35 'referent/search' => $this->make_hook('referent', AUTH_COOKIE
, 'user'),
36 'search/referent/countries' => $this->make_hook('referent_countries', AUTH_COOKIE
, 'user'),
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 $view->addMod('map', 'Planisphère');
125 if (S
::logged() && !Env
::i('nonins')) {
126 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
, 'with_score' => true
));
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);
143 $page->changeTpl('search/index.tpl');
144 $page->setTitle('Annuaire');
147 /** $model is the way of presenting the results: minifiche, trombi, geoloc.
149 function handler_advanced($page, $model = null
, $byletter = null
)
152 $page->addJsLink('jquery.ui.xorg.js');
153 $page->addJsLink('search.js');
154 $page->assign('advanced',1);
156 $networks = DirEnum
::getOptions(DirEnum
::NETWORKS
);
157 $networks[-1] = 'Tous types';
160 $page->assign('networking_types', $networks);
161 $origin_corps_list = DirEnum
::getOptions(DirEnum
::CURRENTCORPS
);
162 $current_corps_list = DirEnum
::getOptions(DirEnum
::ORIGINCORPS
);
163 $corps_rank_list = DirEnum
::getOptions(DirEnum
::CORPSRANKS
);
164 $origin_corps_list[0] = '-';
165 $current_corps_list[0] = '-';
166 $corps_rank_list[0] = '-';
167 ksort($origin_corps_list);
168 ksort($current_corps_list);
169 ksort($corps_rank_list);
170 $page->assign('origin_corps_list', $origin_corps_list);
171 $page->assign('current_corps_list', $current_corps_list);
172 $page->assign('corps_rank_list', $corps_rank_list);
174 if (!Env
::has('rechercher') && $model != 'geoloc') {
175 $this->form_prepare();
177 if (!Env
::has('page')) {
178 S
::logger()->log('search', 'adv=' . var_export($_GET, true
));
181 require_once 'userset.inc.php';
182 // Enable X.org fields for X.org admins, and AX fields for AX secretaries.
183 $view = new AdvancedSearchSet(S
::admin(),
184 S
::user()->checkPerms(User
::PERM_EDIT_DIRECTORY
));
186 if (!$view->isValid()) {
187 $this->form_prepare();
188 $page->trigError('Recherche invalide.');
190 $view->addMod('minifiche', 'Mini-fiches', true
, array('starts_with' => $byletter));
191 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
));
192 $view->addMod('map', 'Planisphère');
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 static public function format_autocomplete(array $item)
218 return $item['field'] . ' (' . $item['nb'] . ' camarade' . ($item['nb'] > 1 ?
's' : '') . ')';
221 function handler_autocomplete($page, $type = null
, $sub_id = null
)
223 // Autocompletion : according to type required, return
224 // a list of results matching with the number of matches.
225 // The output format is :
229 pl_content_headers("text/plain");
230 $q = preg_replace(array('/\*+$/', // always look for $q*
231 '/([\^\$\[\]])/', // escape special regexp char
232 '/\*/'), // replace joker by regexp joker
240 if (!is_null($sub_id)) {
241 $query = $q . "\t" . $sub_id;
246 // Try to look in cached results.
248 $cache = XDB
::query('SELECT result
249 FROM search_autocomplete
250 WHERE name = {?} AND query = {?} AND generated > NOW() - INTERVAL 1 DAY',
253 if ($cache->numRows() > 0) {
255 $data = explode("\n", $cache->fetchOneCell());
257 foreach ($data as $line) {
259 $aux = explode("\t", $line);
265 $item['value'] = self
::format_autocomplete($item);
266 array_push($list, $item);
271 'binet_text' => DirEnum
::BINETS
,
272 'groupex_text' => DirEnum
::GROUPESX
,
273 'section_text' => DirEnum
::SECTIONS
,
274 'networking_type_text' => DirEnum
::NETWORKS
,
275 'locality_text' => DirEnum
::LOCALITIES
,
276 'country_text' => DirEnum
::COUNTRIES
,
277 'entreprise' => DirEnum
::COMPANIES
,
278 'jobterm_text' => DirEnum
::JOBTERMS
,
279 'description' => DirEnum
::JOBDESCRIPTION
,
280 'nationalite_text' => DirEnum
::NATIONALITIES
,
281 'school_text' => DirEnum
::EDUSCHOOLS
,
283 if (!array_key_exists($type, $enums)) {
287 if (is_null($sub_id)) {
288 $list = DirEnum
::getAutoComplete($enums[$type], $q);
290 $list = DirEnum
::getAutoComplete($enums[$type], $q, $sub_id);
293 foreach ($list as &$item) {
294 $to_cache .= $item['field'] . "\t" . $item['nb'] . "\t" . $item['id'] . "\n";
295 $item['value'] = self
::format_autocomplete($item);
299 $count = count($list);
300 if ($count == DirEnumeration
::AUTOCOMPLETE_LIMIT
) {
307 } elseif ($count == 0) {
309 'value' => 'Aucun camarade trouvé pour ' . $q . '.',
317 XDB
::query('INSERT INTO search_autocomplete (name, query, result, generated)
318 VALUES ({?}, {?}, {?}, NOW())
319 ON DUPLICATE KEY UPDATE result = VALUES(result), generated = VALUES(generated)',
320 $type, $query, $to_cache);
322 echo json_encode($list);
326 function handler_list($page, $type = null
, $idVal = null
)
328 $page->assign('name', $type);
329 $page->assign('with_text_value', true
);
330 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
332 // Give the list of all values possible of type and builds a select input for it
337 $ids = DirEnum
::getOptionsIter(DirEnum
::BINETS
);
339 case 'networking_type':
340 $ids = DirEnum
::getOptionsIter(DirEnum
::NETWORKS
);
343 $ids = DirEnum
::getOptionsIter(DirEnum
::COUNTRIES
);
344 $page->assign('onchange', 'changeAddressComponents(\'' . $type . '\', this.value)');
346 case 'administrative_area_level_1':
347 case 'administrative_area_level_2':
349 $page->assign('onchange', 'changeAddressComponents(\'' . $type . '\', this.value)');
351 $ids = XDB
::iterator("SELECT pace1.id, pace1.long_name AS field
352 FROM profile_addresses_components_enum AS pace1
353 INNER JOIN profile_addresses_components AS pac1 ON (pac1.component_id = pace1.id)
354 INNER JOIN profile_addresses_components AS pac2 ON (pac1.pid = pac2.pid AND pac1.jobid = pac2.jobid AND pac1.id = pac2.id
355 AND pac1.groupid = pac2.groupid AND pac1.type = pac2.type)
356 INNER JOIN profile_addresses_components_enum AS pace2 ON (pac2.component_id = pace2.id AND FIND_IN_SET({?}, pace2.types))
357 WHERE pace2.id = {?} AND FIND_IN_SET({?}, pace1.types) AND pac1.type = 'home'
358 GROUP BY pace1.long_name",
359 Env
::v('previous'), Env
::v('value'), $type);
362 if (Env
::has('school') && Env
::i('school') != 0) {
363 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
, Env
::i('school'));
365 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUDEGREES
);
369 $ids = DirEnum
::getOptionsIter(DirEnum
::GROUPESX
);
372 $ids = DirEnum
::getOptionsIter(DirEnum
::NATIONALITIES
);
375 $ids = DirEnum
::getOptionsIter(DirEnum
::EDUSCHOOLS
);
376 $page->assign('onchange', 'changeSchool(this.value)');
379 $ids = DirEnum
::getOptionsIter(DirEnum
::SECTIONS
);
382 if (Env
::has('jtid')) {
383 JobTerms
::ajaxGetBranch($page, JobTerms
::ONLY_JOBS
);
386 pl_content_headers('text/xml');
387 echo '<div>'; // global container so that response is valid xml
388 echo '<input name="jobtermTxt" type="text" style="display:none" size="32" />';
389 echo '<input name="jobterm" type="hidden"/>';
390 echo '<div class="term_tree"></div>'; // container where to create the tree
391 echo '<script type="text/javascript" src="javascript/jquery.jstree.js"></script>';
392 echo '<script type="text/javascript" src="javascript/jobtermstree.js"></script>';
393 echo '<script type="text/javascript">createJobTermsTree(".term_tree", "search/list/jobterm", "search", "searchForJobTerm");</script>';
400 pl_content_headers("text/plain");
404 pl_content_headers("text/xml");
405 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
406 $page->assign('list', $ids);
409 function handler_referent($page, $action = null
, $subaction = null
)
413 $wp = new PlWikiPage('Docs.Emploi');
416 $page->setTitle('Emploi et Carrières');
417 $page->addJsLink('jquery.ui.xorg.js');
420 $res = XDB
::query("SELECT count(distinct pid) FROM profile_mentor_term");
421 $page->assign('mentors_number', $res->fetchOneCell());
423 // Search for mentors matching filters
424 require_once 'ufbuilder.inc.php';
425 $ufb = new UFB_MentorSearch();
426 if (!$ufb->isEmpty()) {
427 require_once 'userset.inc.php';
428 $ufc = $ufb->getUFC();
429 $set = new ProfileSet($ufc);
430 $set->addMod('mentor', 'Référents');
431 $set->apply('referent/search', $page, $action, $subaction);
432 $nb_tot = $set->count();
433 if ($nb_tot > $globals->search
->private_max
) {
434 $this->form_prepare();
435 $page->trigError('Recherche trop générale.');
436 $page->assign('plset_count', 0);
437 } else if ($nb_tot == 0) {
438 $this->form_prepare();
439 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base.');
443 $page->changeTpl('search/referent.tpl');
447 * Builds a select field to choose among countries that referents
448 * know about. Only referents linked to term (jtid) are displayed.
449 * @param $jtid id of job term to restrict referents
451 function handler_referent_countries($page, $jtid = null
)
453 pl_content_headers("text/xml");
454 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
455 $page->assign('name', 'country');
456 $it = XDB
::iterator("SELECT gc.iso_3166_1_a2 AS id, gc.country AS field
457 FROM geoloc_countries AS gc
458 INNER JOIN profile_mentor_country AS mp ON (mp.country = gc.iso_3166_1_a2)
459 INNER JOIN profile_mentor_term AS mt ON (mt.pid = mp.pid)
460 INNER JOIN profile_job_term_relation AS jtr ON (jtr.jtid_2 = mt.jtid)
461 WHERE jtr.jtid_1 = {?}
462 GROUP BY gc.iso_3166_1_a2
463 ORDER BY gc.country", $jtid);
464 $page->assign('list', $it);
468 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: