NotifyStock.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Block\Adminhtml\Rss;
  7. use Magento\Framework\App\Rss\DataProviderInterface;
  8. /**
  9. * Class NotifyStock
  10. *
  11. * @package Magento\Catalog\Block\Adminhtml\Rss
  12. */
  13. class NotifyStock extends \Magento\Backend\Block\AbstractBlock implements DataProviderInterface
  14. {
  15. /**
  16. * @var \Magento\Framework\App\Rss\UrlBuilderInterface
  17. */
  18. protected $rssUrlBuilder;
  19. /**
  20. * @var \Magento\Catalog\Model\Rss\Product\NotifyStock
  21. */
  22. protected $rssModel;
  23. /**
  24. * @param \Magento\Backend\Block\Context $context
  25. * @param \Magento\Catalog\Model\Rss\Product\NotifyStock $rssModel
  26. * @param \Magento\Framework\App\Rss\UrlBuilderInterface $rssUrlBuilder
  27. * @param array $data
  28. */
  29. public function __construct(
  30. \Magento\Backend\Block\Context $context,
  31. \Magento\Catalog\Model\Rss\Product\NotifyStock $rssModel,
  32. \Magento\Framework\App\Rss\UrlBuilderInterface $rssUrlBuilder,
  33. array $data = []
  34. ) {
  35. $this->rssUrlBuilder = $rssUrlBuilder;
  36. $this->rssModel = $rssModel;
  37. parent::__construct($context, $data);
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. protected function _construct()
  43. {
  44. $this->setCacheKey('rss_catalog_notifystock');
  45. parent::_construct();
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function getRssData()
  51. {
  52. $newUrl = $this->rssUrlBuilder->getUrl(['_secure' => true, '_nosecret' => true, 'type' => 'notifystock']);
  53. $title = __('Low Stock Products')->render();
  54. $data = ['title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8'];
  55. foreach ($this->rssModel->getProductsCollection() as $item) {
  56. /* @var $item \Magento\Catalog\Model\Product */
  57. $url = $this->getUrl(
  58. 'catalog/product/edit',
  59. ['id' => $item->getId(), '_secure' => true, '_nosecret' => true]
  60. );
  61. $qty = 1 * $item->getQty();
  62. $description = __('%1 has reached a quantity of %2.', $item->getName(), $qty)->render();
  63. $data['entries'][] = ['title' => $item->getName(), 'link' => $url, 'description' => $description];
  64. }
  65. return $data;
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function getCacheLifetime()
  71. {
  72. return 600;
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function isAllowed()
  78. {
  79. return true;
  80. }
  81. /**
  82. * @inheritdoc
  83. */
  84. public function getFeeds()
  85. {
  86. return [];
  87. }
  88. /**
  89. * @inheritdoc
  90. */
  91. public function isAuthRequired()
  92. {
  93. return true;
  94. }
  95. }