Модуль импорта v2.26.0 (Light версия)
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.
 
 

192 lines
3.0 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 = array();
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 = array();
$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 = array(); // Определяем теги
foreach ($a[0][0] AS $key => $value)
{
$tags[] = $key;
}
$items = array();
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;
}
/* !DBF */
public static function DBF2Array($fname)
{
$db = dbase_open($fname, 0);
$cols = dbase_get_header_info($db);
$count = dbase_numrecords($db);
$rows = array();
$tags = array();
foreach($cols AS $k => $v)
{
$tags[] = $v['name'];
}
$ii = 1;
for ($ii; $ii <= $count; $ii++)
{
$rows[] = dbase_get_record_with_names($db, $ii);
}
$result =
[
'num' => $count - $parser_row,
'tags' => $tags,
'rows' => $rows
];
dbase_close($db);
unset($cols, $count, $tags, $rows);
return $result;
}
}