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_Product_Compare_Item extends Mage_Core_Model_Abstract
00036 {
00037
00038
00039
00040
00041 protected function _construct()
00042 {
00043 $this->_init('catalog/product_compare_item');
00044 }
00045
00046
00047
00048
00049
00050
00051
00052 public function addCustomerData(Mage_Customer_Model_Customer $customer)
00053 {
00054 $this->setCustomerId($customer->getId());
00055 $this->setVisitorId(0);
00056 return $this;
00057 }
00058
00059
00060
00061
00062
00063
00064
00065 public function addVisitorId($visitorId)
00066 {
00067 $this->setVisitorId($visitorId);
00068 return $this;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077 public function loadByProduct($product)
00078 {
00079 $this->_getResource()->loadByProduct($this, $product);
00080 return $this;
00081 }
00082
00083
00084
00085
00086
00087
00088
00089 public function addProductData($product)
00090 {
00091 if ($product instanceof Mage_Catalog_Model_Product) {
00092 $this->setProductId($product->getId());
00093 }
00094 elseif(intval($product)) {
00095 $this->setProductId(intval($product));
00096 }
00097
00098 return $this;
00099 }
00100
00101
00102
00103
00104
00105
00106 public function getDataForSave()
00107 {
00108 $data = array();
00109 $data['customer_id'] = $this->getCustomerId();
00110 $data['visitor_id'] = $this->getVisitorId();
00111 $data['product_id'] = $this->getProductId();
00112
00113 return $data;
00114 }
00115
00116
00117
00118
00119
00120
00121 public function bindCustomerLogin()
00122 {
00123 $customer = Mage::getSingleton('customer/session')->getCustomer();
00124 $visitorItemCollection = Mage::getResourceModel('catalog/product_compare_item_collection')
00125 ->setObject('catalog/product_compare_item')
00126 ->setVisitorId(Mage::getSingleton('log/visitor')->getId())
00127 ->load();
00128
00129 $customerItemCollection = $this->getResourceCollection()
00130 ->setCustomerId($customer->getId())
00131 ->useProductItem(true)
00132 ->load();
00133
00134 $customerProductIds = $customerItemCollection->getProductIds();
00135
00136 foreach ($visitorItemCollection as $item) {
00137 if (in_array($item->getProductId(), $customerProductIds)) {
00138 $item->delete();
00139 }
00140 else {
00141 $item->setCustomerId($customer->getId())
00142 ->setVisitorId(0)
00143 ->save();
00144 }
00145 }
00146
00147 Mage::helper('catalog/product_compare')->calculate();
00148 return $this;
00149 }
00150
00151
00152
00153
00154
00155
00156
00157 public function bindCustomerLogout(Varien_Event_Observer $observer)
00158 {
00159 Mage::getSingleton('log/visitor')->setCatalogCompareItemsCount(0);
00160 return $this;
00161 }
00162
00163
00164
00165
00166
00167
00168 public function clean()
00169 {
00170 $this->_getResource()->clean($this);
00171 return $this;
00172 }
00173 }