Collection.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\ResourceModel\Link;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. /**
  9. * Downloadable links resource collection
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  14. {
  15. /**
  16. * @var \Magento\Framework\EntityManager\MetadataPool
  17. */
  18. protected $metadataPool;
  19. /**
  20. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  21. * @param \Psr\Log\LoggerInterface $logger
  22. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  23. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  24. * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
  25. * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
  26. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
  27. */
  28. public function __construct(
  29. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  30. \Psr\Log\LoggerInterface $logger,
  31. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  32. \Magento\Framework\Event\ManagerInterface $eventManager,
  33. \Magento\Framework\EntityManager\MetadataPool $metadataPool,
  34. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  35. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  36. ) {
  37. $this->metadataPool = $metadataPool;
  38. parent::__construct(
  39. $entityFactory,
  40. $logger,
  41. $fetchStrategy,
  42. $eventManager,
  43. $connection,
  44. $resource
  45. );
  46. }
  47. /**
  48. * Init resource model
  49. *
  50. * @return void
  51. */
  52. protected function _construct()
  53. {
  54. $this->_init(\Magento\Downloadable\Model\Link::class, \Magento\Downloadable\Model\ResourceModel\Link::class);
  55. }
  56. /**
  57. * Method for product filter
  58. *
  59. * @param \Magento\Catalog\Model\Product|array|integer|null $product
  60. * @return $this
  61. */
  62. public function addProductToFilter($product)
  63. {
  64. if (empty($product)) {
  65. $this->addFieldToFilter('product_id', '');
  66. } else {
  67. $this->join(
  68. ['cpe' => $this->getTable('catalog_product_entity')],
  69. sprintf(
  70. 'cpe.%s = main_table.product_id',
  71. $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()
  72. )
  73. );
  74. if ($product instanceof \Magento\Catalog\Model\Product) {
  75. $this->addFieldToFilter('cpe.entity_id', $product->getEntityId());
  76. } else {
  77. $this->addFieldToFilter('cpe.entity_id', ['in' => $product]);
  78. }
  79. }
  80. return $this;
  81. }
  82. /**
  83. * Retrieve title for current store
  84. *
  85. * @param int $storeId
  86. * @return $this
  87. */
  88. public function addTitleToResult($storeId = 0)
  89. {
  90. $ifNullDefaultTitle = $this->getConnection()->getIfNullSql('st.title', 'd.title');
  91. $this->getSelect()
  92. ->joinLeft(
  93. ['d' => $this->getTable('downloadable_link_title')],
  94. 'd.link_id = main_table.link_id AND d.store_id = 0',
  95. ['default_title' => 'title']
  96. )->joinLeft(
  97. ['st' => $this->getTable('downloadable_link_title')],
  98. 'st.link_id=main_table.link_id AND st.store_id = ' . (int)$storeId,
  99. [
  100. 'store_title' => 'title',
  101. 'title' => $ifNullDefaultTitle
  102. ]
  103. )->order('main_table.sort_order ASC')
  104. ->order('title ASC');
  105. return $this;
  106. }
  107. /**
  108. * Retrieve price for current website
  109. *
  110. * @param int $websiteId
  111. * @return $this
  112. */
  113. public function addPriceToResult($websiteId)
  114. {
  115. $ifNullDefaultPrice = $this->getConnection()->getIfNullSql('stp.price', 'dp.price');
  116. $this->getSelect()->joinLeft(
  117. ['dp' => $this->getTable('downloadable_link_price')],
  118. 'dp.link_id=main_table.link_id AND dp.website_id = 0',
  119. ['default_price' => 'price']
  120. )->joinLeft(
  121. ['stp' => $this->getTable('downloadable_link_price')],
  122. 'stp.link_id=main_table.link_id AND stp.website_id = ' . (int)$websiteId,
  123. ['website_price' => 'price', 'price' => $ifNullDefaultPrice]
  124. );
  125. return $this;
  126. }
  127. }