Skip to content

Commit

Permalink
Merge pull request #4 from landofcoder/check_session
Browse files Browse the repository at this point in the history
Check session
  • Loading branch information
landofcoder authored Sep 23, 2021
2 parents 33c65b5 + 5d1b5e3 commit 8446cf9
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 98 deletions.
14 changes: 14 additions & 0 deletions Api/Data/ChatInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface ChatInterface extends \Magento\Framework\Api\ExtensibleDataInterface
const USER_ID = 'user_id';
const UPDATED_AT = 'updated_at';
const NUMBER_MESSAGE = 'number_message';
const SESSION_ID = 'session_id';

/**
* Get chat_id
Expand Down Expand Up @@ -235,5 +236,18 @@ public function getAnswered();
* @return \Lof\ChatSystem\Api\Data\ChatInterface
*/
public function setAnswered($answered);

/**
* Get session_id
* @return string|null
*/
public function getSessionId();

/**
* Set session_id
* @param string $session_id
* @return \Lof\ChatSystem\Api\Data\ChatInterface
*/
public function setSessionId($session_id);
}

20 changes: 20 additions & 0 deletions Block/Adminhtml/Chat/Edit/Tab/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ protected function _prepareForm()
'base_fieldset',
['legend' => __('Customer Information'), 'class' => 'fieldset-wide', 'disabled' => $isElementDisabled]
);
$fieldset->addField(
'session_id',
'note',
[
'name' => 'session_id',
'label' => __('Chat Key'),
'title' => __('Chat Key'),
'text' => $model->getSessionId()
]
);
$fieldset->addField(
'customer_name',
'note',
Expand All @@ -117,6 +127,16 @@ protected function _prepareForm()
['id' => $model->getCustomerId()]) . "' target='blank' title='" . __('View Customer') . "'>" . $model->getCustomerEmail() . '</a>' : __('Guest')
]
);
$fieldset->addField(
'ip',
'note',
[
'name' => 'ip',
'label' => __('IP Address'),
'title' => __('IP Address'),
'text' => $model->getIp()
]
);

$order_id = $this->order->getCollection()->addFieldToFilter('customer_id',
$model->getCustomerId())->getLastItem()->getId();
Expand Down
34 changes: 6 additions & 28 deletions Block/Chat/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,41 +74,19 @@ public function _toHtml()
return;
}

public function isLogin() {
public function isLogin()
{
if ($this->_customerSession->isLoggedIn()) {
return true;
}
return false;
}
public function getChatId() {

if($this->isLogin()) {
$chat = $this->chat->create()->getCollection()->addFieldToFilter('customer_email',$this->getCustomer()->getData('email'));
if($chat->getSize() > 0) {
$chat_id = $chat->getFirstItem()->getData('chat_id');
}else {
$chatModel = $this->chat->create();

$chatModel
->setCustomerId($this->getCustomerSession()->getCustomerId())
->setCustomerName($this->getCustomer()->getData('firstname').' '.$this->getCustomer()->getData('lastname'))
->setCustomerEmail($this->getCustomer()->getData('email'));
$chatModel->save();
$chat_id = $chatModel->getData('chat_id');
}
} else {
$chat = $this->chat->create()->getCollection()->addFieldToFilter('ip',$this->helper->getIp());
if($chat->getSize() > 0) {
$chat_id = $chat->getFirstItem()->getData('chat_id');
} else {
$chatModel = $this->chat->create();
$chatModel->setIp($this->helper->getIp());
$chatModel->save();
$chat_id = $chatModel->getData('chat_id');
}
}
return $chat_id;
public function getChatId()
{
return $this->helper->getChatId($this->chat);
}

public function getCurrentUrl()
{
return $this->_urlBuilder->getCurrentUrl();
Expand Down
42 changes: 30 additions & 12 deletions Controller/Chat/Msglog.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ class Msglog extends \Magento\Framework\App\Action\Action

protected $_message;

/**
* @var \Lof\ChatSystem\Model\ChatFactory
*/
protected $chatFactory;

/**
* @var \Lof\ChatSystem\Model\Chat
*/
protected $chat;

protected $httpRequest;

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

/**
* @param Context $context
* @param \Magento\Store\Model\StoreManager $storeManager
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
* @param \Lof\ChatSystem\Helper\Data $helper
* @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
* @param \Magento\Framework\Registry $registry
* @param Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
* @param \Lof\ChatSystem\Helper\Data $helper
* @param \Lof\ChatSystem\Model\ChatMessage $message
* @param \Lof\ChatSystem\Model\ChatFactory $chatFactory
* @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
* @param \Lof\ChatSystem\Model\BlacklistFactory $blacklistFactory
*/
public function __construct(
Context $context,
\Magento\Store\Model\StoreManager $storeManager,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Lof\ChatSystem\Helper\Data $helper,
\Lof\ChatSystem\Model\ChatMessage $message,
\Lof\ChatSystem\Model\Chat $chat,
\Lof\ChatSystem\Model\ChatFactory $chatFactory,
\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Customer\Model\Session $customerSession,
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
\Magento\Framework\App\Request\Http $httpRequest,
\Lof\ChatSystem\Model\BlacklistFactory $blacklistFactory
) {
$this->chat = $chat;
$this->chatFactory = $chatFactory;
$this->chat = $chatFactory->create();
$this->resultPageFactory = $resultPageFactory;
$this->_helper = $helper;
$this->_message = $message;
Expand Down Expand Up @@ -147,8 +160,13 @@ public function execute()
if($this->_customerSession->getCustomer()->getEmail()) {
$message = $this->_message->getCollection()->addFieldToFilter('customer_email',$this->_customerSession->getCustomer()->getEmail());
} else {
$chat = $this->chat->load($this->_helper->getIp(),'ip');
$message = $this->_message->getCollection()->addFieldToFilter('chat_id',$chat->getId());
$chat_id = $this->_helper->getChatId($this->chatFactory);
if ($chat_id) {
$chat = $this->chat->load((int)$chat_id);
} else {
$chat = $this->chat->load($this->_helper->getIp(), 'ip');
}
$message = $this->_message->getCollection()->addFieldToFilter('chat_id', $chat->getId());
}
$count = count($message);
$i=0;
Expand Down
40 changes: 24 additions & 16 deletions Controller/Chat/Sendmsg.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ public function execute()
$data['is_read'] =1;
$data['current_time'] = $this->_helper->getCurrentTime();

if($customer_email = $this->_customerSession->getCustomer()->getEmail()) {
if ($customer_email = $this->_customerSession->getCustomer()->getEmail()) {
$customer_id = $this->_customerSession->getCustomerId();
if(!isset($data["customer_id"]) || (empty($data["customer_id"]))){
if (!isset($data["customer_id"]) || (empty($data["customer_id"]))) {
$data["customer_id"] = (int)$customer_id;
}
if(!isset($data["customer_email"]) || ($data["customer_email"] != $customer_email)){
if (!isset($data["customer_email"]) || ($data["customer_email"] != $customer_email)) {
$data["customer_email"] = $customer_email;
}
if(!isset($data["customer_name"]) || (empty($data["customer_name"]))){
if (!isset($data["customer_name"]) || (empty($data["customer_name"]))) {
$data["customer_name"] = $this->_customerSession->getCustomer()->getData("firstname").' '. $this->_customerSession->getCustomer()->getData("lastname");
}
}
if(empty($data['customer_name'])) {
if (empty($data['customer_name'])) {
$data['customer_name'] = __('Guest');
}
$data = $this->_helper->xss_clean_array($data);
Expand Down Expand Up @@ -198,24 +198,30 @@ public function execute()
}
}
}
if(!empty($data) && !empty($data['body_msg'])){
if (!empty($data) && !empty($data['body_msg'])) {

$responseData = [];
$message = $this->_message;
try{
$data['chat_id'] = isset($data['chat_id'])?$data['chat_id']:null;
try {
$chat_id = $this->_helper->getChatId($this->_chatModelFactory);
if ($chat_id) {
$chat = $this->_chatModelFactory->create()->load((int)$chat_id);
} else {
$chat = $this->_chatModelFactory->create()->load($this->_helper->getIp(), 'ip');
}
$data['chat_id'] = $chat->getId();
$number_message = $chat->getData('number_message') + 1;
$message
->setData($data)
->save();
$chat = $this->_chatModelFactory->create()->load($data['chat_id']);
$number_message = $chat->getData('number_message') + 1;

$enable_auto_assign_user = $this->_helper->getConfig('system/enable_auto_assign_user');
$admin_user_id = $this->_helper->getConfig('system/admin_user_id');
if($enable_auto_assign_user && $admin_user_id){
if ($enable_auto_assign_user && $admin_user_id) {
$data["user_id"] = (int)$admin_user_id;
}else {
} else {
$data["user_id"] = 0;
}

$chat
->setData('user_id', (int)$data["user_id"])
->setData('is_read',1)
Expand All @@ -224,17 +230,19 @@ public function execute()
->setData('number_message',$number_message)
->setData('current_url',$data['current_url'])
->setData('ip', $this->_helper->getIp())
->setData('session_id', $this->_helper->getSessionId())
->save();

$this->_cacheTypeList->cleanType('full_page');

if($this->_helper->getConfig('email_settings/enable_email')) {
if ($this->_helper->getConfig('email_settings/enable_email')) {
$chatId = $chat->getId();
if(!$data['chat_id'] || ($data['chat_id'] != $chatId)){ //only send email at first chat
if (!$data['chat_id'] || ($data['chat_id'] != $chatId)) { //only send email at first chat
$data['url'] = $data['current_url'];
$this->sender->sendEmailChat($data);
}
}
}catch(\Exception $e){
} catch(\Exception $e) {
$this->messageManager->addError(
__('We can\'t process your request right now. Sorry, that\'s all we know.')
);
Expand Down
Loading

0 comments on commit 8446cf9

Please sign in to comment.