1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Model_Resource_Product_Type_Configurable_Attribute extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_labelTable;
43:
44: 45: 46: 47: 48:
49: protected $_priceTable;
50:
51: 52: 53: 54:
55: protected function _construct()
56: {
57: $this->_init('catalog/product_super_attribute', 'product_super_attribute_id');
58: $this->_labelTable = $this->getTable('catalog/product_super_attribute_label');
59: $this->_priceTable = $this->getTable('catalog/product_super_attribute_pricing');
60: }
61:
62: 63: 64: 65: 66:
67: public function getCatalogHelper()
68: {
69: return Mage::helper('catalog');
70: }
71:
72: 73: 74: 75: 76: 77: 78:
79: public function loadLabel($attribute)
80: {
81: return $this;
82: }
83:
84: 85: 86: 87: 88: 89: 90:
91: public function loadPrices($attribute)
92: {
93: return $this;
94: }
95:
96: 97: 98: 99: 100: 101:
102: public function saveLabel($attribute)
103: {
104: $adapter = $this->_getWriteAdapter();
105:
106: $select = $adapter->select()
107: ->from($this->_labelTable, 'value_id')
108: ->where('product_super_attribute_id = :product_super_attribute_id')
109: ->where('store_id = :store_id');
110: $bind = array(
111: 'product_super_attribute_id' => (int)$attribute->getId(),
112: 'store_id' => (int)$attribute->getStoreId()
113: );
114: $valueId = $adapter->fetchOne($select, $bind);
115: if ($valueId) {
116: $adapter->update(
117: $this->_labelTable,
118: array(
119: 'use_default' => (int) $attribute->getUseDefault(),
120: 'value' => $attribute->getLabel()
121: ),
122: $adapter->quoteInto('value_id = ?', (int) $valueId)
123: );
124: } else {
125: $adapter->insert(
126: $this->_labelTable,
127: array(
128: 'product_super_attribute_id' => (int) $attribute->getId(),
129: 'store_id' => (int) $attribute->getStoreId(),
130: 'use_default' => (int) $attribute->getUseDefault(),
131: 'value' => $attribute->getLabel()
132: )
133: );
134: }
135: return $this;
136: }
137:
138: 139: 140: 141: 142: 143:
144: public function savePrices($attribute)
145: {
146: $write = $this->_getWriteAdapter();
147:
148: if ($this->getCatalogHelper()->isPriceGlobal()) {
149: $websiteId = 0;
150: } else {
151: $websiteId = (int)Mage::app()->getStore($attribute->getStoreId())->getWebsite()->getId();
152: }
153:
154: $values = $attribute->getValues();
155: if (!is_array($values)) {
156: $values = array();
157: }
158:
159: $new = array();
160: $old = array();
161:
162:
163: $select = $write->select()
164: ->from($this->_priceTable)
165: ->where('product_super_attribute_id = :product_super_attribute_id')
166: ->where('website_id = :website_id');
167:
168: $bind = array(
169: 'product_super_attribute_id' => (int)$attribute->getId(),
170: 'website_id' => $websiteId
171: );
172: $rowSet = $write->fetchAll($select, $bind);
173: foreach ($rowSet as $row) {
174: $key = implode('-', array($row['website_id'], $row['value_index']));
175: if (!isset($old[$key])) {
176: $old[$key] = $row;
177: } else {
178:
179: $where = $write->quoteInto('value_id = ?', $row['value_id']);
180: $write->delete($this->_priceTable, $where);
181: }
182: }
183:
184:
185: foreach ($values as $v) {
186: if (empty($v['value_index'])) {
187: continue;
188: }
189: $key = implode('-', array($websiteId, $v['value_index']));
190: $new[$key] = array(
191: 'value_index' => $v['value_index'],
192: 'pricing_value' => $v['pricing_value'],
193: 'is_percent' => $v['is_percent'],
194: 'website_id' => $websiteId,
195: 'use_default' => !empty($v['use_default_value']) ? true : false
196: );
197: }
198:
199: $insert = array();
200: $update = array();
201: $delete = array();
202:
203: foreach ($old as $k => $v) {
204: if (!isset($new[$k])) {
205: $delete[] = $v['value_id'];
206: }
207: }
208: foreach ($new as $k => $v) {
209: $needInsert = false;
210: $needUpdate = false;
211: $needDelete = false;
212:
213: $isGlobal = true;
214: if (!$this->getCatalogHelper()->isPriceGlobal() && $websiteId != 0) {
215: $isGlobal = false;
216: }
217:
218: $hasValue = ($isGlobal && !empty($v['pricing_value']))
219: || (!$isGlobal && !$v['use_default']);
220:
221: if (isset($old[$k])) {
222:
223: $dataChanged = ($old[$k]['is_percent'] != $v['is_percent'])
224: || ($old[$k]['pricing_value'] != $v['pricing_value']);
225: if (!$hasValue) {
226: $needDelete = true;
227: } else if ($dataChanged) {
228: $needUpdate = true;
229: }
230: } else if ($hasValue) {
231: $needInsert = true;
232: }
233:
234: if (!$isGlobal && empty($v['pricing_value'])) {
235: $v['pricing_value'] = 0;
236: $v['is_percent'] = 0;
237: }
238:
239: if ($needInsert) {
240: $insert[] = array(
241: 'product_super_attribute_id' => $attribute->getId(),
242: 'value_index' => $v['value_index'],
243: 'is_percent' => $v['is_percent'],
244: 'pricing_value' => $v['pricing_value'],
245: 'website_id' => $websiteId
246: );
247: }
248: if ($needUpdate) {
249: $update[$old[$k]['value_id']] = array(
250: 'is_percent' => $v['is_percent'],
251: 'pricing_value' => $v['pricing_value']
252: );
253: }
254: if ($needDelete) {
255: $delete[] = $old[$k]['value_id'];
256: }
257: }
258:
259: if (!empty($delete)) {
260: $where = $write->quoteInto('value_id IN(?)', $delete);
261: $write->delete($this->_priceTable, $where);
262: }
263: if (!empty($update)) {
264: foreach ($update as $valueId => $bind) {
265: $where = $write->quoteInto('value_id=?', $valueId);
266: $write->update($this->_priceTable, $bind, $where);
267: }
268: }
269: if (!empty($insert)) {
270: $write->insertMultiple($this->_priceTable, $insert);
271: }
272:
273:
274: return $this;
275: }
276:
277: 278: 279: 280: 281: 282:
283: public function getUsedAttributes($setId)
284: {
285: $adapter = $this->_getReadAdapter();
286: $select = $adapter->select()
287: ->distinct(true)
288: ->from(array('e' => $this->getTable('catalog/product')), null)
289: ->join(
290: array('a' => $this->getMainTable()),
291: 'e.entity_id = a.product_id',
292: array('attribute_id')
293: )
294: ->where('e.attribute_set_id = :attribute_set_id')
295: ->where('e.type_id = :type_id');
296:
297: $bind = array(
298: 'attribute_set_id' => $setId,
299: 'type_id' => Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE,
300: );
301:
302: return $adapter->fetchCol($select, $bind);
303: }
304: }
305: