2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 on_subscribe($forlife, $uid, $promo, $pass)
43 require_once 'user.func.inc.php';
48 function form_prepare()
50 Platal
::page()->assign('formulaire',1);
53 function get_diplomas($school = null
)
55 if (is_null($school) && Env
::has('school')) {
56 $school = Env
::i('school');
59 if (!is_null($school)) {
60 $sql = 'SELECT type FROM applis_def WHERE id=' . $school;
62 $sql = 'DESCRIBE applis_def type';
65 $res = XDB
::query($sql);
66 $row = $res->fetchOneRow();
67 if (!is_null($school)) {
70 $types = explode('(',$row[1]);
71 $types = str_replace("'","",substr($types[1],0,-1));
73 Platal
::page()->assign('choix_diplomas', explode(',',$types));
76 function handler_quick(&$page, $action = null
, $subaction = null
)
80 $res = XDB
::query("SELECT MIN(`diminutif`), MAX(`diminutif`)
82 WHERE `cat` = 'Promotions'");
83 list($min, $max) = $res->fetchOneRow();
84 $page->assign('promo_min', $min);
85 $page->assign('promo_max', $max);
87 if (Env
::has('quick') ||
$action == 'geoloc') {
88 $quick = trim(Env
::v('quick'));
89 if (S
::logged() && !Env
::has('page')) {
90 S
::logger()->log('search', 'quick=' . $quick);
92 $list = 'profile|prf|fiche|fic|referent|ref|mentor';
94 $list .= '|admin|adm|ax';
96 if (preg_match('/^(' . $list . '):([-a-z]+(\.[-a-z]+(\.\d{2,4})?)?)$/', replace_accent($quick), $matches)) {
99 case 'admin': case 'adm':
100 $base = 'admin/user/';
103 $base = 'profile/ax/';
105 case 'profile': case 'prf': case 'fiche': case 'fic':
108 case 'referent': case 'ref': case 'mentor':
113 $user = User
::getSilent($login);
115 pl_redirect($base . $user->login());
117 $_REQUEST['quick'] = $forlife;
118 $_GET['quick'] = $forlife;
119 } elseif (strpos($quick, 'doc:') === 0) {
120 $url = 'Docs/Recherche?';
121 $url .= 'action=search&q=' . urlencode(substr($quick, 4));
122 $url .= '&group=' . urlencode('-Equipe,-Main,-PmWiki,-Site,-Review');
126 $page->assign('formulaire', 0);
128 require_once 'userset.inc.php';
129 $view = new SearchSet(true
, $action == 'geoloc' && substr($subaction, -3) == 'swf');
130 $view->addMod('minifiche', 'Mini-fiches', true
, array('with_score' => true
));
131 if (S
::logged() && !Env
::i('nonins')) {
132 $view->addMod('trombi', 'Trombinoscope', false
, array('with_promo' => true
, 'with_score' => true
));
133 $view->addMod('geoloc', 'Planisphère', false
, array('with_annu' => 'search/adv'));
135 $view->apply('search', $page, $action, $subaction);
137 $nb_tot = $view->count();
138 $page->assign('search_results_nb', $nb_tot);
142 if (!S
::logged() && $nb_tot > $globals->search
->public_max
) {
143 new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
144 } elseif ($nb_tot > $globals->search
->private_max
) {
145 new ThrowError('Recherche trop générale. Une <a href="search/adv">recherche avancée</a> permet de préciser la recherche.');
146 } elseif (empty($nb_tot)) {
147 new ThrowError('Il n\'existe personne correspondant à ces critères dans la base !');
150 $page->assign('formulaire',1);
151 $page->addJsLink('ajax.js');
154 $this->load('search.inc.php');
155 $page->changeTpl('search/index.tpl');
156 $page->setTitle('Annuaire');
159 function handler_advanced(&$page, $action = null
, $subaction = null
)
162 require_once 'geoloc.inc.php';
163 $this->load('search.inc.php');
164 $page->assign('advanced',1);
165 $page->addJsLink('jquery.autocomplete.js');
167 if (!Env
::has('rechercher') && $action != 'geoloc') {
168 $this->form_prepare();
171 'country' => array('field' => 'a2', 'table' => 'geoloc_pays', 'text' => 'pays', 'exact' => false
),
172 'fonction' => array('field' => 'id', 'table' => 'fonctions_def', 'text' => 'fonction_fr', 'exact' => true
),
173 'secteur' => array('field' => 'id', 'table' => 'emploi_secteur', 'text' => 'label', 'exact' => false
),
174 'nationalite' => array('field' => 'a2', 'table' => 'geoloc_pays', 'text' => 'nat', 'exact' => 'false'),
175 'binet' => array('field' => 'id', 'table' => 'binets_def', 'text' => 'text', 'exact' => false
),
176 'groupex' => array('field' => 'id', 'table' => 'groupex.asso',
177 'text' => "(a.cat = 'GroupesX' OR a.cat = 'Institutions') AND pub = 'public' AND nom",
179 'section' => array('field' => 'id', 'table' => 'sections', 'text' => 'text', 'exact' => false
),
180 'school' => array('field' => 'id', 'table' => 'applis_def', 'text' => 'text', 'exact' => false
),
181 'city' => array('table' => 'geoloc_city', '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);
259 $db = '`binets_def` INNER JOIN
260 `binets_ins` ON(`binets_def`.`id` = `binets_ins`.`binet_id`)';
261 $field='`binets_def`.`text`';
264 $realid = '`binets_def`.`id`';
267 $db = '`geoloc_city` INNER JOIN
268 `adresses` ON(`geoloc_city`.`id` = `adresses`.`cityid`)';
270 $field='`geoloc_city`.`name`';
273 $db = '`geoloc_pays` INNER JOIN
274 `adresses` ON(`geoloc_pays`.`a2` = `adresses`.`country`)';
276 $field = '`geoloc_pays`.`pays`';
277 $field2 = '`geoloc_pays`.`country`';
278 $realid='`geoloc_pays`.`a2`';
281 $db = '`entreprises`';
282 $field = '`entreprise`';
290 $db = '`fonctions_def` INNER JOIN
291 `entreprises` ON(`entreprises`.`fonction` = `fonctions_def`.`id`)';
292 $field = '`fonction_fr`';
294 $realid = '`fonctions_def`.`id`';
298 $db = "groupex.asso AS a INNER JOIN
299 groupex.membres AS m ON(a.id = m.asso_id
300 AND (a.cat = 'GroupesX' OR a.cat = 'Institutions')
301 AND a.pub = 'public')";
303 $field2 = 'a.diminutif';
311 $field2 = '`nom_usage`';
314 case 'nationaliteTxt':
315 $db = '`geoloc_pays` INNER JOIN
316 `auth_user_md5` ON(`geoloc_pays`.`a2` = `auth_user_md5`.`nationalite`)';
317 $field = 'IF(`geoloc_pays`.`nat`=\'\',
318 `geoloc_pays`.`pays`,
319 `geoloc_pays`.`nat`)';
320 $realid = '`geoloc_pays`.`a2`';
323 $field = '`profile_nick`';
324 $db = '`auth_user_quick`';
328 $db = '`entreprises`';
333 $db = '`applis_def` INNER JOIN
334 `applis_ins` ON(`applis_def`.`id` = `applis_ins`.`aid`)';
335 $field='`applis_def`.`text`';
337 $realid = '`applis_def`.`id`';
342 $db = '`emploi_secteur` INNER JOIN
343 `entreprises` ON(`entreprises`.`secteur` = `emploi_secteur`.`id`)';
344 $field = '`emploi_secteur`.`label`';
345 $realid = '`emploi_secteur`.`id`';
350 $db = '`sections` INNER JOIN
351 `auth_user_md5` ON(`auth_user_md5`.`section` = `sections`.`id`)';
352 $field = '`sections`.`text`';
353 $realid = '`sections`.`id`';
359 function make_field_test($fields, $beginwith) {
361 $tests[] = $fields . ' LIKE CONCAT({?}, \'%\')';
363 $tests[] = $fields . ' LIKE CONCAT(\'% \', {?}, \'%\')';
364 $tests[] = $fields . ' LIKE CONCAT(\'%-\', {?}, \'%\')';
366 return '(' . implode(' OR ', $tests) . ')';
368 $field_select = $field;
369 $field_t = make_field_test($field, $beginwith);
371 $field2_t = make_field_test($field2, $beginwith);
372 $field_select = 'IF(' . $field_t . ', ' . $field . ', ' . $field2. ')';
374 $list = XDB
::iterator('SELECT ' . $field_select . ' AS field,
375 COUNT(DISTINCT ' . $unique . ') AS nb
376 ' . ($realid ?
(', ' . $realid . ' AS id') : '') . '
379 ($field2 ?
(' OR ' . $field2_t) : '') . '
380 GROUP BY ' . $field_select . '
383 $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch,
384 $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch, $qsearch);
387 while ($result = $list->next()) {
389 if ($nbResults == 11) {
392 $res .= $result['field'].'|';
393 $res .= $result['nb'];
394 if (isset($result['id'])) {
395 $res .= '|'.$result['id'];
400 XDB
::query('REPLACE INTO `search_autocomplete`
401 VALUES ({?}, {?}, {?}, NOW())',
407 function handler_list(&$page, $type = null
, $idVal = null
)
409 // Give the list of all values possible of type and builds a select input for it
416 $db = '`binets_def`';
419 $db = '`geoloc_pays`';
422 $page->assign('onchange', 'changeCountry(this.value)');
425 $db = '`fonctions_def`';
426 $field = '`fonction_fr`';
429 header('Content-Type: text/xml; charset="UTF-8"');
430 $this->get_diplomas();
431 $page->changeTpl('search/adv.grade.form.tpl', NO_SKIN
);
434 $db = 'groupex.asso';
435 $where = " WHERE (cat = 'GroupesX' OR cat = 'Institutions') AND pub = 'public'";
439 $db = '`geoloc_pays` INNER JOIN
440 `auth_user_md5` ON (`geoloc_pays`.`a2` = `auth_user_md5`.`nationalite`)';
441 $field = 'IF(`nat`=\'\', `pays`, `nat`)';
445 $db = '`geoloc_region`';
448 if (isset($_REQUEST['country'])) {
449 $where .= ' WHERE `a2` = "'.$_REQUEST['country'].'"';
453 $db = '`applis_def`';
454 $page->assign('onchange', 'changeSchool(this.value)');
460 $db = '`emploi_secteur`';
466 header('Content-Type: text/plain; charset="UTF-8"');
467 $result = XDB
::query('SELECT '.$field.' AS field FROM '.$db.' WHERE '.$id.' = {?} LIMIT 1',$idVal);
468 echo $result->fetchOneCell();
471 header('Content-Type: text/xml; charset="UTF-8"');
472 $page->changeTpl('include/field.select.tpl', NO_SKIN
);
473 $page->assign('name', $type);
474 $page->assign('list', XDB
::iterator('SELECT '.$field.' AS field,
479 $page->assign('with_text_value', true
);
480 $page->assign('onchange', "document.forms.recherche.{$type}Txt.value = this.options[this.selectedIndex].text");
484 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: