SourceDataProvider.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryAdminUi\Ui\DataProvider;
  8. use Magento\Backend\Model\Session;
  9. use Magento\Framework\Api\FilterBuilder;
  10. use Magento\Framework\Api\Search\ReportingInterface;
  11. use Magento\Framework\Api\Search\SearchCriteriaBuilder;
  12. use Magento\Framework\App\RequestInterface;
  13. use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider;
  14. use Magento\InventoryApi\Api\Data\SourceInterface;
  15. use Magento\InventoryApi\Api\SourceRepositoryInterface;
  16. use Magento\Ui\DataProvider\SearchResultFactory;
  17. /**
  18. * @api
  19. */
  20. class SourceDataProvider extends DataProvider
  21. {
  22. const SOURCE_FORM_NAME = 'inventory_source_form_data_source';
  23. /**
  24. * @var SourceRepositoryInterface
  25. */
  26. private $sourceRepository;
  27. /**
  28. * @var SearchResultFactory
  29. */
  30. private $searchResultFactory;
  31. /**
  32. * @var Session
  33. */
  34. private $session;
  35. /**
  36. * @param string $name
  37. * @param string $primaryFieldName
  38. * @param string $requestFieldName
  39. * @param ReportingInterface $reporting
  40. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  41. * @param RequestInterface $request
  42. * @param FilterBuilder $filterBuilder
  43. * @param SourceRepositoryInterface $sourceRepository
  44. * @param SearchResultFactory $searchResultFactory
  45. * @param Session $session
  46. * @param array $meta
  47. * @param array $data
  48. * @SuppressWarnings(PHPMD.ExcessiveParameterList) All parameters are needed for backward compatibility
  49. */
  50. public function __construct(
  51. $name,
  52. $primaryFieldName,
  53. $requestFieldName,
  54. ReportingInterface $reporting,
  55. SearchCriteriaBuilder $searchCriteriaBuilder,
  56. RequestInterface $request,
  57. FilterBuilder $filterBuilder,
  58. SourceRepositoryInterface $sourceRepository,
  59. SearchResultFactory $searchResultFactory,
  60. Session $session,
  61. array $meta = [],
  62. array $data = []
  63. ) {
  64. parent::__construct(
  65. $name,
  66. $primaryFieldName,
  67. $requestFieldName,
  68. $reporting,
  69. $searchCriteriaBuilder,
  70. $request,
  71. $filterBuilder,
  72. $meta,
  73. $data
  74. );
  75. $this->sourceRepository = $sourceRepository;
  76. $this->searchResultFactory = $searchResultFactory;
  77. $this->session = $session;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getData()
  83. {
  84. $data = parent::getData();
  85. if (self::SOURCE_FORM_NAME === $this->name) {
  86. // It is need for support of several fieldsets.
  87. // For details see \Magento\Ui\Component\Form::getDataSourceData
  88. if ($data['totalRecords'] > 0) {
  89. $sourceCode = $data['items'][0][SourceInterface::SOURCE_CODE];
  90. $sourceGeneralData = $data['items'][0];
  91. $sourceGeneralData['disable_source_code'] = !empty($sourceGeneralData['source_code']);
  92. $dataForSingle[$sourceCode] = [
  93. 'general' => $sourceGeneralData,
  94. ];
  95. return $dataForSingle;
  96. }
  97. $sessionData = $this->session->getSourceFormData(true);
  98. if (null !== $sessionData) {
  99. // For details see \Magento\Ui\Component\Form::getDataSourceData
  100. $data = [
  101. '' => $sessionData,
  102. ];
  103. }
  104. }
  105. return $data;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getSearchResult()
  111. {
  112. $searchCriteria = $this->getSearchCriteria();
  113. $result = $this->sourceRepository->getList($searchCriteria);
  114. $searchResult = $this->searchResultFactory->create(
  115. $result->getItems(),
  116. $result->getTotalCount(),
  117. $searchCriteria,
  118. SourceInterface::SOURCE_CODE
  119. );
  120. return $searchResult;
  121. }
  122. }