Skip to content

Commit 227a253

Browse files
authored
Merge pull request #35 from landofcoder/develop
Develop
2 parents 6cb05b7 + 046030b commit 227a253

File tree

14 files changed

+97
-119
lines changed

14 files changed

+97
-119
lines changed

Api/Data/TagInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
*/
2121
namespace Lof\ProductTags\Api\Data;
2222

23-
interface TagInterface extends \Magento\Framework\Api\ExtensibleDataInterface
23+
interface TagInterface
2424
{
2525

2626
const TAG_ID = 'tag_id';
2727
const TAG_TITLE = 'tag_title';
2828
const TAG_IDENTIFIER = 'identifier';
2929
const TAG_DESCRIPTION = 'tag_description';
3030
const TAG_STATUS = 'status';
31+
const STORE_ID = 'store_id';
3132
/**
3233
* Get tagID
3334
*

Api/TagRepositoryInterface.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,24 @@
2020
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
* SOFTWARE.
2222
*/
23-
2423
namespace Lof\ProductTags\Api;
25-
2624
use Magento\Framework\Api\SearchCriteriaInterface;
27-
2825
interface TagRepositoryInterface
2926
{
30-
3127
/**
3228
* Save Tag
3329
* @param \Lof\ProductTags\Api\Data\TagInterface $tag
3430
* @return \Lof\ProductTags\Api\Data\TagInterface
3531
* @throws \Magento\Framework\Exception\LocalizedException
3632
*/
3733
public function save(\Lof\ProductTags\Api\Data\TagInterface $tag);
38-
3934
/**
4035
* Retrieve Tag
4136
* @param string $tagId
4237
* @return \Lof\ProductTags\Api\Data\TagInterface
4338
* @throws \Magento\Framework\Exception\LocalizedException
4439
*/
4540
public function getById($tagId);
46-
4741
/**
4842
* Retrieve Tag matching the specified criteria.
4943
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
@@ -53,15 +47,13 @@ public function getById($tagId);
5347
public function getList(
5448
\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
5549
);
56-
5750
/**
5851
* Delete Tag
5952
* @param bool $tagId
6053
* @return bool true on success
6154
* @throws \Magento\Framework\Exception\LocalizedException
6255
*/
6356
public function delete($tagId);
64-
6557
/**
6658
* Delete Tag by ID
6759
* @param string $tagId
@@ -70,4 +62,4 @@ public function delete($tagId);
7062
* @throws \Magento\Framework\Exception\LocalizedException
7163
*/
7264
public function deleteById($tagId);
73-
}
65+
}

Controller/Adminhtml/Tag.php

