123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Downloadable\Model\ResourceModel;
- use Magento\Catalog\Api\Data\ProductInterface;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\EntityManager\MetadataPool;
- /**
- * Downloadable Product Samples resource model
- *
- * @api
- * @since 100.0.2
- */
- class Link extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
- {
- /**
- * @var MetadataPool
- */
- private $metadataPool;
- /**
- * Catalog data
- *
- * @var \Magento\Catalog\Helper\Data
- */
- protected $_catalogData;
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
- */
- protected $_configuration;
- /**
- * @var \Magento\Directory\Model\CurrencyFactory
- */
- protected $_currencyFactory;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
- * @param \Magento\Catalog\Helper\Data $catalogData
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $configuration
- * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param string $connectionName
- */
- public function __construct(
- \Magento\Framework\Model\ResourceModel\Db\Context $context,
- \Magento\Catalog\Helper\Data $catalogData,
- \Magento\Framework\App\Config\ScopeConfigInterface $configuration,
- \Magento\Directory\Model\CurrencyFactory $currencyFactory,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- $connectionName = null
- ) {
- $this->_catalogData = $catalogData;
- $this->_configuration = $configuration;
- $this->_currencyFactory = $currencyFactory;
- $this->_storeManager = $storeManager;
- parent::__construct($context, $connectionName);
- }
- /**
- * Initialize connection and define resource
- *
- * @return void
- */
- protected function _construct()
- {
- $this->_init('downloadable_link', 'link_id');
- }
- /**
- * Save title and price of link item
- *
- * @param \Magento\Downloadable\Model\Link $linkObject
- * @return $this
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- public function saveItemTitleAndPrice($linkObject)
- {
- $connection = $this->getConnection();
- $linkTitleTable = $this->getTable('downloadable_link_title');
- $linkPriceTable = $this->getTable('downloadable_link_price');
- $select = $connection->select()->from(
- $this->getTable('downloadable_link_title')
- )->where(
- 'link_id=:link_id AND store_id=:store_id'
- );
- $bind = [':link_id' => $linkObject->getId(), ':store_id' => (int)$linkObject->getStoreId()];
- if ($connection->fetchOne($select, $bind)) {
- $where = ['link_id = ?' => $linkObject->getId(), 'store_id = ?' => (int)$linkObject->getStoreId()];
- if ($linkObject->getUseDefaultTitle()) {
- $connection->delete($linkTitleTable, $where);
- } else {
- $insertData = ['title' => $linkObject->getTitle()];
- $connection->update($linkTitleTable, $insertData, $where);
- }
- } else {
- if (!$linkObject->getUseDefaultTitle()) {
- $connection->insert(
- $linkTitleTable,
- [
- 'link_id' => $linkObject->getId(),
- 'store_id' => (int)$linkObject->getStoreId(),
- 'title' => $linkObject->getTitle()
- ]
- );
- }
- }
- $select = $connection->select()->from($linkPriceTable)->where('link_id=:link_id AND website_id=:website_id');
- $bind = [':link_id' => $linkObject->getId(), ':website_id' => (int)$linkObject->getWebsiteId()];
- if ($connection->fetchOne($select, $bind)) {
- $where = ['link_id = ?' => $linkObject->getId(), 'website_id = ?' => $linkObject->getWebsiteId()];
- if ($linkObject->getUseDefaultPrice()) {
- $connection->delete($linkPriceTable, $where);
- } else {
- $connection->update($linkPriceTable, ['price' => $linkObject->getPrice()], $where);
- }
- } else {
- if (!$linkObject->getUseDefaultPrice()) {
- $dataToInsert[] = [
- 'link_id' => $linkObject->getId(),
- 'website_id' => (int)$linkObject->getWebsiteId(),
- 'price' => (double)$linkObject->getPrice(),
- ];
- if ($linkObject->getOrigData('link_id') != $linkObject->getLinkId()) {
- $_isNew = true;
- } else {
- $_isNew = false;
- }
- if ($linkObject->getWebsiteId() == 0 && $_isNew && !$this->_catalogData->isPriceGlobal()) {
- $websiteIds = $linkObject->getProductWebsiteIds();
- foreach ($websiteIds as $websiteId) {
- $baseCurrency = $this->_configuration->getValue(
- \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
- 'default'
- );
- $websiteCurrency = $this->_storeManager->getWebsite($websiteId)->getBaseCurrencyCode();
- if ($websiteCurrency == $baseCurrency) {
- continue;
- }
- $rate = $this->_createCurrency()->load($baseCurrency)->getRate($websiteCurrency);
- if (!$rate) {
- $rate = 1;
- }
- $newPrice = $linkObject->getPrice() * $rate;
- $dataToInsert[] = [
- 'link_id' => $linkObject->getId(),
- 'website_id' => (int)$websiteId,
- 'price' => $newPrice,
- ];
- }
- }
- $connection->insertMultiple($linkPriceTable, $dataToInsert);
- }
- }
- return $this;
- }
- /**
- * Delete data by item(s)
- *
- * @param \Magento\Downloadable\Model\Link|array|int $items
- * @return $this
- */
- public function deleteItems($items)
- {
- $connection = $this->getConnection();
- if ($items instanceof \Magento\Downloadable\Model\Link) {
- $where = ['link_id = ?' => $items->getId()];
- } elseif (is_array($items)) {
- $where = ['link_id in (?)' => $items];
- } else {
- $where = ['sample_id = ?' => $items];
- }
- $connection->delete($this->getMainTable(), $where);
- $connection->delete($this->getTable('downloadable_link_title'), $where);
- $connection->delete($this->getTable('downloadable_link_price'), $where);
- return $this;
- }
- /**
- * Retrieve links searchable data
- *
- * @param int $productId
- * @param int $storeId
- * @return array
- */
- public function getSearchableData($productId, $storeId)
- {
- $connection = $this->getConnection();
- $ifNullDefaultTitle = $connection->getIfNullSql('st.title', 's.title');
- $select = $connection->select()->from(
- ['m' => $this->getMainTable()],
- null
- )->join(
- ['s' => $this->getTable('downloadable_link_title')],
- 's.link_id=m.link_id AND s.store_id=0',
- []
- )->join(
- ['cpe' => $this->getTable('catalog_product_entity')],
- sprintf(
- 'cpe.entity_id = m.product_id',
- $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()
- ),
- []
- )->joinLeft(
- ['st' => $this->getTable('downloadable_link_title')],
- 'st.link_id=m.link_id AND st.store_id=:store_id',
- ['title' => $ifNullDefaultTitle]
- )->where(
- 'cpe.entity_id=:product_id'
- );
- $bind = [':store_id' => (int)$storeId, ':product_id' => $productId];
- return $connection->fetchCol($select, $bind);
- }
- /**
- * @return \Magento\Directory\Model\Currency
- */
- protected function _createCurrency()
- {
- return $this->_currencyFactory->create();
- }
- /**
- * Get MetadataPool instance
- * @return MetadataPool
- */
- private function getMetadataPool()
- {
- if (!$this->metadataPool) {
- $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
- }
- return $this->metadataPool;
- }
- }
|