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 extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_labelTable;
00043
00044
00045
00046
00047
00048
00049 protected $_priceTable;
00050
00051
00052
00053
00054
00055 protected function _construct()
00056 {
00057 $this->_init('catalog/product_super_attribute', 'product_super_attribute_id');
00058 $this->_labelTable = $this->getTable('catalog/product_super_attribute_label');
00059 $this->_priceTable = $this->getTable('catalog/product_super_attribute_pricing');
00060 }
00061
00062
00063
00064
00065
00066
00067 public function getCatalogHelper()
00068 {
00069 return Mage::helper('catalog');
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079 public function loadLabel($attribute)
00080 {
00081 return $this;
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091 public function loadPrices($attribute)
00092 {
00093 return $this;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102 public function saveLabel($attribute)
00103 {
00104 $select = $this->_getWriteAdapter()->select()
00105 ->from($this->_labelTable, 'value_id')
00106 ->where('product_super_attribute_id=?', $attribute->getId())
00107 ->where('store_id=?', (int)$attribute->getStoreId());
00108 if ($valueId = $this->_getWriteAdapter()->fetchOne($select)) {
00109 $this->_getWriteAdapter()->update($this->_labelTable,array('value'=>$attribute->getLabel()),
00110 $this->_getWriteAdapter()->quoteInto('value_id=?', $valueId)
00111 );
00112 }
00113 else {
00114 $this->_getWriteAdapter()->insert($this->_labelTable, array(
00115 'product_super_attribute_id' => $attribute->getId(),
00116 'store_id' => (int) $attribute->getStoreId(),
00117 'value' => $attribute->getLabel()
00118 ));
00119 }
00120 return $this;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129 public function savePrices($attribute)
00130 {
00131 $newValues = $attribute->getValues();
00132
00133 $oldValues = array();
00134 $valueIndexes = array();
00135 $select = $this->_getWriteAdapter()->select()
00136 ->from($this->_priceTable)
00137 ->where('product_super_attribute_id=?', $attribute->getId());
00138 $query = $this->_getWriteAdapter()->query($select);
00139 while ($row = $query->fetch()) {
00140 $key = join('-', array($row['website_id'], $row['value_index']));
00141 $oldValues[$key] = $row;
00142 }
00143
00144 $delete = array();
00145 $insert = array();
00146 $update = array();
00147
00148 foreach ($newValues as $value) {
00149 $valueIndexes[$value['value_index']] = $value['value_index'];
00150 }
00151
00152 if ($this->getCatalogHelper()->isPriceGlobal()) {
00153 foreach ($oldValues as $row) {
00154 if (!isset($valueIndexes[$row['value_index']])) {
00155 $delete[] = $row['value_id'];
00156 continue;
00157 }
00158 }
00159 foreach ($newValues as $value) {
00160 $valueObject = new Varien_Object($value);
00161 $key = join('-', array(0, $value['value_index']));
00162
00163 $pricingValue = $valueObject->getPricingValue();
00164 if ($pricingValue == '' || is_null($pricingValue)) {
00165 $pricingValue = null;
00166 }
00167 else {
00168 $pricingValue = Mage::app()->getLocale()->getNumber($pricingValue);
00169 }
00170
00171 if (isset($oldValues[$key])) {
00172 $oldValue = $oldValues[$key];
00173 $update[$oldValue['value_id']] = array(
00174 'pricing_value' => $pricingValue,
00175 'is_percent' => intval($valueObject->getIsPercent())
00176 );
00177 }
00178
00179 else {
00180 if (!empty($value['pricing_value'])) {
00181 $insert[] = array(
00182 'product_super_attribute_id' => $attribute->getId(),
00183 'value_index' => $valueObject->getValueIndex(),
00184 'is_percent' => intval($valueObject->getIsPercent()),
00185 'pricing_value' => $pricingValue,
00186 'website_id' => 0
00187 );
00188 }
00189 }
00190 }
00191 }
00192 else {
00193 $websiteId = Mage::app()->getStore($attribute->getStoreId())->getWebsiteId();
00194 foreach ($oldValues as $row) {
00195 if (!isset($valueIndexes[$row['value_index']])) {
00196 $delete[] = $row['value_id'];
00197 continue;
00198 }
00199 }
00200 foreach ($newValues as $value) {
00201 $valueObject = new Varien_Object($value);
00202 $key = join('-', array($websiteId, $value['value_index']));
00203
00204 $pricingValue = $valueObject->getPricingValue();
00205 if ($pricingValue == '' || is_null($pricingValue)) {
00206 $pricingValue = null;
00207 }
00208 else {
00209 $pricingValue = Mage::app()->getLocale()->getNumber($pricingValue);
00210 }
00211
00212
00213 if (isset($oldValues[$key])) {
00214 $oldValue = $oldValues[$key];
00215
00216 if ($websiteId && $valueObject->getUseDefaultValue()) {
00217 $delete[] = $oldValue['value_id'];
00218 }
00219 else {
00220 $update[$oldValue['value_id']] = array(
00221 'pricing_value' => $pricingValue,
00222 'is_percent' => intval($valueObject->getIsPercent())
00223 );
00224 }
00225 }
00226
00227 else {
00228 if ($websiteId && $valueObject->getUseDefaultValue()) {
00229 continue;
00230 }
00231 $insert[] = array(
00232 'product_super_attribute_id' => $attribute->getId(),
00233 'value_index' => $valueObject->getValueIndex(),
00234 'is_percent' => intval($valueObject->getIsPercent()),
00235 'pricing_value' => $pricingValue,
00236 'website_id' => $websiteId
00237 );
00238 }
00239 $key = join('-', array(0, $value['value_index']));
00240 if (!isset($oldValues[$key])) {
00241 $insert[] = array(
00242 'product_super_attribute_id' => $attribute->getId(),
00243 'value_index' => $valueObject->getValueIndex(),
00244 'is_percent' => 0,
00245 'pricing_value' => null,
00246 'website_id' => 0
00247 );
00248 }
00249 }
00250 }
00251
00252 if (!empty($delete)) {
00253 $where = $this->_getWriteAdapter()->quoteInto('value_id IN(?)', $delete);
00254 $this->_getWriteAdapter()->delete($this->_priceTable, $where);
00255 }
00256
00257 if (!empty($update)) {
00258 foreach ($update as $valueId => $valueData) {
00259 $where = $this->_getWriteAdapter()->quoteInto('value_id=?', $valueId);
00260 $this->_getWriteAdapter()->update($this->_priceTable, $valueData, $where);
00261 }
00262 }
00263
00264 if (!empty($insert)) {
00265 foreach ($insert as $valueData) {
00266 $this->_getWriteAdapter()->insert($this->_priceTable, $valueData);
00267 }
00268 }
00269
00270 return $this;
00271 }
00272 }