[PLUGINS] ~maj globale
[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 * @param unknown_type $texte
33 * @return array
34 */
35 function importer_csv_importcharset($texte) {
36 // le plus frequent, en particulier avec les trucs de ms@@@
37 $charset_source = 'iso-8859-1';
38 // mais open-office sait faire mieux, donc mefiance !
39 if (is_utf8($texte)) {
40 $charset_source = 'utf-8';
41 }
42 return importer_charset($texte, $charset_source);
43 }
44
45 /**
46 * enlever les accents des cles presentes dans le head,
47 * sinon ca pose des problemes ...
48 *
49 * @param string $key
50 * @return string
51 */
52 function importer_csv_nettoie_key($key) {
53 return translitteration($key);
54 /*$accents=array('�','�','�','�','�',"�","�","'");
55 $accents_rep=array('e','e','e','a','u',"o","c","_");
56 return str_replace($accents,$accents_rep,$key);*/
57 }
58
59 /**
60 * Lit un fichier csv et retourne un tableau
61 * si $head est true, la premiere ligne est utilisee en header
62 * pour generer un tableau associatif
63 *
64 * @param string $file
65 * @param bool $head
66 * @param string $delim
67 * @param string $enclos
68 * @param int $len
69 * @return array
70 */
71 function inc_importer_csv_dist($file, $head = false, $delim = ',', $enclos = '"', $len = 10000) {
72 $return = false;
73 if (@file_exists($file)
74 and $handle = fopen($file, 'r')) {
75 if ($head) {
76 $header = fgetcsv($handle, $len, $delim, $enclos);
77 if ($header) {
78 $header = array_map('importer_csv_importcharset', $header);
79 $header = array_map('importer_csv_nettoie_key', $header);
80 $header_type = array();
81 foreach ($header as $heading) {
82 if (!isset($header_type[$heading])) {
83 $header_type[$heading] = 'scalar';
84 } else {
85 $header_type[$heading] = 'array';
86 }
87 }
88 }
89 }
90 while (($data = fgetcsv($handle, $len, $delim, $enclos)) !== false) {
91 $data = array_map('importer_csv_importcharset', $data);
92 if ($head and isset($header)) {
93 $row = array();
94 foreach ($header as $key => $heading) {
95 if ($header_type[$heading] == 'array') {
96 if (!isset($row[$heading])) {
97 $row[$heading] = array();
98 }
99 if (isset($data[$key]) and strlen($data[$key])) {
100 $row[$heading][]= $data[$key];
101 }
102 } else {
103 $row[$heading]=(isset($data[$key])) ? $data[$key] : '';
104 }
105 }
106 $return[]=$row;
107 } else {
108 $return[]=$data;
109 }
110 }
111 }
112 return $return;
113 }