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: 36: 37: 38: 39: 40: 41: 42: 43:
44: class Mage_Wishlist_Model_Wishlist extends Mage_Core_Model_Abstract
45: {
46: 47: 48: 49: 50:
51: protected $_eventPrefix = 'wishlist';
52: 53: 54: 55: 56:
57: protected $_itemCollection = null;
58:
59: 60: 61: 62: 63:
64: protected $_store = null;
65:
66: 67: 68: 69: 70:
71: protected $_storeIds = null;
72:
73: 74: 75:
76: protected function _construct()
77: {
78: $this->_init('wishlist/wishlist');
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: public function loadByCustomer($customer, $create = false)
89: {
90: if ($customer instanceof Mage_Customer_Model_Customer) {
91: $customer = $customer->getId();
92: }
93:
94: $customer = (int) $customer;
95: $customerIdFieldName = $this->_getResource()->getCustomerIdFieldName();
96: $this->_getResource()->load($this, $customer, $customerIdFieldName);
97: if (!$this->getId() && $create) {
98: $this->setCustomerId($customer);
99: $this->setSharingCode($this->_getSharingRandomCode());
100: $this->save();
101: }
102:
103: return $this;
104: }
105:
106: 107: 108: 109: 110:
111: public function getName()
112: {
113: $name = $this->_getData('name');
114: if (!strlen($name)) {
115: return Mage::helper('wishlist')->getDefaultWishlistName();
116: }
117: return $name;
118: }
119:
120: 121: 122: 123: 124:
125: public function generateSharingCode()
126: {
127: $this->setSharingCode($this->_getSharingRandomCode());
128: return $this;
129: }
130:
131: 132: 133: 134: 135: 136:
137: public function loadByCode($code)
138: {
139: $this->_getResource()->load($this, $code, 'sharing_code');
140: if(!$this->getShared()) {
141: $this->setId(null);
142: }
143:
144: return $this;
145: }
146:
147: 148: 149: 150: 151:
152: protected function _getSharingRandomCode()
153: {
154: return Mage::helper('core')->uniqHash();
155: }
156:
157: 158: 159: 160: 161:
162: protected function _beforeSave()
163: {
164: parent::_beforeSave();
165: $this->setUpdatedAt(Mage::getSingleton('core/date')->gmtDate());
166: return $this;
167: }
168:
169: 170: 171: 172: 173:
174: protected function _afterSave()
175: {
176: parent::_afterSave();
177:
178: if (null !== $this->_itemCollection) {
179: $this->getItemCollection()->save();
180: }
181: return $this;
182: }
183:
184: 185: 186: 187: 188: 189: 190: 191: 192:
193: protected function _addCatalogProduct(Mage_Catalog_Model_Product $product, $qty = 1, $forciblySetQty = false)
194: {
195: $item = null;
196: foreach ($this->getItemCollection() as $_item) {
197: if ($_item->representProduct($product)) {
198: $item = $_item;
199: break;
200: }
201: }
202:
203: if ($item === null) {
204: $storeId = $product->hasWishlistStoreId() ? $product->getWishlistStoreId() : $this->getStore()->getId();
205: $item = Mage::getModel('wishlist/item');
206: $item->setProductId($product->getId())
207: ->setWishlistId($this->getId())
208: ->setAddedAt(now())
209: ->setStoreId($storeId)
210: ->setOptions($product->getCustomOptions())
211: ->setProduct($product)
212: ->setQty($qty)
213: ->save();
214: if ($item->getId()) {
215: $this->getItemCollection()->addItem($item);
216: }
217: } else {
218: $qty = $forciblySetQty ? $qty : $item->getQty() + $qty;
219: $item->setQty($qty)
220: ->save();
221: }
222:
223: $this->addItem($item);
224:
225: return $item;
226: }
227:
228: 229: 230: 231: 232:
233: public function getItemCollection()
234: {
235: if (is_null($this->_itemCollection)) {
236:
237: $currentWebsiteOnly = !Mage::app()->getStore()->isAdmin();
238: $this->_itemCollection = Mage::getResourceModel('wishlist/item_collection')
239: ->addWishlistFilter($this)
240: ->addStoreFilter($this->getSharedStoreIds($currentWebsiteOnly))
241: ->setVisibilityFilter();
242: }
243:
244: return $this->_itemCollection;
245: }
246:
247: 248: 249: 250: 251: 252:
253: public function getItem($itemId)
254: {
255: if (!$itemId) {
256: return false;
257: }
258: return $this->getItemCollection()->getItemById($itemId);
259: }
260:
261: 262: 263: 264: 265: 266: 267: 268:
269: public function getProductCollection()
270: {
271: $collection = $this->getData('product_collection');
272: if (is_null($collection)) {
273: $collection = Mage::getResourceModel('wishlist/product_collection');
274: $this->setData('product_collection', $collection);
275: }
276: return $collection;
277: }
278:
279: 280: 281: 282: 283: 284:
285: public function addItem(Mage_Wishlist_Model_Item $item)
286: {
287: $item->setWishlist($this);
288: if (!$item->getId()) {
289: $this->getItemCollection()->addItem($item);
290: Mage::dispatchEvent('wishlist_add_item', array('item' => $item));
291: }
292: return $this;
293: }
294:
295: 296: 297: 298: 299: 300: 301: 302: 303:
304: public function addNewItem($product, $buyRequest = null, $forciblySetQty = false)
305: {
306: 307: 308: 309: 310:
311: if ($product instanceof Mage_Catalog_Model_Product) {
312: $productId = $product->getId();
313:
314: $storeId = $product->hasWishlistStoreId() ? $product->getWishlistStoreId() : $product->getStoreId();
315: } else {
316: $productId = (int) $product;
317: if ($buyRequest->getStoreId()) {
318: $storeId = $buyRequest->getStoreId();
319: } else {
320: $storeId = Mage::app()->getStore()->getId();
321: }
322: }
323:
324:
325: $product = Mage::getModel('catalog/product')
326: ->setStoreId($storeId)
327: ->load($productId);
328:
329: if ($buyRequest instanceof Varien_Object) {
330: $_buyRequest = $buyRequest;
331: } elseif (is_string($buyRequest)) {
332: $_buyRequest = new Varien_Object(unserialize($buyRequest));
333: } elseif (is_array($buyRequest)) {
334: $_buyRequest = new Varien_Object($buyRequest);
335: } else {
336: $_buyRequest = new Varien_Object();
337: }
338:
339: $cartCandidates = $product->getTypeInstance(true)
340: ->processConfiguration($_buyRequest, $product);
341:
342: 343: 344:
345: if (is_string($cartCandidates)) {
346: return $cartCandidates;
347: }
348:
349: 350: 351:
352: if (!is_array($cartCandidates)) {
353: $cartCandidates = array($cartCandidates);
354: }
355:
356: $errors = array();
357: $items = array();
358:
359: foreach ($cartCandidates as $candidate) {
360: if ($candidate->getParentProductId()) {
361: continue;
362: }
363: $candidate->setWishlistStoreId($storeId);
364:
365: $qty = $candidate->getQty() ? $candidate->getQty() : 1;
366: $item = $this->_addCatalogProduct($candidate, $qty, $forciblySetQty);
367: $items[] = $item;
368:
369:
370: if ($item->getHasError()) {
371: $errors[] = $item->getMessage();
372: }
373: }
374:
375: Mage::dispatchEvent('wishlist_product_add_after', array('items' => $items));
376:
377: return $item;
378: }
379:
380: 381: 382: 383: 384: 385:
386: public function setCustomerId($customerId)
387: {
388: return $this->setData($this->_getResource()->getCustomerIdFieldName(), $customerId);
389: }
390:
391: 392: 393: 394: 395:
396: public function getCustomerId()
397: {
398: return $this->getData($this->_getResource()->getCustomerIdFieldName());
399: }
400:
401: 402: 403: 404: 405:
406: public function getDataForSave()
407: {
408: $data = array();
409: $data[$this->_getResource()->getCustomerIdFieldName()] = $this->getCustomerId();
410: $data['shared'] = (int) $this->getShared();
411: $data['sharing_code']= $this->getSharingCode();
412: return $data;
413: }
414:
415: 416: 417: 418: 419: 420:
421: public function getSharedStoreIds($current = true)
422: {
423: if (is_null($this->_storeIds) || !is_array($this->_storeIds)) {
424: if ($current) {
425: $this->_storeIds = $this->getStore()->getWebsite()->getStoreIds();
426: } else {
427: $_storeIds = array();
428: $stores = Mage::app()->getStores();
429: foreach ($stores as $store) {
430: $_storeIds[] = $store->getId();
431: }
432: $this->_storeIds = $_storeIds;
433: }
434: }
435: return $this->_storeIds;
436: }
437:
438: 439: 440: 441: 442: 443:
444: public function setSharedStoreIds($storeIds)
445: {
446: $this->_storeIds = (array) $storeIds;
447: return $this;
448: }
449:
450: 451: 452: 453: 454:
455: public function getStore()
456: {
457: if (is_null($this->_store)) {
458: $this->setStore(Mage::app()->getStore());
459: }
460: return $this->_store;
461: }
462:
463: 464: 465: 466: 467: 468:
469: public function setStore($store)
470: {
471: $this->_store = $store;
472: return $this;
473: }
474:
475: 476: 477: 478: 479:
480: public function getItemsCount()
481: {
482: return $this->getItemCollection()->getSize();
483: }
484:
485: 486: 487: 488: 489:
490: public function isSalable()
491: {
492: foreach ($this->getItemCollection() as $item) {
493: if ($item->getProduct()->getIsSalable()) {
494: return true;
495: }
496: }
497: return false;
498: }
499:
500: 501: 502: 503: 504: 505:
506: public function isOwner($customerId)
507: {
508: return $customerId == $this->getCustomerId();
509: }
510:
511:
512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532:
533: public function updateItem($itemId, $buyRequest, $params = null)
534: {
535: $item = null;
536: if ($itemId instanceof Mage_Wishlist_Model_Item) {
537: $item = $itemId;
538: } else {
539: $item = $this->getItem((int)$itemId);
540: }
541: if (!$item) {
542: Mage::throwException(Mage::helper('wishlist')->__('Cannot specify wishlist item.'));
543: }
544:
545: $product = $item->getProduct();
546: $productId = $product->getId();
547: if ($productId) {
548: if (!$params) {
549: $params = new Varien_Object();
550: } else if (is_array($params)) {
551: $params = new Varien_Object($params);
552: }
553: $params->setCurrentConfig($item->getBuyRequest());
554: $buyRequest = Mage::helper('catalog/product')->addParamsToBuyRequest($buyRequest, $params);
555:
556: $product->setWishlistStoreId($item->getStoreId());
557: $items = $this->getItemCollection();
558: $isForceSetQuantity = true;
559: foreach ($items as $_item) {
560:
561: if ($_item->getProductId() == $product->getId()
562: && $_item->representProduct($product)
563: && $_item->getId() != $item->getId()) {
564:
565: $isForceSetQuantity = false;
566: }
567: }
568: $resultItem = $this->addNewItem($product, $buyRequest, $isForceSetQuantity);
569: 570: 571:
572: if (is_string($resultItem)) {
573: Mage::throwException(Mage::helper('checkout')->__($resultItem));
574: }
575:
576: if ($resultItem->getId() != $itemId) {
577: if ($resultItem->getDescription() != $item->getDescription()) {
578: $resultItem->setDescription($item->getDescription())->save();
579: }
580: $item->isDeleted(true);
581: $this->setDataChanges(true);
582: } else {
583: $resultItem->setQty($buyRequest->getQty() * 1);
584: $resultItem->setOrigData('qty', 0);
585: }
586: } else {
587: Mage::throwException(Mage::helper('checkout')->__('The product does not exist.'));
588: }
589: return $this;
590: }
591:
592: 593: 594: 595: 596:
597: public function save()
598: {
599: $this->_hasDataChanges = true;
600: return parent::save();
601: }
602: }
603: