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 extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('catalog/product_option', 'option_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: protected function _afterSave(Mage_Core_Model_Abstract $object)
53: {
54: $this->_saveValuePrices($object);
55: $this->_saveValueTitles($object);
56:
57: return parent::_afterSave($object);
58: }
59:
60: 61: 62: 63: 64: 65:
66: protected function _saveValuePrices(Mage_Core_Model_Abstract $object)
67: {
68: $priceTable = $this->getTable('catalog/product_option_price');
69: $readAdapter = $this->_getReadAdapter();
70: $writeAdapter = $this->_getWriteAdapter();
71:
72: 73: 74: 75:
76:
77: if ($object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_FIELD
78: || $object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_AREA
79: || $object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_FILE
80: || $object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE
81: || $object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DATE_TIME
82: || $object->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_TIME
83: ) {
84:
85: if (!$object->getData('scope', 'price')) {
86: $statement = $readAdapter->select()
87: ->from($priceTable, 'option_id')
88: ->where('option_id = ?', $object->getId())
89: ->where('store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
90: $optionId = $readAdapter->fetchOne($statement);
91:
92: if ($optionId) {
93: if ($object->getStoreId() == '0') {
94: $data = $this->_prepareDataForTable(
95: new Varien_Object(
96: array(
97: 'price' => $object->getPrice(),
98: 'price_type' => $object->getPriceType())
99: ),
100: $priceTable
101: );
102:
103: $writeAdapter->update(
104: $priceTable,
105: $data,
106: array(
107: 'option_id = ?' => $object->getId(),
108: 'store_id = ?' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID,
109: )
110: );
111: }
112: } else {
113: $data = $this->_prepareDataForTable(
114: new Varien_Object(
115: array(
116: 'option_id' => $object->getId(),
117: 'store_id' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID,
118: 'price' => $object->getPrice(),
119: 'price_type' => $object->getPriceType()
120: )
121: ),
122: $priceTable
123: );
124: $writeAdapter->insert($priceTable, $data);
125: }
126: }
127:
128: $scope = (int) Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
129:
130: if ($object->getStoreId() != '0' && $scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE
131: && !$object->getData('scope', 'price')) {
132:
133: $baseCurrency = Mage::app()->getBaseCurrencyCode();
134:
135: $storeIds = Mage::app()->getStore($object->getStoreId())->getWebsite()->getStoreIds();
136: if (is_array($storeIds)) {
137: foreach ($storeIds as $storeId) {
138: if ($object->getPriceType() == 'fixed') {
139: $storeCurrency = Mage::app()->getStore($storeId)->getBaseCurrencyCode();
140: $rate = Mage::getModel('directory/currency')->load($baseCurrency)->getRate($storeCurrency);
141: if (!$rate) {
142: $rate=1;
143: }
144: $newPrice = $object->getPrice() * $rate;
145: } else {
146: $newPrice = $object->getPrice();
147: }
148:
149: $statement = $readAdapter->select()
150: ->from($priceTable)
151: ->where('option_id = ?', $object->getId())
152: ->where('store_id = ?', $storeId);
153:
154: if ($readAdapter->fetchOne($statement)) {
155: $data = $this->_prepareDataForTable(
156: new Varien_Object(
157: array(
158: 'price' => $newPrice,
159: 'price_type' => $object->getPriceType()
160: )
161: ),
162: $priceTable
163: );
164:
165: $writeAdapter->update(
166: $priceTable,
167: $data,
168: array(
169: 'option_id = ?' => $object->getId(),
170: 'store_id = ?' => $storeId
171: )
172: );
173: } else {
174: $data = $this->_prepareDataForTable(
175: new Varien_Object(
176: array(
177: 'option_id' => $object->getId(),
178: 'store_id' => $storeId,
179: 'price' => $newPrice,
180: 'price_type' => $object->getPriceType()
181: )
182: ),
183: $priceTable
184: );
185: $writeAdapter->insert($priceTable, $data);
186: }
187: }
188: }
189: } elseif ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE && $object->getData('scope', 'price')) {
190: $writeAdapter->delete(
191: $priceTable,
192: array(
193: 'option_id = ?' => $object->getId(),
194: 'store_id = ?' => $object->getStoreId()
195: )
196: );
197: }
198: }
199:
200: return $this;
201: }
202:
203: 204: 205: 206: 207: 208:
209: protected function _saveValueTitles(Mage_Core_Model_Abstract $object)
210: {
211: $readAdapter = $this->_getReadAdapter();
212: $writeAdapter = $this->_getWriteAdapter();
213: $titleTable = $this->getTable('catalog/product_option_title');
214:
215:
216: if (!$object->getData('scope', 'title')) {
217: $statement = $readAdapter->select()
218: ->from($titleTable)
219: ->where('option_id = ?', $object->getId())
220: ->where('store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
221:
222: if ($readAdapter->fetchOne($statement)) {
223: if ($object->getStoreId() == '0') {
224: $data = $this->_prepareDataForTable(
225: new Varien_Object(
226: array(
227: 'title' => $object->getTitle()
228: )
229: ),
230: $titleTable
231: );
232:
233: $writeAdapter->update(
234: $titleTable,
235: $data,
236: array(
237: 'option_id = ?' => $object->getId(),
238: 'store_id = ?' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID
239: )
240: );
241: }
242: } else {
243: $data = $this->_prepareDataForTable(
244: new Varien_Object(
245: array(
246: 'option_id' => $object->getId(),
247: 'store_id' => Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID,
248: 'title' => $object->getTitle()
249: )
250: ),
251: $titleTable
252: );
253:
254: $writeAdapter->insert($titleTable, $data);
255: }
256: }
257:
258: if ($object->getStoreId() != '0' && !$object->getData('scope', 'title')) {
259: $statement = $readAdapter->select()
260: ->from($titleTable)
261: ->where('option_id = ?', $object->getId())
262: ->where('store_id = ?', $object->getStoreId());
263:
264: if ($readAdapter->fetchOne($statement)) {
265: $data = $this->_prepareDataForTable(
266: new Varien_Object(
267: array(
268: 'title' => $object->getTitle()
269: )
270: ),
271: $titleTable
272: );
273:
274: $writeAdapter->update(
275: $titleTable,
276: $data,
277: array(
278: 'option_id = ?' => $object->getId(),
279: 'store_id = ?' => $object->getStoreId()
280: )
281: );
282: } else {
283: $data = $this->_prepareDataForTable(
284: new Varien_Object(
285: array(
286: 'option_id' => $object->getId(),
287: 'store_id' => $object->getStoreId(),
288: 'title' => $object->getTitle()
289: )
290: ),
291: $titleTable
292: );
293: $writeAdapter->insert($titleTable, $data);
294: }
295: } elseif ($object->getData('scope', 'title')) {
296: $writeAdapter->delete(
297: $titleTable,
298: array(
299: 'option_id = ?' => $object->getId(),
300: 'store_id = ?' => $object->getStoreId()
301: )
302: );
303: }
304: }
305:
306: 307: 308: 309: 310: 311:
312: public function deletePrices($optionId)
313: {
314: $this->_getWriteAdapter()->delete(
315: $this->getTable('catalog/product_option_price'),
316: array(
317: 'option_id = ?' => $optionId
318: )
319: );
320:
321: return $this;
322: }
323:
324: 325: 326: 327: 328: 329:
330: public function deleteTitles($optionId)
331: {
332: $this->_getWriteAdapter()->delete(
333: $this->getTable('catalog/product_option_title'),
334: array(
335: 'option_id = ?' => $optionId
336: )
337: );
338:
339: return $this;
340: }
341:
342: 343: 344: 345: 346: 347: 348: 349:
350: public function duplicate(Mage_Catalog_Model_Product_Option $object, $oldProductId, $newProductId)
351: {
352: $write = $this->_getWriteAdapter();
353: $read = $this->_getReadAdapter();
354:
355: $optionsCond = array();
356: $optionsData = array();
357:
358:
359: $select = $read->select()
360: ->from($this->getTable('catalog/product_option'))
361: ->where('product_id = ?', $oldProductId);
362:
363: $query = $read->query($select);
364:
365: while ($row = $query->fetch()) {
366: $optionsData[$row['option_id']] = $row;
367: $optionsData[$row['option_id']]['product_id'] = $newProductId;
368: unset($optionsData[$row['option_id']]['option_id']);
369: }
370:
371:
372: foreach ($optionsData as $oId => $data) {
373: $write->insert($this->getMainTable(), $data);
374: $optionsCond[$oId] = $write->lastInsertId($this->getMainTable());
375: }
376:
377:
378: foreach ($optionsCond as $oldOptionId => $newOptionId) {
379:
380: $table = $this->getTable('catalog/product_option_title');
381:
382: $select = $this->_getReadAdapter()->select()
383: ->from($table, array(new Zend_Db_Expr($newOptionId), 'store_id', 'title'))
384: ->where('option_id = ?', $oldOptionId);
385:
386: $insertSelect = $write->insertFromSelect(
387: $select,
388: $table,
389: array('option_id', 'store_id', 'title'),
390: Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE
391: );
392: $write->query($insertSelect);
393:
394:
395: $table = $this->getTable('catalog/product_option_price');
396:
397: $select = $read->select()
398: ->from($table, array(new Zend_Db_Expr($newOptionId), 'store_id', 'price', 'price_type'))
399: ->where('option_id = ?', $oldOptionId);
400:
401: $insertSelect = $write->insertFromSelect(
402: $select, $table,
403: array(
404: 'option_id',
405: 'store_id',
406: 'price',
407: 'price_type'
408: ),
409: Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE
410: );
411: $write->query($insertSelect);
412:
413: $object->getValueInstance()->duplicate($oldOptionId, $newOptionId);
414: }
415:
416: return $object;
417: }
418:
419: 420: 421: 422: 423: 424: 425:
426: public function getSearchableData($productId, $storeId)
427: {
428: $searchData = array();
429:
430: $adapter = $this->_getReadAdapter();
431:
432: $titleCheckSql = $adapter->getCheckSql(
433: 'option_title_store.title IS NULL',
434: 'option_title_default.title',
435: 'option_title_store.title'
436: );
437:
438:
439:
440:
441: $defaultOptionJoin = implode(
442: ' AND ',
443: array('option_title_default.option_id=product_option.option_id',
444: $adapter->quoteInto('option_title_default.store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID))
445: );
446:
447: $storeOptionJoin = implode(
448: ' AND ',
449: array(
450: 'option_title_store.option_id=product_option.option_id',
451: $adapter->quoteInto('option_title_store.store_id = ?', (int) $storeId))
452: );
453:
454: $select = $adapter->select()
455: ->from(array('product_option' => $this->getMainTable()), null)
456: ->join(
457: array('option_title_default' => $this->getTable('catalog/product_option_title')),
458: $defaultOptionJoin,
459: array()
460: )
461: ->joinLeft(
462: array('option_title_store' => $this->getTable('catalog/product_option_title')),
463: $storeOptionJoin,
464: array('title' => $titleCheckSql)
465: )
466: ->where('product_option.product_id = ?', $productId);
467:
468: if ($titles = $adapter->fetchCol($select)) {
469: $searchData = array_merge($searchData, $titles);
470: }
471:
472:
473:
474: $defaultOptionJoin = implode(
475: ' AND ', array(
476: 'option_title_default.option_type_id=option_type.option_type_id',
477: $adapter->quoteInto('option_title_default.store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID))
478: );
479:
480: $storeOptionJoin = implode(
481: ' AND ', array(
482: 'option_title_store.option_type_id = option_type.option_type_id',
483: $adapter->quoteInto('option_title_store.store_id = ?', (int) $storeId))
484: );
485:
486: $select = $adapter->select()
487: ->from(array('product_option' => $this->getMainTable()), null)
488: ->join(
489: array('option_type' => $this->getTable('catalog/product_option_type_value')),
490: 'option_type.option_id=product_option.option_id',
491: array()
492: )
493: ->join(
494: array('option_title_default' => $this->getTable('catalog/product_option_type_title')),
495: $defaultOptionJoin,
496: array()
497: )
498: ->joinLeft(
499: array('option_title_store' => $this->getTable('catalog/product_option_type_title')),
500: $storeOptionJoin,
501: array('title' => $titleCheckSql)
502: )
503: ->where('product_option.product_id = ?', $productId);
504:
505: if ($titles = $adapter->fetchCol($select)) {
506: $searchData = array_merge($searchData, $titles);
507: }
508:
509: return $searchData;
510: }
511: }
512: