Deletes all references to sectors as they have been replaced by jobterms.
[platal.git] / modules / search.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 'jobs' => $this->make_hook('referent', AUTH_COOKIE),
33 'emploi' => $this->make_hook('referent', AUTH_COOKIE),
34 'referent/search' => $this->make_hook('referent', AUTH_COOKIE),
35 'search/referent/countries' => $this->make_hook('referent_countries', AUTH_COOKIE),
36 );
37 }
38
39 function handler_redir_advanced(&$page, $mode = null)
40 {
41 pl_redirect('search/adv');
42 exit;
43 }
44
45 function form_prepare()
46 {
47 Platal::page()->assign('formulaire',1);
48 }
49
50 /**
51 * $model: The way of presenting the results: minifiche, trombi, geoloc.
52 * $byletter: Show only names beginning with this letter
53 */
54 function handler_quick(&$page, $model = null, $byletter = null)
55 {
56 global $globals;
57
58 if (Env::has('quick') || $model == 'geoloc') {
59 $quick = Env::t('quick');
60 if (S::logged() && !Env::has('page')) {
61 S::logger()->log('search', 'quick=' . $quick);
62 }
63
64 if ($quick == '') {
65 $page->trigWarning('Aucun critère de recherche n\'est spécifié.');
66 $page->changeTpl('search/index.tpl');
67 $page->setTitle('Annuaire');
68 $page->assign('formulaire', 1);
69 $page->addJsLink('ajax.js');
70 return;
71 }
72
73 $list = 'profile|prf|fiche|fic|referent|ref|mentor';
74 if (S::admin()) {
75 $list .= '|admin|adm|ax';
76 }
77 if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.\d{2,4})?)?)$/', replace_accent($quick), $matches)) {
78 $login = $matches[2];
79 switch($matches[1]) {
80 case 'admin': case 'adm':
81 $base = 'admin/user/';
82 break;
83 case 'ax':
84 $base = 'profile/ax/';
85 break;
86 case 'profile': case 'prf': case 'fiche': case 'fic':
87 $base = 'profile/';
88 break;
89 case 'referent': case 'ref': case 'mentor':
90 $base = 'referent/';
91 break;
92 }
93
94 $user = User::getSilent($login);
95 if ($user) {
96 pl_redirect($base . $user->login());
97 }
98 $_REQUEST['quick'] = $login;
99 $_GET['quick'] = $login;
100 } elseif (strpos($quick, 'doc:') === 0) {
101 $url = 'Docs/Recherche?';
102 $url .= 'action=search&q=' . urlencode(substr($quick, 4));
103 $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review');
104 pl_redirect($url);
105 } elseif (strpos($quick, 'trombi:') === 0) {
106 $promo = substr($quick, 7);
107 $res = XDB::query("SELECT diminutif
108 FROM groups
109 WHERE cat = 'Promotions' AND diminutif = {?}",
110 $promo);
111 if ($res->numRows() == 0) {
112 $page->trigWarning("La promotion demandée n'est pas valide: $promo");
113 } else {
114 http_redirect('http://www.polytechnique.net/login/' . $promo . '/annuaire/trombi');
115 }
116 }
117
118 $page->assign('formulaire', 0);
119
120 require_once 'userset.inc.php';
121 $view = new SearchSet(true);
122 $view->addMod('minifiche', 'Mini-fiches', true, array('with_score' => true, 'starts_with' => $byletter));
123 if (S::logged() && !Env::i('nonins')) {
124 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true, 'with_score' => true));
125 // TODO: Reactivate when the new map is completed.
126 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
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 $page->addJsLink('ajax.js');
142 }
143
144 $page->changeTpl('search/index.tpl');
145 $page->setTitle('Annuaire');
146 }
147
148 /** $model is the way of presenting the results: minifiche, trombi, geoloc.
149 */
150 function handler_advanced(&$page, $model = null, $byletter = null)
151 {
152 global $globals;
153 $page->assign('advanced',1);
154 $page->addJsLink('jquery.autocomplete.js');
155
156 if (!Env::has('rechercher') && $model != 'geoloc') {
157 $this->form_prepare();
158 } else {
159 if (!Env::has('page')) {
160 S::logger()->log('search', 'adv=' . var_export($_GET, true));
161 }
162
163 require_once 'userset.inc.php';
164 $view = new SearchSet(false);
165 $view->addMod('minifiche', 'Mini-fiches', true, array('starts_with' => $byletter));
166 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true));
167 // TODO: Reactivate when the new map is completed.
168 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
169 $view->apply('search/adv', $page, $model);
170
171 $nb_tot = $view->count();
172 if ($nb_tot > $globals->search->private_max) {
173 $this->form_prepare();
174 $page->trigError('Recherche trop générale.');
175 } else if ($nb_tot == 0) {
176 $this->form_prepare();
177 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base !');
178 }
179 }
180
181 $page->changeTpl('search/index.tpl', $model == 'mini' ? SIMPLE : SKINNED);
182 $page->addJsLink('ajax.js');
183 $page->assign('public_directory',0);
184 }
185
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 // ...
194 pl_content_headers("text/plain");
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 '.*'),
201 Env::s('q'));
202 if (!$q) exit();
203
204 // try to look in cached results
205 $cache = XDB::query('SELECT result
206 FROM search_autocomplete
207 WHERE name = {?} AND
208 query = {?} AND
209 generated > NOW() - INTERVAL 1 DAY',
210 $type, $q);
211 if ($res = $cache->fetchOneCell()) {
212 echo $res;
213 die();
214 }
215
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,
224 'jobtermTxt' => DirEnum::JOBTERMS,
225 'description' => DirEnum::JOBDESCRIPTION,
226 'nationaliteTxt' => DirEnum::NATIONALITIES,
227 'schoolTxt' => DirEnum::EDUSCHOOLS,
228 );
229 if (!array_key_exists($type, $enums)) {
230 exit();
231 }
232
233 $enum = $enums[$type];
234
235 $list = DirEnum::getAutoComplete($enum, $q);
236 $nbResults = 0;
237 $res = "";
238 while ($result = $list->next()) {
239 $nbResults++;
240 if ($nbResults == 11) {
241 $res .= $q."|-1\n";
242 } else {
243 $res .= $result['field'].'|';
244 if (isset($result['nb'])) {
245 $res .= $result['nb'];
246 }
247 if (isset($result['id'])) {
248 $res .= '|'.$result['id'];
249 }
250 $res .= "\n";
251 }
252 }
253 if ($nbResults == 0) {
254 $res = $q."|-2\n";
255 }
256 XDB::query('REPLACE INTO search_autocomplete
257 VALUES ({?}, {?}, {?}, NOW())',
258 $type, $q, $res);
259 echo $res;
260 exit();
261 }
262
263 function handler_list(&$page, $type = null, $idVal = null)
264 {
265 $page->assign('name', $type);
266 $page->assign('with_text_value', true);
267 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
268
269 // Give the list of all values possible of type and builds a select input for it
270 $ids = null;
271
272 switch ($type) {
273 case 'binet':
274 $ids = DirEnum::getOptionsIter(DirEnum::BINETS);
275 break;
276 case 'networking_type':
277 $ids = DirEnum::getOptionsIter(DirEnum::NETWORKS);
278 break;
279 case 'country':
280 $ids = DirEnum::getOptionsIter(DirEnum::COUNTRIES);
281 $page->assign('onchange', 'changeCountry(this.value)');
282 break;
283 case 'diploma':
284 if (Env::has('school') && Env::i('school') != 0) {
285 $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES, Env::i('school'));
286 } else {
287 $ids = DirEnum::getOptionsIter(DirEnum::EDUDEGREES);
288 }
289 break;
290 case 'groupex':
291 $ids = DirEnum::getOptionsIter(DirEnum::GROUPESX);
292 break;
293 case 'nationalite':
294 $ids = DirEnum::getOptionsIter(DirEnum::NATIONALITIES);
295 break;
296 case 'region':
297 if (Env::has('country')) {
298 $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS, Env::v('country'));
299 } else {
300 $ids = DirEnum::getOptionsIter(DirEnum::ADMINAREAS);
301 }
302 break;
303 case 'school':
304 $ids = DirEnum::getOptionsIter(DirEnum::EDUSCHOOLS);
305 $page->assign('onchange', 'changeSchool(this.value)');
306 break;
307 case 'section':
308 $ids = DirEnum::getOptionsIter(DirEnum::SECTIONS);
309 break;
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 }
326 default: exit();
327 }
328 if (isset($idVal)) {
329 pl_content_headers("text/plain");
330 echo $ids[$idVal];
331 exit();
332 }
333 pl_content_headers("text/xml");
334 $page->changeTpl('include/field.select.tpl', NO_SKIN);
335 $page->assign('list', $ids);
336 }
337
338 function handler_referent(&$page, $action = null, $subaction = null)
339 {
340 global $globals;
341
342 $wp = new PlWikiPage('Docs.Emploi');
343 $wp->buildCache();
344
345 $page->setTitle('Emploi et Carrières');
346
347 // Count mentors
348 $res = XDB::query("SELECT count(distinct pid) FROM profile_mentor_term");
349 $page->assign('mentors_number', $res->fetchOneCell());
350
351 $page->addJsLink('jquery.autocomplete.js');
352
353 // Search for mentors matching filters
354 require_once 'ufbuilder.inc.php';
355 $ufb = new UFB_MentorSearch();
356 if (!$ufb->isEmpty()) {
357 require_once 'userset.inc.php';
358 $ufc = $ufb->getUFC();
359 $set = new ProfileSet($ufc);
360 $set->addMod('mentor', 'Référents');
361 $set->apply('referent/search', $page, $action, $subaction);
362 $nb_tot = $set->count();
363 if ($nb_tot > $globals->search->private_max) {
364 $this->form_prepare();
365 $page->trigError('Recherche trop générale.');
366 $page->assign('plset_count', 0);
367 } else if ($nb_tot == 0) {
368 $this->form_prepare();
369 $page->trigError('Il n\'existe personne correspondant à ces critères dans la base.');
370 }
371 }
372
373 $page->changeTpl('search/referent.tpl');
374 }
375
376 /**
377 * Builds a select field to choose among countries that referents
378 * know about. Only referents linked to term (jtid) are displayed.
379 * @param $jtid id of job term to restrict referents
380 */
381 function handler_referent_countries(&$page, $jtid = null)
382 {
383 pl_content_headers("text/xml");
384 $page->changeTpl('include/field.select.tpl', NO_SKIN);
385 $page->assign('name', 'country');
386 $it = XDB::iterator("SELECT gc.iso_3166_1_a2 AS id, gc.countryFR AS field
387 FROM geoloc_countries AS gc
388 INNER JOIN profile_mentor_country AS mp ON (mp.country = gc.iso_3166_1_a2)
389 INNER JOIN profile_mentor_term AS mt ON (mt.pid = mp.pid)
390 INNER JOIN profile_job_term_relation AS jtr ON (jtr.jtid_2 = mt.jtid)
391 WHERE jtr.jtid_1 = {?}
392 GROUP BY iso_3166_1_a2
393 ORDER BY countryFR", $jtid);
394 $page->assign('list', $it);
395 }
396 }
397
398 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
399 ?>