Skip to content

Commit 8e5d90f

Browse files
committed
updated add user to blacklist
1 parent 11c6cad commit 8e5d90f

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

Block/Adminhtml/Blacklist/Edit/DeleteButton.php

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

Block/Adminhtml/Chat/Edit.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,19 @@ protected function _construct()
5656
$this->_controller = 'adminhtml_chat';
5757

5858
parent::_construct();
59+
$chat_id = $this->_coreRegistry->registry('lofchatsystem_chat')->getId();
5960
$this->buttonList->remove('save');
6061
$this->buttonList->remove('reset');
62+
$this->buttonList->add(
63+
'blacklist',
64+
[
65+
'label' => __('Add to Blacklist'),
66+
'class' => 'save primary',
67+
'onclick' => 'setLocation(\'' . $this->getUrl('lofchatsystem/*/addBlacklist', ["chat_id"=>(int)$chat_id]) . '\')'
68+
]
69+
70+
);
71+
6172
//$this->buttonList->add(
6273
//'close_chat',
6374
//[
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Landofcoder
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* http://www.landofcoder.com/license-agreement.html
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_ChatSystem
18+
* @copyright Copyright (c) 2018 Landofcoder (http://www.landofcoder.com/)
19+
* @license http://www.landofcoder.com/LICENSE-1.0.html
20+
*/
21+
namespace Lof\ChatSystem\Controller\Adminhtml\Chat;
22+
23+
class AddBlacklist extends \Magento\Backend\App\Action
24+
{
25+
protected $chatFactory;
26+
protected $blacklistFactory;
27+
/**
28+
* @param \Magento\Backend\App\Action\Context $context
29+
* @param \Lof\ChatSystem\Model\ChatFactory $chatFactory
30+
* @param \Lof\ChatSystem\Model\BlacklistFactory $blacklistFactory
31+
*/
32+
public function __construct(
33+
\Magento\Backend\App\Action\Context $context,
34+
\Lof\ChatSystem\Model\ChatFactory $chatFactory,
35+
\Lof\ChatSystem\Model\BlacklistFactory $blacklistFactory
36+
)
37+
{
38+
$this->chatFactory = $chatFactory;
39+
$this->blacklistFactory = $blacklistFactory;
40+
parent::__construct($context);
41+
}
42+
/**
43+
* Delete action
44+
*
45+
* @return \Magento\Framework\Controller\ResultInterface
46+
*/
47+
public function execute()
48+
{
49+
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
50+
$resultRedirect = $this->resultRedirectFactory->create();
51+
// check if we know what should be deleted
52+
$id = $this->getRequest()->getParam('chat_id');
53+
if ($id) {
54+
try {
55+
// init model and delete
56+
$model = $this->chatFactory->create();
57+
$model->load($id);
58+
$blacklist = $this->blacklistFactory->create();
59+
$blacklist->addChatToBlacklist($model->getData());
60+
// display success message
61+
$this->messageManager->addSuccessMessage(__('You add user to blacklist.'));
62+
// go to grid
63+
return $resultRedirect->setPath('*/*/');
64+
} catch (\Exception $e) {
65+
// display error message
66+
$this->messageManager->addErrorMessage($e->getMessage());
67+
// go back to edit form
68+
return $resultRedirect->setPath('*/*/edit', ['chat_id' => $id]);
69+
}
70+
}
71+
// display error message
72+
$this->messageManager->addErrorMessage(__('We can\'t find a Chat to add to black list.'));
73+
// go to grid
74+
return $resultRedirect->setPath('*/*/');
75+
}
76+
}
77+

Model/Blacklist.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
*/
2121
namespace Lof\ChatSystem\Model;
2222

23+
use Magento\Framework\Exception\CouldNotSaveException;
24+
use Magento\Framework\Exception\NoSuchEntityException;
25+
2326
class Blacklist extends \Magento\Framework\Model\AbstractModel
2427
{
2528
/**#@+
@@ -74,4 +77,52 @@ public function getAvailableStatuses()
7477
{
7578
return [self::STATUS_ENABLED => __('Blocked'), self::STATUS_DISABLED => __('Un Blocked')];
7679
}
80+
81+
/**
82+
* add chat info to backlist
83+
* @param array $data
84+
* @return boolean
85+
* @throws \Magento\Framework\Exception\LocalizedException
86+
*/
87+
88+
public function addChatToBlacklist($data = array()){
89+
$customer_id = isset($data["customer_id"])?$data["customer_id"]:0;
90+
$email = isset($data["customer_email"])?$data["customer_email"]:"";
91+
$ip = isset($data["ip"])?$data["ip"]:"";
92+
$chat_id = isset($data["chat_id"])?$data["chat_id"]:0;
93+
if (!$email && !$ip) {
94+
$this->messageManager->addError(__('Missing email or ip. You should input one of them.'));
95+
throw new CouldNotSaveException(__(
96+
'Missing email or ip. You should input one of them.'
97+
));
98+
}
99+
$data = [
100+
"customer_id" => $customer_id,
101+
"email" => $email,
102+
"ip" => $ip,
103+
"note" => ("chat_id:".$chat_id)
104+
];
105+
$collection = $this->getCollection();
106+
$blacklist_exists = $collection->addFieldToFilter(['email','ip'], [$email,$ip])->getSize();
107+
if ($email) {
108+
$this->loadByEmail($email);
109+
}
110+
if ($ip && !$this->getId()) {
111+
$this->loadByIp($ip);
112+
}
113+
if ($customer_id && !$this->getId()) {
114+
$this->loadByCustomerId($customer_id);
115+
}
116+
if (!$this->getId()) {
117+
// init model and set data
118+
$this->setData($data);
119+
}
120+
if ($blacklist_exists && (int)$blacklist_exists > 0) {
121+
throw new CouldNotSaveException(__(
122+
'Error: The ip or email was added to blocklist'
123+
));
124+
}
125+
$this->save();
126+
return true;
127+
}
77128
}

0 commit comments

Comments
 (0)