|
7 | 7 |
|
8 | 8 | namespace Itonomy\Flowbox\Block;
|
9 | 9 |
|
| 10 | +/** |
| 11 | + * Class Base |
| 12 | + * @package Itonomy\Flowbox\Block |
| 13 | + */ |
10 | 14 | abstract class Base extends \Magento\Framework\View\Element\Template
|
11 | 15 | {
|
12 |
| - const XML_PATH_FLOWBOX_ENABLE = 'itonomy_flowbox/general/enable'; |
| 16 | + const XML_CONFIG_ENABLE = 'itonomy_flowbox/general/enable'; |
| 17 | + |
| 18 | + const XML_CONFIG_THIRD_PARTY_COOKIE_MGR = 'itonomy_flowbox/general/third_party_cookie_mgr'; |
| 19 | + |
| 20 | + const XML_CONFIG_DEBUG_JAVASCRIPT = 'itonomy_flowbox/general/debug_javascript'; |
| 21 | + |
| 22 | + const XML_CONFIG_API_KEY = 'itonomy_flowbox/general/api_key'; |
13 | 23 |
|
14 |
| - const XML_PATH_FLOWBOX_DEBUG_JS = 'itonomy_flowbox/general/debug_javascript'; |
| 24 | + const XML_CONFIG_PRODUCT_ID_ATTR = 'itonomy_flowbox/general/product_id_attribute'; |
15 | 25 |
|
16 |
| - const XML_PATH_API_KEY = 'itonomy_flowbox/general/api_key'; |
| 26 | + const XML_CONFIG_PRODUCT_ID_ATTR_CUSTOM = 'itonomy_flowbox/general/product_id_attribute_custom'; |
17 | 27 |
|
18 |
| - const FLOW_TYPE_DEFAULT = 'default'; |
| 28 | + const XML_CONFIG_TAGBAR_INPUT_TYPE = 'itonomy_flowbox/general/tagbar_input_type'; |
19 | 29 |
|
20 |
| - const FLOW_TYPE_DYNAMIC_PRODUCT = 'dynamic-product'; |
| 30 | + const XML_CONFIG_SHOW_HASHES = 'itonomy_flowbox/general/show_hashes'; |
21 | 31 |
|
22 |
| - const FLOW_TYPE_DYNAMIC_TAG = 'dynamic-tag'; |
23 | 32 |
|
24 | 33 | /**
|
25 | 34 | * @var \Magento\Framework\Encryption\EncryptorInterface
|
26 | 35 | */
|
27 | 36 | private $encryptor;
|
| 37 | + /** |
| 38 | + * @var \Magento\Catalog\Api\ProductRepositoryInterface |
| 39 | + */ |
| 40 | + private $productRepository; |
| 41 | + /** |
| 42 | + * @var \Magento\Framework\Api\SearchCriteriaBuilder |
| 43 | + */ |
| 44 | + private $searchCriteriaBuilder; |
| 45 | + /** |
| 46 | + * @var array |
| 47 | + */ |
| 48 | + private $errors = []; |
| 49 | + /** |
| 50 | + * @var \Magento\Cookie\Helper\Cookie |
| 51 | + */ |
| 52 | + private $cookieHelper; |
| 53 | + /** |
| 54 | + * @var \Itonomy\Flowbox\Helper\Data |
| 55 | + */ |
| 56 | + protected $dataHelper; |
28 | 57 |
|
29 | 58 | /**
|
30 | 59 | * Base constructor.
|
31 | 60 | * @param \Magento\Framework\View\Element\Template\Context $context
|
32 | 61 | * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
|
| 62 | + * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository |
| 63 | + * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder |
| 64 | + * @param \Magento\Cookie\Helper\Cookie $cookieHelper |
| 65 | + * @param \Itonomy\Flowbox\Helper\Data $dataHelper |
33 | 66 | * @param array $data
|
34 | 67 | */
|
35 | 68 | public function __construct(
|
36 | 69 | \Magento\Framework\View\Element\Template\Context $context,
|
37 | 70 | \Magento\Framework\Encryption\EncryptorInterface $encryptor,
|
| 71 | + \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, |
| 72 | + \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, |
| 73 | + \Magento\Cookie\Helper\Cookie $cookieHelper, |
| 74 | + \Itonomy\Flowbox\Helper\Data $dataHelper, |
38 | 75 | array $data = []
|
39 | 76 | ) {
|
40 |
| - \Magento\Framework\View\Element\Template::__construct($context, $data); |
| 77 | + parent::__construct($context, $data); |
| 78 | + |
| 79 | + $this->cookieHelper = $cookieHelper; |
41 | 80 | $this->encryptor = $encryptor;
|
| 81 | + $this->productRepository = $productRepository; |
| 82 | + $this->searchCriteriaBuilder = $searchCriteriaBuilder; |
| 83 | + $this->dataHelper = $dataHelper; |
42 | 84 | }
|
43 | 85 |
|
44 | 86 | /**
|
45 | 87 | * @return bool
|
46 | 88 | */
|
47 | 89 | public function isFlowboxEnabled(): bool
|
48 | 90 | {
|
49 |
| - return $this->_scopeConfig->isSetFlag(static::XML_PATH_FLOWBOX_ENABLE); |
| 91 | + return $this->_scopeConfig->isSetFlag(self::XML_CONFIG_ENABLE); |
50 | 92 | }
|
51 | 93 |
|
52 | 94 | /**
|
| 95 | + * @return string |
| 96 | + */ |
| 97 | + public function getJsConfig(): string |
| 98 | + { |
| 99 | + $errors = $this->getErrors(); |
| 100 | + if (\count($errors)) { |
| 101 | + $this->setData('errors', $errors); |
| 102 | + } |
| 103 | + return $this->toJson(['flowbox', 'errors']); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Checks if user is allowed to save cookies |
| 108 | + * |
| 109 | + * True if any of the following conditions are met: |
| 110 | + * - A third party cookie manager is in use; |
| 111 | + * - Magento Cookie Restriction Mode is disabled or it allows the user to |
| 112 | + * save cookies. |
| 113 | + * |
53 | 114 | * @return bool
|
54 | 115 | */
|
55 |
| - public function isDebugJavaScript(): bool |
| 116 | + protected function isUserAllowSaveCookie(): bool |
56 | 117 | {
|
57 |
| - return $this->_scopeConfig->isSetFlag(static::XML_PATH_FLOWBOX_DEBUG_JS); |
| 118 | + return ($this->_scopeConfig->isSetFlag(self::XML_CONFIG_THIRD_PARTY_COOKIE_MGR) |
| 119 | + || false === $this->cookieHelper->isUserNotAllowSaveCookie()); |
58 | 120 | }
|
59 | 121 |
|
60 | 122 | /**
|
| 123 | + * Return product identifier |
61 | 124 | * @return string
|
| 125 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
62 | 126 | */
|
63 |
| - public function getApiKey(): string |
| 127 | + protected function getProductIdentifier(): ?string |
64 | 128 | {
|
65 |
| - return $this->encryptor->decrypt( |
66 |
| - $this->_scopeConfig->getValue(static::XML_PATH_API_KEY) |
| 129 | + // Return product ID attribute value for currently viewed product |
| 130 | + return $this->getProductIdAttributeValue( |
| 131 | + $this->getProductIdAttribute(), |
| 132 | + $this->getRequest()->getParam('id') |
67 | 133 | );
|
68 | 134 | }
|
69 | 135 |
|
70 | 136 | /**
|
71 |
| - * Return json configuration for javascript component |
72 |
| - * @return string |
| 137 | + * @return \Magento\Catalog\Api\Data\ProductInterface |
| 138 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
73 | 139 | */
|
74 |
| - public function getJsConfig(): string |
| 140 | + protected function getProduct(): \Magento\Catalog\Api\Data\ProductInterface |
75 | 141 | {
|
76 |
| - if (!$this->hasData('flowbox')) { |
77 |
| - $this->unsetData('errors'); |
78 |
| - $this->prepareConfig(); |
79 |
| - } |
80 |
| - if ($this->hasData('errors')) { |
81 |
| - return $this->toJson(['errors']); |
82 |
| - } |
83 |
| - return $this->toJson(['flowbox']); |
| 142 | + return $this->productRepository->getById( |
| 143 | + $this->getRequest()->getParam('id') |
| 144 | + ); |
84 | 145 | }
|
85 | 146 |
|
86 | 147 | /**
|
87 |
| - * Prepare configuration for javascript component |
88 |
| - * |
89 |
| - * You should set an array 'flowbox' containing configuration, or an array |
90 |
| - * 'errors' containing error messages. |
| 148 | + * @return bool |
| 149 | + */ |
| 150 | + protected function isDebugJavaScript(): bool |
| 151 | + { |
| 152 | + return $this->_scopeConfig->isSetFlag(self::XML_CONFIG_DEBUG_JAVASCRIPT); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @return string|null |
| 157 | + */ |
| 158 | + protected function getApiKey(): ?string |
| 159 | + { |
| 160 | + return $this->encryptor->decrypt( |
| 161 | + $this->_scopeConfig->getValue(self::XML_CONFIG_API_KEY) |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @return string|null |
91 | 167 | */
|
92 |
| - abstract protected function prepareConfig(): void; |
| 168 | + protected function getProductIdAttribute(): ?string |
| 169 | + { |
| 170 | + $value = $this->_scopeConfig->getValue(self::XML_CONFIG_PRODUCT_ID_ATTR); |
| 171 | + if ($value === \Itonomy\Flowbox\Model\Config\Source\ProductIdentifier::CUSTOM) { |
| 172 | + $value = $this->_scopeConfig->getValue(self::XML_CONFIG_PRODUCT_ID_ATTR_CUSTOM); |
| 173 | + } |
| 174 | + return $value; |
| 175 | + } |
93 | 176 |
|
94 | 177 | /**
|
95 | 178 | * @param string $message
|
96 | 179 | */
|
97 | 180 | protected function addError(string $message): void
|
98 | 181 | {
|
99 |
| - if (!$this->hasData('errors')) { |
100 |
| - $this->_data['errors'] = []; |
| 182 | + $this->errors[] = $message; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * @return string[] |
| 187 | + */ |
| 188 | + protected function getErrors(): array |
| 189 | + { |
| 190 | + return $this->errors; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * @param $attributeCode |
| 195 | + * @param $productId |
| 196 | + * @return string |
| 197 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 198 | + */ |
| 199 | + private function getProductIdAttributeValue($attributeCode, $productId): string |
| 200 | + { |
| 201 | + $searchCriteria = $this->searchCriteriaBuilder->addFilter('entity_id', $productId)->setPageSize(1); |
| 202 | + |
| 203 | + $products = $this->productRepository->getList( |
| 204 | + $searchCriteria->create() |
| 205 | + )->getItems(); |
| 206 | + |
| 207 | + $product = \reset($products); |
| 208 | + if (!$product instanceof \Magento\Catalog\Api\Data\ProductInterface) { |
| 209 | + throw new \Magento\Framework\Exception\NoSuchEntityException( |
| 210 | + __('No product found with entity_id=%product_id', ['product_id' => $productId]) |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + $value = (string) $product->getData($attributeCode); |
| 215 | + if (empty($value)) { |
| 216 | + throw new \Magento\Framework\Exception\NoSuchEntityException( |
| 217 | + __( |
| 218 | + 'Attribute with attribute_code=%attribute_code not set on product with entity_id=%product_id', |
| 219 | + ['attribute_code' => $attributeCode, 'product_id' => $productId] |
| 220 | + ) |
| 221 | + ); |
101 | 222 | }
|
102 |
| - $this->_data['errors'][] = $message; |
| 223 | + |
| 224 | + return $value; |
103 | 225 | }
|
104 | 226 | }
|
0 commit comments