PageRenderTime 23ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/XmlConnect/controllers/WishlistController.php

https://github.com/mikhailxu/magento
PHP | 302 lines | 193 code | 29 blank | 80 comment | 31 complexity | 35d09da917068414c41617a71afa836b MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_XmlConnect
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * XmlConnect wishlist controller
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. class Mage_XmlConnect_WishlistController extends Mage_XmlConnect_Controller_Action
  32. {
  33. /**
  34. * Check if customer is logged in
  35. *
  36. * @return void
  37. */
  38. public function preDispatch()
  39. {
  40. parent::preDispatch();
  41. if (!$this->_getCustomerSession()->isLoggedIn()) {
  42. $this->setFlag('', self::FLAG_NO_DISPATCH, true);
  43. $this->_message($this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR);
  44. return ;
  45. }
  46. }
  47. /**
  48. * Get customer session model
  49. *
  50. * @return Mage_Customer_Model_Session
  51. */
  52. protected function _getCustomerSession()
  53. {
  54. return Mage::getSingleton('customer/session');
  55. }
  56. /**
  57. * Retrieve wishlist object
  58. *
  59. * @return Mage_Wishlist_Model_Wishlist|false
  60. */
  61. protected function _getWishlist()
  62. {
  63. try {
  64. $wishlist = Mage::getModel('wishlist/wishlist')
  65. ->loadByCustomer($this->_getCustomerSession()->getCustomer(), true);
  66. Mage::register('wishlist', $wishlist);
  67. } catch (Mage_Core_Exception $e) {
  68. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  69. return false;
  70. } catch (Exception $e) {
  71. $this->_message($this->__('Can\'t create wishlist.'), self::MESSAGE_STATUS_ERROR);
  72. return false;
  73. }
  74. return $wishlist;
  75. }
  76. /**
  77. * Display customer wishlist
  78. *
  79. * @return void
  80. */
  81. public function indexAction()
  82. {
  83. $this->_getWishlist();
  84. $this->loadLayout(false);
  85. $this->renderLayout();
  86. }
  87. /**
  88. * Adding new item
  89. *
  90. * @return void
  91. */
  92. public function addAction()
  93. {
  94. $session = $this->_getCustomerSession();
  95. $wishlist = $this->_getWishlist();
  96. if (!$wishlist) {
  97. return;
  98. }
  99. $request = $this->getRequest();
  100. $productId = (int)$request->getParam('product');
  101. if (!$productId) {
  102. $this->_message($this->__('Product was not specified.'), self::MESSAGE_STATUS_ERROR);
  103. return;
  104. }
  105. $product = Mage::getModel('catalog/product')->load($productId);
  106. if (!$product->getId() || !$product->isVisibleInCatalog()) {
  107. $this->_message($this->__('Can\'t specify product.'), self::MESSAGE_STATUS_ERROR);
  108. return;
  109. }
  110. try {
  111. $item = $wishlist->addNewItem($product->getId());
  112. if (strlen(trim((string)$request->getParam('description')))) {
  113. $item->setDescription($request->getParam('description'))
  114. ->save();
  115. }
  116. $wishlist->save();
  117. Mage::dispatchEvent('wishlist_add_product', array('wishlist'=>$wishlist, 'product'=>$product));
  118. Mage::helper('wishlist')->calculate();
  119. $message = $this->__('%1$s has been added to your wishlist.', $product->getName());
  120. $this->_message($message, self::MESSAGE_STATUS_SUCCESS);
  121. } catch (Mage_Core_Exception $e) {
  122. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  123. } catch (Exception $e) {
  124. $this->_message($this->__('An error occurred while adding item to wishlist.'), self::MESSAGE_STATUS_ERROR);
  125. }
  126. }
  127. /**
  128. * Remove item
  129. *
  130. * @return void
  131. */
  132. public function removeAction()
  133. {
  134. $wishlist = $this->_getWishlist();
  135. $id = (int) $this->getRequest()->getParam('item');
  136. $item = Mage::getModel('wishlist/item')->load($id);
  137. if ($item->getWishlistId() == $wishlist->getId()) {
  138. try {
  139. $item->delete();
  140. $wishlist->save();
  141. $this->_message($this->__('Item has been removed from wishlist.'), self::MESSAGE_STATUS_SUCCESS);
  142. } catch (Mage_Core_Exception $e) {
  143. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  144. } catch(Exception $e) {
  145. $this->_message($this->__('An error occurred while removing item from wishlist.'), self::MESSAGE_STATUS_ERROR);
  146. }
  147. } else {
  148. $this->_message($this->__('Specified item does not exist in wishlist.'), self::MESSAGE_STATUS_ERROR);
  149. }
  150. Mage::helper('wishlist')->calculate();
  151. }
  152. /**
  153. * Clear wishlist action
  154. *
  155. * @return void
  156. */
  157. public function clearAction()
  158. {
  159. $wishlist = $this->_getWishlist();
  160. $items = $wishlist->getItemCollection();
  161. try {
  162. foreach ($items as $item) {
  163. $item->delete();
  164. }
  165. $wishlist->save();
  166. $this->_message($this->__('Wishlist has been cleared.'), self::MESSAGE_STATUS_SUCCESS);
  167. } catch (Mage_Core_Exception $e) {
  168. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  169. } catch(Exception $e) {
  170. $this->_message($this->__('An error occurred while removing items from wishlist.'), self::MESSAGE_STATUS_ERROR);
  171. }
  172. Mage::helper('wishlist')->calculate();
  173. }
  174. /**
  175. * Update wishlist item comments
  176. *
  177. * @return void
  178. */
  179. public function updateAction()
  180. {
  181. $post = $this->getRequest()->getPost();
  182. if ($post && isset($post['description']) && is_array($post['description'])) {
  183. $wishlist = $this->_getWishlist();
  184. if (!$wishlist) {
  185. return;
  186. }
  187. $updatedItems = 0;
  188. $problemsFlag = false;
  189. foreach ($post['description'] as $itemId => $description) {
  190. $item = Mage::getModel('wishlist/item')->load($itemId);
  191. $description = (string) $description;
  192. if ($item->getWishlistId() != $wishlist->getId()) {
  193. continue;
  194. }
  195. try {
  196. $item->setDescription($description)
  197. ->save();
  198. $updatedItems++;
  199. } catch (Exception $e) {
  200. $problemsFlag = true;
  201. }
  202. }
  203. // save wishlist model for setting date of last update
  204. if ($updatedItems) {
  205. try {
  206. $wishlist->save();
  207. if ($problemsFlag) {
  208. $message = $this->__('Wishlist has been updated. But there are accrued some errors while updating some items.');
  209. } else {
  210. $message = $this->__('Wishlist has been updated.');
  211. }
  212. $this->_message($message, self::MESSAGE_STATUS_SUCCESS);
  213. }
  214. catch (Exception $e) {
  215. $this->_message($this->__('Items were updated. But can\'t update wishlist.'), self::MESSAGE_STATUS_SUCCESS);
  216. }
  217. } else {
  218. $this->_message($this->__('No items were updated.'), self::MESSAGE_STATUS_ERROR);
  219. }
  220. } else {
  221. $this->_message($this->__('No items were specifed to update.'), self::MESSAGE_STATUS_ERROR);
  222. }
  223. }
  224. /**
  225. * Add wishlist item to shopping cart and remove from wishlist
  226. *
  227. * If Product has required options - item removed from wishlist and redirect
  228. * to product view page with message about needed defined required options
  229. *
  230. * @return void
  231. */
  232. public function cartAction()
  233. {
  234. $wishlist = $this->_getWishlist();
  235. if (!$wishlist) {
  236. return;
  237. }
  238. $itemId = (int)$this->getRequest()->getParam('item');
  239. /* @var $item Mage_Wishlist_Model_Item */
  240. $item = Mage::getModel('wishlist/item')->load($itemId);
  241. if (!$item->getId() || $item->getWishlistId() != $wishlist->getId()) {
  242. $this->_message($this->__('Invalid item or wishlist.'), self::MESSAGE_STATUS_ERROR);
  243. return;
  244. }
  245. /* @var $session Mage_Wishlist_Model_Session */
  246. $session = Mage::getSingleton('wishlist/session');
  247. $cart = Mage::getSingleton('checkout/cart');
  248. try {
  249. $item->addToCart($cart, true);
  250. $cart->save()->getQuote()->collectTotals();
  251. $wishlist->save();
  252. Mage::helper('wishlist')->calculate();
  253. $this->_message($this->__('Item has been added to cart.'), self::MESSAGE_STATUS_SUCCESS);
  254. } catch (Mage_Core_Exception $e) {
  255. if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_NOT_SALABLE) {
  256. $this->_message($this->__('Product(s) currently out of stock.'), self::MESSAGE_STATUS_ERROR);
  257. } else if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS ||
  258. $e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_IS_GROUPED_PRODUCT) {
  259. $item->delete();
  260. $message = new Mage_XmlConnect_Model_Simplexml_Element('<message></message>');
  261. $message->addChild('status', self::MESSAGE_STATUS_SUCCESS);
  262. $message->addChild('has_required_options', 1);
  263. $message->addChild('product_id', $item->getProductId());
  264. $this->getResponse()->setBody($message->asNiceXml());
  265. } else {
  266. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  267. }
  268. } catch (Exception $e) {
  269. $this->_message($this->__('Can\'t add item to shopping cart.'), self::MESSAGE_STATUS_ERROR);
  270. }
  271. Mage::helper('wishlist')->calculate();
  272. }
  273. }