Révision a192dc0b
Ajouté par Assos Assos il y a environ 8 ans
drupal7/sites/all/modules/feeds/plugins/FeedsCSVParser.inc | ||
---|---|---|
20 | 20 |
// Load and configure parser. |
21 | 21 |
feeds_include_library('ParserCSV.inc', 'ParserCSV'); |
22 | 22 |
$parser = new ParserCSV(); |
23 |
$delimiter = $source_config['delimiter'] == 'TAB' ? "\t" : $source_config['delimiter'];
|
|
23 |
$delimiter = $this->getDelimiterChar($source_config);
|
|
24 | 24 |
$parser->setDelimiter($delimiter); |
25 |
if (isset($source_config['encoding'])) { |
|
26 |
// Encoding can only be set when the mbstring extension is loaded. |
|
27 |
$parser->setEncoding($source_config['encoding']); |
|
28 |
} |
|
25 | 29 |
|
26 | 30 |
$iterator = new ParserCSVIterator($fetcher_result->getFilePath()); |
27 | 31 |
if (empty($source_config['no_headers'])) { |
... | ... | |
101 | 105 |
return parent::getSourceElement($source, $result, drupal_strtolower($element_key)); |
102 | 106 |
} |
103 | 107 |
|
108 |
/** |
|
109 |
* Override parent::getMappingSourceList() to use only lower keys. |
|
110 |
*/ |
|
111 |
public function getMappingSourceList() { |
|
112 |
return array_map('drupal_strtolower', parent::getMappingSourceList()); |
|
113 |
} |
|
114 |
|
|
104 | 115 |
/** |
105 | 116 |
* Define defaults. |
106 | 117 |
*/ |
107 | 118 |
public function sourceDefaults() { |
108 | 119 |
return array( |
109 | 120 |
'delimiter' => $this->config['delimiter'], |
121 |
'encoding' => $this->config['encoding'], |
|
110 | 122 |
'no_headers' => $this->config['no_headers'], |
111 | 123 |
); |
112 | 124 |
} |
... | ... | |
123 | 135 |
$mappings = feeds_importer($this->id)->processor->config['mappings']; |
124 | 136 |
$sources = $uniques = array(); |
125 | 137 |
foreach ($mappings as $mapping) { |
126 |
$sources[] = check_plain($mapping['source']); |
|
138 |
if (strpos($mapping['source'], ',') !== FALSE) { |
|
139 |
$sources[] = '"' . $mapping['source'] . '"'; |
|
140 |
} |
|
141 |
else { |
|
142 |
$sources[] = $mapping['source']; |
|
143 |
} |
|
127 | 144 |
if (!empty($mapping['unique'])) { |
128 |
$uniques[] = check_plain($mapping['source']);
|
|
145 |
$uniques[] = $mapping['source'];
|
|
129 | 146 |
} |
130 | 147 |
} |
131 | 148 |
$sources = array_unique($sources); |
132 | 149 |
|
133 |
$output = t('Import !csv_files with one or more of these columns: !columns.', array('!csv_files' => l(t('CSV files'), 'http://en.wikipedia.org/wiki/Comma-separated_values'), '!columns' => implode(', ', $sources)));
|
|
150 |
$output = t('Import !csv_files with one or more of these columns: @columns.', array('!csv_files' => l(t('CSV files'), 'http://en.wikipedia.org/wiki/Comma-separated_values'), '@columns' => implode(', ', $sources)));
|
|
134 | 151 |
$items = array(); |
135 |
$items[] = format_plural(count($uniques), 'Column <strong>!columns</strong> is mandatory and considered unique: only one item per !columns value will be created.', 'Columns <strong>!columns</strong> are mandatory and values in these columns are considered unique: only one entry per value in one of these column will be created.', array('!columns' => implode(', ', $uniques)));
|
|
152 |
$items[] = format_plural(count($uniques), 'Column <strong>@columns</strong> is mandatory and considered unique: only one item per @columns value will be created.', 'Columns <strong>@columns</strong> are mandatory and values in these columns are considered unique: only one entry per value in one of these column will be created.', array('@columns' => implode(', ', $uniques)));
|
|
136 | 153 |
$items[] = l(t('Download a template'), 'import/' . $this->id . '/template'); |
137 | 154 |
$form['help'] = array( |
138 | 155 |
'#prefix' => '<div class="help">', |
... | ... | |
151 | 168 |
'#type' => 'select', |
152 | 169 |
'#title' => t('Delimiter'), |
153 | 170 |
'#description' => t('The character that delimits fields in the CSV file.'), |
154 |
'#options' => array( |
|
155 |
',' => ',', |
|
156 |
';' => ';', |
|
157 |
'TAB' => 'TAB', |
|
158 |
'|' => '|', |
|
159 |
'+' => '+', |
|
160 |
), |
|
171 |
'#options' => $this->getAllDelimiterTypes(), |
|
161 | 172 |
'#default_value' => isset($source_config['delimiter']) ? $source_config['delimiter'] : ',', |
162 | 173 |
); |
163 | 174 |
$form['no_headers'] = array( |
... | ... | |
166 | 177 |
'#description' => t('Check if the imported CSV file does not start with a header row. If checked, mapping sources must be named \'0\', \'1\', \'2\' etc.'), |
167 | 178 |
'#default_value' => isset($source_config['no_headers']) ? $source_config['no_headers'] : 0, |
168 | 179 |
); |
180 |
$form['encoding'] = $this->configEncodingForm(); |
|
181 |
if (isset($source_config['encoding'])) { |
|
182 |
$form['encoding']['#default_value'] = $source_config['encoding']; |
|
183 |
} |
|
169 | 184 |
return $form; |
170 | 185 |
} |
171 | 186 |
|
... | ... | |
175 | 190 |
public function configDefaults() { |
176 | 191 |
return array( |
177 | 192 |
'delimiter' => ',', |
193 |
'encoding' => 'UTF-8', |
|
178 | 194 |
'no_headers' => 0, |
179 | 195 |
); |
180 | 196 |
} |
... | ... | |
188 | 204 |
'#type' => 'select', |
189 | 205 |
'#title' => t('Default delimiter'), |
190 | 206 |
'#description' => t('Default field delimiter.'), |
191 |
'#options' => array( |
|
192 |
',' => ',', |
|
193 |
';' => ';', |
|
194 |
'TAB' => 'TAB', |
|
195 |
'|' => '|', |
|
196 |
'+' => '+', |
|
197 |
), |
|
207 |
'#options' => $this->getAllDelimiterTypes(), |
|
198 | 208 |
'#default_value' => $this->config['delimiter'], |
199 | 209 |
); |
200 | 210 |
$form['no_headers'] = array( |
... | ... | |
203 | 213 |
'#description' => t('Check if the imported CSV file does not start with a header row. If checked, mapping sources must be named \'0\', \'1\', \'2\' etc.'), |
204 | 214 |
'#default_value' => $this->config['no_headers'], |
205 | 215 |
); |
216 |
$form['encoding'] = $this->configEncodingForm(); |
|
206 | 217 |
return $form; |
207 | 218 |
} |
208 | 219 |
|
220 |
/** |
|
221 |
* Builds configuration field for setting file encoding. |
|
222 |
* |
|
223 |
* If the mbstring extension is not available a markup render array |
|
224 |
* will be returned instead. |
|
225 |
* |
|
226 |
* @return array |
|
227 |
* A renderable array. |
|
228 |
*/ |
|
229 |
public function configEncodingForm() { |
|
230 |
if (extension_loaded('mbstring') && variable_get('feeds_use_mbstring', TRUE)) { |
|
231 |
// Get the system's list of available encodings. |
|
232 |
$options = mb_list_encodings(); |
|
233 |
// Make the key/values the same in the array. |
|
234 |
$options = array_combine($options, $options); |
|
235 |
// Sort alphabetically not-case sensitive. |
|
236 |
natcasesort($options); |
|
237 |
return array( |
|
238 |
'#type' => 'select', |
|
239 |
'#title' => t('File encoding'), |
|
240 |
'#description' => t('Performs character encoding conversion from selected option to UTF-8.'), |
|
241 |
'#options' => $options, |
|
242 |
'#default_value' => $this->config['encoding'], |
|
243 |
); |
|
244 |
} |
|
245 |
else { |
|
246 |
return array( |
|
247 |
'#markup' => '<em>' . t('PHP mbstring extension must be available for character encoding conversion.') . '</em>', |
|
248 |
); |
|
249 |
} |
|
250 |
} |
|
251 |
|
|
209 | 252 |
public function getTemplate() { |
210 | 253 |
$mappings = feeds_importer($this->id)->processor->config['mappings']; |
211 | 254 |
$sources = $uniques = array(); |
255 |
|
|
212 | 256 |
foreach ($mappings as $mapping) { |
213 |
if (in_array(check_plain($mapping['source']), $uniques) || in_array(check_plain($mapping['source']), $sources)) {
|
|
257 |
if (in_array($mapping['source'], $uniques) || in_array($mapping['source'], $sources)) {
|
|
214 | 258 |
// Skip columns we've already seen. |
215 | 259 |
continue; |
216 | 260 |
} |
217 | 261 |
|
218 | 262 |
if (!empty($mapping['unique'])) { |
219 |
$uniques[] = check_plain($mapping['source']);
|
|
263 |
$uniques[] = $mapping['source'];
|
|
220 | 264 |
} |
221 | 265 |
else { |
222 |
$sources[] = check_plain($mapping['source']);
|
|
266 |
$sources[] = $mapping['source'];
|
|
223 | 267 |
} |
224 | 268 |
} |
225 |
$sep = $this->config['delimiter']; |
|
269 |
|
|
270 |
$sep = $this->getDelimiterChar($this->config); |
|
226 | 271 |
$columns = array(); |
272 |
|
|
227 | 273 |
foreach (array_merge($uniques, $sources) as $col) { |
228 | 274 |
if (strpos($col, $sep) !== FALSE) { |
229 | 275 |
$col = '"' . str_replace('"', '""', $col) . '"'; |
230 | 276 |
} |
277 |
|
|
231 | 278 |
$columns[] = $col; |
232 | 279 |
} |
233 |
drupal_add_http_header('Cache-Control', 'max-age=60, must-revalidate'); |
|
234 |
drupal_add_http_header('Content-Disposition', 'attachment; filename="' . $this->id . '_template.csv"'); |
|
235 |
drupal_add_http_header('Content-type', 'text/csv; charset=utf-8'); |
|
280 |
|
|
281 |
$template_file_details = $this->getTemplateFileDetails($this->config); |
|
282 |
|
|
283 |
$filename = "{$this->id}_template.{$template_file_details['extension']}"; |
|
284 |
$cache_control = 'max-age=60, must-revalidate'; |
|
285 |
$content_disposition = 'attachment; filename="' . $filename . '"'; |
|
286 |
$content_type = "{$template_file_details['mime_type']}; charset=utf-8"; |
|
287 |
|
|
288 |
drupal_add_http_header('Cache-Control', $cache_control); |
|
289 |
drupal_add_http_header('Content-Disposition', $content_disposition); |
|
290 |
drupal_add_http_header('Content-type', $content_type); |
|
291 |
|
|
236 | 292 |
print implode($sep, $columns); |
237 |
return; |
|
293 |
} |
|
294 |
|
|
295 |
/** |
|
296 |
* Gets an associative array of the delimiters supported by this parser. |
|
297 |
* |
|
298 |
* The keys represent the value that is persisted into the database, and the |
|
299 |
* value represents the text that is shown in the admins UI. |
|
300 |
* |
|
301 |
* @return array |
|
302 |
* The associative array of delimiter types to display name. |
|
303 |
*/ |
|
304 |
protected function getAllDelimiterTypes() { |
|
305 |
$delimiters = array( |
|
306 |
',', |
|
307 |
';', |
|
308 |
'TAB', |
|
309 |
'|', |
|
310 |
'+', |
|
311 |
); |
|
312 |
|
|
313 |
return array_combine($delimiters, $delimiters); |
|
314 |
} |
|
315 |
|
|
316 |
/** |
|
317 |
* Gets the appropriate delimiter character for the delimiter in the config. |
|
318 |
* |
|
319 |
* @param array $config |
|
320 |
* The configuration for the parser. |
|
321 |
* |
|
322 |
* @return string |
|
323 |
* The delimiter character. |
|
324 |
*/ |
|
325 |
protected function getDelimiterChar(array $config) { |
|
326 |
$config_delimiter = $config['delimiter']; |
|
327 |
|
|
328 |
switch ($config_delimiter) { |
|
329 |
case 'TAB': |
|
330 |
$delimiter = "\t"; |
|
331 |
break; |
|
332 |
|
|
333 |
default: |
|
334 |
$delimiter = $config_delimiter; |
|
335 |
break; |
|
336 |
} |
|
337 |
|
|
338 |
return $delimiter; |
|
339 |
} |
|
340 |
|
|
341 |
/** |
|
342 |
* Gets details about the template file, for the delimiter in the config. |
|
343 |
* |
|
344 |
* The resulting details indicate the file extension and mime type for the |
|
345 |
* delimiter type. |
|
346 |
* |
|
347 |
* @param array $config |
|
348 |
* The configuration for the parser. |
|
349 |
* |
|
350 |
* @return array |
|
351 |
* An array with the following information: |
|
352 |
* - 'extension': The file extension for the template ('tsv', 'csv', etc). |
|
353 |
* - 'mime-type': The mime type for the template |
|
354 |
* ('text/tab-separated-values', 'text/csv', etc). |
|
355 |
*/ |
|
356 |
protected function getTemplateFileDetails(array $config) { |
|
357 |
switch ($config['delimiter']) { |
|
358 |
case 'TAB': |
|
359 |
$extension = 'tsv'; |
|
360 |
$mime_type = 'text/tab-separated-values'; |
|
361 |
break; |
|
362 |
|
|
363 |
default: |
|
364 |
$extension = 'csv'; |
|
365 |
$mime_type = 'text/csv'; |
|
366 |
break; |
|
367 |
} |
|
368 |
|
|
369 |
return array( |
|
370 |
'extension' => $extension, |
|
371 |
'mime_type' => $mime_type, |
|
372 |
); |
|
238 | 373 |
} |
239 | 374 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules