opencart-module/admin/model/extension/retailcrm/icml.php

303 lines
11 KiB
PHP
Raw Normal View History

2015-06-26 18:07:36 +03:00
<?php
class ModelExtensionRetailcrmIcml extends Model
{
2015-06-26 18:07:36 +03:00
protected $shop;
protected $file;
protected $properties;
protected $params;
protected $dd;
protected $eCategories;
protected $eOffers;
2016-07-25 17:21:15 +03:00
private $options;
private $optionValues;
/**
* Constructor
*
* @param Registry $registry
*/
public function __construct($registry)
{
parent::__construct($registry);
$this->load->library('retailcrm/retailcrm');
}
2015-06-26 18:07:36 +03:00
public function generateICML()
{
$this->load->language('extension/module/retailcrm');
2015-06-26 18:07:36 +03:00
$this->load->model('catalog/category');
$this->load->model('catalog/product');
2016-07-25 17:21:15 +03:00
$this->load->model('catalog/option');
2015-06-26 18:07:36 +03:00
$this->load->model('catalog/manufacturer');
$string = '<?xml version="1.0" encoding="UTF-8"?>
<yml_catalog date="'.date('Y-m-d H:i:s').'">
<shop>
<name>'.$this->config->get('config_name').'</name>
<categories/>
<offers/>
</shop>
</yml_catalog>
';
$xml = new SimpleXMLElement(
$string,
LIBXML_NOENT |LIBXML_NOCDATA | LIBXML_COMPACT | LIBXML_PARSEHUGE
);
$this->dd = new DOMDocument();
$this->dd->preserveWhiteSpace = false;
$this->dd->formatOutput = true;
$this->dd->loadXML($xml->asXML());
$this->eCategories = $this->dd
->getElementsByTagName('categories')->item(0);
$this->eOffers = $this->dd
->getElementsByTagName('offers')->item(0);
$this->addCategories();
$this->addOffers();
2015-06-26 18:07:36 +03:00
$this->dd->saveXML();
2016-02-09 16:21:45 +03:00
$downloadPath = DIR_SYSTEM . '/../';
2015-06-26 18:07:36 +03:00
if (!file_exists($downloadPath)) {
mkdir($downloadPath, 0755);
}
$this->dd->save($downloadPath . 'retailcrm.xml');
}
/**
*
*/
2015-06-26 18:07:36 +03:00
private function addCategories()
{
$categories = $this->model_catalog_category->getCategories(array());
foreach($categories as $category) {
2016-04-06 12:34:40 +03:00
$category = $this->model_catalog_category->getCategory($category['category_id']);
2015-06-26 18:07:36 +03:00
$e = $this->eCategories->appendChild(
$this->dd->createElement(
'category', $category['name']
)
);
2016-01-29 17:59:53 +03:00
$e->setAttribute('id', $category['category_id']);
2015-06-26 18:07:36 +03:00
if ($category['parent_id'] > 0) {
$e->setAttribute('parentId', $category['parent_id']);
}
}
}
private function addOffers()
{
$offerManufacturers = array();
$manufacturers = $this->model_catalog_manufacturer
->getManufacturers(array());
foreach ($manufacturers as $manufacturer) {
$offerManufacturers[
$manufacturer['manufacturer_id']
] = $manufacturer['name'];
}
$products = $this->model_catalog_product->getProducts(array());
2016-07-25 17:21:15 +03:00
foreach ($products as $product) {
$offers = $this->retailcrm->getOffers($product);
2016-01-29 17:59:53 +03:00
foreach ($offers as $optionsString => $optionsValues) {
2016-07-25 17:21:15 +03:00
$optionsString = explode('_', $optionsString);
$options = array();
2016-07-25 17:21:15 +03:00
foreach($optionsString as $optionString) {
$option = explode('-', $optionString);
$optionIds = explode(':', $option[0]);
if ($optionString != '0:0-0') {
2016-07-25 17:21:15 +03:00
$optionData = $this->getOptionData($optionIds[1], $option[1]);
$options[$optionIds[0]] = array(
'name' => $optionData['optionName'],
'value' => $optionData['optionValue'],
'value_id' => $option[1]
);
}
}
2016-07-25 17:21:15 +03:00
ksort($options);
2016-07-25 17:21:15 +03:00
$offerId = array();
2016-07-25 17:21:15 +03:00
foreach($options as $optionKey => $optionData) {
$offerId[] = $optionKey.'-'.$optionData['value_id'];
}
2016-07-25 17:21:15 +03:00
$offerId = implode('_', $offerId);
$e = $this->eOffers->appendChild($this->dd->createElement('offer'));
if (!empty($offerId)) {
$e->setAttribute('id', $product['product_id'] . '#' . $offerId);
2017-06-02 23:36:34 +03:00
$e->setAttribute('productId', $product['product_id']);
$e->setAttribute('quantity', $optionsValues['qty']);
} else {
2016-07-25 17:21:15 +03:00
$e->setAttribute('id', $product['product_id']);
2017-06-02 23:36:34 +03:00
$e->setAttribute('productId', $product['product_id']);
$e->setAttribute('quantity', $product['quantity']);
}
2016-07-25 17:21:15 +03:00
/**
* Offer activity
*/
$activity = $product['status'] == 1 ? 'Y' : 'N';
$e->appendChild(
$this->dd->createElement('productActivity')
)->appendChild(
$this->dd->createTextNode($activity)
);
/**
* Offer categories
*/
$categories = $this->model_catalog_product
->getProductCategories($product['product_id']);
if (!empty($categories)) {
foreach ($categories as $category) {
$e->appendChild($this->dd->createElement('categoryId'))
->appendChild(
$this->dd->createTextNode($category)
);
}
}
/**
* Name & price
*/
$e->appendChild($this->dd->createElement('productName'))
->appendChild($this->dd->createTextNode($product['name']));
if(!empty($options)) {
$optionsString = array();
foreach($options as $option) {
$optionsString[] = $option['name'].': '.$option['value'];
}
$optionsString = ' ('.implode(', ', $optionsString).')';
$e->appendChild($this->dd->createElement('name'))
->appendChild($this->dd->createTextNode($product['name'].$optionsString));
} else {
$e->appendChild($this->dd->createElement('name'))
->appendChild($this->dd->createTextNode($product['name']));
}
$e->appendChild($this->dd->createElement('price'))
2017-06-02 23:36:34 +03:00
->appendChild($this->dd->createTextNode($product['price'] + $optionsValues['price']));
2016-07-25 17:21:15 +03:00
/**
* Vendor
*/
if ($product['manufacturer_id'] != 0) {
$e->appendChild($this->dd->createElement('vendor'))
2015-06-26 18:07:36 +03:00
->appendChild(
2016-07-25 17:21:15 +03:00
$this->dd->createTextNode(
$offerManufacturers[$product['manufacturer_id']]
)
2015-06-26 18:07:36 +03:00
);
}
2016-07-25 17:21:15 +03:00
/**
* Image
*/
if ($product['image']) {
$image = $this->generateImage($product['image']);
$e->appendChild($this->dd->createElement('picture'))
->appendChild($this->dd->createTextNode($image));
}
/**
* Url
*/
$this->url = new Url(
HTTP_CATALOG,
$this->config->get('config_secure')
? HTTP_CATALOG
: HTTPS_CATALOG
);
$e->appendChild($this->dd->createElement('url'))
2015-06-26 18:07:36 +03:00
->appendChild(
$this->dd->createTextNode(
2016-07-25 17:21:15 +03:00
$this->url->link(
'product/product&product_id=' . $product['product_id']
)
2015-06-26 18:07:36 +03:00
)
);
2016-07-25 17:21:15 +03:00
// Options
if(!empty($options)) {
foreach($options as $optionKey => $optionData) {
$param = $this->dd->createElement('param');
$param->setAttribute('code', $optionKey);
$param->setAttribute('name', $optionData['name']);
$param->appendChild($this->dd->createTextNode($optionData['value']));
$e->appendChild($param);
}
}
if ($product['sku']) {
$sku = $this->dd->createElement('param');
$sku->setAttribute('code', 'article');
$sku->setAttribute('name', $this->language->get('article'));
$sku->appendChild($this->dd->createTextNode($product['sku']));
$e->appendChild($sku);
}
if ($product['weight'] != '') {
$weight = $this->dd->createElement('param');
$weight->setAttribute('code', 'weight');
$weight->setAttribute('name', $this->language->get('weight'));
$weightValue = (isset($offer['weight_class']))
? round($product['weight'], 3) . ' ' . $product['weight_class']
: round($product['weight'], 3)
;
$weight->appendChild($this->dd->createTextNode($weightValue));
$e->appendChild($weight);
}
2015-06-26 18:07:36 +03:00
}
}
}
/**
* @param $image
* @return mixed
*/
2015-06-26 18:07:36 +03:00
private function generateImage($image)
{
$this->load->model('tool/image');
$currentTheme = $this->config->get('config_theme');
$width = $this->config->get($currentTheme . '_image_related_width') ? $this->config->get($currentTheme . '_image_related_width') : 200;
$height = $this->config->get($currentTheme . '_image_related_height') ? $this->config->get($currentTheme . '_image_related_height') : 200;
2015-06-26 18:07:36 +03:00
return $this->model_tool_image->resize(
$image,
$width,
$height
);
2015-06-26 18:07:36 +03:00
}
2016-07-25 17:21:15 +03:00
private function getOptionData($optionId, $optionValueId) {
if (!empty($this->options[$optionId])) {
2016-07-25 17:21:15 +03:00
$option = $this->options[$optionId];
} else {
$option = $this->model_catalog_option->getOption($optionId);
$this->options[$optionId] = $option;
}
if (!empty($this->optionValues[$optionValueId])) {
2016-07-25 17:21:15 +03:00
$optionValue = $this->optionValues[$optionValueId];
} else {
$optionValue = $this->model_catalog_option->getOptionValue($optionValueId);
$this->optionValues[$optionValueId] = $optionValue;
}
2016-07-25 17:21:15 +03:00
return array(
'optionName' => $option['name'],
'optionValue' => $optionValue['name']
);
}
2015-06-26 18:07:36 +03:00
}