2ca3e5627607dee3774cb7f7e666714da5829716
[lhc/web/www.git] / www / plugins / spip-bonux-3 / inc / importer_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 de lecture d'un fichier CSV pour transformation en array()
9 *
10 */
11
12 if (!defined('_ECRIRE_INC_VERSION')) {
13 return;
14 }
15
16 include_spip('inc/charsets');
17
18 /**
19 * Based on an example by ramdac at ramdac dot org
20 * Returns a multi-dimensional array from a CSV file optionally using the
21 * first row as a header to create the underlying data as associative arrays.
22 * @param string $file Filepath including filename
23 * @param bool $head Use first row as header.
24 * @param string $delim Specify a delimiter other than a comma.
25 * @param int $len Line length to be passed to fgetcsv
26 * @return array or false on failure to retrieve any rows.
27 */
28
29 /**
30 * Importer le charset d'une ligne
31 *
32 * Importe un texte de CSV dans un charset et le passe dans le charset du site (utf8 probablement)
33 * Les CSV peuvent sous ms@@@ avoir le charset 'iso-8859-1', mais pasfois aussi 'windows-1252' :/
34 *
35 * @param mixed $texte
36 * @param bool|string $definir_charset_source
37 * false : ne fait rien
38 * string : utilisera pour les prochains imports le charset indiqué
39 * true : remet le charset d'import par défaut de la fonction
40 * @return array
41 */
42 function importer_csv_importcharset($texte, $definir_charset_source = false) {
43 // le plus frequent, en particulier avec les trucs de ms@@@
44 static $charset_source = 'iso-8859-1';
45 if ($definir_charset_source) {
46 if ($definir_charset_source === true) {
47 $charset_source = 'iso-8859-1';
48 } else {
49 $charset_source = $definir_charset_source;
50 }
51 }
52 // mais open-office sait faire mieux, donc mefiance !
53 if (is_utf8($texte)) {
54 $charset = 'utf-8';
55 } else {
56 $charset = $charset_source;
57 }
58 return importer_charset($texte, $charset);
59 }
60
61 /**
62 * enlever les accents des cles presentes dans le head,
63 * sinon ca pose des problemes ...
64 *
65 * @param string $key
66 * @return string
67 */
68 function importer_csv_nettoie_key($key) {
69 return translitteration($key);
70 /*$accents=array('�','�','�','�','�',"�","�","'");
71 $accents_rep=array('e','e','e','a','u',"o","c","_");
72 return str_replace($accents,$accents_rep,$key);*/
73 }
74
75 /**
76 * Lit un fichier csv et retourne un tableau
77 * si $head est true, la premiere ligne est utilisee en header
78 * pour generer un tableau associatif
79 *
80 * @param string $file
81 * @param bool $head
82 * @param string $delim
83 * @param string $enclos
84 * @param int $len
85 * @param string $charset_source
86 * Permet de définir un charset source du CSV, si différent de utf-8 ou iso-8859-1
87 * @return array
88 */
89 function inc_importer_csv_dist($file, $head = false, $delim = ',', $enclos = '"', $len = 10000, $charset_source = '') {
90 $return = false;
91 if (@file_exists($file)
92 and $handle = fopen($file, 'r')) {
93 if ($charset_source) {
94 importer_csv_importcharset('', $charset_source);
95 }
96 if ($head) {
97 $header = fgetcsv($handle, $len, $delim, $enclos);
98 if ($header) {
99 $header = array_map('importer_csv_importcharset', $header);
100 $header = array_map('importer_csv_nettoie_key', $header);
101 $header_type = array();
102 foreach ($header as $heading) {
103 if (!isset($header_type[$heading])) {
104 $header_type[$heading] = 'scalar';
105 } else {
106 $header_type[$heading] = 'array';
107 }
108 }
109 }
110 }
111 while (($data = fgetcsv($handle, $len, $delim, $enclos)) !== false) {
112 $data = array_map('importer_csv_importcharset', $data);
113 if ($head and isset($header)) {
114 $row = array();
115 foreach ($header as $key => $heading) {
116 if ($header_type[$heading] == 'array') {
117 if (!isset($row[$heading])) {
118 $row[$heading] = array();
119 }
120 if (isset($data[$key]) and strlen($data[$key])) {
121 $row[$heading][]= $data[$key];
122 }
123 } else {
124 $row[$heading]=(isset($data[$key])) ? $data[$key] : '';
125 }
126 }
127 $return[]=$row;
128 } else {
129 $return[]=$data;
130 }
131 }
132 if ($charset_source) {
133 importer_csv_importcharset('', true);
134 }
135 }
136 return $return;
137 }