PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/GiftMessage/Helper/Message.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 335 lines | 172 code | 27 blank | 136 comment | 27 complexity | ed2127732434fe9d30046522b1d4066d MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. namespace Magento\GiftMessage\Helper;
  8. use Magento\Catalog\Model\Product\Attribute\Source\Boolean;
  9. /**
  10. * Gift Message helper
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class Message extends \Magento\Framework\App\Helper\AbstractHelper
  14. {
  15. /**
  16. * Gift messages allow section in configuration
  17. *
  18. */
  19. const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS = 'sales/gift_options/allow_items';
  20. const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER = 'sales/gift_options/allow_order';
  21. /**
  22. * Next id for edit gift message block
  23. *
  24. * @var int
  25. */
  26. protected $_nextId = 0;
  27. /**
  28. * Inner cache
  29. *
  30. * @var array
  31. */
  32. protected $_innerCache = [];
  33. /**
  34. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  35. */
  36. protected $productRepository;
  37. /**
  38. * @var \Magento\Framework\View\LayoutFactory
  39. */
  40. protected $_layoutFactory;
  41. /**
  42. * @var \Magento\GiftMessage\Model\MessageFactory
  43. */
  44. protected $_giftMessageFactory;
  45. /**
  46. * @var \Magento\Framework\Escaper
  47. */
  48. protected $_escaper;
  49. /**
  50. * Pages to skip message checks
  51. *
  52. * @var array
  53. */
  54. protected $skipMessageCheck = [];
  55. /**
  56. * @var \Magento\Store\Model\StoreManagerInterface
  57. */
  58. private $_storeManager;
  59. /**
  60. * @param \Magento\Framework\App\Helper\Context $context
  61. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  62. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  63. * @param \Magento\Framework\View\LayoutFactory $layoutFactory
  64. * @param \Magento\GiftMessage\Model\MessageFactory $giftMessageFactory
  65. * @param \Magento\Framework\Escaper $escaper
  66. * @param array $skipMessageCheck
  67. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  68. */
  69. public function __construct(
  70. \Magento\Framework\App\Helper\Context $context,
  71. \Magento\Store\Model\StoreManagerInterface $storeManager,
  72. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
  73. \Magento\Framework\View\LayoutFactory $layoutFactory,
  74. \Magento\GiftMessage\Model\MessageFactory $giftMessageFactory,
  75. \Magento\Framework\Escaper $escaper,
  76. $skipMessageCheck = []
  77. ) {
  78. $this->_escaper = $escaper;
  79. $this->productRepository = $productRepository;
  80. $this->_layoutFactory = $layoutFactory;
  81. $this->_giftMessageFactory = $giftMessageFactory;
  82. $this->skipMessageCheck = $skipMessageCheck;
  83. $this->_storeManager = $storeManager;
  84. parent::__construct(
  85. $context
  86. );
  87. }
  88. /**
  89. * Retrieve inline giftmessage edit form for specified entity
  90. *
  91. * @param string $type
  92. * @param \Magento\Framework\DataObject $entity
  93. * @param bool $dontDisplayContainer
  94. * @return string
  95. */
  96. public function getInline($type, \Magento\Framework\DataObject $entity, $dontDisplayContainer = false)
  97. {
  98. if (!$this->skipPage($type) && !$this->isMessagesAllowed($type, $entity)) {
  99. return '';
  100. }
  101. return $this->_layoutFactory->create()->createBlock(\Magento\GiftMessage\Block\Message\Inline::class)
  102. ->setId('giftmessage_form_' . $this->_nextId++)
  103. ->setDontDisplayContainer($dontDisplayContainer)
  104. ->setEntity($entity)
  105. ->setCheckoutType($type)->toHtml();
  106. }
  107. /**
  108. * @param string $pageType
  109. * @return bool
  110. */
  111. protected function skipPage($pageType)
  112. {
  113. return in_array($pageType, $this->skipMessageCheck);
  114. }
  115. /**
  116. * Check if giftmessages is allowed for specified entity.
  117. *
  118. * @param string $type
  119. * @param \Magento\Framework\DataObject $entity
  120. * @param \Magento\Store\Model\Store|int|null $store
  121. * @return bool|string|null
  122. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  123. */
  124. public function isMessagesAllowed($type, \Magento\Framework\DataObject $entity, $store = null)
  125. {
  126. if ($type == 'items') {
  127. $items = $entity->getAllItems();
  128. if (!is_array($items) || empty($items)) {
  129. return $this->scopeConfig->getValue(
  130. self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS,
  131. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  132. $store
  133. );
  134. }
  135. if ($entity instanceof \Magento\Quote\Model\Quote) {
  136. $_type = $entity->getIsMultiShipping() ? 'address_item' : 'item';
  137. } else {
  138. $_type = 'order_item';
  139. }
  140. foreach ($items as $item) {
  141. if ($item->getParentItem()) {
  142. continue;
  143. }
  144. if ($this->isMessagesAllowed($_type, $item, $store)) {
  145. return true;
  146. }
  147. }
  148. } elseif ($type == 'item') {
  149. return $this->_getDependenceFromStoreConfig($entity->getProduct()->getGiftMessageAvailable(), $store);
  150. } elseif ($type == 'order_item') {
  151. return $this->_getDependenceFromStoreConfig($entity->getGiftMessageAvailable(), $store);
  152. } elseif ($type == 'address_item') {
  153. $storeId = is_numeric($store) ? $store : $this->_storeManager->getStore($store)->getId();
  154. if (!$this->isCached('address_item_' . $entity->getProductId())) {
  155. try {
  156. $giftMessageAvailable = $this->productRepository->getById($entity->getProductId(), false, $storeId)
  157. ->getGiftMessageAvailable();
  158. } catch (\Magento\Framework\Exception\NoSuchEntityException $noEntityException) {
  159. $giftMessageAvailable = null;
  160. }
  161. $this->setCached('address_item_' . $entity->getProductId(), $giftMessageAvailable);
  162. }
  163. return $this->_getDependenceFromStoreConfig(
  164. $this->getCached('address_item_' . $entity->getProductId()),
  165. $store
  166. );
  167. } else {
  168. return $this->scopeConfig->getValue(
  169. self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER,
  170. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  171. $store
  172. );
  173. }
  174. return false;
  175. }
  176. /**
  177. * Check availablity of gift messages from store config if flag eq 2.
  178. *
  179. * @param bool $productConfig
  180. * @param \Magento\Store\Model\Store|int|null $store
  181. * @return bool|string|null
  182. */
  183. protected function _getDependenceFromStoreConfig($productConfig, $store = null)
  184. {
  185. $result = $this->scopeConfig->getValue(
  186. self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS,
  187. \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $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 (!is_null($messageId)) {
  304. $message->load($messageId);
  305. }
  306. return $message;
  307. }
  308. }