Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
7 
14 class Config
15 {
19  protected $collection;
20 
26  protected $stateStatuses;
27 
31  private $statuses;
32 
37 
42 
46  protected $state;
47 
51  protected $maskStatusesMapping = [
55  ]
56  ];
57 
65  public function __construct(
66  \Magento\Sales\Model\Order\StatusFactory $orderStatusFactory,
67  \Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory $orderStatusCollectionFactory,
68  \Magento\Framework\App\State $state
69  ) {
70  $this->orderStatusFactory = $orderStatusFactory;
71  $this->orderStatusCollectionFactory = $orderStatusCollectionFactory;
72  $this->state = $state;
73  }
74 
78  protected function _getCollection()
79  {
80  if ($this->collection == null) {
81  $this->collection = $this->orderStatusCollectionFactory->create()->joinStates();
82  }
83  return $this->collection;
84  }
85 
90  protected function _getState($state)
91  {
92  foreach ($this->_getCollection() as $item) {
93  if ($item->getData('state') == $state) {
94  return $item;
95  }
96  }
97  return null;
98  }
99 
106  public function getStateDefaultStatus($state)
107  {
108  $status = false;
109  $stateNode = $this->_getState($state);
110  if ($stateNode) {
111  $status = $this->orderStatusFactory->create()->loadDefaultByState($state);
112  $status = $status->getStatus();
113  }
114  return $status;
115  }
116 
123  public function getStatusLabel($code)
124  {
125  $area = $this->state->getAreaCode();
126  $code = $this->maskStatusForArea($area, $code);
127  $status = $this->orderStatusFactory->create()->load($code);
128 
129  if ($area == 'adminhtml') {
130  return $status->getLabel();
131  }
132 
133  return $status->getStoreLabel();
134  }
135 
143  protected function maskStatusForArea($area, $code)
144  {
145  if (isset($this->maskStatusesMapping[$area][$code])) {
146  return $this->maskStatusesMapping[$area][$code];
147  }
148  return $code;
149  }
150 
157  public function getStateLabel($state)
158  {
159  if ($stateItem = $this->_getState($state)) {
160  $label = $stateItem->getData('label');
161  return __($label);
162  }
163  return $state;
164  }
165 
171  public function getStatuses()
172  {
173  $statuses = $this->orderStatusCollectionFactory->create()->toOptionHash();
174  return $statuses;
175  }
176 
182  public function getStates()
183  {
184  $states = [];
185  foreach ($this->_getCollection() as $item) {
186  if ($item->getState()) {
187  $states[$item->getState()] = __($item->getData('label'));
188  }
189  }
190  return $states;
191  }
192 
202  public function getStateStatuses($state, $addLabels = true)
203  {
204  $key = sha1(json_encode([$state, $addLabels]));
205  if (isset($this->stateStatuses[$key])) {
206  return $this->stateStatuses[$key];
207  }
208  $statuses = [];
209 
210  if (!is_array($state)) {
211  $state = [$state];
212  }
213  foreach ($state as $_state) {
214  $stateNode = $this->_getState($_state);
215  if ($stateNode) {
216  $collection = $this->orderStatusCollectionFactory->create()->addStateFilter($_state)->orderByLabel();
217  foreach ($collection as $item) {
218  $status = $item->getData('status');
219  if ($addLabels) {
220  $statuses[$status] = $this->getStatusLabel($status);
221  } else {
222  $statuses[] = $status;
223  }
224  }
225  }
226  }
227  $this->stateStatuses[$key] = $statuses;
228  return $statuses;
229  }
230 
236  public function getVisibleOnFrontStatuses()
237  {
238  return $this->_getStatuses(true);
239  }
240 
246  public function getInvisibleOnFrontStatuses()
247  {
248  return $this->_getStatuses(false);
249  }
250 
258  protected function _getStatuses($visibility)
259  {
260  if ($this->statuses == null) {
261  $this->statuses = [
262  true => [],
263  false => [],
264  ];
265  foreach ($this->_getCollection() as $item) {
266  $visible = (bool) $item->getData('visible_on_front');
267  $this->statuses[$visible][] = $item->getData('status');
268  }
269  }
270  return $this->statuses[(bool) $visibility];
271  }
272 
282  {
283  foreach ($this->_getCollection() as $item) {
284  if ($item->getData('state') == $state && $item->getData('status') == $status) {
285  $label = $item->getData('label');
286  return __($label);
287  }
288  }
289  return $state;
290  }
291 }
__construct(\Magento\Sales\Model\Order\StatusFactory $orderStatusFactory, \Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory $orderStatusCollectionFactory, \Magento\Framework\App\State $state)
Definition: Config.php:65
__()
Definition: __.php:13
$label
Definition: details.phtml:21
getStateLabelByStateAndStatus($state, $status)
Definition: Config.php:281
$status
Definition: order_status.php:8
maskStatusForArea($area, $code)
Definition: Config.php:143
getStateStatuses($state, $addLabels=true)
Definition: Config.php:202
$code
Definition: info.phtml:12