Skip to content

Instantly share code, notes, and snippets.

@VeroLom
Created October 17, 2019 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VeroLom/d1f4c9da5828d334c824f5a14c85d0d5 to your computer and use it in GitHub Desktop.
Save VeroLom/d1f4c9da5828d334c824f5a14c85d0d5 to your computer and use it in GitHub Desktop.
import.php
<meta charset="utf-8" />
<?php
require "standalone.php";
$file_name = 'files/filesToImport/import.xml';
$root_category_id = 2;
$category_type_id = \umiHierarchyTypesCollection::getInstance()->getTypeByName('catalog', 'category')->getId();
$brand_type_id = 126;
$model_type_id = 127;
$car_type_id = 128;
$domain_id = 1;
$reader = new XMLReader();
$reader->open($file_name);
//$reader->xml($data);
$doc = new DOMDocument();
$i = 1;
while($reader->read()) {
if($reader->nodeType == XMLReader::ELEMENT) {
if($reader->localName == 'auto') {
$node = simplexml_import_dom($doc->importNode($reader->expand(), true));
$attributes = $node->attributes();
?>
<p <?= $hl ?>><?= $attributes['name'] ?> <?= $attributes ?></p>
<p><strong>ID:</strong> <?= $attributes['id'] ?></p>
<p><strong>Марка:</strong> <?= $attributes['brand'] ?></p>
<p><strong>Модель:</strong> <?= $attributes['model'] ?></p>
<p><strong>Год:</strong> <?= $attributes['year'] ?></p>
<p><strong>Цвет:</strong> <?= $attributes['color'] ?></p>
<p><strong>VIN:</strong> <?= $attributes['vin'] ?></p>
<p><strong>Двигатель:</strong> <?= $attributes['engine'] ?></p>
<p><strong>Мощность (л/с):</strong> <?= $attributes['hp'] ?></p>
<p><strong>КПП:</strong> <?= $attributes['transm'] ?></p>
<p><strong>Руль:</strong> <?= $attributes['wheel'] ?></p>
<p><strong>Привод:</strong> <?= $attributes['drive'] ?></p>
<p><strong>Пробег:</strong> <?= str_replace(' ', '', $attributes['mileage']) ?></p>
<p><strong>Владельцев:</strong> <?= $attributes['owners'] ?></p>
<br />
<?
// Search a car
$car_selector = new selector('pages');
//$car_selector->where('domain')->equals($domain_id);
$car_selector->types('object-type')->id($car_type_id);
$car_selector->where('hierarchy')->page($root_category_id)->childs(3);
$car_selector->where('code')->equals("{$attributes['id']}");
if(!$car_selector->length) { // Car not found
$brand_id = 0;
if(strlen($attributes['brand'])) {
?><p><strong>Поиск бренда &laquo;<?= $attributes['brand'] ?>&raquo;</strong></p><?
// Search brand
$brand_selector = new selector('pages');
//$brand_selector->types('object-type')->id($brand_type_id);
$brand_selector->types('hierarchy-type')->name('catalog', 'category');
$brand_selector->where('hierarchy')->page($root_category_id)->childs(3);
$brand_selector->where('name')->equals($attributes['brand']);
if(!$brand_selector->length) { // Add if not found
?><p><strong>Бренд не найден</strong></p><?
$brand_id = umiHierarchy::getInstance()->addElement(
$root_category_id,
$category_type_id,
"{$attributes['brand']}",
trans("{$attributes['brand']}")
);
$brand = umiHierarchy::getInstance()->getElement($brand_id);
if($brand instanceof umiHierarchyElement) {
$brand->h1 = "{$attributes['brand']}";
$brand->title = "{$attributes['brand']}";
$brand->setIsActive(true);
$brand->commit();
} else {
throw new publicException("Ошибка при добавлении раздела бренда");
}
?><p>Бренд добавлен, id: <?= $brand_id ?></p><?
} else {
$brand_id = $brand_selector->first;
?><p><strong>Бренд найден, id: <?= $brand_id ?></strong></s</p><?
}
// Search model
// Add car
} else {
?><p style="color: red"><strong>Бренд не заполнен.</strong></p><?
}
} else {
?><p style="color: red"><strong>Автомобиль уже присутствует в системе.</strong></p><?
}
?><hr /><?
$i++;
}
}
}
?>
<p><strong>Итого: <?= $i ?></strong></p>
<style>
p { margin: 0 }
hr { margin: 1em 0 }
</style>
<?
function rus2translit($string) {
$converter = array(
'а' => 'a', 'б' => 'b', 'в' => 'v',
'г' => 'g', 'д' => 'd', 'е' => 'e',
'ё' => 'e', 'ж' => 'zh', 'з' => 'z',
'и' => 'i', 'й' => 'y', 'к' => 'k',
'л' => 'l', 'м' => 'm', 'н' => 'n',
'о' => 'o', 'п' => 'p', 'р' => 'r',
'с' => 's', 'т' => 't', 'у' => 'u',
'ф' => 'f', 'х' => 'h', 'ц' => 'c',
'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch',
'ь' => '\'', 'ы' => 'y', 'ъ' => '\'',
'э' => 'e', 'ю' => 'yu', 'я' => 'ya',
'А' => 'A', 'Б' => 'B', 'В' => 'V',
'Г' => 'G', 'Д' => 'D', 'Е' => 'E',
'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z',
'И' => 'I', 'Й' => 'Y', 'К' => 'K',
'Л' => 'L', 'М' => 'M', 'Н' => 'N',
'О' => 'O', 'П' => 'P', 'Р' => 'R',
'С' => 'S', 'Т' => 'T', 'У' => 'U',
'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sch',
'Ь' => '\'', 'Ы' => 'Y', 'Ъ' => '\'',
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya',
'Š' => 'S',
);
return strtr($string, $converter);
}
function trans($str) {
$str = rus2translit($str);
$str = strtolower($str);
$str = preg_replace('~[^-a-z0-9_]+~u', '-', $str);
$str = trim($str, "-");
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment