mirror of
https://github.com/retailcrm/prestashop-module.git
synced 2025-03-01 19:03:14 +03:00
Optimized ICML generation
This commit is contained in:
parent
36fa3c055f
commit
d40818548e
@ -193,6 +193,8 @@ class RetailcrmCatalog
|
|||||||
$article = null;
|
$article = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$productName = htmlspecialchars(strip_tags($product['name']));
|
||||||
|
|
||||||
$weight = $this->getWeightInKg($product['weight']);
|
$weight = $this->getWeightInKg($product['weight']);
|
||||||
|
|
||||||
$width = round($product['width'], 3);
|
$width = round($product['width'], 3);
|
||||||
@ -265,16 +267,22 @@ class RetailcrmCatalog
|
|||||||
$offerArticle = $article;
|
$offerArticle = $article;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$name = htmlspecialchars(
|
||||||
|
strip_tags(
|
||||||
|
Product::getProductName($product['id_product'], $offer['id_product_attribute'])
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
$item = [
|
$item = [
|
||||||
'id' => $product['id_product'] . '#' . $offer['id_product_attribute'],
|
'id' => $product['id_product'] . '#' . $offer['id_product_attribute'],
|
||||||
'productId' => $product['id_product'],
|
'productId' => $product['id_product'],
|
||||||
'productActivity' => ($available_for_order) ? 'Y' : 'N',
|
'productActivity' => ($available_for_order) ? 'Y' : 'N',
|
||||||
'name' => htmlspecialchars(strip_tags(Product::getProductName($product['id_product'], $offer['id_product_attribute']))),
|
'name' => ('' === $name) ? $productName : $name,
|
||||||
'productName' => htmlspecialchars(strip_tags($product['name'])),
|
'productName' => $productName,
|
||||||
'categoryId' => $categoriesLeft,
|
'categoryId' => $categoriesLeft,
|
||||||
'picture' => $pictures,
|
'picture' => $pictures,
|
||||||
'url' => $url,
|
'url' => $url,
|
||||||
'quantity' => 0 < $quantity ? $quantity : 0,
|
'quantity' => (0 < $quantity) ? $quantity : 0,
|
||||||
'purchasePrice' => $offerPurchasePrice,
|
'purchasePrice' => $offerPurchasePrice,
|
||||||
'price' => round($offerPrice, 2),
|
'price' => round($offerPrice, 2),
|
||||||
'vendor' => $vendor,
|
'vendor' => $vendor,
|
||||||
@ -326,8 +334,8 @@ class RetailcrmCatalog
|
|||||||
'id' => $product['id_product'],
|
'id' => $product['id_product'],
|
||||||
'productId' => $product['id_product'],
|
'productId' => $product['id_product'],
|
||||||
'productActivity' => ($available_for_order) ? 'Y' : 'N',
|
'productActivity' => ($available_for_order) ? 'Y' : 'N',
|
||||||
'name' => htmlspecialchars(strip_tags($product['name'])),
|
'name' => $productName,
|
||||||
'productName' => htmlspecialchars(strip_tags($product['name'])),
|
'productName' => $productName,
|
||||||
'categoryId' => $categoriesLeft,
|
'categoryId' => $categoriesLeft,
|
||||||
'picture' => $pictures,
|
'picture' => $pictures,
|
||||||
'url' => $url,
|
'url' => $url,
|
||||||
|
@ -40,18 +40,25 @@ class RetailcrmIcml
|
|||||||
{
|
{
|
||||||
protected $shop;
|
protected $shop;
|
||||||
protected $file;
|
protected $file;
|
||||||
|
protected $tmpFile;
|
||||||
protected $properties;
|
protected $properties;
|
||||||
protected $params;
|
protected $params;
|
||||||
protected $dd;
|
protected $dd;
|
||||||
protected $eCategories;
|
protected $eCategories;
|
||||||
protected $eOffers;
|
protected $eOffers;
|
||||||
|
/**
|
||||||
|
* @var XMLWriter
|
||||||
|
*/
|
||||||
|
private $writer;
|
||||||
|
|
||||||
public function __construct($shop, $file)
|
public function __construct($shop, $file)
|
||||||
{
|
{
|
||||||
$this->shop = $shop;
|
$this->shop = $shop;
|
||||||
$this->file = $file;
|
$this->file = $file;
|
||||||
|
$this->tmpFile = sprintf('%s.tmp', $this->file);
|
||||||
|
|
||||||
$this->properties = [
|
$this->properties = [
|
||||||
|
'categoryId',
|
||||||
'name',
|
'name',
|
||||||
'productName',
|
'productName',
|
||||||
'price',
|
'price',
|
||||||
@ -75,129 +82,189 @@ class RetailcrmIcml
|
|||||||
|
|
||||||
public function generate($categories, $offers)
|
public function generate($categories, $offers)
|
||||||
{
|
{
|
||||||
$string = '<?xml version="1.0" encoding="UTF-8"?>
|
if (file_exists($this->tmpFile)) {
|
||||||
<yml_catalog date="' . date('Y-m-d H:i:s') . '">
|
unlink($this->tmpFile);
|
||||||
<shop>
|
}
|
||||||
<name>' . $this->shop . '</name>
|
|
||||||
<categories/>
|
|
||||||
<offers/>
|
|
||||||
</shop>
|
|
||||||
</yml_catalog>
|
|
||||||
';
|
|
||||||
|
|
||||||
$xml = new SimpleXMLElement(
|
$this->loadWriter();
|
||||||
$string,
|
$this->writeHead();
|
||||||
LIBXML_NOENT | LIBXML_NOCDATA | LIBXML_COMPACT | LIBXML_PARSEHUGE
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->dd = new DOMDocument();
|
if (!empty($categories)) {
|
||||||
$this->dd->preserveWhiteSpace = false;
|
$this->writeCategories($categories);
|
||||||
$this->dd->formatOutput = true;
|
unset($categories);
|
||||||
$this->dd->loadXML($xml->asXML());
|
}
|
||||||
|
|
||||||
$this->eCategories = $this->dd
|
if (!empty($offers)) {
|
||||||
->getElementsByTagName('categories')->item(0);
|
$this->writeOffers($offers);
|
||||||
$this->eOffers = $this->dd
|
unset($offers);
|
||||||
->getElementsByTagName('offers')->item(0);
|
}
|
||||||
|
|
||||||
|
$this->writeEnd();
|
||||||
|
$this->formatXml();
|
||||||
|
|
||||||
|
rename($this->tmpFile, $this->file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function loadWriter()
|
||||||
|
{
|
||||||
|
if (!$this->writer) {
|
||||||
|
$writer = new \XMLWriter();
|
||||||
|
$writer->openUri($this->tmpFile);
|
||||||
|
|
||||||
|
$this->writer = $writer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function writeHead()
|
||||||
|
{
|
||||||
|
$this->writer->startDocument('1.0', 'UTF-8');
|
||||||
|
$this->writer->startElement('yml_catalog'); // start <yml_catalog>
|
||||||
|
$this->writer->writeAttribute('date', date('Y-m-d H:i:s'));
|
||||||
|
$this->writer->startElement('shop'); // start <shop>
|
||||||
|
$this->writer->WriteElement('name', $this->shop);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $categories
|
||||||
|
*/
|
||||||
|
private function writeCategories($categories)
|
||||||
|
{
|
||||||
|
$this->writer->startElement('categories'); // start <categories>
|
||||||
|
|
||||||
$this->addCategories($categories);
|
$this->addCategories($categories);
|
||||||
$this->addOffers($offers);
|
|
||||||
|
|
||||||
$this->dd->saveXML();
|
$this->writer->endElement(); // end </categories>
|
||||||
$this->dd->save($this->file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addCategories($categories)
|
private function addCategories($categories)
|
||||||
{
|
{
|
||||||
foreach ($categories as $category) {
|
foreach ($categories as $category) {
|
||||||
$e = $this->eCategories->appendChild(
|
if (!array_key_exists('name', $category) || !array_key_exists('id', $category)) {
|
||||||
$this->dd->createElement(
|
continue;
|
||||||
'category'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
$e->setAttribute('id', $category['id']);
|
|
||||||
$e->appendChild($this->dd->createElement('name', $category['name']));
|
|
||||||
|
|
||||||
if ($category['picture']) {
|
|
||||||
$e->appendChild($this->dd->createElement('picture', $category['picture']));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0 < $category['parentId']) {
|
$this->writer->startElement('category'); // start <category>
|
||||||
$e->setAttribute('parentId', $category['parentId']);
|
|
||||||
|
$this->writer->writeAttribute('id', $category['id']);
|
||||||
|
|
||||||
|
if (array_key_exists('parentId', $category) && 0 < $category['parentId']) {
|
||||||
|
$this->writer->writeAttribute('parentId', $category['parentId']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->writer->writeElement('name', $category['name']);
|
||||||
|
|
||||||
|
if (array_key_exists('picture', $category) && $category['picture']) {
|
||||||
|
$this->writer->writeElement('picture', $category['picture']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->writer->endElement(); // end </category>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $offers
|
||||||
|
*/
|
||||||
|
private function writeOffers($offers)
|
||||||
|
{
|
||||||
|
$this->writer->startElement('offers'); // start <offers>
|
||||||
|
$this->addOffers($offers);
|
||||||
|
$this->writer->endElement(); // end </offers>
|
||||||
|
}
|
||||||
|
|
||||||
private function addOffers($offers)
|
private function addOffers($offers)
|
||||||
{
|
{
|
||||||
foreach ($offers as $offer) {
|
foreach ($offers as $offer) {
|
||||||
$e = $this->eOffers->appendChild(
|
if (!array_key_exists('id', $offer)) {
|
||||||
$this->dd->createElement('offer')
|
continue;
|
||||||
);
|
}
|
||||||
|
|
||||||
$e->setAttribute('id', $offer['id']);
|
$this->writer->startElement('offer'); // start <offer>
|
||||||
$e->setAttribute('productId', $offer['productId']);
|
|
||||||
|
|
||||||
if (!empty($offer['quantity'])) {
|
$this->writer->writeAttribute('id', $offer['id']);
|
||||||
$e->setAttribute('quantity', (int) $offer['quantity']);
|
$this->writer->writeAttribute('productId', $offer['productId']);
|
||||||
|
$this->writer->writeAttribute('quantity', (int) $offer['quantity']);
|
||||||
|
|
||||||
|
unset($offer['id'], $offer['productId'], $offer['quantity']);
|
||||||
|
|
||||||
|
$this->setOffersProperties($offer);
|
||||||
|
$this->setOffersParams($offer);
|
||||||
|
$this->setOffersCombinations($offer);
|
||||||
|
|
||||||
|
$this->writer->endElement(); // end </offer>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setOffersProperties($offer)
|
||||||
|
{
|
||||||
|
foreach ($offer as $key => $value) {
|
||||||
|
if (!in_array($key, $this->properties)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($value)) {
|
||||||
|
foreach ($value as $element) {
|
||||||
|
$this->writer->writeElement($key, $element);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$e->setAttribute('quantity', 0);
|
$this->writer->writeElement($key, $value);
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($offer['categoryId'] as $categoryId) {
|
|
||||||
$e->appendChild(
|
|
||||||
$this->dd->createElement('categoryId', $categoryId)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$offerKeys = array_keys($offer);
|
|
||||||
|
|
||||||
foreach ($offerKeys as $key) {
|
|
||||||
if (null == $offer[$key]) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (in_array($key, $this->properties)) {
|
|
||||||
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(
|
|
||||||
$this->dd->createElement($key)
|
|
||||||
)->appendChild(
|
|
||||||
$this->dd->createTextNode(trim($offer[$key]))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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(
|
|
||||||
$this->dd->createTextNode($offer[$key])
|
|
||||||
);
|
|
||||||
$e->appendChild($param);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($offer['combination'])) {
|
|
||||||
foreach ($offer['combination'] as $id => $comb) {
|
|
||||||
$param = $this->dd->createElement('param');
|
|
||||||
$param->setAttribute('code', $id);
|
|
||||||
$param->setAttribute('name', $comb['group_name']);
|
|
||||||
$param->appendChild(
|
|
||||||
$this->dd->createTextNode($comb['attribute_name'])
|
|
||||||
);
|
|
||||||
$e->appendChild($param);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function setOffersParams($offer)
|
||||||
|
{
|
||||||
|
foreach ($offer as $key => $value) {
|
||||||
|
if (!array_key_exists($key, $this->params)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->writer->startElement('param');
|
||||||
|
$this->writer->writeAttribute('code', $key);
|
||||||
|
$this->writer->writeAttribute('name', $this->params[$key]);
|
||||||
|
$this->writer->text($value);
|
||||||
|
$this->writer->endElement();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setOffersCombinations($offer)
|
||||||
|
{
|
||||||
|
if (!array_key_exists('combination', $offer)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($offer['combination'] as $id => $combination) {
|
||||||
|
if (
|
||||||
|
!array_key_exists('group_name', $combination)
|
||||||
|
|| !array_key_exists('attribute_name', $combination)
|
||||||
|
|| empty($combination['group_name'])
|
||||||
|
|| empty($combination['attribute_name'])
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->writer->startElement('param');
|
||||||
|
$this->writer->writeAttribute('code', $id);
|
||||||
|
$this->writer->writeAttribute('name', $combination['group_name']);
|
||||||
|
$this->writer->text($combination['attribute_name']);
|
||||||
|
$this->writer->endElement();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function writeEnd()
|
||||||
|
{
|
||||||
|
$this->writer->endElement(); // end </yml_catalog>
|
||||||
|
$this->writer->endElement(); // end </shop>
|
||||||
|
$this->writer->endDocument();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function formatXml()
|
||||||
|
{
|
||||||
|
$dom = dom_import_simplexml(simplexml_load_file($this->tmpFile))->ownerDocument;
|
||||||
|
$dom->formatOutput = true;
|
||||||
|
$formatted = $dom->saveXML();
|
||||||
|
|
||||||
|
unset($dom, $this->writer);
|
||||||
|
|
||||||
|
file_put_contents($this->tmpFile, $formatted);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user