bad0aedce3cbdad4f248f8dccc320cd52b70d4c5
[ptitvelo/web/www.git] / www / plugins / spip-bonux-3 / inc / exporter_csv.php
1 <?php
2 /**
3 * Plugin Spip-Bonux
4 * Le plugin qui lave plus SPIP que SPIP
5 * (c) 2008 Mathieu Marcillaud, Cedric Morin, Romy Tetue
6 * Licence GPL
7 *
8 * Fonctions d'export d'une requete sql ou d'un tableau
9 * au format CSV
10 * Merge du plugin csv_import et spip-surcharges
11 *
12 */
13
14 if (!defined('_ECRIRE_INC_VERSION')) return;
15
16 include_spip('inc/charsets');
17 include_spip('inc/filtres');
18 include_spip('inc/texte');
19
20 /**
21 * Exporter un champ pour un export CSV : pas de retour a la ligne,
22 * et echapper les guillements par des doubles guillemets
23 * @param string $champ
24 * @return string
25 */
26 function exporter_csv_champ($champ) {
27 #$champ = str_replace("\r", "\n", $champ);
28 #$champ = preg_replace(",[\n]+,ms", "\n", $champ);
29 #$champ = str_replace("\n", ", ", $champ);
30 $champ = preg_replace(',[\s]+,ms', ' ', $champ);
31 $champ = str_replace('"', '""', $champ);
32 return '"'.$champ.'"';
33 }
34
35 /**
36 * Exporter une ligne complete au format CSV, avec delimiteur fourni
37 * @param array $ligne
38 * @param string $delim
39 * @return string
40 */
41 function exporter_csv_ligne($ligne, $delim = ',', $importer_charset = null) {
42 $output = join($delim, array_map('exporter_csv_champ', $ligne))."\r\n";
43 if ($importer_charset){
44 $output = unicode2charset(html2unicode(charset2unicode($output)), $importer_charset);
45 }
46 return $output;
47 }
48
49
50 function inc_exporter_csv_dist($titre, $resource, $delim=',', $entetes = null,$envoyer = true){
51
52 $filename = preg_replace(',[^-_\w]+,', '_', translitteration(textebrut(typo($titre))));
53
54 if ($delim == 'TAB') $delim = "\t";
55 if (!in_array($delim,array(',',';',"\t")))
56 $delim = ",";
57
58 $charset = $GLOBALS['meta']['charset'];
59 $importer_charset = null;
60 if ($delim == ',')
61 $extension = 'csv';
62 else {
63 $extension = 'xls';
64 # Excel n'accepte pas l'utf-8 ni les entites html... on transcode tout ce qu'on peut
65 $importer_charset = $charset = 'iso-8859-1';
66 }
67 $filename = "$filename.$extension";
68
69 if ($entetes AND is_array($entetes) AND count($entetes))
70 $output = exporter_csv_ligne($entetes,$delim,$importer_charset);
71
72 // on passe par un fichier temporaire qui permet de ne pas saturer la memoire
73 // avec les gros exports
74 $fichier = sous_repertoire(_DIR_CACHE,"export") . $filename;
75 $fp = fopen($fichier, 'w');
76 $length = fwrite($fp, $output);
77
78 while ($row=is_array($resource)?array_shift($resource):sql_fetch($resource)){
79 $output = exporter_csv_ligne($row,$delim,$importer_charset);
80 $length += fwrite($fp, $output);
81 }
82 fclose($fp);
83
84 if ($envoyer) {
85 Header("Content-Type: text/comma-separated-values; charset=$charset");
86 Header("Content-Disposition: attachment; filename=$filename");
87 //non supporte
88 //Header("Content-Type: text/plain; charset=$charset");
89 Header("Content-Length: $length");
90 ob_clean();
91 flush();
92 readfile($fichier);
93 }
94
95 return $fichier;
96 }
97
98 ?>