OrderStatus.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Rss;
  7. use Magento\Framework\App\Rss\DataProviderInterface;
  8. /**
  9. * Class OrderStatus
  10. * @package Magento\Sales\Model\Rss
  11. */
  12. class OrderStatus implements DataProviderInterface
  13. {
  14. /**
  15. * Url Builder
  16. *
  17. * @var \Magento\Framework\UrlInterface
  18. */
  19. protected $urlBuilder;
  20. /**
  21. * @var \Magento\Sales\Model\Order
  22. */
  23. protected $order;
  24. /**
  25. * @var \Magento\Sales\Model\ResourceModel\Order\Rss\OrderStatusFactory
  26. */
  27. protected $orderResourceFactory;
  28. /**
  29. * @var \Magento\Framework\ObjectManagerInterface
  30. */
  31. protected $objectManager;
  32. /**
  33. * @var \Magento\Framework\App\RequestInterface
  34. */
  35. protected $request;
  36. /**
  37. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  38. */
  39. protected $localeDate;
  40. /**
  41. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  42. */
  43. protected $config;
  44. /**
  45. * @var \Magento\Sales\Model\OrderFactory
  46. */
  47. protected $orderFactory;
  48. /**
  49. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  50. * @param \Magento\Framework\UrlInterface $urlBuilder
  51. * @param \Magento\Framework\App\RequestInterface $request
  52. * @param \Magento\Sales\Model\ResourceModel\Order\Rss\OrderStatusFactory $orderResourceFactory
  53. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  54. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  55. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  56. */
  57. public function __construct(
  58. \Magento\Framework\ObjectManagerInterface $objectManager,
  59. \Magento\Framework\UrlInterface $urlBuilder,
  60. \Magento\Framework\App\RequestInterface $request,
  61. \Magento\Sales\Model\ResourceModel\Order\Rss\OrderStatusFactory $orderResourceFactory,
  62. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  63. \Magento\Sales\Model\OrderFactory $orderFactory,
  64. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  65. ) {
  66. $this->objectManager = $objectManager;
  67. $this->urlBuilder = $urlBuilder;
  68. $this->request = $request;
  69. $this->orderResourceFactory = $orderResourceFactory;
  70. $this->localeDate = $localeDate;
  71. $this->orderFactory = $orderFactory;
  72. $this->config = $scopeConfig;
  73. }
  74. /**
  75. * Check if RSS feed allowed
  76. *
  77. * @return bool
  78. */
  79. public function isAllowed()
  80. {
  81. if ($this->config->getValue('rss/order/status', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
  82. return true;
  83. }
  84. return false;
  85. }
  86. /**
  87. * @return array
  88. */
  89. public function getRssData()
  90. {
  91. $this->order = $this->getOrder();
  92. if ($this->order === null) {
  93. throw new \InvalidArgumentException('Order not found.');
  94. }
  95. return array_merge($this->getHeader(), $this->getEntries());
  96. }
  97. /**
  98. * @return string
  99. */
  100. public function getCacheKey()
  101. {
  102. $order = $this->getOrder();
  103. $key = '';
  104. if ($order !== null) {
  105. $key = md5($order->getId() . $order->getIncrementId() . $order->getCustomerId());
  106. }
  107. return 'rss_order_status_data_' . $key;
  108. }
  109. /**
  110. * @return int
  111. */
  112. public function getCacheLifetime()
  113. {
  114. return 600;
  115. }
  116. /**
  117. * @return \Magento\Sales\Model\Order
  118. */
  119. protected function getOrder()
  120. {
  121. if ($this->order) {
  122. return $this->order;
  123. }
  124. $data = null;
  125. $json = base64_decode((string)$this->request->getParam('data'));
  126. if ($json) {
  127. $data = json_decode($json, true);
  128. }
  129. if (!is_array($data)) {
  130. return null;
  131. }
  132. if (!isset($data['order_id']) || !isset($data['increment_id']) || !isset($data['customer_id'])) {
  133. return null;
  134. }
  135. /** @var $order \Magento\Sales\Model\Order */
  136. $order = $this->orderFactory->create();
  137. $order->load($data['order_id']);
  138. if ($order->getIncrementId() !== $data['increment_id'] || $order->getCustomerId() !== $data['customer_id']) {
  139. $order = null;
  140. }
  141. $this->order = $order;
  142. return $this->order;
  143. }
  144. /**
  145. * Get RSS feed items
  146. *
  147. * @return array
  148. */
  149. protected function getEntries()
  150. {
  151. /** @var $resourceModel \Magento\Sales\Model\ResourceModel\Order\Rss\OrderStatus */
  152. $resourceModel = $this->orderResourceFactory->create();
  153. $results = $resourceModel->getAllCommentCollection($this->order->getId());
  154. $entries = [];
  155. if ($results) {
  156. foreach ($results as $result) {
  157. $urlAppend = 'view';
  158. $type = $result['entity_type_code'];
  159. if ($type && $type != 'order') {
  160. $urlAppend = $type;
  161. }
  162. $type = __(ucwords($type));
  163. $title = __('Details for %1 #%2', $type, $result['increment_id']);
  164. $description = '<p>' . __('Notified Date: %1', $this->localeDate->formatDate($result['created_at']))
  165. . '<br/>'
  166. . __('Comment: %1<br/>', $result['comment']) . '</p>';
  167. $url = $this->urlBuilder->getUrl(
  168. 'sales/order/' . $urlAppend,
  169. ['order_id' => $this->order->getId()]
  170. );
  171. $entries[] = ['title' => $title, 'link' => $url, 'description' => $description];
  172. }
  173. }
  174. $title = __('Order #%1 created at %2', $this->order->getIncrementId(), $this->localeDate->formatDate(
  175. $this->order->getCreatedAt()
  176. ));
  177. $url = $this->urlBuilder->getUrl('sales/order/view', ['order_id' => $this->order->getId()]);
  178. $description = '<p>' . __('Current Status: %1<br/>', $this->order->getStatusLabel()) .
  179. __('Total: %1<br/>', $this->order->formatPrice($this->order->getGrandTotal())) . '</p>';
  180. $entries[] = ['title' => $title, 'link' => $url, 'description' => $description];
  181. return ['entries' => $entries];
  182. }
  183. /**
  184. * Get data for Header section of RSS feed
  185. *
  186. * @return array
  187. */
  188. protected function getHeader()
  189. {
  190. $title = __('Order # %1 Notification(s)', $this->order->getIncrementId());
  191. $newUrl = $this->urlBuilder->getUrl('sales/order/view', ['order_id' => $this->order->getId()]);
  192. return ['title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8'];
  193. }
  194. /**
  195. * @return array
  196. */
  197. public function getFeeds()
  198. {
  199. return [];
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function isAuthRequired()
  205. {
  206. return true;
  207. }
  208. }