[CSS] +fix page header and title color
[lhc/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, 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')) {
15 return;
16 }
17
18 include_spip('inc/charsets');
19 include_spip('inc/filtres');
20 include_spip('inc/texte');
21
22 /**
23 * Exporter un champ pour un export CSV : pas de retour a la ligne,
24 * et echapper les guillements par des doubles guillemets
25 * @param string $champ
26 * @return string
27 */
28 function exporter_csv_champ($champ) {
29 #$champ = str_replace("\r", "\n", $champ);
30 #$champ = preg_replace(",[\n]+,ms", "\n", $champ);
31 #$champ = str_replace("\n", ", ", $champ);
32 $champ = preg_replace(',[\s]+,ms', ' ', $champ);
33 $champ = str_replace('"', '""', $champ);
34 return '"'.$champ.'"';
35 }
36
37 /**
38 * Exporter une ligne complete au format CSV, avec delimiteur fourni
39 * @param array $ligne
40 * @param string $delim
41 * @return string
42 */
43 function exporter_csv_ligne($ligne, $delim = ',', $importer_charset = null) {
44 $output = join($delim, array_map('exporter_csv_champ', $ligne))."\r\n";
45 if ($importer_charset) {
46 $output = str_replace('’', '\'', $output);
47 $output = unicode2charset(html2unicode(charset2unicode($output)), $importer_charset);
48 }
49 return $output;
50 }
51
52
53 function inc_exporter_csv_dist($titre, $resource, $delim = ',', $entetes = null, $envoyer = true) {
54
55 $filename = preg_replace(',[^-_\w]+,', '_', translitteration(textebrut(typo($titre))));
56
57 if ($delim == 'TAB') {
58 $delim = "\t";
59 }
60 if (!in_array($delim, array(',', ';', "\t"))) {
61 $delim = ',';
62 }
63
64 $charset = $GLOBALS['meta']['charset'];
65 $importer_charset = null;
66 if ($delim == ',') {
67 $extension = 'csv';
68 } else {
69 $extension = 'xls';
70 # Excel n'accepte pas l'utf-8 ni les entites html... on transcode tout ce qu'on peut
71 $importer_charset = $charset = 'iso-8859-1';
72 }
73 $filename = "$filename.$extension";
74
75 $output = '';
76 if ($entetes and is_array($entetes) and count($entetes)) {
77 $output = exporter_csv_ligne($entetes, $delim, $importer_charset);
78 }
79
80 // on passe par un fichier temporaire qui permet de ne pas saturer la memoire
81 // avec les gros exports
82 $fichier = sous_repertoire(_DIR_CACHE, 'export') . $filename;
83 $fp = fopen($fichier, 'w');
84 $length = fwrite($fp, $output);
85
86 while ($row = is_array($resource) ? array_shift($resource) : sql_fetch($resource)) {
87 $output = exporter_csv_ligne($row, $delim, $importer_charset);
88 $length += fwrite($fp, $output);
89 }
90 fclose($fp);
91 if ($envoyer) {
92 ob_start();
93 Header("Content-Type: text/comma-separated-values; charset=$charset");
94 Header("Content-Disposition: attachment; filename=$filename");
95 //non supporte
96 //Header("Content-Type: text/plain; charset=$charset");
97 Header("Content-Length: $length");
98 ob_clean();
99 flush();
100 readfile($fichier);
101 }
102
103 return $fichier;
104 }