Добавлены настройки Daemon Collector
This commit is contained in:
parent
f4eca5a7b0
commit
2c8a87c582
89
src/Block/Frontend/DaemonCollector.php
Normal file
89
src/Block/Frontend/DaemonCollector.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Retailcrm\Retailcrm\Block\Frontend;
|
||||
|
||||
class DaemonCollector extends \Magento\Framework\View\Element\Template
|
||||
{
|
||||
private $customer;
|
||||
private $helper;
|
||||
private $storeManager;
|
||||
private $storeResolver;
|
||||
private $js = '';
|
||||
|
||||
private $template = <<<EOT
|
||||
<script type="text/javascript">
|
||||
(function(_,r,e,t,a,i,l){_['retailCRMObject']=a;_[a]=_[a]||function(){(_[a].q=_[a].q||[]).push(arguments)};_[a].l=1*new Date();l=r.getElementsByTagName(e)[0];i=r.createElement(e);i.async=!0;i.src=t;l.parentNode.insertBefore(i,l)})(window,document,'script','https://collector.retailcrm.pro/w.js','_rc');
|
||||
{{ code }}
|
||||
_rc('send', 'pageView');
|
||||
</script>
|
||||
EOT;
|
||||
|
||||
/**
|
||||
* DaemonCollector constructor.
|
||||
*
|
||||
* @param \Magento\Framework\View\Element\Template\Context $context
|
||||
* @param \Magento\Customer\Model\Session $customerSession
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
* @param \Magento\Store\Api\StoreResolverInterface $storeResolver
|
||||
* @param \Retailcrm\Retailcrm\Helper\Data $helper
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\View\Element\Template\Context $context,
|
||||
\Magento\Customer\Model\Session $customerSession,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager,
|
||||
\Magento\Store\Api\StoreResolverInterface $storeResolver,
|
||||
\Retailcrm\Retailcrm\Helper\Data $helper,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
$this->customer = $customerSession->getCustomer();
|
||||
$this->storeManager = $storeManager;
|
||||
$this->storeResolver = $storeResolver;
|
||||
$this->helper = $helper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getJs()
|
||||
{
|
||||
return $this->js;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function buildScript()
|
||||
{
|
||||
$params = array();
|
||||
|
||||
if ($this->customer->getId()) {
|
||||
$params['customerId'] = $this->customer->getId();
|
||||
}
|
||||
|
||||
try {
|
||||
$siteKey = $this->helper->getSiteKey(
|
||||
$this->storeManager->getStore(
|
||||
$this->storeResolver->getCurrentStoreId()
|
||||
)->getWebsiteId()
|
||||
);
|
||||
} catch (\Magento\Framework\Exception\NoSuchEntityException $entityException) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if ($siteKey) {
|
||||
$this->js = preg_replace(
|
||||
'/{{ code }}/',
|
||||
sprintf(
|
||||
"\t_rc('create', '%s', %s);\n",
|
||||
$siteKey,
|
||||
json_encode((object) $params)
|
||||
),
|
||||
$this->template
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Retailcrm\Retailcrm\Helper;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\Helper\AbstractHelper;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Framework\App\Helper\Context;
|
||||
@ -14,6 +15,7 @@ class Data extends AbstractHelper
|
||||
const XML_PATH_RETAILCRM = 'retailcrm/';
|
||||
const XML_PATH_DEFAULT_SITE = 'retailcrm_site/default';
|
||||
const XML_PATH_SITES = 'retailcrm_sites/';
|
||||
const XML_PATH_DAEMON_COLLECTOR = 'daemon_collector/';
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
@ -45,6 +47,7 @@ class Data extends AbstractHelper
|
||||
* @param $store
|
||||
*
|
||||
* @return mixed|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getSite($store)
|
||||
{
|
||||
@ -70,6 +73,9 @@ class Data extends AbstractHelper
|
||||
return $websitesConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getMappingSites()
|
||||
{
|
||||
$sites = [];
|
||||
@ -90,6 +96,72 @@ class Data extends AbstractHelper
|
||||
return $sites;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $website
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public function getDaemonCollector($website)
|
||||
{
|
||||
$forWebsite = $this->scopeConfig->getValue(
|
||||
self::XML_PATH_RETAILCRM . self::XML_PATH_DAEMON_COLLECTOR . 'active',
|
||||
ScopeInterface::SCOPE_WEBSITES,
|
||||
$website
|
||||
);
|
||||
|
||||
if ($forWebsite) {
|
||||
return [
|
||||
'website' => $website,
|
||||
'active' => $forWebsite
|
||||
];
|
||||
}
|
||||
|
||||
$forDefault = $this->scopeConfig->getValue(
|
||||
self::XML_PATH_RETAILCRM . self::XML_PATH_DAEMON_COLLECTOR . 'active',
|
||||
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
|
||||
);
|
||||
|
||||
if ($forDefault) {
|
||||
return [
|
||||
'website' => 0,
|
||||
'active' => $forDefault
|
||||
];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $website
|
||||
*
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function getSiteKey($website)
|
||||
{
|
||||
$daemonCollector = $this->getDaemonCollector($website);
|
||||
|
||||
if ($daemonCollector === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($daemonCollector['active']) {
|
||||
if ($daemonCollector['website'] > 0) {
|
||||
return $this->scopeConfig->getValue(
|
||||
self::XML_PATH_RETAILCRM . self::XML_PATH_DAEMON_COLLECTOR . 'key',
|
||||
ScopeInterface::SCOPE_WEBSITES,
|
||||
$website
|
||||
);
|
||||
} else {
|
||||
return $this->scopeConfig->getValue(
|
||||
self::XML_PATH_RETAILCRM . self::XML_PATH_DAEMON_COLLECTOR . 'key',
|
||||
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive array filter
|
||||
*
|
||||
|
14
src/Model/Setting/DaemonCollector.php
Normal file
14
src/Model/Setting/DaemonCollector.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Retailcrm\Retailcrm\Model\Setting;
|
||||
|
||||
class DaemonCollector implements \Magento\Framework\Option\ArrayInterface
|
||||
{
|
||||
public function toOptionArray()
|
||||
{
|
||||
return [
|
||||
['value' => true, 'label' => __('Yes')],
|
||||
['value' => false, 'label' => __('No')]
|
||||
];
|
||||
}
|
||||
}
|
75
src/Test/Unit/Block/Frontend/DaemonCollectorTest.php
Normal file
75
src/Test/Unit/Block/Frontend/DaemonCollectorTest.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
class DaemonCollectorTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
private $unit;
|
||||
private $customer;
|
||||
|
||||
const SITE_KEY = 'RC-XXXXXXX-X';
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
|
||||
$context = $objectManager->getObject(\Magento\Framework\View\Element\Template\Context::class);
|
||||
$customerSession = $this->createMock(\Magento\Customer\Model\Session::class);
|
||||
$storeManager = $this->createMock(\Magento\Store\Model\StoreManager::class);
|
||||
$storeResolver = $this->createMock(\Magento\Store\Model\StoreResolver::class);
|
||||
$helper = $this->createMock(\Retailcrm\Retailcrm\Helper\Data::class);
|
||||
$this->customer = $this->createMock(\Magento\Customer\Model\Customer::class);
|
||||
$store = $this->createMock(\Magento\Store\Model\Store::class);
|
||||
|
||||
$customerSession->expects($this->any())
|
||||
->method('getCustomer')
|
||||
->willReturn($this->customer);
|
||||
$storeResolver->expects($this->any())
|
||||
->method('getCurrentStoreId')
|
||||
->willReturn(1);
|
||||
$store->expects($this->any())
|
||||
->method('getWebsiteId')
|
||||
->willReturn(1);
|
||||
$storeManager->expects($this->any())
|
||||
->method('getStore')
|
||||
->willReturn($store);
|
||||
$helper->expects($this->any())
|
||||
->method('getSiteKey')
|
||||
->willReturn(self::SITE_KEY);
|
||||
|
||||
$this->unit = new \Retailcrm\Retailcrm\Block\Frontend\DaemonCollector(
|
||||
$context,
|
||||
$customerSession,
|
||||
$storeManager,
|
||||
$storeResolver,
|
||||
$helper
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetJSWithCustomer()
|
||||
{
|
||||
$this->customer->expects($this->any())
|
||||
->method('getId')
|
||||
->willReturn(1);
|
||||
|
||||
$js = $this->unit->buildScript()->getJs();
|
||||
|
||||
$this->assertContains('<script type="text/javascript">', $js);
|
||||
$this->assertContains('</script>', $js);
|
||||
$this->assertContains('_rc(\'send\', \'pageView\');', $js);
|
||||
$this->assertContains(self::SITE_KEY, $js);
|
||||
$this->assertContains('customerId', $js);
|
||||
}
|
||||
|
||||
public function testGetJSWithoutCustomer()
|
||||
{
|
||||
$this->customer->expects($this->any())
|
||||
->method('getId')
|
||||
->willReturn(null);
|
||||
|
||||
$js = $this->unit->buildScript()->getJs();
|
||||
|
||||
$this->assertContains('<script type="text/javascript">', $js);
|
||||
$this->assertContains('</script>', $js);
|
||||
$this->assertContains('_rc(\'send\', \'pageView\');', $js);
|
||||
$this->assertContains(self::SITE_KEY, $js);
|
||||
$this->assertNotContains('customerId', $js);
|
||||
}
|
||||
}
|
@ -64,6 +64,16 @@
|
||||
<label>Setting the stores correspondence</label>
|
||||
<frontend_model>Retailcrm\Retailcrm\Block\Adminhtml\System\Config\Form\Fieldset\Sites</frontend_model>
|
||||
</group>
|
||||
<group id="daemon_collector" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="0">
|
||||
<label>Daemon Collector</label>
|
||||
<field id="active" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
|
||||
<label>Activate</label>
|
||||
<source_model>Retailcrm\Retailcrm\Model\Setting\DaemonCollector</source_model>
|
||||
</field>
|
||||
<field id="key" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
|
||||
<label>Site key</label>
|
||||
</field>
|
||||
</group>
|
||||
</section>
|
||||
</system>
|
||||
</config>
|
||||
|
@ -4,10 +4,8 @@
|
||||
<job name="create_icml" instance="Retailcrm\Retailcrm\Cron\Icml" method="execute">
|
||||
<schedule>* */4 * * *</schedule>
|
||||
</job>
|
||||
|
||||
<job name="order_hystory" instance="Retailcrm\Retailcrm\Cron\OrderHistory" method="execute">
|
||||
<schedule>*/5 * * * *</schedule>
|
||||
</job>
|
||||
|
||||
</group>
|
||||
</config>
|
@ -21,4 +21,6 @@
|
||||
"Incorrect URL of retailCRM or API key","La dirección del retailCRM o la llave API son incorrectos"
|
||||
"The selected API version is unavailable","La versión de la API seleccionada no está disponible"
|
||||
"Send","Enviar"
|
||||
"Default site","Tienda por defecto"
|
||||
"Default site","Tienda por defecto"
|
||||
"Activate","Active"
|
||||
"Site key","Clave de la página web"
|
|
@ -21,4 +21,6 @@
|
||||
"Incorrect URL of retailCRM or API key","Некорректный адрес retailCRM или API ключ"
|
||||
"The selected API version is unavailable","Выбранная версия API недоступна"
|
||||
"Send","Выгрузить"
|
||||
"Default site","Сайт по умолчанию"
|
||||
"Default site","Сайт по умолчанию"
|
||||
"Activate","Активировать"
|
||||
"Site key","Ключ сайта"
|
|
8
src/view/frontend/layout/default.xml
Normal file
8
src/view/frontend/layout/default.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0"?>
|
||||
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
|
||||
<body>
|
||||
<referenceBlock name="head.additional">
|
||||
<block class="Retailcrm\Retailcrm\Block\Frontend\DaemonCollector" name="block_name" template="Retailcrm_Retailcrm::daemon_collector.phtml" />
|
||||
</referenceBlock>
|
||||
</body>
|
||||
</page>
|
@ -3,4 +3,9 @@
|
||||
<referenceContainer name="content">
|
||||
<block class="Retailcrm\Retailcrm\Block\Display" name="retailcrm_display" template="Retailcrm_Retailcrm::sayhello.phtml" />
|
||||
</referenceContainer>
|
||||
<body>
|
||||
<referenceBlock name="head.additional">
|
||||
<block class="Retailcrm\Retailcrm\Block\Frontend\DaemonCollector" name="block_name" template="Retailcrm_Retailcrm::daemon_collector.phtml" />
|
||||
</referenceBlock>
|
||||
</body>
|
||||
</page>
|
1
src/view/frontend/templates/daemon_collector.phtml
Normal file
1
src/view/frontend/templates/daemon_collector.phtml
Normal file
@ -0,0 +1 @@
|
||||
<?php echo $block->buildScript()->getJs() ?>
|
Loading…
Reference in New Issue
Block a user