Config.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order;
  7. use Magento\Framework\Exception\LocalizedException;
  8. /**
  9. * Order configuration model
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Config
  15. {
  16. /**
  17. * @var \Magento\Sales\Model\ResourceModel\Order\Status\Collection
  18. */
  19. protected $collection;
  20. /**
  21. * Statuses per state array
  22. *
  23. * @var array
  24. */
  25. protected $stateStatuses;
  26. /**
  27. * @var array
  28. */
  29. private $statuses;
  30. /**
  31. * @var Status
  32. */
  33. protected $orderStatusFactory;
  34. /**
  35. * @var \Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory
  36. */
  37. protected $orderStatusCollectionFactory;
  38. /**
  39. * @var \Magento\Framework\App\State
  40. */
  41. protected $state;
  42. /**
  43. * @var array
  44. */
  45. protected $maskStatusesMapping = [
  46. \Magento\Framework\App\Area::AREA_FRONTEND => [
  47. \Magento\Sales\Model\Order::STATUS_FRAUD => \Magento\Sales\Model\Order::STATE_PROCESSING,
  48. \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW => \Magento\Sales\Model\Order::STATE_PROCESSING
  49. ]
  50. ];
  51. /**
  52. * Constructor
  53. *
  54. * @param \Magento\Sales\Model\Order\StatusFactory $orderStatusFactory
  55. * @param \Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory $orderStatusCollectionFactory
  56. * @param \Magento\Framework\App\State $state
  57. */
  58. public function __construct(
  59. \Magento\Sales\Model\Order\StatusFactory $orderStatusFactory,
  60. \Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory $orderStatusCollectionFactory,
  61. \Magento\Framework\App\State $state
  62. ) {
  63. $this->orderStatusFactory = $orderStatusFactory;
  64. $this->orderStatusCollectionFactory = $orderStatusCollectionFactory;
  65. $this->state = $state;
  66. }
  67. /**
  68. * Get collection.
  69. *
  70. * @return \Magento\Sales\Model\ResourceModel\Order\Status\Collection
  71. */
  72. protected function _getCollection()
  73. {
  74. if ($this->collection == null) {
  75. $this->collection = $this->orderStatusCollectionFactory->create()->joinStates();
  76. }
  77. return $this->collection;
  78. }
  79. /**
  80. * Get state.
  81. *
  82. * @param string $state
  83. * @return Status
  84. */
  85. protected function _getState($state)
  86. {
  87. foreach ($this->_getCollection() as $item) {
  88. if ($item->getData('state') == $state) {
  89. return $item;
  90. }
  91. }
  92. return null;
  93. }
  94. /**
  95. * Retrieve default status for state
  96. *
  97. * @param string $state
  98. * @return string|null
  99. */
  100. public function getStateDefaultStatus($state): ?string
  101. {
  102. $status = false;
  103. $stateNode = $this->_getState($state);
  104. if ($stateNode) {
  105. $status = $this->orderStatusFactory->create()->loadDefaultByState($state);
  106. $status = $status->getStatus();
  107. }
  108. return $status;
  109. }
  110. /**
  111. * Get status label for a specified area
  112. *
  113. * @param string|null $code
  114. * @param string $area
  115. * @return string|null
  116. */
  117. private function getStatusLabelForArea(?string $code, string $area): ?string
  118. {
  119. $code = $this->maskStatusForArea($area, $code);
  120. $status = $this->orderStatusFactory->create()->load($code);
  121. if ($area === 'adminhtml') {
  122. return $status->getLabel();
  123. }
  124. return $status->getStoreLabel();
  125. }
  126. /**
  127. * Retrieve status label for detected area
  128. *
  129. * @param string|null $code
  130. * @return string|null
  131. * @throws LocalizedException
  132. */
  133. public function getStatusLabel($code)
  134. {
  135. $area = $this->state->getAreaCode() ?: \Magento\Framework\App\Area::AREA_FRONTEND;
  136. return $this->getStatusLabelForArea($code, $area);
  137. }
  138. /**
  139. * Retrieve status label for area
  140. *
  141. * @param string|null $code
  142. * @return string|null
  143. * @since 102.0.1
  144. */
  145. public function getStatusFrontendLabel(?string $code): ?string
  146. {
  147. return $this->getStatusLabelForArea($code, \Magento\Framework\App\Area::AREA_FRONTEND);
  148. }
  149. /**
  150. * Mask status for order for specified area
  151. *
  152. * @param string $area
  153. * @param string $code
  154. * @return string
  155. */
  156. protected function maskStatusForArea($area, $code)
  157. {
  158. if (isset($this->maskStatusesMapping[$area][$code])) {
  159. return $this->maskStatusesMapping[$area][$code];
  160. }
  161. return $code;
  162. }
  163. /**
  164. * State label getter
  165. *
  166. * @param string $state
  167. * @return \Magento\Framework\Phrase|string
  168. */
  169. public function getStateLabel($state)
  170. {
  171. if ($stateItem = $this->_getState($state)) {
  172. $label = $stateItem->getData('label');
  173. return __($label);
  174. }
  175. return $state;
  176. }
  177. /**
  178. * Retrieve all statuses
  179. *
  180. * @return array
  181. */
  182. public function getStatuses()
  183. {
  184. $statuses = $this->orderStatusCollectionFactory->create()->toOptionHash();
  185. return $statuses;
  186. }
  187. /**
  188. * Order states getter
  189. *
  190. * @return array
  191. */
  192. public function getStates()
  193. {
  194. $states = [];
  195. foreach ($this->_getCollection() as $item) {
  196. if ($item->getState()) {
  197. $states[$item->getState()] = __($item->getData('label'));
  198. }
  199. }
  200. return $states;
  201. }
  202. /**
  203. * Retrieve statuses available for state
  204. * Get all possible statuses, or for specified state, or specified states array
  205. * Add labels by default. Return plain array of statuses, if no labels.
  206. *
  207. * @param mixed $state
  208. * @param bool $addLabels
  209. * @return array
  210. */
  211. public function getStateStatuses($state, $addLabels = true)
  212. {
  213. $key = sha1(json_encode([$state, $addLabels]));
  214. if (isset($this->stateStatuses[$key])) {
  215. return $this->stateStatuses[$key];
  216. }
  217. $statuses = [];
  218. if (!is_array($state)) {
  219. $state = [$state];
  220. }
  221. foreach ($state as $_state) {
  222. $stateNode = $this->_getState($_state);
  223. if ($stateNode) {
  224. $collection = $this->orderStatusCollectionFactory->create()->addStateFilter($_state)->orderByLabel();
  225. foreach ($collection as $item) {
  226. $status = $item->getData('status');
  227. if ($addLabels) {
  228. $statuses[$status] = $this->getStatusLabel($status);
  229. } else {
  230. $statuses[] = $status;
  231. }
  232. }
  233. }
  234. }
  235. $this->stateStatuses[$key] = $statuses;
  236. return $statuses;
  237. }
  238. /**
  239. * Retrieve states which are visible on front end
  240. *
  241. * @return array
  242. */
  243. public function getVisibleOnFrontStatuses()
  244. {
  245. return $this->_getStatuses(true);
  246. }
  247. /**
  248. * Get order statuses, invisible on frontend
  249. *
  250. * @return array
  251. */
  252. public function getInvisibleOnFrontStatuses()
  253. {
  254. return $this->_getStatuses(false);
  255. }
  256. /**
  257. * Get existing order statuses.
  258. *
  259. * Visible or invisible on frontend according to passed param.
  260. *
  261. * @param bool $visibility
  262. * @return array
  263. */
  264. protected function _getStatuses($visibility)
  265. {
  266. if ($this->statuses == null) {
  267. $this->statuses = [
  268. true => [],
  269. false => [],
  270. ];
  271. foreach ($this->_getCollection() as $item) {
  272. $visible = (bool) $item->getData('visible_on_front');
  273. $this->statuses[$visible][] = $item->getData('status');
  274. }
  275. }
  276. return $this->statuses[(bool) $visibility];
  277. }
  278. /**
  279. * Retrieve label by state and status
  280. *
  281. * @param string $state
  282. * @param string $status
  283. * @return \Magento\Framework\Phrase|string
  284. * @since 101.0.0
  285. */
  286. public function getStateLabelByStateAndStatus($state, $status)
  287. {
  288. foreach ($this->_getCollection() as $item) {
  289. if ($item->getData('state') == $state && $item->getData('status') == $status) {
  290. $label = $item->getData('label');
  291. return __($label);
  292. }
  293. }
  294. return $state;
  295. }
  296. }