Browse Source

On se synchronise un peu avec les fonctions du Core afin de faciliter l’identification au grand jeu des 7 différences !

svn/attic/spip-bonux-3/118152
marcimat@rezo.net 3 years ago
parent
commit
87144e9724
  1. 44
      inc/exporter_csv.php
  2. 19
      inc/importer_csv.php

44
inc/exporter_csv.php

@ -22,6 +22,7 @@ include_spip('inc/texte');
/**
* Exporter un champ pour un export CSV : pas de retour a la ligne,
* et echapper les guillements par des doubles guillemets
*
* @param string $champ
* @return string
*/
@ -31,17 +32,23 @@ function exporter_csv_champ($champ) {
#$champ = str_replace("\n", ", ", $champ);
$champ = preg_replace(',[\s]+,ms', ' ', $champ);
$champ = str_replace('"', '""', $champ);
return '"'.$champ.'"';
return '"' . $champ . '"';
}
/**
* Exporter une ligne complete au format CSV, avec delimiteur fourni
*
* @uses exporter_csv_champ()
*
* @param array $ligne
* @param string $delim
* @param string|null $importer_charset
* Si défini exporte dans le charset indiqué
* @return string
*/
function exporter_csv_ligne($ligne, $delim = ',', $importer_charset = null) {
$output = join($delim, array_map('exporter_csv_champ', $ligne))."\r\n";
$output = join($delim, array_map('exporter_csv_champ', $ligne)) . "\r\n";
if ($importer_charset) {
$output = str_replace('’', '\'', $output);
$output = unicode2charset(html2unicode(charset2unicode($output)), $importer_charset);
@ -49,8 +56,28 @@ function exporter_csv_ligne($ligne, $delim = ',', $importer_charset = null) {
return $output;
}
function inc_exporter_csv_dist($titre, $resource, $delim = ',', $entetes = null, $envoyer = true) {
/**
* Exporte une ressource sous forme de fichier CSV
*
* La ressource peut etre un tableau ou une resource SQL issue d'une requete
* L'extension est choisie en fonction du delimiteur :
* - si on utilise ',' c'est un vrai csv avec extension csv
* - si on utilise ';' ou tabulation c'est pour E*cel, et on exporte en iso-truc, avec une extension .xls
*
* @uses exporter_csv_ligne()
*
* @param string $titre
* titre utilise pour nommer le fichier
* @param array|resource $resource
* @param string $delim
* delimiteur
* @param array $entetes
* tableau d'en-tetes pour nommer les colonnes (genere la premiere ligne)
* @param bool $envoyer
* pour envoyer le fichier exporte (permet le telechargement)
* @return string
*/
function inc_exporter_csv_dist($titre, $resource, $delim = ', ', $entetes = null, $envoyer = true) {
$filename = preg_replace(',[^-_\w]+,', '_', translitteration(textebrut(typo($titre))));
@ -88,13 +115,14 @@ function inc_exporter_csv_dist($titre, $resource, $delim = ',', $entetes = null,
$length += fwrite($fp, $output);
}
fclose($fp);
if ($envoyer) {
ob_start();
Header("Content-Type: text/comma-separated-values; charset=$charset");
Header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: text/comma-separated-values; charset=$charset");
header("Content-Disposition: attachment; filename=$filename");
//non supporte
//Header("Content-Type: text/plain; charset=$charset");
Header("Content-Length: $length");
//header("Content-Type: text/plain; charset=$charset");
header("Content-Length: $length");
ob_clean();
flush();
readfile($fichier);

19
inc/importer_csv.php

@ -19,6 +19,7 @@ include_spip('inc/charsets');
* Based on an example by ramdac at ramdac dot org
* Returns a multi-dimensional array from a CSV file optionally using the
* first row as a header to create the underlying data as associative arrays.
*
* @param string $file Filepath including filename
* @param bool $head Use first row as header.
* @param string $delim Specify a delimiter other than a comma.
@ -67,9 +68,6 @@ function importer_csv_importcharset($texte, $definir_charset_source = false) {
*/
function importer_csv_nettoie_key($key) {
return translitteration($key);
/*$accents=array('<EFBFBD>','<EFBFBD>','<EFBFBD>','<EFBFBD>','<EFBFBD>',"<EFBFBD>","<EFBFBD>","'");
$accents_rep=array('e','e','e','a','u',"o","c","_");
return str_replace($accents,$accents_rep,$key);*/
}
/**
@ -101,9 +99,9 @@ function inc_importer_csv_dist($file, $head = false, $delim = ',', $enclos = '"'
$header_type = array();
foreach ($header as $heading) {
if (!isset($header_type[$heading])) {
$header_type[$heading] = 'scalar';
$header_type[$heading] = "scalar";
} else {
$header_type[$heading] = 'array';
$header_type[$heading] = "array";
}
}
}
@ -113,25 +111,26 @@ function inc_importer_csv_dist($file, $head = false, $delim = ',', $enclos = '"'
if ($head and isset($header)) {
$row = array();
foreach ($header as $key => $heading) {
if ($header_type[$heading] == 'array') {
if ($header_type[$heading] == "array") {
if (!isset($row[$heading])) {
$row[$heading] = array();
}
if (isset($data[$key]) and strlen($data[$key])) {
$row[$heading][]= $data[$key];
$row[$heading][] = $data[$key];
}
} else {
$row[$heading]=(isset($data[$key])) ? $data[$key] : '';
$row[$heading] = (isset($data[$key])) ? $data[$key] : '';
}
}
$return[]=$row;
$return[] = $row;
} else {
$return[]=$data;
$return[] = $data;
}
}
if ($charset_source) {
importer_csv_importcharset('', true);
}
}
return $return;
}

Loading…
Cancel
Save