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_Compare_Item_Collection
36: extends Mage_Catalog_Model_Resource_Product_Collection
37: {
38: 39: 40: 41: 42:
43: protected $_customerId = 0;
44:
45: 46: 47: 48: 49:
50: protected $_visitorId = 0;
51:
52: 53: 54: 55: 56:
57: protected $_comparableAttributes;
58:
59: 60: 61:
62: protected function _construct()
63: {
64: $this->_init('catalog/product_compare_item', 'catalog/product');
65: $this->_initTables();
66: }
67:
68: 69: 70: 71: 72: 73:
74: public function setCustomerId($customerId)
75: {
76: $this->_customerId = (int)$customerId;
77: $this->_addJoinToSelect();
78: return $this;
79: }
80:
81: 82: 83: 84: 85: 86:
87: public function setVisitorId($visitorId)
88: {
89: $this->_visitorId = (int)$visitorId;
90: $this->_addJoinToSelect();
91: return $this;
92: }
93:
94: 95: 96: 97: 98:
99: public function getCustomerId()
100: {
101: return $this->_customerId;
102: }
103:
104: 105: 106: 107: 108:
109: public function getVisitorId()
110: {
111: return $this->_visitorId;
112: }
113:
114: 115: 116: 117: 118:
119: public function getConditionForJoin()
120: {
121: if ($this->getCustomerId()) {
122: return array('customer_id' => $this->getCustomerId());
123: }
124:
125: if ($this->getVisitorId()) {
126: return array('visitor_id' => $this->getVisitorId());
127: }
128:
129: return array('customer_id' => array('null' => true),'visitor_id' => '0');
130: }
131:
132: 133: 134: 135: 136:
137: public function _addJoinToSelect()
138: {
139: $this->joinTable(
140: array('t_compare' => 'catalog/compare_item'),
141: 'product_id=entity_id',
142: array(
143: 'product_id' => 'product_id',
144: 'customer_id' => 'customer_id',
145: 'visitor_id' => 'visitor_id',
146: 'item_store_id' => 'store_id',
147: 'catalog_compare_item_id' => 'catalog_compare_item_id'
148: ),
149: $this->getConditionForJoin()
150: );
151:
152: $this->_productLimitationFilters['store_table'] = 't_compare';
153:
154: return $this;
155: }
156:
157: 158: 159: 160: 161:
162: protected function _getAttributeSetIds()
163: {
164:
165: $compareConds = array(
166: 'compare.product_id=entity.entity_id',
167: );
168: if ($this->getCustomerId()) {
169: $compareConds[] = $this->getConnection()
170: ->quoteInto('compare.customer_id = ?', $this->getCustomerId());
171: } else {
172: $compareConds[] = $this->getConnection()
173: ->quoteInto('compare.visitor_id = ?', $this->getVisitorId());
174: }
175:
176:
177: $websiteId = (int)Mage::app()->getStore($this->getStoreId())->getWebsiteId();
178: $websiteConds = array(
179: 'website.product_id = entity.entity_id',
180: $this->getConnection()->quoteInto('website.website_id = ?', $websiteId)
181: );
182:
183:
184: $select = $this->getConnection()->select()
185: ->distinct(true)
186: ->from(
187: array('entity' => $this->getEntity()->getEntityTable()),
188: 'attribute_set_id')
189: ->join(
190: array('website' => $this->getTable('catalog/product_website')),
191: join(' AND ', $websiteConds),
192: array())
193: ->join(
194: array('compare' => $this->getTable('catalog/compare_item')),
195: join(' AND ', $compareConds),
196: array()
197: );
198: return $this->getConnection()->fetchCol($select);
199: }
200:
201: 202: 203: 204: 205: 206:
207: protected function _getAttributeIdsBySetIds(array $setIds)
208: {
209: $select = $this->getConnection()->select()
210: ->distinct(true)
211: ->from($this->getTable('eav/entity_attribute'), 'attribute_id')
212: ->where('attribute_set_id IN(?)', $setIds);
213: return $this->getConnection()->fetchCol($select);
214: }
215:
216: 217: 218: 219: 220:
221: public function getComparableAttributes()
222: {
223: if (is_null($this->_comparableAttributes)) {
224: $this->_comparableAttributes = array();
225: $setIds = $this->_getAttributeSetIds();
226: if ($setIds) {
227: $attributeIds = $this->_getAttributeIdsBySetIds($setIds);
228:
229: $select = $this->getConnection()->select()
230: ->from(array('main_table' => $this->getTable('eav/attribute')))
231: ->join(
232: array('additional_table' => $this->getTable('catalog/eav_attribute')),
233: 'additional_table.attribute_id=main_table.attribute_id'
234: )
235: ->joinLeft(
236: array('al' => $this->getTable('eav/attribute_label')),
237: 'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int) $this->getStoreId(),
238: array('store_label' => $this->getConnection()->getCheckSql('al.value IS NULL', 'main_table.frontend_label', 'al.value'))
239: )
240: ->where('additional_table.is_comparable=?', 1)
241: ->where('main_table.attribute_id IN(?)', $attributeIds);
242: $attributesData = $this->getConnection()->fetchAll($select);
243: if ($attributesData) {
244: $entityType = Mage_Catalog_Model_Product::ENTITY;
245: Mage::getSingleton('eav/config')
246: ->importAttributesData($entityType, $attributesData);
247: foreach ($attributesData as $data) {
248: $attribute = Mage::getSingleton('eav/config')
249: ->getAttribute($entityType, $data['attribute_code']);
250: $this->_comparableAttributes[$attribute->getAttributeCode()] = $attribute;
251: }
252: unset($attributesData);
253: }
254: }
255: }
256: return $this->_comparableAttributes;
257: }
258:
259: 260: 261: 262: 263:
264: public function loadComparableAttributes()
265: {
266: $comparableAttributes = $this->getComparableAttributes();
267: $attributes = array();
268: foreach ($comparableAttributes as $attribute) {
269: $attributes[] = $attribute->getAttributeCode();
270: }
271: $this->addAttributeToSelect($attributes);
272:
273: return $this;
274: }
275:
276: 277: 278: 279: 280:
281: public function useProductItem()
282: {
283: $this->setObject('catalog/product');
284:
285: $this->setFlag('url_data_object', true);
286: $this->setFlag('do_not_use_category_id', true);
287:
288: return $this;
289: }
290:
291: 292: 293: 294: 295:
296: public function getProductIds()
297: {
298: $ids = array();
299: foreach ($this->getItems() as $item) {
300: $ids[] = $item->getProductId();
301: }
302:
303: return $ids;
304: }
305:
306: 307: 308: 309: 310:
311: public function clear()
312: {
313: Mage::getResourceSingleton('catalog/product_compare_item')
314: ->clearItems($this->getVisitorId(), $this->getCustomerId());
315: Mage::dispatchEvent('catalog_product_compare_item_collection_clear');
316:
317: return $this;
318: }
319:
320: 321: 322: 323: 324: 325:
326: public function isEnabledFlat()
327: {
328: if (!Mage::helper('catalog/product_compare')->getAllowUsedFlat()) {
329: return false;
330: }
331: return parent::isEnabledFlat();
332: }
333: }
334: