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_Value_Collection
36: extends Mage_Core_Model_Resource_Db_Collection_Abstract
37: {
38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('catalog/product_option_value');
44: }
45:
46: 47: 48: 49: 50: 51:
52: public function getValues($storeId)
53: {
54: $this->addPriceToResult($storeId)
55: ->addTitleToResult($storeId);
56:
57: return $this;
58: }
59:
60: 61: 62: 63: 64: 65:
66: public function addTitlesToResult($storeId)
67: {
68: $adapter = $this->getConnection();
69: $optionTypePriceTable = $this->getTable('catalog/product_option_type_price');
70: $optionTitleTable = $this->getTable('catalog/product_option_type_title');
71: $priceExpr = $adapter->getCheckSql(
72: 'store_value_price.price IS NULL',
73: 'default_value_price.price',
74: 'store_value_price.price'
75: );
76: $priceTypeExpr = $adapter->getCheckSql(
77: 'store_value_price.price_type IS NULL',
78: 'default_value_price.price_type',
79: 'store_value_price.price_type'
80: );
81: $titleExpr = $adapter->getCheckSql(
82: 'store_value_title.title IS NULL',
83: 'default_value_title.title',
84: 'store_value_title.title'
85: );
86: $joinExprDefaultPrice = 'default_value_price.option_type_id = main_table.option_type_id AND '
87: . $adapter->quoteInto('default_value_price.store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
88:
89: $joinExprStorePrice = 'store_value_price.option_type_id = main_table.option_type_id AND '
90: . $adapter->quoteInto('store_value_price.store_id = ?', $storeId);
91:
92: $joinExprTitle = 'store_value_title.option_type_id = main_table.option_type_id AND '
93: . $adapter->quoteInto('store_value_title.store_id = ?', $storeId);
94:
95: $this->getSelect()
96: ->joinLeft(
97: array('default_value_price' => $optionTypePriceTable),
98: $joinExprDefaultPrice,
99: array('default_price'=>'price','default_price_type'=>'price_type')
100: )
101: ->joinLeft(
102: array('store_value_price' => $optionTypePriceTable),
103: $joinExprStorePrice,
104: array(
105: 'store_price' => 'price',
106: 'store_price_type' => 'price_type',
107: 'price' => $priceExpr,
108: 'price_type' => $priceTypeExpr
109: )
110: )
111: ->join(
112: array('default_value_title' => $optionTitleTable),
113: 'default_value_title.option_type_id = main_table.option_type_id',
114: array('default_title' => 'title')
115: )
116: ->joinLeft(
117: array('store_value_title' => $optionTitleTable),
118: $joinExprTitle,
119: array(
120: 'store_title' => 'title',
121: 'title' => $titleExpr)
122: )
123: ->where('default_value_title.store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
124:
125: return $this;
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function addTitleToResult($storeId)
135: {
136: $optionTitleTable = $this->getTable('catalog/product_option_type_title');
137: $titleExpr = $this->getConnection()
138: ->getCheckSql('store_value_title.title IS NULL', 'default_value_title.title', 'store_value_title.title');
139:
140: $joinExpr = 'store_value_title.option_type_id = main_table.option_type_id AND '
141: . $this->getConnection()->quoteInto('store_value_title.store_id = ?', $storeId);
142: $this->getSelect()
143: ->join(
144: array('default_value_title' => $optionTitleTable),
145: 'default_value_title.option_type_id = main_table.option_type_id',
146: array('default_title' => 'title')
147: )
148: ->joinLeft(
149: array('store_value_title' => $optionTitleTable),
150: $joinExpr,
151: array(
152: 'store_title' => 'title',
153: 'title' => $titleExpr
154: )
155: )
156: ->where('default_value_title.store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
157:
158: return $this;
159: }
160:
161: 162: 163: 164: 165: 166:
167: public function addPriceToResult($storeId)
168: {
169: $optionTypeTable = $this->getTable('catalog/product_option_type_price');
170: $priceExpr = $this->getConnection()
171: ->getCheckSql('store_value_price.price IS NULL', 'default_value_price.price', 'store_value_price.price');
172: $priceTypeExpr = $this->getConnection()
173: ->getCheckSql(
174: 'store_value_price.price_type IS NULL',
175: 'default_value_price.price_type',
176: 'store_value_price.price_type'
177: );
178:
179: $joinExprDefault = 'default_value_price.option_type_id = main_table.option_type_id AND '
180: . $this->getConnection()->quoteInto('default_value_price.store_id = ?', Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
181: $joinExprStore = 'store_value_price.option_type_id = main_table.option_type_id AND '
182: . $this->getConnection()->quoteInto('store_value_price.store_id = ?', $storeId);
183: $this->getSelect()
184: ->joinLeft(
185: array('default_value_price' => $optionTypeTable),
186: $joinExprDefault,
187: array(
188: 'default_price' => 'price',
189: 'default_price_type'=>'price_type'
190: )
191: )
192: ->joinLeft(
193: array('store_value_price' => $optionTypeTable),
194: $joinExprStore,
195: array(
196: 'store_price' => 'price',
197: 'store_price_type' => 'price_type',
198: 'price' => $priceExpr,
199: 'price_type' => $priceTypeExpr
200: )
201: );
202:
203: return $this;
204: }
205:
206: 207: 208: 209: 210: 211: 212:
213: public function getValuesByOption($optionIds, $storeId = null)
214: {
215: if (!is_array($optionIds)) {
216: $optionIds = array($optionIds);
217: }
218:
219: return $this->addFieldToFilter('main_table.option_type_id', array('in' => $optionIds));
220: }
221:
222: 223: 224: 225: 226: 227:
228: public function addOptionToFilter($option)
229: {
230: if (empty($option)) {
231: $this->addFieldToFilter('option_id', '');
232: } elseif (is_array($option)) {
233: $this->addFieldToFilter('option_id', array('in' => $option));
234: } elseif ($option instanceof Mage_Catalog_Model_Product_Option) {
235: $this->addFieldToFilter('option_id', $option->getId());
236: } else {
237: $this->addFieldToFilter('option_id', $option);
238: }
239:
240: return $this;
241: }
242: }
243: