188 lines
6.6 KiB
PHP
188 lines
6.6 KiB
PHP
<?php
|
|
/**
|
|
* @package Plumrocket_CookieConsent
|
|
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
|
* @license https://plumrocket.com/license End-user License Agreement
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Plumrocket\CookieConsent\Block;
|
|
|
|
use Magento\Cms\Model\Template\FilterProvider;
|
|
use Magento\Framework\Serialize\SerializerInterface;
|
|
use Magento\Framework\View\Element\Template;
|
|
use Magento\Framework\View\Element\Template\Context;
|
|
use Plumrocket\CookieConsent\Api\CanManageCookieInterface;
|
|
use Plumrocket\CookieConsent\Api\Data\CategoryInterface;
|
|
use Plumrocket\CookieConsent\Api\Data\CookieInterface;
|
|
use Plumrocket\CookieConsent\Helper\Config\SettingsBar as SettingsBarConfig;
|
|
use Plumrocket\CookieConsent\ViewModel\CategoryProvider;
|
|
use Plumrocket\CookieConsent\ViewModel\CookieProvider;
|
|
use Plumrocket\CookieConsent\ViewModel\ExtractCategoryData;
|
|
use Plumrocket\CookieConsent\ViewModel\ExtractCookieData;
|
|
|
|
/**
|
|
* @since 1.0.0
|
|
*/
|
|
class Settings extends Template
|
|
{
|
|
/**
|
|
* @var \Magento\Framework\Serialize\SerializerInterface
|
|
*/
|
|
private $serializer;
|
|
|
|
/**
|
|
* @var \Plumrocket\CookieConsent\ViewModel\ExtractCookieData
|
|
*/
|
|
private $extractCookieData;
|
|
|
|
/**
|
|
* @var \Plumrocket\CookieConsent\ViewModel\ExtractCategoryData
|
|
*/
|
|
private $extractCategoryData;
|
|
|
|
/**
|
|
* @var \Plumrocket\CookieConsent\Helper\Config\SettingsBar
|
|
*/
|
|
private $settingsBarConfig;
|
|
|
|
/**
|
|
* @var \Magento\Cms\Model\Template\FilterProvider
|
|
*/
|
|
private $filterProvider;
|
|
|
|
/**
|
|
* @var \Plumrocket\CookieConsent\ViewModel\CategoryProvider
|
|
*/
|
|
private $categoryProvider;
|
|
|
|
/**
|
|
* @var \Plumrocket\CookieConsent\ViewModel\CookieProvider
|
|
*/
|
|
private $cookieProvider;
|
|
|
|
/**
|
|
* @param \Magento\Framework\View\Element\Template\Context $context
|
|
* @param \Magento\Framework\Serialize\SerializerInterface $serializer
|
|
* @param \Plumrocket\CookieConsent\ViewModel\ExtractCookieData $extractCookieData
|
|
* @param \Plumrocket\CookieConsent\ViewModel\ExtractCategoryData $extractCategoryData
|
|
* @param \Plumrocket\CookieConsent\Helper\Config\SettingsBar $settingsBarConfig
|
|
* @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
|
|
* @param \Plumrocket\CookieConsent\ViewModel\CategoryProvider $categoryProvider
|
|
* @param \Plumrocket\CookieConsent\ViewModel\CookieProvider $cookieProvider
|
|
* @param array $data
|
|
*/
|
|
public function __construct(
|
|
Context $context,
|
|
SerializerInterface $serializer,
|
|
ExtractCookieData $extractCookieData,
|
|
ExtractCategoryData $extractCategoryData,
|
|
SettingsBarConfig $settingsBarConfig,
|
|
FilterProvider $filterProvider,
|
|
CategoryProvider $categoryProvider,
|
|
CookieProvider $cookieProvider,
|
|
array $data = []
|
|
) {
|
|
parent::__construct($context, $data);
|
|
$this->serializer = $serializer;
|
|
$this->extractCookieData = $extractCookieData;
|
|
$this->extractCategoryData = $extractCategoryData;
|
|
$this->settingsBarConfig = $settingsBarConfig;
|
|
$this->filterProvider = $filterProvider;
|
|
$this->categoryProvider = $categoryProvider;
|
|
$this->cookieProvider = $cookieProvider;
|
|
}
|
|
|
|
/**
|
|
* Get configurations for setting panel.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getJsComponentConfig(): array
|
|
{
|
|
return [
|
|
'categories' => $this->getCategoriesData(),
|
|
'cookies' => $this->getCookiesData(),
|
|
'displayStyle' => $this->settingsBarConfig->getDisplayStyle(),
|
|
'canShowCookieDetails' => $this->settingsBarConfig->canShowCookieDetails(),
|
|
'overview' => [
|
|
'title' => $this->settingsBarConfig->getOverviewTitle(),
|
|
'text' => $this->filterProvider->getPageFilter()->filter($this->settingsBarConfig->getOverviewText()),
|
|
],
|
|
'consentPreferences' => [
|
|
'header' => __($this->settingsBarConfig->getConsentPreferencesHeader()),
|
|
'essential_category_status' => __($this->settingsBarConfig->getEssentialCategoryStatus()),
|
|
'cookie_details_link' => __($this->settingsBarConfig->getCookieDetailsLinkTitle()),
|
|
],
|
|
'design' => [
|
|
'textColor' => $this->settingsBarConfig->getTextColor(),
|
|
'backgroundColor' => $this->settingsBarConfig->getBackgroundColor(),
|
|
],
|
|
'acceptButtonConfig' => $this->prepareButtonConfig($this->settingsBarConfig->getAcceptButtonConfig()),
|
|
'declineButtonConfig' => $this->prepareButtonConfig($this->settingsBarConfig->getDeclineButtonConfig()),
|
|
'confirmButtonConfig' => $this->prepareButtonConfig($this->settingsBarConfig->getConfirmButtonConfig()),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get js layout.
|
|
*
|
|
* @return false|string
|
|
*/
|
|
public function getJsLayout()
|
|
{
|
|
if (isset($this->jsLayout['components']['pr-cookie-settings-bar'])) {
|
|
$this->jsLayout['components']['pr-cookie-settings-bar'] = array_merge_recursive(
|
|
$this->jsLayout['components']['pr-cookie-settings-bar'],
|
|
$this->getJsComponentConfig()
|
|
);
|
|
} else {
|
|
$this->jsLayout['components']['pr-cookie-settings-bar'] = $this->getJsComponentConfig();
|
|
}
|
|
|
|
return $this->jsLayout ? $this->serializer->serialize($this->jsLayout) : '';
|
|
}
|
|
|
|
/**
|
|
* Get categories data for render.
|
|
*
|
|
* @return array[]
|
|
*/
|
|
private function getCategoriesData(): array
|
|
{
|
|
$extractCategoryData = $this->extractCategoryData;
|
|
return array_values(array_map(static function (CategoryInterface $category) use ($extractCategoryData) {
|
|
return $extractCategoryData->execute($category);
|
|
}, $this->categoryProvider->get()));
|
|
}
|
|
|
|
/**
|
|
* Get cookie data for render.
|
|
*
|
|
* @return array[]
|
|
*/
|
|
private function getCookiesData(): array
|
|
{
|
|
$extractCookieData = $this->extractCookieData;
|
|
return array_values(array_map(static function (CookieInterface $cookie) use ($extractCookieData) {
|
|
return $extractCookieData->execute($cookie);
|
|
}, $this->cookieProvider->get()));
|
|
}
|
|
|
|
/**
|
|
* Correct button config.
|
|
*
|
|
* @param array $config
|
|
* @return array
|
|
*/
|
|
private function prepareButtonConfig(array $config): array
|
|
{
|
|
if (isset($config['enabled'])) {
|
|
$config['enabled'] = (bool) $config['enabled'];
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
}
|