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_Wishlist
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: * Wishlist item option collection
30: *
31: * @category Mage
32: * @package Mage_Wishlist
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Wishlist_Model_Resource_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: * @return void
55: */
56: protected function _construct()
57: {
58: $this->_init('wishlist/item_option');
59: }
60:
61: /**
62: * Fill array of options by item and product
63: *
64: * @return Mage_Wishlist_Model_Resource_Item_Option_Collection
65: */
66: protected function _afterLoad()
67: {
68: parent::_afterLoad();
69:
70: foreach ($this as $option) {
71: $optionId = $option->getId();
72: $itemId = $option->getWishlistItemId();
73: $productId = $option->getProductId();
74: if (isset($this->_optionsByItem[$itemId])) {
75: $this->_optionsByItem[$itemId][] = $optionId;
76: } else {
77: $this->_optionsByItem[$itemId] = array($optionId);
78: }
79: if (isset($this->_optionsByProduct[$productId])) {
80: $this->_optionsByProduct[$productId][] = $optionId;
81: } else {
82: $this->_optionsByProduct[$productId] = array($optionId);
83: }
84: }
85:
86: return $this;
87: }
88:
89: /**
90: * Apply quote item(s) filter to collection
91: *
92: * @param int|array $item
93: * @return Mage_Wishlist_Model_Resource_Item_Option_Collection
94: */
95: public function addItemFilter($item)
96: {
97: if (empty($item)) {
98: $this->_totalRecords = 0;
99: $this->_setIsLoaded(true);
100: } else if (is_array($item)) {
101: $this->addFieldToFilter('wishlist_item_id', array('in' => $item));
102: } else if ($item instanceof Mage_Wishlist_Model_Item) {
103: $this->addFieldToFilter('wishlist_item_id', $item->getId());
104: } else {
105: $this->addFieldToFilter('wishlist_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_Wishlist_Model_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 mixed $item
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: