SearchResult.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AsynchronousOperations\Ui\Component\DataProvider;
  7. use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
  8. use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
  9. use Magento\Framework\Event\ManagerInterface as EventManager;
  10. use Psr\Log\LoggerInterface as Logger;
  11. use Magento\Authorization\Model\UserContextInterface;
  12. use Magento\Framework\Bulk\BulkSummaryInterface;
  13. use Magento\AsynchronousOperations\Model\StatusMapper;
  14. use Magento\AsynchronousOperations\Model\BulkStatus\CalculatedStatusSql;
  15. /**
  16. * Class SearchResult
  17. */
  18. class SearchResult extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
  19. {
  20. /**
  21. * @var UserContextInterface
  22. */
  23. private $userContext;
  24. /**
  25. * @var StatusMapper
  26. */
  27. private $statusMapper;
  28. /**
  29. * @var array|int
  30. */
  31. private $operationStatus;
  32. /**
  33. * @var CalculatedStatusSql
  34. */
  35. private $calculatedStatusSql;
  36. /**
  37. * SearchResult constructor.
  38. * @param EntityFactory $entityFactory
  39. * @param Logger $logger
  40. * @param FetchStrategy $fetchStrategy
  41. * @param EventManager $eventManager
  42. * @param UserContextInterface $userContextInterface
  43. * @param StatusMapper $statusMapper
  44. * @param CalculatedStatusSql $calculatedStatusSql
  45. * @param string $mainTable
  46. * @param null $resourceModel
  47. * @param string $identifierName
  48. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  49. */
  50. public function __construct(
  51. EntityFactory $entityFactory,
  52. Logger $logger,
  53. FetchStrategy $fetchStrategy,
  54. EventManager $eventManager,
  55. UserContextInterface $userContextInterface,
  56. StatusMapper $statusMapper,
  57. CalculatedStatusSql $calculatedStatusSql,
  58. $mainTable = 'magento_bulk',
  59. $resourceModel = null,
  60. $identifierName = 'uuid'
  61. ) {
  62. $this->userContext = $userContextInterface;
  63. $this->statusMapper = $statusMapper;
  64. $this->calculatedStatusSql = $calculatedStatusSql;
  65. parent::__construct(
  66. $entityFactory,
  67. $logger,
  68. $fetchStrategy,
  69. $eventManager,
  70. $mainTable,
  71. $resourceModel,
  72. $identifierName
  73. );
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function _initSelect()
  79. {
  80. $this->getSelect()->from(
  81. ['main_table' => $this->getMainTable()],
  82. [
  83. '*',
  84. 'status' => $this->calculatedStatusSql->get($this->getTable('magento_operation'))
  85. ]
  86. )->where(
  87. 'user_id=?',
  88. $this->userContext->getUserId()
  89. );
  90. return $this;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. protected function _afterLoad()
  96. {
  97. /** @var BulkSummaryInterface $item */
  98. foreach ($this->getItems() as $item) {
  99. $item->setStatus($this->statusMapper->operationStatusToBulkSummaryStatus($item->getStatus()));
  100. }
  101. return parent::_afterLoad();
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function addFieldToFilter($field, $condition = null)
  107. {
  108. if ($field == 'status') {
  109. if (is_array($condition)) {
  110. foreach ($condition as $value) {
  111. $this->operationStatus = $this->statusMapper->bulkSummaryStatusToOperationStatus($value);
  112. if (is_array($this->operationStatus)) {
  113. foreach ($this->operationStatus as $statusValue) {
  114. $this->getSelect()->orHaving('status = ?', $statusValue);
  115. }
  116. continue;
  117. }
  118. $this->getSelect()->having('status = ?', $this->operationStatus);
  119. }
  120. }
  121. return $this;
  122. }
  123. return parent::addFieldToFilter($field, $condition);
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function getSelectCountSql()
  129. {
  130. $select = parent::getSelectCountSql();
  131. $select->columns(['status' => $this->calculatedStatusSql->get($this->getTable('magento_operation'))]);
  132. //add grouping by status if filtering by status was executed
  133. if (isset($this->operationStatus)) {
  134. $select->group('status');
  135. }
  136. return $select;
  137. }
  138. }