[SPIP] ~maj v3.0.14-->v3.0.17
[ptitvelo/web/www.git] / www / ecrire / exec / rechercher.php
1 <?php
2 /***************************************************************************\
3 * SPIP, Systeme de publication pour l'internet *
4 * *
5 * Copyright (c) 2001-2014 *
6 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
7 * *
8 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
9 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
10 \***************************************************************************/
11
12 if (!defined('_ECRIRE_INC_VERSION')) return;
13
14 include_spip('inc/actions');
15 include_spip('inc/texte');
16
17 // http://doc.spip.org/@exec_rechercher_dist
18 function exec_rechercher_dist(){
19 $id = intval(_request('id'));
20 $exclus = intval(_request('exclus'));
21 $rac = spip_htmlentities(_request('rac'));
22 $type = _request('type');
23 $do = _request('do');
24 if (preg_match('/^\w*$/', $do))
25 $r = exec_rechercher_args($id, $type, $exclus, $rac, $do);
26 else $r = '';
27 ajax_retour($r);
28 }
29
30 // http://doc.spip.org/@exec_rechercher_args
31 function exec_rechercher_args($id, $type, $exclus, $rac, $do){
32 if (!$do) $do = 'aff';
33
34 $where = preg_split(",\s+,", $type);
35 if ($where){
36 foreach ($where as $k => $v){
37 $where[$k] = "'%" . substr(str_replace("%", "\%", sql_quote($v,'','string')), 1, -1) . "%'";
38 }
39 $where_titre = ("(titre LIKE " . join(" AND titre LIKE ", $where) . ")");
40 $where_desc = ("(descriptif LIKE " . join(" AND descriptif LIKE ", $where) . ")");
41 $where_id = ("(id_rubrique = " . intval($type) . ")");
42 } else {
43 $where_titre = " 1=2";
44 $where_desc = " 1=2";
45 $where_id = " 1=2";
46 }
47
48 if ($exclus){
49 include_spip('inc/rubriques');
50 $where_exclus = " AND " . sql_in('id_rubrique', calcul_branche_in($exclus), 'NOT');
51 } else $where_exclus = '';
52
53 $res = sql_select("id_rubrique, id_parent, titre", "spip_rubriques", "$where_id$where_exclus");
54
55 $points = $rub = array();
56
57 while ($row = sql_fetch($res)){
58 $id_rubrique = $row["id_rubrique"];
59 $rub[$id_rubrique]["titre"] = typo($row["titre"]);
60 $rub[$id_rubrique]["id_parent"] = $row["id_parent"];
61 $points[$id_rubrique] = $points[$id_rubrique]+3;
62 }
63 $res = sql_select("id_rubrique, id_parent, titre", "spip_rubriques", "$where_titre$where_exclus");
64
65 while ($row = sql_fetch($res)){
66 $id_rubrique = $row["id_rubrique"];
67 $rub[$id_rubrique]["titre"] = typo($row["titre"]);
68 $rub[$id_rubrique]["id_parent"] = $row["id_parent"];
69 if (isset($points[$id_rubrique]))
70 $points[$id_rubrique] += 2;
71 else $points[$id_rubrique] = 0;
72 }
73 $res = sql_select("id_rubrique, id_parent, titre", "spip_rubriques", "$where_desc$where_exclus");
74
75 while ($row = sql_fetch($res)){
76 $id_rubrique = $row["id_rubrique"];
77 $rub[$id_rubrique]["titre"] = typo($row["titre"]);
78 $rub[$id_rubrique]["id_parent"] = $row["id_parent"];
79 if (isset($points[$id_rubrique]))
80 $points[$id_rubrique] += 1;
81 else $points[$id_rubrique] = 0;
82 }
83
84 if ($points){
85 arsort($points);
86 $style = " style='background-image: url(" . chemin_image('secteur-12.png') . ")'";
87 foreach ($rub as $k => $v){
88 $rub[$k]['atts'] = ($v["id_parent"] ? $style : '')
89 . " class='petite-rubrique'";
90 }
91 }
92
93 return (proposer_item($points, $rub, $rac, $type, $do));
94 }
95
96 // Resultat de la recherche interactive demandee par la fonction JS
97 // onkey_rechercher qui testera s'il comporte une seule balise au premier niveau
98 // car cela qui indique qu'un seul resultat a ete trouve.
99 // ==> attention a composer le message d'erreur avec au moins 2 balises
100
101 // http://doc.spip.org/@proposer_item
102 function proposer_item($ids, $titles, $rac, $type, $do){
103
104 if (!$ids)
105 return "<br /><br /><div style='padding: 5px; color: red;'><b>"
106 . spip_htmlentities($type)
107 . "</b> : " . _T('avis_aucun_resultat') . "</div>";
108
109 $ret = '';
110 $info = generer_url_ecrire('informer', "type=rubrique&rac=$rac&id=");
111
112 $onClick = "aff_selection(this.firstChild.title,'$rac" . "_selection','$info', event)";
113
114 $ondbClick = "$do(this.firstChild.firstChild.nodeValue,this.firstChild.title,'selection_rubrique', 'id_parent');";
115
116 foreach ($ids as $id => $bof){
117
118 $titre = strtr(str_replace("'", "&#8217;", str_replace('"', "&#34;", textebrut($titles[$id]["titre"]))), "\n\r", " ");
119
120 $ret .= "<div class='highlight off'\nonclick=\"changerhighlight(this); "
121 . $onClick
122 . "\"\nondblclick=\""
123 . $ondbClick
124 . $onClick
125 . " \"><div"
126 . $titles[$id]["atts"]
127 . " title='$id'>&nbsp; "
128 . $titre
129 . "</div></div>";
130 }
131 return $ret;
132 }
133
134 ?>