Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace NicolasBejean\CategoryWidget\Model\ResourceModel;
use \NicolasBejean\CategoryWidget\Api\Data\CategoryWidgetInterface;
use \Magento\Framework\DB\Select;
use \Magento\Framework\EntityManager\EntityManager;
use \Magento\Framework\EntityManager\MetadataPool;
use \Magento\Framework\Exception\LocalizedException;
use \Magento\Framework\Model\AbstractModel;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use \Magento\Framework\Model\ResourceModel\Db\Context;
use \Magento\Store\Model\Store;
use \Magento\Store\Model\StoreManagerInterface;
use \Exception;
use \NicolasBejean\CategoryWidget\Model\CategoryWidget as CategoryWidgetModel;
/**
* Class CategoryWidget
*
* @category PHP
* @package NicolasBejean\CategoryWidget\Model\ResourceModel
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/category-widget/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class CategoryWidget extends AbstractDb
{
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var EntityManager
*/
protected $entityManager;
/**
* @var MetadataPool
*/
protected $metadataPool;
/**
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param EntityManager $entityManager
* @param MetadataPool $metadataPool
* @param string $connectionName
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
EntityManager $entityManager,
MetadataPool $metadataPool,
$connectionName = null
) {
$this->storeManager = $storeManager;
$this->entityManager = $entityManager;
$this->metadataPool = $metadataPool;
parent::__construct($context, $connectionName);
}
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('nicolasbejean_categorywidget', 'entity_id');
}
/**
* @inheritDoc
* @throws Exception
*/
public function getConnection()
{
return $this->metadataPool->getMetadata(CategoryWidgetInterface::class)->getEntityConnection();
}
/**
* Perform operations before object save
*
* @param AbstractModel $object
* @return $this
* @throws LocalizedException
*/
protected function _beforeSave(AbstractModel $object)
{
if (!$this->getIsUniqueCategoryWidgetToStores($object)) {
throw new LocalizedException(
__('A category widget identifier with the same properties already exists in the selected store.')
);
}
return $this;
}
/**
* @param AbstractModel $object
* @param mixed $value
* @param null $field
* @return bool|int|string
* @throws LocalizedException
* @throws Exception
*/
private function getCategoryWidgetId(AbstractModel $object, $value, $field = null)
{
$entityMetadata = $this->metadataPool->getMetadata(CategoryWidgetInterface::class);
if (!is_numeric($value) && $field === null) {
$field = 'identifier';
} elseif (!$field) {
$field = $entityMetadata->getIdentifierField();
}
$entityId = $value;
if ($field != $entityMetadata->getIdentifierField() || $object->getStoreId()) {
$select = $this->_getLoadSelect($field, $value, $object);
$select->reset(Select::COLUMNS)
->columns($this->getMainTable() . '.' . $entityMetadata->getIdentifierField())
->limit(1);
$result = $this->getConnection()->fetchCol($select);
$entityId = count($result) ? $result[0] : false;
}
return $entityId;
}
/**
* Load an object
*
* @param CategoryWidgetModel|AbstractModel $object
* @param mixed $value
* @param string $field field to load by (defaults to model id)
* @return $this
* @throws LocalizedException
*/
public function load(AbstractModel $object, $value, $field = null)
{
$categoryWidgetId = $this->getCategoryWidgetId($object, $value, $field);
if ($categoryWidgetId) {
$this->entityManager->load($object, $categoryWidgetId);
}
return $this;
}
/**
* Retrieve select object for load object data
*
* @param string $field
* @param mixed $value
* @param CategoryWidgetModel|AbstractModel $object
* @return Select
* @throws LocalizedException
* @throws Exception
*/
protected function _getLoadSelect($field, $value, $object)
{
$entityMetadata = $this->metadataPool->getMetadata(CategoryWidgetInterface::class);
$linkField = $entityMetadata->getLinkField();
$select = parent::_getLoadSelect($field, $value, $object);
if ($object->getStoreId()) {
$stores = [(int)$object->getStoreId(), Store::DEFAULT_STORE_ID];
$select->join(
['ncws' => $this->getTable('nicolasbejean_categorywidget_store')],
$this->getMainTable() . '.' . $linkField . ' = ncws.' . $linkField,
->where('ncws.store_id in (?)', $stores)
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
->order('store_id DESC')
->limit(1);
}
return $select;
}
/**
* Check for unique of path of category widgetto selected store(s).
*
* @param AbstractModel $object
* @return bool
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
* @throws LocalizedException
* @throws Exception
*/
public function getIsUniqueCategoryWidgetToStores(AbstractModel $object)
{
$entityMetadata = $this->metadataPool->getMetadata(CategoryWidgetInterface::class);
$linkField = $entityMetadata->getLinkField();
if ($this->storeManager->isSingleStoreMode()) {
$stores = [Store::DEFAULT_STORE_ID];
} else {
$stores = (array)$object->getData('store_id');
}
$select = $this->getConnection()->select()
->from(['ncw' => $this->getMainTable()])
['ncws' => $this->getTable('nicolasbejean_categorywidget_store')],
'ncw.' . $linkField . ' = ncws.' . $linkField,
->where('ncw.identifier = ?', $object->getData('identifier'))
->where('ncws.store_id IN (?)', $stores);
$select->where('ncw.' . $entityMetadata->getIdentifierField() . ' <> ?', $object->getId());
}
if ($this->getConnection()->fetchRow($select)) {
return false;
}
return true;
}
/**
* Get store ids to which specified item is assigned
*
* @param int $id
* @return array
* @throws LocalizedException
* @throws Exception
*/
public function lookupStoreIds($id)
{
$connection = $this->getConnection();
$entityMetadata = $this->metadataPool->getMetadata(CategoryWidgetInterface::class);
$linkField = $entityMetadata->getLinkField();
$select = $connection->select()
->from(['ncws' => $this->getTable('nicolasbejean_categorywidget_store')], 'store_id')
['ncw' => $this->getMainTable()],
'ncws.' . $linkField . ' = ncw.' . $linkField,
->where('ncw.' . $entityMetadata->getIdentifierField() . ' = :entity_id');
return $connection->fetchCol($select, ['entity_id' => (int)$id]);
}
/**
* @param AbstractModel $object
* @return $this
* @throws Exception
*/
public function save(AbstractModel $object)
{
$this->entityManager->save($object);
return $this;
}
/**
* @inheritDoc
*/
public function delete(AbstractModel $object)
{
$this->entityManager->delete($object);
return $this;
}
}