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_Sales
23: * @copyright Copyright (c) 2012 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: /**
28: * Recurring profiles view/management controller
29: */
30: class Mage_Sales_Recurring_ProfileController extends Mage_Core_Controller_Front_Action
31: {
32: /**
33: *
34: * @var Mage_Customer_Model_Session
35: */
36: protected $_session = null;
37:
38: /**
39: * Make sure customer is logged in and put it into registry
40: */
41: public function preDispatch()
42: {
43: parent::preDispatch();
44: if (!$this->getRequest()->isDispatched()) {
45: return;
46: }
47: $this->_session = Mage::getSingleton('customer/session');
48: if (!$this->_session->authenticate($this)) {
49: $this->setFlag('', 'no-dispatch', true);
50: }
51: Mage::register('current_customer', $this->_session->getCustomer());
52: }
53:
54: /**
55: * Profiles listing
56: */
57: public function indexAction()
58: {
59: $this->_title($this->__('Recurring Profiles'));
60: $this->loadLayout();
61: $this->_initLayoutMessages('customer/session');
62: $this->renderLayout();
63: }
64:
65: /**
66: * Profile main view
67: */
68: public function viewAction()
69: {
70: $this->_viewAction();
71: }
72:
73: /**
74: * Profile history view
75: */
76: // TODO: implement
77: // public function historyAction()
78: // {
79: // $this->_viewAction();
80: // }
81:
82: /**
83: * Profile related orders view
84: */
85: public function ordersAction()
86: {
87: $this->_viewAction();
88: }
89:
90: /**
91: * Profile payment gateway info view
92: */
93: // TODO: implement
94: // public function vendorAction()
95: // {
96: // $this->_viewAction();
97: // }
98:
99: /**
100: * Attempt to set profile state
101: */
102: public function updateStateAction()
103: {
104: $profile = null;
105: try {
106: $profile = $this->_initProfile();
107:
108: switch ($this->getRequest()->getParam('action')) {
109: case 'cancel':
110: $profile->cancel();
111: break;
112: case 'suspend':
113: $profile->suspend();
114: break;
115: case 'activate':
116: $profile->activate();
117: break;
118: }
119: $this->_session->addSuccess($this->__('The profile state has been updated.'));
120: } catch (Mage_Core_Exception $e) {
121: $this->_session->addError($e->getMessage());
122: } catch (Exception $e) {
123: $this->_session->addError($this->__('Failed to update the profile.'));
124: Mage::logException($e);
125: }
126: if ($profile) {
127: $this->_redirect('*/*/view', array('profile' => $profile->getId()));
128: } else {
129: $this->_redirect('*/*/');
130: }
131: }
132:
133: /**
134: * Fetch an update with profile
135: */
136: public function updateProfileAction()
137: {
138: $profile = null;
139: try {
140: $profile = $this->_initProfile();
141: $profile->fetchUpdate();
142: if ($profile->hasDataChanges()) {
143: $profile->save();
144: $this->_session->addSuccess($this->__('The profile has been updated.'));
145: } else {
146: $this->_session->addNotice($this->__('The profile has no changes.'));
147: }
148: } catch (Mage_Core_Exception $e) {
149: $this->_session->addError($e->getMessage());
150: } catch (Exception $e) {
151: $this->_session->addError($this->__('Failed to update the profile.'));
152: Mage::logException($e);
153: }
154: if ($profile) {
155: $this->_redirect('*/*/view', array('profile' => $profile->getId()));
156: } else {
157: $this->_redirect('*/*/');
158: }
159: }
160:
161: /**
162: * Generic profile view action
163: */
164: protected function _viewAction()
165: {
166: try {
167: $profile = $this->_initProfile();
168: $this->_title($this->__('Recurring Profiles'))->_title($this->__('Profile #%s', $profile->getReferenceId()));
169: $this->loadLayout();
170: $this->_initLayoutMessages('customer/session');
171: $navigationBlock = $this->getLayout()->getBlock('customer_account_navigation');
172: if ($navigationBlock) {
173: $navigationBlock->setActive('sales/recurring_profile/');
174: }
175: $this->renderLayout();
176: return;
177: } catch (Mage_Core_Exception $e) {
178: $this->_session->addError($e->getMessage());
179: } catch (Exception $e) {
180: Mage::logException($e);
181: }
182: $this->_redirect('*/*/');
183: }
184:
185: /**
186: * Instantiate current profile and put it into registry
187: *
188: * @return Mage_Sales_Model_Recurring_Profile
189: * @throws Mage_Core_Exception
190: */
191: protected function _initProfile()
192: {
193: $profile = Mage::getModel('sales/recurring_profile')->load($this->getRequest()->getParam('profile'));
194: if (!$profile->getId()) {
195: Mage::throwException($this->__('Specified profile does not exist.'));
196: }
197: Mage::register('current_recurring_profile', $profile);
198: return $profile;
199: }
200: }
201: