From f25a3937dc858715a8b6ebc7240f38194eb647a4 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Thu, 12 Nov 2015 07:56:31 -0500 Subject: [PATCH 1/4] fix motice generate category --- retailcrm/src/Helpers/IcmlHelper.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/retailcrm/src/Helpers/IcmlHelper.php b/retailcrm/src/Helpers/IcmlHelper.php index 822b0c4..eff3956 100644 --- a/retailcrm/src/Helpers/IcmlHelper.php +++ b/retailcrm/src/Helpers/IcmlHelper.php @@ -74,6 +74,10 @@ class IcmlHelper private function addCategories($categories) { foreach($categories as $category) { + if (!array_key_exists('name', $category) || !array_key_exists('id', $category)) { + continue; + } + $e = $this->categories->appendChild( $this->document->createElement( 'category', $category['name'] @@ -82,7 +86,7 @@ class IcmlHelper $e->setAttribute('id', $category['id']); - if ($category['parentId'] > 0) { + if (array_key_exists('parentId', $category) && $category['parentId'] > 0) { $e->setAttribute('parentId', $category['parentId']); } } From 318dbc9682959f7fb31bd89ac23e4fc83b3696b0 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Thu, 12 Nov 2015 08:09:48 -0500 Subject: [PATCH 2/4] fix notice && category generate offers --- retailcrm/src/Helpers/IcmlHelper.php | 33 ++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/retailcrm/src/Helpers/IcmlHelper.php b/retailcrm/src/Helpers/IcmlHelper.php index eff3956..d14b320 100644 --- a/retailcrm/src/Helpers/IcmlHelper.php +++ b/retailcrm/src/Helpers/IcmlHelper.php @@ -96,11 +96,19 @@ class IcmlHelper { foreach ($offers as $offer) { + if (!array_key_exists('id', $offer)) { + continue; + } + $e = $this->offers->appendChild( $this->document->createElement('offer') ); $e->setAttribute('id', $offer['id']); + + if (!array_key_exists('productId', $offer) || empty($offer['productId'])) { + $offer['productId'] = $offer['id']; + } $e->setAttribute('productId', $offer['productId']); if (!empty($offer['quantity'])) { @@ -109,16 +117,30 @@ class IcmlHelper $e->setAttribute('quantity', 0); } - foreach ($offer['categoryId'] as $categoryId) { + if (is_array($offer['categoryId'])) { + foreach ($offer['categoryId'] as $categoryId) { + $e->appendChild( + $this->document->createElement('categoryId', $categoryId) + ); + } + } else { $e->appendChild( - $this->document->createElement('categoryId', $categoryId) + $this->document->createElement('categoryId', $offer['categoryId']) ); } + if (!array_key_exists('name', $offer) || empty($offer['name'])) { + $offer['name'] = 'Без названия'; + } + + if (!array_key_exists('productName', $offer) || empty($offer['productName'])) { + $offer['name'] = $offer['name']; + } + $offerKeys = array_keys($offer); foreach ($offerKeys as $key) { - if (in_array($key, $this->properties)) { + if (in_array($key, $this->properties) && !is_null($offer[$key]) && $offer[$key] !== '') { $e->appendChild( $this->document->createElement($key) )->appendChild( @@ -126,7 +148,10 @@ class IcmlHelper ); } - if (in_array($key, array_keys($this->params))) { + if ( + in_array($key, array_keys($this->params)) && + !is_null($offer[$key]) && $offer[$key] !== '' + ) { $param = $this->document->createElement('param'); $param->setAttribute('code', $key); $param->setAttribute('name', $this->params[$key]); From e796ab3ed0a7fe2138fe2638c0f0bccf9c5b53c1 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Thu, 12 Nov 2015 08:14:28 -0500 Subject: [PATCH 3/4] fix name --- retailcrm/src/Helpers/IcmlHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retailcrm/src/Helpers/IcmlHelper.php b/retailcrm/src/Helpers/IcmlHelper.php index d14b320..283ed7b 100644 --- a/retailcrm/src/Helpers/IcmlHelper.php +++ b/retailcrm/src/Helpers/IcmlHelper.php @@ -134,7 +134,7 @@ class IcmlHelper } if (!array_key_exists('productName', $offer) || empty($offer['productName'])) { - $offer['name'] = $offer['name']; + $offer['productName'] = $offer['name']; } $offerKeys = array_keys($offer); From 435081dbb634f721182e0fe1938027f8e6f50bff Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Thu, 12 Nov 2015 09:26:04 -0500 Subject: [PATCH 4/4] refactoring icml --- retailcrm/src/Helpers/IcmlHelper.php | 90 +++++++++++++++------------- 1 file changed, 48 insertions(+), 42 deletions(-) diff --git a/retailcrm/src/Helpers/IcmlHelper.php b/retailcrm/src/Helpers/IcmlHelper.php index 283ed7b..dfe3432 100644 --- a/retailcrm/src/Helpers/IcmlHelper.php +++ b/retailcrm/src/Helpers/IcmlHelper.php @@ -5,19 +5,7 @@ class IcmlHelper protected $shop; protected $file; - protected $properties; - protected $params; - - protected $document; - protected $categories; - protected $offers; - - public function __construct($shop, $file) - { - $this->shop = $shop; - $this->file = $file; - - $this->properties = array( + protected $properties = array( 'name', 'productName', 'price', @@ -27,14 +15,16 @@ class IcmlHelper 'url', 'xmlId', 'productActivity' - ); + ); - $this->params = array( - 'article' => 'Артикул', - 'color' => 'Цвет', - 'weight' => 'Вес', - 'size' => 'Размер', - ); + protected $document; + protected $categories; + protected $offers; + + public function __construct($shop, $file) + { + $this->shop = $shop; + $this->file = $file; } public function generate($categories, $offers) @@ -73,6 +63,8 @@ class IcmlHelper private function addCategories($categories) { + $categories = DataHelper::filterRecursive($categories); + foreach($categories as $category) { if (!array_key_exists('name', $category) || !array_key_exists('id', $category)) { continue; @@ -94,6 +86,8 @@ class IcmlHelper private function addOffers($offers) { + $offers = DataHelper::filterRecursive($offers); + foreach ($offers as $offer) { if (!array_key_exists('id', $offer)) { @@ -137,30 +131,42 @@ class IcmlHelper $offer['productName'] = $offer['name']; } - $offerKeys = array_keys($offer); + unset($offer['id'], $offer['productId'], $offer['categoryId'], $offer['quantity']); + array_walk($offer, array($this, 'setOffersProperties'), $e); - foreach ($offerKeys as $key) { - if (in_array($key, $this->properties) && !is_null($offer[$key]) && $offer[$key] !== '') { - $e->appendChild( - $this->document->createElement($key) - )->appendChild( - $this->document->createTextNode($offer[$key]) - ); - } - - if ( - in_array($key, array_keys($this->params)) && - !is_null($offer[$key]) && $offer[$key] !== '' - ) { - $param = $this->document->createElement('param'); - $param->setAttribute('code', $key); - $param->setAttribute('name', $this->params[$key]); - $param->appendChild( - $this->document->createTextNode($offer[$key]) - ); - $e->appendChild($param); - } + if (array_key_exists('params', $offer) && !empty($offer['params'])) { + array_walk($offer['params'], array($this, 'setOffersParams'), $e); } } } + + private function setOffersProperties($value, $key, &$e) { + if (in_array($key, $this->properties) && $key != 'params') { + $e->appendChild( + $this->document->createElement($key) + )->appendChild( + $this->document->createTextNode($value) + ); + } + } + + private function setOffersParams($value, $key, &$e) { + if ( + array_key_exists('code', $value) && + array_key_exists('name', $value) && + array_key_exists('value', $value) && + !empty($value['code']) && + !empty($value['name']) && + !empty($value['value']) + ) { + $param = $this->document->createElement('param'); + $param->setAttribute('code', $value['code']); + $param->setAttribute('name', $value['name']); + $param->appendChild( + $this->document->createTextNode($value['value']) + ); + + $e->appendChild($param); + } + } }