|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Magento Chatbot Integration |
| 4 | + * Copyright (C) 2017 |
| 5 | + * |
| 6 | + * This file is part of Werules/Chatbot. |
| 7 | + * |
| 8 | + * Werules/Chatbot is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +namespace Werules\Chatbot\Block\Chatbox; |
| 23 | + |
| 24 | +class Messenger extends \Magento\Framework\View\Element\Template |
| 25 | +{ |
| 26 | + protected $_helper; |
| 27 | + protected $_define; |
| 28 | + protected $_chatbotAPI; |
| 29 | + protected $_store; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + \Magento\Framework\View\Element\Template\Context $context, |
| 33 | + \Werules\Chatbot\Helper\Data $helperData, |
| 34 | + \Magento\Store\Api\Data\StoreInterface $store, |
| 35 | + \Werules\Chatbot\Model\ChatbotAPI $chatbotAPI, |
| 36 | + array $data = array() |
| 37 | + ) |
| 38 | + { |
| 39 | + $this->_chatbotAPI = $chatbotAPI; |
| 40 | + $this->_helper = $helperData; |
| 41 | + $this->_store = $store; |
| 42 | + $this->_define = new \Werules\Chatbot\Helper\Define; |
| 43 | + parent::__construct($context, $data); |
| 44 | + } |
| 45 | + |
| 46 | + private function getMessengerInstance() |
| 47 | + { |
| 48 | + $api_token = $this->_helper->getConfigValue('werules_chatbot_messenger/general/api_key'); |
| 49 | + $messenger = $this->_chatbotAPI->initMessengerAPI($api_token); |
| 50 | + return $messenger; |
| 51 | + } |
| 52 | + |
| 53 | + public function getFacebookPageId() |
| 54 | + { |
| 55 | + $pageId = $this->getConfigValue('werules_chatbot_messenger/general/page_id'); |
| 56 | + if ($pageId) |
| 57 | + return $pageId; |
| 58 | + |
| 59 | + $messengerInstance = $this->getMessengerInstance(); |
| 60 | + $pageDetails = $messengerInstance->getPageDetails(); |
| 61 | + if (isset($pageDetails['id'])) |
| 62 | + { |
| 63 | + $pageId = $pageDetails['id']; |
| 64 | + $this->setConfigValue('werules_chatbot_messenger/general/page_id', $pageId); |
| 65 | + return $pageId; |
| 66 | + } |
| 67 | + |
| 68 | + return ''; |
| 69 | + } |
| 70 | + |
| 71 | + public function getFacebookAppId() |
| 72 | + { |
| 73 | + $appId = $this->getConfigValue('werules_chatbot_messenger/general/app_id'); |
| 74 | + return $appId; |
| 75 | + } |
| 76 | + |
| 77 | + public function isDomainWhitelisted() |
| 78 | + { |
| 79 | + $enable = $this->getConfigValue('werules_chatbot_messenger/general/domain_whitelisted'); |
| 80 | + if ($enable) |
| 81 | + return true; |
| 82 | + |
| 83 | + $messengerInstance = $this->getMessengerInstance(); |
| 84 | +// $url = parse_url($_SERVER['SERVER_NAME'], PHP_URL_HOST); |
| 85 | + $url = $_SERVER['SERVER_NAME']; |
| 86 | + $domain = array($url); |
| 87 | + $result = $messengerInstance->addDomainsToWhitelist($domain); |
| 88 | + if (!isset($result['error'])) |
| 89 | + { |
| 90 | + $this->setConfigValue('werules_chatbot_messenger/general/domain_whitelisted', $this->_define::WHITELABELED); |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + public function isChatboxEnabled() |
| 98 | + { |
| 99 | + $enable = $this->getConfigValue('werules_chatbot_messenger/general/enable_messenger_box'); |
| 100 | + $isWhitelisted = $this->isDomainWhitelisted(); |
| 101 | + if ($enable && $isWhitelisted) |
| 102 | + return true; |
| 103 | + |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + private function getConfigValue($code) |
| 108 | + { |
| 109 | + return $this->_helper->getConfigValue($code); |
| 110 | + } |
| 111 | + |
| 112 | + private function setConfigValue($field, $value) |
| 113 | + { |
| 114 | + $this->_helper->setConfigValue($field, $value); |
| 115 | + } |
| 116 | + |
| 117 | + public function getLocaleCode() |
| 118 | + { |
| 119 | + return $this->_store->getLocaleCode(); |
| 120 | + } |
| 121 | +} |
0 commit comments