Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. /** Solvingmagento_AffiliateProduct_RedirectController
  2. *
  3. * @category Solvingmagento
  4. * @package Solvingmagento_AffiliateProduct
  5. *
  6. * @author Oleg Ishenko <oleg.ishenko@solvingmagento.com>
  7. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  8. * @version Release: <package_version>
  9. * @link http://www.solvingmagento.com/
  10. */
  11. class Solvingmagento_AffiliateProduct_RedirectController extends Mage_Core_Controller_Front_Action
  12. {
  13. /** @var Solvingmagento_AffiliateProduct_Helper_Data */
  14. protected $helper;
  15.  
  16. /**
  17. * Protected construct method
  18. *
  19. * @return void
  20. */
  21. protected function _construct()
  22. {
  23. parent::_construct();
  24.  
  25. $this->helper = Mage::helper('solvingmagento_affiliateproduct');
  26. }
  27.  
  28. /**
  29. * Make sure the customer is authenticated of necessary
  30. *
  31. * @return Mage_Core_Controller_Front_Action | void
  32. */
  33. public function preDispatch()
  34. {
  35. parent::preDispatch();
  36.  
  37. if (!$this->getRequest()->isDispatched()) {
  38. return;
  39. }
  40.  
  41. $authenticationRequired = (bool) Mage::getStoreConfig(
  42. Solvingmagento_AffiliateProduct_Model_Product_Type::XML_PATH_AUTHENTICATION
  43. );
  44.  
  45. if ($authenticationRequired) {
  46. $customer = Mage::getSingleton('customer/session')->getCustomer();
  47. if ($customer && $customer->getId()) {
  48. $validationResult = $customer->validate();
  49. if ((true !== $validationResult) && is_array($validationResult)) {
  50. foreach ($validationResult as $error) {
  51. Mage::getSingleton('core/session')->addError($error);
  52. }
  53. $this->goBack();
  54. $this->setFlag('', self::FLAG_NO_DISPATCH, true);
  55.  
  56. return $this;
  57. }
  58. return $this;
  59. } else {
  60. Mage::getSingleton('customer/session')->addError(
  61. $this->helper->__('You must log in to access the partner product')
  62. );
  63. $this->_redirect('customer/account/login');
  64. $this->setFlag('', self::FLAG_NO_DISPATCH, true);
  65. return $this;
  66. }
  67. }
  68. }
  69.  
  70. /**
  71. * Redirect to partner link
  72. *
  73. * @return void
  74. */
  75. public function productAction()
  76. {
  77. $productId = (int) $this->getRequest()->getParam('id');
  78.  
  79. $product = Mage::getModel('catalog/product')->load($productId);
  80.  
  81. if (($product instanceof Mage_Catalog_Model_Product)
  82. && ($product->getTypeId() === Solvingmagento_AffiliateProduct_Model_Product_Type::TYPE_AFFILIATE)
  83. ) {
  84. if (!Zend_Uri::check($product->getAffiliateLink())) {
  85. Mage::getSingleton('core/session')->addError(
  86. $this->helper->__('The partner product is not accessible.')
  87. );
  88.  
  89. $this->goBack();
  90. return;
  91. }
  92.  
  93. $this->getResponse()->setRedirect($product->getAffiliateLink());
  94. return;
  95.  
  96. } else {
  97. Mage::getSingleton('core/session')->addError(
  98. $this->helper->__('Affiliate product not found')
  99. );
  100.  
  101. $this->goBack();
  102. return;
  103. }
  104. }
  105.  
  106. /**
  107. * Performs a redirect to a previously visited page
  108. *
  109. * @return Solvingmagento_AffiliateProduct_RedirectController
  110. */
  111. protected function goBack()
  112. {
  113. $returnUrl = $this->_getRefererUrl();
  114.  
  115. if ($returnUrl) {
  116. $this->getResponse()->setRedirect($returnUrl);
  117. } else {
  118. $this->_redirect('checkout/cart');
  119. }
  120.  
  121. return $this;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement