AbstractDashboard.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Helper\Dashboard;
  7. /**
  8. * Adminhtml abstract dashboard helper.
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. abstract class AbstractDashboard extends \Magento\Framework\App\Helper\AbstractHelper
  14. {
  15. /**
  16. * Helper collection
  17. *
  18. * @var \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection|array
  19. */
  20. protected $_collection;
  21. /**
  22. * Parameters for helper
  23. *
  24. * @var array
  25. */
  26. protected $_params = [];
  27. /**
  28. * @return array|\Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  29. */
  30. public function getCollection()
  31. {
  32. if ($this->_collection === null) {
  33. $this->_initCollection();
  34. }
  35. return $this->_collection;
  36. }
  37. /**
  38. * @return void
  39. */
  40. abstract protected function _initCollection();
  41. /**
  42. * Returns collection items
  43. *
  44. * @return array
  45. */
  46. public function getItems()
  47. {
  48. return is_array($this->getCollection()) ? $this->getCollection() : $this->getCollection()->getItems();
  49. }
  50. /**
  51. * @return int
  52. */
  53. public function getCount()
  54. {
  55. return sizeof($this->getItems());
  56. }
  57. /**
  58. * @param string $index
  59. * @return array
  60. */
  61. public function getColumn($index)
  62. {
  63. $result = [];
  64. foreach ($this->getItems() as $item) {
  65. if (is_array($item)) {
  66. if (isset($item[$index])) {
  67. $result[] = $item[$index];
  68. } else {
  69. $result[] = null;
  70. }
  71. } elseif ($item instanceof \Magento\Framework\DataObject) {
  72. $result[] = $item->getData($index);
  73. } else {
  74. $result[] = null;
  75. }
  76. }
  77. return $result;
  78. }
  79. /**
  80. * @param string $name
  81. * @param mixed $value
  82. * @return void
  83. */
  84. public function setParam($name, $value)
  85. {
  86. $this->_params[$name] = $value;
  87. }
  88. /**
  89. * @param array $params
  90. * @return void
  91. */
  92. public function setParams(array $params)
  93. {
  94. $this->_params = $params;
  95. }
  96. /**
  97. * @param string $name
  98. * @return mixed
  99. */
  100. public function getParam($name)
  101. {
  102. if (isset($this->_params[$name])) {
  103. return $this->_params[$name];
  104. }
  105. return null;
  106. }
  107. /**
  108. * @return array
  109. */
  110. public function getParams()
  111. {
  112. return $this->_params;
  113. }
  114. }