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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Wishlist\Controller\Index;
use Magento\Framework\App\Action;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Controller\ResultFactory;
/**
* Class Update
*/
class Update extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPostActionInterface
{
/**
* @var \Magento\Wishlist\Controller\WishlistProviderInterface
*/
protected $wishlistProvider;
/**
* @var \Magento\Framework\Data\Form\FormKey\Validator
*/
protected $_formKeyValidator;
/**
* @var \Magento\Wishlist\Model\LocaleQuantityProcessor
*/
protected $quantityProcessor;
/**
* @param Action\Context $context
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
* @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
* @param \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor
*/
public function __construct(
Action\Context $context,
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
\Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
\Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor
) {
$this->_formKeyValidator = $formKeyValidator;
$this->wishlistProvider = $wishlistProvider;
$this->quantityProcessor = $quantityProcessor;
parent::__construct($context);
}
/**
* Update wishlist item comments
*
* @return \Magento\Framework\Controller\Result\Redirect
* @throws NotFoundException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
if (!$this->_formKeyValidator->validate($this->getRequest())) {
$resultRedirect->setPath('*/*/');
return $resultRedirect;
}
$wishlist = $this->wishlistProvider->getWishlist();
if (!$wishlist) {
throw new NotFoundException(__('Page not found.'));
}
$post = $this->getRequest()->getPostValue();
if ($post && isset($post['description']) && is_array($post['description'])) {
$updatedItems = 0;
foreach ($post['description'] as $itemId => $description) {
$item = $this->_objectManager->create(\Magento\Wishlist\Model\Item::class)->load($itemId);
if ($item->getWishlistId() != $wishlist->getId()) {
continue;
}
// Extract new values
$description = (string)$description;
if ($description == $this->_objectManager->get(
\Magento\Wishlist\Helper\Data::class
)->defaultCommentString()
) {
$description = '';
}
$qty = null;
if (isset($post['qty'][$itemId])) {
$qty = $this->quantityProcessor->process($post['qty'][$itemId]);
}
if ($qty === null) {
$qty = $item->getQty();
if (!$qty) {
$qty = 1;
}
} elseif (0 == $qty) {
try {
$item->delete();
} catch (\Exception $e) {
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
$this->messageManager->addError(__('We can\'t delete item from Wish List right now.'));
}
}
// Check that we need to save
if ($item->getDescription() == $description && $item->getQty() == $qty) {
continue;
}
try {
$item->setDescription($description)->setQty($qty)->save();
$this->messageManager->addSuccessMessage(
__('%1 has been updated in your Wish List.', $item->getProduct()->getName())
);
$updatedItems++;
} catch (\Exception $e) {
$this->messageManager->addError(
__(
'Can\'t save description %1',
$this->_objectManager->get(\Magento\Framework\Escaper::class)->escapeHtml($description)
)
);
}
}
// save wishlist model for setting date of last update
if ($updatedItems) {
try {
$wishlist->save();
$this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
} catch (\Exception $e) {
$this->messageManager->addError(__('Can\'t update wish list'));
}
}
if (isset($post['save_and_share'])) {
$resultRedirect->setPath('*/*/share', ['wishlist_id' => $wishlist->getId()]);
return $resultRedirect;
}
}
$resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]);
return $resultRedirect;
}
}