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_CurrencySymbol_Model_System_Currencysymbol
35: {
36: 37: 38: 39: 40:
41: protected $_symbolsData = array();
42:
43: 44: 45: 46: 47:
48: protected $_storeId;
49:
50: 51: 52: 53: 54:
55: protected $_websiteId;
56: 57: 58: 59: 60:
61: protected $_cacheTypes = array(
62: 'config',
63: 'block_html',
64: 'layout'
65: );
66:
67: 68: 69:
70: const XML_PATH_CUSTOM_CURRENCY_SYMBOL = 'currency/options/customsymbol';
71: const XML_PATH_ALLOWED_CURRENCIES = 'currency/options/allow';
72:
73: 74: 75:
76: const ALLOWED_CURRENCIES_CONFIG_SEPARATOR = ',';
77:
78: 79: 80:
81: const CONFIG_SECTION = 'currency';
82:
83: 84: 85: 86: 87: 88:
89: public function setStoreId($storeId=null)
90: {
91: $this->_storeId = $storeId;
92: $this->_symbolsData = array();
93:
94: return $this;
95: }
96:
97: 98: 99: 100: 101: 102:
103: public function setWebsiteId($websiteId=null)
104: {
105: $this->_websiteId = $websiteId;
106: $this->_symbolsData = array();
107:
108: return $this;
109: }
110:
111: 112: 113: 114: 115:
116: public function getCurrencySymbolsData()
117: {
118: if ($this->_symbolsData) {
119: return $this->_symbolsData;
120: }
121:
122: $this->_symbolsData = array();
123:
124: $allowedCurrencies = explode(
125: self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR,
126: Mage::getStoreConfig(self::XML_PATH_ALLOWED_CURRENCIES, null)
127: );
128:
129:
130: $storeModel = Mage::getSingleton('adminhtml/system_store');
131: foreach ($storeModel->getWebsiteCollection() as $website) {
132: $websiteShow = false;
133: foreach ($storeModel->getGroupCollection() as $group) {
134: if ($group->getWebsiteId() != $website->getId()) {
135: continue;
136: }
137: foreach ($storeModel->getStoreCollection() as $store) {
138: if ($store->getGroupId() != $group->getId()) {
139: continue;
140: }
141: if (!$websiteShow) {
142: $websiteShow = true;
143: $websiteSymbols = $website->getConfig(self::XML_PATH_ALLOWED_CURRENCIES);
144: $allowedCurrencies = array_merge($allowedCurrencies, explode(
145: self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR,
146: $websiteSymbols
147: ));
148: }
149: $storeSymbols = Mage::getStoreConfig(self::XML_PATH_ALLOWED_CURRENCIES, $store);
150: $allowedCurrencies = array_merge($allowedCurrencies, explode(
151: self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR,
152: $storeSymbols
153: ));
154: }
155: }
156: }
157: ksort($allowedCurrencies);
158:
159: $currentSymbols = $this->_unserializeStoreConfig(self::XML_PATH_CUSTOM_CURRENCY_SYMBOL);
160:
161:
162: $locale = Mage::app()->getLocale();
163: foreach ($allowedCurrencies as $code) {
164: if (!$symbol = $locale->getTranslation($code, 'currencysymbol')) {
165: $symbol = $code;
166: }
167: $name = $locale->getTranslation($code, 'nametocurrency');
168: if (!$name) {
169: $name = $code;
170: }
171: $this->_symbolsData[$code] = array(
172: 'parentSymbol' => $symbol,
173: 'displayName' => $name
174: );
175:
176: if (isset($currentSymbols[$code]) && !empty($currentSymbols[$code])) {
177: $this->_symbolsData[$code]['displaySymbol'] = $currentSymbols[$code];
178: } else {
179: $this->_symbolsData[$code]['displaySymbol'] = $this->_symbolsData[$code]['parentSymbol'];
180: }
181: if ($this->_symbolsData[$code]['parentSymbol'] == $this->_symbolsData[$code]['displaySymbol']) {
182: $this->_symbolsData[$code]['inherited'] = true;
183: } else {
184: $this->_symbolsData[$code]['inherited'] = false;
185: }
186: }
187:
188: return $this->_symbolsData;
189: }
190:
191: 192: 193: 194: 195: 196:
197: public function setCurrencySymbolsData($symbols=array())
198: {
199: foreach ($this->getCurrencySymbolsData() as $code => $values) {
200: if (isset($symbols[$code])) {
201: if ($symbols[$code] == $values['parentSymbol'] || empty($symbols[$code]))
202: unset($symbols[$code]);
203: }
204: }
205: if ($symbols) {
206: $value['options']['fields']['customsymbol']['value'] = serialize($symbols);
207: } else {
208: $value['options']['fields']['customsymbol']['inherit'] = 1;
209: }
210:
211: Mage::getModel('adminhtml/config_data')
212: ->setSection(self::CONFIG_SECTION)
213: ->setWebsite(null)
214: ->setStore(null)
215: ->setGroups($value)
216: ->save();
217:
218: Mage::dispatchEvent('admin_system_config_changed_section_currency_before_reinit',
219: array('website' => $this->_websiteId, 'store' => $this->_storeId)
220: );
221:
222:
223: Mage::getConfig()->reinit();
224: Mage::app()->reinitStores();
225:
226: $this->clearCache();
227:
228: Mage::dispatchEvent('admin_system_config_changed_section_currency',
229: array('website' => $this->_websiteId, 'store' => $this->_storeId)
230: );
231:
232: return $this;
233: }
234:
235: 236: 237: 238: 239: 240:
241: public function getCurrencySymbol($code)
242: {
243: $customSymbols = $this->_unserializeStoreConfig(self::XML_PATH_CUSTOM_CURRENCY_SYMBOL);
244: if (array_key_exists($code, $customSymbols)) {
245: return $customSymbols[$code];
246: }
247:
248: return false;
249: }
250:
251: 252: 253: 254: 255:
256: public function clearCache()
257: {
258:
259: foreach ($this->_cacheTypes as $cacheType) {
260: Mage::app()->getCacheInstance()->invalidateType($cacheType);
261: }
262: return $this;
263: }
264:
265: 266: 267: 268: 269: 270: 271:
272: protected function _unserializeStoreConfig($configPath, $storeId = null)
273: {
274: $result = array();
275: $configData = (string)Mage::getStoreConfig($configPath, $storeId);
276: if ($configData) {
277: $result = unserialize($configData);
278: }
279:
280: return is_array($result) ? $result : array();
281: }
282: }
283: