PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/community/ET/CurrencyManager/Helper/Data.php

https://bitbucket.org/ttphong2612/billigastamplar-new.se
PHP | 274 lines | 187 code | 42 blank | 45 comment | 38 complexity | fe8b786351a7f27fec7b11eac0ef4eb3 MD5 | raw file
  1. <?php
  2. /**
  3. * NOTICE OF LICENSE
  4. *
  5. * You may not sell, sub-license, rent or lease
  6. * any portion of the Software or Documentation to anyone.
  7. *
  8. * DISCLAIMER
  9. *
  10. * Do not edit or add to this file if you wish to upgrade to newer
  11. * versions in the future.
  12. *
  13. * @category ET
  14. * @package ET_CurrencyManager
  15. * @copyright Copyright (c) 2012 ET Web Solutions (http://etwebsolutions.com)
  16. * @contacts support@etwebsolutions.com
  17. * @license http://shop.etwebsolutions.com/etws-license-free-v1/ ETWS Free License (EFL1)
  18. */
  19. class ET_CurrencyManager_Helper_Data extends Mage_Core_Helper_Abstract
  20. {
  21. /**
  22. * ZEND constants avalable in /lib/Zend/Currency.php
  23. *
  24. * NOTICE
  25. *
  26. * Magento ver 1.3.x - display - USE_SHORTNAME(3) by default
  27. * Magento ver 1.4.x - display - USE_SYMBOL(2) by default
  28. *
  29. * position: 8 - standart; 16 - right; 32 - left
  30. *
  31. */
  32. protected $_options = array();
  33. protected $_optionsadvanced = array();
  34. public function getOptions($options = array(), $old = false, $currency = "default") //$old for support Magento 1.3.x
  35. {
  36. $storeId = Mage::app()->getStore()->getStoreId();
  37. if ((!isset($this->_options[$storeId][$currency])) || (!isset($this->_optionsadvanced[$storeId][$currency]))) {
  38. $this->setOptions($currency);
  39. }
  40. $newOptions = $this->_options[$storeId][$currency];
  41. $newOptionsAdvanced = $this->_optionsadvanced[$storeId][$currency];
  42. if (!$old) {
  43. $newOptions = $newOptions + $newOptionsAdvanced;
  44. }
  45. // For JavaScript prices: Strange Symbol extracting in function getOutputFormat
  46. // in file app/code/core/Mage/Directory/Model/Currency.php
  47. // For Configurable, Bundle and Simple with custom options
  48. // This causes problem if any currency has by default NO_SYMBOL
  49. // with this module can't change display value in this case
  50. if (isset($options["display"])) {
  51. if ($options["display"] == Zend_Currency::NO_SYMBOL) {
  52. unset($newOptions["display"]);
  53. }
  54. }
  55. if (count($options) > 0) {
  56. return $newOptions + $options;
  57. } else {
  58. return $newOptions;
  59. }
  60. }
  61. public function clearOptions($options)
  62. {
  63. $oldOptions = array("position", "format", "display", "precision", "script", "name", "currency", "symbol");
  64. foreach (array_keys($options) as $optionKey) {
  65. if (!in_array($optionKey, $oldOptions)) {
  66. unset($options[$optionKey]);
  67. }
  68. }
  69. return $options;
  70. }
  71. public function isEnabled()
  72. {
  73. $config = Mage::getStoreConfig('currencymanager/general');
  74. $storeId = Mage::app()->getStore()->getStoreId();
  75. return ((($config['enabled']) && ($storeId > 0)) || (($config['enabledadm']) && ($storeId == 0)));
  76. }
  77. public function setOptions($currency = "default")
  78. {
  79. $config = Mage::getStoreConfig('currencymanager/general');
  80. $options = array();
  81. $optionsAdvanced = array();
  82. $storeId = Mage::app()->getStore()->getStoreId();
  83. if ($this->isEnabled()) {
  84. $notCheckout = !($config['excludecheckout'] & $this->isInOrder());
  85. $this->_getGeneralOptions($config, $options, $optionsAdvanced, $notCheckout);
  86. // formatting symbols from admin, preparing to use. Maybe can do better :)
  87. // если в админке будут внесены
  88. // несколько значений для одной валюты,
  89. // то использоваться будет только одна
  90. if (isset($config['symbolreplace'])) {
  91. $this->_collectCurrencyOptions($config, $currency, $notCheckout, $options, $optionsAdvanced);
  92. }
  93. } // end NOT ENABLED
  94. $this->_options[$storeId][$currency] = $options;
  95. $this->_optionsadvanced[$storeId][$currency] = $optionsAdvanced;
  96. if (!isset($this->_options[$storeId]["default"])) {
  97. $this->_options[$storeId]["default"] = $options;
  98. $this->_optionsadvanced[$storeId]["default"] = $optionsAdvanced;
  99. }
  100. return $this;
  101. }
  102. protected function _getGeneralOptions($config, &$options, &$optionsAdvanced, $notCheckout)
  103. {
  104. if ($notCheckout) {
  105. if (isset($config['precision'])) { // precision must be in range -1 .. 30
  106. $options['precision'] = min(30, max(-1, (int)$config['precision']));
  107. }
  108. if (isset($config['zerotext'])) {
  109. $optionsAdvanced['zerotext'] = $config['zerotext'];
  110. }
  111. }
  112. if (isset($config['position'])) {
  113. $options['position'] = (int)$config['position'];
  114. }
  115. if (isset($config['display'])) {
  116. $options['display'] = (int)$config['display'];
  117. }
  118. if (isset($config['input_admin'])) {
  119. if ($config['input_admin'] > 0) {
  120. $optionsAdvanced['input_admin'] = (int)$config['input_admin'];
  121. }
  122. }
  123. $optionsAdvanced['excludecheckout'] = $config['excludecheckout'];
  124. $optionsAdvanced['cutzerodecimal'] = $config['cutzerodecimal'];
  125. $optionsAdvanced['cutzerodecimal_suffix'] = isset($config['cutzerodecimal_suffix']) ?
  126. $config['cutzerodecimal_suffix'] : "";
  127. $optionsAdvanced['min_decimal_count'] = isset($config['min_decimal_count']) ?
  128. $config['min_decimal_count'] : "2";
  129. }
  130. protected function _collectCurrencyOptions($config, $currency, $notCheckout, &$options, &$optionsAdvanced)
  131. {
  132. $symbolReplace = $this->_unsetSymbolReplace($config);
  133. if (count($symbolReplace['currency']) > 0) {
  134. $tmpOptions = array();
  135. $tmpOptionsAdvanced = array();
  136. $tmpOptionsAdvanced['cutzerodecimal'] = $this->_getCurrencyOption(
  137. $currency, $symbolReplace, 'cutzerodecimal', true
  138. );
  139. if (isset($symbolReplace['cutzerodecimal_suffix'])) {
  140. $tmpOptionsAdvanced["cutzerodecimal_suffix"] = $this->_getCurrencyOption(
  141. $currency, $symbolReplace, 'cutzerodecimal_suffix'
  142. );
  143. }
  144. if (isset($symbolReplace['min_decimal_count'])) {
  145. $tmpOptionsAdvanced["min_decimal_count"] = $this->_getCurrencyOption(
  146. $currency, $symbolReplace, 'min_decimal_count'
  147. );
  148. }
  149. $tmpOptions['position'] = $this->_getCurrencyOption($currency, $symbolReplace, 'position', true);
  150. $tmpOptions['display'] = $this->_getCurrencyOption($currency, $symbolReplace, 'display', true);
  151. $tmpOptions['symbol'] = $this->_getCurrencyOption($currency, $symbolReplace, 'symbol');
  152. if ($notCheckout) {
  153. $tmpOptionsAdvanced['zerotext'] = $this->_getCurrencyOption($currency, $symbolReplace, 'zerotext');
  154. $precision = $this->_getCurrencyOption($currency, $symbolReplace, 'precision', true);
  155. if ($precision !== false) {
  156. $tmpOptions['precision'] = min(30, max(-1, $precision));
  157. }
  158. }
  159. foreach ($tmpOptions as $option => $value) {
  160. if ($value !== false) {
  161. $options[$option] = $value;
  162. }
  163. }
  164. foreach ($tmpOptionsAdvanced as $option => $value) {
  165. if ($value !== false) {
  166. $optionsAdvanced[$option] = $value;
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * To check where price is used
  173. * in some cases default values for precision and zerotext should be used
  174. * for sales/checkout in frontend
  175. * for admin AND sales_order*
  176. *
  177. * @return bool
  178. */
  179. public function isInOrder()
  180. {
  181. $moduleName = Mage::app()->getRequest()->getModuleName();
  182. $controllerName = Mage::app()->getRequest()->getControllerName();
  183. $orderModules = array('sales', 'checkout', 'paypal');
  184. $modifiedOrderModules = array(
  185. 'order_modules' => new Varien_Object(array('module_names'=>$orderModules)),
  186. );
  187. Mage::dispatchEvent('et_currencymanager_checking_is_in_order_before', $modifiedOrderModules);
  188. $orderModules = $modifiedOrderModules['order_modules']->getData('module_names');
  189. return ((in_array($moduleName, $orderModules))
  190. || (
  191. ($moduleName == 'admin') && (strpos($controllerName, 'sales_order') !== false)
  192. ));
  193. }
  194. protected function _unsetSymbolReplace($config)
  195. {
  196. if (!is_array($config['symbolreplace'])) {
  197. $symbolReplace = unserialize($config['symbolreplace']);
  198. foreach (array_keys($symbolReplace['currency']) as $symbolReplaceKey) {
  199. if (strlen(trim($symbolReplace['currency'][$symbolReplaceKey])) == 0) {
  200. unset($symbolReplace['currency'][$symbolReplaceKey]);
  201. unset($symbolReplace['precision'][$symbolReplaceKey]);
  202. unset($symbolReplace['min_decimal_count'][$symbolReplaceKey]);
  203. unset($symbolReplace['cutzerodecimal'][$symbolReplaceKey]);
  204. unset($symbolReplace['cutzerodecimal_suffix'][$symbolReplaceKey]);
  205. unset($symbolReplace['position'][$symbolReplaceKey]);
  206. unset($symbolReplace['display'][$symbolReplaceKey]);
  207. unset($symbolReplace['symbol'][$symbolReplaceKey]);
  208. unset($symbolReplace['zerotext'][$symbolReplaceKey]);
  209. }
  210. }
  211. return $symbolReplace;
  212. }
  213. return false;
  214. }
  215. public function resetOptions()
  216. {
  217. $this->_options = array();
  218. $this->_optionsadvanced = array();
  219. }
  220. protected function _getCurrencyOption($currency, $symbolReplace, $option, $int = false)
  221. {
  222. $configSubData = array_combine($symbolReplace['currency'], $symbolReplace[$option]);
  223. if (array_key_exists($currency, $configSubData)) {
  224. $value = $configSubData[$currency];
  225. if ($value === "") {
  226. return false;
  227. }
  228. return ($int) ? (int)$value : $value;
  229. }
  230. return false;
  231. }
  232. }