Skip to content

Commit ee7ff67

Browse files
committed
Merge branch 'feature8' of https://github.com/landofcoder/module-product-tags into feature8
2 parents 1f1334b + 75d3820 commit ee7ff67

File tree

21 files changed

+550
-146
lines changed

21 files changed

+550
-146
lines changed

Api/Data/TagInterface.php

Lines changed: 96 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,109 @@
11
<?php
22
namespace Lof\ProductTags\Api\Data;
33

4-
interface TagInterface extends \Magento\Framework\Api\ExtensibleDataInterface
4+
interface TagInterface
55
{
66

77
const TAG_ID = 'tag_id';
8-
const TAG_NAME = 'tag_name';
8+
const TAG_TITLE = 'tag_title';
9+
const TAG_IDENTIFIER = 'identifier';
10+
const TAG_DESCRIPTION = 'tag_description';
11+
const TAG_STATUS = 'status';
12+
/**
13+
* Get tagID
14+
*
15+
* @return int|null
16+
*/
917
public function getTagId();
10-
18+
/**
19+
* Set tagID
20+
*
21+
* @param int|null
22+
* @return $this
23+
*/
1124
public function setTagId($tagId);
12-
public function getTagName();
13-
public function setTagName($tagName);
25+
/**
26+
* Set tagTitle
27+
*
28+
* @return string|null
29+
*/
30+
public function getTagTitle();
31+
/**
32+
* Set tagTitle
33+
*
34+
* @param string|null
35+
* @return $this
36+
*/
37+
public function setTagTitle($tagTitle);
38+
/**
39+
* Set status
40+
*
41+
* @return bool|null
42+
*/
43+
public function getStatus();
44+
/**
45+
* Set status
46+
*
47+
* @param bool|null
48+
* @return $this
49+
*/
50+
public function setStatus($status);
51+
52+
/**
53+
* Set identifier
54+
*
55+
* @return string|null
56+
*/
57+
public function getIdentifier();
58+
/**
59+
* Set identifier
60+
*
61+
* @param string|null
62+
* @return $this
63+
*/
64+
public function setIdentifier($identifier);
65+
66+
/**
67+
* Set tag_description
68+
*
69+
* @return string|null
70+
*/
71+
public function getTagDescription();
72+
/**
73+
* Set tag_description
74+
*
75+
* @param string|null
76+
* @return $this
77+
*/
78+
public function setTagDescription($tagDescription);
1479

1580
/**
16-
* Retrieve existing extension attributes object or create a new one.
17-
* @return \Lof\ProductTags\Api\Data\TagExtensionInterface|null
81+
* Set StoreId
82+
*
83+
* @return int|null
84+
*/
85+
public function getStoreId();
86+
/**
87+
* Set storeId
88+
*
89+
* @param int|null
90+
* @return $this
91+
*/
92+
public function setStoreId($storeId);
93+
94+
/**
95+
* Set products
96+
*
97+
* @return string[]|null
98+
*/
99+
public function getProducts();
100+
/**
101+
* Set products
102+
*
103+
* @param string[]|null
104+
* @return $this
18105
*/
19-
// public function getExtensionAttributes();
106+
public function setProducts($products);
20107

21-
// /**
22-
// * Set an extension attributes object.
23-
// * @param \Lof\ProductTags\Api\Data\TagExtensionInterface $extensionAttributes
24-
// * @return $this
25-
// */
26-
// public function setExtensionAttributes(
27-
// \Lof\ProductTags\Api\Data\TagExtensionInterface $extensionAttributes
28-
// );
108+
29109
}

Api/ProductsManagementInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ interface ProductsManagementInterface
2828

2929
/**
3030
* GET for products api
31-
* @param string $param
32-
* @return string
31+
* @param string[] $param
32+
* @return string[]
3333
*/
3434
public function getProducts($param);
3535
}

Api/TagRepositoryInterface.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ interface TagRepositoryInterface
3434
* @return \Lof\ProductTags\Api\Data\TagInterface
3535
* @throws \Magento\Framework\Exception\LocalizedException
3636
*/
37-
public function save(
38-
\Lof\ProductTags\Api\Data\TagInterface $tag
39-
);
37+
public function save(\Lof\ProductTags\Api\Data\TagInterface $tag);
4038

4139
/**
4240
* Retrieve Tag
@@ -58,13 +56,11 @@ public function getList(
5856

5957
/**
6058
* Delete Tag
61-
* @param \Lof\ProductTags\Api\Data\TagInterface $tag
59+
* @param bool $tagId
6260
* @return bool true on success
6361
* @throws \Magento\Framework\Exception\LocalizedException
6462
*/
65-
public function delete(
66-
\Lof\ProductTags\Api\Data\TagInterface $tag
67-
);
63+
public function delete($tagId);
6864

6965
/**
7066
* Delete Tag by ID

Block/Adminhtml/Tags/Edit/DeleteButton.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ public function getButtonData()
3838
*/
3939
public function getDeleteUrl()
4040
{
41-
return $this->getUrl('*/*/delete', ['tag_id' => $this->getBlockId()]);
41+
return $this->getUrl('*/*/delete', ['tag_id' => $this->getTagId()]);
4242
}
4343
}

Controller/Adminhtml/Tag.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ public function __construct(
4242
* Initialize requested category and put it into registry.
4343
* Root category can be returned, if inappropriate store/category is specified
4444
*
45-
* @param bool $getRootInstead
4645
* @return \Lof\ProductTags\Model\Tag|false
4746
*/
48-
protected function _initCategory($getRootInstead = false)
47+
protected function _initTag()
4948
{
5049
$tagId = $this->resolveTagId();
5150
$storeId = $this->resolveStoreId();
@@ -59,7 +58,7 @@ protected function _initCategory($getRootInstead = false)
5958
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('current_tag', $tag);
6059
$this->_objectManager->get(\Magento\Cms\Model\Wysiwyg\Config::class)
6160
->setStoreId($storeId);
62-
return $category;
61+
return $tag;
6362
}
6463

6564
/**
@@ -69,7 +68,7 @@ protected function _initCategory($getRootInstead = false)
6968
*/
7069
private function resolveTagId() : int
7170
{
72-
$tagId = (int)$this->getRequest()->getParam('id', false);
71+
$tagId = (int)$this->getRequest()->getParam('tag_id', false);
7372

7473
return $tagId ?: (int)$this->getRequest()->getParam('entity_id', false);
7574
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Lof\ProductTags\Controller\Adminhtml\Tag;
8+
9+
use Magento\Framework\App\Action\HttpPostActionInterface;
10+
11+
class Delete extends \Lof\ProductTags\Controller\Adminhtml\Tag implements HttpPostActionInterface
12+
{
13+
/**
14+
* Delete action
15+
*
16+
* @return \Magento\Framework\Controller\ResultInterface
17+
*/
18+
public function execute()
19+
{
20+
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
21+
$resultRedirect = $this->resultRedirectFactory->create();
22+
// check if we know what should be deleted
23+
$id = $this->getRequest()->getParam('tag_id');
24+
if ($id) {
25+
try {
26+
// init model and delete
27+
$model = $this->_objectManager->create(\Lof\ProductTags\Model\Tag::class);
28+
$model->load($id);
29+
$model->delete();
30+
// display success message
31+
$this->messageManager->addSuccessMessage(__('You deleted the Tag.'));
32+
// go to grid
33+
return $resultRedirect->setPath('*/*/');
34+
} catch (\Exception $e) {
35+
// display error message
36+
$this->messageManager->addErrorMessage($e->getMessage());
37+
// go back to edit form
38+
return $resultRedirect->setPath('*/*/edit', ['tag_id' => $id]);
39+
}
40+
}
41+
// display error message
42+
$this->messageManager->addErrorMessage(__('We can\'t find a Tag to delete.'));
43+
// go to grid
44+
return $resultRedirect->setPath('*/*/');
45+
}
46+
}

Controller/Adminhtml/Tag/Edit.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* Edit CMS page action.
1313
*/
14-
class Edit extends \Magento\Backend\App\Action implements HttpGetActionInterface
14+
class Edit extends \Lof\ProductTags\Controller\Adminhtml\Tag implements HttpGetActionInterface
1515
{
1616
/**
1717
* Authorization level of a basic admin session
@@ -72,21 +72,9 @@ protected function _initAction()
7272
public function execute()
7373
{
7474
// 1. Get ID and create model
75-
$id = $this->getRequest()->getParam('tag_id');
76-
$model = $this->_objectManager->create(\Lof\ProductTags\Model\Tag::class);
77-
75+
$tag = $this->_initTag();
76+
$id = $tag->getId();
7877
// 2. Initial checking
79-
if ($id) {
80-
$model->load($id);
81-
if (!$model->getId()) {
82-
$this->messageManager->addErrorMessage(__('This tag no longer exists.'));
83-
/** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
84-
$resultRedirect = $this->resultRedirectFactory->create();
85-
return $resultRedirect->setPath('*/*/');
86-
}
87-
}
88-
89-
$this->_coreRegistry->register('product_tag', $model);
9078

9179
// 5. Build edit form
9280
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
@@ -97,7 +85,7 @@ public function execute()
9785
);
9886
$resultPage->getConfig()->getTitle()->prepend(__('Tags'));
9987
$resultPage->getConfig()->getTitle()
100-
->prepend($model->getId() ? $model->getTitle() : __('New Tag'));
88+
->prepend($tag->getId() ? $tag->getTagTitle() : __('New Tag'));
10189

10290
return $resultPage;
10391
}

Controller/Adminhtml/Tag/Grid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(
4141
*/
4242
public function execute()
4343
{
44-
$tag = $this->_initTag(true);
44+
$tag = $this->_initTag();
4545
if (!$tag) {
4646
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
4747
$resultRedirect = $this->resultRedirectFactory->create();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Lof\ProductTags\Controller\Adminhtml\Tag;
8+
9+
use Magento\Framework\App\Action\HttpPostActionInterface;
10+
use Magento\Framework\Controller\ResultFactory;
11+
use Magento\Backend\App\Action\Context;
12+
use Magento\Ui\Component\MassAction\Filter;
13+
use Lof\ProductTags\Model\ResourceModel\Tag\CollectionFactory;
14+
15+
/**
16+
* Class MassDelete
17+
*/
18+
class MassDelete extends \Magento\Backend\App\Action implements HttpPostActionInterface
19+
{
20+
21+
const ADMIN_RESOURCE = 'Lof_ProductTags::Tag';
22+
23+
protected $filter;
24+
protected $collectionFactory;
25+
public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
26+
{
27+
$this->filter = $filter;
28+
$this->collectionFactory = $collectionFactory;
29+
parent::__construct($context);
30+
}
31+
32+
33+
public function execute()
34+
{
35+
$collection = $this->filter->getCollection($this->collectionFactory->create());
36+
$collectionSize = $collection->getSize();
37+
38+
foreach ($collection as $item) {
39+
$item->delete();
40+
}
41+
42+
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been deleted.', $collectionSize));
43+
44+
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
45+
return $resultRedirect->setPath('*/*/');
46+
}
47+
}

Controller/Adminhtml/Tag/Save.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Magento\Framework\Stdlib\DateTime\Filter\Date;
1616

1717
/**
18-
* Save CMS Tag action.
18+
* Save Lof Tag action.
1919
*/
2020
class Save extends \Lof\ProductTags\Controller\Adminhtml\Tag implements HttpPostActionInterface
2121
{
@@ -51,18 +51,28 @@ public function execute()
5151

5252
/** @var \Lof\ProductTags\Model\Tag $model */
5353
$model = $this->TagFactory->create();
54-
5554
$id = $this->getRequest()->getParam('tag_id');
5655
if ($id) {
5756
try {
58-
$model = $this->tagRepository->getById($id);
57+
$model = $model->load($id);
5958
} catch (LocalizedException $e) {
6059
$this->messageManager->addErrorMessage(__('This tag no longer exists.'));
6160
return $resultRedirect->setPath('*/*/');
6261
}
6362
}
6463
$model->setData($data);
65-
try {
64+
if (isset($data['tag_products'])
65+
&& is_string($data['tag_products'])) {
66+
$products = json_decode($data['tag_products'], true);
67+
$model->setPostedProducts($products);
68+
}
69+
$this->_eventManager->dispatch(
70+
'lof_producttags_prepare_save',
71+
['tag' => $model, 'request' => $this->getRequest()]
72+
);
73+
$products = $model->getPostedProducts();
74+
75+
try{
6676
$model->save($model);
6777
$this->messageManager->addSuccessMessage(__('You saved the tag.'));
6878
$this->dataPersistor->clear('lof_productags_tag');
@@ -81,7 +91,6 @@ public function execute()
8191
private function processBlockReturn($model, $data, $resultRedirect)
8292
{
8393
$redirect = $data['back'] ?? 'close';
84-
8594
if ($redirect ==='continue') {
8695
$resultRedirect->setPath('*/*/edit', ['tag_id' => $model->getId()]);
8796
} else if ($redirect === 'close') {

0 commit comments

Comments
 (0)