| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 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. * |
| 10 | * * |
| 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. * |
| 15 | * * |
| 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 * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | class SearchModule extends PLModule |
| 23 | { |
| 24 | function handlers() |
| 25 | { |
| 26 | return array( |
| 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), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | function handler_redir_advanced($page, $mode = null) |
| 41 | { |
| 42 | pl_redirect('search/adv'); |
| 43 | exit; |
| 44 | } |
| 45 | |
| 46 | function form_prepare() |
| 47 | { |
| 48 | Platal::page()->assign('formulaire',1); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * $model: The way of presenting the results: minifiche, trombi, geoloc. |
| 53 | * $byletter: Show only names beginning with this letter |
| 54 | */ |
| 55 | function handler_quick($page, $model = null, $byletter = null) |
| 56 | { |
| 57 | global $globals; |
| 58 | |
| 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); |
| 63 | } |
| 64 | |
| 65 | if ($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); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | $list = 'profile|prf|fiche|fic|referent|ref|mentor'; |
| 74 | if (S::admin()) { |
| 75 | $list .= '|admin|adm|ax'; |
| 76 | } |
| 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)) { |
| 80 | $login = $matches[2]; |
| 81 | switch($matches[1]) { |
| 82 | case 'admin': case 'adm': |
| 83 | $base = 'admin/user/'; |
| 84 | break; |
| 85 | case 'ax': |
| 86 | $base = 'profile/ax/'; |
| 87 | break; |
| 88 | case 'profile': case 'prf': case 'fiche': case 'fic': |
| 89 | $base = 'profile/'; |
| 90 | break; |
| 91 | case 'referent': case 'ref': case 'mentor': |
| 92 | $base = 'referent/'; |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | $user = User::getSilent($login); |
| 97 | if ($user) { |
| 98 | pl_redirect($base . $user->login()); |
| 99 | } |
| 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'); |
| 105 | pl_redirect($url); |
| 106 | } elseif (strpos($quick, 'trombi:') === 0) { |
| 107 | $promo = substr($quick, 7); |
| 108 | $res = XDB::query("SELECT diminutif |
| 109 | FROM groups |
| 110 | WHERE cat = 'Promotions' AND diminutif = {?}", |
| 111 | $promo); |
| 112 | if ($res->numRows() == 0) { |
| 113 | $page->trigWarning("La promotion demandée n'est pas valide: $promo"); |
| 114 | } else { |
| 115 | http_redirect('http://www.polytechnique.net/login/' . $promo . '/annuaire/trombi'); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | $page->assign('formulaire', 0); |
| 120 | |
| 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)); |
| 127 | } |
| 128 | $view->apply('search', $page, $model); |
| 129 | |
| 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 !'); |
| 138 | } |
| 139 | } else { |
| 140 | $page->assign('formulaire',1); |
| 141 | } |
| 142 | |
| 143 | $page->changeTpl('search/index.tpl'); |
| 144 | $page->setTitle('Annuaire'); |
| 145 | } |
| 146 | |
| 147 | /** $model is the way of presenting the results: minifiche, trombi, geoloc. |
| 148 | */ |
| 149 | function handler_advanced($page, $model = null, $byletter = null) |
| 150 | { |
| 151 | global $globals; |
| 152 | $page->addJsLink('search.js'); |
| 153 | $page->assign('advanced',1); |
| 154 | |
| 155 | $networks = DirEnum::getOptions(DirEnum::NETWORKS); |
| 156 | $networks[-1] = 'Tous types'; |
| 157 | $networks[0] = '-'; |
| 158 | ksort($networks); |
| 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); |
| 172 | |
| 173 | if (!Env::has('rechercher') && $model != 'geoloc') { |
| 174 | $this->form_prepare(); |
| 175 | } else { |
| 176 | if (!Env::has('page')) { |
| 177 | S::logger()->log('search', 'adv=' . var_export($_GET, true)); |
| 178 | } |
| 179 | |
| 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)); |
| 184 | |
| 185 | if (!$view->isValid()) { |
| 186 | $this->form_prepare(); |
| 187 | $page->trigError('Recherche invalide.'); |
| 188 | } else { |
| 189 | $view->addMod('minifiche', 'Mini-fiches', true, array('starts_with' => $byletter)); |
| 190 | $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true)); |
| 191 | $view->addMod('map', 'Planisphère'); |
| 192 | if (S::user()->checkPerms(User::PERM_EDIT_DIRECTORY) || S::admin()) { |
| 193 | $view->addMod('addresses', 'Adresses postales', false); |
| 194 | } |
| 195 | $view->apply('search/adv', $page, $model); |
| 196 | |
| 197 | $nb_tot = $view->count(); |
| 198 | if ($nb_tot > $globals->search->private_max) { |
| 199 | $this->form_prepare(); |
| 200 | if ($model != 'addresses' && (S::user()->checkPerms(User::PERM_EDIT_DIRECTORY) || S::admin())) { |
| 201 | $page->assign('suggestAddresses', true); |
| 202 | } |
| 203 | $page->trigError('Recherche trop générale.'); |
| 204 | } else if ($nb_tot == 0) { |
| 205 | $this->form_prepare(); |
| 206 | $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !'); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | $page->changeTpl('search/index.tpl', $model == 'mini' ? SIMPLE : SKINNED); |
| 212 | $page->assign('public_directory',0); |
| 213 | } |
| 214 | |
| 215 | function handler_autocomplete($page, $type = null) |
| 216 | { |
| 217 | // Autocompletion : according to type required, return |
| 218 | // a list of results matching with the number of matches. |
| 219 | // The output format is : |
| 220 | // result1|nb1 |
| 221 | // result2|nb2 |
| 222 | // ... |
| 223 | pl_content_headers("text/plain"); |
| 224 | $q = preg_replace(array('/\*+$/', // always look for $q* |
| 225 | '/([\^\$\[\]])/', // escape special regexp char |
| 226 | '/\*/'), // replace joker by regexp joker |
| 227 | array('', |
| 228 | '\\\\\1', |
| 229 | '.*'), |
| 230 | Env::s('q')); |
| 231 | if (!$q) exit(); |
| 232 | |
| 233 | // try to look in cached results |
| 234 | $cache = XDB::query('SELECT result |
| 235 | FROM search_autocomplete |
| 236 | WHERE name = {?} AND |
| 237 | query = {?} AND |
| 238 | generated > NOW() - INTERVAL 1 DAY', |
| 239 | $type, $q); |
| 240 | if ($res = $cache->fetchOneCell()) { |
| 241 | echo $res; |
| 242 | die(); |
| 243 | } |
| 244 | |
| 245 | $enums = array( |
| 246 | 'binet_text' => DirEnum::BINETS, |
| 247 | 'groupex_text' => DirEnum::GROUPESX, |
| 248 | 'section_text' => DirEnum::SECTIONS, |
| 249 | 'networking_type_text' => DirEnum::NETWORKS, |
| 250 | 'locality_text' => DirEnum::LOCALITIES, |
| 251 | 'country_text' => DirEnum::COUNTRIES, |
| 252 | 'entreprise' => DirEnum::COMPANIES, |
| 253 | 'jobterm_text' => DirEnum::JOBTERMS, |
| 254 | 'description' => DirEnum::JOBDESCRIPTION, |
| 255 | 'nationalite_text' => DirEnum::NATIONALITIES, |
| 256 | 'school_text' => DirEnum::EDUSCHOOLS, |
| 257 | ); |
| 258 | if (!array_key_exists($type, $enums)) { |
| 259 | exit(); |
| 260 | } |
| 261 | |
| 262 | $enum = $enums[$type]; |
| 263 | |
| 264 | $list = DirEnum::getAutoComplete($enum, $q); |
| 265 | $nbResults = 0; |
| 266 | $res = ""; |
| 267 | while ($result = $list->next()) { |
| 268 | $nbResults++; |
| 269 | if ($nbResults == 11) { |
| 270 | $res .= $q."|-1\n"; |
| 271 | } else { |
| 272 | $res .= $result['field'].'|'; |
| 273 | if (isset($result['nb'])) { |
| 274 | $res .= $result['nb']; |
| 275 | } |
| 276 | if (isset($result['id'])) { |
| 277 | $res .= '|'.$result['id']; |
| 278 | } |
| 279 | $res .= "\n"; |
| 280 | } |
| 281 | } |
| 282 | if ($nbResults == 0) { |
| 283 | $res = $q."|-2\n"; |
| 284 | } |
| 285 | XDB::query('INSERT INTO search_autocomplete (name, query, result, generated) |
| 286 | VALUES ({?}, {?}, {?}, NOW()) |
| 287 | ON DUPLICATE KEY UPDATE result = VALUES(result), generated = VALUES(generated)', |
| 288 | $type, $q, $res); |
| 289 | echo $res; |
| 290 | exit(); |
| 291 | } |
| 292 | |
| 293 | function handler_list($page, $type = null, $idVal = null) |
| 294 | { |
| 295 | $page->assign('name', $type); |
| 296 | $page->assign('with_text_value', true); |
| 297 | $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text"); |
| 298 | |
| 299 | // Give the list of all values possible of type and builds a select input for it |
| 300 | $ids = null; |
| 301 | |
| 302 | switch ($type) { |
| 303 | case 'binet': |
| 304 | $ids = DirEnum::getOptionsIter(DirEnum::BINETS); |
| 305 | break; |
| 306 | case 'networking_type': |
| 307 | $ids = DirEnum::getOptionsIter(DirEnum::NETWORKS); |
| 308 | break; |
| 309 | case 'country': |
| 310 | $ids = DirEnum::getOptionsIter(DirEnum::COUNTRIES); |
| 311 | $page->assign('onchange', 'changeAddressComponents(\'' . $type . '\', this.value)'); |
| 312 | break; |
| 313 | case 'administrative_area_level_1': |
| 314 | case 'administrative_area_level_2': |
| 315 | case 'locality': |
| 316 | $page->assign('onchange', 'changeAddressComponents(\'' . $type . '\', this.value)'); |
| 317 | case 'postal_code': |
| 318 | $ids = XDB::iterator("SELECT pace1.id, pace1.long_name AS field |
| 319 | FROM profile_addresses_components_enum AS pace1 |
| 320 | INNER JOIN profile_addresses_components AS pac1 ON (pac1.component_id = pace1.id) |
| 321 | INNER JOIN profile_addresses_components AS pac2 ON (pac1.pid = pac2.pid AND pac1.jobid = pac2.jobid AND pac1.id = pac2.id |
| 322 | AND pac1.groupid = pac2.groupid AND pac1.type = pac2.type) |
| 323 | INNER JOIN profile_addresses_components_enum AS pace2 ON (pac2.component_id = pace2.id AND FIND_IN_SET({?}, pace2.types)) |
| 324 | WHERE pace2.id = {?} AND FIND_IN_SET({?}, pace1.types) AND pac1.type = 'home' |
| 325 | GROUP BY pace1.long_name", |
| 326 | Env::v('previous'), Env::v('value'), $type); |
| 327 | break; |
| 328 | case 'diploma': |
| 329 | if (Env::has('school') && Env::i('school') != 0) { |
| 330 | $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES, Env::i('school')); |
| 331 | } else { |
| 332 | $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES); |
| 333 | } |
| 334 | break; |
| 335 | case 'groupex': |
| 336 | $ids = DirEnum::getOptionsIter(DirEnum::GROUPESX); |
| 337 | break; |
| 338 | case 'nationalite': |
| 339 | $ids = DirEnum::getOptionsIter(DirEnum::NATIONALITIES); |
| 340 | break; |
| 341 | case 'school': |
| 342 | $ids = DirEnum::getOptionsIter(DirEnum::EDUSCHOOLS); |
| 343 | $page->assign('onchange', 'changeSchool(this.value)'); |
| 344 | break; |
| 345 | case 'section': |
| 346 | $ids = DirEnum::getOptionsIter(DirEnum::SECTIONS); |
| 347 | break; |
| 348 | case 'jobterm': |
| 349 | if (Env::has('jtid')) { |
| 350 | JobTerms::ajaxGetBranch($page, JobTerms::ONLY_JOBS); |
| 351 | return; |
| 352 | } else { |
| 353 | pl_content_headers('text/xml'); |
| 354 | echo '<div>'; // global container so that response is valid xml |
| 355 | echo '<input name="jobtermTxt" type="text" style="display:none" size="32" />'; |
| 356 | echo '<input name="jobterm" type="hidden"/>'; |
| 357 | echo '<div class="term_tree"></div>'; // container where to create the tree |
| 358 | echo '<script type="text/javascript" src="javascript/jquery.jstree.js"></script>'; |
| 359 | echo '<script type="text/javascript" src="javascript/jobtermstree.js"></script>'; |
| 360 | echo '<script type="text/javascript">createJobTermsTree(".term_tree", "search/list/jobterm", "search", "searchForJobTerm");</script>'; |
| 361 | echo '</div>'; |
| 362 | exit(); |
| 363 | } |
| 364 | default: exit(); |
| 365 | } |
| 366 | if (isset($idVal)) { |
| 367 | pl_content_headers("text/plain"); |
| 368 | echo $ids[$idVal]; |
| 369 | exit(); |
| 370 | } |
| 371 | pl_content_headers("text/xml"); |
| 372 | $page->changeTpl('include/field.select.tpl', NO_SKIN); |
| 373 | $page->assign('list', $ids); |
| 374 | } |
| 375 | |
| 376 | function handler_referent($page, $action = null, $subaction = null) |
| 377 | { |
| 378 | global $globals; |
| 379 | |
| 380 | $wp = new PlWikiPage('Docs.Emploi'); |
| 381 | $wp->buildCache(); |
| 382 | |
| 383 | $page->setTitle('Emploi et Carrières'); |
| 384 | |
| 385 | // Count mentors |
| 386 | $res = XDB::query("SELECT count(distinct pid) FROM profile_mentor_term"); |
| 387 | $page->assign('mentors_number', $res->fetchOneCell()); |
| 388 | |
| 389 | // Search for mentors matching filters |
| 390 | require_once 'ufbuilder.inc.php'; |
| 391 | $ufb = new UFB_MentorSearch(); |
| 392 | if (!$ufb->isEmpty()) { |
| 393 | require_once 'userset.inc.php'; |
| 394 | $ufc = $ufb->getUFC(); |
| 395 | $set = new ProfileSet($ufc); |
| 396 | $set->addMod('mentor', 'Référents'); |
| 397 | $set->apply('referent/search', $page, $action, $subaction); |
| 398 | $nb_tot = $set->count(); |
| 399 | if ($nb_tot > $globals->search->private_max) { |
| 400 | $this->form_prepare(); |
| 401 | $page->trigError('Recherche trop générale.'); |
| 402 | $page->assign('plset_count', 0); |
| 403 | } else if ($nb_tot == 0) { |
| 404 | $this->form_prepare(); |
| 405 | $page->trigError('Il n\'existe personne correspondant à ces critères dans la base.'); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | $page->changeTpl('search/referent.tpl'); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Builds a select field to choose among countries that referents |
| 414 | * know about. Only referents linked to term (jtid) are displayed. |
| 415 | * @param $jtid id of job term to restrict referents |
| 416 | */ |
| 417 | function handler_referent_countries($page, $jtid = null) |
| 418 | { |
| 419 | pl_content_headers("text/xml"); |
| 420 | $page->changeTpl('include/field.select.tpl', NO_SKIN); |
| 421 | $page->assign('name', 'country'); |
| 422 | $it = XDB::iterator("SELECT gc.iso_3166_1_a2 AS id, gc.country AS field |
| 423 | FROM geoloc_countries AS gc |
| 424 | INNER JOIN profile_mentor_country AS mp ON (mp.country = gc.iso_3166_1_a2) |
| 425 | INNER JOIN profile_mentor_term AS mt ON (mt.pid = mp.pid) |
| 426 | INNER JOIN profile_job_term_relation AS jtr ON (jtr.jtid_2 = mt.jtid) |
| 427 | WHERE jtr.jtid_1 = {?} |
| 428 | GROUP BY gc.iso_3166_1_a2 |
| 429 | ORDER BY gc.country", $jtid); |
| 430 | $page->assign('list', $it); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 435 | ?> |