Mise en route du suivi.
[aidenligne_francais_universite.git] / plugins / forms_1_9_1 / exec / forms_telecharger.php
1 <?php
2 /*
3 * forms
4 * version plug-in de spip_form
5 *
6 * Auteur :
7 * Antoine Pitrou
8 * adaptation en 182e puis plugin par cedric.morin@yterium.com
9 * © 2005,2006 - Distribue sous licence GNU/GPL
10 *
11 */
12
13
14 include_spip('inc/forms');
15 include_spip("inc/charsets");
16 include_spip("inc/presentation");
17
18 function csv_champ($champ) {
19 $champ = preg_replace(',[\s]+,', ' ', $champ);
20 $champ = str_replace(',",', '""', $champ);
21 return '"'.$champ.'"';
22 }
23
24 function csv_ligne($ligne,$delim=',') {
25 return join($delim, array_map('csv_champ', $ligne))."\r\n";
26 }
27
28 function formater_reponse($ligne, $structure, $valeurs,$delim=',') {
29 static $groupes, $mots;
30
31 // Prendre les differents champs dans l'ordre
32 foreach ($structure as $index => $t) {
33 if (!$v = $valeurs[$t['code']]) {
34 $ligne[] = "";
35 }
36 else{
37 if ($t['type']=='multiple'){
38 // pour un choix multiple on cree une colonne par reponse potentielle
39 $type_ext = $t['type_ext'];
40 foreach($type_ext as $choix)
41 if (in_array($choix,$v))
42 $ligne[] = strval($choix);
43 else
44 $ligne[] = "";
45 }
46 else
47 $ligne[] = strval(join(', ', $v));
48 }
49 }
50 return csv_ligne($ligne,$delim);
51 }
52
53 function acces_interdit() {
54 debut_page(_T('avis_acces_interdit'), "documents", "forms");
55 debut_gauche();
56 debut_droite();
57 echo "<strong>"._T('avis_acces_interdit')."</strong>";
58 fin_page();
59 exit;
60 }
61
62
63 //
64 // Telechargement d'un fichier particulier
65 //
66 function exec_forms_telecharger(){
67 $id_reponse = _request('id_reponse');
68 $id_form = _request('id_form');
69 $champ = _request('champ');
70
71 if ($id_reponse = intval($id_reponse) AND $champ) {
72 $result = spip_query("SELECT id_form FROM spip_reponses WHERE id_reponse=".spip_abstract_quote($id_reponse));
73 if ($row = spip_fetch_array($result)) {
74 $id_form = $row['id_form'];
75 }
76 if (!$id_form || !Forms_form_administrable($id_form)) {
77 acces_interdit();
78 }
79 $query = "SELECT structure FROM spip_forms WHERE id_form=$id_form";
80 $result = spip_query($query);
81 if ($row = spip_fetch_array($result)) {
82 $structure = unserialize($row['structure']);
83 }
84 $ok = false;
85 foreach ($structure as $index => $t) {
86 if ($t['code'] == $champ) {
87 $ok = ($t['type'] == 'fichier');
88 break;
89 }
90 }
91 if (!$ok) {
92 acces_interdit();
93 }
94 $result = spip_fetch_array(spip_query("SELECT valeur FROM spip_reponses_champs WHERE id_reponse=$id_reponse AND champ='$champ'"));
95 $fichier = $result['valeur'];
96 if (is_int(strpos($fichier, "..")) || !preg_match(',^IMG/,', $fichier)) {
97 acces_interdit();
98 }
99 $filename = basename($fichier);
100 $mimetype = "";
101 if (preg_match(',\.([^\.]+)$,', $fichier, $r)) {
102 $ext = $r[1];
103 $query = "SELECT * FROM spip_types_documents WHERE extension='".addslashes($ext)."'";
104 $result = spip_query($query);
105 if ($row = spip_fetch_array($result)) {
106 $mimetype = $row['mime_type'];
107 }
108 }
109 if (!$mimetype) $mimetype = "application/octet-stream";
110 $chemin = "../".$fichier;
111 if (!is_file($chemin)) {
112 acces_interdit();
113 }
114
115 Header("Content-Type: $mimetype");
116 Header("Content-Disposition: inline; filename=$filename");
117 Header("Content-Length :".filesize($chemin));
118 readfile($chemin);
119 exit;
120 }
121
122
123 $delim = _request('delim');
124 if ($delim == 'TAB') $delim = "\t";
125
126 $retour = _request('retour');
127 if ($retour!==NULL)
128 $retour = urldecode($retour);
129 else
130 $retour = generer_url_ecrire('forms_tous');
131
132 $titre = _T("forms:telecharger_reponses");
133 if (!$delim){
134 $icone = "../"._DIR_PLUGIN_FORMS."/img_pack/form-24.png";
135
136 debut_page($titre, "documents", "forms");
137 debut_gauche();
138
139 echo "<br /><br />\n";
140 debut_droite();
141
142 debut_cadre_relief($icone);
143 gros_titre($titre);
144 echo "<br />\n";
145 echo _T("forms:format_fichier");
146 echo "<br />\n";
147 // Extrait de la table en commencant par les dernieres maj
148 echo generer_url_post_ecrire('forms_telecharger');
149 echo form_hidden(self());
150 echo "<select name='delim'>\n";
151 echo "<option value=','>"._T("forms:csv_classique")."</option>\n";
152 echo "<option value=';'>"._T("forms:csv_excel")."</option>\n";
153 echo "<option value='TAB'>"._T("forms:csv_tab")."</option>\n";
154 echo "</select>";
155 echo "<br /><br />\n";
156 echo "<input type='submit' name='ok' value='"._T("forms:telecharger")."' />\n";
157
158 fin_cadre_relief();
159
160
161 //
162 // Icones retour
163 //
164 if ($retour) {
165 echo "<br />\n";
166 echo "<div align='$spip_lang_right'>";
167 icone(_T('icone_retour'), $retour, $icone, "rien.gif");
168 echo "</div>\n";
169 }
170 fin_page();
171 exit;
172
173 }
174
175 //
176 // Telechargement du tableau de reponses au format CSV
177 //
178 $id_form = intval($id_form);
179 if ($id_form) {
180 $query = "SELECT COUNT(*) FROM spip_reponses WHERE id_form=$id_form AND statut='valide'";
181 $result = spip_query($query);
182 list($nb_reponses) = spip_fetch_array($result,SPIP_NUM);
183 }
184 else $nb_reponses = 0;
185
186 if (!$id_form || !Forms_form_administrable($id_form)) {
187 acces_interdit();
188 }
189
190 $query = "SELECT * FROM spip_forms WHERE id_form=$id_form";
191 $result = spip_query($query);
192 if ($row = spip_fetch_array($result)) {
193 $id_form = $row['id_form'];
194 $titre = $row['titre'];
195 $descriptif = $row['descriptif'];
196 $sondage = $row['sondage'];
197 $structure = unserialize($row['structure']);
198 }
199
200 $charset = $GLOBALS['meta']['charset'];
201 $filename = preg_replace(',[^-_\w]+,', '_', translitteration(textebrut(typo($titre))));
202
203 $s = '';
204
205 // Preparer la table de traduction code->valeur
206 $trans = array();
207 $types = array();
208 foreach ($structure as $index => $t) {
209 $code = $t['code'];
210 $type = $t['type'];
211 $types[$code] = $type;
212 $trans[$code] = array();
213
214 if ($type == 'select' || $type == 'multiple') {
215 $trans[$code] = $t['type_ext'];
216 }
217 else if ($type == 'mot') {
218 $id_groupe = intval($t['type_ext']['id_groupe']);
219 $query = "SELECT id_mot, titre FROM spip_mots WHERE id_groupe=$id_groupe";
220 $result = spip_query($query);
221 while ($row = spip_fetch_array($result)) {
222 $id_mot = $row['id_mot'];
223 $titre = $row['titre'];
224 $trans[$code][$id_mot] = trim(textebrut(typo($titre)));
225 }
226 }
227 }
228
229 // Une premiere ligne avec les noms de champs
230 $ligne = array();
231 $ligne[] = _T("forms:date");
232 $ligne[] = _T("forms:page");
233 foreach ($structure as $index => $t) {
234 $ligne[] = textebrut(typo($t['nom']));
235 if ($t['type']=='multiple'){
236 // pour un choix multiple on cree une colonne par reponse potentielle
237 $type_ext = $t['type_ext'];
238 array_pop($type_ext);
239 foreach($type_ext as $choix)
240 $ligne[] = "";
241 }
242 }
243 $s .= csv_ligne($ligne,$delim);
244
245
246 // Ensuite les reponses
247 $fichier = array();
248 $id_reponse = 0;
249 $query = "SELECT r.id_reponse, r.date,r.url, c.champ, c.valeur ".
250 "FROM spip_reponses AS r LEFT JOIN spip_reponses_champs AS c USING (id_reponse) ".
251 "WHERE id_form=$id_form AND statut='valide' AND c.id_reponse IS NOT NULL ".
252 "ORDER BY date, r.id_reponse";
253 $result = spip_query($query);
254 while ($row = spip_fetch_array($result)) {
255 if ($id_reponse != $row['id_reponse']) {
256 if ($id_reponse) {
257 $s .= formater_reponse($ligne, $structure, $valeurs,$delim);
258 }
259 $id_reponse = $row['id_reponse'];
260 $ligne = array();
261 $valeurs = array();
262 $date = $row['date'];
263 $ligne[] = jour($date).'/'.mois($date).'/'.annee($date);
264 $ligne[] = str_replace("&amp;","&",$row['url']);
265 }
266 $champ = $row['champ'];
267 if ($types[$champ] == 'fichier') {
268 $fichiers[] = $row['valeur'];
269 //$valeurs[$champ][] = $GLOBALS['meta']['adresse_site']."/ecrire/forms_telecharger.php?id_reponse=$id_reponse&champ=$champ";
270 $valeurs[$champ][] = 'fichiers/'.basename($row['valeur']);
271 }
272 else if ($v = $trans[$champ][$row['valeur']])
273 $valeurs[$champ][] = $v;
274 else
275 $valeurs[$champ][] = $row['valeur'];
276 }
277
278 // Ne pas oublier la derniere reponse
279 if ($id_reponse) {
280 $s .= formater_reponse($ligne, $structure, $valeurs,$delim);
281 }
282
283 // Excel ?
284 if ($delim == ',')
285 $extension = 'csv';
286 else {
287 $extension = 'xls';
288 # Excel n'accepte pas l'utf-8 ni les entites html... on fait quoi?
289 include_spip('inc/charsets');
290 $s = unicode2charset(charset2unicode($s), 'iso-8859-1');
291 $charset = 'iso-8859-1';
292 }
293
294 if (!count($fichiers)) {
295 Header("Content-Type: text/comma-separated-values; charset=$charset");
296 Header("Content-Disposition: attachment; filename=$filename.$extension");
297 //Header("Content-Type: text/plain; charset=$charset");
298 Header("Content-Length: ".strlen($s));
299 echo $s;
300 exit;
301 }
302
303 //
304 // S'il y a des fichiers joints, creer un ZIP
305 //
306 include_spip("inc/pclzip");
307 include_spip("inc/session");
308
309 $zip = "data/form".$id_form."_".rand().".zip";
310 $csv = "data/$filename.$extension";
311
312 $f = fopen($csv, "wb");
313 fwrite($f, $s);
314 fclose($f);
315
316 $chemin = "../";
317 $fichiers = $chemin.join(",$chemin", $fichiers);
318
319 $archive = new PclZip($zip);
320 $archive->add($csv, PCLZIP_OPT_REMOVE_ALL_PATH, PCLZIP_OPT_ADD_PATH, $filename);
321 $archive->add($fichiers, PCLZIP_OPT_REMOVE_ALL_PATH, PCLZIP_OPT_ADD_PATH, $filename.'/fichiers');
322
323 Header("Content-Type: application/zip");
324 Header("Content-Disposition: attachment; filename=$filename.zip");
325 Header("Content-Length: ".filesize($zip));
326 readfile($zip);
327
328 @unlink($csv);
329 @unlink($zip);
330
331 exit;
332 }
333 ?>