@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | \***************************************************************************/ |
| 12 | 12 | |
| 13 | 13 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | include_spip('inc/charsets'); |
@@ -42,22 +42,22 @@ discard block |
||
| 42 | 42 | * @return array |
| 43 | 43 | */ |
| 44 | 44 | function importer_csv_importcharset($texte, $definir_charset_source = false) { |
| 45 | - // le plus frequent, en particulier avec les trucs de ms@@@ |
|
| 46 | - static $charset_source = 'iso-8859-1'; |
|
| 47 | - if ($definir_charset_source) { |
|
| 48 | - if ($definir_charset_source === true) { |
|
| 49 | - $charset_source = 'iso-8859-1'; |
|
| 50 | - } else { |
|
| 51 | - $charset_source = $definir_charset_source; |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - // mais open-office sait faire mieux, donc mefiance ! |
|
| 55 | - if (is_utf8($texte)) { |
|
| 56 | - $charset = 'utf-8'; |
|
| 57 | - } else { |
|
| 58 | - $charset = $charset_source; |
|
| 59 | - } |
|
| 60 | - return importer_charset($texte, $charset); |
|
| 45 | + // le plus frequent, en particulier avec les trucs de ms@@@ |
|
| 46 | + static $charset_source = 'iso-8859-1'; |
|
| 47 | + if ($definir_charset_source) { |
|
| 48 | + if ($definir_charset_source === true) { |
|
| 49 | + $charset_source = 'iso-8859-1'; |
|
| 50 | + } else { |
|
| 51 | + $charset_source = $definir_charset_source; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + // mais open-office sait faire mieux, donc mefiance ! |
|
| 55 | + if (is_utf8($texte)) { |
|
| 56 | + $charset = 'utf-8'; |
|
| 57 | + } else { |
|
| 58 | + $charset = $charset_source; |
|
| 59 | + } |
|
| 60 | + return importer_charset($texte, $charset); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | * @return string |
| 69 | 69 | */ |
| 70 | 70 | function importer_csv_nettoie_key($key) { |
| 71 | - return translitteration($key); |
|
| 71 | + return translitteration($key); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -87,75 +87,75 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | function inc_importer_csv_dist($file, $options = []) { |
| 89 | 89 | |
| 90 | - // support ancienne syntaxe |
|
| 91 | - // inc_importer_csv_dist($file, $head = false, $delim = ',', $enclos = '"', $len = 10000, $charset_source = '') |
|
| 92 | - if (!is_array($options)) { |
|
| 93 | - $args = func_get_args(); |
|
| 94 | - $options = []; |
|
| 95 | - foreach ([1 => 'head', 2 => 'delim', 3 => 'enclos', 4 => 'len', 5 => 'charset_source'] as $k => $option) { |
|
| 96 | - if (!empty($args[$k])) { |
|
| 97 | - $options[$option] = $args[$k]; |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - } |
|
| 90 | + // support ancienne syntaxe |
|
| 91 | + // inc_importer_csv_dist($file, $head = false, $delim = ',', $enclos = '"', $len = 10000, $charset_source = '') |
|
| 92 | + if (!is_array($options)) { |
|
| 93 | + $args = func_get_args(); |
|
| 94 | + $options = []; |
|
| 95 | + foreach ([1 => 'head', 2 => 'delim', 3 => 'enclos', 4 => 'len', 5 => 'charset_source'] as $k => $option) { |
|
| 96 | + if (!empty($args[$k])) { |
|
| 97 | + $options[$option] = $args[$k]; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - $default_options = [ |
|
| 103 | - 'head' => false, |
|
| 104 | - 'delim' => ',', |
|
| 105 | - 'enclos' => '"', |
|
| 106 | - 'len' => 10000, |
|
| 107 | - 'charset_source' => '', |
|
| 108 | - ]; |
|
| 109 | - $options = array_merge($default_options, $options); |
|
| 102 | + $default_options = [ |
|
| 103 | + 'head' => false, |
|
| 104 | + 'delim' => ',', |
|
| 105 | + 'enclos' => '"', |
|
| 106 | + 'len' => 10000, |
|
| 107 | + 'charset_source' => '', |
|
| 108 | + ]; |
|
| 109 | + $options = array_merge($default_options, $options); |
|
| 110 | 110 | |
| 111 | 111 | |
| 112 | - $return = false; |
|
| 113 | - if (@file_exists($file) |
|
| 114 | - and $handle = fopen($file, 'r')) { |
|
| 115 | - if ($options['charset_source']) { |
|
| 116 | - importer_csv_importcharset('', $options['charset_source']); |
|
| 117 | - } |
|
| 118 | - if ($options['head']) { |
|
| 119 | - $header = fgetcsv($handle, $options['len'], $options['delim'], $options['enclos']); |
|
| 120 | - if ($header) { |
|
| 121 | - $header = array_map('importer_csv_importcharset', $header); |
|
| 122 | - $header = array_map('importer_csv_nettoie_key', $header); |
|
| 123 | - $header_type = array(); |
|
| 124 | - foreach ($header as $heading) { |
|
| 125 | - if (!isset($header_type[$heading])) { |
|
| 126 | - $header_type[$heading] = "scalar"; |
|
| 127 | - } else { |
|
| 128 | - $header_type[$heading] = "array"; |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - } |
|
| 112 | + $return = false; |
|
| 113 | + if (@file_exists($file) |
|
| 114 | + and $handle = fopen($file, 'r')) { |
|
| 115 | + if ($options['charset_source']) { |
|
| 116 | + importer_csv_importcharset('', $options['charset_source']); |
|
| 117 | + } |
|
| 118 | + if ($options['head']) { |
|
| 119 | + $header = fgetcsv($handle, $options['len'], $options['delim'], $options['enclos']); |
|
| 120 | + if ($header) { |
|
| 121 | + $header = array_map('importer_csv_importcharset', $header); |
|
| 122 | + $header = array_map('importer_csv_nettoie_key', $header); |
|
| 123 | + $header_type = array(); |
|
| 124 | + foreach ($header as $heading) { |
|
| 125 | + if (!isset($header_type[$heading])) { |
|
| 126 | + $header_type[$heading] = "scalar"; |
|
| 127 | + } else { |
|
| 128 | + $header_type[$heading] = "array"; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - while (($data = fgetcsv($handle, $options['len'], $options['delim'], $options['enclos'])) !== false) { |
|
| 135 | - $data = array_map('importer_csv_importcharset', $data); |
|
| 136 | - if ($options['head'] and isset($header)) { |
|
| 137 | - $row = array(); |
|
| 138 | - foreach ($header as $key => $heading) { |
|
| 139 | - if ($header_type[$heading] == "array") { |
|
| 140 | - if (!isset($row[$heading])) { |
|
| 141 | - $row[$heading] = array(); |
|
| 142 | - } |
|
| 143 | - if (isset($data[$key]) and strlen($data[$key])) { |
|
| 144 | - $row[$heading][] = $data[$key]; |
|
| 145 | - } |
|
| 146 | - } else { |
|
| 147 | - $row[$heading] = (isset($data[$key])) ? $data[$key] : ''; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - $return[] = $row; |
|
| 151 | - } else { |
|
| 152 | - $return[] = $data; |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - if ($options['charset_source']) { |
|
| 156 | - importer_csv_importcharset('', true); |
|
| 157 | - } |
|
| 158 | - } |
|
| 134 | + while (($data = fgetcsv($handle, $options['len'], $options['delim'], $options['enclos'])) !== false) { |
|
| 135 | + $data = array_map('importer_csv_importcharset', $data); |
|
| 136 | + if ($options['head'] and isset($header)) { |
|
| 137 | + $row = array(); |
|
| 138 | + foreach ($header as $key => $heading) { |
|
| 139 | + if ($header_type[$heading] == "array") { |
|
| 140 | + if (!isset($row[$heading])) { |
|
| 141 | + $row[$heading] = array(); |
|
| 142 | + } |
|
| 143 | + if (isset($data[$key]) and strlen($data[$key])) { |
|
| 144 | + $row[$heading][] = $data[$key]; |
|
| 145 | + } |
|
| 146 | + } else { |
|
| 147 | + $row[$heading] = (isset($data[$key])) ? $data[$key] : ''; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + $return[] = $row; |
|
| 151 | + } else { |
|
| 152 | + $return[] = $data; |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + if ($options['charset_source']) { |
|
| 156 | + importer_csv_importcharset('', true); |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - return $return; |
|
| 160 | + return $return; |
|
| 161 | 161 | } |