Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderStatus.php
Go to the documentation of this file.
1 <?php
7 
9 
15 {
21  protected $urlBuilder;
22 
26  protected $order;
27 
32 
36  protected $objectManager;
37 
41  protected $request;
42 
46  protected $localeDate;
47 
51  protected $config;
52 
56  protected $orderFactory;
57 
67  public function __construct(
69  \Magento\Framework\UrlInterface $urlBuilder,
70  \Magento\Framework\App\RequestInterface $request,
71  \Magento\Sales\Model\ResourceModel\Order\Rss\OrderStatusFactory $orderResourceFactory,
72  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
73  \Magento\Sales\Model\OrderFactory $orderFactory,
74  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
75  ) {
76  $this->objectManager = $objectManager;
77  $this->urlBuilder = $urlBuilder;
78  $this->request = $request;
79  $this->orderResourceFactory = $orderResourceFactory;
80  $this->localeDate = $localeDate;
81  $this->orderFactory = $orderFactory;
82  $this->config = $scopeConfig;
83  }
84 
90  public function isAllowed()
91  {
92  if ($this->config->getValue('rss/order/status', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
93  return true;
94  }
95  return false;
96  }
97 
101  public function getRssData()
102  {
103  $this->order = $this->getOrder();
104  if ($this->order === null) {
105  throw new \InvalidArgumentException('Order not found.');
106  }
107  return array_merge($this->getHeader(), $this->getEntries());
108  }
109 
113  public function getCacheKey()
114  {
115  $order = $this->getOrder();
116  $key = '';
117  if ($order !== null) {
118  $key = md5($order->getId() . $order->getIncrementId() . $order->getCustomerId());
119  }
120  return 'rss_order_status_data_' . $key;
121  }
122 
126  public function getCacheLifetime()
127  {
128  return 600;
129  }
130 
134  protected function getOrder()
135  {
136  if ($this->order) {
137  return $this->order;
138  }
139 
140  $data = null;
141  $json = base64_decode((string)$this->request->getParam('data'));
142  if ($json) {
143  $data = json_decode($json, true);
144  }
145  if (!is_array($data)) {
146  return null;
147  }
148 
149  if (!isset($data['order_id']) || !isset($data['increment_id']) || !isset($data['customer_id'])) {
150  return null;
151  }
152 
154  $order = $this->orderFactory->create();
155  $order->load($data['order_id']);
156 
157  if ($order->getIncrementId() !== $data['increment_id'] || $order->getCustomerId() !== $data['customer_id']) {
158  $order = null;
159  }
160  $this->order = $order;
161 
162  return $this->order;
163  }
164 
170  protected function getEntries()
171  {
173  $resourceModel = $this->orderResourceFactory->create();
174  $results = $resourceModel->getAllCommentCollection($this->order->getId());
175  $entries = [];
176  if ($results) {
177  foreach ($results as $result) {
178  $urlAppend = 'view';
179  $type = $result['entity_type_code'];
180  if ($type && $type != 'order') {
181  $urlAppend = $type;
182  }
183  $type = __(ucwords($type));
184  $title = __('Details for %1 #%2', $type, $result['increment_id']);
185  $description = '<p>' . __('Notified Date: %1', $this->localeDate->formatDate($result['created_at']))
186  . '<br/>'
187  . __('Comment: %1<br/>', $result['comment']) . '</p>';
188  $url = $this->urlBuilder->getUrl(
189  'sales/order/' . $urlAppend,
190  ['order_id' => $this->order->getId()]
191  );
192  $entries[] = ['title' => $title, 'link' => $url, 'description' => $description];
193  }
194  }
195  $title = __('Order #%1 created at %2', $this->order->getIncrementId(), $this->localeDate->formatDate(
196  $this->order->getCreatedAt()
197  ));
198  $url = $this->urlBuilder->getUrl('sales/order/view', ['order_id' => $this->order->getId()]);
199  $description = '<p>' . __('Current Status: %1<br/>', $this->order->getStatusLabel()) .
200  __('Total: %1<br/>', $this->order->formatPrice($this->order->getGrandTotal())) . '</p>';
201 
202  $entries[] = ['title' => $title, 'link' => $url, 'description' => $description];
203 
204  return ['entries' => $entries];
205  }
206 
212  protected function getHeader()
213  {
214  $title = __('Order # %1 Notification(s)', $this->order->getIncrementId());
215  $newUrl = $this->urlBuilder->getUrl('sales/order/view', ['order_id' => $this->order->getId()]);
216 
217  return ['title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8'];
218  }
219 
223  public function getFeeds()
224  {
225  return [];
226  }
227 
231  public function isAuthRequired()
232  {
233  return true;
234  }
235 }
$title
Definition: default.phtml:14
$results
Definition: popup.phtml:13
__()
Definition: __.php:13
$type
Definition: item.phtml:13
$resourceModel
Definition: tablerates.php:10
__construct(\Magento\Framework\ObjectManagerInterface $objectManager, \Magento\Framework\UrlInterface $urlBuilder, \Magento\Framework\App\RequestInterface $request, \Magento\Sales\Model\ResourceModel\Order\Rss\OrderStatusFactory $orderResourceFactory, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
Definition: OrderStatus.php:67