PageRenderTime 54ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/arush/desparation-deprecated
PHP | 326 lines | 217 code | 27 blank | 82 comment | 32 complexity | 33dd6014a9fc3de950d9fb7ac21bd79b 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) 2011 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. * @category Mage
  30. * @package Mage_XmlConnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_WishlistController extends Mage_XmlConnect_Controller_Action
  34. {
  35. /**
  36. * Check if customer is logged in
  37. *
  38. * @return null
  39. */
  40. public function preDispatch()
  41. {
  42. parent::preDispatch();
  43. if (!$this->_getCustomerSession()->isLoggedIn()) {
  44. $this->setFlag('', self::FLAG_NO_DISPATCH, true);
  45. $this->_message(
  46. $this->__('Customer not logged in.'), self::MESSAGE_STATUS_ERROR, array('logged_in' => '0')
  47. );
  48. return ;
  49. }
  50. }
  51. /**
  52. * Get customer session model
  53. *
  54. * @return Mage_Customer_Model_Session
  55. */
  56. protected function _getCustomerSession()
  57. {
  58. return Mage::getSingleton('customer/session');
  59. }
  60. /**
  61. * Retrieve wishlist object
  62. *
  63. * @return Mage_Wishlist_Model_Wishlist|false
  64. */
  65. protected function _getWishlist()
  66. {
  67. try {
  68. $wishlist = Mage::getModel('wishlist/wishlist')
  69. ->loadByCustomer($this->_getCustomerSession()->getCustomer(), true);
  70. Mage::register('wishlist', $wishlist);
  71. } catch (Mage_Core_Exception $e) {
  72. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  73. return false;
  74. } catch (Exception $e) {
  75. $this->_message($this->__('Can\'t create wishlist.'), self::MESSAGE_STATUS_ERROR);
  76. return false;
  77. }
  78. return $wishlist;
  79. }
  80. /**
  81. * Display customer wishlist
  82. *
  83. * @return null
  84. */
  85. public function indexAction()
  86. {
  87. $this->_getWishlist();
  88. try {
  89. $this->loadLayout(false);
  90. $this->renderLayout();
  91. } catch (Mage_Core_Exception $e) {
  92. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  93. } catch (Exception $e) {
  94. Mage::logException($e);
  95. $this->_message(
  96. $this->__('An error occurred while loading wishlist.'),
  97. self::MESSAGE_STATUS_ERROR
  98. );
  99. }
  100. }
  101. /**
  102. * Adding new item
  103. *
  104. * @return null
  105. */
  106. public function addAction()
  107. {
  108. $wishlist = $this->_getWishlist();
  109. if (!$wishlist) {
  110. return;
  111. }
  112. $request = $this->getRequest();
  113. $productId = (int)$request->getParam('product');
  114. if (!$productId) {
  115. $this->_message($this->__('Product was not specified.'), self::MESSAGE_STATUS_ERROR);
  116. return;
  117. }
  118. $product = Mage::getModel('catalog/product')->load($productId);
  119. if (!$product->getId() || !$product->isVisibleInCatalog()) {
  120. $this->_message($this->__('Can\'t specify product.'), self::MESSAGE_STATUS_ERROR);
  121. return;
  122. }
  123. try {
  124. $buyRequest = new Varien_Object($this->getRequest()->getParams());
  125. $result = $wishlist->addNewItem($product, $buyRequest);
  126. if (strlen(trim((string)$request->getParam('description')))) {
  127. $result->setDescription($request->getParam('description'))->save();
  128. }
  129. $wishlist->save();
  130. Mage::dispatchEvent('wishlist_add_product', array(
  131. 'wishlist' => $wishlist,
  132. 'product' => $product,
  133. 'item' => $result
  134. ));
  135. Mage::helper('wishlist')->calculate();
  136. $this->_message(
  137. $this->__('%1$s has been added to your wishlist.', $product->getName()),
  138. self::MESSAGE_STATUS_SUCCESS
  139. );
  140. } catch (Mage_Core_Exception $e) {
  141. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  142. } catch (Exception $e) {
  143. Mage::logException($e);
  144. $this->_message(
  145. $this->__('An error occurred while adding item to wishlist.'), self::MESSAGE_STATUS_ERROR
  146. );
  147. }
  148. }
  149. /**
  150. * Remove item
  151. *
  152. * @return null
  153. */
  154. public function removeAction()
  155. {
  156. $wishlist = $this->_getWishlist();
  157. $id = (int) $this->getRequest()->getParam('item');
  158. $item = Mage::getModel('wishlist/item')->load($id);
  159. if ($item->getWishlistId() == $wishlist->getId()) {
  160. try {
  161. $item->delete();
  162. $wishlist->save();
  163. $this->_message($this->__('Item has been removed from wishlist.'), self::MESSAGE_STATUS_SUCCESS);
  164. } catch (Mage_Core_Exception $e) {
  165. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  166. } catch(Exception $e) {
  167. $this->_message($this->__('An error occurred while removing item from wishlist.'), self::MESSAGE_STATUS_ERROR);
  168. }
  169. } else {
  170. $this->_message($this->__('Specified item does not exist in wishlist.'), self::MESSAGE_STATUS_ERROR);
  171. }
  172. Mage::helper('wishlist')->calculate();
  173. }
  174. /**
  175. * Clear wishlist action
  176. *
  177. * @return null
  178. */
  179. public function clearAction()
  180. {
  181. $wishlist = $this->_getWishlist();
  182. $items = $wishlist->getItemCollection();
  183. try {
  184. foreach ($items as $item) {
  185. $item->delete();
  186. }
  187. $wishlist->save();
  188. $this->_message($this->__('Wishlist has been cleared.'), self::MESSAGE_STATUS_SUCCESS);
  189. } catch (Mage_Core_Exception $e) {
  190. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  191. } catch(Exception $e) {
  192. $this->_message(
  193. $this->__('An error occurred while removing items from wishlist.'), self::MESSAGE_STATUS_ERROR
  194. );
  195. }
  196. Mage::helper('wishlist')->calculate();
  197. }
  198. /**
  199. * Update wishlist item comments
  200. *
  201. * @return null
  202. */
  203. public function updateAction()
  204. {
  205. $post = $this->getRequest()->getPost();
  206. if ($post && isset($post['description']) && is_array($post['description'])) {
  207. $wishlist = $this->_getWishlist();
  208. if (!$wishlist) {
  209. return;
  210. }
  211. $updatedItems = 0;
  212. $problemsFlag = false;
  213. foreach ($post['description'] as $itemId => $description) {
  214. /** @var $item Mage_Wishlist_Model_Item */
  215. $item = Mage::getModel('wishlist/item')->load($itemId);
  216. $description = (string) $description;
  217. if ($item->getWishlistId() != $wishlist->getId()) {
  218. continue;
  219. }
  220. try {
  221. $item->setDescription($description)->save();
  222. $updatedItems++;
  223. } catch (Exception $e) {
  224. $problemsFlag = true;
  225. }
  226. }
  227. // save wishlist model for setting date of last update
  228. if ($updatedItems) {
  229. try {
  230. $wishlist->save();
  231. if ($problemsFlag) {
  232. $message = $this->__('Wishlist has been updated. But there are accrued some errors while updating some items.');
  233. } else {
  234. $message = $this->__('Wishlist has been updated.');
  235. }
  236. $this->_message($message, self::MESSAGE_STATUS_SUCCESS);
  237. }
  238. catch (Exception $e) {
  239. $this->_message(
  240. $this->__('Items were updated. But can\'t update wishlist.'),
  241. self::MESSAGE_STATUS_SUCCESS
  242. );
  243. }
  244. } else {
  245. $this->_message($this->__('No items were updated.'), self::MESSAGE_STATUS_ERROR);
  246. }
  247. } else {
  248. $this->_message($this->__('No items were specifed to update.'), self::MESSAGE_STATUS_ERROR);
  249. }
  250. }
  251. /**
  252. * Add wishlist item to shopping cart and remove from wishlist
  253. *
  254. * If Product has required options - item removed from wishlist and redirect
  255. * to product view page with message about needed defined required options
  256. *
  257. * @return null
  258. */
  259. public function cartAction()
  260. {
  261. $wishlist = $this->_getWishlist();
  262. if (!$wishlist) {
  263. return;
  264. }
  265. $itemId = (int)$this->getRequest()->getParam('item');
  266. /* @var $item Mage_Wishlist_Model_Item */
  267. $item = Mage::getModel('wishlist/item')->load($itemId);
  268. if (!$item->getId() || $item->getWishlistId() != $wishlist->getId()) {
  269. $this->_message($this->__('Invalid item or wishlist.'), self::MESSAGE_STATUS_ERROR);
  270. return;
  271. }
  272. try {
  273. $cart = Mage::getSingleton('checkout/cart');
  274. $item->addToCart($cart, true);
  275. $cart->save()->getQuote()->collectTotals();
  276. $wishlist->save();
  277. Mage::helper('wishlist')->calculate();
  278. $this->_message($this->__('Item has been added to cart.'), self::MESSAGE_STATUS_SUCCESS);
  279. } catch (Mage_Core_Exception $e) {
  280. if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_NOT_SALABLE) {
  281. $this->_message($this->__('Product(s) currently out of stock.'), self::MESSAGE_STATUS_ERROR);
  282. } else if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS
  283. || $e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_IS_GROUPED_PRODUCT
  284. ) {
  285. $item->delete();
  286. $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
  287. $message->addChild('status', self::MESSAGE_STATUS_SUCCESS);
  288. $message->addChild('has_required_options', 1);
  289. $message->addChild('product_id', $item->getProductId());
  290. $this->getResponse()->setBody($message->asNiceXml());
  291. } else {
  292. $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
  293. }
  294. } catch (Exception $e) {
  295. $this->_message($this->__('Can\'t add item to shopping cart.'), self::MESSAGE_STATUS_ERROR);
  296. }
  297. Mage::helper('wishlist')->calculate();
  298. }
  299. }