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 extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037 protected function _construct()
00038 {
00039 $this->_init('catalog/product_option', 'option_id');
00040 }
00041
00042 protected function _afterSave(Mage_Core_Model_Abstract $object)
00043 {
00044 $priceTable = $this->getTable('catalog/product_option_price');
00045 $titleTable = $this->getTable('catalog/product_option_title');
00046
00047
00048 if ($object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_FIELD
00049 || $object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_AREA
00050 || $object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_FILE
00051 || $object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE
00052 || $object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE_TIME
00053 || $object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_TIME
00054 ) {
00055
00056
00057 if (!$object->getData('scope', 'price')) {
00058 $statement = $this->_getReadAdapter()->select()
00059 ->from($priceTable)
00060 ->where('option_id = '.$object->getId().' AND store_id = ?', 0);
00061 if ($this->_getReadAdapter()->fetchOne($statement)) {
00062 if ($object->getStoreId() == '0') {
00063 $this->_getWriteAdapter()->update(
00064 $priceTable,
00065 array(
00066 'price' => $object->getPrice(),
00067 'price_type' => $object->getPriceType()
00068 ),
00069 $this->_getWriteAdapter()->quoteInto('option_id = '.$object->getId().' AND store_id = ?', 0)
00070 );
00071 }
00072 } else {
00073 $this->_getWriteAdapter()->insert(
00074 $priceTable,
00075 array(
00076 'option_id' => $object->getId(),
00077 'store_id' => 0,
00078 'price' => $object->getPrice(),
00079 'price_type' => $object->getPriceType()
00080 )
00081 );
00082 }
00083 }
00084
00085 $scope = (int) Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
00086
00087 if ($object->getStoreId() != '0' && $scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE
00088 && !$object->getData('scope', 'price')) {
00089
00090 $baseCurrency = Mage::app()->getBaseCurrencyCode();
00091
00092 $storeIds = $object->getProduct()->getStoreIds();
00093 if (is_array($storeIds)) {
00094 foreach ($storeIds as $storeId) {
00095 if ($object->getPriceType() == 'fixed') {
00096 $storeCurrency = Mage::app()->getStore($storeId)->getBaseCurrencyCode();
00097 $rate = Mage::getModel('directory/currency')->load($baseCurrency)->getRate($storeCurrency);
00098 if (!$rate) {
00099 $rate=1;
00100 }
00101 $newPrice = $object->getPrice() * $rate;
00102 } else {
00103 $newPrice = $object->getPrice();
00104 }
00105 $statement = $this->_getReadAdapter()->select()
00106 ->from($priceTable)
00107 ->where('option_id = '.$object->getId().' AND store_id = ?', $storeId);
00108
00109 if ($this->_getReadAdapter()->fetchOne($statement)) {
00110 $this->_getWriteAdapter()->update(
00111 $priceTable,
00112 array(
00113 'price' => $newPrice,
00114 'price_type' => $object->getPriceType()
00115 ),
00116 $this->_getWriteAdapter()->quoteInto('option_id = '.$object->getId().' AND store_id = ?', $storeId)
00117 );
00118 } else {
00119 $this->_getWriteAdapter()->insert(
00120 $priceTable,
00121 array(
00122 'option_id' => $object->getId(),
00123 'store_id' => $storeId,
00124 'price' => $newPrice,
00125 'price_type' => $object->getPriceType()
00126 )
00127 );
00128 }
00129 }
00130 }
00131 } elseif ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE && $object->getData('scope', 'price')) {
00132 $this->_getWriteAdapter()->delete(
00133 $priceTable,
00134 $this->_getWriteAdapter()->quoteInto('option_id = '.$object->getId().' AND store_id = ?', $object->getStoreId())
00135 );
00136 }
00137 }
00138
00139
00140 if (!$object->getData('scope', 'title')) {
00141 $statement = $this->_getReadAdapter()->select()
00142 ->from($titleTable)
00143 ->where('option_id = '.$object->getId().' and store_id = ?', 0);
00144
00145 if ($this->_getReadAdapter()->fetchOne($statement)) {
00146 if ($object->getStoreId() == '0') {
00147 $this->_getWriteAdapter()->update(
00148 $titleTable,
00149 array('title' => $object->getTitle()),
00150 $this->_getWriteAdapter()->quoteInto('option_id='.$object->getId().' AND store_id=?', 0)
00151 );
00152 }
00153 } else {
00154 $this->_getWriteAdapter()->insert(
00155 $titleTable,
00156 array(
00157 'option_id' => $object->getId(),
00158 'store_id' => 0,
00159 'title' => $object->getTitle()
00160 ));
00161 }
00162 }
00163
00164 if ($object->getStoreId() != '0' && !$object->getData('scope', 'title')) {
00165 $statement = $this->_getReadAdapter()->select()
00166 ->from($titleTable)
00167 ->where('option_id = '.$object->getId().' and store_id = ?', $object->getStoreId());
00168
00169 if ($this->_getReadAdapter()->fetchOne($statement)) {
00170 $this->_getWriteAdapter()->update(
00171 $titleTable,
00172 array('title' => $object->getTitle()),
00173 $this->_getWriteAdapter()
00174 ->quoteInto('option_id='.$object->getId().' AND store_id=?', $object->getStoreId()));
00175 } else {
00176 $this->_getWriteAdapter()->insert(
00177 $titleTable,
00178 array(
00179 'option_id' => $object->getId(),
00180 'store_id' => $object->getStoreId(),
00181 'title' => $object->getTitle()
00182 ));
00183 }
00184 } elseif ($object->getData('scope', 'title')) {
00185 $this->_getWriteAdapter()->delete(
00186 $titleTable,
00187 $this->_getWriteAdapter()->quoteInto('option_id = '.$object->getId().' AND store_id = ?', $object->getStoreId())
00188 );
00189 }
00190
00191 return parent::_afterSave($object);
00192 }
00193
00194 public function deletePrices($option_id)
00195 {
00196 $condition = $this->_getWriteAdapter()->quoteInto('option_id=?', $option_id);
00197
00198 $this->_getWriteAdapter()->delete(
00199 $this->getTable('catalog/product_option_price'),
00200 $condition);
00201
00202 return $this;
00203 }
00204
00205 public function deleteTitles($option_id)
00206 {
00207 $condition = $this->_getWriteAdapter()->quoteInto('option_id=?', $option_id);
00208
00209 $this->_getWriteAdapter()->delete(
00210 $this->getTable('catalog/product_option_title'),
00211 $condition);
00212
00213 return $this;
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 public function duplicate(Mage_Catalog_Model_Product_Option $object, $oldProductId, $newProductId)
00225 {
00226 $write = $this->_getWriteAdapter();
00227 $read = $this->_getReadAdapter();
00228
00229 $optionsCond = array();
00230 $optionsData = array();
00231
00232
00233 $select = $read->select()
00234 ->from($this->getTable('catalog/product_option'))
00235 ->where('product_id=?', $oldProductId);
00236 $query = $read->query($select);
00237 while ($row = $query->fetch()) {
00238 $optionsData[$row['option_id']] = $row;
00239 $optionsData[$row['option_id']]['product_id'] = $newProductId;
00240 unset($optionsData[$row['option_id']]['option_id']);
00241 }
00242
00243
00244 foreach ($optionsData as $oId => $data) {
00245 $write->insert($this->getMainTable(), $data);
00246 $optionsCond[$oId] = $write->lastInsertId();
00247 }
00248
00249
00250 foreach ($optionsCond as $oldOptionId => $newOptionId) {
00251
00252 $table = $this->getTable('catalog/product_option_title');
00253 $sql = 'REPLACE INTO `' . $table . '` '
00254 . 'SELECT NULL, ' . $newOptionId . ', `store_id`, `title`'
00255 . 'FROM `' . $table . '` WHERE `option_id`=' . $oldOptionId;
00256 $this->_getWriteAdapter()->query($sql);
00257
00258
00259 $table = $this->getTable('catalog/product_option_price');
00260 $sql = 'REPLACE INTO `' . $table . '` '
00261 . 'SELECT NULL, ' . $newOptionId . ', `store_id`, `price`, `price_type`'
00262 . 'FROM `' . $table . '` WHERE `option_id`=' . $oldOptionId;
00263 $this->_getWriteAdapter()->query($sql);
00264
00265 $object->getValueInstance()->duplicate($oldOptionId, $newOptionId);
00266 }
00267
00268 return $object;
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278 public function getSearchableData($productId, $storeId)
00279 {
00280 $searchData = array();
00281
00282 $select = $this->_getReadAdapter()->select()
00283 ->from(array('option' => $this->getMainTable()), null)
00284 ->join(
00285 array('option_title_default' => $this->getTable('catalog/product_option_title')),
00286 'option_title_default.option_id=option.option_id AND option_title_default.store_id=0',
00287 array())
00288 ->joinLeft(
00289 array('option_title_store' => $this->getTable('catalog/product_option_title')),
00290 'option_title_store.option_id=option.option_id AND option_title_store.store_id=' . intval($storeId),
00291 array('title' => 'IFNULL(option_title_store.title, option_title_default.title)'))
00292 ->where('option.product_id=?', $productId);
00293 if ($titles = $this->_getReadAdapter()->fetchCol($select)) {
00294 $searchData = array_merge($searchData, $titles);
00295 }
00296
00297
00298 $select = $this->_getReadAdapter()->select()
00299 ->from(array('option' => $this->getMainTable()), null)
00300 ->join(
00301 array('option_type' => $this->getTable('catalog/product_option_type_value')),
00302 'option_type.option_id=option.option_id',
00303 array())
00304 ->join(
00305 array('option_title_default' => $this->getTable('catalog/product_option_type_title')),
00306 'option_title_default.option_type_id=option_type.option_type_id AND option_title_default.store_id=0',
00307 array())
00308 ->joinLeft(
00309 array('option_title_store' => $this->getTable('catalog/product_option_type_title')),
00310 'option_title_store.option_type_id=option_type.option_type_id AND option_title_store.store_id=' . intval($storeId),
00311 array('title' => 'IFNULL(option_title_store.title, option_title_default.title)'))
00312 ->where('option.product_id=?', $productId);
00313 if ($titles = $this->_getReadAdapter()->fetchCol($select)) {
00314 $searchData = array_merge($searchData, $titles);
00315 }
00316
00317 return $searchData;
00318 }
00319 }