00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Adminhtml_System_CurrencyController extends Mage_Adminhtml_Controller_Action
00035 {
00036
00037
00038
00039
00040
00041 protected function _initCurrency()
00042 {
00043 $code = $this->getRequest()->getParam('currency');
00044 $currency = Mage::getModel('directory/currency')
00045 ->load($code);
00046
00047 Mage::register('currency', $currency);
00048 return $this;
00049 }
00050
00051
00052
00053
00054 public function indexAction()
00055 {
00056 $this->loadLayout();
00057 $this->_setActiveMenu('system/currency');
00058 $this->_addContent($this->getLayout()->createBlock('adminhtml/system_currency'));
00059 $this->renderLayout();
00060 }
00061
00062 public function fetchRatesAction()
00063 {
00064 try {
00065 $service = $this->getRequest()->getParam('rate_services');
00066 $this->_getSession()->setCurrencyRateService($service);
00067 if( !$service ) {
00068 throw new Exception(Mage::helper('adminhtml')->__('Invalid Import Service Specified'));
00069 }
00070 try {
00071 $importModel = Mage::getModel(Mage::getConfig()->getNode('global/currency/import/services/' . $service . '/model')->asArray());
00072 } catch (Exception $e) {
00073 Mage::throwException(Mage::helper('adminhtml')->__('Unable to initialize import model'));
00074 }
00075 $rates = $importModel->fetchRates();
00076 $errors = $importModel->getMessages();
00077 if( sizeof($errors) > 0 ) {
00078 foreach ($errors as $error) {
00079 Mage::getSingleton('adminhtml/session')->addWarning($error);
00080 }
00081 Mage::getSingleton('adminhtml/session')->addWarning(Mage::helper('adminhtml')->__('All possible rates were fetched, click on "Save" to apply'));
00082 } else {
00083 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('All rates were fetched, click on "Save" to apply'));
00084 }
00085
00086 Mage::getSingleton('adminhtml/session')->setRates($rates);
00087 }
00088 catch (Exception $e){
00089 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00090 }
00091 $this->_redirect('*/*/');
00092 }
00093
00094 public function saveRatesAction()
00095 {
00096 $data = $this->getRequest()->getParam('rate');
00097 if( is_array($data) ) {
00098 try {
00099 foreach ($data as $currencyCode => $rate) {
00100 foreach( $rate as $currencyTo => $value ) {
00101 $value = abs(Mage::getSingleton('core/locale')->getNumber($value));
00102 $data[$currencyCode][$currencyTo] = $value;
00103 if( $value == 0 ) {
00104 Mage::getSingleton('adminhtml/session')->addWarning(Mage::helper('adminhtml')->__('Invalid input data for %s => %s rate', $currencyCode, $currencyTo));
00105 }
00106 }
00107 }
00108
00109 Mage::getModel('directory/currency')->saveRates($data);
00110 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('All valid rates successfully saved'));
00111 } catch (Exception $e) {
00112 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00113 }
00114 }
00115
00116 $this->_redirect('*/*/');
00117 }
00118
00119 protected function _isAllowed()
00120 {
00121 return Mage::getSingleton('admin/session')->isAllowed('system/currency');
00122 }
00123 }