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: class Mage_Wishlist_IndexController extends Mage_Wishlist_Controller_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_cookieCheckActions = array('add');
43:
44: 45: 46: 47: 48:
49: protected $_skipAuthentication = false;
50:
51: public function preDispatch()
52: {
53: parent::preDispatch();
54:
55: if (!$this->_skipAuthentication && !Mage::getSingleton('customer/session')->authenticate($this)) {
56: $this->setFlag('', 'no-dispatch', true);
57: if (!Mage::getSingleton('customer/session')->getBeforeWishlistUrl()) {
58: Mage::getSingleton('customer/session')->setBeforeWishlistUrl($this->_getRefererUrl());
59: }
60: Mage::getSingleton('customer/session')->setBeforeWishlistRequest($this->getRequest()->getParams());
61: }
62: if (!Mage::getStoreConfigFlag('wishlist/general/active')) {
63: $this->norouteAction();
64: return;
65: }
66: }
67:
68: 69: 70: 71: 72:
73: public function skipAuthentication()
74: {
75: $this->_skipAuthentication = true;
76: return $this;
77: }
78:
79: 80: 81: 82: 83:
84: protected function _getWishlist($wishlistId = null)
85: {
86: $wishlist = Mage::registry('wishlist');
87: if ($wishlist) {
88: return $wishlist;
89: }
90:
91: try {
92: if (!$wishlistId) {
93: $wishlistId = $this->getRequest()->getParam('wishlist_id');
94: }
95: $customerId = Mage::getSingleton('customer/session')->getCustomerId();
96:
97: $wishlist = Mage::getModel('wishlist/wishlist');
98: if ($wishlistId) {
99: $wishlist->load($wishlistId);
100: } else {
101: $wishlist->loadByCustomer($customerId, true);
102: }
103:
104: if (!$wishlist->getId() || $wishlist->getCustomerId() != $customerId) {
105: $wishlist = null;
106: Mage::throwException(
107: Mage::helper('wishlist')->__("Requested wishlist doesn't exist")
108: );
109: }
110:
111: Mage::register('wishlist', $wishlist);
112: } catch (Mage_Core_Exception $e) {
113: Mage::getSingleton('wishlist/session')->addError($e->getMessage());
114: return false;
115: } catch (Exception $e) {
116: Mage::getSingleton('wishlist/session')->addException($e,
117: Mage::helper('wishlist')->__('Wishlist could not be created.')
118: );
119: return false;
120: }
121:
122: return $wishlist;
123: }
124:
125: 126: 127:
128: public function indexAction()
129: {
130: if (!$this->_getWishlist()) {
131: return $this->norouteAction();
132: }
133: $this->loadLayout();
134:
135: $session = Mage::getSingleton('customer/session');
136: $block = $this->getLayout()->getBlock('customer.wishlist');
137: $referer = $session->getAddActionReferer(true);
138: if ($block) {
139: $block->setRefererUrl($this->_getRefererUrl());
140: if ($referer) {
141: $block->setRefererUrl($referer);
142: }
143: }
144:
145: $this->_initLayoutMessages('customer/session');
146: $this->_initLayoutMessages('checkout/session');
147: $this->_initLayoutMessages('catalog/session');
148: $this->_initLayoutMessages('wishlist/session');
149:
150: $this->renderLayout();
151: }
152:
153: 154: 155:
156: public function addAction()
157: {
158: $wishlist = $this->_getWishlist();
159: if (!$wishlist) {
160: return $this->norouteAction();
161: }
162:
163: $session = Mage::getSingleton('customer/session');
164:
165: $productId = (int) $this->getRequest()->getParam('product');
166: if (!$productId) {
167: $this->_redirect('*/');
168: return;
169: }
170:
171: $product = Mage::getModel('catalog/product')->load($productId);
172: if (!$product->getId() || !$product->isVisibleInCatalog()) {
173: $session->addError($this->__('Cannot specify product.'));
174: $this->_redirect('*/');
175: return;
176: }
177:
178: try {
179: $requestParams = $this->getRequest()->getParams();
180: if ($session->getBeforeWishlistRequest()) {
181: $requestParams = $session->getBeforeWishlistRequest();
182: $session->unsBeforeWishlistRequest();
183: }
184: $buyRequest = new Varien_Object($requestParams);
185:
186: $result = $wishlist->addNewItem($product, $buyRequest);
187: if (is_string($result)) {
188: Mage::throwException($result);
189: }
190: $wishlist->save();
191:
192: Mage::dispatchEvent(
193: 'wishlist_add_product',
194: array(
195: 'wishlist' => $wishlist,
196: 'product' => $product,
197: 'item' => $result
198: )
199: );
200:
201: $referer = $session->getBeforeWishlistUrl();
202: if ($referer) {
203: $session->setBeforeWishlistUrl(null);
204: } else {
205: $referer = $this->_getRefererUrl();
206: }
207:
208: 209: 210:
211: $session->setAddActionReferer($referer);
212:
213: Mage::helper('wishlist')->calculate();
214:
215: $message = $this->__('%1$s has been added to your wishlist. Click <a href="%2$s">here</a> to continue shopping.', $product->getName(), Mage::helper('core')->escapeUrl($referer));
216: $session->addSuccess($message);
217: }
218: catch (Mage_Core_Exception $e) {
219: $session->addError($this->__('An error occurred while adding item to wishlist: %s', $e->getMessage()));
220: }
221: catch (Exception $e) {
222: $session->addError($this->__('An error occurred while adding item to wishlist.'));
223: }
224:
225: $this->_redirect('*', array('wishlist_id' => $wishlist->getId()));
226: }
227:
228: 229: 230:
231: public function configureAction()
232: {
233: $id = (int) $this->getRequest()->getParam('id');
234: try {
235:
236: $item = Mage::getModel('wishlist/item');
237: $item->loadWithOptions($id);
238: if (!$item->getId()) {
239: Mage::throwException($this->__('Cannot load wishlist item'));
240: }
241: $wishlist = $this->_getWishlist($item->getWishlistId());
242: if (!$wishlist) {
243: return $this->norouteAction();
244: }
245:
246: Mage::register('wishlist_item', $item);
247:
248: $params = new Varien_Object();
249: $params->setCategoryId(false);
250: $params->setConfigureMode(true);
251: $buyRequest = $item->getBuyRequest();
252: if (!$buyRequest->getQty() && $item->getQty()) {
253: $buyRequest->setQty($item->getQty());
254: }
255: if ($buyRequest->getQty() && !$item->getQty()) {
256: $item->setQty($buyRequest->getQty());
257: Mage::helper('wishlist')->calculate();
258: }
259: $params->setBuyRequest($buyRequest);
260: Mage::helper('catalog/product_view')->prepareAndRender($item->getProductId(), $this, $params);
261: } catch (Mage_Core_Exception $e) {
262: Mage::getSingleton('customer/session')->addError($e->getMessage());
263: $this->_redirect('*');
264: return;
265: } catch (Exception $e) {
266: Mage::getSingleton('customer/session')->addError($this->__('Cannot configure product'));
267: Mage::logException($e);
268: $this->_redirect('*');
269: return;
270: }
271: }
272:
273: 274: 275:
276: public function updateItemOptionsAction()
277: {
278: $session = Mage::getSingleton('customer/session');
279: $productId = (int) $this->getRequest()->getParam('product');
280: if (!$productId) {
281: $this->_redirect('*/');
282: return;
283: }
284:
285: $product = Mage::getModel('catalog/product')->load($productId);
286: if (!$product->getId() || !$product->isVisibleInCatalog()) {
287: $session->addError($this->__('Cannot specify product.'));
288: $this->_redirect('*/');
289: return;
290: }
291:
292: try {
293: $id = (int) $this->getRequest()->getParam('id');
294:
295: $item = Mage::getModel('wishlist/item');
296: $item->load($id);
297: $wishlist = $this->_getWishlist($item->getWishlistId());
298: if (!$wishlist) {
299: $this->_redirect('*/');
300: return;
301: }
302:
303: $buyRequest = new Varien_Object($this->getRequest()->getParams());
304:
305: $wishlist->updateItem($id, $buyRequest)
306: ->save();
307:
308: Mage::helper('wishlist')->calculate();
309: Mage::dispatchEvent('wishlist_update_item', array(
310: 'wishlist' => $wishlist, 'product' => $product, 'item' => $wishlist->getItem($id))
311: );
312:
313: Mage::helper('wishlist')->calculate();
314:
315: $message = $this->__('%1$s has been updated in your wishlist.', $product->getName());
316: $session->addSuccess($message);
317: } catch (Mage_Core_Exception $e) {
318: $session->addError($e->getMessage());
319: } catch (Exception $e) {
320: $session->addError($this->__('An error occurred while updating wishlist.'));
321: Mage::logException($e);
322: }
323: $this->_redirect('*/*', array('wishlist_id' => $wishlist->getId()));
324: }
325:
326: 327: 328:
329: public function updateAction()
330: {
331: if (!$this->_validateFormKey()) {
332: return $this->_redirect('*/*/');
333: }
334: $wishlist = $this->_getWishlist();
335: if (!$wishlist) {
336: return $this->norouteAction();
337: }
338:
339: $post = $this->getRequest()->getPost();
340: if($post && isset($post['description']) && is_array($post['description'])) {
341: $updatedItems = 0;
342:
343: foreach ($post['description'] as $itemId => $description) {
344: $item = Mage::getModel('wishlist/item')->load($itemId);
345: if ($item->getWishlistId() != $wishlist->getId()) {
346: continue;
347: }
348:
349:
350: $description = (string) $description;
351: if (!strlen($description)) {
352: $description = $item->getDescription();
353: }
354:
355: $qty = null;
356: if (isset($post['qty'][$itemId])) {
357: $qty = $this->_processLocalizedQty($post['qty'][$itemId]);
358: }
359: if (is_null($qty)) {
360: $qty = $item->getQty();
361: if (!$qty) {
362: $qty = 1;
363: }
364: } elseif (0 == $qty) {
365: try {
366: $item->delete();
367: } catch (Exception $e) {
368: Mage::logException($e);
369: Mage::getSingleton('customer/session')->addError(
370: $this->__('Can\'t delete item from wishlist')
371: );
372: }
373: }
374:
375:
376: if (($item->getDescription() == $description) && ($item->getQty() == $qty)) {
377: continue;
378: }
379: try {
380: $item->setDescription($description)
381: ->setQty($qty)
382: ->save();
383: $updatedItems++;
384: } catch (Exception $e) {
385: Mage::getSingleton('customer/session')->addError(
386: $this->__('Can\'t save description %s', Mage::helper('core')->htmlEscape($description))
387: );
388: }
389: }
390:
391:
392: if ($updatedItems) {
393: try {
394: $wishlist->save();
395: Mage::helper('wishlist')->calculate();
396: }
397: catch (Exception $e) {
398: Mage::getSingleton('customer/session')->addError($this->__('Can\'t update wishlist'));
399: }
400: }
401:
402: if (isset($post['save_and_share'])) {
403: $this->_redirect('*/*/share', array('wishlist_id' => $wishlist->getId()));
404: return;
405: }
406: }
407: $this->_redirect('*', array('wishlist_id' => $wishlist->getId()));
408: }
409:
410: 411: 412:
413: public function removeAction()
414: {
415: $id = (int) $this->getRequest()->getParam('item');
416: $item = Mage::getModel('wishlist/item')->load($id);
417: if (!$item->getId()) {
418: return $this->norouteAction();
419: }
420: $wishlist = $this->_getWishlist($item->getWishlistId());
421: if (!$wishlist) {
422: return $this->norouteAction();
423: }
424: try {
425: $item->delete();
426: $wishlist->save();
427: } catch (Mage_Core_Exception $e) {
428: Mage::getSingleton('customer/session')->addError(
429: $this->__('An error occurred while deleting the item from wishlist: %s', $e->getMessage())
430: );
431: } catch(Exception $e) {
432: Mage::getSingleton('customer/session')->addError(
433: $this->__('An error occurred while deleting the item from wishlist.')
434: );
435: }
436:
437: Mage::helper('wishlist')->calculate();
438:
439: $this->_redirectReferer(Mage::getUrl('*/*'));
440: }
441:
442: 443: 444: 445: 446: 447:
448: public function cartAction()
449: {
450: $itemId = (int) $this->getRequest()->getParam('item');
451:
452:
453: $item = Mage::getModel('wishlist/item')->load($itemId);
454: if (!$item->getId()) {
455: return $this->_redirect('*/*');
456: }
457: $wishlist = $this->_getWishlist($item->getWishlistId());
458: if (!$wishlist) {
459: return $this->_redirect('*/*');
460: }
461:
462:
463: $qty = $this->getRequest()->getParam('qty');
464: if (is_array($qty)) {
465: if (isset($qty[$itemId])) {
466: $qty = $qty[$itemId];
467: } else {
468: $qty = 1;
469: }
470: }
471: $qty = $this->_processLocalizedQty($qty);
472: if ($qty) {
473: $item->setQty($qty);
474: }
475:
476:
477: $session = Mage::getSingleton('wishlist/session');
478: $cart = Mage::getSingleton('checkout/cart');
479:
480: $redirectUrl = Mage::getUrl('*/*');
481:
482: try {
483: $options = Mage::getModel('wishlist/item_option')->getCollection()
484: ->addItemFilter(array($itemId));
485: $item->setOptions($options->getOptionsByItem($itemId));
486:
487: $buyRequest = Mage::helper('catalog/product')->addParamsToBuyRequest(
488: $this->getRequest()->getParams(),
489: array('current_config' => $item->getBuyRequest())
490: );
491:
492: $item->mergeBuyRequest($buyRequest);
493: $item->addToCart($cart, true);
494: $cart->save()->getQuote()->collectTotals();
495: $wishlist->save();
496:
497: Mage::helper('wishlist')->calculate();
498:
499: if (Mage::helper('checkout/cart')->getShouldRedirectToCart()) {
500: $redirectUrl = Mage::helper('checkout/cart')->getCartUrl();
501: } else if ($this->_getRefererUrl()) {
502: $redirectUrl = $this->_getRefererUrl();
503: }
504: Mage::helper('wishlist')->calculate();
505: } catch (Mage_Core_Exception $e) {
506: if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_NOT_SALABLE) {
507: $session->addError(Mage::helper('wishlist')->__('This product(s) is currently out of stock'));
508: } else if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS) {
509: Mage::getSingleton('catalog/session')->addNotice($e->getMessage());
510: $redirectUrl = Mage::getUrl('*/*/configure/', array('id' => $item->getId()));
511: } else {
512: Mage::getSingleton('catalog/session')->addNotice($e->getMessage());
513: $redirectUrl = Mage::getUrl('*/*/configure/', array('id' => $item->getId()));
514: }
515: } catch (Exception $e) {
516: $session->addException($e, Mage::helper('wishlist')->__('Cannot add item to shopping cart'));
517: }
518:
519: Mage::helper('wishlist')->calculate();
520:
521: return $this->_redirectUrl($redirectUrl);
522: }
523:
524: 525: 526:
527: public function fromcartAction()
528: {
529: $wishlist = $this->_getWishlist();
530: if (!$wishlist) {
531: return $this->norouteAction();
532: }
533: $itemId = (int) $this->getRequest()->getParam('item');
534:
535:
536: $cart = Mage::getSingleton('checkout/cart');
537: $session = Mage::getSingleton('checkout/session');
538:
539: try{
540: $item = $cart->getQuote()->getItemById($itemId);
541: if (!$item) {
542: Mage::throwException(
543: Mage::helper('wishlist')->__("Requested cart item doesn't exist")
544: );
545: }
546:
547: $productId = $item->getProductId();
548: $buyRequest = $item->getBuyRequest();
549:
550: $wishlist->addNewItem($productId, $buyRequest);
551:
552: $productIds[] = $productId;
553: $cart->getQuote()->removeItem($itemId);
554: $cart->save();
555: Mage::helper('wishlist')->calculate();
556: $productName = Mage::helper('core')->escapeHtml($item->getProduct()->getName());
557: $wishlistName = Mage::helper('core')->escapeHtml($wishlist->getName());
558: $session->addSuccess(
559: Mage::helper('wishlist')->__("%s has been moved to wishlist %s", $productName, $wishlistName)
560: );
561: $wishlist->save();
562: } catch (Mage_Core_Exception $e) {
563: $session->addError($e->getMessage());
564: } catch (Exception $e) {
565: $session->addException($e, Mage::helper('wishlist')->__('Cannot move item to wishlist'));
566: }
567:
568: return $this->_redirectUrl(Mage::helper('checkout/cart')->getCartUrl());
569: }
570:
571: 572: 573:
574: public function shareAction()
575: {
576: $this->_getWishlist();
577: $this->loadLayout();
578: $this->_initLayoutMessages('customer/session');
579: $this->_initLayoutMessages('wishlist/session');
580: $this->renderLayout();
581: }
582:
583: 584: 585: 586: 587:
588: public function sendAction()
589: {
590: if (!$this->_validateFormKey()) {
591: return $this->_redirect('*/*/');
592: }
593:
594: $wishlist = $this->_getWishlist();
595: if (!$wishlist) {
596: return $this->norouteAction();
597: }
598:
599: $emails = explode(',', $this->getRequest()->getPost('emails'));
600: $message = nl2br(htmlspecialchars((string) $this->getRequest()->getPost('message')));
601: $error = false;
602: if (empty($emails)) {
603: $error = $this->__('Email address can\'t be empty.');
604: }
605: else {
606: foreach ($emails as $index => $email) {
607: $email = trim($email);
608: if (!Zend_Validate::is($email, 'EmailAddress')) {
609: $error = $this->__('Please input a valid email address.');
610: break;
611: }
612: $emails[$index] = $email;
613: }
614: }
615: if ($error) {
616: Mage::getSingleton('wishlist/session')->addError($error);
617: Mage::getSingleton('wishlist/session')->setSharingForm($this->getRequest()->getPost());
618: $this->_redirect('*/*/share');
619: return;
620: }
621:
622: $translate = Mage::getSingleton('core/translate');
623:
624: $translate->setTranslateInline(false);
625:
626: try {
627: $customer = Mage::getSingleton('customer/session')->getCustomer();
628:
629:
630: if ($this->getRequest()->getParam('rss_url')) {
631: $rss_url = $this->getLayout()
632: ->createBlock('wishlist/share_email_rss')
633: ->setWishlistId($wishlist->getId())
634: ->toHtml();
635: $message .=$rss_url;
636: }
637: $wishlistBlock = $this->getLayout()->createBlock('wishlist/share_email_items')->toHtml();
638:
639: $emails = array_unique($emails);
640:
641: $emailModel = Mage::getModel('core/email_template');
642:
643: $sharingCode = $wishlist->getSharingCode();
644: foreach($emails as $email) {
645: $emailModel->sendTransactional(
646: Mage::getStoreConfig('wishlist/email/email_template'),
647: Mage::getStoreConfig('wishlist/email/email_identity'),
648: $email,
649: null,
650: array(
651: 'customer' => $customer,
652: 'salable' => $wishlist->isSalable() ? 'yes' : '',
653: 'items' => $wishlistBlock,
654: 'addAllLink' => Mage::getUrl('*/shared/allcart', array('code' => $sharingCode)),
655: 'viewOnSiteLink'=> Mage::getUrl('*/shared/index', array('code' => $sharingCode)),
656: 'message' => $message
657: )
658: );
659: }
660:
661: $wishlist->setShared(1);
662: $wishlist->save();
663:
664: $translate->setTranslateInline(true);
665:
666: Mage::dispatchEvent('wishlist_share', array('wishlist'=>$wishlist));
667: Mage::getSingleton('customer/session')->addSuccess(
668: $this->__('Your Wishlist has been shared.')
669: );
670: $this->_redirect('*/*', array('wishlist_id' => $wishlist->getId()));
671: }
672: catch (Exception $e) {
673: $translate->setTranslateInline(true);
674:
675: Mage::getSingleton('wishlist/session')->addError($e->getMessage());
676: Mage::getSingleton('wishlist/session')->setSharingForm($this->getRequest()->getPost());
677: $this->_redirect('*/*/share');
678: }
679: }
680:
681: 682: 683: 684:
685: public function downloadCustomOptionAction()
686: {
687: $option = Mage::getModel('wishlist/item_option')->load($this->getRequest()->getParam('id'));
688:
689: if (!$option->getId()) {
690: return $this->_forward('noRoute');
691: }
692:
693: $optionId = null;
694: if (strpos($option->getCode(), Mage_Catalog_Model_Product_Type_Abstract::OPTION_PREFIX) === 0) {
695: $optionId = str_replace(Mage_Catalog_Model_Product_Type_Abstract::OPTION_PREFIX, '', $option->getCode());
696: if ((int)$optionId != $optionId) {
697: return $this->_forward('noRoute');
698: }
699: }
700: $productOption = Mage::getModel('catalog/product_option')->load($optionId);
701:
702: if (!$productOption
703: || !$productOption->getId()
704: || $productOption->getProductId() != $option->getProductId()
705: || $productOption->getType() != 'file'
706: ) {
707: return $this->_forward('noRoute');
708: }
709:
710: try {
711: $info = unserialize($option->getValue());
712: $filePath = Mage::getBaseDir() . $info['quote_path'];
713: $secretKey = $this->getRequest()->getParam('key');
714:
715: if ($secretKey == $info['secret_key']) {
716: $this->_prepareDownloadResponse($info['title'], array(
717: 'value' => $filePath,
718: 'type' => 'filename'
719: ));
720: }
721:
722: } catch(Exception $e) {
723: $this->_forward('noRoute');
724: }
725: exit(0);
726: }
727: }
728: