Skip to content

Commit f8bed71

Browse files
committed
Added store view settings to the blog tag
1 parent 960e923 commit f8bed71

File tree

16 files changed

+483
-8
lines changed

16 files changed

+483
-8
lines changed

Block/Sidebar/TagClaud.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function getTags()
6969
{
7070
if ($this->_tags === null) {
7171
$this->_tags = $this->_tagCollectionFactory->create()
72+
->addStoreFilter($this->_storeManager->getStore()->getId())
7273
->addActiveFilter();
7374

7475
$resource = $this->_tags->getResource();

Controller/Author/View.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public function execute()
5050
protected function _initAuthor()
5151
{
5252
$id = (int)$this->getRequest()->getParam('id');
53+
if (!$id) {
54+
return false;
55+
}
5356

5457
$author = $this->_objectManager->create(\Magefan\Blog\Api\AuthorInterface::class)->load($id);
5558

Controller/Category/View.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function execute()
6363
protected function _initCategory()
6464
{
6565
$id = (int)$this->getRequest()->getParam('id');
66+
if (!$id) {
67+
return false;
68+
}
69+
6670
$storeId = $this->_storeManager->getStore()->getId();
6771

6872
$category = $this->_objectManager->create(\Magefan\Blog\Model\Category::class)->load($id);

Controller/Post/View.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function execute()
6363
protected function _initPost()
6464
{
6565
$id = (int)$this->getRequest()->getParam('id');
66+
if (!$id) {
67+
return false;
68+
}
69+
6670
$secret = (string)$this->getRequest()->getParam('secret');
6771
$storeId = $this->_storeManager->getStore()->getId();
6872

@@ -92,10 +96,12 @@ protected function _initPost()
9296
*/
9397
protected function _initCategory()
9498
{
95-
$id = $this->getRequest()->getParam('category_id');
99+
$id = (int)$this->getRequest()->getParam('category_id');
100+
if (!$id) {
101+
return false;
102+
}
96103

97104
$storeId = $this->_storeManager->getStore()->getId();
98-
99105
$category = $this->_objectManager->create(\Magefan\Blog\Model\Category::class)->load($id);
100106

101107
if (!$category->isVisibleOnStore($storeId)) {

Controller/Tag/View.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
*/
1515
class View extends \Magefan\Blog\App\Action\Action
1616
{
17+
/**
18+
* Store manager
19+
*
20+
* @var \Magento\Store\Model\StoreManagerInterface
21+
*/
22+
private $_storeManager;
23+
1724
/**
1825
* View blog author action
1926
*
@@ -46,13 +53,30 @@ public function execute()
4653
protected function _initTag()
4754
{
4855
$id = (int)$this->getRequest()->getParam('id');
56+
if (!$id) {
57+
return false;
58+
}
4959

60+
$storeId = $this->getStoreManager()->getStore()->getId();
5061
$tag = $this->_objectManager->create(\Magefan\Blog\Model\Tag::class)->load($id);
5162

52-
if (!$tag->isActive()) {
63+
if (!$tag->isVisibleOnStore($storeId)) {
5364
return false;
5465
}
5566

67+
$post->setStoreId($storeId);
68+
5669
return $tag;
5770
}
71+
72+
/**
73+
* @return \Magento\Store\Model\StoreManagerInterface|mixed
74+
*/
75+
private function getStoreManager()
76+
{
77+
if (null === $this->_storeManager) {
78+
$this->_storeManager = $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
79+
}
80+
return $this->_storeManager;
81+
}
5882
}

Model/Post.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ public function getRelatedTags()
710710
if (null === $this->_relatedTags) {
711711
$this->_relatedTags = $this->_tagCollectionFactory->create()
712712
->addFieldToFilter('tag_id', ['in' => $this->getTags()])
713+
->addStoreFilter($this->getStoreId())
713714
->addActiveFilter()
714715
->setOrder('title');
715716
}

Model/ResourceModel/Tag.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
8282
return parent::_beforeSave($object);
8383
}
8484

85+
/**
86+
* Assign tag to store views
87+
*
88+
* @param \Magento\Framework\Model\AbstractModel $object
89+
* @return $this
90+
*/
91+
protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
92+
{
93+
$oldIds = $this->lookupStoreIds($object->getId());
94+
$newIds = (array)$object->getStoreIds();
95+
if (!$newIds || in_array(0, $newIds)) {
96+
$newIds = [0];
97+
}
98+
99+
$this->_updateLinks($object, $newIds, $oldIds, 'magefan_blog_tag_store', 'store_id');
100+
101+
return parent::_afterSave($object);
102+
}
103+
85104
/**
86105
* Load an object using 'identifier' field if there's no field specified and value is not numeric
87106
*
@@ -99,6 +118,22 @@ public function load(\Magento\Framework\Model\AbstractModel $object, $value, $fi
99118
return parent::load($object, $value, $field);
100119
}
101120

121+
/**
122+
* Perform operations after object load
123+
*
124+
* @param \Magento\Framework\Model\AbstractModel $object
125+
* @return $this
126+
*/
127+
protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
128+
{
129+
if ($object->getId()) {
130+
$storeIds = $this->lookupStoreIds($object->getId());
131+
$object->setData('store_ids', $storeIds);
132+
}
133+
134+
return parent::_afterLoad($object);
135+
}
136+
102137
/**
103138
* Check whether tag identifier is numeric
104139
*
@@ -120,4 +155,125 @@ protected function isValidPageIdentifier(\Magento\Framework\Model\AbstractModel
120155
{
121156
return preg_match('/^([^?#<>@!&*()$%^\\+=,{}"\']+)?$/', $object->getData('identifier'));
122157
}
158+
159+
/**
160+
* Get store ids to which specified item is assigned
161+
*
162+
* @param int $tagId
163+
* @return array
164+
*/
165+
public function lookupStoreIds($tagId)
166+
{
167+
return $this->_lookupIds($tagId, 'magefan_blog_tag_store', 'store_id');
168+
}
169+
170+
/**
171+
* Get ids to which specified item is assigned
172+
* @param int $tagId
173+
* @param string $tableName
174+
* @param string $field
175+
* @return array
176+
*/
177+
protected function _lookupIds($tagId, $tableName, $field)
178+
{
179+
$adapter = $this->getConnection();
180+
$select = $adapter->select()->from(
181+
$this->getTable($tableName),
182+
$field
183+
)->where(
184+
'tag_id = ?',
185+
(int)$tagId
186+
);
187+
188+
return $adapter->fetchCol($select);
189+
}
190+
191+
/**
192+
* Update tag connections
193+
* @param \Magento\Framework\Model\AbstractModel $object
194+
* @param Array $newRelatedIds
195+
* @param Array $oldRelatedIds
196+
* @param String $tableName
197+
* @param String $field
198+
* @param Array $rowData
199+
* @return void
200+
*/
201+
protected function _updateLinks(
202+
\Magento\Framework\Model\AbstractModel $object,
203+
array $newRelatedIds,
204+
array $oldRelatedIds,
205+
$tableName,
206+
$field,
207+
$rowData = []
208+
) {
209+
$table = $this->getTable($tableName);
210+
211+
if ($object->getId() && empty($rowData)) {
212+
$currentData = $this->_lookupAll($object->getId(), $tableName, '*');
213+
foreach ($currentData as $item) {
214+
$rowData[$item[$field]] = $item;
215+
}
216+
}
217+
218+
$insert = $newRelatedIds;
219+
$delete = $oldRelatedIds;
220+
221+
if ($delete) {
222+
$where = ['tag_id = ?' => (int)$object->getId(), $field.' IN (?)' => $delete];
223+
224+
$this->getConnection()->delete($table, $where);
225+
}
226+
227+
if ($insert) {
228+
$data = [];
229+
230+
foreach ($insert as $id) {
231+
$id = (int)$id;
232+
$data[] = array_merge(
233+
['tag_id' => (int)$object->getId(), $field => $id],
234+
(isset($rowData[$id]) && is_array($rowData[$id])) ? $rowData[$id] : []
235+
);
236+
}
237+
238+
/* Fix if some rows have extra data */
239+
$allFields = [];
240+
foreach ($data as $i => $row) {
241+
foreach ($row as $key => $value) {
242+
$allFields[$key] = $key;
243+
}
244+
}
245+
foreach ($data as $i => $row) {
246+
foreach ($allFields as $key) {
247+
if (!array_key_exists($key, $row)) {
248+
$data[$i][$key] = null;
249+
}
250+
}
251+
}
252+
/* End fix */
253+
254+
$this->getConnection()->insertMultiple($table, $data);
255+
}
256+
}
257+
258+
/**
259+
* Get rows to which specified item is assigned
260+
* @param int $tagId
261+
* @param string $tableName
262+
* @param string $field
263+
* @return array
264+
*/
265+
protected function _lookupAll($tagId, $tableName, $field)
266+
{
267+
$adapter = $this->getConnection();
268+
269+
$select = $adapter->select()->from(
270+
$this->getTable($tableName),
271+
$field
272+
)->where(
273+
'tag_id = ?',
274+
(int)$tagId
275+
);
276+
277+
return $adapter->fetchAll($select);
278+
}
123279
}

0 commit comments

Comments
 (0)