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_IndexController extends Mage_Core_Controller_Front_Action
00036 {
00037
00038
00039
00040
00041
00042 protected $_cookieCheckActions = array('add');
00043
00044 public function preDispatch()
00045 {
00046 parent::preDispatch();
00047
00048 if (!Mage::getSingleton('customer/session')->authenticate($this)) {
00049 $this->setFlag('', 'no-dispatch', true);
00050 if(!Mage::getSingleton('customer/session')->getBeforeWishlistUrl()) {
00051 Mage::getSingleton('customer/session')->setBeforeWishlistUrl($this->_getRefererUrl());
00052 }
00053 }
00054 if (!Mage::getStoreConfigFlag('wishlist/general/active')) {
00055 $this->norouteAction();
00056 return;
00057 }
00058 }
00059
00060
00061
00062
00063
00064
00065 protected function _getWishlist()
00066 {
00067 try {
00068 $wishlist = Mage::getModel('wishlist/wishlist')
00069 ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomer(), true);
00070 Mage::register('wishlist', $wishlist);
00071 }
00072 catch (Exception $e) {
00073 Mage::getSingleton('wishlist/session')->addError($this->__('Cannot create wishlist'));
00074 return false;
00075 }
00076 return $wishlist;
00077 }
00078
00079
00080
00081
00082 public function indexAction()
00083 {
00084 $this->_getWishlist();
00085 $this->loadLayout();
00086 $this->_initLayoutMessages('customer/session');
00087 $this->_initLayoutMessages('catalog/session');
00088
00089 if ($block = $this->getLayout()->getBlock('customer.wishlist')) {
00090 $block->setRefererUrl($this->_getRefererUrl());
00091 }
00092 $this->_initLayoutMessages('customer/session');
00093 $this->_initLayoutMessages('checkout/session');
00094 $this->renderLayout();
00095 }
00096
00097
00098
00099
00100 public function addAction()
00101 {
00102 $session = Mage::getSingleton('customer/session');
00103 $wishlist = $this->_getWishlist();
00104 if (!$wishlist) {
00105 $this->_redirect('*/');
00106 return;
00107 }
00108
00109 $productId = (int) $this->getRequest()->getParam('product');
00110 if (!$productId) {
00111 $this->_redirect('*/');
00112 return;
00113 }
00114
00115 $product = Mage::getModel('catalog/product')->load($productId);
00116 if (!$product->getId() || !$product->isVisibleInCatalog()) {
00117 $session->addError($this->__('Cannot specify product'));
00118 $this->_redirect('*/');
00119 return;
00120 }
00121
00122 try {
00123 $wishlist->addNewItem($product->getId());
00124 Mage::dispatchEvent('wishlist_add_product', array('wishlist'=>$wishlist, 'product'=>$product));
00125
00126 if ($referer = $session->getBeforeWishlistUrl()) {
00127 $session->setBeforeWishlistUrl(null);
00128 }
00129 else {
00130 $referer = $this->_getRefererUrl();
00131 }
00132 $message = $this->__('%1$s was successfully added to your wishlist. Click <a href="%2$s">here</a> to continue shopping', $product->getName(), $referer);
00133 $session->addSuccess($message);
00134 }
00135 catch (Mage_Core_Exception $e) {
00136 $session->addError($this->__('There was an error while adding item to wishlist: %s', $e->getMessage()));
00137 }
00138 catch (Exception $e) {
00139 $session->addError($this->__('There was an error while adding item to wishlist.'));
00140 }
00141 $this->_redirect('*');
00142 }
00143
00144
00145
00146
00147 public function updateAction()
00148 {
00149 if (!$this->_validateFormKey()) {
00150 return $this->_redirect('*/*/');
00151 }
00152 $post = $this->getRequest()->getPost();
00153 if($post && isset($post['description']) && is_array($post['description'])) {
00154 $wishlist = $this->_getWishlist();
00155
00156 foreach ($post['description'] as $itemId => $description) {
00157 $item = Mage::getModel('wishlist/item')->load($itemId);
00158 $description = (string) $description;
00159 if(!strlen($description) || $item->getWishlistId()!=$wishlist->getId()) {
00160 continue;
00161 }
00162 try {
00163 $item->setDescription($description)
00164 ->save();
00165 }
00166 catch (Exception $e) {
00167 Mage::getSingleton('customer/session')->addError(
00168 $this->__('Can\'t save description %s', Mage::helper('core')->htmlEscape($description))
00169 );
00170 }
00171 }
00172 }
00173 $this->_redirect('*');
00174 }
00175
00176
00177
00178
00179 public function removeAction()
00180 {
00181 $wishlist = $this->_getWishlist();
00182 $id = (int) $this->getRequest()->getParam('item');
00183 $item = Mage::getModel('wishlist/item')->load($id);
00184
00185 if($item->getWishlistId()==$wishlist->getId()) {
00186 try {
00187 $item->delete();
00188 }
00189 catch (Mage_Core_Exception $e) {
00190 Mage::getSingleton('customer/session')->addError(
00191 $this->__('There was an error while deleting item from wishlist: %s', $e->getMessage())
00192 );
00193 }
00194 catch(Exception $e) {
00195 Mage::getSingleton('customer/session')->addError(
00196 $this->__('There was an error while deleting item from wishlist.')
00197 );
00198 }
00199 }
00200 $this->_redirectReferer(Mage::getUrl('*/*'));
00201 }
00202
00203
00204
00205
00206 public function cartAction()
00207 {
00208 $wishlist = $this->_getWishlist();
00209 $id = (int) $this->getRequest()->getParam('item');
00210 $item = Mage::getModel('wishlist/item')->load($id);
00211
00212 if($item->getWishlistId()==$wishlist->getId()) {
00213 try {
00214 $product = Mage::getModel('catalog/product')->load($item->getProductId())->setQty(1);
00215 $quote = Mage::getSingleton('checkout/cart')
00216 ->addProduct($product)
00217 ->save();
00218 $item->delete();
00219 }
00220 catch(Exception $e) {
00221 Mage::getSingleton('checkout/session')->addError($e->getMessage());
00222 $url = Mage::getSingleton('checkout/session')->getRedirectUrl(true);
00223 if ($url) {
00224 $url = Mage::getModel('core/url')->getUrl('catalog/product/view', array(
00225 'id'=>$item->getProductId(),
00226 'wishlist_next'=>1
00227 ));
00228 Mage::getSingleton('checkout/session')->setSingleWishlistId($item->getId());
00229 $this->getResponse()->setRedirect($url);
00230 }
00231 else {
00232 $this->_redirect('*/*/');
00233 }
00234 return;
00235 }
00236 }
00237
00238 if (Mage::getStoreConfig('checkout/cart/redirect_to_cart')) {
00239 $this->_redirect('checkout/cart');
00240 } else {
00241 if ($this->getRequest()->getParam(self::PARAM_NAME_BASE64_URL)) {
00242 $this->getResponse()->setRedirect(
00243 Mage::helper('core')->urlDecode($this->getRequest()->getParam(self::PARAM_NAME_BASE64_URL))
00244 );
00245 } else {
00246 $this->_redirect('*/*/');
00247 }
00248 }
00249 }
00250
00251
00252
00253
00254
00255 public function allcartAction() {
00256 $messages = array();
00257 $urls = array();
00258 $wishlistIds = array();
00259 $notSalableNames = array();
00260
00261 $wishlist = $this->_getWishlist();
00262 $wishlist->getItemCollection()->load();
00263
00264 foreach ($wishlist->getItemCollection() as $item) {
00265 try {
00266 $product = Mage::getModel('catalog/product')
00267 ->load($item->getProductId())
00268 ->setQty(1);
00269 if ($product->isSalable()) {
00270 Mage::getSingleton('checkout/cart')->addProduct($product);
00271 $item->delete();
00272 }
00273 else {
00274 $notSalableNames[] = $product->getName();
00275 }
00276 } catch(Exception $e) {
00277 $url = Mage::getSingleton('checkout/session')
00278 ->getRedirectUrl(true);
00279 if ($url) {
00280 $url = Mage::getModel('core/url')
00281 ->getUrl('catalog/product/view', array(
00282 'id' => $item->getProductId(),
00283 'wishlist_next' => 1
00284 ));
00285
00286 $urls[] = $url;
00287 $messages[] = $e->getMessage();
00288 $wishlistIds[] = $item->getId();
00289 } else {
00290 $item->delete();
00291 }
00292 }
00293 Mage::getSingleton('checkout/cart')->save();
00294 }
00295
00296 if (count($notSalableNames) > 0) {
00297 Mage::getSingleton('checkout/session')
00298 ->addNotice($this->__('This product(s) is currently out of stock:'));
00299 array_map(array(Mage::getSingleton('checkout/session'), 'addNotice'), $notSalableNames);
00300 }
00301
00302 if ($urls) {
00303 Mage::getSingleton('checkout/session')->addError(array_shift($messages));
00304 $this->getResponse()->setRedirect(array_shift($urls));
00305
00306 Mage::getSingleton('checkout/session')->setWishlistPendingUrls($urls);
00307 Mage::getSingleton('checkout/session')->setWishlistPendingMessages($messages);
00308 Mage::getSingleton('checkout/session')->setWishlistIds($wishlistIds);
00309 }
00310 else {
00311 $this->_redirect('checkout/cart');
00312 }
00313 }
00314
00315 public function shareAction()
00316 {
00317 $this->loadLayout();
00318 $this->_initLayoutMessages('customer/session');
00319 $this->_initLayoutMessages('wishlist/session');
00320 $this->renderLayout();
00321 }
00322
00323 public function sendAction()
00324 {
00325 if (!$this->_validateFormKey()) {
00326 return $this->_redirect('*/*/');
00327 }
00328
00329 $emails = explode(',', $this->getRequest()->getPost('emails'));
00330 $message= nl2br(htmlspecialchars((string) $this->getRequest()->getPost('message')));
00331 $error = false;
00332 if (empty($emails)) {
00333 $error = $this->__('Email address can\'t be empty.');
00334 }
00335 else {
00336 foreach ($emails as $index => $email) {
00337 $email = trim($email);
00338 if (!Zend_Validate::is($email, 'EmailAddress')) {
00339 $error = $this->__('You input not valid email address.');
00340 break;
00341 }
00342 $emails[$index] = $email;
00343 }
00344 }
00345 if ($error) {
00346 Mage::getSingleton('wishlist/session')->addError($error);
00347 Mage::getSingleton('wishlist/session')->setSharingForm($this->getRequest()->getPost());
00348 $this->_redirect('*/*/share');
00349 return;
00350 }
00351
00352 $translate = Mage::getSingleton('core/translate');
00353
00354 $translate->setTranslateInline(false);
00355
00356 try {
00357 $customer = Mage::getSingleton('customer/session')->getCustomer();
00358 $wishlist = $this->_getWishlist();
00359
00360
00361 if ($this->getRequest()->getParam('rss_url')) {
00362 $rss_url = $this->getLayout()->createBlock('wishlist/share_email_rss')->toHtml();
00363 $message .=$rss_url;
00364 }
00365 $wishlistBlock = $this->getLayout()->createBlock('wishlist/share_email_items')->toHtml();
00366
00367 $emails = array_unique($emails);
00368 $emailModel = Mage::getModel('core/email_template');
00369
00370 foreach($emails as $email) {
00371 $emailModel->sendTransactional(
00372 Mage::getStoreConfig('wishlist/email/email_template'),
00373 Mage::getStoreConfig('wishlist/email/email_identity'),
00374 $email,
00375 null,
00376 array(
00377 'customer' => $customer,
00378 'salable' => $wishlist->isSalable() ? 'yes' : '',
00379 'items' => &$wishlistBlock,
00380 'addAllLink' => Mage::getUrl('*/shared/allcart',array('code'=>$wishlist->getSharingCode())),
00381 'viewOnSiteLink'=> Mage::getUrl('*/shared/index',array('code'=>$wishlist->getSharingCode())),
00382 'message' => $message
00383 ));
00384 }
00385
00386 $wishlist->setShared(1);
00387 $wishlist->save();
00388
00389 $translate->setTranslateInline(true);
00390
00391 Mage::dispatchEvent('wishlist_share', array('wishlist'=>$wishlist));
00392 Mage::getSingleton('customer/session')->addSuccess(
00393 $this->__('Your Wishlist was successfully shared')
00394 );
00395 $this->_redirect('*/*');
00396 }
00397 catch (Exception $e) {
00398 $translate->setTranslateInline(true);
00399
00400 Mage::getSingleton('wishlist/session')->addError($e->getMessage());
00401 Mage::getSingleton('wishlist/session')->setSharingForm($this->getRequest()->getPost());
00402 $this->_redirect('*/*/share');
00403 }
00404 }
00405 }