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_Sales
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: * Item option collection
30: *
31: * @category Mage
32: * @package Mage_Sales
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Sales_Model_Resource_Quote_Item_Option_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * Array of option ids grouped by item id
39: *
40: * @var array
41: */
42: protected $_optionsByItem = array();
43:
44: /**
45: * Array of option ids grouped by product id
46: *
47: * @var array
48: */
49: protected $_optionsByProduct = array();
50:
51: /**
52: * Define resource model for collection
53: *
54: */
55: protected function _construct()
56: {
57: $this->_init('sales/quote_item_option');
58: }
59:
60: /**
61: * Fill array of options by item and product
62: *
63: * @return Mage_Sales_Model_Resource_Quote_Item_Option_Collection
64: */
65: protected function _afterLoad()
66: {
67: parent::_afterLoad();
68:
69: foreach ($this as $option) {
70: $optionId = $option->getId();
71: $itemId = $option->getItemId();
72: $productId = $option->getProductId();
73: if (isset($this->_optionsByItem[$itemId])) {
74: $this->_optionsByItem[$itemId][] = $optionId;
75: } else {
76: $this->_optionsByItem[$itemId] = array($optionId);
77: }
78: if (isset($this->_optionsByProduct[$productId])) {
79: $this->_optionsByProduct[$productId][] = $optionId;
80: } else {
81: $this->_optionsByProduct[$productId] = array($optionId);
82: }
83: }
84:
85: return $this;
86: }
87:
88: /**
89: * Apply quote item(s) filter to collection
90: *
91: * @param int | array $item
92: * @return Mage_Sales_Model_Resource_Quote_Item_Option_Collection
93: */
94: public function addItemFilter($item)
95: {
96: if (empty($item)) {
97: $this->_totalRecords = 0;
98: $this->_setIsLoaded(true);
99: //$this->addFieldToFilter('item_id', '');
100: } elseif (is_array($item)) {
101: $this->addFieldToFilter('item_id', array('in' => $item));
102: } elseif ($item instanceof Mage_Sales_Model_Quote_Item) {
103: $this->addFieldToFilter('item_id', $item->getId());
104: } else {
105: $this->addFieldToFilter('item_id', $item);
106: }
107:
108: return $this;
109: }
110:
111: /**
112: * Get array of all product ids
113: *
114: * @return array
115: */
116: public function getProductIds()
117: {
118: $this->load();
119:
120: return array_keys($this->_optionsByProduct);
121: }
122:
123: /**
124: * Get all option for item
125: *
126: * @param mixed $item
127: * @return array
128: */
129: public function getOptionsByItem($item)
130: {
131: if ($item instanceof Mage_Sales_Model_Quote_Item) {
132: $itemId = $item->getId();
133: } else {
134: $itemId = $item;
135: }
136:
137: $this->load();
138:
139: $options = array();
140: if (isset($this->_optionsByItem[$itemId])) {
141: foreach ($this->_optionsByItem[$itemId] as $optionId) {
142: $options[] = $this->_items[$optionId];
143: }
144: }
145:
146: return $options;
147: }
148:
149: /**
150: * Get all option for item
151: *
152: * @param int | Mage_Catalog_Model_Product $product
153: * @return array
154: */
155: public function getOptionsByProduct($product)
156: {
157: if ($product instanceof Mage_Catalog_Model_Product) {
158: $productId = $product->getId();
159: } else {
160: $productId = $product;
161: }
162:
163: $this->load();
164:
165: $options = array();
166: if (isset($this->_optionsByProduct[$productId])) {
167: foreach ($this->_optionsByProduct[$productId] as $optionId) {
168: $options[] = $this->_items[$optionId];
169: }
170: }
171:
172: return $options;
173: }
174: }
175: