PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/GiftMessage/Model/Save.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 363 lines | 197 code | 46 blank | 120 comment | 25 complexity | 40cfad2b88ad485d33e2cad88eb04fc4 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GiftMessage\Model;
  7. /**
  8. * Adminhtml giftmessage save model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Save extends \Magento\Framework\DataObject
  13. {
  14. /**
  15. * @var bool
  16. */
  17. protected $_saved = false;
  18. /**
  19. * Gift message message
  20. *
  21. * @var \Magento\GiftMessage\Helper\Message|null
  22. */
  23. protected $_giftMessageMessage = null;
  24. /**
  25. * @var \Magento\Backend\Model\Session\Quote
  26. */
  27. protected $_session;
  28. /**
  29. * @var \Magento\GiftMessage\Model\MessageFactory
  30. */
  31. protected $_messageFactory;
  32. /**
  33. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  34. */
  35. protected $productRepository;
  36. /**
  37. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  38. * @param \Magento\GiftMessage\Model\MessageFactory $messageFactory
  39. * @param \Magento\Backend\Model\Session\Quote $session
  40. * @param \Magento\GiftMessage\Helper\Message $giftMessageMessage
  41. */
  42. public function __construct(
  43. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
  44. \Magento\GiftMessage\Model\MessageFactory $messageFactory,
  45. \Magento\Backend\Model\Session\Quote $session,
  46. \Magento\GiftMessage\Helper\Message $giftMessageMessage
  47. ) {
  48. $this->productRepository = $productRepository;
  49. $this->_messageFactory = $messageFactory;
  50. $this->_session = $session;
  51. $this->_giftMessageMessage = $giftMessageMessage;
  52. }
  53. /**
  54. * Save all seted giftmessages
  55. *
  56. * @return $this
  57. */
  58. public function saveAllInQuote()
  59. {
  60. $giftmessages = $this->getGiftmessages();
  61. if (!is_array($giftmessages)) {
  62. return $this;
  63. }
  64. foreach ($giftmessages as $entityId => $giftmessage) {
  65. $entityType = $this->getMappedType($giftmessage['type']);
  66. $this->_saveOne($entityId, $giftmessage, $entityType);
  67. }
  68. return $this;
  69. }
  70. /**
  71. * @return bool
  72. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  73. * @codeCoverageIgnore
  74. */
  75. public function getSaved()
  76. {
  77. return $this->_saved;
  78. }
  79. /**
  80. * @return $this
  81. */
  82. public function saveAllInOrder()
  83. {
  84. $giftMessages = $this->getGiftmessages();
  85. if (!is_array($giftMessages)) {
  86. return $this;
  87. }
  88. foreach ($giftMessages as $entityId => $giftMessage) {
  89. $entityType = $this->getMappedType($giftMessage['type']);
  90. $this->_saveOne($entityId, $giftMessage, $entityType);
  91. }
  92. return $this;
  93. }
  94. /**
  95. * Save a single gift message
  96. *
  97. * @param int $entityId
  98. * @param array $giftmessage
  99. * @param string $entityType
  100. * @return $this
  101. */
  102. protected function _saveOne($entityId, $giftmessage, $entityType)
  103. {
  104. /* @var $giftmessageModel \Magento\GiftMessage\Model\Message */
  105. $giftmessageModel = $this->_messageFactory->create();
  106. switch ($entityType) {
  107. case 'quote':
  108. $entityModel = $this->_getQuote();
  109. break;
  110. case 'quote_item':
  111. $entityModel = $this->_getQuote()->getItemById($entityId);
  112. break;
  113. default:
  114. $entityModel = $giftmessageModel->getEntityModelByType($entityType)->load($entityId);
  115. break;
  116. }
  117. if (!$entityModel) {
  118. return $this;
  119. }
  120. if ($entityModel->getGiftMessageId()) {
  121. $giftmessageModel->load($entityModel->getGiftMessageId());
  122. }
  123. $giftmessageModel->addData($giftmessage);
  124. if ($giftmessageModel->isMessageEmpty() && $giftmessageModel->getId()) {
  125. // remove empty giftmessage
  126. $this->_deleteOne($entityModel, $giftmessageModel);
  127. $this->_saved = false;
  128. } elseif (!$giftmessageModel->isMessageEmpty()) {
  129. $giftmessageModel->save();
  130. $entityModel->setGiftMessageId($giftmessageModel->getId());
  131. if ($entityType != 'quote') {
  132. $entityModel->save();
  133. }
  134. $this->_saved = true;
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Delete a single gift message from entity
  140. *
  141. * @param \Magento\Framework\DataObject $entityModel
  142. * @param \Magento\GiftMessage\Model\Message|null $giftmessageModel
  143. * @return $this
  144. */
  145. protected function _deleteOne($entityModel, $giftmessageModel = null)
  146. {
  147. if ($giftmessageModel === null) {
  148. $giftmessageModel = $this->_messageFactory->create()->load($entityModel->getGiftMessageId());
  149. }
  150. $giftmessageModel->delete();
  151. $entityModel->setGiftMessageId(0)->save();
  152. return $this;
  153. }
  154. /**
  155. * Set allowed quote items for gift messages
  156. *
  157. * @param array $items
  158. * @return $this
  159. */
  160. public function setAllowQuoteItems($items)
  161. {
  162. $this->_session->setAllowQuoteItemsGiftMessage($items);
  163. return $this;
  164. }
  165. /**
  166. * Add allowed quote item for gift messages
  167. *
  168. * @param int $item
  169. * @return $this
  170. */
  171. public function addAllowQuoteItem($item)
  172. {
  173. $items = $this->getAllowQuoteItems();
  174. if (!in_array($item, $items)) {
  175. $items[] = $item;
  176. }
  177. $this->setAllowQuoteItems($items);
  178. return $this;
  179. }
  180. /**
  181. * Retrieve allowed quote items for gift messages
  182. *
  183. * @return array
  184. */
  185. public function getAllowQuoteItems()
  186. {
  187. if (!is_array($this->_session->getAllowQuoteItemsGiftMessage())) {
  188. $this->setAllowQuoteItems([]);
  189. }
  190. return $this->_session->getAllowQuoteItemsGiftMessage();
  191. }
  192. /**
  193. * Retrieve allowed quote items products for gift messages
  194. *
  195. * @return array
  196. */
  197. public function getAllowQuoteItemsProducts()
  198. {
  199. $result = [];
  200. foreach ($this->getAllowQuoteItems() as $itemId) {
  201. $item = $this->_getQuote()->getItemById($itemId);
  202. if (!$item) {
  203. continue;
  204. }
  205. $result[] = $item->getProduct()->getId();
  206. }
  207. return $result;
  208. }
  209. /**
  210. * Checks allowed quote item for gift messages
  211. *
  212. * @param \Magento\Framework\DataObject $item
  213. * @return bool
  214. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  215. */
  216. public function getIsAllowedQuoteItem($item)
  217. {
  218. if (!in_array($item->getId(), $this->getAllowQuoteItems())) {
  219. if ($item->getGiftMessageId() && $this->isGiftMessagesAvailable($item)) {
  220. $this->addAllowQuoteItem($item->getId());
  221. return true;
  222. }
  223. return false;
  224. }
  225. return true;
  226. }
  227. /**
  228. * Retrieve is gift message available for item (product)
  229. *
  230. * @param \Magento\Framework\DataObject $item
  231. * @return bool
  232. */
  233. public function isGiftMessagesAvailable($item)
  234. {
  235. return $this->_giftMessageMessage->isMessagesAllowed('item', $item, $item->getStore());
  236. }
  237. /**
  238. * Imports quote items for gift messages from products data
  239. *
  240. * @param mixed $products
  241. * @return $this
  242. */
  243. public function importAllowQuoteItemsFromProducts($products)
  244. {
  245. $allowedItems = $this->getAllowQuoteItems();
  246. $deleteAllowedItems = [];
  247. foreach ($products as $productId => $data) {
  248. $product = $this->productRepository->getById($productId, false, $this->_session->getStore()->getId());
  249. $item = $this->_getQuote()->getItemByProduct($product);
  250. if (!$item) {
  251. continue;
  252. }
  253. if (in_array($item->getId(), $allowedItems) && !isset($data['giftmessage'])) {
  254. $deleteAllowedItems[] = $item->getId();
  255. } elseif (!in_array($item->getId(), $allowedItems) && isset($data['giftmessage'])) {
  256. $allowedItems[] = $item->getId();
  257. }
  258. }
  259. $allowedItems = array_diff($allowedItems, $deleteAllowedItems);
  260. $this->setAllowQuoteItems($allowedItems);
  261. return $this;
  262. }
  263. /**
  264. * @param mixed $items
  265. * @return $this
  266. */
  267. public function importAllowQuoteItemsFromItems($items)
  268. {
  269. $allowedItems = $this->getAllowQuoteItems();
  270. $deleteAllowedItems = [];
  271. foreach ($items as $itemId => $data) {
  272. $item = $this->_getQuote()->getItemById($itemId);
  273. if (!$item) {
  274. // Clean not exists items
  275. $deleteAllowedItems[] = $itemId;
  276. continue;
  277. }
  278. if (in_array($item->getId(), $allowedItems) && !isset($data['giftmessage'])) {
  279. $deleteAllowedItems[] = $item->getId();
  280. } elseif (!in_array($item->getId(), $allowedItems) && isset($data['giftmessage'])) {
  281. $allowedItems[] = $item->getId();
  282. }
  283. }
  284. $allowedItems = array_diff($allowedItems, $deleteAllowedItems);
  285. $this->setAllowQuoteItems($allowedItems);
  286. return $this;
  287. }
  288. /**
  289. * Retrieve mapped type for entity
  290. *
  291. * @param string $type
  292. * @return string|null
  293. */
  294. protected function getMappedType($type)
  295. {
  296. $map = [
  297. 'main' => 'quote',
  298. 'item' => 'quote_item',
  299. 'order' => 'order',
  300. 'order_item' => 'order_item',
  301. ];
  302. if (isset($map[$type])) {
  303. return $map[$type];
  304. }
  305. return null;
  306. }
  307. /**
  308. * Retrieve quote object
  309. *
  310. * @return \Magento\Quote\Model\Quote
  311. * @codeCoverageIgnore
  312. */
  313. protected function _getQuote()
  314. {
  315. return $this->_session->getQuote();
  316. }
  317. }