/app/code/core/Mage/Downloadable/Model/Resource/Link.php

https://bitbucket.org/kdms/sh-magento · PHP · 210 lines · 146 code · 10 blank · 54 comment · 23 complexity · 4fdd76d5abaa8b64a52dcd246d106f74 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Downloadable
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Downloadable Product Samples resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Downloadable
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Downloadable_Model_Resource_Link extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Initialize connection and define resource
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('downloadable/link', 'link_id');
  42. }
  43. /**
  44. * Save title and price of link item
  45. *
  46. * @param Mage_Downloadable_Model_Link $linkObject
  47. * @return Mage_Downloadable_Model_Resource_Link
  48. */
  49. public function saveItemTitleAndPrice($linkObject)
  50. {
  51. $writeAdapter = $this->_getWriteAdapter();
  52. $linkTitleTable = $this->getTable('downloadable/link_title');
  53. $linkPriceTable = $this->getTable('downloadable/link_price');
  54. $select = $writeAdapter->select()
  55. ->from($this->getTable('downloadable/link_title'))
  56. ->where('link_id=:link_id AND store_id=:store_id');
  57. $bind = array(
  58. ':link_id' => $linkObject->getId(),
  59. ':store_id' => (int)$linkObject->getStoreId()
  60. );
  61. if ($writeAdapter->fetchOne($select, $bind)) {
  62. $where = array(
  63. 'link_id = ?' => $linkObject->getId(),
  64. 'store_id = ?' => (int)$linkObject->getStoreId()
  65. );
  66. if ($linkObject->getUseDefaultTitle()) {
  67. $writeAdapter->delete(
  68. $linkTitleTable, $where);
  69. } else {
  70. $insertData = array('title' => $linkObject->getTitle());
  71. $writeAdapter->update(
  72. $linkTitleTable,
  73. $insertData,
  74. $where);
  75. }
  76. } else {
  77. if (!$linkObject->getUseDefaultTitle()) {
  78. $writeAdapter->insert(
  79. $linkTitleTable,
  80. array(
  81. 'link_id' => $linkObject->getId(),
  82. 'store_id' => (int)$linkObject->getStoreId(),
  83. 'title' => $linkObject->getTitle(),
  84. ));
  85. }
  86. }
  87. $select = $writeAdapter->select()
  88. ->from($linkPriceTable)
  89. ->where('link_id=:link_id AND website_id=:website_id');
  90. $bind = array(
  91. ':link_id' => $linkObject->getId(),
  92. ':website_id' => (int)$linkObject->getWebsiteId(),
  93. );
  94. if ($writeAdapter->fetchOne($select, $bind)) {
  95. $where = array(
  96. 'link_id = ?' => $linkObject->getId(),
  97. 'website_id = ?' => $linkObject->getWebsiteId()
  98. );
  99. if ($linkObject->getUseDefaultPrice()) {
  100. $writeAdapter->delete(
  101. $linkPriceTable, $where);
  102. } else {
  103. $writeAdapter->update(
  104. $linkPriceTable,
  105. array('price' => $linkObject->getPrice()),
  106. $where);
  107. }
  108. } else {
  109. if (!$linkObject->getUseDefaultPrice()) {
  110. $dataToInsert[] = array(
  111. 'link_id' => $linkObject->getId(),
  112. 'website_id' => (int)$linkObject->getWebsiteId(),
  113. 'price' => (float)$linkObject->getPrice()
  114. );
  115. if ($linkObject->getOrigData('link_id') != $linkObject->getLinkId()) {
  116. $_isNew = true;
  117. } else {
  118. $_isNew = false;
  119. }
  120. if ($linkObject->getWebsiteId() == 0 && $_isNew && !Mage::helper('catalog')->isPriceGlobal()) {
  121. $websiteIds = $linkObject->getProductWebsiteIds();
  122. foreach ($websiteIds as $websiteId) {
  123. $baseCurrency = Mage::app()->getBaseCurrencyCode();
  124. $websiteCurrency = Mage::app()->getWebsite($websiteId)->getBaseCurrencyCode();
  125. if ($websiteCurrency == $baseCurrency) {
  126. continue;
  127. }
  128. $rate = Mage::getModel('directory/currency')->load($baseCurrency)->getRate($websiteCurrency);
  129. if (!$rate) {
  130. $rate = 1;
  131. }
  132. $newPrice = $linkObject->getPrice() * $rate;
  133. $dataToInsert[] = array(
  134. 'link_id' => $linkObject->getId(),
  135. 'website_id' => (int)$websiteId,
  136. 'price' => $newPrice
  137. );
  138. }
  139. }
  140. $writeAdapter->insertMultiple($linkPriceTable, $dataToInsert);
  141. }
  142. }
  143. return $this;
  144. }
  145. /**
  146. * Delete data by item(s)
  147. *
  148. * @param Mage_Downloadable_Model_Link|array|int $items
  149. * @return Mage_Downloadable_Model_Resource_Link
  150. */
  151. public function deleteItems($items)
  152. {
  153. $writeAdapter = $this->_getWriteAdapter();
  154. $where = array();
  155. if ($items instanceof Mage_Downloadable_Model_Link) {
  156. $where = array('link_id = ?' => $items->getId());
  157. } elseif (is_array($items)) {
  158. $where = array('link_id in (?)' => $items);
  159. } else {
  160. $where = array('sample_id = ?' => $items);
  161. }
  162. if ($where) {
  163. $writeAdapter->delete(
  164. $this->getMainTable(), $where);
  165. $writeAdapter->delete(
  166. $this->getTable('downloadable/link_title'), $where);
  167. $writeAdapter->delete(
  168. $this->getTable('downloadable/link_price'), $where);
  169. }
  170. return $this;
  171. }
  172. /**
  173. * Retrieve links searchable data
  174. *
  175. * @param int $productId
  176. * @param int $storeId
  177. * @return array
  178. */
  179. public function getSearchableData($productId, $storeId)
  180. {
  181. $adapter = $this->_getReadAdapter();
  182. $ifNullDefaultTitle = $adapter->getIfNullSql('st.title', 's.title');
  183. $select = $adapter->select()
  184. ->from(array('m' => $this->getMainTable()), null)
  185. ->join(
  186. array('s' => $this->getTable('downloadable/link_title')),
  187. 's.link_id=m.link_id AND s.store_id=0',
  188. array())
  189. ->joinLeft(
  190. array('st' => $this->getTable('downloadable/link_title')),
  191. 'st.link_id=m.link_id AND st.store_id=:store_id',
  192. array('title' => $ifNullDefaultTitle))
  193. ->where('m.product_id=:product_id');
  194. $bind = array(
  195. ':store_id' => (int)$storeId,
  196. ':product_id' => $productId
  197. );
  198. return $adapter->fetchCol($select, $bind);
  199. }
  200. }