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_Option_Value extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037 protected function _construct()
00038 {
00039 $this->_init('catalog/product_option_type_value', 'option_type_id');
00040 }
00041
00042 protected function _afterSave(Mage_Core_Model_Abstract $object)
00043 {
00044 $priceTable = $this->getTable('catalog/product_option_type_price');
00045 $titleTable = $this->getTable('catalog/product_option_type_title');
00046
00047 if (!$object->getData('scope', 'price')) {
00048
00049 $statement = $this->_getReadAdapter()->select()
00050 ->from($priceTable)
00051 ->where('option_type_id = '.$object->getId().' AND store_id = ?', 0);
00052 if ($this->_getReadAdapter()->fetchOne($statement)) {
00053 if ($object->getStoreId() == '0') {
00054 $this->_getWriteAdapter()->update(
00055 $priceTable,
00056 array(
00057 'price' => $object->getPrice(),
00058 'price_type' => $object->getPriceType()
00059 ),
00060 $this->_getWriteAdapter()->quoteInto('option_type_id = '.$object->getId().' AND store_id = ?', 0)
00061 );
00062 }
00063 } else {
00064 $this->_getWriteAdapter()->insert(
00065 $priceTable,
00066 array(
00067 'option_type_id' => $object->getId(),
00068 'store_id' => 0,
00069 'price' => $object->getPrice(),
00070 'price_type' => $object->getPriceType()
00071 )
00072 );
00073 }
00074 }
00075
00076 $scope = (int) Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
00077
00078 if ($object->getStoreId() != '0' && $scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE
00079 && !$object->getData('scope', 'price')) {
00080
00081 $baseCurrency = Mage::app()->getBaseCurrencyCode();
00082
00083 $storeIds = $object->getProduct()->getStoreIds();
00084 if (is_array($storeIds)) {
00085 foreach ($storeIds as $storeId) {
00086 if ($object->getPriceType() == 'fixed') {
00087 $storeCurrency = Mage::app()->getStore($storeId)->getBaseCurrencyCode();
00088 $rate = Mage::getModel('directory/currency')->load($baseCurrency)->getRate($storeCurrency);
00089 if (!$rate) {
00090 $rate = 1;
00091 }
00092 $newPrice = $object->getPrice() * $rate;
00093 } else {
00094 $newPrice = $object->getPrice();
00095 }
00096 $statement = $this->_getReadAdapter()->select()
00097 ->from($priceTable)
00098 ->where('option_type_id = '.$object->getId().' AND store_id = ?', $storeId);
00099
00100 if ($this->_getReadAdapter()->fetchOne($statement)) {
00101 $this->_getWriteAdapter()->update(
00102 $priceTable,
00103 array(
00104 'price' => $newPrice,
00105 'price_type' => $object->getPriceType()
00106 ),
00107 $this->_getWriteAdapter()->quoteInto('option_type_id = '.$object->getId().' AND store_id = ?', $storeId)
00108 );
00109 } else {
00110 $this->_getWriteAdapter()->insert(
00111 $priceTable,
00112 array(
00113 'option_type_id' => $object->getId(),
00114 'store_id' => $storeId,
00115 'price' => $newPrice,
00116 'price_type' => $object->getPriceType()
00117 )
00118 );
00119 }
00120 }
00121 }
00122 } elseif ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE && $object->getData('scope', 'price')) {
00123 $this->_getWriteAdapter()->delete(
00124 $priceTable,
00125 $this->_getWriteAdapter()->quoteInto('option_type_id = '.$object->getId().' AND store_id = ?', $object->getStoreId())
00126 );
00127 }
00128
00129
00130 if (!$object->getData('scope', 'title')) {
00131 $statement = $this->_getReadAdapter()->select()
00132 ->from($titleTable)
00133 ->where('option_type_id = '.$object->getId().' AND store_id = ?', 0);
00134
00135 if ($this->_getReadAdapter()->fetchOne($statement)) {
00136 if ($object->getStoreId() == '0') {
00137 $this->_getWriteAdapter()->update(
00138 $titleTable,
00139 array('title' => $object->getTitle()),
00140 $this->_getWriteAdapter()->quoteInto('option_type_id='.$object->getId().' AND store_id=?', 0)
00141 );
00142 }
00143 } else {
00144 $this->_getWriteAdapter()->insert(
00145 $titleTable,
00146 array(
00147 'option_type_id' => $object->getId(),
00148 'store_id' => 0,
00149 'title' => $object->getTitle()
00150 ));
00151 }
00152 }
00153
00154 if ($object->getStoreId() != '0' && !$object->getData('scope', 'title')) {
00155 $statement = $this->_getReadAdapter()->select()
00156 ->from($titleTable)
00157 ->where('option_type_id = '.$object->getId().' AND store_id = ?', $object->getStoreId());
00158
00159 if ($this->_getReadAdapter()->fetchOne($statement)) {
00160 $this->_getWriteAdapter()->update(
00161 $titleTable,
00162 array('title' => $object->getTitle()),
00163 $this->_getWriteAdapter()
00164 ->quoteInto('option_type_id='.$object->getId().' AND store_id=?', $object->getStoreId()));
00165 } else {
00166 $this->_getWriteAdapter()->insert(
00167 $titleTable,
00168 array(
00169 'option_type_id' => $object->getId(),
00170 'store_id' => $object->getStoreId(),
00171 'title' => $object->getTitle()
00172 ));
00173 }
00174 } elseif ($object->getData('scope', 'title')) {
00175 $this->_getWriteAdapter()->delete(
00176 $titleTable,
00177 $this->_getWriteAdapter()->quoteInto('option_type_id = '.$object->getId().' AND store_id = ?', $object->getStoreId())
00178 );
00179 }
00180
00181 return parent::_afterSave($object);
00182 }
00183
00184 public function deleteValue($option_id)
00185 {
00186 $condition = $this->_getWriteAdapter()->quoteInto('option_id=?', $option_id);
00187
00188 $statement = $this->_getReadAdapter()->select()
00189 ->from($this->getTable('catalog/product_option_type_value'))
00190 ->where($condition);
00191
00192 foreach ($this->_getReadAdapter()->fetchAll($statement) as $optionType) {
00193 $this->deleteValues($optionType['option_type_id']);
00194 }
00195
00196 $this->_getWriteAdapter()->delete(
00197 $this->getMainTable(),
00198 $condition
00199 );
00200
00201 return $this;
00202 }
00203
00204 public function deleteValues($option_type_id)
00205 {
00206 $childCondition = $this->_getWriteAdapter()->quoteInto('option_type_id=?', $option_type_id);
00207 $this->_getWriteAdapter()->delete(
00208 $this->getTable('catalog/product_option_type_price'),
00209 $childCondition
00210 );
00211 $this->_getWriteAdapter()->delete(
00212 $this->getTable('catalog/product_option_type_title'),
00213 $childCondition
00214 );
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 public function duplicate(Mage_Catalog_Model_Product_Option_Value $object, $oldOptionId, $newOptionId)
00226 {
00227 $select = $this->_getReadAdapter()->select()
00228 ->from($this->getMainTable())
00229 ->where('option_id=?', $oldOptionId);
00230 $valueData = $this->_getReadAdapter()->fetchAll($select);
00231
00232 $valueCond = array();
00233
00234 foreach ($valueData as $data) {
00235 $optionTypeId = $data[$this->getIdFieldName()];
00236 unset($data[$this->getIdFieldName()]);
00237 $data['option_id'] = $newOptionId;
00238
00239 $this->_getWriteAdapter()->insert($this->getMainTable(), $data);
00240 $valueCond[$optionTypeId] = $this->_getWriteAdapter()->lastInsertId();
00241 }
00242
00243 unset($valueData);
00244
00245 foreach ($valueCond as $oldTypeId => $newTypeId) {
00246
00247 $table = $this->getTable('catalog/product_option_type_price');
00248 $sql = 'REPLACE INTO `' . $table . '` '
00249 . 'SELECT NULL, ' . $newTypeId . ', `store_id`, `price`, `price_type`'
00250 . 'FROM `' . $table . '` WHERE `option_type_id`=' . $oldTypeId;
00251 $this->_getWriteAdapter()->query($sql);
00252
00253
00254 $table = $this->getTable('catalog/product_option_type_title');
00255 $sql = 'REPLACE INTO `' . $table . '` '
00256 . 'SELECT NULL, ' . $newTypeId . ', `store_id`, `title`'
00257 . 'FROM `' . $table . '` WHERE `option_type_id`=' . $oldTypeId;
00258 $this->_getWriteAdapter()->query($sql);
00259 }
00260
00261 return $object;
00262 }
00263 }