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_Catalog
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: * Catalog product abstract price backend attribute model with customer group specific
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: abstract class Mage_Catalog_Model_Resource_Product_Attribute_Backend_Groupprice_Abstract
36: extends Mage_Core_Model_Resource_Db_Abstract
37: {
38: /**
39: * Load Tier Prices for product
40: *
41: * @param int $productId
42: * @param int $websiteId
43: * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Tierprice
44: */
45: public function loadPriceData($productId, $websiteId = null)
46: {
47: $adapter = $this->_getReadAdapter();
48:
49: $columns = array(
50: 'price_id' => $this->getIdFieldName(),
51: 'website_id' => 'website_id',
52: 'all_groups' => 'all_groups',
53: 'cust_group' => 'customer_group_id',
54: 'price' => 'value',
55: );
56:
57: $columns = $this->_loadPriceDataColumns($columns);
58:
59: $select = $adapter->select()
60: ->from($this->getMainTable(), $columns)
61: ->where('entity_id=?', $productId);
62:
63: $this->_loadPriceDataSelect($select);
64:
65: if (!is_null($websiteId)) {
66: if ($websiteId == '0') {
67: $select->where('website_id = ?', $websiteId);
68: } else {
69: $select->where('website_id IN(?)', array(0, $websiteId));
70: }
71: }
72:
73: return $adapter->fetchAll($select);
74: }
75:
76: /**
77: * Load specific sql columns
78: *
79: * @param array $columns
80: * @return array
81: */
82: protected function _loadPriceDataColumns($columns)
83: {
84: return $columns;
85: }
86:
87: /**
88: * Load specific db-select data
89: *
90: * @param Varien_Db_Select $select
91: * @return Varien_Db_Select
92: */
93: protected function _loadPriceDataSelect($select)
94: {
95: return $select;
96: }
97:
98: /**
99: * Delete Tier Prices for product
100: *
101: * @param int $productId
102: * @param int $websiteId
103: * @param int $priceId
104: * @return int The number of affected rows
105: */
106: public function deletePriceData($productId, $websiteId = null, $priceId = null)
107: {
108: $adapter = $this->_getWriteAdapter();
109:
110: $conds = array(
111: $adapter->quoteInto('entity_id = ?', $productId)
112: );
113:
114: if (!is_null($websiteId)) {
115: $conds[] = $adapter->quoteInto('website_id = ?', $websiteId);
116: }
117:
118: if (!is_null($priceId)) {
119: $conds[] = $adapter->quoteInto($this->getIdFieldName() . ' = ?', $priceId);
120: }
121:
122: $where = implode(' AND ', $conds);
123:
124: return $adapter->delete($this->getMainTable(), $where);
125: }
126:
127: /**
128: * Save tier price object
129: *
130: * @param Varien_Object $priceObject
131: * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Tierprice
132: */
133: public function savePriceData(Varien_Object $priceObject)
134: {
135: $adapter = $this->_getWriteAdapter();
136: $data = $this->_prepareDataForTable($priceObject, $this->getMainTable());
137:
138: if (!empty($data[$this->getIdFieldName()])) {
139: $where = $adapter->quoteInto($this->getIdFieldName() . ' = ?', $data[$this->getIdFieldName()]);
140: unset($data[$this->getIdFieldName()]);
141: $adapter->update($this->getMainTable(), $data, $where);
142: } else {
143: $adapter->insert($this->getMainTable(), $data);
144: }
145: return $this;
146: }
147: }
148: