Skip to content

Commit a744393

Browse files
authored
Merge pull request #15 from landofcoder/feature2-1
Feature2 1
2 parents ad7013f + 71b6e57 commit a744393

File tree

6 files changed

+98
-64
lines changed

6 files changed

+98
-64
lines changed

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
}
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+
}
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ public function execute()
6161
return $resultRedirect->setPath('*/*/');
6262
}
6363
}
64+
6465
$model->setData($data);
65-
try {
66+
try{
6667
$model->save($model);
6768
$this->messageManager->addSuccessMessage(__('You saved the tag.'));
6869
$this->dataPersistor->clear('lof_productags_tag');

Model/ResourceModel/Tag.php

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,10 @@
11
<?php
2-
/**
3-
* Copyright (c) 2019 Landofcoder
4-
*
5-
* Permission is hereby granted, free of charge, to any person obtaining a copy
6-
* of this software and associated documentation files (the "Software"), to deal
7-
* in the Software without restriction, including without limitation the rights
8-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
* copies of the Software, and to permit persons to whom the Software is
10-
* furnished to do so, subject to the following conditions:
11-
*
12-
* The above copyright notice and this permission notice shall be included in all
13-
* copies or substantial portions of the Software.
14-
*
15-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
* SOFTWARE.
22-
*/
23-
242
namespace Lof\ProductTags\Model\ResourceModel;
253

264
class Tag extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
275
{
28-
29-
protected $_tagProductTable;
30-
/**
31-
* Define resource model
32-
*
33-
* @return void
34-
*/
356
protected function _construct()
367
{
378
$this->_init('lof_producttags_tag', 'tag_id');
389
}
39-
/**
40-
* Category product table name getter
41-
*
42-
* @return string
43-
*/
44-
public function getTagProductTable()
45-
{
46-
if (!$this->_tagProductTable) {
47-
$this->_tagProductTable = $this->getTable('lof_producttags_product');
48-
}
49-
return $this->_tagProductTable;
50-
}
51-
/**
52-
* Get positions of associated to category products
53-
*
54-
* @param \Lof\ProductTags\Model\Tag $tag
55-
* @return array
56-
*/
57-
public function getProductsPosition($tag)
58-
{
59-
$select = $this->getConnection()->select()->from(
60-
$this->getTagProductTable(),
61-
['product_id', 'position']
62-
)->where(
63-
"{$this->getTable('lof_producttags_product')}.tag_id = ?",
64-
$tag->getId()
65-
);
66-
$bind = ['tag_id' => (int)$tag->getId()];
67-
68-
return $this->getConnection()->fetchPairs($select, $bind);
69-
}
70-
}
10+
}

view/adminhtml/ui_component/lof_producttags_tag_listing.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<message translate="true">Are you sure you want to delete selected items?</message>
4949
<title translate="true">Delete items</title>
5050
</confirm>
51-
<url path="lof_producttags/tag/massDelete"/>
51+
<url path="lof_producttags/tag/massdelete"/>
5252
<type>delete</type>
5353
<label translate="true">Delete</label>
5454
</settings>

0 commit comments

Comments
 (0)