PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/core/components/shopkeeper/elements/plugins/multi_currency.php

https://bitbucket.org/andchir/shopkeeper-revo
PHP | 178 lines | 109 code | 46 blank | 23 comment | 26 complexity | 425010d7afc5c2f8bb732047a1aa7cc8 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. plugin shk_multicurrency
  4. System event: OnLoadWebDocument, OnSHKgetProductPrice, OnSHKgetProductAdditParamPrice
  5. <div>
  6. Валюта:
  7. <select id="site_currency" name="curency">
  8. <option value="1">руб.</option>
  9. <option value="2">грн.</option>
  10. <option value="3">USD</option>
  11. <option value="4">euro</option>
  12. </select>
  13. </div>
  14. Пример значения парметра shk.currency_rate:
  15. руб.==1||грн.==4.6||USD==32||euro==42
  16. */
  17. $scriptProperties = array_merge(
  18. $scriptProperties,
  19. array(
  20. 'currency_default' => $modx->getOption('shk.currency_default', null, 1),
  21. 'currency_rate' => $modx->getOption('shk.currency_rate', null, '')
  22. )
  23. );
  24. $shk_currency = !empty($_COOKIE['shk_currency']) && is_numeric($_COOKIE['shk_currency']) ? abs(intval($_COOKIE['shk_currency'])) : $scriptProperties['currency_default'];
  25. $currency = isset($_GET['scurr']) && is_numeric($_GET['scurr']) ? intval($_GET['scurr']) : $shk_currency;
  26. if(!function_exists('shk_currency_calc')){
  27. function shk_currency_calc($properties, $base_price, $currency, $rate_ratio = 0){
  28. $inverse = isset($properties['inverse']) ? $properties['inverse'] : false;//обратный перевод цены
  29. if(!$rate_ratio){
  30. if(isset($_SESSION['shk_curr_rate']) && is_numeric($_SESSION['shk_curr_rate']) && !$inverse){
  31. $rate_ratio = $_SESSION['shk_curr_rate'];
  32. }else if(count(explode('||',$properties['currency_rate'])) >= $currency){
  33. if($properties['currency_default'] != $currency){
  34. $curr_rate = explode('||',$properties['currency_rate']);
  35. $temp_arr = explode('==',$curr_rate[$currency-1]);
  36. $rate = isset($temp_arr[1]) && is_numeric($temp_arr[1]) ? floatval(str_replace(',','.',$temp_arr[1])) : 1;
  37. $temp_arr = explode('==',$curr_rate[$properties['currency_default']-1]);
  38. $temp_rate = floatval(str_replace(',','.',$temp_arr[1]));//курс базовой валюты
  39. if( !$inverse ){
  40. $rate_ratio = $temp_rate / $rate;
  41. $_SESSION['shk_curr_rate'] = $rate_ratio;
  42. }else{
  43. $rate_ratio = $rate / $temp_rate;
  44. }
  45. }else{
  46. $rate_ratio = 1;
  47. }
  48. }else{
  49. $rate_ratio = 1;
  50. }
  51. }
  52. if($rate_ratio){
  53. $price = $base_price * $rate_ratio;
  54. $price = round($price,(ceil($price) == $price ? 0 : 2));
  55. return $price;
  56. }else{
  57. return $base_price;
  58. }
  59. }
  60. }
  61. switch($modx->event->name){
  62. case 'OnLoadWebDocument':
  63. if(empty($scriptProperties['noScript'])){
  64. //document.cookie = "shk_currency="+this.value+"; expires=" + new Date(new Date().getTime() + 3600000*24*7).toGMTString() + "; path=/";
  65. $script_str = '
  66. <script type="text/javascript">
  67. var shk_cindex = document.cookie.indexOf("shk_currency=") > -1 ? document.cookie.indexOf("shk_currency=") + new String("shk_currency=").length : -1;
  68. var shk_currency = shk_cindex > -1 ? document.cookie.substring(shk_cindex,shk_cindex+1) : 1;
  69. jQuery("#site_currency")
  70. .val(shk_currency)
  71. .bind("change",function(){
  72. var loc_href = window.location.pathname+document.location.search;
  73. window.location.href = loc_href+(loc_href.indexOf("?") > -1 ? "&" : "?") + "scurr=" + this.value;
  74. });
  75. </script>
  76. ';
  77. $modx->regClientScript($script_str, true);
  78. }
  79. if(isset($_GET['scurr'])){
  80. $purchases = !empty($_SESSION['shk_purchases']) ? $_SESSION['shk_purchases'] : array();
  81. $addit_params = !empty($_SESSION['shk_addit_params']) ? $_SESSION['shk_addit_params'] : array();
  82. if($currency != $shk_currency){
  83. $_SESSION['shk_curr_rate'] = null;
  84. unset($_SESSION['shk_curr_rate']);
  85. //Изменяем цены товаров в корзине
  86. if(!empty($purchases)){
  87. foreach($purchases as $key => &$purchase){
  88. if(isset($purchase['tv_add']['shk_oldprice'])) $purchase['tv_add']['base_price'] = $purchase['tv_add']['shk_oldprice'];
  89. else if(!isset($purchase['tv_add']['base_price'])) $purchase['tv_add']['base_price'] = $purchase['price'];
  90. $base_price = $purchase['tv_add']['base_price'];
  91. $purchase['price'] = shk_currency_calc($scriptProperties, $base_price, $currency);
  92. //Доп. параметры
  93. foreach($addit_params[$key] as &$addit_param){
  94. if(!isset($addit_param[2])) $addit_param[2] = $addit_param[1];
  95. $addit_param[1] = shk_currency_calc($scriptProperties, $addit_param[2], $currency);
  96. }
  97. }
  98. $_SESSION['shk_purchases'] = $purchases;
  99. $_SESSION['shk_addit_params'] = $addit_params;
  100. }
  101. $shk_currency = $currency;
  102. setcookie("shk_currency", $shk_currency, time()+3600*24, "/");
  103. $curr_rate = explode('||',$scriptProperties['currency_rate']);
  104. $currency_name = isset($curr_rate[$shk_currency-1]) ? current(explode('==',$curr_rate[$shk_currency-1])) : '';
  105. $_SESSION['shk_currency_name'] = $currency_name;
  106. }
  107. $back_url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $modx->makeURL($modx->resource->get('parent'),'','','abs');
  108. if( $modx->config['friendly_urls'] ){
  109. if( strpos($back_url,'?') !== false ) $back_url = substr( $back_url, 0, strpos($back_url,'?') );
  110. }
  111. $modx->sendRedirect( $back_url, 0 );
  112. }
  113. $currency_name = !empty($_SESSION['shk_currency_name']) ? $_SESSION['shk_currency_name'] : '';
  114. if(!$currency_name){
  115. $currency_name = $modx->getOption('shk.currency',null,'');
  116. }
  117. $modx->setPlaceholder('shk_currency',$currency_name);
  118. break;
  119. case 'OnSHKgetProductAdditParamPrice':
  120. case 'OnSHKgetProductPrice':
  121. $output = $modx->getOption('price',$scriptProperties,0);
  122. //Считаем цену по курсу
  123. $output = shk_currency_calc($scriptProperties, $output, $currency );
  124. $modx->event->_output = '';
  125. $modx->event->output($output);
  126. break;
  127. }
  128. return '';