С горем пополам реализовал парсинг товаров из xls. По товарам проблем нет. Проблема осталась такая - неправильно парсятся категории. А именно проблема в построении иерархии. Наверняка что-то упустил в БД. В excel категории и подкатегории реализованы как вложенные раскрывающиеся списки.
class Category {
public $id = 0;
public $name = '';
public $parent = 0;
public $level = 0;
function __construct($params) {
if (isset($params['data']) && isset($params['row']) && isset($params['parent']) && isset($params['pdo'])) {
$unformatted_name = $params['data']->getCellByColumnAndRow(2, $params['row'])->getValue();
$this->name = $this->format_name($unformatted_name);
$this->level = $this->detect_level($unformatted_name);
$this->parent = ($this->level === 0) ? 0 : $params['parent'];
$this->load($params['pdo'], $params['parent']);
}
}
private function format_name($unformatted_name) {
$lower = mb_strtolower( trim($unformatted_name), 'UTF-8' );
return mb_strtoupper(mb_substr($lower, 0, 1, 'UTF-8'), 'UTF-8') . mb_substr(mb_convert_case($lower, MB_CASE_LOWER, 'UTF-8'), 1, mb_strlen($lower), 'UTF-8');
}
private function detect_level($unformatted_name) {
$spaces = preg_match_all("/^(\s+).*/", $unformatted_name, $matches);
return strlen( $matches[1][0] ) / 4 - 1;
}
private function load($pdo, $parent) {
$sth = $pdo->prepare("SELECT * FROM ".DB_PREFIX."category_description WHERE name='".$this->name."'");
$sth->execute();
$result = $sth->fetch();
if ( $result !== false )
$this->id = $result['category_id'];
else {
$sth = $pdo->prepare("INSERT INTO ".DB_PREFIX."category (`parent_id`, `column`, `status`) VALUES (:parent_id, 1, 1)");
$sth->bindParam(':parent_id', $this->parent, PDO::PARAM_INT);
$sth->execute();
$this->id = $pdo->lastInsertId();
$sth = $pdo->prepare("INSERT INTO ".DB_PREFIX."category_description (`category_id`, `language_id`, `name`) VALUES (:category_id, 1, :name)");
$sth->bindParam(':category_id', $this->id, PDO::PARAM_INT);
$sth->bindParam(':name', $this->name, PDO::PARAM_STR);
$sth->execute();
$sth = $pdo->prepare("INSERT INTO ".DB_PREFIX."category_to_store (`category_id`, `store_id`) VALUES (:category_id, 0)");
$sth->bindParam(':category_id', $this->id, PDO::PARAM_INT);
$sth->execute();
}
}
public static function isCategory($data, $row) {
return ( trim($data->getCellByColumnAndRow(1, $row)->getValue()) == "" );
}
}