CustomerDetails.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\Order;
  6. use Magento\Customer\Api\CustomerMetadataInterface;
  7. use Magento\Customer\Api\GroupRepositoryInterface;
  8. use Magento\Customer\Model\Metadata\CustomerMetadata;
  9. use Magento\Customer\Model\Metadata\ElementFactory;
  10. use Magento\Eav\Model\AttributeDataFactory;
  11. use Magento\Framework\Escaper;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\UrlInterface;
  14. use Magento\Framework\View\Element\Block\ArgumentInterface;
  15. use Magento\Sales\Api\Data\OrderInterface;
  16. /**
  17. * View model for customer related information.
  18. *
  19. * @package Temando\Shipping\ViewModel
  20. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.temando.com/
  23. */
  24. class CustomerDetails implements ArgumentInterface
  25. {
  26. /**
  27. * @var Escaper
  28. */
  29. private $escaper;
  30. /**
  31. * @var UrlInterface
  32. */
  33. private $urlBuilder;
  34. /**
  35. * @var GroupRepositoryInterface
  36. */
  37. private $customerGroupRepository;
  38. /**
  39. * @var CustomerMetadataInterface|CustomerMetadata
  40. */
  41. private $customerMetadata;
  42. /**
  43. * @var ElementFactory
  44. */
  45. private $metadataElementFactory;
  46. /**
  47. * OrderDetails constructor.
  48. * @param Escaper $escaper
  49. * @param UrlInterface $urlBuilder
  50. * @param GroupRepositoryInterface $customerGroupRepository
  51. * @param CustomerMetadataInterface $customerMetadata
  52. * @param ElementFactory $metadataElementFactory
  53. */
  54. public function __construct(
  55. Escaper $escaper,
  56. UrlInterface $urlBuilder,
  57. GroupRepositoryInterface $customerGroupRepository,
  58. CustomerMetadataInterface $customerMetadata,
  59. ElementFactory $metadataElementFactory
  60. ) {
  61. $this->escaper = $escaper;
  62. $this->urlBuilder = $urlBuilder;
  63. $this->customerGroupRepository = $customerGroupRepository;
  64. $this->customerMetadata = $customerMetadata;
  65. $this->metadataElementFactory = $metadataElementFactory;
  66. }
  67. /**
  68. * Find sort order for account data
  69. * Sort Order used as array key
  70. *
  71. * @param array $data
  72. * @param int $sortOrder
  73. * @return int
  74. */
  75. private function prepareAccountDataSortOrder(array $data, $sortOrder)
  76. {
  77. if (isset($data[$sortOrder])) {
  78. return $this->prepareAccountDataSortOrder($data, $sortOrder + 1);
  79. }
  80. return $sortOrder;
  81. }
  82. /**
  83. * Get URL to edit the customer.
  84. *
  85. * @see \Magento\Sales\Block\Adminhtml\Order\View\Info::getCustomerViewUrl
  86. *
  87. * @param OrderInterface $order
  88. * @return string
  89. */
  90. public function getCustomerViewUrl(OrderInterface $order)
  91. {
  92. if ($order->getCustomerIsGuest() || !$order->getCustomerId()) {
  93. return '';
  94. }
  95. return $this->urlBuilder->getUrl('customer/index/edit', ['id' => $order->getCustomerId()]);
  96. }
  97. /**
  98. * Return name of the customer group.
  99. *
  100. * @see \Magento\Sales\Block\Adminhtml\Order\View\Info::getCustomerGroupName
  101. *
  102. * @param OrderInterface $order
  103. * @return string
  104. */
  105. public function getCustomerGroupName(OrderInterface $order)
  106. {
  107. $customerGroupId = $order->getCustomerGroupId();
  108. if ($customerGroupId === null) {
  109. return '';
  110. }
  111. try {
  112. $customerGroup = $this->customerGroupRepository->getById($customerGroupId);
  113. return $customerGroup->getCode();
  114. } catch (LocalizedException $e) {
  115. return '';
  116. }
  117. }
  118. /**
  119. * Return array of additional account data
  120. * Value is option style array
  121. *
  122. * @see \Magento\Sales\Block\Adminhtml\Order\View\Info::getCustomerAccountData
  123. *
  124. * @param OrderInterface|\Magento\Sales\Model\Order $order
  125. * @return mixed[]
  126. */
  127. public function getCustomerAccountData(OrderInterface $order)
  128. {
  129. $accountData = [];
  130. $entityType = 'customer';
  131. foreach ($this->customerMetadata->getAllAttributesMetadata($entityType) as $attribute) {
  132. if (!$attribute->isVisible() || $attribute->isSystem()) {
  133. continue;
  134. }
  135. $orderKey = sprintf('customer_%s', $attribute->getAttributeCode());
  136. $orderValue = $order->getData($orderKey);
  137. if ($orderValue != '') {
  138. $metadataElement = $this->metadataElementFactory->create($attribute, $orderValue, $entityType);
  139. $value = $metadataElement->outputValue(AttributeDataFactory::OUTPUT_FORMAT_HTML);
  140. $sortOrder = $attribute->getSortOrder() + $attribute->isUserDefined() ? 200 : 0;
  141. $sortOrder = $this->prepareAccountDataSortOrder($accountData, $sortOrder);
  142. $accountData[$sortOrder] = [
  143. 'label' => $attribute->getFrontendLabel(),
  144. 'value' => $this->escaper->escapeHtml($value, ['br']),
  145. ];
  146. }
  147. }
  148. ksort($accountData, SORT_NUMERIC);
  149. return $accountData;
  150. }
  151. }