Skip to content

Commit 8446cf9

Browse files
authored
Merge pull request #4 from landofcoder/check_session
Check session
2 parents 33c65b5 + 5d1b5e3 commit 8446cf9

File tree

12 files changed

+312
-98
lines changed

12 files changed

+312
-98
lines changed

Api/Data/ChatInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface ChatInterface extends \Magento\Framework\Api\ExtensibleDataInterface
2525
const USER_ID = 'user_id';
2626
const UPDATED_AT = 'updated_at';
2727
const NUMBER_MESSAGE = 'number_message';
28+
const SESSION_ID = 'session_id';
2829

2930
/**
3031
* Get chat_id
@@ -235,5 +236,18 @@ public function getAnswered();
235236
* @return \Lof\ChatSystem\Api\Data\ChatInterface
236237
*/
237238
public function setAnswered($answered);
239+
240+
/**
241+
* Get session_id
242+
* @return string|null
243+
*/
244+
public function getSessionId();
245+
246+
/**
247+
* Set session_id
248+
* @param string $session_id
249+
* @return \Lof\ChatSystem\Api\Data\ChatInterface
250+
*/
251+
public function setSessionId($session_id);
238252
}
239253

Block/Adminhtml/Chat/Edit/Tab/Customer.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ protected function _prepareForm()
9696
'base_fieldset',
9797
['legend' => __('Customer Information'), 'class' => 'fieldset-wide', 'disabled' => $isElementDisabled]
9898
);
99+
$fieldset->addField(
100+
'session_id',
101+
'note',
102+
[
103+
'name' => 'session_id',
104+
'label' => __('Chat Key'),
105+
'title' => __('Chat Key'),
106+
'text' => $model->getSessionId()
107+
]
108+
);
99109
$fieldset->addField(
100110
'customer_name',
101111
'note',
@@ -117,6 +127,16 @@ protected function _prepareForm()
117127
['id' => $model->getCustomerId()]) . "' target='blank' title='" . __('View Customer') . "'>" . $model->getCustomerEmail() . '</a>' : __('Guest')
118128
]
119129
);
130+
$fieldset->addField(
131+
'ip',
132+
'note',
133+
[
134+
'name' => 'ip',
135+
'label' => __('IP Address'),
136+
'title' => __('IP Address'),
137+
'text' => $model->getIp()
138+
]
139+
);
120140

121141
$order_id = $this->order->getCollection()->addFieldToFilter('customer_id',
122142
$model->getCustomerId())->getLastItem()->getId();

Block/Chat/Chat.php

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,41 +74,19 @@ public function _toHtml()
7474
return;
7575
}
7676

