1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GiftMessage\Helper;
use Magento\Catalog\Model\Product\Attribute\Source\Boolean;
/**
* Gift Message helper
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Message extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* Gift messages allow section in configuration
*
*/
const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS = 'sales/gift_options/allow_items';
const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER = 'sales/gift_options/allow_order';
/**
* Next id for edit gift message block
*
* @var int
*/
protected $_nextId = 0;
/**
* Inner cache
*
* @var array
*/
protected $_innerCache = [];
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var \Magento\Framework\View\LayoutFactory
*/
protected $_layoutFactory;
/**
* @var \Magento\GiftMessage\Model\MessageFactory
*/
protected $_giftMessageFactory;
/**
* @var \Magento\Framework\Escaper
*/
protected $_escaper;
/**
* Pages to skip message checks
*
* @var array
*/
protected $skipMessageCheck = [];
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $_storeManager;
/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
* @param \Magento\GiftMessage\Model\MessageFactory $giftMessageFactory
* @param \Magento\Framework\Escaper $escaper
* @param array $skipMessageCheck
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Framework\View\LayoutFactory $layoutFactory,
\Magento\GiftMessage\Model\MessageFactory $giftMessageFactory,
\Magento\Framework\Escaper $escaper,
$skipMessageCheck = []
) {
$this->_escaper = $escaper;
$this->productRepository = $productRepository;
$this->_layoutFactory = $layoutFactory;
$this->_giftMessageFactory = $giftMessageFactory;
$this->skipMessageCheck = $skipMessageCheck;
$this->_storeManager = $storeManager;
parent::__construct(
$context
);
}
/**
* Retrieve inline giftmessage edit form for specified entity
*
* @param string $type
* @param \Magento\Framework\DataObject $entity
* @param bool $dontDisplayContainer
* @return string
*/
public function getInline($type, \Magento\Framework\DataObject $entity, $dontDisplayContainer = false)
{
if (!$this->skipPage($type) && !$this->isMessagesAllowed($type, $entity)) {
return '';
}
return $this->_layoutFactory->create()->createBlock(\Magento\GiftMessage\Block\Message\Inline::class)
->setId('giftmessage_form_' . $this->_nextId++)
->setDontDisplayContainer($dontDisplayContainer)
->setEntity($entity)
->setCheckoutType($type)->toHtml();
}
/**
* @param string $pageType
* @return bool
*/
protected function skipPage($pageType)
{
return in_array($pageType, $this->skipMessageCheck);
}
/**
* Check if giftmessages is allowed for specified entity.
*
* @param string $type
* @param \Magento\Framework\DataObject $entity
* @param \Magento\Store\Model\Store|int|null $store
* @return bool|string|null
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function isMessagesAllowed($type, \Magento\Framework\DataObject $entity, $store = null)
{
if ($type == 'items') {
$items = $entity->getAllItems();
if (!is_array($items) || empty($items)) {
return $this->scopeConfig->getValue(
self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
);
}
if ($entity instanceof \Magento\Quote\Model\Quote) {
$_type = $entity->getIsMultiShipping() ? 'address_item' : 'item';
} else {
$_type = 'order_item';
}
foreach ($items as $item) {
if ($item->getParentItem()) {
continue;
}
if ($this->isMessagesAllowed($_type, $item, $store)) {
return true;
}
}
} elseif ($type == 'item') {
return $this->_getDependenceFromStoreConfig($entity->getProduct()->getGiftMessageAvailable(), $store);
} elseif ($type == 'order_item') {
return $this->_getDependenceFromStoreConfig($entity->getGiftMessageAvailable(), $store);
} elseif ($type == 'address_item') {
$storeId = is_numeric($store) ? $store : $this->_storeManager->getStore($store)->getId();
if (!$this->isCached('address_item_' . $entity->getProductId())) {
try {
$giftMessageAvailable = $this->productRepository->getById($entity->getProductId(), false, $storeId)
->getGiftMessageAvailable();
} catch (\Magento\Framework\Exception\NoSuchEntityException $noEntityException) {
$giftMessageAvailable = null;
}
$this->setCached('address_item_' . $entity->getProductId(), $giftMessageAvailable);
}
return $this->_getDependenceFromStoreConfig(
$this->getCached('address_item_' . $entity->getProductId()),
$store
);
} else {
return $this->scopeConfig->getValue(
self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
);
}
return false;
}
/**
* Check availablity of gift messages from store config if flag eq 2.
*
* @param bool $productConfig
* @param \Magento\Store\Model\Store|int|null $store
* @return bool|string|null
*/
protected function _getDependenceFromStoreConfig($productConfig, $store = null)
{
$result = $this->scopeConfig->getValue(
self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
);
if ($productConfig === null || '' === $productConfig || $productConfig == Boolean::VALUE_USE_CONFIG) {
return $result;
} else {
return $productConfig;
}
}
/**
* Retrieve escaped and preformated gift message text for specified entity
*
* @param \Magento\Framework\DataObject $entity
* @return string|null
*/
public function getEscapedGiftMessage(\Magento\Framework\DataObject $entity)
{
$message = $this->getGiftMessageForEntity($entity);
if ($message) {
return nl2br($this->_escaper->escapeHtml($message->getMessage()));
}
return null;
}
/**
* Retrieve gift message for entity. If message not exists return null
*
* @param \Magento\Framework\DataObject $entity
* @return \Magento\GiftMessage\Model\Message
*/
public function getGiftMessageForEntity(\Magento\Framework\DataObject $entity)
{
if ($entity->getGiftMessageId() && !$entity->getGiftMessage()) {
$message = $this->getGiftMessage($entity->getGiftMessageId());
$entity->setGiftMessage($message);
}
return $entity->getGiftMessage();
}
/**
* Retrieve internal cached data with specified key.
*
* If cached data not found return null.
*
* @param string $key
* @return mixed
*/
public function getCached($key)
{
if ($this->isCached($key)) {
return $this->_innerCache[$key];
}
return null;
}
/**
* Check availability for internal cached data with specified key
*
* @param string $key
* @return bool
*/
public function isCached($key)
{
return isset($this->_innerCache[$key]);
}
/**
* Set internal cache data with specified key
*
* @param string $key
* @param mixed $value
* @return $this
*/
public function setCached($key, $value)
{
$this->_innerCache[$key] = $value;
return $this;
}
/**
* Check availability for onepage checkout items
*
* @param array $quote
* @param \Magento\Store\Model\Store|int|null $store
* @return bool
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
*/
public function getAvailableForQuoteItems($quote, $store = null)
{
foreach ($quote->getAllItems() as $item) {
if ($this->isMessagesAllowed('item', $item, $store)) {
return true;
}
}
return false;
}
/**
* Check availability for multishipping checkout items
*
* @param array $items
* @param \Magento\Store\Model\Store|int|null $store
* @return bool
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
*/
public function getAvailableForAddressItems($items, $store = null)
{
foreach ($items as $item) {
if ($this->isMessagesAllowed('address_item', $item, $store)) {
return true;
}
}
return false;
}
/**
* Retrieve gift message with specified id
*
* @param int $messageId
* @return \Magento\GiftMessage\Model\Message
*/
public function getGiftMessage($messageId = null)
{
$message = $this->_giftMessageFactory->create();
if ($messageId !== null) {
$message->load($messageId);
}
return $message;
}
}