Moves referent search from profile to search module
[platal.git] / modules / search.php
CommitLineData
09824164 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 Polytechnique.org *
09824164 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
22class SearchModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
abed2826 27 'search' => $this->make_hook('quick', AUTH_PUBLIC),
20f36c54 28 'search/adv' => $this->make_hook('advanced', AUTH_COOKIE, 'directory_ax'),
ba2afb88 29 'advanced_search.php' => $this->make_hook('redir_advanced', AUTH_PUBLIC),
20f36c54
FB
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),
9dc65790
PC
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),
09824164 35 );
36 }
37
ba2afb88 38 function handler_redir_advanced(&$page, $mode = null)
39 {
40 pl_redirect('search/adv');
41 exit;
42 }
43
09824164 44 function form_prepare()
45 {
d7610c35 46 Platal::page()->assign('formulaire',1);
3c640222 47 }
48
8eb6931f
RB
49 /**
50 * $model: The way of presenting the results: minifiche, trombi, geoloc.
51 * $byletter: Show only names beginning with this letter
52 */
53 function handler_quick(&$page, $model = null, $byletter = null)
09824164 54 {
55 global $globals;
56
8eb6931f 57 if (Env::has('quick') || $model == 'geoloc') {
dd23c027 58 $quick = Env::t('quick');
85bb670b 59 if (S::logged() && !Env::has('page')) {
732e5855 60 S::logger()->log('search', 'quick=' . $quick);
e31c1c3e 61 }
dd23c027
SJ
62
63 if ($quick == '') {
64 $page->trigWarning('Aucun critère de recherche n\'est spécifié.');
65 $page->changeTpl('search/index.tpl');
66 $page->setTitle('Annuaire');
67 $page->assign('formulaire', 1);
68 $page->addJsLink('ajax.js');
69 return;
70 }
71
383bcdec 72 $list = 'profile|prf|fiche|fic|referent|ref|mentor';
dd70cd28 73 if (S::admin()) {
383bcdec 74 $list .= '|admin|adm|ax';
75 }
11647113 76 if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.\d{2,4})?)?)$/', replace_accent($quick), $matches)) {
4514bea3 77 $login = $matches[2];
383bcdec 78 switch($matches[1]) {
79 case 'admin': case 'adm':
80 $base = 'admin/user/';
81 break;
82 case 'ax':
83 $base = 'profile/ax/';
84 break;
85 case 'profile': case 'prf': case 'fiche': case 'fic':
86 $base = 'profile/';
87 break;
88 case 'referent': case 'ref': case 'mentor':
89 $base = 'referent/';
90 break;
91 }
7511200d 92
4514bea3
VZ
93 $user = User::getSilent($login);
94 if ($user) {
95 pl_redirect($base . $user->login());
7511200d 96 }
90c614cd
VZ
97 $_REQUEST['quick'] = $login;
98 $_GET['quick'] = $login;
8fee4fbd 99 } elseif (strpos($quick, 'doc:') === 0) {
100 $url = 'Docs/Recherche?';
101 $url .= 'action=search&q=' . urlencode(substr($quick, 4));
7511200d 102 $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review');
8fee4fbd 103 pl_redirect($url);
85b45717
FB
104 } elseif (strpos($quick, 'trombi:') === 0) {
105 $promo = substr($quick, 7);
106 $res = XDB::query("SELECT diminutif
107 FROM groups
108 WHERE cat = 'Promotions' AND diminutif = {?}",
109 $promo);
110 if ($res->numRows() == 0) {
111 $page->trigWarning("La promotion demandée n'est pas valide: $promo");
112 } else {
113 http_redirect('http://www.polytechnique.net/login/' . $promo . '/annuaire/trombi');
114 }
383bcdec 115 }
8fee4fbd 116
09824164 117 $page->assign('formulaire', 0);
118
8c4a0c30 119 require_once 'userset.inc.php';
8eb6931f
RB
120 $view = new SearchSet(true);
121 $view->addMod('minifiche', 'Mini-fiches', true, array('with_score' => true, 'starts_with' => $byletter));
35fa92e8 122 if (S::logged() && !Env::i('nonins')) {
123 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true, 'with_score' => true));
1fe46b8f
SJ
124 // TODO: Reactivate when the new map is completed.
125 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
35fa92e8 126 }
8eb6931f 127 $view->apply('search', $page, $model);
09824164 128
8c4a0c30 129 $nb_tot = $view->count();
e35882be 130 $page->assign('search_results_nb', $nb_tot);
cab08090 131 if (!S::logged() && $nb_tot > $globals->search->public_max) {
825a13a4 132 $page->trigError('Votre recherche a généré trop de résultats pour un affichage public.');
09824164 133 } elseif ($nb_tot > $globals->search->private_max) {
825a13a4 134 $page->trigError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.');
09824164 135 } elseif (empty($nb_tot)) {
825a13a4 136 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
09824164 137 }
138 } else {
139 $page->assign('formulaire',1);
e654517d 140 $page->addJsLink('ajax.js');
09824164 141 }
142
eaf30d86 143 $page->changeTpl('search/index.tpl');
46f272fe 144 $page->setTitle('Annuaire');
09824164 145 }
146
8eb6931f
RB
147 /** $model is the way of presenting the results: minifiche, trombi, geoloc.
148 */
149 function handler_advanced(&$page, $model = null, $byletter = null)
09824164 150 {
151 global $globals;
c7c2a181 152 require_once 'geocoding.inc.php';
bc67c37c 153 $page->assign('advanced',1);
4b4b4b67 154 $page->addJsLink('jquery.autocomplete.js');
bc67c37c 155
8eb6931f 156 if (!Env::has('rechercher') && $model != 'geoloc') {
09824164 157 $this->form_prepare();
158 } else {
e31c1c3e 159 if (!Env::has('page')) {
732e5855 160 S::logger()->log('search', 'adv=' . var_export($_GET, true));
e31c1c3e 161 }
137e819f 162
8c4a0c30 163 require_once 'userset.inc.php';
8eb6931f
RB
164 $view = new SearchSet(false);
165 $view->addMod('minifiche', 'Mini-fiches', true, array('starts_with' => $byletter));
35fa92e8 166 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true));
1fe46b8f
SJ
167 // TODO: Reactivate when the new map is completed.
168 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
8eb6931f 169 $view->apply('search/adv', $page, $model);
eaf30d86 170
8c4a0c30 171 $nb_tot = $view->count();
09824164 172 if ($nb_tot > $globals->search->private_max) {
173 $this->form_prepare();
825a13a4 174 $page->trigError('Recherche trop générale.');
6a48a49a
SJ
175 } else if ($nb_tot == 0) {
176 $this->form_prepare();
177 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
09824164 178 }
09824164 179 }
180
8eb6931f 181 $page->changeTpl('search/index.tpl', $model == 'mini' ? SIMPLE : SKINNED);
3c640222 182 $page->addJsLink('ajax.js');
8c4a0c30 183 $page->assign('public_directory',0);
09824164 184 }
3c640222 185
838cc16a 186 function handler_autocomplete(&$page, $type = null)
187 {
188 // Autocompletion : according to type required, return
189 // a list of results matching with the number of matches.
190 // The output format is :
191 // result1|nb1
192 // result2|nb2
193 // ...
3cb500d5 194 pl_content_headers("text/plain");
ff3eb9b7 195 $q = preg_replace(array('/\*+$/', // always look for $q*
196 '/([\^\$\[\]])/', // escape special regexp char
197 '/\*/'), // replace joker by regexp joker
198 array('',
199 '\\\\\1',
200 '.*'),
78e4b1f6 201 Env::s('q'));
838cc16a 202 if (!$q) exit();
c15afc4e 203
eaf30d86 204 // try to look in cached results
78e4b1f6
FB
205 $cache = XDB::query('SELECT result
206 FROM search_autocomplete
207 WHERE name = {?} AND
208 query = {?} AND
209 generated > NOW() - INTERVAL 1 DAY',
ff3eb9b7 210 $type, $q);
c15afc4e 211 if ($res = $cache->fetchOneCell()) {
ff3eb9b7 212 echo $res;
213 die();
c15afc4e 214 }
eaf30d86 215
8e720253
RB
216 $enums = array(
217 'binetTxt' => DirEnum::BINETS,
218 'groupexTxt' => DirEnum::GROUPESX,
219 'sectionTxt' => DirEnum::SECTIONS,
220 'networking_typeTxt' => DirEnum::NETWORKS,
221 'city' => DirEnum::LOCALITIES,
222 'countryTxt' => DirEnum::COUNTRIES,
223 'entreprise' => DirEnum::COMPANIES,
3ac45f10 224 'jobtermTxt' => DirEnum::JOBTERMS,
8e720253
RB
225 'description' => DirEnum::JOBDESCRIPTION,
226 'nationaliteTxt' => DirEnum::NATIONALITIES,
fb3b6547 227 'schoolTxt' => DirEnum::EDUSCHOOLS,
8e720253 228 );
9d44d2c7 229 if (!array_key_exists($type, $enums)) {
8e720253 230 exit();
838cc16a 231 }
232
8e720253 233 $enum = $enums[$type];
f781871c 234
8e720253 235 $list = DirEnum::getAutoComplete($enum, $q);
838cc16a 236 $nbResults = 0;
c15afc4e 237 $res = "";
2ab7a09f 238 while ($result = $list->next()) {
838cc16a 239 $nbResults++;
240 if ($nbResults == 11) {
b3ec63d5 241 $res .= $q."|-1\n";
838cc16a 242 } else {
f6818108 243 $res .= $result['field'].'|';
c7139c07
SJ
244 if (isset($result['nb'])) {
245 $res .= $result['nb'];
246 }
ff3eb9b7 247 if (isset($result['id'])) {
248 $res .= '|'.$result['id'];
249 }
250 $res .= "\n";
838cc16a 251 }
252 }
78e4b1f6 253 XDB::query('REPLACE INTO search_autocomplete
ff3eb9b7 254 VALUES ({?}, {?}, {?}, NOW())',
255 $type, $q, $res);
c15afc4e 256 echo $res;
838cc16a 257 exit();
258 }
eaf30d86 259
2ab7a09f 260 function handler_list(&$page, $type = null, $idVal = null)
261 {
ffd70398
PC
262 $page->assign('name', $type);
263 $page->assign('with_text_value', true);
264 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
265
ff3eb9b7 266 // Give the list of all values possible of type and builds a select input for it
8e720253 267 $ids = null;
ff3eb9b7 268
269 switch ($type) {
8e720253 270 case 'binet':
2998edf1 271 $ids = DirEnum::getOptionsIter(DirEnum::BINETS);
ff3eb9b7 272 break;
92c3f9e5 273 case 'networking_type':
2998edf1 274 $ids = DirEnum::getOptionsIter(DirEnum::NETWORKS);
92c3f9e5 275 break;
ff3eb9b7 276 case 'country':
2998edf1 277 $ids = DirEnum::getOptionsIter(DirEnum::COUNTRIES);
ff3eb9b7 278 $page->assign('onchange', 'changeCountry(this.value)');
279 break;
ff3eb9b7 280 case 'diploma':
8e720253 281 if (Env::has('school') && Env::i('school') != 0) {
2998edf1 282 $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES, Env::i('school'));
8e720253 283 } else {
2998edf1 284 $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES);
8e720253
RB
285 }
286 break;
ff3eb9b7 287 case 'groupex':
2998edf1 288 $ids = DirEnum::getOptionsIter(DirEnum::GROUPESX);
ff3eb9b7 289 break;
290 case 'nationalite':
2998edf1 291 $ids = DirEnum::getOptionsIter(DirEnum::NATIONALITIES);
ff3eb9b7 292 break;
ffd70398
PC
293 case 'region':
294 if (Env::has('country')) {
295 $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS, Env::v('country'));
8e720253 296 } else {
2998edf1 297 $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS);
f6818108 298 }
ff3eb9b7 299 break;
300 case 'school':
2998edf1 301 $ids = DirEnum::getOptionsIter(DirEnum::EDUSCHOOLS);
ff3eb9b7 302 $page->assign('onchange', 'changeSchool(this.value)');
303 break;
304 case 'section':
2998edf1 305 $ids = DirEnum::getOptionsIter(DirEnum::SECTIONS);
ff3eb9b7 306 break;
307 case 'secteur':
2998edf1 308 $ids = DirEnum::getOptionsIter(DirEnum::SECTORS);
ff3eb9b7 309 break;
3ac45f10
PC
310 case 'jobterm':
311 if (Env::has('jtid')) {
312 JobTerms::ajaxGetBranch(&$page, JobTerms::ONLY_JOBS);
313 return;
314 } else {
315 pl_content_headers('text/xml');
316 echo '<div>'; // global container so that response is valid xml
317 echo '<input name="jobtermTxt" type="text" style="display:none" size="32" />';
318 echo '<input name="jobterm" type="hidden"/>';
319 echo '<div class="term_tree"></div>'; // container where to create the tree
320 echo '<script type="text/javascript" src="javascript/jquery.jstree.js"></script>';
321 echo '<script type="text/javascript" src="javascript/jobtermstree.js"></script>';
322 echo '<script type="text/javascript">createJobTermsTree(".term_tree", "search/list/jobterm", "search", "searchForJobTerm");</script>';
323 echo '</div>';
324 exit();
325 }
ff3eb9b7 326 default: exit();
327 }
328 if (isset($idVal)) {
3cb500d5 329 pl_content_headers("text/plain");
8e720253 330 echo $ids[$idVal];
ff3eb9b7 331 exit();
332 }
3cb500d5 333 pl_content_headers("text/xml");
05cb05c0 334 $page->changeTpl('include/field.select.tpl', NO_SKIN);
8e720253 335 $page->assign('list', $ids);
2ab7a09f 336 }
9dc65790
PC
337
338 function handler_referent(&$page, $action = null, $subaction = null)
339 {
340 $wp = new PlWikiPage('Docs.Emploi');
341 $wp->buildCache();
342
343 $page->setTitle('Emploi et Carrières');
344
345 // nb de mentors
346 $res = XDB::query("SELECT count(distinct pid) FROM profile_mentor_term");
347 $page->assign('mentors_number', $res->fetchOneCell());
348
349 $page->addJsLink('jquery.autocomplete.js');
350
351 // On vient d'un formulaire
352 require_once 'ufbuilder.inc.php';
353 $ufb = new UFB_MentorSearch();
354 if (!$ufb->isEmpty()) {
355 require_once 'userset.inc.php';
356 $ufc = $ufb->getUFC();
357 $set = new ProfileSet($ufc);
358 $set->addMod('mentor', 'Référents');
359 $set->apply('referent/search', $page, $action, $subaction);
360 if ($set->count() > 100) {
361 $page->assign('recherche_trop_large', true);
362 }
363 }
364
365 $page->changeTpl('search/referent.tpl');
366 }
367
09824164 368}
369
a7de4ef7 370// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
09824164 371?>