77-
public function isLogin() {
77+
public function isLogin()
78+
{
7879
if ($this->_customerSession->isLoggedIn()) {
7980
return true;
8081
}
8182
return false;
8283
}
83-
public function getChatId() {
8484

85-
if($this->isLogin()) {
86-
$chat = $this->chat->create()->getCollection()->addFieldToFilter('customer_email',$this->getCustomer()->getData('email'));
87-
if($chat->getSize() > 0) {
88-
$chat_id = $chat->getFirstItem()->getData('chat_id');
89-
}else {
90-
$chatModel = $this->chat->create();
91-
92-
$chatModel
93-
->setCustomerId($this->getCustomerSession()->getCustomerId())
94-
->setCustomerName($this->getCustomer()->getData('firstname').' '.$this->getCustomer()->getData('lastname'))
95-
->setCustomerEmail($this->getCustomer()->getData('email'));
96-
$chatModel->save();
97-
$chat_id = $chatModel->getData('chat_id');
98-
}
99-
} else {
100-
$chat = $this->chat->create()->getCollection()->addFieldToFilter('ip',$this->helper->getIp());
101-
if($chat->getSize() > 0) {
102-
$chat_id = $chat->getFirstItem()->getData('chat_id');
103-
} else {
104-
$chatModel = $this->chat->create();
105-
$chatModel->setIp($this->helper->getIp());
106-
$chatModel->save();
107-
$chat_id = $chatModel->getData('chat_id');
108-
}
109-
}
110-
return $chat_id;
85+
public function getChatId()
86+
{
87+
return $this->helper->getChatId($this->chat);
11188
}
89+
11290
public function getCurrentUrl()
11391
{
11492
return $this->_urlBuilder->getCurrentUrl();

Controller/Chat/Msglog.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,16 @@ class Msglog extends \Magento\Framework\App\Action\Action
6363

6464
protected $_message;
6565

66+
/**
67+
* @var \Lof\ChatSystem\Model\ChatFactory
68+
*/
69+
protected $chatFactory;
70+
71+
/**
72+
* @var \Lof\ChatSystem\Model\Chat
73+
*/
6674
protected $chat;
75+
6776
protected $httpRequest;
6877

6978
/**
@@ -74,29 +83,33 @@ class Msglog extends \Magento\Framework\App\Action\Action
7483
protected $blacklistFactory;
7584

7685
/**
77-
* @param Context $context
78-
* @param \Magento\Store\Model\StoreManager $storeManager
79-
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
80-
* @param \Lof\ChatSystem\Helper\Data $helper
81-
* @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
82-
* @param \Magento\Framework\Registry $registry
86+
* @param Context $context
87+
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
88+
* @param \Lof\ChatSystem\Helper\Data $helper
89+
* @param \Lof\ChatSystem\Model\ChatMessage $message
90+
* @param \Lof\ChatSystem\Model\ChatFactory $chatFactory
91+
* @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
92+
* @param \Magento\Framework\Registry $registry
93+
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
94+
* @param \Magento\Customer\Model\Session $customerSession
95+
* @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
96+
* @param \Lof\ChatSystem\Model\BlacklistFactory $blacklistFactory
8397
*/
8498
public function __construct(
8599
Context $context,
86-
\Magento\Store\Model\StoreManager $storeManager,
87100
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
88101
\Lof\ChatSystem\Helper\Data $helper,
89102
\Lof\ChatSystem\Model\ChatMessage $message,
90-
\Lof\ChatSystem\Model\Chat $chat,
103+
\Lof\ChatSystem\Model\ChatFactory $chatFactory,
91104
\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
92105
\Magento\Framework\Registry $registry,
93106
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
94107
\Magento\Customer\Model\Session $customerSession,
95108
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
96-
\Magento\Framework\App\Request\Http $httpRequest,
97109
\Lof\ChatSystem\Model\BlacklistFactory $blacklistFactory
98110
) {
99-
$this->chat = $chat;
111+
$this->chatFactory = $chatFactory;
112+
$this->chat = $chatFactory->create();
100113
$this->resultPageFactory = $resultPageFactory;
101114
$this->_helper = $helper;
102115
$this->_message = $message;
@@ -147,8 +160,13 @@ public function execute()
147160
if($this->_customerSession->getCustomer()->getEmail()) {
148161
$message = $this->_message->getCollection()->addFieldToFilter('customer_email',$this->_customerSession->getCustomer()->getEmail());
149162
} else {
150-
$chat = $this->chat->load($this->_helper->getIp(),'ip');
151-
$message = $this->_message->getCollection()->addFieldToFilter('chat_id',$chat->getId());
163+
$chat_id = $this->_helper->getChatId($this->chatFactory);
164+
if ($chat_id) {
165+
$chat = $this->chat->load((int)$chat_id);
166+
} else {
167+
$chat = $this->chat->load($this->_helper->getIp(), 'ip');
168+
}
169+
$message = $this->_message->getCollection()->addFieldToFilter('chat_id', $chat->getId());
152170
}
153171
$count = count($message);
154172
$i=0;

Controller/Chat/Sendmsg.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,19 @@ public function execute()
121121
$data['is_read'] =1;
122122
$data['current_time'] = $this->_helper->getCurrentTime();
123123

124-
if($customer_email = $this->_customerSession->getCustomer()->getEmail()) {
124+
if ($customer_email = $this->_customerSession->getCustomer()->getEmail()) {
125125
$customer_id = $this->_customerSession->getCustomerId();
126-
if(!isset($data["customer_id"]) || (empty($data["customer_id"]))){
126+
if (!isset($data["customer_id"]) || (empty($data["customer_id"]))) {
127127
$data["customer_id"] = (int)$customer_id;
128128
}
129-
if(!isset($data["customer_email"]) || ($data["customer_email"] != $customer_email)){
129+
if (!isset($data["customer_email"]) || ($data["customer_email"] != $customer_email)) {
130130
$data["customer_email"] = $customer_email;
131131
}
132-
if(!isset($data["customer_name"]) || (empty($data["customer_name"]))){
132+
if (!isset($data["customer_name"]) || (empty($data["customer_name"]))) {
133133
$data["customer_name"] = $this->_customerSession->getCustomer()->getData("firstname").' '. $this->_customerSession->getCustomer()->getData("lastname");
134134
}
135135
}
136-
if(empty($data['customer_name'])) {
136+
if (empty($data['customer_name'])) {
137137
$data['customer_name'] = __('Guest');
138138
}
139139
$data = $this->_helper->xss_clean_array($data);
@@ -198,24 +198,30 @@ public function execute()
198198
}
199199
}
200200
}
201-
if(!empty($data) && !empty($data['body_msg'])){
201+
if (!empty($data) && !empty($data['body_msg'])) {
202+
202203
$responseData = [];
203204
$message = $this->_message;
204-
try{
205-
$data['chat_id'] = isset($data['chat_id'])?$data['chat_id']:null;
205+
try {
206+
$chat_id = $this->_helper->getChatId($this->_chatModelFactory);
207+
if ($chat_id) {
208+
$chat = $this->_chatModelFactory->create()->load((int)$chat_id);
209+
} else {
210+
$chat = $this->_chatModelFactory->create()->load($this->_helper->getIp(), 'ip');
211+
}
212+
$data['chat_id'] = $chat->getId();
213+
$number_message = $chat->getData('number_message') + 1;
206214
$message
207215
->setData($data)
208216
->save();
209-
$chat = $this->_chatModelFactory->create()->load($data['chat_id']);
210-
$number_message = $chat->getData('number_message') + 1;
211-
212217
$enable_auto_assign_user = $this->_helper->getConfig('system/enable_auto_assign_user');
213218
$admin_user_id = $this->_helper->getConfig('system/admin_user_id');
214-
if($enable_auto_assign_user && $admin_user_id){
219+
if ($enable_auto_assign_user && $admin_user_id) {
215220
$data["user_id"] = (int)$admin_user_id;
216-
}else {
221+
} else {
217222
$data["user_id"] = 0;
218223
}
224+
219225
$chat
220226
->setData('user_id', (int)$data["user_id"])
221227
->setData('is_read',1)
@@ -224,17 +230,19 @@ public function execute()
224230
->setData('number_message',$number_message)
225231
->setData('current_url',$data['current_url'])
226232
->setData('ip', $this->_helper->getIp())
233+
->setData('session_id', $this->_helper->getSessionId())
227234
->save();
235+
228236
$this->_cacheTypeList->cleanType('full_page');
229237

230-
if($this->_helper->getConfig('email_settings/enable_email')) {
238+
if ($this->_helper->getConfig('email_settings/enable_email')) {
231239
$chatId = $chat->getId();
232-
if(!$data['chat_id'] || ($data['chat_id'] != $chatId)){ //only send email at first chat
240+
if (!$data['chat_id'] || ($data['chat_id'] != $chatId)) { //only send email at first chat
233241
$data['url'] = $data['current_url'];
234242
$this->sender->sendEmailChat($data);
235243
}
236244
}
237-
}catch(\Exception $e){
245+
} catch(\Exception $e) {
238246
$this->messageManager->addError(
239247
__('We can\'t process your request right now. Sorry, that\'s all we know.')
240248
);

0 commit comments

Comments
 (0)