Update.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Controller\Index;
  7. use Magento\Framework\App\Action;
  8. use Magento\Framework\App\Action\HttpPostActionInterface;
  9. use Magento\Framework\Exception\NotFoundException;
  10. use Magento\Framework\Controller\ResultFactory;
  11. /**
  12. * Class Update
  13. */
  14. class Update extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPostActionInterface
  15. {
  16. /**
  17. * @var \Magento\Wishlist\Controller\WishlistProviderInterface
  18. */
  19. protected $wishlistProvider;
  20. /**
  21. * @var \Magento\Framework\Data\Form\FormKey\Validator
  22. */
  23. protected $_formKeyValidator;
  24. /**
  25. * @var \Magento\Wishlist\Model\LocaleQuantityProcessor
  26. */
  27. protected $quantityProcessor;
  28. /**
  29. * @param Action\Context $context
  30. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  31. * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
  32. * @param \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor
  33. */
  34. public function __construct(
  35. Action\Context $context,
  36. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
  37. \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
  38. \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor
  39. ) {
  40. $this->_formKeyValidator = $formKeyValidator;
  41. $this->wishlistProvider = $wishlistProvider;
  42. $this->quantityProcessor = $quantityProcessor;
  43. parent::__construct($context);
  44. }
  45. /**
  46. * Update wishlist item comments
  47. *
  48. * @return \Magento\Framework\Controller\Result\Redirect
  49. * @throws NotFoundException
  50. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  51. * @SuppressWarnings(PHPMD.NPathComplexity)
  52. */
  53. public function execute()
  54. {
  55. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  56. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  57. if (!$this->_formKeyValidator->validate($this->getRequest())) {
  58. $resultRedirect->setPath('*/*/');
  59. return $resultRedirect;
  60. }
  61. $wishlist = $this->wishlistProvider->getWishlist();
  62. if (!$wishlist) {
  63. throw new NotFoundException(__('Page not found.'));
  64. }
  65. $post = $this->getRequest()->getPostValue();
  66. if ($post && isset($post['description']) && is_array($post['description'])) {
  67. $updatedItems = 0;
  68. foreach ($post['description'] as $itemId => $description) {
  69. $item = $this->_objectManager->create(\Magento\Wishlist\Model\Item::class)->load($itemId);
  70. if ($item->getWishlistId() != $wishlist->getId()) {
  71. continue;
  72. }
  73. // Extract new values
  74. $description = (string)$description;
  75. if ($description == $this->_objectManager->get(
  76. \Magento\Wishlist\Helper\Data::class
  77. )->defaultCommentString()
  78. ) {
  79. $description = '';
  80. }
  81. $qty = null;
  82. if (isset($post['qty'][$itemId])) {
  83. $qty = $this->quantityProcessor->process($post['qty'][$itemId]);
  84. }
  85. if ($qty === null) {
  86. $qty = $item->getQty();
  87. if (!$qty) {
  88. $qty = 1;
  89. }
  90. } elseif (0 == $qty) {
  91. try {
  92. $item->delete();
  93. } catch (\Exception $e) {
  94. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  95. $this->messageManager->addError(__('We can\'t delete item from Wish List right now.'));
  96. }
  97. }
  98. // Check that we need to save
  99. if ($item->getDescription() == $description && $item->getQty() == $qty) {
  100. continue;
  101. }
  102. try {
  103. $item->setDescription($description)->setQty($qty)->save();
  104. $this->messageManager->addSuccessMessage(
  105. __('%1 has been updated in your Wish List.', $item->getProduct()->getName())
  106. );
  107. $updatedItems++;
  108. } catch (\Exception $e) {
  109. $this->messageManager->addError(
  110. __(
  111. 'Can\'t save description %1',
  112. $this->_objectManager->get(\Magento\Framework\Escaper::class)->escapeHtml($description)
  113. )
  114. );
  115. }
  116. }
  117. // save wishlist model for setting date of last update
  118. if ($updatedItems) {
  119. try {
  120. $wishlist->save();
  121. $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
  122. } catch (\Exception $e) {
  123. $this->messageManager->addError(__('Can\'t update wish list'));
  124. }
  125. }
  126. if (isset($post['save_and_share'])) {
  127. $resultRedirect->setPath('*/*/share', ['wishlist_id' => $wishlist->getId()]);
  128. return $resultRedirect;
  129. }
  130. }
  131. $resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]);
  132. return $resultRedirect;
  133. }
  134. }