00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Compare_Item_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
00036 {
00037
00038
00039
00040
00041
00042 protected $_customerId = 0;
00043
00044
00045
00046
00047
00048
00049 protected $_visitorId = 0;
00050
00051
00052
00053
00054
00055
00056 protected $_comparableAttributes;
00057
00058
00059
00060
00061
00062 protected function _construct()
00063 {
00064 $this->_init('catalog/product_compare_item', 'catalog/product');
00065
00066 $this->_productWebsiteTable = $this->getResource()->getTable('catalog/product_website');
00067 $this->_productCategoryTable= $this->getResource()->getTable('catalog/category_product');
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 public function setCustomerId($customerId)
00077 {
00078 $this->_customerId = $customerId;
00079 $this->_addJoinToSelect();
00080 return $this;
00081 }
00082
00083
00084
00085
00086
00087
00088
00089 public function setVisitorId($visitorId)
00090 {
00091 $this->_visitorId = $visitorId;
00092 $this->_addJoinToSelect();
00093 return $this;
00094 }
00095
00096
00097
00098
00099
00100
00101 public function getCustomerId()
00102 {
00103 return $this->_customerId;
00104 }
00105
00106
00107
00108
00109
00110
00111 public function getVisitorId()
00112 {
00113 return $this->_visitorId;
00114 }
00115
00116
00117
00118
00119
00120
00121 public function getConditionForJoin()
00122 {
00123 if ($this->getCustomerId()) {
00124 return array('customer_id' => $this->getCustomerId());
00125 }
00126
00127 if ($this->getVisitorId()) {
00128 return array('visitor_id' => $this->getVisitorId());
00129 }
00130
00131 return null;
00132 }
00133
00134
00135
00136
00137
00138
00139 public function _addJoinToSelect()
00140 {
00141 $this->joinField(
00142 'catalog_compare_item_id',
00143 'catalog/compare_item',
00144 'catalog_compare_item_id',
00145 'product_id=entity_id',
00146 $this->getConditionForJoin()
00147 );
00148 $this->joinTable(
00149 'catalog/compare_item',
00150 'catalog_compare_item_id=catalog_compare_item_id',
00151 array('product_id', 'customer_id', 'visitor_id'));
00152 $this->addStoreFilter();
00153 return $this;
00154 }
00155
00156
00157
00158
00159
00160
00161 protected function _getAttributeSetIds()
00162 {
00163
00164 $compareConds = array(
00165 'compare.product_id=entity.entity_id',
00166 );
00167 if ($this->getCustomerId()) {
00168 $compareConds[] = $this->getConnection()
00169 ->quoteInto('compare.customer_id=?', $this->getCustomerId());
00170 } else {
00171 $compareConds[] = $this->getConnection()
00172 ->quoteInto('compare.visitor_id=?', $this->getVisitorId());
00173 }
00174
00175
00176 $websiteId = Mage::app()->getStore($this->getStoreId())->getWebsiteId();
00177 $websiteConds = array(
00178 'website.product_id=entity.entity_id',
00179 $this->getConnection()->quoteInto('website.website_id=?', $websiteId)
00180 );
00181
00182
00183 $select = $this->getConnection()->select()
00184 ->distinct(true)
00185 ->from(
00186 array('entity' => $this->getEntity()->getEntityTable()),
00187 'attribute_set_id')
00188 ->join(
00189 array('website' => $this->getTable('catalog/product_website')),
00190 join(' AND ', $websiteConds),
00191 array())
00192 ->join(
00193 array('compare' => $this->getTable('catalog/compare_item')),
00194 join(' AND ', $compareConds),
00195 array()
00196 );
00197 return $this->getConnection()->fetchCol($select);
00198 }
00199
00200
00201
00202
00203
00204
00205
00206 protected function _getAttributeIdsBySetIds(array $setIds)
00207 {
00208 $select = $this->getConnection()->select()
00209 ->distinct(true)
00210 ->from($this->getTable('eav/entity_attribute'), 'attribute_id')
00211 ->where('attribute_set_id IN(?)', $setIds);
00212 return $this->getConnection()->fetchCol($select);
00213 }
00214
00215
00216
00217
00218
00219
00220 public function getComparableAttributes()
00221 {
00222 if (is_null($this->_comparableAttributes)) {
00223 $setIds = $this->_getAttributeSetIds();
00224 if ($setIds) {
00225 $attributeIds = $this->_getAttributeIdsBySetIds($setIds);
00226
00227 $select = $this->getConnection()->select()
00228 ->from($this->getTable('eav/attribute'))
00229 ->where('is_comparable=?', 1)
00230 ->where('attribute_id IN(?)', $attributeIds);
00231 $attributesData = $this->getConnection()->fetchAll($select);
00232
00233 if ($attributesData) {
00234 $entityType = 'catalog_product';
00235 Mage::getSingleton('eav/config')
00236 ->importAttributesData($entityType, $attributesData);
00237 foreach ($attributesData as $data) {
00238 $attribute = Mage::getSingleton('eav/config')
00239 ->getAttribute($entityType, $data['attribute_code']);
00240 $this->_comparableAttributes[$attribute->getAttributeCode()] = $attribute;
00241 }
00242 unset($attributesData);
00243 }
00244 }
00245 else {
00246 $this->_comparableAttributes = array();
00247 }
00248 }
00249 return $this->_comparableAttributes;
00250 }
00251
00252
00253
00254
00255
00256
00257 public function loadComparableAttributes()
00258 {
00259 $comparableAttributes = $this->getComparableAttributes();
00260 $attributes = array();
00261 foreach ($comparableAttributes as $attribute) {
00262 $attributes[] = $attribute->getAttributeCode();
00263 }
00264 $this->addAttributeToSelect($attributes);
00265
00266 return $this;
00267 }
00268
00269
00270
00271
00272
00273
00274 public function useProductItem()
00275 {
00276 $this->setObject('catalog/product');
00277 return $this;
00278 }
00279
00280
00281
00282
00283
00284
00285 public function getProductIds()
00286 {
00287 $ids = array();
00288 foreach ($this->getItems() as $item) {
00289 $ids[] = $item->getProductId();
00290 }
00291
00292 return $ids;
00293 }
00294
00295
00296
00297
00298
00299
00300 public function clear()
00301 {
00302 $where = array();
00303 if ($this->getCustomerId()) {
00304 $where[] = $this->getConnection()->quoteInto('customer_id=?', $this->getCustomerId());
00305 }
00306 if ($this->getVisitorId()) {
00307 $where[] = $this->getConnection()->quoteInto('visitor_id=?', $this->getVisitorId());
00308 }
00309 if (!$where) {
00310 return $this;
00311 }
00312
00313 $this->getConnection()->delete($this->getTable('catalog/compare_item'), $where);
00314
00315 return $this;
00316 }
00317
00318
00319
00320
00321
00322
00323
00324 public function isEnabledFlat()
00325 {
00326 if (!Mage::helper('catalog/product_compare')->getAllowUsedFlat()) {
00327 return false;
00328 }
00329 return parent::isEnabledFlat();
00330 }
00331 }