PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Wishlist/controllers/IndexController.php

https://github.com/leochaves/magento-pt_br
PHP | 387 lines | 298 code | 38 blank | 51 comment | 37 complexity | d89b8b1aaeb6ca621bb4a70b10cb9cde MD5 | raw file
Possible License(s): GPL-2.0
  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_Wishlist
  23. * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Wishlist front controller
  28. *
  29. * @category Mage
  30. * @package Mage_Wishlist
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Wishlist_IndexController extends Mage_Core_Controller_Front_Action
  34. {
  35. public function preDispatch()
  36. {
  37. parent::preDispatch();
  38. if (!Mage::getSingleton('customer/session')->authenticate($this)) {
  39. $this->setFlag('', 'no-dispatch', true);
  40. if(!Mage::getSingleton('customer/session')->getBeforeWishlistUrl()) {
  41. Mage::getSingleton('customer/session')->setBeforeWishlistUrl($this->_getRefererUrl());
  42. }
  43. }
  44. if (!Mage::getStoreConfigFlag('wishlist/general/active')) {
  45. $this->norouteAction();
  46. return;
  47. }
  48. }
  49. protected function _getWishlist()
  50. {
  51. try {
  52. $wishlist = Mage::getModel('wishlist/wishlist')
  53. ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomer(), true);
  54. Mage::register('wishlist', $wishlist);
  55. }
  56. catch (Exception $e) {
  57. Mage::getSingleton('wishlist/session')->addError($this->__('Cannot create wishlist'));
  58. return false;
  59. }
  60. return $wishlist;
  61. }
  62. /**
  63. * Display customer wishlist
  64. */
  65. public function indexAction()
  66. {
  67. $this->_getWishlist();
  68. $this->loadLayout();
  69. $this->_initLayoutMessages('customer/session');
  70. $this->_initLayoutMessages('catalog/session');
  71. if ($block = $this->getLayout()->getBlock('customer.wishlist')) {
  72. $block->setRefererUrl($this->_getRefererUrl());
  73. }
  74. $this->_initLayoutMessages('customer/session');
  75. $this->_initLayoutMessages('checkout/session');
  76. $this->renderLayout();
  77. }
  78. /**
  79. * Adding new item
  80. */
  81. public function addAction()
  82. {
  83. $session = Mage::getSingleton('customer/session');
  84. $wishlist = $this->_getWishlist();
  85. if (!$wishlist) {
  86. $this->_redirect('*/');
  87. return;
  88. }
  89. $productId = (int) $this->getRequest()->getParam('product');
  90. if (!$productId) {
  91. $this->_redirect('*/');
  92. return;
  93. }
  94. $product = Mage::getModel('catalog/product')->load($productId);
  95. if (!$product->getId() || !$product->isVisibleInCatalog()) {
  96. $session->addError($this->__('Cannot specify product'));
  97. $this->_redirect('*/');
  98. return;
  99. }
  100. try {
  101. $wishlist->addNewItem($product->getId());
  102. Mage::dispatchEvent('wishlist_add_product', array('wishlist'=>$wishlist, 'product'=>$product));
  103. if ($referer = $session->getBeforeWishlistUrl()) {
  104. $session->setBeforeWishlistUrl(null);
  105. }
  106. else {
  107. $referer = $this->_getRefererUrl();
  108. }
  109. $message = $this->__('%1$s was successfully added to your wishlist. Click <a href="%2$s">here</a> to continue shopping', $product->getName(), $referer);
  110. $session->addSuccess($message);
  111. }
  112. catch (Mage_Core_Exception $e) {
  113. $session->addError($this->__('There was an error while adding item to wishlist: %s', $e->getMessage()));
  114. }
  115. catch (Exception $e) {
  116. $session->addError($this->__('There was an error while adding item to wishlist.'));
  117. }
  118. $this->_redirect('*');
  119. }
  120. /**
  121. * Update wishlist item comments
  122. */
  123. public function updateAction()
  124. {
  125. if (!$this->_validateFormKey()) {
  126. return $this->_redirect('*/*/');
  127. }
  128. $post = $this->getRequest()->getPost();
  129. if($post && isset($post['description']) && is_array($post['description'])) {
  130. $wishlist = $this->_getWishlist();
  131. foreach ($post['description'] as $itemId => $description) {
  132. $item = Mage::getModel('wishlist/item')->load($itemId);
  133. $description = (string) $description;
  134. if(!strlen($description) || $item->getWishlistId()!=$wishlist->getId()) {
  135. continue;
  136. }
  137. try {
  138. $item->setDescription($description)
  139. ->save();
  140. }
  141. catch (Exception $e) {
  142. Mage::getSingleton('customer/session')->addError(
  143. $this->__('Can\'t save description %s', Mage::helper('core')->htmlEscape($description))
  144. );
  145. }
  146. }
  147. }
  148. $this->_redirect('*');
  149. }
  150. /**
  151. * Remove item
  152. */
  153. public function removeAction()
  154. {
  155. $wishlist = $this->_getWishlist();
  156. $id = (int) $this->getRequest()->getParam('item');
  157. $item = Mage::getModel('wishlist/item')->load($id);
  158. if($item->getWishlistId()==$wishlist->getId()) {
  159. try {
  160. $item->delete();
  161. }
  162. catch (Mage_Core_Exception $e) {
  163. Mage::getSingleton('customer/session')->addError(
  164. $this->__('There was an error while deleting item from wishlist: %s', $e->getMessage())
  165. );
  166. }
  167. catch(Exception $e) {
  168. Mage::getSingleton('customer/session')->addError(
  169. $this->__('There was an error while deleting item from wishlist.')
  170. );
  171. }
  172. }
  173. $this->_redirectReferer(Mage::getUrl('*/*'));
  174. }
  175. /**
  176. * Add wishlist item to shopping cart
  177. */
  178. public function cartAction()
  179. {
  180. $wishlist = $this->_getWishlist();
  181. $id = (int) $this->getRequest()->getParam('item');
  182. $item = Mage::getModel('wishlist/item')->load($id);
  183. if($item->getWishlistId()==$wishlist->getId()) {
  184. try {
  185. $product = Mage::getModel('catalog/product')->load($item->getProductId())->setQty(1);
  186. $quote = Mage::getSingleton('checkout/cart')
  187. ->addProduct($product)
  188. ->save();
  189. $item->delete();
  190. }
  191. catch(Exception $e) {
  192. Mage::getSingleton('checkout/session')->addError($e->getMessage());
  193. $url = Mage::getSingleton('checkout/session')->getRedirectUrl(true);
  194. if ($url) {
  195. $url = Mage::getModel('core/url')->getUrl('catalog/product/view', array(
  196. 'id'=>$item->getProductId(),
  197. 'wishlist_next'=>1
  198. ));
  199. Mage::getSingleton('checkout/session')->setSingleWishlistId($item->getId());
  200. $this->getResponse()->setRedirect($url);
  201. }
  202. else {
  203. $this->_redirect('*/*/');
  204. }
  205. return;
  206. }
  207. }
  208. if (Mage::getStoreConfig('checkout/cart/redirect_to_cart')) {
  209. $this->_redirect('checkout/cart');
  210. } else {
  211. if ($this->getRequest()->getParam(self::PARAM_NAME_BASE64_URL)) {
  212. $this->getResponse()->setRedirect(
  213. Mage::helper('core')->urlDecode($this->getRequest()->getParam(self::PARAM_NAME_BASE64_URL))
  214. );
  215. } else {
  216. $this->_redirect('*/*/');
  217. }
  218. }
  219. }
  220. /**
  221. * Add all items to shoping cart
  222. */
  223. public function allcartAction() {
  224. $messages = array();
  225. $urls = array();
  226. $wishlistIds = array();
  227. $wishlist = $this->_getWishlist();
  228. $wishlist->getItemCollection()->load();
  229. $notSalableNames = array(); // Out of stock products message
  230. foreach ($wishlist->getItemCollection() as $item) {
  231. try {
  232. $product = Mage::getModel('catalog/product')->load($item->getProductId())->setQty(1);
  233. if ($product->isSalable()){
  234. Mage::getSingleton('checkout/cart')->addProduct($product);
  235. $item->delete();
  236. } else {
  237. $notSalableNames[] = $product->getName();
  238. }
  239. } catch(Exception $e) {
  240. $url = Mage::getSingleton('checkout/session')->getRedirectUrl(true);
  241. if ($url){
  242. $url = Mage::getModel('core/url')->getUrl('catalog/product/view', array(
  243. 'id'=>$item->getProductId(),
  244. 'wishlist_next'=>1
  245. ));
  246. $urls[] = $url;
  247. $messages[] = $e->getMessage();
  248. $wishlistIds[] = $item->getId();
  249. } else {
  250. $item->delete();
  251. }
  252. }
  253. Mage::getSingleton('checkout/cart')->save();
  254. }
  255. if (count($notSalableNames) > 0) {
  256. Mage::getSingleton('checkout/session')
  257. ->addNotice($this->__('This product(s) is currently out of stock:'));
  258. array_map(array(Mage::getSingleton('checkout/session'), 'addNotice'), $notSalableNames);
  259. }
  260. if ($urls) {
  261. Mage::getSingleton('checkout/session')->addError(array_shift($messages));
  262. $this->getResponse()->setRedirect(array_shift($urls));
  263. Mage::getSingleton('checkout/session')->setWishlistPendingUrls($urls);
  264. Mage::getSingleton('checkout/session')->setWishlistPendingMessages($messages);
  265. Mage::getSingleton('checkout/session')->setWishlistIds($wishlistIds);
  266. } else {
  267. $this->_redirect('checkout/cart');
  268. }
  269. }
  270. public function shareAction()
  271. {
  272. $this->loadLayout();
  273. $this->_initLayoutMessages('customer/session');
  274. $this->_initLayoutMessages('wishlist/session');
  275. $this->renderLayout();
  276. }
  277. public function sendAction()
  278. {
  279. if (!$this->_validateFormKey()) {
  280. return $this->_redirect('*/*/');
  281. }
  282. $emails = explode(',', $this->getRequest()->getPost('emails'));
  283. $message= nl2br(htmlspecialchars((string) $this->getRequest()->getPost('message')));
  284. $error = false;
  285. if (empty($emails)) {
  286. $error = $this->__('Email address can\'t be empty.');
  287. }
  288. else {
  289. foreach ($emails as $index => $email) {
  290. $email = trim($email);
  291. if (!Zend_Validate::is($email, 'EmailAddress')) {
  292. $error = $this->__('You input not valid email address.');
  293. break;
  294. }
  295. $emails[$index] = $email;
  296. }
  297. }
  298. if ($error) {
  299. Mage::getSingleton('wishlist/session')->addError($error);
  300. Mage::getSingleton('wishlist/session')->setSharingForm($this->getRequest()->getPost());
  301. $this->_redirect('*/*/share');
  302. return;
  303. }
  304. $translate = Mage::getSingleton('core/translate');
  305. /* @var $translate Mage_Core_Model_Translate */
  306. $translate->setTranslateInline(false);
  307. try {
  308. $customer = Mage::getSingleton('customer/session')->getCustomer();
  309. $wishlist = $this->_getWishlist();
  310. /*if share rss added rss feed to email template*/
  311. if ($this->getRequest()->getParam('rss_url')) {
  312. $rss_url = $this->getLayout()->createBlock('wishlist/share_email_rss')->toHtml();
  313. $message .=$rss_url;
  314. }
  315. $wishlistBlock = $this->getLayout()->createBlock('wishlist/share_email_items')->toHtml();
  316. $emails = array_unique($emails);
  317. $emailModel = Mage::getModel('core/email_template');
  318. foreach($emails as $email) {
  319. $emailModel->sendTransactional(
  320. Mage::getStoreConfig('wishlist/email/email_template'),
  321. Mage::getStoreConfig('wishlist/email/email_identity'),
  322. $email,
  323. null,
  324. array(
  325. 'customer' => $customer,
  326. 'salable' => $wishlist->isSalable() ? 'yes' : '',
  327. 'items' => &$wishlistBlock,
  328. 'addAllLink' => Mage::getUrl('*/shared/allcart',array('code'=>$wishlist->getSharingCode())),
  329. 'viewOnSiteLink'=> Mage::getUrl('*/shared/index',array('code'=>$wishlist->getSharingCode())),
  330. 'message' => $message
  331. ));
  332. }
  333. $wishlist->setShared(1);
  334. $wishlist->save();
  335. $translate->setTranslateInline(true);
  336. Mage::dispatchEvent('wishlist_share', array('wishlist'=>$wishlist));
  337. Mage::getSingleton('customer/session')->addSuccess(
  338. $this->__('Your Wishlist was successfully shared')
  339. );
  340. $this->_redirect('*/*');
  341. }
  342. catch (Exception $e) {
  343. $translate->setTranslateInline(true);
  344. Mage::getSingleton('wishlist/session')->addError($e->getMessage());
  345. Mage::getSingleton('wishlist/session')->setSharingForm($this->getRequest()->getPost());
  346. $this->_redirect('*/*/share');
  347. }
  348. }
  349. }