Skip to content

Commit 7ed5f03

Browse files
authored
Merge pull request #24 from landofcoder/develop
Develop
2 parents b3bda39 + 45628b0 commit 7ed5f03

File tree

22 files changed

+592
-127
lines changed

22 files changed

+592
-127
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/Tag/Product/ListProduct.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,47 @@
2525

2626
class ListProduct extends \Magento\Framework\View\Element\Template
2727
{
28+
protected $resultPageFactory;
29+
30+
protected $_tagFactory;
31+
32+
protected $_tagcollection;
33+
34+
protected $_tagHelper;
2835

29-
/**
30-
* Constructor
31-
*
32-
* @param \Magento\Framework\View\Element\Template\Context $context
33-
* @param array $data
34-
*/
3536
public function __construct(
3637
\Magento\Framework\View\Element\Template\Context $context,
38+
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
39+
\Lof\ProductTags\Model\TagFactory $tagFactory,
40+
\Lof\ProductTags\Helper\Data $tagdata,
3741
array $data = []
3842
) {
43+
$this->resultPageFactory = $resultPageFactory;
44+
$this->_tagFactory = $tagFactory;
45+
$this->_tagHelper = $tagdata;
3946
parent::__construct($context, $data);
4047
}
41-
42-
/**
43-
* @return string
44-
*/
45-
public function getListProducts()
48+
public function _toHtml(){
49+
if(!$this->_tagHelper->getGeneralConfig('enabled')) return;
50+
if(!$this->_tagHelper->getGeneralConfig('enable_tag_sidebar')) return;
51+
return parent::_toHtml();
52+
}
53+
function getTagHelper(){
54+
return $this->_tagHelper;
55+
}
56+
public function getTagCollection()
4657
{
47-
//Your block code
48-
return __('Hello Developer! This how to get the storename: %1 and this is the way to build a url: %2', $this->_storeManager->getStore()->getName(), $this->getUrl('contacts'));
58+
if(!$this->_tagcollection){
59+
$limit = $this->_tagHelper->getGeneralConfig('number_tags_sidebar');
60+
$limit = $limit?(int)$limit:10;
61+
$tag = $this->_tagFactory->create();
62+
$collection = $tag->getCollection();
63+
$collection->addFieldToFilter("status", 1);
64+
$collection->setOrder("tag_id","DESC");
65+
$collection->setPageSize($limit);
66+
//$collection->setLimit($limit);
67+
$this->_tagcollection = $collection;
68+
}
69+
return $this->_tagcollection;
4970
}
5071
}

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
}

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();

Controller/Adminhtml/Tag/Save.php

Lines changed: 13 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,27 @@ 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
}
64-
6563
$model->setData($data);
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+
6675
try{
6776
$model->save($model);
6877
$this->messageManager->addSuccessMessage(__('You saved the tag.'));
@@ -82,7 +91,6 @@ public function execute()
8291
private function processBlockReturn($model, $data, $resultRedirect)
8392
{
8493
$redirect = $data['back'] ?? 'close';
85-
8694
if ($redirect ==='continue') {
8795
$resultRedirect->setPath('*/*/edit', ['tag_id' => $model->getId()]);
8896
} else if ($redirect === 'close') {

Helper/Data.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ class Data extends AbstractHelper
3636
*/
3737

3838
protected $scopeConfig;
39+
public $_storeManager;
3940
const XML_PATH_TAG = 'lofproductags/';
40-
public function __construct(Context $context,ScopeConfigInterface $scopeConfig)
41+
public function __construct(
42+
Context $context,
43+
ScopeConfigInterface $scopeConfig,
44+
\Magento\Store\Model\StoreManagerInterface $storeManager
45+
)
4146
{
4247
parent::__construct($context);
4348
$this->scopeConfig=$scopeConfig;
49+
$this->_storeManager=$storeManager;
4450
}
4551

4652
/**
@@ -55,5 +61,12 @@ public function getConfigValue($field, $storeId = null)
5561
public function getGeneralConfig($code, $storeId = null)
5662
{
5763
return $this->getConfigValue(self::XML_PATH_TAG .'general/'. $code, $storeId);
58-
}
64+
}
65+
public function getTagUrl($tag_identifier=""){
66+
$route = $this->getGeneralConfig("route");
67+
$route = $route?$route:"lofproducttags";
68+
$url = $route."/".$tag_identifier;
69+
$base_url = $this->_storeManager->getStore()->getBaseUrl();
70+
return $base_url.$url;
71+
}
5972
}

0 commit comments

Comments
 (0)