2016-01-13 18:43:41 +03:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class RetailcrmIcml
|
|
|
|
|
{
|
|
|
|
|
protected $shop;
|
|
|
|
|
protected $file;
|
|
|
|
|
protected $properties;
|
|
|
|
|
protected $params;
|
|
|
|
|
protected $dd;
|
|
|
|
|
protected $eCategories;
|
|
|
|
|
protected $eOffers;
|
|
|
|
|
|
|
|
|
|
public function __construct($shop, $file)
|
|
|
|
|
{
|
|
|
|
|
$this->shop = $shop;
|
|
|
|
|
$this->file = $file;
|
|
|
|
|
|
|
|
|
|
$this->properties = array(
|
|
|
|
|
'name',
|
|
|
|
|
'productName',
|
|
|
|
|
'price',
|
|
|
|
|
'purchasePrice',
|
|
|
|
|
'vendor',
|
|
|
|
|
'picture',
|
|
|
|
|
'url',
|
|
|
|
|
'xmlId',
|
2019-01-16 13:11:34 +03:00
|
|
|
|
'productActivity',
|
|
|
|
|
'dimensions'
|
2016-01-13 18:43:41 +03:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->params = array(
|
|
|
|
|
'article' => 'Артикул',
|
|
|
|
|
'color' => 'Цвет',
|
|
|
|
|
'weight' => 'Вес',
|
2017-08-14 12:06:10 +02:00
|
|
|
|
'tax' => 'Наценка'
|
2016-01-13 18:43:41 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function generate($categories, $offers)
|
|
|
|
|
{
|
|
|
|
|
$string = '<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
<yml_catalog date="' . date('Y-m-d H:i:s') . '">
|
|
|
|
|
<shop>
|
|
|
|
|
<name>' . $this->shop . '</name>
|
|
|
|
|
<categories/>
|
|
|
|
|
<offers/>
|
|
|
|
|
</shop>
|
|
|
|
|
</yml_catalog>
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
$xml = new SimpleXMLElement(
|
2018-12-25 13:31:07 +03:00
|
|
|
|
$string, LIBXML_NOENT | LIBXML_NOCDATA | LIBXML_COMPACT | LIBXML_PARSEHUGE
|
2016-01-13 18:43:41 +03:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->dd = new DOMDocument();
|
|
|
|
|
$this->dd->preserveWhiteSpace = false;
|
|
|
|
|
$this->dd->formatOutput = true;
|
|
|
|
|
$this->dd->loadXML($xml->asXML());
|
|
|
|
|
|
|
|
|
|
$this->eCategories = $this->dd
|
2018-12-25 13:31:07 +03:00
|
|
|
|
->getElementsByTagName('categories')->item(0);
|
2016-01-13 18:43:41 +03:00
|
|
|
|
$this->eOffers = $this->dd
|
2018-12-25 13:31:07 +03:00
|
|
|
|
->getElementsByTagName('offers')->item(0);
|
2016-01-13 18:43:41 +03:00
|
|
|
|
|
|
|
|
|
$this->addCategories($categories);
|
|
|
|
|
$this->addOffers($offers);
|
|
|
|
|
|
|
|
|
|
$this->dd->saveXML();
|
|
|
|
|
$this->dd->save($this->file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function addCategories($categories)
|
|
|
|
|
{
|
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
|
$e = $this->eCategories->appendChild(
|
2018-12-25 13:31:07 +03:00
|
|
|
|
$this->dd->createElement(
|
|
|
|
|
'category'
|
|
|
|
|
)
|
2016-01-13 18:43:41 +03:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$e->setAttribute('id', $category['id']);
|
2018-12-25 13:31:07 +03:00
|
|
|
|
$e->appendChild($this->dd->createElement('name', $category['name']));
|
|
|
|
|
|
|
|
|
|
if ($category['picture']) {
|
|
|
|
|
$e->appendChild($this->dd->createElement('picture', $category['picture']));
|
|
|
|
|
}
|
2016-01-13 18:43:41 +03:00
|
|
|
|
|
|
|
|
|
if ($category['parentId'] > 0) {
|
2017-08-14 12:06:10 +02:00
|
|
|
|
$e->setAttribute('parentId', $category['parentId']);
|
2016-01-13 18:43:41 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function addOffers($offers)
|
|
|
|
|
{
|
|
|
|
|
foreach ($offers as $offer) {
|
|
|
|
|
$e = $this->eOffers->appendChild(
|
2018-12-25 13:31:07 +03:00
|
|
|
|
$this->dd->createElement('offer')
|
2016-01-13 18:43:41 +03:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$e->setAttribute('id', $offer['id']);
|
|
|
|
|
$e->setAttribute('productId', $offer['productId']);
|
|
|
|
|
|
|
|
|
|
if (!empty($offer['quantity'])) {
|
|
|
|
|
$e->setAttribute('quantity', (int) $offer['quantity']);
|
|
|
|
|
} else {
|
|
|
|
|
$e->setAttribute('quantity', 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($offer['categoryId'] as $categoryId) {
|
|
|
|
|
$e->appendChild(
|
2018-12-25 13:31:07 +03:00
|
|
|
|
$this->dd->createElement('categoryId', $categoryId)
|
2016-01-13 18:43:41 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$offerKeys = array_keys($offer);
|
|
|
|
|
|
|
|
|
|
foreach ($offerKeys as $key) {
|
2018-12-25 13:31:07 +03:00
|
|
|
|
if ($offer[$key] == null) continue;
|
2016-08-29 15:59:19 +03:00
|
|
|
|
|
2016-01-13 18:43:41 +03:00
|
|
|
|
if (in_array($key, $this->properties)) {
|
2016-08-29 15:59:19 +03:00
|
|
|
|
if(is_array($offer[$key])) {
|
|
|
|
|
foreach($offer[$key] as $property) {
|
|
|
|
|
$e->appendChild(
|
|
|
|
|
$this->dd->createElement($key)
|
|
|
|
|
)->appendChild(
|
|
|
|
|
$this->dd->createTextNode(trim($property))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$e->appendChild(
|
2016-01-13 18:43:41 +03:00
|
|
|
|
$this->dd->createElement($key)
|
2016-08-29 15:59:19 +03:00
|
|
|
|
)->appendChild(
|
2016-01-13 18:43:41 +03:00
|
|
|
|
$this->dd->createTextNode(trim($offer[$key]))
|
2016-08-29 15:59:19 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
2016-01-13 18:43:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (in_array($key, array_keys($this->params))) {
|
|
|
|
|
$param = $this->dd->createElement('param');
|
|
|
|
|
$param->setAttribute('code', $key);
|
|
|
|
|
$param->setAttribute('name', $this->params[$key]);
|
|
|
|
|
$param->appendChild(
|
2018-12-25 13:31:07 +03:00
|
|
|
|
$this->dd->createTextNode($offer[$key])
|
2016-01-13 18:43:41 +03:00
|
|
|
|
);
|
|
|
|
|
$e->appendChild($param);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|