| 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, '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'), |
| 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('jquery.ui.xorg.js'); |
| 153 | $page->addJsLink('search.js'); |
| 154 | $page->assign('advanced',1); |
| 155 | |
| 156 | $networks = DirEnum::getOptions(DirEnum::NETWORKS); |
| 157 | $networks[-1] = 'Tous types'; |
| 158 | $networks[0] = '-'; |
| 159 | ksort($networks); |
| 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); |
| 173 | |
| 174 | if (!Env::has('rechercher') && $model != 'geoloc') { |
| 175 | $this->form_prepare(); |
| 176 | } else { |
| 177 | if (!Env::has('page')) { |
| 178 | S::logger()->log('search', 'adv=' . var_export($_GET, true)); |
| 179 | } |
| 180 | |
| 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)); |
| 185 | |
| 186 | if (!$view->isValid()) { |
| 187 | $this->form_prepare(); |
| 188 | $page->trigError('Recherche invalide.'); |
| 189 | } else { |
| 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); |
| 195 | } |
| 196 | $view->apply('search/adv', $page, $model); |
| 197 | |
| 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); |
| 203 | } |
| 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 !'); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | $page->changeTpl('search/index.tpl', $model == 'mini' ? SIMPLE : SKINNED); |
| 213 | $page->assign('public_directory',0); |
| 214 | } |
| 215 | |
| 216 | static public function format_autocomplete(array $item) |
| 217 | { |
| 218 | return $item['field'] . ' (' . $item['nb'] . ' camarade' . ($item['nb'] > 1 ? 's' : '') . ')'; |
| 219 | } |
| 220 | |
| 221 | function handler_autocomplete($page, $type = null, $sub_id = null) |
| 222 | { |
| 223 | // Autocompletion : according to type required, return |
| 224 | // a list of results matching with the number of matches. |
| 225 | // The output format is : |
| 226 | // result1|nb1 |
| 227 | // result2|nb2 |
| 228 | // ... |
| 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 |
| 233 | array('', |
| 234 | '\\\\\1', |
| 235 | '.*'), |
| 236 | Get::t('term')); |
| 237 | if (!$q) { |
| 238 | exit(); |
| 239 | } |
| 240 | if (!is_null($sub_id)) { |
| 241 | $query = $q . "\t" . $sub_id; |
| 242 | } else { |
| 243 | $query = $q; |
| 244 | } |
| 245 | |
| 246 | // Try to look in cached results. |
| 247 | $cached = false; |
| 248 | $cache = XDB::query('SELECT result |
| 249 | FROM search_autocomplete |
| 250 | WHERE name = {?} AND query = {?} AND generated > NOW() - INTERVAL 1 DAY', |
| 251 | $type, $query); |
| 252 | |
| 253 | if ($cache->numRows() > 0) { |
| 254 | $cached = true; |
| 255 | $data = explode("\n", $cache->fetchOneCell()); |
| 256 | $list = array(); |
| 257 | foreach ($data as $line) { |
| 258 | if ($line != '') { |
| 259 | $aux = explode("\t", $line); |
| 260 | $item = array( |
| 261 | 'field' => $aux[0], |
| 262 | 'nb' => $aux[1], |
| 263 | 'id' => $aux[2] |
| 264 | ); |
| 265 | $item['value'] = self::format_autocomplete($item); |
| 266 | array_push($list, $item); |
| 267 | } |
| 268 | } |
| 269 | } else { |
| 270 | $enums = array( |
| 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, |
| 282 | ); |
| 283 | if (!array_key_exists($type, $enums)) { |
| 284 | exit(); |
| 285 | } |
| 286 | |
| 287 | if (is_null($sub_id)) { |
| 288 | $list = DirEnum::getAutoComplete($enums[$type], $q); |
| 289 | } else { |
| 290 | $list = DirEnum::getAutoComplete($enums[$type], $q, $sub_id); |
| 291 | } |
| 292 | $to_cache = ''; |
| 293 | foreach ($list as &$item) { |
| 294 | $to_cache .= $item['field'] . "\t" . $item['nb'] . "\t" . $item['id'] . "\n"; |
| 295 | $item['value'] = self::format_autocomplete($item); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | $count = count($list); |
| 300 | if ($count == DirEnumeration::AUTOCOMPLETE_LIMIT) { |
| 301 | $list[] = array( |
| 302 | 'value' => '…', |
| 303 | 'field' => '', |
| 304 | 'nb' => 0, |
| 305 | 'id' => -1 |
| 306 | ); |
| 307 | } elseif ($count == 0) { |
| 308 | $list[] = array( |
| 309 | 'value' => 'Aucun camarade trouvé pour ' . $q . '.', |
| 310 | 'field' => '', |
| 311 | 'nb' => 0, |
| 312 | 'id' => -1 |
| 313 | ); |
| 314 | } |
| 315 | |
| 316 | if (!$cached) { |
| 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); |
| 321 | } |
| 322 | echo json_encode($list); |
| 323 | exit(); |
| 324 | } |
| 325 | |
| 326 | function handler_list($page, $type = null, $idVal = null) |
| 327 | { |
| 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"); |
| 331 | |
| 332 | // Give the list of all values possible of type and builds a select input for it |
| 333 | $ids = null; |
| 334 | |
| 335 | switch ($type) { |
| 336 | case 'binet': |
| 337 | $ids = DirEnum::getOptionsIter(DirEnum::BINETS); |
| 338 | break; |
| 339 | case 'networking_type': |
| 340 | $ids = DirEnum::getOptionsIter(DirEnum::NETWORKS); |
| 341 | break; |
| 342 | case 'country': |
| 343 | $ids = DirEnum::getOptionsIter(DirEnum::COUNTRIES); |
| 344 | $page->assign('onchange', 'changeAddressComponents(\'' . $type . '\', this.value)'); |
| 345 | break; |
| 346 | case 'administrative_area_level_1': |
| 347 | case 'administrative_area_level_2': |
| 348 | case 'locality': |
| 349 | $page->assign('onchange', 'changeAddressComponents(\'' . $type . '\', this.value)'); |
| 350 | case 'postal_code': |
| 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); |
| 360 | break; |
| 361 | case 'diploma': |
| 362 | if (Env::has('school') && Env::i('school') != 0) { |
| 363 | $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES, Env::i('school')); |
| 364 | } else { |
| 365 | $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES); |
| 366 | } |
| 367 | break; |
| 368 | case 'groupex': |
| 369 | $ids = DirEnum::getOptionsIter(DirEnum::GROUPESX); |
| 370 | break; |
| 371 | case 'nationalite': |
| 372 | $ids = DirEnum::getOptionsIter(DirEnum::NATIONALITIES); |
| 373 | break; |
| 374 | case 'school': |
| 375 | $ids = DirEnum::getOptionsIter(DirEnum::EDUSCHOOLS); |
| 376 | $page->assign('onchange', 'changeSchool(this.value)'); |
| 377 | break; |
| 378 | case 'section': |
| 379 | $ids = DirEnum::getOptionsIter(DirEnum::SECTIONS); |
| 380 | break; |
| 381 | case 'jobterm': |
| 382 | if (Env::has('jtid')) { |
| 383 | JobTerms::ajaxGetBranch($page, JobTerms::ONLY_JOBS); |
| 384 | return; |
| 385 | } else { |
| 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>'; |
| 394 | echo '</div>'; |
| 395 | exit(); |
| 396 | } |
| 397 | default: exit(); |
| 398 | } |
| 399 | if (isset($idVal)) { |
| 400 | pl_content_headers("text/plain"); |
| 401 | echo $ids[$idVal]; |
| 402 | exit(); |
| 403 | } |
| 404 | pl_content_headers("text/xml"); |
| 405 | $page->changeTpl('include/field.select.tpl', NO_SKIN); |
| 406 | $page->assign('list', $ids); |
| 407 | } |
| 408 | |
| 409 | function handler_referent($page, $action = null, $subaction = null) |
| 410 | { |
| 411 | global $globals; |
| 412 | |
| 413 | $wp = new PlWikiPage('Docs.Emploi'); |
| 414 | $wp->buildCache(); |
| 415 | |
| 416 | $page->setTitle('Emploi et Carrières'); |
| 417 | $page->addJsLink('jquery.ui.xorg.js'); |
| 418 | |
| 419 | // Count mentors |
| 420 | $res = XDB::query("SELECT count(distinct pid) FROM profile_mentor_term"); |
| 421 | $page->assign('mentors_number', $res->fetchOneCell()); |
| 422 | |
| 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.'); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | $page->changeTpl('search/referent.tpl'); |
| 444 | } |
| 445 | |
| 446 | /** |
| 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 |
| 450 | */ |
| 451 | function handler_referent_countries($page, $jtid = null) |
| 452 | { |
| 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); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 469 | ?> |