00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Type_Configurable_Attribute_Collection
00036 extends Mage_Core_Model_Mysql4_Collection_Abstract
00037 {
00038
00039
00040
00041
00042
00043 protected $_labelTable;
00044
00045
00046
00047
00048
00049
00050 protected $_priceTable;
00051
00052
00053
00054
00055
00056
00057 protected $_product;
00058
00059
00060
00061
00062
00063 protected function _construct()
00064 {
00065 $this->_init('catalog/product_type_configurable_attribute');
00066 $this->_labelTable = $this->getTable('catalog/product_super_attribute_label');
00067 $this->_priceTable = $this->getTable('catalog/product_super_attribute_pricing');
00068 }
00069
00070
00071
00072
00073
00074
00075 public function getHelper()
00076 {
00077 return Mage::helper('catalog');
00078 }
00079
00080
00081
00082
00083
00084
00085
00086 public function setProductFilter($product)
00087 {
00088 $this->_product = $product;
00089 $this->addFieldToFilter('product_id', $product->getId());
00090 return $this;
00091 }
00092
00093
00094
00095
00096
00097
00098
00099 public function orderByPosition($dir='asc')
00100 {
00101 $this->getSelect()->order('position '.$dir);
00102 return $this;
00103 }
00104
00105
00106
00107
00108
00109
00110 public function getStoreId()
00111 {
00112 return (int)$this->_product->getStoreId();
00113 }
00114
00115
00116
00117
00118
00119
00120 protected function _afterLoad()
00121 {
00122 parent::_afterLoad();
00123 Varien_Profiler::start('TTT1:'.__METHOD__);
00124 $this->_addProductAttributes();
00125 Varien_Profiler::stop('TTT1:'.__METHOD__);
00126 Varien_Profiler::start('TTT2:'.__METHOD__);
00127 $this->_addAssociatedProductFilters();
00128 Varien_Profiler::stop('TTT2:'.__METHOD__);
00129 Varien_Profiler::start('TTT3:'.__METHOD__);
00130 $this->_loadLabels();
00131 Varien_Profiler::stop('TTT3:'.__METHOD__);
00132 Varien_Profiler::start('TTT4:'.__METHOD__);
00133 $this->_loadPrices();
00134 Varien_Profiler::stop('TTT4:'.__METHOD__);
00135 return $this;
00136 }
00137
00138
00139
00140
00141
00142
00143 protected function _addProductAttributes()
00144 {
00145 foreach ($this->_items as $item) {
00146 $productAttribute = $this->getProduct()->getTypeInstance(true)
00147 ->getAttributeById($item->getAttributeId(), $this->getProduct());
00148 $item->setProductAttribute($productAttribute);
00149 }
00150 return $this;
00151 }
00152
00153
00154
00155
00156
00157
00158 public function _addAssociatedProductFilters()
00159 {
00160 $this->getProduct()->getTypeInstance(true)
00161 ->getUsedProducts($this->getColumnValues('attribute_id'), $this->getProduct());
00162 return $this;
00163 }
00164
00165
00166
00167
00168
00169
00170 protected function _loadLabels()
00171 {
00172 if ($this->count()) {
00173 $select = $this->getConnection()->select()
00174 ->from(array('default'=>$this->_labelTable))
00175 ->joinLeft(
00176 array('store'=>$this->_labelTable),
00177 'store.product_super_attribute_id=default.product_super_attribute_id AND store.store_id='.$this->getStoreId(),
00178 array(
00179 'store_lebel'=>'value',
00180 'label' => new Zend_Db_Expr('IFNULL(store.value, default.value)')
00181 )
00182 )
00183 ->where('default.product_super_attribute_id IN (?)', array_keys($this->_items))
00184 ->where('default.store_id=0');
00185 foreach ($this->getConnection()->fetchAll($select) as $data) {
00186 $this->getItemById($data['product_super_attribute_id'])->setLabel($data['label']);
00187 }
00188 }
00189 return $this;
00190 }
00191
00192
00193
00194
00195
00196
00197 protected function _loadPrices()
00198 {
00199 if ($this->count()) {
00200 $pricings = array(
00201 0 => array()
00202 );
00203 if ($this->getHelper()->isPriceGlobal()) {
00204 $websiteId = 0;
00205 }
00206 else {
00207 $websiteId = (int)Mage::app()->getStore($this->getStoreId())->getWebsiteId();
00208 $pricing[$websiteId] = array();
00209 }
00210 $select = $this->getConnection()->select()
00211 ->from(array('price' => $this->_priceTable))
00212 ->where('price.product_super_attribute_id IN (?)', array_keys($this->_items));
00213 if ($websiteId > 0) {
00214 $select->where('price.website_id IN(?)', array(0, $websiteId));
00215 }
00216 else {
00217 $select->where('price.website_id=0');
00218 }
00219 $query = $this->getConnection()->query($select);
00220
00221 while ($row = $query->fetch()) {
00222 $pricings[(int)$row['website_id']][] = $row;
00223 }
00224
00225 $values = array();
00226
00227 foreach ($this->_items as $item) {
00228 $productAttribute = $item->getProductAttribute();
00229 if (!($productAttribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract)) {
00230 continue;
00231 }
00232 $options = $productAttribute->getFrontend()->getSelectOptions();
00233 foreach ($options as $option) {
00234 foreach ($this->getProduct()->getTypeInstance(true)->getUsedProducts(null, $this->getProduct()) as $associatedProduct) {
00235 if (!empty($option['value'])
00236 && $option['value'] == $associatedProduct->getData(
00237 $productAttribute->getAttributeCode())) {
00238
00239 if (!isset($values[$item->getId() . ':' . $option['value']])) {
00240
00241 $values[$item->getId() . ':' . $option['value']] = array(
00242 'product_super_attribute_id' => $item->getId(),
00243 'value_index' => $option['value'],
00244 'label' => $option['label'],
00245 'default_label' => $option['label'],
00246 'store_label' => $option['label'],
00247 'is_percent' => 0,
00248 'pricing_value' => null,
00249 'use_default_value' => true
00250 );
00251 }
00252 }
00253 }
00254 }
00255 }
00256
00257 foreach ($pricings[0] as $pricing) {
00258
00259 $valueKey = $pricing['product_super_attribute_id'] . ':' . $pricing['value_index'];
00260 if (isset($values[$valueKey])) {
00261 $values[$valueKey]['pricing_value'] = $pricing['pricing_value'];
00262 $values[$valueKey]['is_percent'] = $pricing['is_percent'];
00263 $values[$valueKey]['value_id'] = $pricing['value_id'];
00264 $values[$valueKey]['use_default_value'] = true;
00265 }
00266 }
00267
00268 if ($websiteId && isset($pricings[$websiteId])) {
00269 foreach ($pricings[$websiteId] as $pricing) {
00270 $valueKey = $pricing['product_super_attribute_id'] . ':' . $pricing['value_index'];
00271 if (isset($values[$valueKey])) {
00272 $values[$valueKey]['pricing_value'] = $pricing['pricing_value'];
00273 $values[$valueKey]['is_percent'] = $pricing['is_percent'];
00274 $values[$valueKey]['value_id'] = $pricing['value_id'];
00275 $values[$valueKey]['use_default_value'] = false;
00276 }
00277 }
00278 }
00279
00280 foreach ($values as $data) {
00281 $this->getItemById($data['product_super_attribute_id'])->addPrice($data);
00282 }
00283 }
00284 return $this;
00285 }
00286
00287
00288
00289
00290
00291
00292 public function getProduct()
00293 {
00294 return $this->_product;
00295 }
00296 }