You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
2.5 KiB
153 lines
2.5 KiB
<? |
|
/** |
|
* Класс, включающий все парсеры импорта |
|
* |
|
* @package AVE.cms |
|
* @subpackage module_DocManager |
|
* @filesource |
|
*/ |
|
|
|
class ModuleImportParsers |
|
{ |
|
/* !CSV */ |
|
function CSV2Array($csvfile, $sep=';', $protect='"') |
|
{ |
|
$data = file_get_contents($csvfile); |
|
|
|
if (! $data) |
|
return; |
|
|
|
// проверяем кодировку |
|
if (mb_check_encoding($data, 'cp1251')) |
|
{ |
|
$utf8_tmp = true; |
|
$csvfile = $csvfile . '.utf8.tmp'; |
|
$data = @iconv('cp1251', 'UTF-8', $data); |
|
file_put_contents($csvfile, $data); |
|
} |
|
|
|
$handle = fopen($csvfile, 'r'); |
|
|
|
// берём первую строку как теги |
|
$tags = fgetcsv($handle, 0, $sep, $protect); |
|
|
|
array_walk_recursive($tags, 'trim'); |
|
|
|
$ii = 0; |
|
|
|
$items = []; |
|
|
|
while ($item = fgetcsv($handle, 0, $sep, $protect)) |
|
{ |
|
array_walk_recursive($item, 'trim'); |
|
|
|
foreach($item AS $key => $val) |
|
{ |
|
if ($val != '') |
|
$items[$ii][$tags[$key]] = $val; |
|
} |
|
|
|
$ii++; |
|
} |
|
|
|
fclose($handle); |
|
|
|
if ($utf8_tmp) |
|
unlink($csvfile); |
|
|
|
$result = |
|
[ |
|
'num' => count($items), |
|
'tags' => $tags, |
|
'rows' => $items |
|
]; |
|
|
|
unset($tags, $items); |
|
|
|
return $result; |
|
} |
|
|
|
/* !Excel */ |
|
function Excel2Array($fname) |
|
{ |
|
require_once(dirname(__FILE__).'/excel_reader.php'); |
|
|
|
$Excel = new Spreadsheet_Excel_Reader(); // создаем объект |
|
|
|
$Excel->setOutputEncoding('UTF-8'); // устанавливаем кодировку |
|
|
|
$Excel->read($fname); // открываем файл |
|
|
|
$tags = $Excel->sheets[0]['cells'][1]; // Определяем теги |
|
|
|
$items = []; |
|
|
|
$ii = 0; |
|
|
|
foreach ($Excel->sheets[0]['cells'] AS $key => $value) |
|
{ |
|
if ($key == 1) |
|
continue; |
|
|
|
foreach($value AS $k => $val) |
|
{ |
|
$items[$ii][$tags[$k]] = $val; |
|
} |
|
|
|
$ii++; |
|
} |
|
|
|
$result = |
|
[ |
|
'num' => count($items), |
|
'tags' => $tags, |
|
'rows' => $items |
|
]; |
|
|
|
unset($Excel, $tags, $items); |
|
|
|
return $result; |
|
} |
|
|
|
/* !XML */ |
|
function XML2Array($fname) |
|
{ |
|
$xml = (simplexml_load_file($fname)); |
|
|
|
|
|
$xml = object2array($xml); |
|
|
|
unset ($xml['@attributes']); |
|
|
|
$a = array_values($xml); |
|
|
|
$tags = []; // Определяем теги |
|
|
|
foreach ($a[0][0] AS $key => $value) |
|
{ |
|
$tags[] = $key; |
|
} |
|
|
|
$items = []; |
|
|
|
foreach ($a[0] AS $key => $row) |
|
{ |
|
foreach($row AS $k => $val) |
|
{ |
|
if ($val != '') |
|
$items[$key][$k] = $val; |
|
} |
|
} |
|
|
|
$result = |
|
[ |
|
'num' => count($items), |
|
'tags' => $tags, |
|
'rows' => $items |
|
]; |
|
|
|
unset($xml, $a, $tags, $items); |
|
|
|
return $result; |
|
} |
|
}
|
|
|