Skip to content

Commit e3e3410

Browse files
authored
Merge pull request #37 from landofcoder/develop
Develop
2 parents 227a253 + fa110c0 commit e3e3410

File tree

11 files changed

+877
-47
lines changed

11 files changed

+877
-47
lines changed

Api/Data/TagProductLinkInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
*/
1515
interface TagProductLinkInterface extends ExtensibleDataInterface
1616
{
17+
const KEY_SKU = 'sku';
18+
const KEY_POSITION = 'position';
19+
const KEY_TAG_ID = 'tag_id';
1720
/**
1821
* @return string|null
1922
*/

Controller/Adminhtml/Tag/Save.php

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct(
3434
?: \Magento\Framework\App\ObjectManager::getInstance()->get(TagRepositoryInterface::class);
3535
parent::__construct($context, $date);
3636
}
37+
3738
public function execute()
3839
{
3940
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
@@ -53,41 +54,63 @@ public function execute()
5354
}
5455
/** @var \Lof\ProductTags\Model\Tag $model */
5556
$model = $this->TagFactory->create();
56-
$id = $this->getRequest()->getParam('tag_id');
57-
if ($id) {
58-
try {
59-
$model = $model->load($id);
57+
$id = $data['tag_id'];
58+
$identifier = $this->getRequest()->getParam('identifier');
59+
$store_id = $this->getRequest()->getParam('store_id');
60+
$status = $this->getRequest()->getParam('status');
61+
$is_exists_identifier = $this->validateTagIdentifier($id, $identifier, $store_id, $status);
62+
if($is_exists_identifier){
63+
$this->messageManager->addErrorMessage(__('The identifier already exists'));
64+
}
65+
else{
66+
if ($id) {
67+
try {
68+
$model = $model->load($id);
69+
} catch (LocalizedException $e) {
70+
$this->messageManager->addErrorMessage(__('This tag no longer exists.'));
71+
return $resultRedirect->setPath('*/*/');
72+
}
73+
}
74+
$model->setData($data);
75+
if (isset($data['tag_products'])
76+
&& is_string($data['tag_products'])) {
77+
$products = json_decode($data['tag_products'], true);
78+
$model->setPostedProducts($products);
79+
}
80+
$this->_eventManager->dispatch(
81+
'lof_producttags_prepare_save',
82+
['tag' => $model, 'request' => $this->getRequest()]
83+
);
84+
$products = $model->getPostedProducts();
85+
try{
86+
$model->save($model);
87+
$this->messageManager->addSuccessMessage(__('You saved the tag.'));
88+
$this->dataPersistor->clear('lof_productags_tag');
89+
return $this->processBlockReturn($model, $data, $resultRedirect);
6090
} catch (LocalizedException $e) {
61-
$this->messageManager->addErrorMessage(__('This tag no longer exists.'));
62-
return $resultRedirect->setPath('*/*/');
91+
$this->messageManager->addErrorMessage($e->getMessage());
92+
} catch (\Exception $e) {
93+
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the tag.'));
6394
}
95+
$this->dataPersistor->set('lof_productags_tag', $data);
96+
return $resultRedirect->setPath('*/*/edit', ['tag_id' => $id]);
6497
}
65-
$model->setData($data);
66-
if (isset($data['tag_products'])
67-
&& is_string($data['tag_products'])) {
68-
$products = json_decode($data['tag_products'], true);
69-
$model->setPostedProducts($products);
70-
}
71-
$this->_eventManager->dispatch(
72-
'lof_producttags_prepare_save',
73-
['tag' => $model, 'request' => $this->getRequest()]
74-
);
75-
$products = $model->getPostedProducts();
76-
try{
77-
$model->save($model);
78-
$this->messageManager->addSuccessMessage(__('You saved the tag.'));
79-
$this->dataPersistor->clear('lof_productags_tag');
80-
return $this->processBlockReturn($model, $data, $resultRedirect);
81-
} catch (LocalizedException $e) {
82-
$this->messageManager->addErrorMessage($e->getMessage());
83-
} catch (\Exception $e) {
84-
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the tag.'));
85-
}
86-
$this->dataPersistor->set('lof_productags_tag', $data);
8798
return $resultRedirect->setPath('*/*/edit', ['tag_id' => $id]);
8899
}
89100
return $resultRedirect->setPath('*/*/');
90101
}
102+
103+
protected function validateTagIdentifier($tag_id, $identifier, $store_id, $status){
104+
$model = $this->TagFactory->create();
105+
$checked_tag_id = $model->checkIdentifier($identifier, $store_id, true);
106+
if($checked_tag_id){
107+
if(!$tag_id || ($tag_id && ($checked_tag_id != $tag_id))){
108+
return true;
109+
}
110+
}
111+
return false;
112+
}
113+
91114
private function processBlockReturn($model, $data, $resultRedirect)
92115
{
93116
$redirect = $data['back'] ?? 'close';

Model/Data/TagProductLink.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ class TagProductLink extends \Magento\Framework\Api\AbstractExtensibleObject imp
1515
/**#@+
1616
* Constant for confirmation status
1717
*/
18-
const KEY_SKU = 'sku';
19-
const KEY_POSITION = 'position';
20-
const KEY_TAG_ID = 'tag_id';
18+
2119
/**#@-*/
2220

2321
/**
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Lof\ProductTags\Model\ResourceModel;
7+
8+
use Magento\Store\Model\Store;
9+
10+
/**
11+
* Abstract collection of CMS pages and blocks
12+
*/
13+
abstract class AbstractCollection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
14+
{
15+
/**
16+
* Store manager
17+
*
18+
* @var \Magento\Store\Model\StoreManagerInterface
19+
*/
20+
protected $storeManager;
21+
22+
/**
23+
* @var \Magento\Framework\EntityManager\MetadataPool
24+
*/
25+
protected $metadataPool;
26+
27+
/**
28+
* @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
29+
* @param \Psr\Log\LoggerInterface $logger
30+
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
31+
* @param \Magento\Framework\Event\ManagerInterface $eventManager
32+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
33+
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
34+
* @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
35+
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
36+
*/
37+
public function __construct(
38+
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
39+
\Psr\Log\LoggerInterface $logger,
40+
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
41+
\Magento\Framework\Event\ManagerInterface $eventManager,
42+
\Magento\Store\Model\StoreManagerInterface $storeManager,
43+
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
44+
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
45+
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
46+
) {
47+
$this->storeManager = $storeManager;
48+
$this->metadataPool = $metadataPool;
49+
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
50+
}
51+
52+
/**
53+
* Perform operations after collection load
54+
*
55+
* @param string $tableName
56+
* @param string|null $linkField
57+
* @return void
58+
*/
59+
protected function performAfterLoad($tableName, $linkField)
60+
{
61+
$linkedIds = $this->getColumnValues($linkField);
62+
if (count($linkedIds)) {
63+
$connection = $this->getConnection();
64+
$select = $connection->select()->from(['product_tag_store' => $this->getTable($tableName)])
65+
->where('product_tag_store.' . $linkField . ' IN (?)', $linkedIds);
66+
67+
$result = $connection->fetchAll($select);
68+
if ($result) {
69+
$storesData = [];
70+
foreach ($result as $storeData) {
71+
$storesData[$storeData[$linkField]][] = $storeData['store_id'];
72+
}
73+
74+
foreach ($this as $item) {
75+
$linkedId = $item->getData($linkField);
76+
if (!isset($storesData[$linkedId])) {
77+
continue;
78+
}
79+
$storeIdKey = array_search(Store::DEFAULT_STORE_ID, $storesData[$linkedId], true);
80+
if ($storeIdKey !== false) {
81+
$stores = $this->storeManager->getStores(false, true);
82+
$storeId = current($stores)->getId();
83+
$storeCode = key($stores);
84+
} else {
85+
$storeId = current($storesData[$linkedId]);
86+
$storeCode = $this->storeManager->getStore($storeId)->getCode();
87+
}
88+
$item->setData('_first_store_id', $storeId);
89+
$item->setData('store_code', $storeCode);
90+
$item->setData('store_id', $storesData[$linkedId]);
91+
}
92+
}
93+
}
94+
}
95+
96+
/**
97+
* Add field filter to collection
98+
*
99+
* @param array|string $field
100+
* @param string|int|array|null $condition
101+
* @return $this
102+
*/
103+
public function addFieldToFilter($field, $condition = null)
104+
{
105+
if ($field === 'store_id') {
106+
return $this->addStoreFilter($condition, false);
107+
}
108+
109+
return parent::addFieldToFilter($field, $condition);
110+
}
111+
112+
/**
113+
* Add filter by store
114+
*
115+
* @param int|array|Store $store
116+
* @param bool $withAdmin
117+
* @return $this
118+
*/
119+
abstract public function addStoreFilter($store, $withAdmin = true);
120+
121+
/**
122+
* Perform adding filter by store
123+
*
124+
* @param int|array|Store $store
125+
* @param bool $withAdmin
126+
* @return void
127+
*/
128+
protected function performAddStoreFilter($store, $withAdmin = true)
129+
{
130+
if ($store instanceof Store) {
131+
$store = [$store->getId()];
132+
}
133+
134+
if (!is_array($store)) {
135+
$store = [$store];
136+
}
137+
138+
if ($withAdmin) {
139+
$store[] = Store::DEFAULT_STORE_ID;
140+
}
141+
142+
$this->addFilter('store', ['in' => $store], 'public');
143+
}
144+
145+
/**
146+
* Join store relation table if there is store filter
147+
*
148+
* @param string $tableName
149+
* @param string|null $linkField
150+
* @return void
151+
*/
152+
protected function joinStoreRelationTable($tableName, $linkField)
153+
{
154+
if ($this->getFilter('store')) {
155+
$this->getSelect()->join(
156+
['store_table' => $this->getTable($tableName)],
157+
'main_table.' . $linkField . ' = store_table.' . $linkField,
158+
[]
159+
)->group(
160+
'main_table.' . $linkField
161+
);
162+
}
163+
parent::_renderFiltersBefore();
164+
}
165+
166+
/**
167+
* Get SQL for get record count
168+
*
169+
* Extra GROUP BY strip added.
170+
*
171+
* @return \Magento\Framework\DB\Select
172+
*/
173+
public function getSelectCountSql()
174+
{
175+
$countSelect = parent::getSelectCountSql();
176+
$countSelect->reset(\Magento\Framework\DB\Select::GROUP);
177+
178+
return $countSelect;
179+
}
180+
181+
/**
182+
* Returns pairs identifier - title for unique identifiers
183+
* and pairs identifier|entity_id - title for non-unique after first
184+
*
185+
* @return array
186+
*/
187+
public function toOptionIdArray()
188+
{
189+
$res = [];
190+
$existingIdentifiers = [];
191+
foreach ($this as $item) {
192+
$identifier = $item->getData('identifier');
193+
194+
$data['value'] = $identifier;
195+
$data['label'] = $item->getData('tag_title');
196+
197+
if (in_array($identifier, $existingIdentifiers)) {
198+
$data['value'] .= '|' . $item->getData($this->getIdFieldName());
199+
} else {
200+
$existingIdentifiers[] = $identifier;
201+
}
202+
203+
$res[] = $data;
204+
}
205+
206+
return $res;
207+
}
208+
}

0 commit comments

Comments
 (0)