Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Link.php
Go to the documentation of this file.
1 <?php
7 
11 
19 {
23  private $metadataPool;
24 
30  protected $_catalogData;
31 
35  protected $_configuration;
36 
40  protected $_currencyFactory;
41 
45  protected $_storeManager;
46 
55  public function __construct(
56  \Magento\Framework\Model\ResourceModel\Db\Context $context,
57  \Magento\Catalog\Helper\Data $catalogData,
58  \Magento\Framework\App\Config\ScopeConfigInterface $configuration,
59  \Magento\Directory\Model\CurrencyFactory $currencyFactory,
60  \Magento\Store\Model\StoreManagerInterface $storeManager,
61  $connectionName = null
62  ) {
63  $this->_catalogData = $catalogData;
64  $this->_configuration = $configuration;
65  $this->_currencyFactory = $currencyFactory;
66  $this->_storeManager = $storeManager;
67  parent::__construct($context, $connectionName);
68  }
69 
75  protected function _construct()
76  {
77  $this->_init('downloadable_link', 'link_id');
78  }
79 
87  public function saveItemTitleAndPrice($linkObject)
88  {
89  $connection = $this->getConnection();
90  $linkTitleTable = $this->getTable('downloadable_link_title');
91  $linkPriceTable = $this->getTable('downloadable_link_price');
92 
93  $select = $connection->select()->from(
94  $this->getTable('downloadable_link_title')
95  )->where(
96  'link_id=:link_id AND store_id=:store_id'
97  );
98  $bind = [':link_id' => $linkObject->getId(), ':store_id' => (int)$linkObject->getStoreId()];
99 
100  if ($connection->fetchOne($select, $bind)) {
101  $where = ['link_id = ?' => $linkObject->getId(), 'store_id = ?' => (int)$linkObject->getStoreId()];
102  if ($linkObject->getUseDefaultTitle()) {
103  $connection->delete($linkTitleTable, $where);
104  } else {
105  $insertData = ['title' => $linkObject->getTitle()];
106  $connection->update($linkTitleTable, $insertData, $where);
107  }
108  } else {
109  if (!$linkObject->getUseDefaultTitle()) {
110  $connection->insert(
111  $linkTitleTable,
112  [
113  'link_id' => $linkObject->getId(),
114  'store_id' => (int)$linkObject->getStoreId(),
115  'title' => $linkObject->getTitle()
116  ]
117  );
118  }
119  }
120 
121  $select = $connection->select()->from($linkPriceTable)->where('link_id=:link_id AND website_id=:website_id');
122  $bind = [':link_id' => $linkObject->getId(), ':website_id' => (int)$linkObject->getWebsiteId()];
123  if ($connection->fetchOne($select, $bind)) {
124  $where = ['link_id = ?' => $linkObject->getId(), 'website_id = ?' => $linkObject->getWebsiteId()];
125  if ($linkObject->getUseDefaultPrice()) {
126  $connection->delete($linkPriceTable, $where);
127  } else {
128  $connection->update($linkPriceTable, ['price' => $linkObject->getPrice()], $where);
129  }
130  } else {
131  if (!$linkObject->getUseDefaultPrice()) {
132  $dataToInsert[] = [
133  'link_id' => $linkObject->getId(),
134  'website_id' => (int)$linkObject->getWebsiteId(),
135  'price' => (double)$linkObject->getPrice(),
136  ];
137  if ($linkObject->getOrigData('link_id') != $linkObject->getLinkId()) {
138  $_isNew = true;
139  } else {
140  $_isNew = false;
141  }
142  if ($linkObject->getWebsiteId() == 0 && $_isNew && !$this->_catalogData->isPriceGlobal()) {
143  $websiteIds = $linkObject->getProductWebsiteIds();
144  foreach ($websiteIds as $websiteId) {
145  $baseCurrency = $this->_configuration->getValue(
146  \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
147  'default'
148  );
149  $websiteCurrency = $this->_storeManager->getWebsite($websiteId)->getBaseCurrencyCode();
150  if ($websiteCurrency == $baseCurrency) {
151  continue;
152  }
153  $rate = $this->_createCurrency()->load($baseCurrency)->getRate($websiteCurrency);
154  if (!$rate) {
155  $rate = 1;
156  }
157  $newPrice = $linkObject->getPrice() * $rate;
158  $dataToInsert[] = [
159  'link_id' => $linkObject->getId(),
160  'website_id' => (int)$websiteId,
161  'price' => $newPrice,
162  ];
163  }
164  }
165  $connection->insertMultiple($linkPriceTable, $dataToInsert);
166  }
167  }
168  return $this;
169  }
170 
177  public function deleteItems($items)
178  {
179  $connection = $this->getConnection();
180  if ($items instanceof \Magento\Downloadable\Model\Link) {
181  $where = ['link_id = ?' => $items->getId()];
182  } elseif (is_array($items)) {
183  $where = ['link_id in (?)' => $items];
184  } else {
185  $where = ['sample_id = ?' => $items];
186  }
187  $connection->delete($this->getMainTable(), $where);
188  $connection->delete($this->getTable('downloadable_link_title'), $where);
189  $connection->delete($this->getTable('downloadable_link_price'), $where);
190  return $this;
191  }
192 
201  {
202  $connection = $this->getConnection();
203  $ifNullDefaultTitle = $connection->getIfNullSql('st.title', 's.title');
204  $select = $connection->select()->from(
205  ['m' => $this->getMainTable()],
206  null
207  )->join(
208  ['s' => $this->getTable('downloadable_link_title')],
209  's.link_id=m.link_id AND s.store_id=0',
210  []
211  )->join(
212  ['cpe' => $this->getTable('catalog_product_entity')],
213  sprintf(
214  'cpe.entity_id = m.product_id',
215  $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()
216  ),
217  []
218  )->joinLeft(
219  ['st' => $this->getTable('downloadable_link_title')],
220  'st.link_id=m.link_id AND st.store_id=:store_id',
221  ['title' => $ifNullDefaultTitle]
222  )->where(
223  'cpe.entity_id=:product_id'
224  );
225  $bind = [':store_id' => (int)$storeId, ':product_id' => $productId];
226 
227  return $connection->fetchCol($select, $bind);
228  }
229 
233  protected function _createCurrency()
234  {
235  return $this->_currencyFactory->create();
236  }
237 
242  private function getMetadataPool()
243  {
244  if (!$this->metadataPool) {
245  $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
246  }
247  return $this->metadataPool;
248  }
249 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$configuration
Definition: index.php:33
$storeManager
$connection
Definition: bulk.php:13
$items