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: class Mage_Directory_Model_Observer
33: {
34: const CRON_STRING_PATH = 'crontab/jobs/currency_rates_update/schedule/cron_expr';
35: const IMPORT_ENABLE = 'currency/import/enabled';
36: const IMPORT_SERVICE = 'currency/import/service';
37:
38: const XML_PATH_ERROR_TEMPLATE = 'currency/import/error_email_template';
39: const XML_PATH_ERROR_IDENTITY = 'currency/import/error_email_identity';
40: const XML_PATH_ERROR_RECIPIENT = 'currency/import/error_email';
41:
42: public function scheduledUpdateCurrencyRates($schedule)
43: {
44: $importWarnings = array();
45: if(!Mage::getStoreConfig(self::IMPORT_ENABLE) || !Mage::getStoreConfig(self::CRON_STRING_PATH)) {
46: return;
47: }
48:
49: $service = Mage::getStoreConfig(self::IMPORT_SERVICE);
50: if( !$service ) {
51: $importWarnings[] = Mage::helper('directory')->__('FATAL ERROR:') . ' ' . Mage::helper('directory')->__('Invalid Import Service specified.');
52: }
53:
54: try {
55: $importModel = Mage::getModel(Mage::getConfig()->getNode('global/currency/import/services/' . $service . '/model')->asArray());
56: } catch (Exception $e) {
57: $importWarnings[] = Mage::helper('directory')->__('FATAL ERROR:') . ' ' . Mage::throwException(Mage::helper('directory')->__('Unable to initialize the import model.'));
58: }
59:
60: $rates = $importModel->fetchRates();
61: $errors = $importModel->getMessages();
62:
63: if( sizeof($errors) > 0 ) {
64: foreach ($errors as $error) {
65: $importWarnings[] = Mage::helper('directory')->__('WARNING:') . ' ' . $error;
66: }
67: }
68:
69: if (sizeof($importWarnings) == 0) {
70: Mage::getModel('directory/currency')->saveRates($rates);
71: }
72: else {
73: $translate = Mage::getSingleton('core/translate');
74:
75: $translate->setTranslateInline(false);
76:
77:
78: $mailTemplate = Mage::getModel('core/email_template');
79: $mailTemplate->setDesignConfig(array(
80: 'area' => 'frontend',
81: ))->sendTransactional(
82: Mage::getStoreConfig(self::XML_PATH_ERROR_TEMPLATE),
83: Mage::getStoreConfig(self::XML_PATH_ERROR_IDENTITY),
84: Mage::getStoreConfig(self::XML_PATH_ERROR_RECIPIENT),
85: null,
86: array(
87: 'warnings' => join("\n", $importWarnings),
88: )
89: );
90:
91: $translate->setTranslateInline(true);
92: }
93: }
94: }
95: