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

https://github.com/seloshow/Magento-Pruebas · PHP · 185 lines · 127 code · 4 blank · 54 comment · 22 complexity · 458c34fbed9a8cc1408b467a0d880061 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  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_Mysql4_Link extends Mage_Core_Model_Mysql4_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_Mysql4_link
  48. */
  49. public function saveItemTitleAndPrice($linkObject)
  50. {
  51. $stmt = $this->_getReadAdapter()->select()
  52. ->from($this->getTable('downloadable/link_title'))
  53. ->where('link_id = ?', $linkObject->getId())
  54. ->where('store_id = ?', $linkObject->getStoreId());
  55. if ($this->_getReadAdapter()->fetchOne($stmt)) {
  56. $where = $this->_getReadAdapter()->quoteInto('link_id = ?', $linkObject->getId()) .
  57. ' AND ' . $this->_getReadAdapter()->quoteInto('store_id = ?', $linkObject->getStoreId());
  58. if ($linkObject->getUseDefaultTitle()) {
  59. $this->_getWriteAdapter()->delete(
  60. $this->getTable('downloadable/link_title'), $where);
  61. } else {
  62. $this->_getWriteAdapter()->update(
  63. $this->getTable('downloadable/link_title'),
  64. array('title' => $linkObject->getTitle()), $where);
  65. }
  66. } else {
  67. if (!$linkObject->getUseDefaultTitle()) {
  68. $this->_getWriteAdapter()->insert(
  69. $this->getTable('downloadable/link_title'),
  70. array(
  71. 'link_id' => $linkObject->getId(),
  72. 'store_id' => $linkObject->getStoreId(),
  73. 'title' => $linkObject->getTitle(),
  74. ));
  75. }
  76. }
  77. $stmt = null;
  78. $stmt = $this->_getReadAdapter()->select()
  79. ->from($this->getTable('downloadable/link_price'))
  80. ->where('link_id = ?', $linkObject->getId())
  81. ->where('website_id = ?', $linkObject->getWebsiteId());
  82. if ($this->_getReadAdapter()->fetchOne($stmt)) {
  83. $where = $this->_getReadAdapter()->quoteInto('link_id = ?', $linkObject->getId()) .
  84. ' AND ' . $this->_getReadAdapter()->quoteInto('website_id = ?', $linkObject->getWebsiteId());
  85. if ($linkObject->getUseDefaultPrice()) {
  86. $this->_getReadAdapter()->delete(
  87. $this->getTable('downloadable/link_price'), $where);
  88. } else {
  89. $this->_getWriteAdapter()->update(
  90. $this->getTable('downloadable/link_price'),
  91. array('price' => $linkObject->getPrice()), $where);
  92. }
  93. } else {
  94. if (!$linkObject->getUseDefaultPrice()) {
  95. $dataToInsert[] = array(
  96. 'link_id' => $linkObject->getId(),
  97. 'website_id' => $linkObject->getWebsiteId(),
  98. 'price' => $linkObject->getPrice()
  99. );
  100. $_isNew = $linkObject->getOrigData('link_id') != $linkObject->getLinkId();
  101. if ($linkObject->getWebsiteId() == 0 && $_isNew && !Mage::helper('catalog')->isPriceGlobal()) {
  102. $websiteIds = $linkObject->getProductWebsiteIds();
  103. foreach ($websiteIds as $websiteId) {
  104. $baseCurrency = Mage::app()->getBaseCurrencyCode();
  105. $websiteCurrency = Mage::app()->getWebsite($websiteId)->getBaseCurrencyCode();
  106. if ($websiteCurrency == $baseCurrency) {
  107. continue;
  108. }
  109. $rate = Mage::getModel('directory/currency')->load($baseCurrency)->getRate($websiteCurrency);
  110. if (!$rate) {
  111. $rate = 1;
  112. }
  113. $newPrice = $linkObject->getPrice() * $rate;
  114. $dataToInsert[] = array(
  115. 'link_id' => $linkObject->getId(),
  116. 'website_id' => $websiteId,
  117. 'price' => $newPrice
  118. );
  119. }
  120. }
  121. foreach ($dataToInsert as $_data) {
  122. $this->_getWriteAdapter()->insert($this->getTable('downloadable/link_price'), $_data);
  123. }
  124. }
  125. }
  126. return $this;
  127. }
  128. /**
  129. * Delete data by item(s)
  130. *
  131. * @param Mage_Downloadable_Model_Link|array|int $items
  132. * @return Mage_Downloadable_Model_Mysql4_Link
  133. */
  134. public function deleteItems($items)
  135. {
  136. $where = '';
  137. if ($items instanceof Mage_Downloadable_Model_Link) {
  138. $where = $this->_getReadAdapter()->quoteInto('link_id = ?', $items->getId());
  139. }
  140. elseif (is_array($items)) {
  141. $where = $this->_getReadAdapter()->quoteInto('link_id in (?)', $items);
  142. }
  143. else {
  144. $where = $this->_getReadAdapter()->quoteInto('sample_id = ?', $items);
  145. }
  146. if ($where) {
  147. $this->_getWriteAdapter()->delete(
  148. $this->getTable('downloadable/link'), $where);
  149. $this->_getWriteAdapter()->delete(
  150. $this->getTable('downloadable/link_title'), $where);
  151. $this->_getWriteAdapter()->delete(
  152. $this->getTable('downloadable/link_price'), $where);
  153. }
  154. return $this;
  155. }
  156. /**
  157. * Retrieve links searchable data
  158. *
  159. * @param int $productId
  160. * @param int $storeId
  161. * @return array
  162. */
  163. public function getSearchableData($productId, $storeId)
  164. {
  165. $select = $this->_getReadAdapter()->select()
  166. ->from(array('link' => $this->getMainTable()), null)
  167. ->join(
  168. array('link_title_default' => $this->getTable('downloadable/link_title')),
  169. 'link_title_default.link_id=link.link_id AND link_title_default.store_id=0',
  170. array())
  171. ->joinLeft(
  172. array('link_title_store' => $this->getTable('downloadable/link_title')),
  173. 'link_title_store.link_id=link.link_id AND link_title_store.store_id=' . intval($storeId),
  174. array('title' => 'IFNULL(link_title_store.title, link_title_default.title)'))
  175. ->where('link.product_id=?', $productId);
  176. if (!$searchData = $this->_getReadAdapter()->fetchCol($select)) {
  177. $searchData = array();
  178. }
  179. return $searchData;
  180. }
  181. }