2014-12-17 16:24:51 +03:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class MoySkladICMLParser
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Базовый адрес для запросов
|
|
|
|
|
*/
|
2017-04-27 15:40:14 +04:00
|
|
|
|
const BASE_URL = 'https://online.moysklad.ru/api/remap/1.1';
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2015-07-20 17:56:21 +03:00
|
|
|
|
/**
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* развернутые ссылки в запросе
|
|
|
|
|
*/
|
|
|
|
|
const ASSORTIMENT_EXPAND = 'product,productFolder,product.productFolder,supplier,product.supplier';
|
2015-07-20 17:56:21 +03:00
|
|
|
|
|
2014-12-17 16:24:51 +03:00
|
|
|
|
/**
|
|
|
|
|
* Таймаут в секундах
|
|
|
|
|
*/
|
|
|
|
|
const TIMEOUT = 20;
|
|
|
|
|
|
2015-01-15 12:43:36 +03:00
|
|
|
|
/**
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* imgur url
|
|
|
|
|
*/
|
|
|
|
|
const IMGUR_URL = 'https://api.imgur.com/3/image.json';
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
|
|
|
|
/**
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* Шаг для выгрузки элементов в API
|
2014-12-17 16:24:51 +03:00
|
|
|
|
*/
|
2017-04-27 15:40:14 +04:00
|
|
|
|
const STEP = 100;
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
|
|
|
|
/**
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* Лимит выгруженных данных
|
2014-12-17 16:24:51 +03:00
|
|
|
|
*/
|
2017-04-27 15:40:14 +04:00
|
|
|
|
const LIMIT = 100;
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
|
|
|
|
/**
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* Адрес для запроса ассортимента
|
2014-12-17 16:24:51 +03:00
|
|
|
|
*/
|
2017-04-27 15:40:14 +04:00
|
|
|
|
const ASSORT_LIST_URL = '/entity/assortment';
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
|
|
|
|
/**
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* Адрес для запроса ассортимента
|
2014-12-17 16:24:51 +03:00
|
|
|
|
*/
|
2017-04-27 15:40:14 +04:00
|
|
|
|
const FOLDER_LIST_URL = '/entity/productfolder';
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
|
|
|
|
/**
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* @var boolean флаг создания корневой дирректории каталога warehouseRoot
|
2014-12-17 16:24:51 +03:00
|
|
|
|
*/
|
2017-04-27 15:40:14 +04:00
|
|
|
|
protected $noCategory;
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string $login - логин МойСклад
|
|
|
|
|
*/
|
|
|
|
|
protected $login;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string $pass - пароль МойСклад
|
|
|
|
|
*/
|
|
|
|
|
protected $pass;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string $shop - имя магазина
|
|
|
|
|
*/
|
|
|
|
|
protected $shop;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array $options - дополнительные опции
|
|
|
|
|
*/
|
|
|
|
|
protected $options;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $login - логин МойСклад
|
|
|
|
|
* @param $pass - пароль МойСклад
|
|
|
|
|
* @param $shop - имя магазина
|
|
|
|
|
* @param array $options - дополнительные опции
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
$login,
|
|
|
|
|
$pass,
|
|
|
|
|
$shop,
|
|
|
|
|
array $options = array()
|
|
|
|
|
) {
|
|
|
|
|
$this->login = $login;
|
|
|
|
|
$this->pass = $pass;
|
|
|
|
|
$this->shop = $shop;
|
|
|
|
|
$this->options = $options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Генерирует ICML файл
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function generateICML()
|
|
|
|
|
{
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$assortiment = $this->parseAssortiment();
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$categories = $this->parserFolder();
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2015-07-21 10:33:02 +03:00
|
|
|
|
if (isset($this->options['imgur']) && isset($this->options['imgur']['clientId'])) {
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$assortiment = $this->uploadImage($assortiment);
|
2015-07-20 17:56:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$icml = $this->ICMLCreate($categories, $assortiment);
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
|
|
|
|
$icml->asXML($this->getFilePath());
|
2017-04-27 15:40:14 +04:00
|
|
|
|
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 17:56:21 +03:00
|
|
|
|
/**
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* @param string $uri
|
|
|
|
|
* @return JSON
|
2015-07-20 17:56:21 +03:00
|
|
|
|
*/
|
2017-04-27 15:40:14 +04:00
|
|
|
|
protected function requestJson($uri)
|
2015-07-20 17:56:21 +03:00
|
|
|
|
{
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$url = self::BASE_URL . $uri;
|
2015-07-20 17:56:21 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$curlHandler = curl_init();
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_USERPWD, $this->login . ':' . $this->pass);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_URL, $url);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_FAILONERROR, false);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_TIMEOUT, self::TIMEOUT);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, 60);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_HTTPHEADER, array(
|
|
|
|
|
'Content-Type: application/json'
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$result = curl_exec($curlHandler);
|
|
|
|
|
curl_close($curlHandler);
|
2015-07-20 17:56:21 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($result === false) {
|
|
|
|
|
return null;
|
2015-07-20 17:56:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$result = json_decode($result,true);
|
2015-07-20 17:56:21 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
return $result;
|
2015-07-20 17:56:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-17 16:24:51 +03:00
|
|
|
|
/**
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* Получение категорий товаров
|
2014-12-17 16:24:51 +03:00
|
|
|
|
*
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* @return array $categories
|
2014-12-17 16:24:51 +03:00
|
|
|
|
*/
|
2017-04-27 15:40:14 +04:00
|
|
|
|
protected function parserFolder()
|
2014-12-17 16:24:51 +03:00
|
|
|
|
{
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$offset = 0;
|
|
|
|
|
$end = null;
|
|
|
|
|
$ignoreCategories = $this->getIgnoreProductGroupsInfo();
|
|
|
|
|
if ($this->noCategory==true){
|
|
|
|
|
$categories[0] = array(
|
|
|
|
|
'name' => 'warehouseRoot',
|
|
|
|
|
'externalCode' =>'warehouseRoot',
|
|
|
|
|
);
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
2017-04-27 15:40:14 +04:00
|
|
|
|
while (true){
|
|
|
|
|
$response = $this->requestJson(self::FOLDER_LIST_URL.'?expand=productFolder&limit=100&offset='.$offset);
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
foreach ($response['rows'] as $folder){
|
|
|
|
|
if (isset($ignoreCategories['ids']) && is_array($ignoreCategories['ids'])){
|
|
|
|
|
if (in_array($folder['id'],$ignoreCategories['id'])){
|
2015-01-15 12:43:36 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (isset($folder['productFolder']['id'])){
|
|
|
|
|
if (in_array($folder['productFolder']['id'],$ignoreCategories['ids'])){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-01-15 12:43:36 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (isset($ignoreCategories['externalCode']) && is_array($ignoreCategories['externalCode'])){
|
|
|
|
|
if (in_array($folder['externalCode'],$ignoreCategories['externalCode'])){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (isset($folder['productFolder']['externalCode'])){
|
|
|
|
|
if (in_array($folder['productFolder']['externalCode'],$ignoreCategories['externalCode'])){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if($folder['archived'] == false){
|
|
|
|
|
$categories[] =
|
|
|
|
|
array(
|
|
|
|
|
'name' => $folder['name'],
|
|
|
|
|
'externalCode' => $folder['externalCode'],
|
|
|
|
|
'parentId' => isset($folder['productFolder']) ?
|
|
|
|
|
$folder['productFolder']['externalCode'] : '',
|
|
|
|
|
);
|
2015-01-15 12:43:36 +03:00
|
|
|
|
|
|
|
|
|
}
|
2017-04-27 15:40:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_null($end)) {
|
|
|
|
|
$end = $response['meta']['size'] - self::STEP;
|
2015-01-15 12:43:36 +03:00
|
|
|
|
} else {
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$end -= self::STEP;
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
2015-01-15 12:43:36 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($end >= 0) {
|
|
|
|
|
$offset += self::STEP;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
return $categories;
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* Получение ассортимента товаров
|
2014-12-17 16:24:51 +03:00
|
|
|
|
*
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* @return array $products
|
2014-12-17 16:24:51 +03:00
|
|
|
|
*/
|
2017-04-27 15:40:14 +04:00
|
|
|
|
protected function parseAssortiment()
|
|
|
|
|
{
|
|
|
|
|
$product = array();
|
2015-08-25 17:14:35 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$offset = 0;
|
|
|
|
|
$end = null;
|
2015-08-25 17:14:35 +03:00
|
|
|
|
$ignoreNoCategoryOffers = isset($this->options['ignoreNoCategoryOffers']) && $this->options['ignoreNoCategoryOffers'];
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$ignoreCategories = $this->getIgnoreProductGroupsInfo();
|
2015-08-25 17:14:35 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
while (true) {
|
2015-03-05 16:38:03 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$response = $this->requestJson(self::ASSORT_LIST_URL.'?expand='.self::ASSORTIMENT_EXPAND.'&limit='.self::LIMIT.'&offset='.$offset);
|
2015-07-21 10:33:02 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($response && $response['rows']) {
|
|
|
|
|
foreach ($response['rows'] as $assortiment) {
|
|
|
|
|
if (!empty($assortiment['modificationsCount']) ||
|
|
|
|
|
$assortiment['meta']['type'] == 'service') {
|
|
|
|
|
continue;
|
2015-07-21 10:33:02 +03:00
|
|
|
|
}
|
2015-07-20 17:56:21 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($ignoreNoCategoryOffers === true){
|
2015-01-15 12:43:36 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ( !isset($assortiment['productFolder']['externalCode']) &&
|
|
|
|
|
!isset($assortiment['product']['productFolder']['externalCode']) ){
|
2015-01-15 12:43:36 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (isset($ignoreCategories['ids']) && is_array($ignoreCategories['ids'])){
|
|
|
|
|
if (!empty($assortiment['productFolder']['id'])){
|
|
|
|
|
if (in_array($assortiment['productFolder']['id'],$ignoreCategories['ids'])){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!empty($assortiment['product']['productFolder']['id'])){
|
|
|
|
|
if (in_array($assortiment['product']['productFolder']['id'],$ignoreCategories['ids'])){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (isset($ignoreCategories['externalCode']) && is_array($ignoreCategories['externalCode'])){
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (!empty($assortiment['productFolder']['externalCode'])){
|
|
|
|
|
if (in_array($assortiment['productFolder']['externalCode'],$ignoreCategories['externalCode'])){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (!empty($assortiment['product']['productFolder']['externalCode'])){
|
|
|
|
|
if (in_array($assortiment['product']['productFolder']['externalCode'],$ignoreCategories['externalCode'])){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$url = isset($assortiment['product']['image']['meta']['href']) ?
|
|
|
|
|
$assortiment['product']['image']['meta']['href'] : '';
|
|
|
|
|
if (isset($assortiment['product']['image']['meta']['href'])){
|
|
|
|
|
$url = $assortiment['product']['image']['meta']['href'];
|
|
|
|
|
} elseif (isset($assortiment['image']['meta']['href'])) {
|
|
|
|
|
$url = $assortiment['image']['meta']['href'];
|
|
|
|
|
}else {
|
|
|
|
|
$url = '';
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($url !=''){
|
|
|
|
|
$image = $this->requestImage($url);
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$products[$assortiment['id']] = array(
|
|
|
|
|
'id' => $assortiment['id'],
|
|
|
|
|
'exCode' => $assortiment['externalCode'],
|
|
|
|
|
'productId' => isset($assortiment['product']['externalCode']) ?
|
|
|
|
|
$assortiment['product']['externalCode'] : $assortiment['externalCode'],
|
|
|
|
|
'name' => $assortiment['name'],
|
|
|
|
|
'productName'=> isset($assortiment['product']['name'])?
|
|
|
|
|
$assortiment['product']['name'] : $assortiment['name'],
|
|
|
|
|
'price' => isset($assortiment['salePrices'][0]['value']) ?
|
|
|
|
|
((float)$assortiment['salePrices'][0]['value']) / 100 :
|
|
|
|
|
((float)$assortiment['product']['salePrices'][0]['value']) / 100,
|
|
|
|
|
|
|
|
|
|
'purchasePrice' => isset($assortiment['buyPrice']['value']) ?
|
|
|
|
|
((float)$assortiment['buyPrice']['value']) / 100 :
|
|
|
|
|
((float)$assortiment['product']['buyPrice']['value']) / 100,
|
|
|
|
|
|
|
|
|
|
'weight' => isset($assortiment['weight']) ?
|
|
|
|
|
$assortiment['weight'] :
|
|
|
|
|
$assortiment['product']['weight'],
|
|
|
|
|
|
|
|
|
|
'code' => isset($assortiment['code']) ? (string) $assortiment['code'] : '',
|
|
|
|
|
|
|
|
|
|
'xmlId' => !empty($assortiment['product']['externalCode']) ?
|
|
|
|
|
$assortiment['product']['externalCode'] . '#'. $assortiment['externalCode'] :
|
|
|
|
|
$assortiment['externalCode'],
|
|
|
|
|
|
|
|
|
|
'image'=> array(
|
|
|
|
|
'content'=>isset($image) ? $image : '',
|
|
|
|
|
'name' => isset($assortiment['product']['image']['filename']) ? $assortiment['product']['image']['filename'] : '',
|
|
|
|
|
'id'=>$assortiment['id'],
|
|
|
|
|
),
|
|
|
|
|
);
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (isset($assortiment['productFolder']['externalCode'])){
|
|
|
|
|
$products[$assortiment['id']]['categoryId'] = $assortiment['productFolder']['externalCode'];
|
|
|
|
|
} elseif (isset($assortiment['product']['productFolder']['externalCode'])){
|
|
|
|
|
$products[$assortiment['id']]['categoryId'] = $assortiment['product']['productFolder']['externalCode'];
|
|
|
|
|
} else {
|
|
|
|
|
$products[$assortiment['id']]['categoryId'] = '';
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (isset($assortiment['article'])){
|
|
|
|
|
$products[$assortiment['id']]['article'] = (string) $assortiment['article'];
|
|
|
|
|
} elseif (isset($assortiment['product']['article'])){
|
|
|
|
|
$products[$assortiment['id']]['article'] = (string) $assortiment['product']['article'];
|
|
|
|
|
} else {
|
|
|
|
|
$products[$assortiment['id']]['article'] = '';
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (isset($assortiment['product']['supplier']['name'])){
|
|
|
|
|
$products[$assortiment['id']]['vendor'] = $assortiment['product']['supplier']['name'];
|
|
|
|
|
} elseif (isset($assortiment['supplier']['name'])){
|
|
|
|
|
$products[$assortiment['id']]['vendor'] = $assortiment['supplier']['name'];
|
|
|
|
|
} else {
|
|
|
|
|
$products[$assortiment['id']]['vendor'] = '';
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($products[$assortiment['id']]['categoryId'] == null){
|
|
|
|
|
$this->noCategory = true;
|
|
|
|
|
}
|
|
|
|
|
unset($image);
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (is_null($end)) {
|
|
|
|
|
$end = $response['meta']['size'] - self::STEP;
|
2014-12-17 16:24:51 +03:00
|
|
|
|
} else {
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$end -= self::STEP;
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($end >= 0) {
|
|
|
|
|
$offset += self::STEP;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
2017-04-27 15:40:14 +04:00
|
|
|
|
unset($response, $assortiment);
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
return $products;
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Формируем итоговый ICML
|
|
|
|
|
*
|
|
|
|
|
* @param array $categories
|
2017-04-27 15:40:14 +04:00
|
|
|
|
* @param array $products
|
2014-12-17 16:24:51 +03:00
|
|
|
|
* @return SimpleXMLElement
|
|
|
|
|
*/
|
2017-04-27 15:40:14 +04:00
|
|
|
|
protected function ICMLCreate($categories, $products)
|
|
|
|
|
{
|
2014-12-17 16:24:51 +03:00
|
|
|
|
$date = new DateTime();
|
|
|
|
|
$xmlstr = '<yml_catalog date="'.$date->format('Y-m-d H:i:s').'"><shop><name>'.$this->shop.'</name></shop></yml_catalog>';
|
|
|
|
|
$xml = new SimpleXMLElement($xmlstr);
|
|
|
|
|
if (count($categories)) {
|
|
|
|
|
$categoriesXml = $this->icmlAdd($xml->shop, 'categories', '');
|
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
|
$categoryXml = $this->icmlAdd($categoriesXml, 'category', $category['name']);
|
|
|
|
|
$categoryXml->addAttribute('id', $category['externalCode']);
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (!empty($category['parentId'])){
|
|
|
|
|
$categoryXml->addAttribute('parentId',$category['parentId']);
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$offersXml = $this->icmlAdd($xml->shop, 'offers', '');
|
|
|
|
|
foreach ($products as $product) {
|
|
|
|
|
$offerXml = $offersXml->addChild('offer');
|
|
|
|
|
$offerXml->addAttribute('id', $product['id']);
|
|
|
|
|
$offerXml->addAttribute('productId', $product['productId']);
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
$this->icmlAdd($offerXml, 'xmlId', $product['xmlId']);
|
|
|
|
|
$this->icmlAdd($offerXml, 'price', number_format($product['price'], 2, '.', ''));
|
|
|
|
|
$this->icmlAdd($offerXml, 'purchasePrice', number_format($product['purchasePrice'], 2, '.', ''));
|
|
|
|
|
$this->icmlAdd($offerXml, 'name', $product['name']);
|
|
|
|
|
$this->icmlAdd($offerXml, 'productName', $product['productName']);
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($product['categoryId']) {
|
2014-12-17 16:24:51 +03:00
|
|
|
|
$this->icmlAdd($offerXml, 'categoryId', $product['categoryId']);
|
2017-04-27 15:40:14 +04:00
|
|
|
|
}else {
|
|
|
|
|
$this->icmlAdd($offerXml, 'categoryId', 'warehouseRoot');
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($product['article']) {
|
2014-12-17 16:24:51 +03:00
|
|
|
|
$art = $this->icmlAdd($offerXml, 'param', $product['article']);
|
2015-07-21 11:13:59 +03:00
|
|
|
|
$art->addAttribute('code', 'article');
|
|
|
|
|
$art->addAttribute('name', 'Артикул');
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($product['weight']) {
|
2015-07-21 11:13:59 +03:00
|
|
|
|
$wei = $this->icmlAdd($offerXml, 'param', $product['weight']);
|
|
|
|
|
$wei->addAttribute('code', 'weight');
|
|
|
|
|
$wei->addAttribute('name', 'Вес');
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($product['code']) {
|
2015-07-21 11:13:59 +03:00
|
|
|
|
$cod = $this->icmlAdd($offerXml, 'param', $product['code']);
|
|
|
|
|
$cod->addAttribute('code', 'code');
|
|
|
|
|
$cod->addAttribute('name', 'Код');
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if ($product['vendor']) {
|
2014-12-17 16:24:51 +03:00
|
|
|
|
$this->icmlAdd($offerXml, 'vendor', $product['vendor']);
|
|
|
|
|
}
|
2015-03-05 16:50:55 +03:00
|
|
|
|
|
2017-04-27 15:40:14 +04:00
|
|
|
|
if (isset($product['image']['url'])) {
|
|
|
|
|
$this->icmlAdd($offerXml, 'picture', $product['image']['url']);
|
2015-03-05 16:50:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|
|
|
|
|
return $xml;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Добавляем элемент в icml
|
|
|
|
|
*
|
|
|
|
|
* @param SimpleXMLElement $xml
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return SimpleXMLElement
|
|
|
|
|
*/
|
|
|
|
|
protected function icmlAdd(
|
|
|
|
|
SimpleXMLElement $xml,
|
|
|
|
|
$name,
|
|
|
|
|
$value
|
|
|
|
|
) {
|
2017-04-27 15:40:14 +04:00
|
|
|
|
|
|
|
|
|
$elem = $xml->addChild($name, $value);
|
2014-12-17 16:24:51 +03:00
|
|
|
|
|
|
|
|
|
return $elem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Возвращает имя ICML-файла
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getFilePath()
|
|
|
|
|
{
|
|
|
|
|
$path = isset($this->options['directory']) && $this->options['directory'] ?
|
|
|
|
|
$this->options['directory'] : __DIR__;
|
|
|
|
|
|
|
|
|
|
if (substr($path, -1) === '/') {
|
|
|
|
|
$path = substr($path, 0, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$file = isset($this->options['file']) && $this->options['file'] ?
|
|
|
|
|
$this->options['file'] : $this->shop.'.catalog.xml';
|
|
|
|
|
|
|
|
|
|
return $path.'/'.$file;
|
|
|
|
|
}
|
2017-04-27 15:40:14 +04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получаем данные для игнорирования товарных групп
|
|
|
|
|
*/
|
|
|
|
|
protected function getIgnoreProductGroupsInfo()
|
|
|
|
|
{
|
|
|
|
|
//var_dump($this->options['ignoreCategories']);
|
|
|
|
|
if (!isset($this->options['ignoreCategories']) || !is_array($this->options['ignoreCategories'])){
|
|
|
|
|
$info = array();
|
|
|
|
|
} else {
|
|
|
|
|
$info = $this->options['ignoreCategories'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($info['id']) || !is_array($info['id'])){
|
|
|
|
|
$info['id'] = array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($info['externalCode']) || !is_array($info['externalCode'])){
|
|
|
|
|
$info['externalCode'] = array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получаем изображения для товаров
|
|
|
|
|
*
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @return jpg
|
|
|
|
|
*/
|
|
|
|
|
protected function requestImage($url)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$curlHandler = curl_init();
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_USERPWD, $this->login . ':' . $this->pass);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_URL, $url);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_FAILONERROR, false);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_TIMEOUT, self::TIMEOUT);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, 60);
|
|
|
|
|
curl_setopt($curlHandler, CURLOPT_HTTPHEADER, array(
|
|
|
|
|
'Content-Type: application/json'
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$result = curl_exec($curlHandler);
|
|
|
|
|
curl_close($curlHandler);
|
|
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Загружаем изображения
|
|
|
|
|
*
|
|
|
|
|
* @param array $products
|
|
|
|
|
* @return array $products
|
|
|
|
|
*/
|
|
|
|
|
protected function uploadImage($products)
|
|
|
|
|
{
|
|
|
|
|
if (file_exists(__DIR__ . '/images') === false) {
|
|
|
|
|
@mkdir(__DIR__ . '/images');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$uploaded = array();
|
|
|
|
|
if (file_exists(__DIR__ . "/images/{$this->shop}.json")) {
|
|
|
|
|
$uploaded = json_decode(file_get_contents(__DIR__ . "/images/{$this->shop}.json"), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($products as $id => $product) {
|
|
|
|
|
if (isset($product['image'])) {
|
|
|
|
|
if (isset($uploaded) && isset($uploaded[$product['image']['id']])) {
|
|
|
|
|
$products[$id]['image']['url'] = $uploaded[$product['image']['id']];
|
|
|
|
|
unset($product['image']);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
|
'image' => $product['image']['content'],
|
|
|
|
|
'name' => $product['image']['name']
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$ch = curl_init();
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL, self::IMGUR_URL);
|
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Client-ID {$this->options['imgur']['clientId']}"));
|
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
|
|
$result = curl_exec($ch);
|
|
|
|
|
curl_close($ch);
|
|
|
|
|
$result = @json_decode($result, true);
|
|
|
|
|
|
|
|
|
|
if (isset($result['success']) && $result['success'] == true) {
|
|
|
|
|
$products[$id]['image']['url'] = $result['data']['link'];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
unset($product['image']['content']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count($uploaded) > 0) {
|
|
|
|
|
file_put_contents(__DIR__ . "/images/{$this->shop}.json", json_encode($uploaded));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $products;
|
|
|
|
|
}
|
2014-12-17 16:24:51 +03:00
|
|
|
|
}
|