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_Option_Value extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('catalog/product_option_type_value', 'option_type_id');
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: protected function _afterSave(Mage_Core_Model_Abstract $object)
54: {
55: $this->_saveValuePrices($object);
56: $this->_saveValueTitles($object);
57:
58: return parent::_afterSave($object);
59: }
60:
61: 62: 63: 64: 65:
66: protected function _saveValuePrices(Mage_Core_Model_Abstract $object)
67: {
68: $priceTable = $this->getTable('catalog/product_option_type_price');
69:
70: $price = (float)sprintf('%F', $object->getPrice());
71: $priceType = $object->getPriceType();
72:
73: if (!$object->getData('scope', 'price')) {
74:
75: $select = $this->_getReadAdapter()->select()
76: ->from($priceTable, 'option_type_id')
77: ->where('option_type_id = ?', (int)$object->getId())
78: ->where('store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
79: $optionTypeId = $this->_getReadAdapter()->fetchOne($select);
80:
81: if ($optionTypeId) {
82: if ($object->getStoreId() == '0') {
83: $bind = array(
84: 'price' => $price,
85: 'price_type' => $priceType
86: );
87: $where = array(
88: 'option_type_id = ?' => $optionTypeId,
89: 'store_id = ?' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID
90: );
91:
92: $this->_getWriteAdapter()->update($priceTable, $bind, $where);
93: }
94: } else {
95: $bind = array(
96: 'option_type_id' => (int)$object->getId(),
97: 'store_id' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID,
98: 'price' => $price,
99: 'price_type' => $priceType
100: );
101: $this->_getWriteAdapter()->insert($priceTable, $bind);
102: }
103: }
104:
105: $scope = (int)Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
106:
107: if ($object->getStoreId() != '0' && $scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE
108: && !$object->getData('scope', 'price')) {
109:
110: $baseCurrency = Mage::app()->getBaseCurrencyCode();
111:
112: $storeIds = Mage::app()->getStore($object->getStoreId())
113: ->getWebsite()
114: ->getStoreIds();
115: if (is_array($storeIds)) {
116: foreach ($storeIds as $storeId) {
117: if ($priceType == 'fixed') {
118: $storeCurrency = Mage::app()->getStore($storeId)->getBaseCurrencyCode();
119: $rate = Mage::getModel('directory/currency')->load($baseCurrency)->getRate($storeCurrency);
120: if (!$rate) {
121: $rate = 1;
122: }
123: $newPrice = $price * $rate;
124: } else {
125: $newPrice = $price;
126: }
127:
128: $select = $this->_getReadAdapter()->select()
129: ->from($priceTable, 'option_type_id')
130: ->where('option_type_id = ?', (int)$object->getId())
131: ->where('store_id = ?', (int)$storeId);
132: $optionTypeId = $this->_getReadAdapter()->fetchOne($select);
133:
134: if ($optionTypeId) {
135: $bind = array(
136: 'price' => $newPrice,
137: 'price_type' => $priceType
138: );
139: $where = array(
140: 'option_type_id = ?' => (int)$optionTypeId,
141: 'store_id = ?' => (int)$storeId
142: );
143:
144: $this->_getWriteAdapter()->update($priceTable, $bind, $where);
145: } else {
146: $bind = array(
147: 'option_type_id' => (int)$object->getId(),
148: 'store_id' => (int)$storeId,
149: 'price' => $newPrice,
150: 'price_type' => $priceType
151: );
152:
153: $this->_getWriteAdapter()->insert($priceTable, $bind);
154: }
155: }
156: }
157: } else if ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE && $object->getData('scope', 'price')) {
158: $where = array(
159: 'option_type_id = ?' => (int)$object->getId(),
160: 'store_id = ?' => (int)$object->getStoreId(),
161: );
162: $this->_getWriteAdapter()->delete($priceTable, $where);
163: }
164:
165: }
166:
167: 168: 169: 170: 171:
172: protected function _saveValueTitles(Mage_Core_Model_Abstract $object)
173: {
174: $titleTable = $this->getTable('catalog/product_option_type_title');
175:
176: if (!$object->getData('scope', 'title')) {
177: $select = $this->_getReadAdapter()->select()
178: ->from($titleTable, array('option_type_id'))
179: ->where('option_type_id = ?', (int)$object->getId())
180: ->where('store_id = ?', 0);
181: $optionTypeId = $this->_getReadAdapter()->fetchOne($select);
182:
183: if ($optionTypeId) {
184: if ($object->getStoreId() == '0') {
185: $where = array(
186: 'option_type_id = ?' => (int)$optionTypeId,
187: 'store_id = ?' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID
188: );
189: $bind = array(
190: 'title' => $object->getTitle()
191: );
192: $this->_getWriteAdapter()->update($titleTable, $bind, $where);
193: }
194: } else {
195: $bind = array(
196: 'option_type_id' => (int)$object->getId(),
197: 'store_id' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID,
198: 'title' => $object->getTitle()
199: );
200: $this->_getWriteAdapter()->insert($titleTable, $bind);
201: }
202: }
203:
204: if ($object->getStoreId() != '0' && !$object->getData('scope', 'title')) {
205: $select = $this->_getReadAdapter()->select()
206: ->from($titleTable, array('option_type_id'))
207: ->where('option_type_id = ?', (int)$object->getId())
208: ->where('store_id = ?', (int)$object->getStoreId());
209: $optionTypeId = $this->_getReadAdapter()->fetchOne($select);
210:
211: if ($optionTypeId) {
212: $bind = array(
213: 'title' => $object->getTitle()
214: );
215: $where = array(
216: 'option_type_id = ?' => (int)$optionTypeId,
217: 'store_id = ?' => (int)$object->getStoreId()
218: );
219: $this->_getWriteAdapter()->update($titleTable, $bind, $where);
220: } else {
221: $bind = array(
222: 'option_type_id' => (int)$object->getId(),
223: 'store_id' => (int)$object->getStoreId(),
224: 'title' => $object->getTitle()
225: );
226: $this->_getWriteAdapter()->insert($titleTable, $bind);
227: }
228: } else if ($object->getData('scope', 'title')) {
229: $where = array(
230: 'option_type_id = ?' => (int)$optionTypeId,
231: 'store_id = ?' => (int)$object->getStoreId()
232: );
233: $this->_getWriteAdapter()->delete($titleTable, $where);
234: }
235: }
236:
237: 238: 239: 240: 241: 242:
243: public function deleteValue($optionId)
244: {
245: $statement = $this->_getReadAdapter()->select()
246: ->from($this->getTable('catalog/product_option_type_value'))
247: ->where('option_id = ?', $optionId);
248:
249: $rowSet = $this->_getReadAdapter()->fetchAll($statement);
250:
251: foreach ($rowSet as $optionType) {
252: $this->deleteValues($optionType['option_type_id']);
253: }
254:
255: $this->_getWriteAdapter()->delete(
256: $this->getMainTable(),
257: array(
258: 'option_id = ?' => $optionId,
259: )
260: );
261:
262: return $this;
263: }
264:
265: 266: 267: 268: 269:
270: public function deleteValues($optionTypeId)
271: {
272: $condition = array(
273: 'option_type_id = ?' => $optionTypeId
274: );
275:
276: $this->_getWriteAdapter()->delete(
277: $this->getTable('catalog/product_option_type_price'),
278: $condition
279: );
280:
281: $this->_getWriteAdapter()->delete(
282: $this->getTable('catalog/product_option_type_title'),
283: $condition
284: );
285: }
286:
287: 288: 289: 290: 291: 292: 293: 294:
295: public function duplicate(Mage_Catalog_Model_Product_Option_Value $object, $oldOptionId, $newOptionId)
296: {
297: $writeAdapter = $this->_getWriteAdapter();
298: $readAdapter = $this->_getReadAdapter();
299: $select = $readAdapter->select()
300: ->from($this->getMainTable())
301: ->where('option_id = ?', $oldOptionId);
302: $valueData = $readAdapter->fetchAll($select);
303:
304: $valueCond = array();
305:
306: foreach ($valueData as $data) {
307: $optionTypeId = $data[$this->getIdFieldName()];
308: unset($data[$this->getIdFieldName()]);
309: $data['option_id'] = $newOptionId;
310:
311: $writeAdapter->insert($this->getMainTable(), $data);
312: $valueCond[$optionTypeId] = $writeAdapter->lastInsertId($this->getMainTable());
313: }
314:
315: unset($valueData);
316:
317: foreach ($valueCond as $oldTypeId => $newTypeId) {
318:
319: $priceTable = $this->getTable('catalog/product_option_type_price');
320: $columns= array(
321: new Zend_Db_Expr($newTypeId),
322: 'store_id', 'price', 'price_type'
323: );
324:
325: $select = $readAdapter->select()
326: ->from($priceTable, array())
327: ->where('option_type_id = ?', $oldTypeId)
328: ->columns($columns);
329: $insertSelect = $writeAdapter->insertFromSelect($select, $priceTable,
330: array('option_type_id', 'store_id', 'price', 'price_type'));
331: $writeAdapter->query($insertSelect);
332:
333:
334: $titleTable = $this->getTable('catalog/product_option_type_title');
335: $columns= array(
336: new Zend_Db_Expr($newTypeId),
337: 'store_id', 'title'
338: );
339:
340: $select = $this->_getReadAdapter()->select()
341: ->from($titleTable, array())
342: ->where('option_type_id = ?', $oldTypeId)
343: ->columns($columns);
344: $insertSelect = $writeAdapter->insertFromSelect($select, $titleTable,
345: array('option_type_id', 'store_id', 'title'));
346: $writeAdapter->query($insertSelect);
347: }
348:
349: return $object;
350: }
351: }
352: