DataProvider.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AsynchronousOperations\Ui\Component\Operation;
  7. /**
  8. * Class DataProvider
  9. */
  10. class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
  11. {
  12. /**
  13. * @var \Magento\AsynchronousOperations\Model\ResourceModel\Bulk\Collection
  14. */
  15. protected $collection;
  16. /**
  17. * @var \Magento\AsynchronousOperations\Model\Operation\Details
  18. */
  19. private $operationDetails;
  20. /**
  21. * @var \Magento\Framework\App\RequestInterface $request,
  22. */
  23. private $request;
  24. /**
  25. * DataProvider constructor.
  26. * @param string $name
  27. * @param string $primaryFieldName
  28. * @param string $requestFieldName
  29. * @param \Magento\AsynchronousOperations\Model\ResourceModel\Bulk\CollectionFactory $bulkCollectionFactory
  30. * @param \Magento\AsynchronousOperations\Model\Operation\Details $operationDetails
  31. * @param \Magento\Framework\App\RequestInterface $request
  32. * @param array $meta
  33. * @param array $data
  34. */
  35. public function __construct(
  36. $name,
  37. $primaryFieldName,
  38. $requestFieldName,
  39. \Magento\AsynchronousOperations\Model\ResourceModel\Bulk\CollectionFactory $bulkCollectionFactory,
  40. \Magento\AsynchronousOperations\Model\Operation\Details $operationDetails,
  41. \Magento\Framework\App\RequestInterface $request,
  42. array $meta = [],
  43. array $data = []
  44. ) {
  45. $this->collection = $bulkCollectionFactory->create();
  46. $this->operationDetails = $operationDetails;
  47. $this->request = $request;
  48. parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
  49. $this->meta = $this->prepareMeta($this->meta);
  50. }
  51. /**
  52. * Human readable summary for bulk
  53. *
  54. * @param array $operationDetails structure is implied as getOperationDetails() result
  55. * @return string
  56. */
  57. private function getSummaryReport($operationDetails)
  58. {
  59. if (0 == $operationDetails['operations_successful'] && 0 == $operationDetails['operations_failed']) {
  60. return __('Pending, in queue...');
  61. }
  62. $summaryReport = __('%1 items selected for mass update', $operationDetails['operations_total'])->__toString();
  63. if ($operationDetails['operations_successful'] > 0) {
  64. $summaryReport .= __(', %1 successfully updated', $operationDetails['operations_successful']);
  65. }
  66. if ($operationDetails['operations_failed'] > 0) {
  67. $summaryReport .= __(', %1 failed to update', $operationDetails['operations_failed']);
  68. }
  69. return $summaryReport;
  70. }
  71. /**
  72. * Bulk summary with operation statistics
  73. *
  74. * @return array
  75. */
  76. public function getData()
  77. {
  78. $data = [];
  79. $items = $this->collection->getItems();
  80. if (count($items) == 0) {
  81. return $data;
  82. }
  83. $bulk = array_shift($items);
  84. /** @var \Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface $bulk */
  85. $data = $bulk->getData();
  86. $operationDetails = $this->operationDetails->getDetails($data['uuid']);
  87. $data['summary'] = $this->getSummaryReport($operationDetails);
  88. $data = array_merge($data, $operationDetails);
  89. return [$bulk->getBulkId() => $data];
  90. }
  91. /**
  92. * Prepares Meta
  93. *
  94. * @param array $meta
  95. * @return array
  96. */
  97. public function prepareMeta($meta)
  98. {
  99. $requestId = $this->request->getParam($this->requestFieldName);
  100. $operationDetails = $this->operationDetails->getDetails($requestId);
  101. if (isset($operationDetails['failed_retriable']) && !$operationDetails['failed_retriable']) {
  102. $meta['retriable_operations']['arguments']['data']['disabled'] = true;
  103. }
  104. if (isset($operationDetails['failed_not_retriable']) && !$operationDetails['failed_not_retriable']) {
  105. $meta['failed_operations']['arguments']['data']['disabled'] = true;
  106. }
  107. return $meta;
  108. }
  109. }