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