Skip to content
Open

Tbd #107

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions Model/Banner.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

namespace Magestore\Bannerslider\Model;

use Magento\Framework\Api\AttributeValueFactory;

/**
* Banner Model
* @category Magestore
Expand Down Expand Up @@ -91,6 +93,11 @@ class Banner extends \Magento\Framework\Model\AbstractModel
*/
protected $_monolog;

/**
* @var AttributeValueFactory
*/
protected $customAttributeFactory;

/**
* [__construct description].
*
Expand All @@ -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,
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}
}
14 changes: 12 additions & 2 deletions Model/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

namespace Magestore\Bannerslider\Model;

use Magestore\Bannerslider\Model\ResourceModel\Value\CollectionFactory as ValueCollectionFactory;

/**
* Value Model
* @category Magestore
Expand All @@ -31,6 +33,11 @@
*/
class Value extends \Magento\Framework\Model\AbstractModel
{
/**
* @var ValueCollectionFactory
*/
protected $valueCollectionFactory;

/**
* constructor.
*
Expand All @@ -43,14 +50,17 @@ 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,
$registry,
$resource,
$resourceCollection
);
$this->valueCollectionFactory = $valueCollectionFactory;
}

/**
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<argument name="eventPrefix" xsi:type="string">banner_grid_collection</argument>
<argument name="eventObject" xsi:type="string">banner_collection</argument>
<argument name="resourceModel" xsi:type="string">Magestore\Bannerslider\Model\ResourceModel\Banner</argument>
<argument name="model" xsi:type="string">Magestore\Bannerslider\Model\Banner</argument>
</arguments>
</type>
<type name="Magestore\Bannerslider\Model\ResourceModel\Slider\Grid\Collection">
Expand Down
14 changes: 7 additions & 7 deletions view/adminhtml/layout/bannerslideradmin_banner_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
<argument name="pageTitle" xsi:type="string">Banner Manager</argument>
</action>
</referenceBlock>
<referenceContainer name="page.main.actions">
<block class="Magento\Backend\Block\Store\Switcher" name="store_switcher">
<action method="setUseConfirm">
<argument name="params" xsi:type="string">0</argument>
</action>
</block>
</referenceContainer>
<!--<referenceContainer name="page.main.actions">-->
<!--<block class="Magento\Backend\Block\Store\Switcher" name="store_switcher">-->
<!--<action method="setUseConfirm">-->
<!--<argument name="params" xsi:type="string">0</argument>-->
<!--</action>-->
<!--</block>-->
<!--</referenceContainer>-->
<!--<referenceContainer name="content">-->
<!--<block class="Magestore\Bannerslider\Block\Adminhtml\Banner" name="admin.block.banner.grid.container"/>-->
<!--</referenceContainer>-->
Expand Down