Message.php 10 KB

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