+4-18
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
* See COPYING.txt for license details.
55
*/
66
declare(strict_types=1);
7-
87
namespace Lof\ProductTags\Controller\Adminhtml;
9-
108
use Magento\Store\Model\Store;
11-
129
/**
1310
* Catalog Tag controller
1411
*/
@@ -20,12 +17,10 @@ abstract class Tag extends \Magento\Backend\App\Action
2017
* @see _isAllowed()
2118
*/
2219
const ADMIN_RESOURCE = 'Lof_ProductTags::Tag';
23-
2420
/**
2521
* @var \Magento\Framework\Stdlib\DateTime\Filter\Date
2622
*/
2723
protected $dateFilter;
28-
2924
/**
3025
* @param \Magento\Backend\App\Action\Context $context
3126
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date|null $dateFilter
@@ -37,43 +32,36 @@ public function __construct(
3732
$this->dateFilter = $dateFilter;
3833
parent::__construct($context);
3934
}
40-
4135
/**
4236
* Initialize requested category and put it into registry.
4337
* Root category can be returned, if inappropriate store/category is specified
4438
*
45-
* @param bool $getRootInstead
4639
* @return \Lof\ProductTags\Model\Tag|false
4740
*/
48-
protected function _initCategory($getRootInstead = false)
41+
protected function _initTag()
4942
{
5043
$tagId = $this->resolveTagId();
5144
$storeId = $this->resolveStoreId();
5245
$tag = $this->_objectManager->create(\Lof\ProductTags\Model\Tag::class);
53-
5446
if ($tagId) {
5547
$tag->load($tagId);
5648
}
57-
5849
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('tag', $tag);
5950
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('current_tag', $tag);
6051
$this->_objectManager->get(\Magento\Cms\Model\Wysiwyg\Config::class)
6152
->setStoreId($storeId);
62-
return $category;
53+
return $tag;
6354
}
64-
6555
/**
6656
* Resolve Category Id (from get or from post)
6757
*
6858
* @return int
6959
*/
7060
private function resolveTagId() : int
7161
{
72-
$tagId = (int)$this->getRequest()->getParam('id', false);
73-
62+
$tagId = (int)$this->getRequest()->getParam('tag_id', false);
7463
return $tagId ?: (int)$this->getRequest()->getParam('entity_id', false);
7564
}
76-
7765
/**
7866
* Resolve store id
7967
*
@@ -85,8 +73,6 @@ private function resolveTagId() : int
8573
private function resolveStoreId() : int
8674
{
8775
$storeId = (int)$this->getRequest()->getParam('store', false);
88-
8976
return $storeId ?: (int)$this->getRequest()->getParam('store_id', Store::DEFAULT_STORE_ID);
9077
}
91-
92-
}
78+
}

Controller/Adminhtml/Tag/Edit.php

+5-26
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,29 @@
44
* See COPYING.txt for license details.
55
*/
66
namespace Lof\ProductTags\Controller\Adminhtml\Tag;
7-
87
use Magento\Framework\App\Action\HttpGetActionInterface;
98
use Magento\Backend\App\Action;
10-
119
/**
1210
* Edit CMS page action.
1311
*/
14-
class Edit extends \Magento\Backend\App\Action implements HttpGetActionInterface
12+
class Edit extends \Lof\ProductTags\Controller\Adminhtml\Tag implements HttpGetActionInterface
1513
{
1614
/**
1715
* Authorization level of a basic admin session
1816
*
1917
* @see _isAllowed()
2018
*/
2119
const ADMIN_RESOURCE = 'Lof_ProductTags::Tag_edit';
22-
2320
/**
2421
* Core registry
2522
*
2623
* @var \Magento\Framework\Registry
2724
*/
2825
protected $_coreRegistry;
29-
3026
/**
3127
* @var \Magento\Framework\View\Result\PageFactory
3228
*/
3329
protected $resultPageFactory;
34-
3530
/**
3631
* @param Action\Context $context
3732
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
@@ -46,7 +41,6 @@ public function __construct(
4641
$this->_coreRegistry = $registry;
4742
parent::__construct($context);
4843
}
49-
5044
/**
5145
* Init actions
5246
*
@@ -62,7 +56,6 @@ protected function _initAction()
6256
->addBreadcrumb(__('Manage Tags'), __('Manage Tags'));
6357
return $resultPage;
6458
}
65-
6659
/**
6760
* Edit CMS page
6861
*
@@ -72,22 +65,9 @@ protected function _initAction()
7265
public function execute()
7366
{
7467
// 1. Get ID and create model
75-
$id = $this->getRequest()->getParam('tag_id');
76-
$model = $this->_objectManager->create(\Lof\ProductTags\Model\Tag::class);
77-
68+
$tag = $this->_initTag();
69+
$id = $tag->getId();
7870
// 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);
90-
9171
// 5. Build edit form
9272
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
9373
$resultPage = $this->_initAction();
@@ -97,8 +77,7 @@ public function execute()
9777
);
9878
$resultPage->getConfig()->getTitle()->prepend(__('Tags'));
9979
$resultPage->getConfig()->getTitle()
100-
->prepend($model->getId() ? $model->getTitle() : __('New Tag'));
101-
80+
->prepend($tag->getId() ? $tag->getTagTitle() : __('New Tag'));
10281
return $resultPage;
10382
}
104-
}
83+
}

Controller/Adminhtml/Tag/Grid.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@
55
* See COPYING.txt for license details.
66
*/
77
namespace Lof\ProductTags\Controller\Adminhtml\Tag;
8-
98
class Grid extends \Lof\ProductTags\Controller\Adminhtml\Tag
109
{
1110
/**
1211
* @var \Magento\Framework\Controller\Result\RawFactory
1312
*/
1413
protected $resultRawFactory;
15-
1614
/**
1715
* @var \Magento\Framework\View\LayoutFactory
1816
*/
1917
protected $layoutFactory;
20-
2118
/**
2219
* @param \Magento\Backend\App\Action\Context $context
2320
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
@@ -32,7 +29,6 @@ public function __construct(
3229
$this->resultRawFactory = $resultRawFactory;
3330
$this->layoutFactory = $layoutFactory;
3431
}
35-
3632
/**
3733
* Grid Action
3834
* Display list of products related to current category
@@ -41,7 +37,7 @@ public function __construct(
4137
*/
4238
public function execute()
4339
{
44-
$tag = $this->_initTag(true);
40+
$tag = $this->_initTag();
4541
if (!$tag) {
4642
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
4743
$resultRedirect = $this->resultRedirectFactory->create();
@@ -56,4 +52,4 @@ public function execute()
5652
)->toHtml()
5753
);
5854
}
59-
}
55+
}

Controller/Adminhtml/Tag/Save.php

+17-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* See COPYING.txt for license details.
55
*/
66
namespace Lof\ProductTags\Controller\Adminhtml\Tag;
7-
87
use Magento\Framework\App\Action\HttpPostActionInterface;
98
use Magento\Backend\App\Action\Context;
109
use Lof\ProductTags\Api\TagRepositoryInterface;
@@ -13,7 +12,6 @@
1312
use Magento\Framework\Exception\LocalizedException;
1413
use Magento\Framework\Registry;
1514
use Magento\Framework\Stdlib\DateTime\Filter\Date;
16-
1715
/**
1816
* Save Lof Tag action.
1917
*/
@@ -48,20 +46,33 @@ public function execute()
4846
if (empty($data['tag_id'])) {
4947
$data['tag_id'] = null;
5048
}
51-
49+
if (!empty($data['identifier'])) {
50+
$data['identifier'] = preg_replace('/(#)|(%)|(&)|({)|(})|(!)|(@)|(:)|(;)|(,)|(<)|(>)|(=)/', '', $data['identifier']);
51+
$data['identifier'] = str_replace(" ","-",trim($data['identifier']));
52+
$data['identifier'] = strtolower($data['identifier']);
53+
}
5254
/** @var \Lof\ProductTags\Model\Tag $model */
5355
$model = $this->TagFactory->create();
5456
$id = $this->getRequest()->getParam('tag_id');
5557
if ($id) {
5658
try {
57-
$model = $this->tagRepository->getById($id);
59+
$model = $model->load($id);
5860
} catch (LocalizedException $e) {
5961
$this->messageManager->addErrorMessage(__('This tag no longer exists.'));
6062
return $resultRedirect->setPath('*/*/');
6163
}
6264
}
63-
6465
$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();
6576
try{
6677
$model->save($model);
6778
$this->messageManager->addSuccessMessage(__('You saved the tag.'));
@@ -72,7 +83,6 @@ public function execute()
7283
} catch (\Exception $e) {
7384
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the tag.'));
7485
}
75-
7686
$this->dataPersistor->set('lof_productags_tag', $data);
7787
return $resultRedirect->setPath('*/*/edit', ['tag_id' => $id]);
7888
}
@@ -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') {
@@ -99,4 +108,4 @@ private function processBlockReturn($model, $data, $resultRedirect)
99108
}
100109
return $resultRedirect;
101110
}
102-
}
111+
}

0 commit comments

Comments
 (0)