Collection.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Reports\Model\Grouped;
  7. use Magento\Framework\Data\Collection\AbstractDb as DbCollection;
  8. /**
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class Collection extends \Magento\Framework\Data\Collection
  13. {
  14. /**
  15. * Column name for group by clause
  16. *
  17. * @var string
  18. */
  19. protected $_columnGroupBy = null;
  20. /**
  21. * Collection resource
  22. *
  23. * @var \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  24. */
  25. protected $_resourceCollection = null;
  26. /**
  27. * Set column to group by
  28. * @codeCoverageIgnore
  29. *
  30. * @param string $column
  31. * @return $this
  32. */
  33. public function setColumnGroupBy($column)
  34. {
  35. $this->_columnGroupBy = (string)$column;
  36. return $this;
  37. }
  38. /**
  39. * Load collection
  40. *
  41. * @param bool $printQuery
  42. * @param bool $logQuery
  43. * @return $this
  44. */
  45. public function load($printQuery = false, $logQuery = false)
  46. {
  47. if ($this->isLoaded()) {
  48. return $this;
  49. }
  50. parent::load($printQuery, $logQuery);
  51. $this->_setIsLoaded();
  52. if ($this->_columnGroupBy !== null) {
  53. $this->_mergeWithEmptyData();
  54. $this->_groupResourceData();
  55. }
  56. return $this;
  57. }
  58. /**
  59. * Setter for resource collection
  60. * @codeCoverageIgnore
  61. *
  62. * @param DbCollection $collection
  63. * @return $this
  64. */
  65. public function setResourceCollection($collection)
  66. {
  67. $this->_resourceCollection = $collection;
  68. return $this;
  69. }
  70. /**
  71. * Merge empty data collection with resource collection
  72. *
  73. * @return $this
  74. */
  75. protected function _mergeWithEmptyData()
  76. {
  77. if (count($this->_items) == 0) {
  78. return $this;
  79. }
  80. foreach ($this->_items as $key => $item) {
  81. foreach ($this->_resourceCollection as $dataItem) {
  82. if ($item->getData($this->_columnGroupBy) == $dataItem->getData($this->_columnGroupBy)) {
  83. if ($this->_items[$key]->getIsEmpty()) {
  84. $this->_items[$key] = $dataItem;
  85. } else {
  86. $this->_items[$key]->addChild($dataItem);
  87. }
  88. }
  89. }
  90. }
  91. return $this;
  92. }
  93. /**
  94. * Group data in resource collection
  95. *
  96. * @return $this
  97. */
  98. protected function _groupResourceData()
  99. {
  100. if (count($this->_items) == 0) {
  101. foreach ($this->_resourceCollection as $item) {
  102. if (isset($this->_items[$item->getData($this->_columnGroupBy)])) {
  103. $this->_items[$item->getData($this->_columnGroupBy)]->addChild($item->setIsEmpty(false));
  104. } else {
  105. $this->_items[$item->getData($this->_columnGroupBy)] = $item->setIsEmpty(false);
  106. }
  107. }
  108. }
  109. return $this;
  110. }
  111. }