2 /***************************************************************************
3 * Copyright (C) 2003-2009 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
),
29 'advanced_search.php' => $this->make_hook('redir_advanced', AUTH_PUBLIC
),
30 'search/autocomplete' => $this->make_hook('autocomplete', AUTH_COOKIE
, 'user', NO_AUTH
),
31 'search/list' => $this->make_hook('list', AUTH_COOKIE
, 'user', NO_AUTH
),
35 function handler_redir_advanced(&$page, $mode = null
)
37 pl_redirect('search/adv');
41 function form_prepare()
43 Platal
::page()->assign('formulaire',1);
46 function get_diplomas($school = null
)
48 if (is_null($school) && Env
::has('school')) {
49 $school = Env
::i('school');
52 if ((!is_null($school)) && ($school != '')) {
53 $sql = 'SELECT degreeid
54 FROM profile_education_degree
55 WHERE eduid=' . $school;
58 FROM profile_education_degree_enum
62 $res = XDB
::query($sql);
63 Platal
::page()->assign('choix_diplomas', $res->fetchColumn());
66 FROM profile_education_degree_enum
68 $res = XDB
::query($sql);
69 Platal
::page()->assign('name_diplomas', $res->fetchColumn());
72 function handler_quick(&$page, $action = null
, $subaction = null
)
76 $res = XDB
::query("SELECT MIN(`diminutif`), MAX(`diminutif`)
78 WHERE `cat` = 'Promotions'");
79 list($min, $max) = $res->fetchOneRow();
80 $page->assign('promo_min', $min);
81 $page->assign('promo_max', $max);
83 if (Env
::has('quick') ||
$action == 'geoloc') {
84 $quick = trim(Env
::v('quick'));
85 if (S
::logged() && !Env
::has('page')) {
86 S
::logger()->log('search', 'quick=' . $quick);
88 $list = 'profile|prf|fiche|fic|referent|ref|mentor';
90 $list .= '|admin|adm|ax';
92 if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.\d{2,4})?)?)$/', replace_accent($quick), $matches)) {
95 case 'admin': case 'adm':
96 $base = 'admin/user/';
99 $base = 'profile/ax/';
101 case 'profile': case 'prf': case 'fiche': case 'fic':
104 case 'referent': case 'ref': case 'mentor':
109 $user = User
::getSilent($login);
111 pl_redirect($base . $user->login());
113 $_REQUEST['quick'] = $login;
114 $_GET['quick'] = $login;
115 } elseif (strpos($quick, 'doc:') === 0) {
116 $url = 'Docs/Recherche?';
117 $url .= 'action=search&q=' . urlencode(substr($quick, 4));
118 $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review');
122 $page->assign('formulaire', 0);
124 require_once 'userset.inc.php';
125 $view = new SearchSet(true
, $action == 'geoloc' && substr($subaction, -3) == 'swf');
126 $view->addMod('minifiche', 'Mini-fiches', true
, array('with_score' => true
));
127 if (S
::logged() && !Env
::i('nonins')) {
128 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
, 'with_score' => true
));
129 $view->addMod('geoloc', 'Planisphère', false
, array('with_annu' => 'search/adv'));
131 $view->apply('search', $page, $action, $subaction);
133 $nb_tot = $view->count();
134 $page->assign('search_results_nb', $nb_tot);
138 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
139 new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
140 } elseif ($nb_tot > $globals->search
->private_max
) {
141 new ThrowError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.');
142 } elseif (empty($nb_tot)) {
143 new ThrowError('Il n\'existe personne correspondant à ces critères dans la base !');
146 $page->assign('formulaire',1);
147 $page->addJsLink('ajax.js');
150 $this->load('search.inc.php');
151 $page->changeTpl('search/index.tpl');
152 $page->setTitle('Annuaire');
155 function handler_advanced(&$page, $action = null
, $subaction = null
)
158 require_once 'geoloc.inc.php';
159 $this->load('search.inc.php');
160 $page->assign('advanced',1);
161 $page->addJsLink('jquery.autocomplete.js');
163 if (!Env
::has('rechercher') && $action != 'geoloc') {
164 $this->form_prepare();
167 'country' => array('field' => 'iso_3166_1_a2', 'table' => 'geoloc_countries', 'text' => 'countryFR',
169 'fonction' => array('field' => 'id', 'table' => 'fonctions_def', 'text' => 'fonction_fr', 'exact' => true
),
170 'secteur' => array('field' => 'id', 'table' => 'profile_job_sector_enum', 'text' => 'name', 'exact' => false
),
171 'nationalite' => array('field' => 'iso_3166_1_a2', 'table' => 'geoloc_countries',
172 'text' => 'nationalityFR', 'exact' => 'false'),
173 'binet' => array('field' => 'id', 'table' => 'binets_def', 'text' => 'text', 'exact' => false
),
174 'networking_type' => array('field' => 'network_type', 'table' => 'profile_networking_enum',
175 'text' => 'name', 'exact' => false
),
176 'groupex' => array('field' => 'id', 'table' => 'groupex.asso',
177 'text' => "(cat = 'GroupesX' OR cat = 'Institutions') AND pub = 'public' AND nom",
179 'section' => array('field' => 'id', 'table' => 'sections', 'text' => 'text', 'exact' => false
),
180 'school' => array('field' => 'id', 'table' => 'profile_education_enum', 'text' => 'name', 'exact' => false
),
181 'city' => array('table' => 'geoloc_localities', 'text' => 'name', 'exact' => false
)
183 if (!Env
::has('page')) {
184 S
::logger()->log('search', 'adv=' . var_export($_GET, true
));
186 foreach ($textFields as $field=>&$query) {
187 if (!Env
::v($field) && Env
::v($field . 'Txt')) {
188 $res = XDB
::query("SELECT {$query['field']}
189 FROM {$query['table']}
190 WHERE {$query['text']} " . ($query['exact'] ?
" = {?}" :
191 " LIKE CONCAT('%', {?}, '%')"),
192 Env
::v($field . 'Txt'));
193 $_REQUEST[$field] = $res->fetchOneCell();
197 require_once 'userset.inc.php';
198 $view = new SearchSet(false
, $action == 'geoloc' && substr($subaction, -3) == 'swf');
199 $view->addMod('minifiche', 'Mini-fiches', true
);
200 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
));
201 //$view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'search/adv'));
202 $view->apply('search/adv', $page, $action, $subaction);
207 $nb_tot = $view->count();
208 if ($nb_tot > $globals->search
->private_max
) {
209 $this->form_prepare();
210 new ThrowError('Recherche trop générale.');
214 $page->changeTpl('search/index.tpl', $action == 'mini' ? SIMPLE
: SKINNED
);
215 $page->addJsLink('ajax.js');
216 $page->assign('public_directory',0);
219 function handler_autocomplete(&$page, $type = null
)
221 // Autocompletion : according to type required, return
222 // a list of results matching with the number of matches.
223 // The output format is :
227 header('Content-Type: text/plain; charset="UTF-8"');
228 $q = preg_replace(array('/\*+$/', // always look for $q*
229 '/([\^\$\[\]])/', // escape special regexp char
230 '/\*/'), // replace joker by regexp joker
237 // try to look in cached results
238 $cache = XDB
::query('SELECT `result`
239 FROM `search_autocomplete`
240 WHERE `name` = {?} AND
242 `generated` > NOW() - INTERVAL 1 DAY',
244 if ($res = $cache->fetchOneCell()) {
250 $unique = '`user_id`';
251 $db = '`auth_user_md5`';
255 $qsearch = str_replace(array('%', '_'), '', $q);
260 $db = '`binets_def` INNER JOIN
261 `binets_ins` ON(`binets_def`.`id` = `binets_ins`.`binet_id`)';
262 $field = '`binets_def`.`text`';
265 $realid = '`binets_def`.`id`';
267 case 'networking_typeTxt':
268 $db = '`profile_networking_enum` INNER JOIN
269 `profile_networking` ON(`profile_networking`.`network_type` = `profile_networking_enum`.`network_type`)';
270 $field = '`profile_networking_enum`.`name`';
272 $realid = '`profile_networking_enum`.`network_type`';
275 $db = 'geoloc_localities INNER JOIN
276 profile_addresses ON (geoloc_localities.id = profile_addresses.localityId)';
278 $field ='geoloc_localities.name';
281 $db = 'geoloc_countries INNER JOIN
282 profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
284 $field = 'geoloc_countries.countryFR';
285 $realid = 'geoloc_countries.iso_3166_1_a2';
288 $db = 'profile_job_enum INNER JOIN
289 profile_job ON (profile_job.jobid = profile_job_enum.id)';
290 $field = 'profile_job_enum.name';
291 $unique = 'profile_job.uid';
294 $db = 'fonctions_def INNER JOIN
295 profile_job ON (profile_job.fonctionid = fonctions_def.id)';
296 $field = 'fonction_fr';
298 $realid = 'fonctions_def.id';
302 $db = "groupex.asso AS a INNER JOIN
303 groupex.membres AS m ON(a.id = m.asso_id
304 AND (a.cat = 'GroupesX' OR a.cat = 'Institutions')
305 AND a.pub = 'public')";
307 $field2 = 'a.diminutif';
313 case 'nationaliteTxt':
314 $db = 'geoloc_countries INNER JOIN
315 auth_user_md5 ON (geoloc_countries.a2 = auth_user_md5.nationalite
316 OR geoloc_countries.a2 = auth_user_md5.nationalite2
317 OR geoloc_countries.a2 = auth_user_md5.nationalite3)';
318 $field = 'geoloc_countries.nationalityFR';
319 $realid = 'geoloc_countries.iso_3166_1_a2';
323 $field = 'description';
327 $db = 'profile_education_enum INNER JOIN
328 profile_education ON (profile_education_enum.id = profile_education.eduid)';
329 $field = 'profile_education_enum.name';
331 $realid = 'profile_education_enum.id';
336 $db = 'profile_job_sector_enum INNER JOIN
337 profile_job ON (profile_job.sectorid = profile_job_sector_enum.id)';
338 $field = 'profile_job_sector_enum.name';
339 $realid = 'profile_job_sector_enum.id';
344 $db = 'profile_job_subsubsector_enum';
351 $db = '`sections` INNER JOIN
352 `auth_user_md5` ON(`auth_user_md5`.`section` = `sections`.`id`)';
353 $field = '`sections`.`text`';
354 $realid = '`sections`.`id`';
360 function make_field_test($fields, $beginwith) {
362 $tests[] = $fields . ' LIKE CONCAT({?}, \'%\')';
364 $tests[] = $fields . ' LIKE CONCAT(\'% \', {?}, \'%\')';
365 $tests[] = $fields . ' LIKE CONCAT(\'%-\', {?}, \'%\')';
367 return '(' . implode(' OR ', $tests) . ')';
369 $field_select = $field;
370 $field_t = make_field_test($field, $beginwith);
372 $field2_t = make_field_test($field2, $beginwith);
373 $field_select = 'IF(' . $field_t . ', ' . $field . ', ' . $field2. ')';
375 $list = XDB
::iterator('SELECT ' . $field_select . ' AS field'
376 . ($distinct ?
(', COUNT(DISTINCT ' . $unique . ') AS nb') : '')
377 . ($realid ?
(', ' . $realid . ' AS id') : '') . '
380 ($field2 ?
(' OR ' . $field2_t) : '') . '
381 GROUP BY ' . $field_select . '
382 ORDER BY ' . ($distinct ?
'nb DESC' : $field_select) . '
384 $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch,
385 $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch);
389 while ($result = $list->next()) {
391 if ($nbResults == 11) {
394 $res .= $result['field'].'|';
395 if (isset($result['nb'])) {
396 $res .= $result['nb'];
398 if (isset($result['id'])) {
399 $res .= '|'.$result['id'];
404 XDB
::query('REPLACE INTO `search_autocomplete`
405 VALUES ({?}, {?}, {?}, NOW())',
411 function handler_list(&$page, $type = null
, $idVal = null
)
413 // Give the list of all values possible of type and builds a select input for it
420 $db = '`binets_def`';
422 case 'networking_type':
423 $db = '`profile_networking_enum`';
425 $id = '`network_type`';
428 $db = 'geoloc_countries';
429 $field = 'countryFR';
430 $id = 'iso_3166_1_a2';
431 $page->assign('onchange', 'changeCountry(this.value)');
434 $db = '`fonctions_def`';
435 $field = '`fonction_fr`';
438 header('Content-Type: text/xml; charset="UTF-8"');
439 $this->get_diplomas();
440 $page->changeTpl('search/adv.grade.form.tpl', NO_SKIN
);
443 $db = 'groupex.asso';
444 $where = " WHERE (cat = 'GroupesX' OR cat = 'Institutions') AND pub = 'public'";
448 $db = 'geoloc_countries INNER JOIN
449 auth_user_md5 ON (geoloc_countries.iso_3166_1_a2 = auth_user_md5.nationalite
450 OR geoloc_countries.iso_3166_1_a2 = auth_user_md5.nationalite2
451 OR geoloc_countries.iso_3166_1_a2 = auth_user_md5.nationalite3)';
452 $field = 'nationalityFR';
453 $id = 'iso_3166_1_a2';
456 $db = 'geoloc_administrativeareas';
459 if (isset($_REQUEST['country'])) {
460 $where .= ' WHERE country = "' . $_REQUEST['country'] . '"';
464 $db = 'profile_education_enum';
467 $page->assign('onchange', 'changeSchool(this.value)');
473 $db = 'profile_job_sector_enum INNER JOIN
474 profile_job ON (profile_job.sectorid = profile_job_sector_enum.id)';
475 $field = 'profile_job_sector_enum.name';
476 $id = 'profile_job_sector_enum.id';
481 header('Content-Type: text/plain; charset="UTF-8"');
482 $result = XDB
::query('SELECT '.$field.' AS field FROM '.$db.' WHERE '.$id.' = {?} LIMIT 1',$idVal);
483 echo $result->fetchOneCell();
486 header('Content-Type: text/xml; charset="UTF-8"');
487 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
488 $page->assign('name', $type);
489 $page->assign('list', XDB
::iterator('SELECT '.$field.' AS field,
494 $page->assign('with_text_value', true
);
495 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
499 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: