134 lines
3.5 KiB
PHP
134 lines
3.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* File from http://PrestaShow.pl
|
|
*
|
|
* DISCLAIMER
|
|
* Do not edit or add to this file if you wish to upgrade this module to newer
|
|
* versions in the future.
|
|
*
|
|
* @authors PrestaShow.pl <kontakt@prestashow.pl>
|
|
* @copyright 2015 PrestaShow.pl
|
|
* @license http://PrestaShow.pl/license
|
|
*/
|
|
class PShow_Csv
|
|
{
|
|
|
|
/**
|
|
* Convert csv into xml
|
|
*
|
|
* @param string $csv_filepath
|
|
* @param char $delim
|
|
* @param bool $removeCsvFile
|
|
* @return boolean
|
|
*/
|
|
public static function convertToXml($csv_filepath, $delim, $removeCsvFile = false, $headers_in_the_file = true, $encoding = 'UTF-8')
|
|
{
|
|
ini_set("auto_detect_line_endings", "1");
|
|
|
|
copy($csv_filepath, $csv_filepath . '.convert');
|
|
$csv = fopen($csv_filepath . '.convert', "r");
|
|
|
|
if (!$csv) {
|
|
return false;
|
|
}
|
|
|
|
$xml_filepath = str_replace('.csv', '.xml', $csv_filepath);
|
|
|
|
// init xml document
|
|
$xml = new XMLWriter();
|
|
$xml->openURI($xml_filepath);
|
|
$xml->startDocument('1.0', 'UTF-8');
|
|
$xml->setIndent(true);
|
|
|
|
$xml->startElement('OBJECTS'); // <objects>
|
|
|
|
$getrow = function() use ($csv, $delim, $encoding) {
|
|
|
|
$csv_row = fgetcsv($csv, 0, (($delim == 'tab') ? "\t" : $delim));
|
|
|
|
// support for broken pipe
|
|
if ($delim == '¦') {
|
|
$skipFirst = true;
|
|
foreach ($csv_row as &$csv_col) {
|
|
if ($skipFirst) {
|
|
$skipFirst = false;
|
|
continue;
|
|
}
|
|
|
|
$csv_col = substr($csv_col, 1);
|
|
}
|
|
}
|
|
// / support for broken pipe
|
|
|
|
if ($encoding != 'UTF-8') {
|
|
foreach ($csv_row as &$val) {
|
|
$val = iconv($encoding, "UTF-8", $val);
|
|
}
|
|
}
|
|
|
|
return $csv_row;
|
|
};
|
|
|
|
$checkRow = function ($data) {
|
|
if (!is_array($data) || count($data) == 0) {
|
|
return false;
|
|
}
|
|
|
|
foreach ($data as $v) {
|
|
if (!empty($v)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
$headers = array();
|
|
|
|
while (($data = $getrow()) !== false) {
|
|
|
|
if (!$checkRow($data)) {
|
|
continue;
|
|
}
|
|
|
|
if (!count($headers) && $headers_in_the_file) {
|
|
$headers = $data;
|
|
|
|
foreach ($headers as &$header) {
|
|
$header = str_replace(' ', '_', $header);
|
|
$header = preg_replace('/[^A-Za-z0-9\-\_]/', '_', $header);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (!count($headers) && !$headers_in_the_file) {
|
|
$headers = $data;
|
|
|
|
foreach ($headers as $index => $h) {
|
|
$headers[$index] = 'column_' . (((int) $index) + 1);
|
|
}
|
|
}
|
|
|
|
$xml->startElement('OBJECT'); // <object>
|
|
|
|
foreach ($headers as $index => $head) {
|
|
$xml->writeElement($head, $data[$index]);
|
|
}
|
|
|
|
$xml->endElement(); // </object>
|
|
}
|
|
|
|
fclose($csv);
|
|
|
|
$xml->endElement(); // </objects>
|
|
|
|
$xml->endDocument();
|
|
|
|
@unlink($csv_filepath . '.convert');
|
|
if ($removeCsvFile && strtolower(pathinfo($csv_filepath, PATHINFO_EXTENSION)) != 'xml') {
|
|
@unlink($csv_filepath);
|
|
}
|
|
}
|
|
}
|