PageRenderTime 44ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/CurrencySymbol/Model/System/Currencysymbol.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 282 lines | 147 code | 30 blank | 105 comment | 22 complexity | f43af0d9441283cb3583dfb25c9c72c2 MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_CurrencySymbol
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Custom currency symbol model
  28. *
  29. * @category Mage
  30. * @package Mage_CurrencySymbol
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_CurrencySymbol_Model_System_Currencysymbol
  34. {
  35. /**
  36. * Custom currency symbol properties
  37. *
  38. * @var array
  39. */
  40. protected $_symbolsData = array();
  41. /**
  42. * Store id
  43. *
  44. * @var string | null
  45. */
  46. protected $_storeId;
  47. /**
  48. * Website id
  49. *
  50. * @var string | null
  51. */
  52. protected $_websiteId;
  53. /**
  54. * Cache types which should be invalidated
  55. *
  56. * @var array
  57. */
  58. protected $_cacheTypes = array(
  59. 'config',
  60. 'block_html',
  61. 'layout'
  62. );
  63. /**
  64. * Config path to custom currency symbol value
  65. */
  66. const XML_PATH_CUSTOM_CURRENCY_SYMBOL = 'currency/options/customsymbol';
  67. const XML_PATH_ALLOWED_CURRENCIES = 'currency/options/allow';
  68. /*
  69. * Separator used in config in allowed currencies list
  70. */
  71. const ALLOWED_CURRENCIES_CONFIG_SEPARATOR = ',';
  72. /**
  73. * Config currency section
  74. */
  75. const CONFIG_SECTION = 'currency';
  76. /**
  77. * Sets store Id
  78. *
  79. * @param $storeId
  80. * @return Mage_CurrencySymbol_Model_System_Currencysymbol
  81. */
  82. public function setStoreId($storeId=null)
  83. {
  84. $this->_storeId = $storeId;
  85. $this->_symbolsData = array();
  86. return $this;
  87. }
  88. /**
  89. * Sets website Id
  90. *
  91. * @param $websiteId
  92. * @return Mage_CurrencySymbol_Model_System_Currencysymbol
  93. */
  94. public function setWebsiteId($websiteId=null)
  95. {
  96. $this->_websiteId = $websiteId;
  97. $this->_symbolsData = array();
  98. return $this;
  99. }
  100. /**
  101. * Returns currency symbol properties array based on config values
  102. *
  103. * @return array
  104. */
  105. public function getCurrencySymbolsData()
  106. {
  107. if ($this->_symbolsData) {
  108. return $this->_symbolsData;
  109. }
  110. $this->_symbolsData = array();
  111. $allowedCurrencies = explode(
  112. self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR,
  113. Mage::getStoreConfig(self::XML_PATH_ALLOWED_CURRENCIES, null)
  114. );
  115. /* @var $storeModel Mage_Adminhtml_Model_System_Store */
  116. $storeModel = Mage::getSingleton('adminhtml/system_store');
  117. foreach ($storeModel->getWebsiteCollection() as $website) {
  118. $websiteShow = false;
  119. foreach ($storeModel->getGroupCollection() as $group) {
  120. if ($group->getWebsiteId() != $website->getId()) {
  121. continue;
  122. }
  123. foreach ($storeModel->getStoreCollection() as $store) {
  124. if ($store->getGroupId() != $group->getId()) {
  125. continue;
  126. }
  127. if (!$websiteShow) {
  128. $websiteShow = true;
  129. $websiteSymbols = $website->getConfig(self::XML_PATH_ALLOWED_CURRENCIES);
  130. $allowedCurrencies = array_merge($allowedCurrencies, explode(
  131. self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR,
  132. $websiteSymbols
  133. ));
  134. }
  135. $storeSymbols = Mage::getStoreConfig(self::XML_PATH_ALLOWED_CURRENCIES, $store);
  136. $allowedCurrencies = array_merge($allowedCurrencies, explode(
  137. self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR,
  138. $storeSymbols
  139. ));
  140. }
  141. }
  142. }
  143. ksort($allowedCurrencies);
  144. $currentSymbols = $this->_unserializeStoreConfig(self::XML_PATH_CUSTOM_CURRENCY_SYMBOL);
  145. /** @var $locale Mage_Core_Model_Locale */
  146. $locale = Mage::app()->getLocale();
  147. foreach ($allowedCurrencies as $code) {
  148. if (!$symbol = $locale->getTranslation($code, 'currencysymbol')) {
  149. $symbol = $code;
  150. }
  151. $name = $locale->getTranslation($code, 'nametocurrency');
  152. if (!$name) {
  153. $name = $code;
  154. }
  155. $this->_symbolsData[$code] = array(
  156. 'parentSymbol' => $symbol,
  157. 'displayName' => $name
  158. );
  159. if (isset($currentSymbols[$code]) && !empty($currentSymbols[$code])) {
  160. $this->_symbolsData[$code]['displaySymbol'] = $currentSymbols[$code];
  161. } else {
  162. $this->_symbolsData[$code]['displaySymbol'] = $this->_symbolsData[$code]['parentSymbol'];
  163. }
  164. if ($this->_symbolsData[$code]['parentSymbol'] == $this->_symbolsData[$code]['displaySymbol']) {
  165. $this->_symbolsData[$code]['inherited'] = true;
  166. } else {
  167. $this->_symbolsData[$code]['inherited'] = false;
  168. }
  169. }
  170. return $this->_symbolsData;
  171. }
  172. /**
  173. * Saves currency symbol to config
  174. *
  175. * @param $symbols array
  176. * @return Mage_CurrencySymbol_Model_System_Currencysymbol
  177. */
  178. public function setCurrencySymbolsData($symbols=array())
  179. {
  180. foreach ($this->getCurrencySymbolsData() as $code => $values) {
  181. if (isset($symbols[$code])) {
  182. if ($symbols[$code] == $values['parentSymbol'] || empty($symbols[$code]))
  183. unset($symbols[$code]);
  184. }
  185. }
  186. if ($symbols) {
  187. $value['options']['fields']['customsymbol']['value'] = serialize($symbols);
  188. } else {
  189. $value['options']['fields']['customsymbol']['inherit'] = 1;
  190. }
  191. Mage::getModel('adminhtml/config_data')
  192. ->setSection(self::CONFIG_SECTION)
  193. ->setWebsite(null)
  194. ->setStore(null)
  195. ->setGroups($value)
  196. ->save();
  197. Mage::dispatchEvent('admin_system_config_changed_section_currency_before_reinit',
  198. array('website' => $this->_websiteId, 'store' => $this->_storeId)
  199. );
  200. // reinit configuration
  201. Mage::getConfig()->reinit();
  202. Mage::app()->reinitStores();
  203. $this->clearCache();
  204. Mage::dispatchEvent('admin_system_config_changed_section_currency',
  205. array('website' => $this->_websiteId, 'store' => $this->_storeId)
  206. );
  207. return $this;
  208. }
  209. /**
  210. * Returns custom currency symbol by currency code
  211. *
  212. * @param $code
  213. * @return bool|string
  214. */
  215. public function getCurrencySymbol($code)
  216. {
  217. $customSymbols = $this->_unserializeStoreConfig(self::XML_PATH_CUSTOM_CURRENCY_SYMBOL);
  218. if (array_key_exists($code, $customSymbols)) {
  219. return $customSymbols[$code];
  220. }
  221. return false;
  222. }
  223. /**
  224. * Clear translate cache
  225. *
  226. * @return Saas_Translate_Helper_Data
  227. */
  228. public function clearCache()
  229. {
  230. // clear cache for frontend
  231. foreach ($this->_cacheTypes as $cacheType) {
  232. Mage::app()->getCacheInstance()->invalidateType($cacheType);
  233. }
  234. return $this;
  235. }
  236. /**
  237. * Unserialize data from Store Config.
  238. *
  239. * @param string $configPath
  240. * @param int $storeId
  241. * @return array
  242. */
  243. protected function _unserializeStoreConfig($configPath, $storeId = null)
  244. {
  245. $result = array();
  246. $configData = (string)Mage::getStoreConfig($configPath, $storeId);
  247. if ($configData) {
  248. $result = unserialize($configData);
  249. }
  250. return is_array($result) ? $result : array();
  251. }
  252. }