diff --git a/Model/Banner.php b/Model/Banner.php index 01b3506..7902567 100644 --- a/Model/Banner.php +++ b/Model/Banner.php @@ -22,6 +22,8 @@ namespace Magestore\Bannerslider\Model; +use Magento\Framework\Api\AttributeValueFactory; + /** * Banner Model * @category Magestore @@ -91,6 +93,11 @@ class Banner extends \Magento\Framework\Model\AbstractModel */ protected $_monolog; + /** + * @var AttributeValueFactory + */ + protected $customAttributeFactory; + /** * [__construct description]. * @@ -113,8 +120,10 @@ public function __construct( \Magestore\Bannerslider\Model\ResourceModel\Slider\CollectionFactory $sliderCollectionFactory, \Magestore\Bannerslider\Model\ResourceModel\Value\CollectionFactory $valueCollectionFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Framework\Logger\Monolog $monolog + \Magento\Framework\Logger\Monolog $monolog, + AttributeValueFactory $customAttributeFactory ) { + $this->customAttributeFactory = $customAttributeFactory; parent::__construct( $context, $registry, @@ -268,16 +277,26 @@ public function afterSave() { if ($storeViewId = $this->getStoreViewId()) { $storeAttributes = $this->getStoreAttributes(); - $collectionBanner = $this->_valueCollectionFactory->create(); $attributeValue = $this->_valueFactory->create() - ->loadAttributeValue($this->getId(), $storeViewId, $storeAttributes, $collectionBanner); - foreach ($attributeValue as $model) { - if ($this->getData($model->getData('attribute_code') . '_in_store')) { + ->loadAttributeValue($this->getId(), $storeViewId, $storeAttributes); + + + foreach ($storeAttributes as $attributeCode) { + + $model = $attributeValue->getItemByColumnValue('attribute_code',$attributeCode); + if(!$model){ + $model=$this->_valueFactory->create(); + $model->setData('banner_id', $this->getId()) + ->setData('store_id', $storeViewId) + ->setData('attribute_code', $attributeCode); + } + + if ($this->getData($attributeCode . '_in_store')) { try { - if ($model->getData('attribute_code') == 'image' && $this->getData('delete_image')) { + if ($attributeCode == 'image' && $this->getData('delete_image')) { $model->delete(); } else { - $model->setValue($this->getData($model->getData('attribute_code') . '_value'))->save(); + $model->setValue($this->getData($attributeCode . '_value'))->save(); } } catch (\Exception $e) { $this->_monolog->addError($e->getMessage()); @@ -355,4 +374,25 @@ public function getTargetValue() return '_blank'; } } + + /** + * Retrieve custom attributes values. + * + * @return \Magento\Framework\Api\AttributeInterface[]|null + */ + public function getCustomAttributes() + { + $customAttributes = []; + + $customAttributeCodes = array_keys($this->_data); + + foreach ($customAttributeCodes as $customAttributeCode) { + $customAttribute = $this->customAttributeFactory->create() + ->setAttributeCode($customAttributeCode) + ->setValue($this->_data[$customAttributeCode]); + $customAttributes[$customAttributeCode] = $customAttribute; + } + + return array_values($customAttributes); + } } diff --git a/Model/Value.php b/Model/Value.php index 16937da..b75603f 100644 --- a/Model/Value.php +++ b/Model/Value.php @@ -22,6 +22,8 @@ namespace Magestore\Bannerslider\Model; +use Magestore\Bannerslider\Model\ResourceModel\Value\CollectionFactory as ValueCollectionFactory; + /** * Value Model * @category Magestore @@ -31,6 +33,11 @@ */ class Value extends \Magento\Framework\Model\AbstractModel { + /** + * @var ValueCollectionFactory + */ + protected $valueCollectionFactory; + /** * constructor. * @@ -43,7 +50,9 @@ public function __construct( \Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magestore\Bannerslider\Model\ResourceModel\Value $resource, - \Magestore\Bannerslider\Model\ResourceModel\Value\Collection $resourceCollection + \Magestore\Bannerslider\Model\ResourceModel\Value\Collection $resourceCollection, + ValueCollectionFactory $valueCollectionFactory + ) { parent::__construct( $context, @@ -51,6 +60,7 @@ public function __construct( $resource, $resourceCollection ); + $this->valueCollectionFactory = $valueCollectionFactory; } /** @@ -84,7 +94,7 @@ public function __construct( public function loadAttributeValue($bannerId, $storeViewId, $attributeCode) { - $attributeValue = $this->getResourceCollection() + $attributeValue = $this->valueCollectionFactory->create() ->addFieldToFilter('banner_id', $bannerId) ->addFieldToFilter('store_id', $storeViewId) ->addFieldToFilter('attribute_code', array('in' => $attributeCode)); diff --git a/composer.json b/composer.json index b0de85e..cb27834 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "magestore/bannerslider-magento2", "description": "N/A", "require": { - "php": "~5.5.0|~5.6.0|~7.0.0" + "php": "^5.5.0 || ^7.0.0" }, "type": "magento2-module", "version": "1.8.0", diff --git a/etc/di.xml b/etc/di.xml index d4051a5..ea13157 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -50,6 +50,7 @@ banner_grid_collection banner_collection Magestore\Bannerslider\Model\ResourceModel\Banner + Magestore\Bannerslider\Model\Banner diff --git a/view/adminhtml/layout/bannerslideradmin_banner_index.xml b/view/adminhtml/layout/bannerslideradmin_banner_index.xml index e3ac17a..b32f477 100644 --- a/view/adminhtml/layout/bannerslideradmin_banner_index.xml +++ b/view/adminhtml/layout/bannerslideradmin_banner_index.xml @@ -36,13 +36,13 @@ Banner Manager - - - - 0 - - - + + + + + + +