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_Adminhtml
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: * TODO: implement ACL restrictions
31: */
32: class Mage_Adminhtml_Sales_Recurring_ProfileController extends Mage_Adminhtml_Controller_Action
33: {
34: /**
35: * Recurring profiles list
36: *
37: * @return void
38: */
39: public function indexAction()
40: {
41: $this->_title(Mage::helper('sales')->__('Sales'))->_title(Mage::helper('sales')->__('Recurring Profiles'))
42: ->loadLayout()
43: ->_setActiveMenu('sales/recurring_profile')
44: ->renderLayout();
45: return $this;
46: }
47:
48: /**
49: * View recurring profile detales
50: */
51: public function viewAction()
52: {
53: try {
54: $this->_title(Mage::helper('sales')->__('Sales'))->_title(Mage::helper('sales')->__('Recurring Profiles'));
55: $profile = $this->_initProfile();
56: $this->loadLayout()
57: ->_setActiveMenu('sales/recurring_profile')
58: ->_title(Mage::helper('sales')->__('Profile #%s', $profile->getReferenceId()))
59: ->renderLayout()
60: ;
61: return;
62: } catch (Mage_Core_Exception $e) {
63: $this->_getSession()->addError($e->getMessage());
64: } catch (Exception $e) {
65: Mage::logException($e);
66: }
67: $this->_redirect('*/*/');
68: }
69:
70: /**
71: * Profiles ajax grid
72: */
73: public function gridAction()
74: {
75: try {
76: $this->loadLayout()->renderLayout();
77: return;
78: } catch (Mage_Core_Exception $e) {
79: $this->_getSession()->addError($e->getMessage());
80: } catch (Exception $e) {
81: Mage::logException($e);
82: }
83: $this->_redirect('*/*/');
84: }
85:
86: /**
87: * Profile orders ajax grid
88: */
89: public function ordersAction()
90: {
91: try {
92: $this->_initProfile();
93: $this->loadLayout()->renderLayout();
94: } catch (Exception $e) {
95: Mage::logException($e);
96: $this->norouteAction();
97: }
98: }
99:
100: /**
101: * Profile state updater action
102: */
103: public function updateStateAction()
104: {
105: $profile = null;
106: try {
107: $profile = $this->_initProfile();
108:
109: switch ($this->getRequest()->getParam('action')) {
110: case 'cancel':
111: $profile->cancel();
112: break;
113: case 'suspend':
114: $profile->suspend();
115: break;
116: case 'activate':
117: $profile->activate();
118: break;
119: }
120: $this->_getSession()->addSuccess(Mage::helper('sales')->__('The profile state has been updated.'));
121: } catch (Mage_Core_Exception $e) {
122: $this->_getSession()->addError($e->getMessage());
123: } catch (Exception $e) {
124: $this->_getSession()->addError(Mage::helper('sales')->__('Failed to update the profile.'));
125: Mage::logException($e);
126: }
127: if ($profile) {
128: $this->_redirect('*/*/view', array('profile' => $profile->getId()));
129: } else {
130: $this->_redirect('*/*/');
131: }
132: }
133:
134: /**
135: * Profile information updater action
136: */
137: public function updateProfileAction()
138: {
139: $profile = null;
140: try {
141: $profile = $this->_initProfile();
142: $profile->fetchUpdate();
143: if ($profile->hasDataChanges()) {
144: $profile->save();
145: $this->_getSession()->addSuccess($this->__('The profile has been updated.'));
146: } else {
147: $this->_getSession()->addNotice($this->__('The profile has no changes.'));
148: }
149: } catch (Mage_Core_Exception $e) {
150: $this->_getSession()->addError($e->getMessage());
151: } catch (Exception $e) {
152: $this->_getSession()->addError($this->__('Failed to update the profile.'));
153: Mage::logException($e);
154: }
155: if ($profile) {
156: $this->_redirect('*/*/view', array('profile' => $profile->getId()));
157: } else {
158: $this->_redirect('*/*/');
159: }
160: }
161:
162: /**
163: * Cutomer billing agreements ajax action
164: *
165: */
166: public function customerGridAction()
167: {
168: $this->_initCustomer();
169: $this->loadLayout(false)
170: ->renderLayout();
171: }
172:
173: /**
174: * Initialize customer by ID specified in request
175: *
176: * @return Mage_Adminhtml_Sales_Billing_AgreementController
177: */
178: protected function _initCustomer()
179: {
180: $customerId = (int) $this->getRequest()->getParam('id');
181: $customer = Mage::getModel('customer/customer');
182:
183: if ($customerId) {
184: $customer->load($customerId);
185: }
186:
187: Mage::register('current_customer', $customer);
188: return $this;
189: }
190:
191: /**
192: * Load/set profile
193: *
194: * @return Mage_Sales_Model_Recurring_Profile
195: */
196: protected function _initProfile()
197: {
198: $profile = Mage::getModel('sales/recurring_profile')->load($this->getRequest()->getParam('profile'));
199: if (!$profile->getId()) {
200: Mage::throwException($this->__('Specified profile does not exist.'));
201: }
202: Mage::register('current_recurring_profile', $profile);
203: return $profile;
204: }
205: }
206: