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_Wishlist_Model_Wishlist extends Mage_Core_Model_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_itemCollection = null;
00043
00044
00045
00046
00047
00048
00049 protected $_store = null;
00050
00051
00052
00053
00054
00055
00056 protected $_storeIds = null;
00057
00058
00059
00060
00061
00062 protected function _construct()
00063 {
00064 $this->_init('wishlist/wishlist');
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074 public function loadByCustomer($customer, $create = false)
00075 {
00076 if ($customer instanceof Mage_Customer_Model_Customer) {
00077 $customer = $customer->getId();
00078 }
00079
00080 $customerIdFieldName = $this->_getResource()->getCustomerIdFieldName();
00081 $this->_getResource()->load($this, $customer, $customerIdFieldName);
00082 if (!$this->getId() && $create) {
00083 $this->setCustomerId($customer);
00084 $this->setSharingCode($this->_getSharingRandomCode());
00085 $this->save();
00086 }
00087
00088 return $this;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097 public function loadByCode($code)
00098 {
00099 $this->_getResource()->load($this, $code, 'sharing_code');
00100 if(!$this->getShared()) {
00101 $this->setId(null);
00102 }
00103 return $this;
00104 }
00105
00106
00107
00108
00109
00110
00111 protected function _getSharingRandomCode()
00112 {
00113 return md5(microtime() . rand());
00114 }
00115
00116
00117
00118
00119
00120
00121 public function getItemCollection()
00122 {
00123 if(is_null($this->_itemCollection)) {
00124 $this->_itemCollection = Mage::getResourceModel('wishlist/item_collection')
00125 ->setStoreId($this->getStore()->getId())
00126 ->addWishlistFilter($this);
00127 }
00128
00129 return $this->_itemCollection;
00130 }
00131
00132
00133
00134
00135
00136
00137 public function getProductCollection()
00138 {
00139 $collection = $this->getData('product_collection');
00140 if (is_null($collection)) {
00141 $collection = Mage::getResourceModel('wishlist/product_collection')
00142 ->setStoreId($this->getStore()->getId())
00143 ->addWishlistFilter($this)
00144 ->addWishListSortOrder();
00145 $this->setData('product_collection', $collection);
00146 }
00147 return $collection;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156 public function addNewItem($productId)
00157 {
00158 $item = Mage::getModel('wishlist/item');
00159 $item->loadByProductWishlist($this->getId(), $productId, $this->getSharedStoreIds());
00160
00161 if (!$item->getId()) {
00162 $item->setProductId($productId)
00163 ->setWishlistId($this->getId())
00164 ->setAddedAt(now())
00165 ->setStoreId($this->getStore()->getId())
00166 ->save();
00167 }
00168
00169 return $item;
00170 }
00171
00172
00173
00174
00175
00176
00177
00178 public function setCustomerId($customerId)
00179 {
00180 return $this->setData($this->_getResource()->getCustomerIdFieldName(), $customerId);
00181 }
00182
00183
00184
00185
00186
00187
00188 public function getCustomerId()
00189 {
00190 return $this->getData($this->_getResource()->getCustomerIdFieldName());
00191 }
00192
00193
00194
00195
00196
00197
00198 public function getDataForSave()
00199 {
00200 $data = array();
00201 $data[$this->_getResource()->getCustomerIdFieldName()] = $this->getCustomerId();
00202 $data['shared'] = (int) $this->getShared();
00203 $data['sharing_code']= $this->getSharingCode();
00204 return $data;
00205 }
00206
00207
00208
00209
00210
00211
00212 public function getSharedStoreIds()
00213 {
00214 if (is_null($this->_storeIds)) {
00215 $this->_storeIds = $this->getStore()->getWebsite()->getStoreIds();
00216 }
00217 return $this->_storeIds;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226 public function setSharedStoreIds($storeIds)
00227 {
00228 $this->_storeIds = $storeIds;
00229 return $this;
00230 }
00231
00232
00233
00234
00235
00236
00237 public function getStore()
00238 {
00239 if (is_null($this->_store)) {
00240 $this->setStore(Mage::app()->getStore());
00241 }
00242 return $this->_store;
00243 }
00244
00245
00246
00247
00248
00249
00250
00251 public function setStore($store)
00252 {
00253 $this->_store = $store;
00254 return $this;
00255 }
00256
00257
00258
00259
00260
00261
00262 public function getItemsCount()
00263 {
00264 return $this->_getResource()->fetchItemsCount($this);
00265 }
00266
00267
00268
00269
00270
00271
00272 public function isSalable()
00273 {
00274 foreach ($this->getProductCollection() as $product) {
00275 if ($product->getIsSalable()) {
00276 return true;
00277 }
00278 }
00279 return false;
00280 }
00281 }