PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Backend/Block/Dashboard/Tab/Products/Ordered.php

https://gitlab.com/svillegas/magento2
PHP | 153 lines | 96 code | 16 blank | 41 comment | 5 complexity | 1ee36d9e1a548375e25fb10a16e6a61d MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Dashboard\Tab\Products;
  7. /**
  8. * Adminhtml dashboard most ordered products grid
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  12. */
  13. class Ordered extends \Magento\Backend\Block\Dashboard\Grid
  14. {
  15. /**
  16. * @var \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory
  17. */
  18. protected $_collectionFactory;
  19. /**
  20. * @var \Magento\Framework\Module\Manager
  21. */
  22. protected $_moduleManager;
  23. /**
  24. * @param \Magento\Backend\Block\Template\Context $context
  25. * @param \Magento\Backend\Helper\Data $backendHelper
  26. * @param \Magento\Framework\Module\Manager $moduleManager
  27. * @param \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory
  28. * @param array $data
  29. */
  30. public function __construct(
  31. \Magento\Backend\Block\Template\Context $context,
  32. \Magento\Backend\Helper\Data $backendHelper,
  33. \Magento\Framework\Module\Manager $moduleManager,
  34. \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
  35. array $data = []
  36. ) {
  37. $this->_collectionFactory = $collectionFactory;
  38. $this->_moduleManager = $moduleManager;
  39. parent::__construct($context, $backendHelper, $data);
  40. }
  41. /**
  42. * @return void
  43. */
  44. protected function _construct()
  45. {
  46. parent::_construct();
  47. $this->setId('productsOrderedGrid');
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function _prepareCollection()
  53. {
  54. if (!$this->_moduleManager->isEnabled('Magento_Sales')) {
  55. return $this;
  56. }
  57. if ($this->getParam('website')) {
  58. $storeIds = $this->_storeManager->getWebsite($this->getParam('website'))->getStoreIds();
  59. $storeId = array_pop($storeIds);
  60. } elseif ($this->getParam('group')) {
  61. $storeIds = $this->_storeManager->getGroup($this->getParam('group'))->getStoreIds();
  62. $storeId = array_pop($storeIds);
  63. } else {
  64. $storeId = (int)$this->getParam('store');
  65. }
  66. $collection = $this->_collectionFactory->create()->setModel(
  67. \Magento\Catalog\Model\Product::class
  68. )->addStoreFilter(
  69. $storeId
  70. );
  71. $this->setCollection($collection);
  72. return parent::_prepareCollection();
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function _prepareColumns()
  78. {
  79. $this->addColumn(
  80. 'name',
  81. [
  82. 'header' => __('Product'),
  83. 'sortable' => false,
  84. 'index' => 'product_name',
  85. 'header_css_class' => 'col-product',
  86. 'column_css_class' => 'col-product'
  87. ]
  88. );
  89. $this->addColumn(
  90. 'price',
  91. [
  92. 'header' => __('Price'),
  93. 'type' => 'currency',
  94. 'currency_code' => (string)$this->_storeManager->getStore(
  95. (int)$this->getParam('store')
  96. )->getBaseCurrencyCode(),
  97. 'sortable' => false,
  98. 'index' => 'product_price'
  99. ]
  100. );
  101. $this->addColumn(
  102. 'ordered_qty',
  103. [
  104. 'header' => __('Quantity'),
  105. 'sortable' => false,
  106. 'index' => 'qty_ordered',
  107. 'type' => 'number',
  108. 'header_css_class' => 'col-qty',
  109. 'column_css_class' => 'col-qty'
  110. ]
  111. );
  112. $this->setFilterVisibility(false);
  113. $this->setPagerVisibility(false);
  114. return parent::_prepareColumns();
  115. }
  116. /**
  117. * Returns row url to show in admin dashboard
  118. * $row is bestseller row wrapped in Product model
  119. *
  120. * @param \Magento\Catalog\Model\Product $row
  121. * @return string
  122. */
  123. public function getRowUrl($row)
  124. {
  125. // getId() would return id of bestseller row, and product id we get by getProductId()
  126. $productId = $row->getProductId();
  127. // No url is possible for non-existing products
  128. if (!$productId) {
  129. return '';
  130. }
  131. $params = ['id' => $productId];
  132. if ($this->getRequest()->getParam('store')) {
  133. $params['store'] = $this->getRequest()->getParam('store');
  134. }
  135. return $this->getUrl('catalog/product/edit', $params);
  136. }
  137. }