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: class Mage_Catalog_Model_Resource_Product_Option_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39:
40: protected function _construct()
41: {
42: $this->_init('catalog/product_option');
43: }
44:
45: 46: 47: 48: 49: 50:
51: public function getOptions($storeId)
52: {
53: $this->addPriceToResult($storeId)
54: ->addTitleToResult($storeId);
55:
56: return $this;
57: }
58:
59: 60: 61: 62: 63: 64:
65: public function addTitleToResult($storeId)
66: {
67: $productOptionTitleTable = $this->getTable('catalog/product_option_title');
68: $adapter = $this->getConnection();
69: $titleExpr = $adapter->getCheckSql(
70: 'store_option_title.title IS NULL',
71: 'default_option_title.title',
72: 'store_option_title.title'
73: );
74:
75: $this->getSelect()
76: ->join(array('default_option_title' => $productOptionTitleTable),
77: 'default_option_title.option_id = main_table.option_id',
78: array('default_title' => 'title'))
79: ->joinLeft(
80: array('store_option_title' => $productOptionTitleTable),
81: 'store_option_title.option_id = main_table.option_id AND '
82: . $adapter->quoteInto('store_option_title.store_id = ?', $storeId),
83: array(
84: 'store_title' => 'title',
85: 'title' => $titleExpr
86: ))
87: ->where('default_option_title.store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
88:
89: return $this;
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function addPriceToResult($storeId)
99: {
100: $productOptionPriceTable = $this->getTable('catalog/product_option_price');
101: $adapter = $this->getConnection();
102: $priceExpr = $adapter->getCheckSql(
103: 'store_option_price.price IS NULL',
104: 'default_option_price.price',
105: 'store_option_price.price'
106: );
107: $priceTypeExpr = $adapter->getCheckSql(
108: 'store_option_price.price_type IS NULL',
109: 'default_option_price.price_type',
110: 'store_option_price.price_type'
111: );
112:
113: $this->getSelect()
114: ->joinLeft(
115: array('default_option_price' => $productOptionPriceTable),
116: 'default_option_price.option_id = main_table.option_id AND '
117: . $adapter->quoteInto(
118: 'default_option_price.store_id = ?',
119: Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID
120: ),
121: array(
122: 'default_price' => 'price',
123: 'default_price_type' => 'price_type'
124: ))
125: ->joinLeft(
126: array('store_option_price' => $productOptionPriceTable),
127: 'store_option_price.option_id = main_table.option_id AND '
128: . $adapter->quoteInto('store_option_price.store_id = ?', $storeId),
129: array(
130: 'store_price' => 'price',
131: 'store_price_type' => 'price_type',
132: 'price' => $priceExpr,
133: 'price_type' => $priceTypeExpr
134: ));
135:
136: return $this;
137: }
138:
139: 140: 141: 142: 143: 144:
145: public function addValuesToResult($storeId = null)
146: {
147: if ($storeId === null) {
148: $storeId = Mage::app()->getStore()->getId();
149: }
150: $optionIds = array();
151: foreach ($this as $option) {
152: $optionIds[] = $option->getId();
153: }
154: if (!empty($optionIds)) {
155:
156: $values = Mage::getModel('catalog/product_option_value')
157: ->getCollection()
158: ->addTitleToResult($storeId)
159: ->addPriceToResult($storeId)
160: ->addOptionToFilter($optionIds)
161: ->setOrder('sort_order', self::SORT_ORDER_ASC)
162: ->setOrder('title', self::SORT_ORDER_ASC);
163:
164: foreach ($values as $value) {
165: $optionId = $value->getOptionId();
166: if($this->getItemById($optionId)) {
167: $this->getItemById($optionId)->addValue($value);
168: $value->setOption($this->getItemById($optionId));
169: }
170: }
171: }
172:
173: return $this;
174: }
175:
176: 177: 178: 179: 180: 181:
182: public function addProductToFilter($product)
183: {
184: if (empty($product)) {
185: $this->addFieldToFilter('product_id', '');
186: } elseif (is_array($product)) {
187: $this->addFieldToFilter('product_id', array('in' => $product));
188: } elseif ($product instanceof Mage_Catalog_Model_Product) {
189: $this->addFieldToFilter('product_id', $product->getId());
190: } else {
191: $this->addFieldToFilter('product_id', $product);
192: }
193:
194: return $this;
195: }
196:
197: 198: 199: 200: 201: 202:
203: public function addRequiredFilter($required = true)
204: {
205: $this->addFieldToFilter('main_table.is_require', (string)$required);
206: return $this;
207: }
208:
209: 210: 211: 212: 213: 214:
215: public function addIdsToFilter($optionIds)
216: {
217: $this->addFieldToFilter('main_table.option_id', $optionIds);
218: return $this;
219: }
220:
221: 222: 223: 224: 225:
226: public function reset()
227: {
228: return $this->_reset();
229: }
230: }
231: