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_Collection
36: extends Mage_Core_Model_Resource_Db_Collection_Abstract
37: {
38: 39: 40: 41: 42:
43: protected $_labelTable;
44:
45: 46: 47: 48: 49:
50: protected $_priceTable;
51:
52: 53: 54: 55: 56:
57: protected $_product;
58:
59: 60: 61: 62:
63: protected function _construct()
64: {
65: $this->_init('catalog/product_type_configurable_attribute');
66: $this->_labelTable = $this->getTable('catalog/product_super_attribute_label');
67: $this->_priceTable = $this->getTable('catalog/product_super_attribute_pricing');
68: }
69:
70: 71: 72: 73: 74:
75: public function getHelper()
76: {
77: return Mage::helper('catalog');
78: }
79:
80: 81: 82: 83: 84: 85:
86: public function setProductFilter($product)
87: {
88: $this->_product = $product;
89: return $this->addFieldToFilter('product_id', $product->getId());
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function orderByPosition($dir = self::SORT_ORDER_ASC)
99: {
100: $this->setOrder('position ', $dir);
101: return $this;
102: }
103:
104: 105: 106: 107: 108:
109: public function getStoreId()
110: {
111: return (int)$this->_product->getStoreId();
112: }
113:
114: 115: 116: 117: 118:
119: protected function _afterLoad()
120: {
121: parent::_afterLoad();
122: Varien_Profiler::start('TTT1:'.__METHOD__);
123: $this->_addProductAttributes();
124: Varien_Profiler::stop('TTT1:'.__METHOD__);
125: Varien_Profiler::start('TTT2:'.__METHOD__);
126: $this->_addAssociatedProductFilters();
127: Varien_Profiler::stop('TTT2:'.__METHOD__);
128: Varien_Profiler::start('TTT3:'.__METHOD__);
129: $this->_loadLabels();
130: Varien_Profiler::stop('TTT3:'.__METHOD__);
131: Varien_Profiler::start('TTT4:'.__METHOD__);
132: $this->_loadPrices();
133: Varien_Profiler::stop('TTT4:'.__METHOD__);
134: return $this;
135: }
136:
137: 138: 139: 140: 141:
142: protected function _addProductAttributes()
143: {
144: foreach ($this->_items as $item) {
145: $productAttribute = $this->getProduct()->getTypeInstance(true)
146: ->getAttributeById($item->getAttributeId(), $this->getProduct());
147: $item->setProductAttribute($productAttribute);
148: }
149: return $this;
150: }
151:
152: 153: 154: 155: 156:
157: public function _addAssociatedProductFilters()
158: {
159: $this->getProduct()->getTypeInstance(true)
160: ->getUsedProducts($this->getColumnValues('attribute_id'), $this->getProduct());
161: return $this;
162: }
163:
164: 165: 166: 167: 168:
169: protected function _loadLabels()
170: {
171: if ($this->count()) {
172: $useDefaultCheck = $this->getConnection()->getCheckSql(
173: 'store.use_default IS NULL',
174: 'def.use_default',
175: 'store.use_default'
176: );
177:
178: $labelCheck = $this->getConnection()->getCheckSql(
179: 'store.value IS NULL',
180: 'def.value',
181: 'store.value'
182: );
183:
184: $select = $this->getConnection()->select()
185: ->from(array('def' => $this->_labelTable))
186: ->joinLeft(
187: array('store' => $this->_labelTable),
188: $this->getConnection()->quoteInto('store.product_super_attribute_id = def.product_super_attribute_id AND store.store_id = ?', $this->getStoreId()),
189: array(
190: 'use_default' => $useDefaultCheck,
191: 'label' => $labelCheck
192: ))
193: ->where('def.product_super_attribute_id IN (?)', array_keys($this->_items))
194: ->where('def.store_id = ?', 0);
195:
196: $result = $this->getConnection()->fetchAll($select);
197: foreach ($result as $data) {
198: $this->getItemById($data['product_super_attribute_id'])->setLabel($data['label']);
199: $this->getItemById($data['product_super_attribute_id'])->setUseDefault($data['use_default']);
200: }
201: }
202: return $this;
203: }
204:
205: 206: 207: 208: 209:
210: protected function _loadPrices()
211: {
212: if ($this->count()) {
213: $pricings = array(
214: 0 => array()
215: );
216:
217: if ($this->getHelper()->isPriceGlobal()) {
218: $websiteId = 0;
219: } else {
220: $websiteId = (int)Mage::app()->getStore($this->getStoreId())->getWebsiteId();
221: $pricing[$websiteId] = array();
222: }
223:
224: $select = $this->getConnection()->select()
225: ->from(array('price' => $this->_priceTable))
226: ->where('price.product_super_attribute_id IN (?)', array_keys($this->_items));
227:
228: if ($websiteId > 0) {
229: $select->where('price.website_id IN(?)', array(0, $websiteId));
230: } else {
231: $select->where('price.website_id = ?', 0);
232: }
233:
234: $query = $this->getConnection()->query($select);
235:
236: while ($row = $query->fetch()) {
237: $pricings[(int)$row['website_id']][] = $row;
238: }
239:
240: $values = array();
241:
242: foreach ($this->_items as $item) {
243: $productAttribute = $item->getProductAttribute();
244: if (!($productAttribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract)) {
245: continue;
246: }
247: $options = $productAttribute->getFrontend()->getSelectOptions();
248: foreach ($options as $option) {
249: foreach ($this->getProduct()->getTypeInstance(true)->getUsedProducts(null, $this->getProduct()) as $associatedProduct) {
250: if (!empty($option['value'])
251: && $option['value'] == $associatedProduct->getData(
252: $productAttribute->getAttributeCode())) {
253:
254: if (!isset($values[$item->getId() . ':' . $option['value']])) {
255:
256: $values[$item->getId() . ':' . $option['value']] = array(
257: 'product_super_attribute_id' => $item->getId(),
258: 'value_index' => $option['value'],
259: 'label' => $option['label'],
260: 'default_label' => $option['label'],
261: 'store_label' => $option['label'],
262: 'is_percent' => 0,
263: 'pricing_value' => null,
264: 'use_default_value' => true
265: );
266: }
267: }
268: }
269: }
270: }
271:
272: foreach ($pricings[0] as $pricing) {
273:
274: $valueKey = $pricing['product_super_attribute_id'] . ':' . $pricing['value_index'];
275: if (isset($values[$valueKey])) {
276: $values[$valueKey]['pricing_value'] = $pricing['pricing_value'];
277: $values[$valueKey]['is_percent'] = $pricing['is_percent'];
278: $values[$valueKey]['value_id'] = $pricing['value_id'];
279: $values[$valueKey]['use_default_value'] = true;
280: }
281: }
282:
283: if ($websiteId && isset($pricings[$websiteId])) {
284: foreach ($pricings[$websiteId] as $pricing) {
285: $valueKey = $pricing['product_super_attribute_id'] . ':' . $pricing['value_index'];
286: if (isset($values[$valueKey])) {
287: $values[$valueKey]['pricing_value'] = $pricing['pricing_value'];
288: $values[$valueKey]['is_percent'] = $pricing['is_percent'];
289: $values[$valueKey]['value_id'] = $pricing['value_id'];
290: $values[$valueKey]['use_default_value'] = false;
291: }
292: }
293: }
294:
295: foreach ($values as $data) {
296: $this->getItemById($data['product_super_attribute_id'])->addPrice($data);
297: }
298: }
299: return $this;
300: }
301:
302: 303: 304: 305: 306:
307: public function getProduct()
308: {
309: return $this->_product;
310: }
311: }
312: