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: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54:
55: class Mage_Catalog_Model_Product_Option extends Mage_Core_Model_Abstract
56: {
57: const OPTION_GROUP_TEXT = 'text';
58: const OPTION_GROUP_FILE = 'file';
59: const OPTION_GROUP_SELECT = 'select';
60: const OPTION_GROUP_DATE = 'date';
61:
62: const OPTION_TYPE_FIELD = 'field';
63: const OPTION_TYPE_AREA = 'area';
64: const OPTION_TYPE_FILE = 'file';
65: const OPTION_TYPE_DROP_DOWN = 'drop_down';
66: const OPTION_TYPE_RADIO = 'radio';
67: const OPTION_TYPE_CHECKBOX = 'checkbox';
68: const OPTION_TYPE_MULTIPLE = 'multiple';
69: const OPTION_TYPE_DATE = 'date';
70: const OPTION_TYPE_DATE_TIME = 'date_time';
71: const OPTION_TYPE_TIME = 'time';
72:
73: protected $_product;
74:
75: protected $_options = array();
76:
77: protected $_valueInstance;
78:
79: protected $_values = array();
80:
81: protected function _construct()
82: {
83: $this->_init('catalog/product_option');
84: }
85:
86: 87: 88: 89: 90: 91:
92: public function addValue(Mage_Catalog_Model_Product_Option_Value $value)
93: {
94: $this->_values[$value->getId()] = $value;
95: return $this;
96: }
97:
98: 99: 100: 101: 102: 103:
104: public function getValueById($valueId)
105: {
106: if (isset($this->_values[$valueId])) {
107: return $this->_values[$valueId];
108: }
109:
110: return null;
111: }
112:
113: public function getValues()
114: {
115: return $this->_values;
116: }
117:
118: 119: 120: 121: 122:
123: public function getValueInstance()
124: {
125: if (!$this->_valueInstance) {
126: $this->_valueInstance = Mage::getSingleton('catalog/product_option_value');
127: }
128: return $this->_valueInstance;
129: }
130:
131: 132: 133: 134: 135: 136:
137: public function addOption($option)
138: {
139: $this->_options[] = $option;
140: return $this;
141: }
142:
143: 144: 145: 146: 147:
148: public function getOptions()
149: {
150: return $this->_options;
151: }
152:
153: 154: 155: 156: 157: 158:
159: public function setOptions($options)
160: {
161: $this->_options = $options;
162: return $this;
163: }
164:
165: 166: 167: 168: 169:
170: public function unsetOptions()
171: {
172: $this->_options = array();
173: return $this;
174: }
175:
176: 177: 178: 179: 180:
181: public function getProduct()
182: {
183: return $this->_product;
184: }
185:
186: 187: 188: 189: 190: 191:
192: public function setProduct(Mage_Catalog_Model_Product $product = null)
193: {
194: $this->_product = $product;
195: return $this;
196: }
197:
198: 199: 200: 201: 202: 203:
204: public function getGroupByType($type = null)
205: {
206: if (is_null($type)) {
207: $type = $this->getType();
208: }
209: $optionGroupsToTypes = array(
210: self::OPTION_TYPE_FIELD => self::OPTION_GROUP_TEXT,
211: self::OPTION_TYPE_AREA => self::OPTION_GROUP_TEXT,
212: self::OPTION_TYPE_FILE => self::OPTION_GROUP_FILE,
213: self::OPTION_TYPE_DROP_DOWN => self::OPTION_GROUP_SELECT,
214: self::OPTION_TYPE_RADIO => self::OPTION_GROUP_SELECT,
215: self::OPTION_TYPE_CHECKBOX => self::OPTION_GROUP_SELECT,
216: self::OPTION_TYPE_MULTIPLE => self::OPTION_GROUP_SELECT,
217: self::OPTION_TYPE_DATE => self::OPTION_GROUP_DATE,
218: self::OPTION_TYPE_DATE_TIME => self::OPTION_GROUP_DATE,
219: self::OPTION_TYPE_TIME => self::OPTION_GROUP_DATE,
220: );
221:
222: return isset($optionGroupsToTypes[$type])?$optionGroupsToTypes[$type]:'';
223: }
224:
225: 226: 227: 228: 229: 230:
231: public function groupFactory($type)
232: {
233: $group = $this->getGroupByType($type);
234: if (!empty($group)) {
235: return Mage::getModel('catalog/product_option_type_' . $group);
236: }
237: Mage::throwException(Mage::helper('catalog')->__('Wrong option type to get group instance.'));
238: }
239:
240: 241: 242: 243: 244:
245: public function saveOptions()
246: {
247: foreach ($this->getOptions() as $option) {
248: $this->setData($option)
249: ->setData('product_id', $this->getProduct()->getId())
250: ->setData('store_id', $this->getProduct()->getStoreId());
251:
252: if ($this->getData('option_id') == '0') {
253: $this->unsetData('option_id');
254: } else {
255: $this->setId($this->getData('option_id'));
256: }
257: $isEdit = (bool)$this->getId()? true:false;
258:
259: if ($this->getData('is_delete') == '1') {
260: if ($isEdit) {
261: $this->getValueInstance()->deleteValue($this->getId());
262: $this->deletePrices($this->getId());
263: $this->deleteTitles($this->getId());
264: $this->delete();
265: }
266: } else {
267: if ($this->getData('previous_type') != '') {
268: $previousType = $this->getData('previous_type');
269:
270: 271: 272: 273:
274: if ($this->getGroupByType($previousType) != $this->getGroupByType($this->getData('type'))) {
275:
276: switch ($this->getGroupByType($previousType)) {
277: case self::OPTION_GROUP_SELECT:
278: $this->unsetData('values');
279: if ($isEdit) {
280: $this->getValueInstance()->deleteValue($this->getId());
281: }
282: break;
283: case self::OPTION_GROUP_FILE:
284: $this->setData('file_extension', '');
285: $this->setData('image_size_x', '0');
286: $this->setData('image_size_y', '0');
287: break;
288: case self::OPTION_GROUP_TEXT:
289: $this->setData('max_characters', '0');
290: break;
291: case self::OPTION_GROUP_DATE:
292: break;
293: }
294: if ($this->getGroupByType($this->getData('type')) == self::OPTION_GROUP_SELECT) {
295: $this->setData('sku', '');
296: $this->unsetData('price');
297: $this->unsetData('price_type');
298: if ($isEdit) {
299: $this->deletePrices($this->getId());
300: }
301: }
302: }
303: }
304: $this->save(); }
305: }
306: return $this;
307: }
308:
309: protected function _afterSave()
310: {
311: $this->getValueInstance()->unsetValues();
312: if (is_array($this->getData('values'))) {
313: foreach ($this->getData('values') as $value) {
314: $this->getValueInstance()->addValue($value);
315: }
316:
317: $this->getValueInstance()->setOption($this)
318: ->saveValues();
319: } elseif ($this->getGroupByType($this->getType()) == self::OPTION_GROUP_SELECT) {
320: Mage::throwException(Mage::helper('catalog')->__('Select type options required values rows.'));
321: }
322:
323: return parent::_afterSave();
324: }
325:
326: 327: 328: 329: 330: 331: 332:
333: public function getPrice($flag=false)
334: {
335: if ($flag && $this->getPriceType() == 'percent') {
336: $basePrice = $this->getProduct()->getFinalPrice();
337: $price = $basePrice*($this->_getData('price')/100);
338: return $price;
339: }
340: return $this->_getData('price');
341: }
342:
343: 344: 345: 346: 347: 348:
349: public function deletePrices($option_id)
350: {
351: $this->getResource()->deletePrices($option_id);
352: return $this;
353: }
354:
355: 356: 357: 358: 359: 360:
361: public function deleteTitles($option_id)
362: {
363: $this->getResource()->deleteTitles($option_id);
364: return $this;
365: }
366:
367: 368: 369: 370: 371: 372:
373: public function getProductOptionCollection(Mage_Catalog_Model_Product $product)
374: {
375: $collection = $this->getCollection()
376: ->addFieldToFilter('product_id', $product->getId())
377: ->addTitleToResult($product->getStoreId())
378: ->addPriceToResult($product->getStoreId())
379: ->setOrder('sort_order', 'asc')
380: ->setOrder('title', 'asc');
381:
382: if ($this->getAddRequiredFilter()) {
383: $collection->addRequiredFilter($this->getAddRequiredFilterValue());
384: }
385:
386: $collection->addValuesToResult($product->getStoreId());
387: return $collection;
388: }
389:
390: 391: 392: 393: 394:
395: public function getValuesCollection()
396: {
397: $collection = $this->getValueInstance()
398: ->getValuesCollection($this);
399:
400: return $collection;
401: }
402:
403: 404: 405: 406: 407: 408: 409:
410: public function getOptionValuesByOptionId($optionIds, $store_id)
411: {
412: $collection = Mage::getModel('catalog/product_option_value')
413: ->getValuesByOption($optionIds, $this->getId(), $store_id);
414:
415: return $collection;
416: }
417:
418: 419: 420: 421: 422:
423: public function prepareOptionForDuplicate()
424: {
425: $this->setProductId(null);
426: $this->setOptionId(null);
427: $newOption = $this->__toArray();
428: if ($_values = $this->getValues()) {
429: $newValuesArray = array();
430: foreach ($_values as $_value) {
431: $newValuesArray[] = $_value->prepareValueForDuplicate();
432: }
433: $newOption['values'] = $newValuesArray;
434: }
435:
436: return $newOption;
437: }
438:
439: 440: 441: 442: 443: 444: 445:
446: public function duplicate($oldProductId, $newProductId)
447: {
448: $this->getResource()->duplicate($this, $oldProductId, $newProductId);
449:
450: return $this;
451: }
452:
453: 454: 455: 456: 457: 458: 459:
460: public function getSearchableData($productId, $storeId)
461: {
462: return $this->_getResource()->getSearchableData($productId, $storeId);
463: }
464:
465: 466: 467: 468: 469:
470: protected function _clearData()
471: {
472: $this->_data = array();
473: $this->_values = array();
474: return $this;
475: }
476:
477: 478: 479: 480: 481:
482: protected function _clearReferences()
483: {
484: if (!empty($this->_values)) {
485: foreach ($this->_values as $value) {
486: $value->unsetOption();
487: }
488: }
489: return $this;
490: }
491: }
492: