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: /**
29: * Bundle Options Resource Collection
30: *
31: * @category Mage
32: * @package Mage_Bundle
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Bundle_Model_Resource_Option_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * All item ids cache
39: *
40: * @var array
41: */
42: protected $_itemIds;
43:
44: /**
45: * True when selections a
46: *
47: * @var bool
48: */
49: protected $_selectionsAppended = false;
50:
51: /**
52: * Init model and resource model
53: *
54: */
55: protected function _construct()
56: {
57: $this->_init('bundle/option');
58: }
59:
60: /**
61: * Joins values to options
62: *
63: * @param int $storeId
64: * @return Mage_Bundle_Model_Resource_Option_Collection
65: */
66: public function joinValues($storeId)
67: {
68: $this->getSelect()
69: ->joinLeft(
70: array('option_value_default' => $this->getTable('bundle/option_value')),
71: 'main_table.option_id = option_value_default.option_id and option_value_default.store_id = 0',
72: array()
73: )
74: ->columns(array('default_title' => 'option_value_default.title'));
75:
76: $title = $this->getConnection()->getCheckSql(
77: 'option_value.title IS NOT NULL',
78: 'option_value.title',
79: 'option_value_default.title'
80: );
81: if ($storeId !== null) {
82: $this->getSelect()
83: ->columns(array('title' => $title))
84: ->joinLeft(array('option_value' => $this->getTable('bundle/option_value')),
85: $this->getConnection()->quoteInto(
86: 'main_table.option_id = option_value.option_id and option_value.store_id = ?',
87: $storeId
88: ),
89: array()
90: );
91: }
92: return $this;
93: }
94:
95: /**
96: * Sets product id filter
97: *
98: * @param int $productId
99: * @return Mage_Bundle_Model_Resource_Option_Collection
100: */
101: public function setProductIdFilter($productId)
102: {
103: $this->addFieldToFilter('main_table.parent_id', $productId);
104: return $this;
105: }
106:
107: /**
108: * Sets order by position
109: *
110: * @return Mage_Bundle_Model_Resource_Option_Collection
111: */
112: public function setPositionOrder()
113: {
114: $this->getSelect()->order('main_table.position asc')
115: ->order('main_table.option_id asc');
116: return $this;
117: }
118:
119: /**
120: * Append selection to options
121: * stripBefore - indicates to reload
122: * appendAll - indicates do we need to filter by saleable and required custom options
123: *
124: * @param Mage_Bundle_Model_Resource_Selection_Collection $selectionsCollection
125: * @param bool $stripBefore
126: * @param bool $appendAll
127: * @return array
128: */
129: public function appendSelections($selectionsCollection, $stripBefore = false, $appendAll = true)
130: {
131: if ($stripBefore) {
132: $this->_stripSelections();
133: }
134:
135: if (!$this->_selectionsAppended) {
136: foreach ($selectionsCollection->getItems() as $key => $_selection) {
137: if ($_option = $this->getItemById($_selection->getOptionId())) {
138: if ($appendAll || ($_selection->isSalable() && !$_selection->getRequiredOptions())) {
139: $_selection->setOption($_option);
140: $_option->addSelection($_selection);
141: } else {
142: $selectionsCollection->removeItemByKey($key);
143: }
144: }
145: }
146: $this->_selectionsAppended = true;
147: }
148:
149: return $this->getItems();
150: }
151:
152: /**
153: * Removes appended selections before
154: *
155: * @return Mage_Bundle_Model_Resource_Option_Collection
156: */
157: protected function _stripSelections()
158: {
159: foreach ($this->getItems() as $option) {
160: $option->setSelections(array());
161: }
162: $this->_selectionsAppended = false;
163: return $this;
164: }
165:
166: /**
167: * Sets filter by option id
168: *
169: * @param array|int $ids
170: * @return Mage_Bundle_Model_Resource_Option_Collection
171: */
172: public function setIdFilter($ids)
173: {
174: if (is_array($ids)) {
175: $this->addFieldToFilter('main_table.option_id', array('in' => $ids));
176: } else if ($ids != '') {
177: $this->addFieldToFilter('main_table.option_id', $ids);
178: }
179: return $this;
180: }
181:
182: /**
183: * Reset all item ids cache
184: *
185: * @return Mage_Bundle_Model_Resource_Option_Collection
186: */
187: public function resetAllIds()
188: {
189: $this->_itemIds = null;
190: return $this;
191: }
192:
193: /**
194: * Retrive all ids for collection
195: *
196: * @return array
197: */
198: public function getAllIds()
199: {
200: if (is_null($this->_itemIds)) {
201: $this->_itemIds = parent::getAllIds();
202: }
203: return $this->_itemIds;
204: }
205: }
206:
207: