Wishlist.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Model\Rss;
  7. use Magento\Framework\App\Rss\DataProviderInterface;
  8. use Magento\Store\Model\ScopeInterface;
  9. /**
  10. * Wishlist RSS model
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class Wishlist implements DataProviderInterface
  15. {
  16. /**
  17. * Url Builder
  18. *
  19. * @var \Magento\Framework\UrlInterface
  20. */
  21. protected $urlBuilder;
  22. /**
  23. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  24. */
  25. protected $scopeConfig;
  26. /**
  27. * System event manager
  28. * @var \Magento\Framework\Event\ManagerInterface
  29. */
  30. protected $eventManager;
  31. /**
  32. * Parent layout of the block
  33. *
  34. * @var \Magento\Framework\View\LayoutInterface
  35. */
  36. protected $layout;
  37. /**
  38. * @var \Magento\Wishlist\Helper\Data
  39. */
  40. protected $wishlistHelper;
  41. /**
  42. * @var \Magento\Catalog\Helper\Output
  43. */
  44. protected $outputHelper;
  45. /**
  46. * @var \Magento\Catalog\Helper\Image
  47. */
  48. protected $imageHelper;
  49. /**
  50. * @var \Magento\Wishlist\Block\Customer\Wishlist
  51. */
  52. protected $wishlistBlock;
  53. /**
  54. * @var \Magento\Framework\App\RequestInterface
  55. */
  56. protected $request;
  57. /**
  58. * @var \Magento\Customer\Model\CustomerFactory
  59. */
  60. protected $customerFactory;
  61. /**
  62. * Wishlist constructor.
  63. *
  64. * @param \Magento\Wishlist\Helper\Rss $wishlistHelper
  65. * @param \Magento\Wishlist\Block\Customer\Wishlist $wishlistBlock
  66. * @param \Magento\Catalog\Helper\Output $outputHelper
  67. * @param \Magento\Catalog\Helper\Image $imageHelper
  68. * @param \Magento\Framework\UrlInterface $urlBuilder
  69. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  70. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  71. * @param \Magento\Customer\Model\CustomerFactory $customerFactory
  72. * @param \Magento\Framework\View\LayoutInterface $layout
  73. * @param \Magento\Framework\App\RequestInterface $request
  74. *
  75. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  76. */
  77. public function __construct(
  78. \Magento\Wishlist\Helper\Rss $wishlistHelper,
  79. \Magento\Wishlist\Block\Customer\Wishlist $wishlistBlock,
  80. \Magento\Catalog\Helper\Output $outputHelper,
  81. \Magento\Catalog\Helper\Image $imageHelper,
  82. \Magento\Framework\UrlInterface $urlBuilder,
  83. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  84. \Magento\Framework\Event\ManagerInterface $eventManager,
  85. \Magento\Customer\Model\CustomerFactory $customerFactory,
  86. \Magento\Framework\View\LayoutInterface $layout,
  87. \Magento\Framework\App\RequestInterface $request
  88. ) {
  89. $this->wishlistHelper = $wishlistHelper;
  90. $this->wishlistBlock = $wishlistBlock;
  91. $this->outputHelper = $outputHelper;
  92. $this->imageHelper = $imageHelper;
  93. $this->urlBuilder = $urlBuilder;
  94. $this->scopeConfig = $scopeConfig;
  95. $this->eventManager = $eventManager;
  96. $this->customerFactory = $customerFactory;
  97. $this->layout = $layout;
  98. $this->request = $request;
  99. }
  100. /**
  101. * Check if RSS feed allowed
  102. *
  103. * @return mixed
  104. */
  105. public function isAllowed()
  106. {
  107. return $this->scopeConfig->isSetFlag('rss/wishlist/active', ScopeInterface::SCOPE_STORE)
  108. && $this->getWishlist()->getCustomerId() === $this->wishlistHelper->getCustomer()->getId();
  109. }
  110. /**
  111. * Get RSS feed items
  112. *
  113. * @return array
  114. * @throws \Magento\Framework\Exception\LocalizedException
  115. */
  116. public function getRssData()
  117. {
  118. $wishlist = $this->getWishlist();
  119. if ($wishlist->getId()) {
  120. $data = $this->getHeader();
  121. /** @var $wishlistItem \Magento\Wishlist\Model\Item */
  122. foreach ($wishlist->getItemCollection() as $wishlistItem) {
  123. /* @var $product \Magento\Catalog\Model\Product */
  124. $product = $wishlistItem->getProduct();
  125. $productUrl = $this->wishlistBlock->getProductUrl($product, ['_rss' => true]);
  126. $product->setAllowedInRss(true);
  127. $product->setAllowedPriceInRss(true);
  128. $product->setProductUrl($productUrl);
  129. $args = ['product' => $product];
  130. $this->eventManager->dispatch('rss_wishlist_xml_callback', $args);
  131. if (!$product->getAllowedInRss()) {
  132. continue;
  133. }
  134. $description = '<table><tr><td><a href="' . $productUrl . '"><img src="'
  135. . $this->imageHelper->init($product, 'rss_thumbnail')->getUrl()
  136. . '" border="0" align="left" height="75" width="75"></a></td>'
  137. . '<td style="text-decoration:none;">'
  138. . $this->outputHelper->productAttribute(
  139. $product,
  140. $product->getShortDescription(),
  141. 'short_description'
  142. ) . '<p>';
  143. if ($product->getAllowedPriceInRss()) {
  144. $description .= $this->getProductPriceHtml($product);
  145. }
  146. $description .= '</p>';
  147. if (trim($product->getDescription()) != '') {
  148. $description .= '<p>' . __('Comment:') . ' '
  149. . $this->outputHelper->productAttribute(
  150. $product,
  151. $product->getDescription(),
  152. 'description'
  153. ) . '<p>';
  154. }
  155. $description .= '</td></tr></table>';
  156. $data['entries'][] = ([
  157. 'title' => $product->getName(),
  158. 'link' => $productUrl,
  159. 'description' => $description,
  160. ]);
  161. }
  162. } else {
  163. $data = [
  164. 'title' => __('We cannot retrieve the Wish List.')->render(),
  165. 'description' => __('We cannot retrieve the Wish List.')->render(),
  166. 'link' => $this->urlBuilder->getUrl(),
  167. 'charset' => 'UTF-8',
  168. ];
  169. }
  170. return $data;
  171. }
  172. /**
  173. * GetCacheKey
  174. *
  175. * @return string
  176. */
  177. public function getCacheKey()
  178. {
  179. return 'rss_wishlist_data_' . $this->getWishlist()->getId();
  180. }
  181. /**
  182. * Get Cache Lifetime
  183. *
  184. * @return int
  185. */
  186. public function getCacheLifetime()
  187. {
  188. return 60;
  189. }
  190. /**
  191. * Get data for Header section of RSS feed
  192. *
  193. * @return array
  194. */
  195. public function getHeader()
  196. {
  197. $customerId = $this->getWishlist()->getCustomerId();
  198. $customer = $this->customerFactory->create()->load($customerId);
  199. $title = __('%1\'s Wishlist', $customer->getName())->render();
  200. $newUrl = $this->urlBuilder->getUrl(
  201. 'wishlist/shared/index',
  202. ['code' => $this->getWishlist()->getSharingCode()]
  203. );
  204. return ['title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8'];
  205. }
  206. /**
  207. * Retrieve Wishlist model
  208. *
  209. * @return \Magento\Wishlist\Model\Wishlist
  210. */
  211. protected function getWishlist()
  212. {
  213. $wishlist = $this->wishlistHelper->getWishlist();
  214. return $wishlist;
  215. }
  216. /**
  217. * Return HTML block with product price
  218. *
  219. * @param \Magento\Catalog\Model\Product $product
  220. * @return string
  221. */
  222. public function getProductPriceHtml(\Magento\Catalog\Model\Product $product)
  223. {
  224. $price = '';
  225. /** @var \Magento\Framework\Pricing\Render $priceRender */
  226. $priceRender = $this->layout->getBlock('product.price.render.default');
  227. if (!$priceRender) {
  228. $priceRender = $this->layout->createBlock(
  229. \Magento\Framework\Pricing\Render::class,
  230. 'product.price.render.default',
  231. ['data' => ['price_render_handle' => 'catalog_product_prices']]
  232. );
  233. }
  234. if ($priceRender) {
  235. $price = $priceRender->render(
  236. 'wishlist_configured_price',
  237. $product,
  238. ['zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST]
  239. );
  240. }
  241. return $price;
  242. }
  243. /**
  244. * @inheritdoc
  245. */
  246. public function getFeeds()
  247. {
  248. return [];
  249. }
  250. /**
  251. * @inheritdoc
  252. */
  253. public function isAuthRequired()
  254. {
  255. if ($this->request->getParam('sharing_code') == $this->getWishlist()->getSharingCode()) {
  256. return false;
  257. }
  258. return true;
  259. }
  260. }