Sample.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\ResourceModel;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. /**
  9. * Downloadable Product Samples resource model
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Sample extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  15. {
  16. /**
  17. * @var \Magento\Framework\EntityManager\MetadataPool
  18. * @since 100.1.0
  19. */
  20. protected $metadataPool;
  21. /**
  22. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  23. * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
  24. * @param null $connectionName
  25. */
  26. public function __construct(
  27. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  28. \Magento\Framework\EntityManager\MetadataPool $metadataPool,
  29. $connectionName = null
  30. ) {
  31. $this->metadataPool = $metadataPool;
  32. parent::__construct($context, $connectionName);
  33. }
  34. /**
  35. * Initialize connection
  36. *
  37. * @return void
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('downloadable_sample', 'sample_id');
  42. }
  43. /**
  44. * Save title of sample item in store scope
  45. *
  46. * @param \Magento\Downloadable\Model\Sample $sampleObject
  47. * @return $this
  48. */
  49. public function saveItemTitle($sampleObject)
  50. {
  51. $connection = $this->getConnection();
  52. $sampleTitleTable = $this->getTable('downloadable_sample_title');
  53. $bind = [':sample_id' => $sampleObject->getId(), ':store_id' => (int)$sampleObject->getStoreId()];
  54. $select = $connection->select()->from(
  55. $sampleTitleTable
  56. )->where(
  57. 'sample_id=:sample_id AND store_id=:store_id'
  58. );
  59. if ($connection->fetchOne($select, $bind)) {
  60. $where = [
  61. 'sample_id = ?' => $sampleObject->getId(),
  62. 'store_id = ?' => (int)$sampleObject->getStoreId(),
  63. ];
  64. if ($sampleObject->getUseDefaultTitle()) {
  65. $connection->delete($sampleTitleTable, $where);
  66. } else {
  67. $connection->update($sampleTitleTable, ['title' => $sampleObject->getTitle()], $where);
  68. }
  69. } else {
  70. if (!$sampleObject->getUseDefaultTitle()) {
  71. $connection->insert(
  72. $sampleTitleTable,
  73. [
  74. 'sample_id' => $sampleObject->getId(),
  75. 'store_id' => (int)$sampleObject->getStoreId(),
  76. 'title' => $sampleObject->getTitle()
  77. ]
  78. );
  79. }
  80. }
  81. return $this;
  82. }
  83. /**
  84. * Delete data by item(s)
  85. *
  86. * @param \Magento\Downloadable\Model\Sample|array|int $items
  87. * @return $this
  88. */
  89. public function deleteItems($items)
  90. {
  91. $connection = $this->getConnection();
  92. if ($items instanceof \Magento\Downloadable\Model\Sample) {
  93. $where = ['sample_id = ?' => $items->getId()];
  94. } else {
  95. $where = ['sample_id in (?)' => $items];
  96. }
  97. $connection->delete($this->getMainTable(), $where);
  98. $connection->delete($this->getTable('downloadable_sample_title'), $where);
  99. return $this;
  100. }
  101. /**
  102. * Retrieve links searchable data
  103. *
  104. * @param int $productId
  105. * @param int $storeId
  106. * @return array
  107. */
  108. public function getSearchableData($productId, $storeId)
  109. {
  110. $connection = $this->getConnection();
  111. $ifNullDefaultTitle = $connection->getIfNullSql('st.title', 'd.title');
  112. $select = $connection->select()->from(
  113. ['m' => $this->getMainTable()],
  114. null
  115. )->join(
  116. ['d' => $this->getTable('downloadable_sample_title')],
  117. 'd.sample_id=m.sample_id AND d.store_id=0',
  118. []
  119. )->join(
  120. ['cpe' => $this->getTable('catalog_product_entity')],
  121. sprintf(
  122. 'cpe.entity_id = m.product_id',
  123. $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()
  124. ),
  125. []
  126. )->joinLeft(
  127. ['st' => $this->getTable('downloadable_sample_title')],
  128. 'st.sample_id=m.sample_id AND st.store_id=:store_id',
  129. ['title' => $ifNullDefaultTitle]
  130. )->where(
  131. 'cpe.entity_id=:product_id',
  132. $productId
  133. );
  134. $bind = [':store_id' => (int)$storeId, ':product_id' => $productId];
  135. return $connection->fetchCol($select, $bind);
  136. }
  137. }