PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Related/Ui/Component/DataProvider.php

https://bitbucket.org/tsn-AlekseyD/tsn-module
PHP | 127 lines | 69 code | 20 blank | 38 comment | 1 complexity | ab936684ef141f4e2d0bdca2e0c2dd46 MD5 | raw file
  1. <?php
  2. /**
  3. * @author TSN-Media Team
  4. * @copyright Copyright (c) 2018 TSN-Media (https://tsn-media.com)
  5. * @package TSN_Base
  6. */
  7. namespace TSN\Related\Ui\Component;
  8. use Magento\Framework\Api\FilterBuilder;
  9. use Magento\Framework\Api\Search\ReportingInterface;
  10. use Magento\Framework\Api\Search\SearchCriteriaBuilder;
  11. use Magento\Framework\App\RequestInterface;
  12. use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider as UiDataProvider;
  13. use TSN\Related\Model\ResourceModel\Related\Collection as RelatedCollection;
  14. use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
  15. /**
  16. * Class DataProvider
  17. * @package TSN\Related\Ui\Component
  18. */
  19. class DataProvider extends UiDataProvider
  20. {
  21. const QTY = 'qty';
  22. const NAME = 'name';
  23. const ITEMS = 'items';
  24. const ENTITY_ID = 'entity_id';
  25. const PRODUCT_NAME = 'product_name';
  26. const TOTAL_RECORDS = 'totalRecords';
  27. /**
  28. * @var RelatedCollection
  29. */
  30. protected $_relatedCollection;
  31. /**
  32. * @var ProductCollection
  33. */
  34. protected $_productCollection;
  35. /**
  36. * DataProvider constructor.
  37. * @param string $name
  38. * @param string $primaryFieldName
  39. * @param string $requestFieldName
  40. * @param ReportingInterface $reporting
  41. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  42. * @param RequestInterface $request
  43. * @param FilterBuilder $filterBuilder
  44. * @param RelatedCollection $relatedCollection
  45. * @param ProductCollection $productCollection
  46. * @param array $meta
  47. * @param array $data
  48. */
  49. public function __construct(
  50. $name,
  51. $primaryFieldName,
  52. $requestFieldName,
  53. ReportingInterface $reporting,
  54. SearchCriteriaBuilder $searchCriteriaBuilder,
  55. RequestInterface $request,
  56. FilterBuilder $filterBuilder,
  57. RelatedCollection $relatedCollection,
  58. ProductCollection $productCollection,
  59. array $meta = [],
  60. array $data = []
  61. )
  62. {
  63. parent::__construct($name, $primaryFieldName, $requestFieldName, $reporting, $searchCriteriaBuilder, $request, $filterBuilder, $meta, $data);
  64. $this->_relatedCollection = $relatedCollection;
  65. $this->_productCollection = $productCollection;
  66. }
  67. /**
  68. * @return array
  69. */
  70. public function getData()
  71. {
  72. $dataSearch = $this->searchCriteriaBuilder->getData();
  73. $pageSize = isset($dataSearch['page_size']) ? $dataSearch['page_size'] : 10;
  74. $currentPage = isset($dataSearch['current_page']) ? $dataSearch['current_page'] : 1;
  75. $visitorsQty = $this->getProductVisitorsQty();
  76. $this->_productCollection
  77. ->addFieldToFilter(self::ENTITY_ID, ['in' => $this->getProductIds()])
  78. ->addAttributeToSelect(self::NAME)
  79. ->setPageSize($pageSize)
  80. ->setCurPage($currentPage);
  81. foreach ($this->_productCollection as $product) {
  82. if(isset($visitorsQty[$product->getId()])) {
  83. $result[self::ITEMS][] = [
  84. static::ENTITY_ID => $product->getId(),
  85. static::PRODUCT_NAME => $product->getName(),
  86. static::QTY => $visitorsQty[$product->getId()]
  87. ];
  88. }
  89. }
  90. $result[self::TOTAL_RECORDS] = $this->_productCollection->getSize();
  91. return $result;
  92. }
  93. /**
  94. * @return array
  95. */
  96. protected function getProductIds()
  97. {
  98. return array_unique($this->_relatedCollection->getProductIds());
  99. }
  100. /**
  101. * @return array
  102. */
  103. protected function getProductVisitorsQty()
  104. {
  105. return $this->_relatedCollection->getProductVisitorQty();
  106. }
  107. }