1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Adminhtml_System_CurrencyController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected function _initCurrency()
42: {
43: $code = $this->getRequest()->getParam('currency');
44: $currency = Mage::getModel('directory/currency')
45: ->load($code);
46:
47: Mage::register('currency', $currency);
48: return $this;
49: }
50:
51: 52: 53:
54: public function indexAction()
55: {
56: $this->_title($this->__('System'))->_title($this->__('Manage Currency Rates'));
57:
58: $this->loadLayout();
59: $this->_setActiveMenu('system/currency');
60: $this->_addContent($this->getLayout()->createBlock('adminhtml/system_currency'));
61: $this->renderLayout();
62: }
63:
64: public function fetchRatesAction()
65: {
66: try {
67: $service = $this->getRequest()->getParam('rate_services');
68: $this->_getSession()->setCurrencyRateService($service);
69: if( !$service ) {
70: throw new Exception(Mage::helper('adminhtml')->__('Invalid Import Service Specified'));
71: }
72: try {
73: $importModel = Mage::getModel(
74: Mage::getConfig()->getNode('global/currency/import/services/' . $service . '/model')->asArray()
75: );
76: } catch (Exception $e) {
77: Mage::throwException(Mage::helper('adminhtml')->__('Unable to initialize import model'));
78: }
79: $rates = $importModel->fetchRates();
80: $errors = $importModel->getMessages();
81: if( sizeof($errors) > 0 ) {
82: foreach ($errors as $error) {
83: Mage::getSingleton('adminhtml/session')->addWarning($error);
84: }
85: Mage::getSingleton('adminhtml/session')->addWarning(Mage::helper('adminhtml')->__('All possible rates were fetched, please click on "Save" to apply'));
86: } else {
87: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('All rates were fetched, please click on "Save" to apply'));
88: }
89:
90: Mage::getSingleton('adminhtml/session')->setRates($rates);
91: }
92: catch (Exception $e){
93: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
94: }
95: $this->_redirect('*/*/');
96: }
97:
98: public function saveRatesAction()
99: {
100: $data = $this->getRequest()->getParam('rate');
101: if( is_array($data) ) {
102: try {
103: foreach ($data as $currencyCode => $rate) {
104: foreach( $rate as $currencyTo => $value ) {
105: $value = abs(Mage::getSingleton('core/locale')->getNumber($value));
106: $data[$currencyCode][$currencyTo] = $value;
107: if( $value == 0 ) {
108: Mage::getSingleton('adminhtml/session')->addWarning(Mage::helper('adminhtml')->__('Invalid input data for %s => %s rate', $currencyCode, $currencyTo));
109: }
110: }
111: }
112:
113: Mage::getModel('directory/currency')->saveRates($data);
114: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('All valid rates have been saved.'));
115: } catch (Exception $e) {
116: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
117: }
118: }
119:
120: $this->_redirect('*/*/');
121: }
122:
123: protected function _isAllowed()
124: {
125: return Mage::getSingleton('admin/session')->isAllowed('system/currency/rates');
126: }
127: }
128: