1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Bundle
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Bundle Option Model
29: *
30: * @method Mage_Bundle_Model_Resource_Option _getResource()
31: * @method Mage_Bundle_Model_Resource_Option getResource()
32: * @method int getParentId()
33: * @method Mage_Bundle_Model_Option setParentId(int $value)
34: * @method int getRequired()
35: * @method Mage_Bundle_Model_Option setRequired(int $value)
36: * @method int getPosition()
37: * @method Mage_Bundle_Model_Option setPosition(int $value)
38: * @method string getType()
39: * @method Mage_Bundle_Model_Option setType(string $value)
40: *
41: * @category Mage
42: * @package Mage_Bundle
43: * @author Magento Core Team <core@magentocommerce.com>
44: */
45: class Mage_Bundle_Model_Option extends Mage_Core_Model_Abstract
46: {
47: /**
48: * Default selection object
49: *
50: * @var Mage_Bundle_Model_Selection
51: */
52: protected $_defaultSelection = null;
53:
54: /**
55: * Initialize resource model
56: *
57: */
58: protected function _construct()
59: {
60: $this->_init('bundle/option');
61: parent::_construct();
62: }
63:
64: /**
65: * Add selection to option
66: *
67: * @param Mage_Bundle_Model_Selection $selection
68: * @return Mage_Bundle_Model_Option
69: */
70: public function addSelection($selection)
71: {
72: if (!$selection) {
73: return false;
74: }
75: if (!$selections = $this->getData('selections')) {
76: $selections = array();
77: }
78: array_push($selections, $selection);
79: $this->setSelections($selections);
80: return $this;
81: }
82:
83: /**
84: * Check Is Saleable Option
85: *
86: * @return bool
87: */
88: public function isSaleable()
89: {
90: $saleable = 0;
91: if ($this->getSelections()) {
92: foreach ($this->getSelections() as $selection) {
93: if ($selection->isSaleable()) {
94: $saleable++;
95: }
96: }
97: return (bool)$saleable;
98: }
99: else {
100: return false;
101: }
102: }
103:
104: /**
105: * Retrieve default Selection object
106: *
107: * @return Mage_Bundle_Model_Selection
108: */
109: public function getDefaultSelection()
110: {
111: if (!$this->_defaultSelection && $this->getSelections()) {
112: foreach ($this->getSelections() as $selection) {
113: if ($selection->getIsDefault()) {
114: $this->_defaultSelection = $selection;
115: break;
116: }
117: }
118: }
119: return $this->_defaultSelection;
120: /**
121: * if (!$this->_defaultSelection && $this->getSelections()) {
122: $_selections = array();
123: foreach ($this->getSelections() as $selection) {
124: if ($selection->getIsDefault()) {
125: $_selections[] = $selection;
126: }
127: }
128: if (!empty($_selections)) {
129: $this->_defaultSelection = $_selections;
130: } else {
131: return null;
132: }
133: }
134: return $this->_defaultSelection;
135: */
136: }
137:
138: /**
139: * Check is multi Option selection
140: *
141: * @return bool
142: */
143: public function isMultiSelection()
144: {
145: if ($this->getType() == 'checkbox' || $this->getType() == 'multi') {
146: return true;
147: } else {
148: return false;
149: }
150: }
151:
152: /**
153: * Retrieve options searchable data
154: *
155: * @param int $productId
156: * @param int $storeId
157: * @return array
158: */
159: public function getSearchableData($productId, $storeId)
160: {
161: return $this->_getResource()
162: ->getSearchableData($productId, $storeId);
163: }
164:
165: /**
166: * Return selection by it's id
167: *
168: * @param int $selectionId
169: * @return Mage_Bundle_Model_Selection
170: */
171: public function getSelectionById($selectionId)
172: {
173: $selections = $this->getSelections();
174: $i = count($selections);
175:
176: while ($i-- && $selections[$i]->getSelectionId() != $selectionId);
177:
178: return $i == -1 ? false : $selections[$i];
179: }
180: }
181: