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) 2012 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:
28: /**
29: * Downloadable links resource collection
30: *
31: * @category Mage
32: * @package Mage_Downloadable
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Downloadable_Model_Resource_Link_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * Init resource model
39: */
40: protected function _construct()
41: {
42: $this->_init('downloadable/link');
43: }
44:
45: /**
46: * Method for product filter
47: *
48: * @param Mage_Catalog_Model_Product|array|integer|null $product
49: * @return Mage_Downloadable_Model_Resource_Link_Collection
50: */
51: public function addProductToFilter($product)
52: {
53: if (empty($product)) {
54: $this->addFieldToFilter('product_id', '');
55: } elseif ($product instanceof Mage_Catalog_Model_Product) {
56: $this->addFieldToFilter('product_id', $product->getId());
57: } else {
58: $this->addFieldToFilter('product_id', array('in' => $product));
59: }
60:
61: return $this;
62: }
63:
64: /**
65: * Retrieve title for for current store
66: *
67: * @param integer $storeId
68: * @return Mage_Downloadable_Model_Resource_Link_Collection
69: */
70: public function addTitleToResult($storeId = 0)
71: {
72: $ifNullDefaultTitle = $this->getConnection()
73: ->getIfNullSql('st.title', 'd.title');
74: $this->getSelect()
75: ->joinLeft(array('d' => $this->getTable('downloadable/link_title')),
76: 'd.link_id=main_table.link_id AND d.store_id = 0',
77: array('default_title' => 'title'))
78: ->joinLeft(array('st' => $this->getTable('downloadable/link_title')),
79: 'st.link_id=main_table.link_id AND st.store_id = ' . (int)$storeId,
80: array('store_title' => 'title','title' => $ifNullDefaultTitle))
81: ->order('main_table.sort_order ASC')
82: ->order('title ASC');
83:
84: return $this;
85: }
86:
87: /**
88: * Retrieve price for for current website
89: *
90: * @param integer $websiteId
91: * @return Mage_Downloadable_Model_Resource_Link_Collection
92: */
93: public function addPriceToResult($websiteId)
94: {
95: $ifNullDefaultPrice = $this->getConnection()
96: ->getIfNullSql('stp.price', 'dp.price');
97: $this->getSelect()
98: ->joinLeft(array('dp' => $this->getTable('downloadable/link_price')),
99: 'dp.link_id=main_table.link_id AND dp.website_id = 0',
100: array('default_price' => 'price'))
101: ->joinLeft(array('stp' => $this->getTable('downloadable/link_price')),
102: 'stp.link_id=main_table.link_id AND stp.website_id = ' . (int)$websiteId,
103: array('website_price' => 'price','price' => $ifNullDefaultPrice));
104:
105: return $this;
106: }
107: }
108: