[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / ecrire / exec / rechercher.php
1 <?php
2 /***************************************************************************\
3 * SPIP, Systeme de publication pour l'internet *
4 * *
5 * Copyright (c) 2001-2017 *
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 /**
13 * Gestion de la recherche ajax du mini navigateur de rubriques
14 *
15 * Cette possibilité de recherche apparaît s'il y a beaucoup de rubriques dans le site.
16 *
17 * @package SPIP\Core\Rechercher
18 **/
19
20 if (!defined('_ECRIRE_INC_VERSION')) {
21 return;
22 }
23
24 include_spip('inc/actions');
25 include_spip('inc/texte');
26
27 /**
28 * Prépare la fonction de recherche ajax du mini navigateur de rubriques
29 *
30 * @uses exec_rechercher_args() Formate le rendu de la recherche.
31 * @uses ajax_retour()
32 **/
33 function exec_rechercher_dist() {
34 $id = intval(_request('id'));
35 $exclus = intval(_request('exclus'));
36 $rac = spip_htmlentities(_request('rac'));
37 $type = _request('type');
38 $do = _request('do');
39 if (preg_match('/^\w*$/', $do)) {
40 $r = exec_rechercher_args($id, $type, $exclus, $rac, $do);
41 } else {
42 $r = '';
43 }
44 ajax_retour($r);
45 }
46
47 /**
48 * Formate le rendu de la recherche ajax du mini navigateur de rubriques
49 *
50 * @see calcul_branche_in()
51 * @see proposer_item()
52 *
53 * @param int $id
54 * @param string $type
55 * @param string|int|array $exclus
56 * @param string|bool $rac
57 * @param string $do
58 * @return string
59 **/
60 function exec_rechercher_args($id, $type, $exclus, $rac, $do) {
61 if (!$do) {
62 $do = 'aff';
63 }
64
65 $points = $rub = array();
66
67 $where = preg_split(",\s+,", $type);
68 if ($where) {
69 foreach ($where as $k => $v) {
70 $where[$k] = "'%" . substr(str_replace("%", "\%", sql_quote($v, '', 'string')), 1, -1) . "%'";
71 }
72 $where_titre = ("(titre LIKE " . join(" AND titre LIKE ", $where) . ")");
73 $where_desc = ("(descriptif LIKE " . join(" AND descriptif LIKE ", $where) . ")");
74 $where_id = ("(id_rubrique = " . intval($type) . ")");
75
76 if ($exclus) {
77 include_spip('inc/rubriques');
78 $where_exclus = " AND " . sql_in('id_rubrique', calcul_branche_in($exclus), 'NOT');
79 } else {
80 $where_exclus = '';
81 }
82
83 foreach (array(
84 3 => $where_titre,
85 2 => $where_desc,
86 1 => $where_id,
87 ) as $point => $recherche) {
88 $res = sql_select("id_rubrique, id_parent, titre", "spip_rubriques", "$recherche$where_exclus");
89 while ($row = sql_fetch($res)) {
90 $id_rubrique = $row["id_rubrique"];
91 if (!isset($rub[$id_rubrique])) {
92 $rub[$id_rubrique] = array();
93 }
94 $rub[$id_rubrique]["titre"] = typo($row["titre"]);
95 $rub[$id_rubrique]["id_parent"] = $row["id_parent"];
96 if (!isset($points[$id_rubrique])) {
97 $points[$id_rubrique] = 0;
98 }
99 $points[$id_rubrique] = $points[$id_rubrique] + $point;
100 }
101 }
102 }
103
104 if ($points) {
105 arsort($points);
106 $style = " style='background-image: url(" . chemin_image('secteur-12.png') . ")'";
107 foreach ($rub as $k => $v) {
108 $rub[$k]['atts'] = ($v["id_parent"] ? $style : '')
109 . " class='petite-rubrique'";
110 }
111 }
112
113 return (proposer_item($points, $rub, $rac, $type, $do));
114 }
115
116
117 /**
118 * Résultat de la recherche intéractive demandée par la fonction JS
119 * `onkey_rechercher`
120 *
121 * @note
122 * `onkey_rechercher()` testera s'il comporte une seule balise au premier niveau
123 * car cela qui indique qu'un seul résultat a été trouvé.
124 *
125 * Attention donc à composer le message d'erreur avec au moins 2 balises.
126 *
127 * @param array $ids
128 * @param array|string $titles
129 * @param string|bool $rac
130 * @param string $type
131 * @param string $do
132 * @return string
133 **/
134 function proposer_item($ids, $titles, $rac, $type, $do) {
135
136 if (!$ids) {
137 return "<br /><br /><div style='padding: 5px; color: red;'><b>"
138 . spip_htmlentities($type)
139 . "</b> : " . _T('avis_aucun_resultat') . "</div>";
140 }
141
142 $ret = '';
143 $info = generer_url_ecrire('informer', "type=rubrique&rac=$rac&id=");
144
145 $onClick = "aff_selection(this.firstChild.title,'$rac" . "_selection','$info', event)";
146
147 $ondbClick = "$do(this.firstChild.firstChild.nodeValue,this.firstChild.title,'selection_rubrique', 'id_parent');";
148
149 foreach ($ids as $id => $bof) {
150
151 $titre = strtr(str_replace("'", "&#8217;", str_replace('"', "&#34;", textebrut($titles[$id]["titre"]))), "\n\r",
152 " ");
153
154 $ret .= "<div class='highlight off'\nonclick=\"changerhighlight(this); "
155 . $onClick
156 . "\"\nondblclick=\""
157 . $ondbClick
158 . $onClick
159 . " \"><div"
160 . $titles[$id]["atts"]
161 . " title='$id'>&nbsp; "
162 . $titre
163 . "</div></div>";
164 }
165
166 return $ret;
167 }