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_Helper_Product_Compare extends Mage_Core_Helper_Url
00036 {
00037
00038
00039
00040
00041
00042 protected $_itemCollection;
00043
00044
00045
00046
00047
00048
00049 protected $_hasItems;
00050
00051
00052
00053
00054
00055
00056 protected $_allowUsedFlat = true;
00057
00058
00059
00060
00061
00062
00063 public function getListUrl()
00064 {
00065 $itemIds = array();
00066 foreach ($this->getItemCollection() as $item) {
00067 $itemIds[] = $item->getId();
00068 }
00069
00070 $params = array(
00071 'items'=>implode(',', $itemIds),
00072 Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
00073 );
00074
00075 return $this->_getUrl('catalog/product_compare', $params);
00076 }
00077
00078
00079
00080
00081
00082
00083
00084 protected function _getUrlParams($product)
00085 {
00086 return array(
00087 'product' => $product->getId(),
00088 Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
00089 );
00090 }
00091
00092
00093
00094
00095
00096
00097
00098 public function getAddUrl($product)
00099 {
00100 return $this->_getUrl('catalog/product_compare/add', $this->_getUrlParams($product));
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 public function getAddToWishlistUrl($product)
00110 {
00111 $beforeCompareUrl = Mage::getSingleton('catalog/session')->getBeforeCompareUrl();
00112
00113 $params = array(
00114 'product'=>$product->getId(),
00115 Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl($beforeCompareUrl)
00116 );
00117
00118 return $this->_getUrl('wishlist/index/add', $params);
00119 }
00120
00121
00122
00123
00124
00125
00126
00127 public function getAddToCartUrl($product)
00128 {
00129 $beforeCompareUrl = Mage::getSingleton('catalog/session')->getBeforeCompareUrl();
00130 $params = array(
00131 'product'=>$product->getId(),
00132 Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl($beforeCompareUrl)
00133 );
00134
00135 return $this->_getUrl('checkout/cart/add', $params);
00136 }
00137
00138
00139
00140
00141
00142
00143
00144 public function getRemoveUrl($item)
00145 {
00146 $params = array(
00147 'product'=>$item->getId(),
00148 Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
00149 );
00150 return $this->_getUrl('catalog/product_compare/remove', $params);
00151 }
00152
00153
00154
00155
00156
00157
00158 public function getClearListUrl()
00159 {
00160 $params = array(
00161 Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
00162 );
00163 return $this->_getUrl('catalog/product_compare/clear', $params);
00164 }
00165
00166
00167
00168
00169
00170
00171 public function getItemCollection()
00172 {
00173 if (!$this->_itemCollection) {
00174 $this->_itemCollection = Mage::getResourceModel('catalog/product_compare_item_collection')
00175 ->useProductItem(true)
00176 ->setStoreId(Mage::app()->getStore()->getId());
00177
00178 if (Mage::getSingleton('customer/session')->isLoggedIn()) {
00179 $this->_itemCollection->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
00180 }
00181 else {
00182 $this->_itemCollection->setVisitorId(Mage::getSingleton('log/visitor')->getId());
00183 }
00184
00185 Mage::getSingleton('catalog/product_visibility')->addVisibleInSiteFilterToCollection($this->_itemCollection);
00186
00187 $this->_itemCollection->addAttributeToSelect('name')
00188 ->addUrlRewrite()
00189 ->load();
00190 }
00191
00192 return $this->_itemCollection;
00193 }
00194
00195
00196
00197
00198
00199
00200 public function calculate()
00201 {
00202 if (!Mage::getSingleton('customer/session')->isLoggedIn()
00203 and !Mage::getSingleton('log/visitor')->hasCatalogCompareItemsCount()
00204 ) {
00205 Mage::getSingleton('log/visitor')->setCatalogCompareItemsCount(0);
00206 return $this;
00207 }
00208
00209 $itemCollection = Mage::getResourceModel('catalog/product_compare_item_collection');
00210
00211 $itemCollection->setStoreId(Mage::app()->getStore()->getId());
00212 if (Mage::getSingleton('customer/session')->isLoggedIn()) {
00213 $itemCollection->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
00214 }
00215 else {
00216 $itemCollection->setVisitorId(Mage::getSingleton('log/visitor')->getId());
00217 }
00218 Mage::getSingleton('catalog/product_visibility')
00219 ->addVisibleInSiteFilterToCollection($itemCollection);
00220
00221 Mage::getSingleton('log/visitor')->setCatalogCompareItemsCount($itemCollection->getSize());
00222 return $this;
00223 }
00224
00225
00226
00227
00228
00229
00230 public function getItemCount()
00231 {
00232 if (is_null(Mage::getSingleton('log/visitor')->getCatalogCompareItemsCount())) {
00233 $this->calculate();
00234 }
00235 return Mage::getSingleton('log/visitor')->getCatalogCompareItemsCount();
00236 }
00237
00238
00239
00240
00241
00242
00243 public function hasItems()
00244 {
00245 return $this->getItemCount() > 0;
00246 }
00247
00248
00249
00250
00251
00252
00253
00254 public function setAllowUsedFlat($flag)
00255 {
00256 $this->_allowUsedFlat = (bool)$flag;
00257 return $this;
00258 }
00259
00260
00261
00262
00263
00264
00265 public function getAllowUsedFlat()
00266 {
00267 return $this->_allowUsedFlat;
00268 }
00269 }