Link.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. /**
  11. * Downloadable Product Samples resource model
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Link extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  17. {
  18. /**
  19. * @var MetadataPool
  20. */
  21. private $metadataPool;
  22. /**
  23. * Catalog data
  24. *
  25. * @var \Magento\Catalog\Helper\Data
  26. */
  27. protected $_catalogData;
  28. /**
  29. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  30. */
  31. protected $_configuration;
  32. /**
  33. * @var \Magento\Directory\Model\CurrencyFactory
  34. */
  35. protected $_currencyFactory;
  36. /**
  37. * @var \Magento\Store\Model\StoreManagerInterface
  38. */
  39. protected $_storeManager;
  40. /**
  41. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  42. * @param \Magento\Catalog\Helper\Data $catalogData
  43. * @param \Magento\Framework\App\Config\ScopeConfigInterface $configuration
  44. * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
  45. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  46. * @param string $connectionName
  47. */
  48. public function __construct(
  49. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  50. \Magento\Catalog\Helper\Data $catalogData,
  51. \Magento\Framework\App\Config\ScopeConfigInterface $configuration,
  52. \Magento\Directory\Model\CurrencyFactory $currencyFactory,
  53. \Magento\Store\Model\StoreManagerInterface $storeManager,
  54. $connectionName = null
  55. ) {
  56. $this->_catalogData = $catalogData;
  57. $this->_configuration = $configuration;
  58. $this->_currencyFactory = $currencyFactory;
  59. $this->_storeManager = $storeManager;
  60. parent::__construct($context, $connectionName);
  61. }
  62. /**
  63. * Initialize connection and define resource
  64. *
  65. * @return void
  66. */
  67. protected function _construct()
  68. {
  69. $this->_init('downloadable_link', 'link_id');
  70. }
  71. /**
  72. * Save title and price of link item
  73. *
  74. * @param \Magento\Downloadable\Model\Link $linkObject
  75. * @return $this
  76. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  77. */
  78. public function saveItemTitleAndPrice($linkObject)
  79. {
  80. $connection = $this->getConnection();
  81. $linkTitleTable = $this->getTable('downloadable_link_title');
  82. $linkPriceTable = $this->getTable('downloadable_link_price');
  83. $select = $connection->select()->from(
  84. $this->getTable('downloadable_link_title')
  85. )->where(
  86. 'link_id=:link_id AND store_id=:store_id'
  87. );
  88. $bind = [':link_id' => $linkObject->getId(), ':store_id' => (int)$linkObject->getStoreId()];
  89. if ($connection->fetchOne($select, $bind)) {
  90. $where = ['link_id = ?' => $linkObject->getId(), 'store_id = ?' => (int)$linkObject->getStoreId()];
  91. if ($linkObject->getUseDefaultTitle()) {
  92. $connection->delete($linkTitleTable, $where);
  93. } else {
  94. $insertData = ['title' => $linkObject->getTitle()];
  95. $connection->update($linkTitleTable, $insertData, $where);
  96. }
  97. } else {
  98. if (!$linkObject->getUseDefaultTitle()) {
  99. $connection->insert(
  100. $linkTitleTable,
  101. [
  102. 'link_id' => $linkObject->getId(),
  103. 'store_id' => (int)$linkObject->getStoreId(),
  104. 'title' => $linkObject->getTitle()
  105. ]
  106. );
  107. }
  108. }
  109. $select = $connection->select()->from($linkPriceTable)->where('link_id=:link_id AND website_id=:website_id');
  110. $bind = [':link_id' => $linkObject->getId(), ':website_id' => (int)$linkObject->getWebsiteId()];
  111. if ($connection->fetchOne($select, $bind)) {
  112. $where = ['link_id = ?' => $linkObject->getId(), 'website_id = ?' => $linkObject->getWebsiteId()];
  113. if ($linkObject->getUseDefaultPrice()) {
  114. $connection->delete($linkPriceTable, $where);
  115. } else {
  116. $connection->update($linkPriceTable, ['price' => $linkObject->getPrice()], $where);
  117. }
  118. } else {
  119. if (!$linkObject->getUseDefaultPrice()) {
  120. $dataToInsert[] = [
  121. 'link_id' => $linkObject->getId(),
  122. 'website_id' => (int)$linkObject->getWebsiteId(),
  123. 'price' => (double)$linkObject->getPrice(),
  124. ];
  125. if ($linkObject->getOrigData('link_id') != $linkObject->getLinkId()) {
  126. $_isNew = true;
  127. } else {
  128. $_isNew = false;
  129. }
  130. if ($linkObject->getWebsiteId() == 0 && $_isNew && !$this->_catalogData->isPriceGlobal()) {
  131. $websiteIds = $linkObject->getProductWebsiteIds();
  132. foreach ($websiteIds as $websiteId) {
  133. $baseCurrency = $this->_configuration->getValue(
  134. \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
  135. 'default'
  136. );
  137. $websiteCurrency = $this->_storeManager->getWebsite($websiteId)->getBaseCurrencyCode();
  138. if ($websiteCurrency == $baseCurrency) {
  139. continue;
  140. }
  141. $rate = $this->_createCurrency()->load($baseCurrency)->getRate($websiteCurrency);
  142. if (!$rate) {
  143. $rate = 1;
  144. }
  145. $newPrice = $linkObject->getPrice() * $rate;
  146. $dataToInsert[] = [
  147. 'link_id' => $linkObject->getId(),
  148. 'website_id' => (int)$websiteId,
  149. 'price' => $newPrice,
  150. ];
  151. }
  152. }
  153. $connection->insertMultiple($linkPriceTable, $dataToInsert);
  154. }
  155. }
  156. return $this;
  157. }
  158. /**
  159. * Delete data by item(s)
  160. *
  161. * @param \Magento\Downloadable\Model\Link|array|int $items
  162. * @return $this
  163. */
  164. public function deleteItems($items)
  165. {
  166. $connection = $this->getConnection();
  167. if ($items instanceof \Magento\Downloadable\Model\Link) {
  168. $where = ['link_id = ?' => $items->getId()];
  169. } elseif (is_array($items)) {
  170. $where = ['link_id in (?)' => $items];
  171. } else {
  172. $where = ['sample_id = ?' => $items];
  173. }
  174. $connection->delete($this->getMainTable(), $where);
  175. $connection->delete($this->getTable('downloadable_link_title'), $where);
  176. $connection->delete($this->getTable('downloadable_link_price'), $where);
  177. return $this;
  178. }
  179. /**
  180. * Retrieve links searchable data
  181. *
  182. * @param int $productId
  183. * @param int $storeId
  184. * @return array
  185. */
  186. public function getSearchableData($productId, $storeId)
  187. {
  188. $connection = $this->getConnection();
  189. $ifNullDefaultTitle = $connection->getIfNullSql('st.title', 's.title');
  190. $select = $connection->select()->from(
  191. ['m' => $this->getMainTable()],
  192. null
  193. )->join(
  194. ['s' => $this->getTable('downloadable_link_title')],
  195. 's.link_id=m.link_id AND s.store_id=0',
  196. []
  197. )->join(
  198. ['cpe' => $this->getTable('catalog_product_entity')],
  199. sprintf(
  200. 'cpe.entity_id = m.product_id',
  201. $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()
  202. ),
  203. []
  204. )->joinLeft(
  205. ['st' => $this->getTable('downloadable_link_title')],
  206. 'st.link_id=m.link_id AND st.store_id=:store_id',
  207. ['title' => $ifNullDefaultTitle]
  208. )->where(
  209. 'cpe.entity_id=:product_id'
  210. );
  211. $bind = [':store_id' => (int)$storeId, ':product_id' => $productId];
  212. return $connection->fetchCol($select, $bind);
  213. }
  214. /**
  215. * @return \Magento\Directory\Model\Currency
  216. */
  217. protected function _createCurrency()
  218. {
  219. return $this->_currencyFactory->create();
  220. }
  221. /**
  222. * Get MetadataPool instance
  223. * @return MetadataPool
  224. */
  225. private function getMetadataPool()
  226. {
  227. if (!$this->metadataPool) {
  228. $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
  229. }
  230. return $this->metadataPool;
  231. }
  232. }