BxCreditsTemplate.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Credits Credits
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxCreditsTemplate extends BxBaseModGeneralTemplate
  12. {
  13. function __construct(&$oConfig, &$oDb)
  14. {
  15. $this->MODULE = 'bx_credits';
  16. parent::__construct($oConfig, $oDb);
  17. $this->aMethodsToCallAddJsCss = array('getBlockBundles', 'getBlockCheckout');
  18. }
  19. public function getJsCode($sType, $aParams = array(), $bWrap = true)
  20. {
  21. $aParams = array_merge(array(
  22. 'aHtmlIds' => $this->_oConfig->getHtmlIds()
  23. ), $aParams);
  24. return parent::getJsCode($sType, $aParams, $bWrap);
  25. }
  26. public function getEmptyAuthor()
  27. {
  28. return MsgBox(_t('_bx_credits_msg_empty_author'));
  29. }
  30. public function getUnit()
  31. {
  32. $CNF = &$this->_oConfig->CNF;
  33. $sUnit = '';
  34. if(($sCode = getParam($CNF['PARAM_CODE'])) != '')
  35. $sUnit = $sCode;
  36. else if(($sIcon = getParam($CNF['PARAM_ICON'])) != '')
  37. $sUnit = $this->parseIcon($sIcon, array('class' => $this->_oConfig->getPrefix('style') . '-prefix-icon bx-def-margin-thd-right'));
  38. return $sUnit;
  39. }
  40. public function getBlockCheckout($oBuyer, $oSeller, $aData)
  41. {
  42. $sStylePrefix = $this->_oConfig->getPrefix('style');
  43. $sJsObject = $this->_oConfig->getJsObject('checkout');
  44. $sTxtQt = _t('_bx_credits_txt_checkout_qt');
  45. $fRate = $this->_oConfig->getConversionRateUse();
  46. $aCurrency = $this->_oConfig->getCurrency();
  47. $sCurrencySign = $aCurrency['sign'];
  48. if(!empty($aData['currency']['sign']))
  49. $sCurrencySign = $aData['currency']['sign'];
  50. $aTmplVarsItems = array();
  51. foreach($aData['items'] as $iIndex => $aItem)
  52. $aTmplVarsItems[] = array(
  53. 'sp' => $sStylePrefix,
  54. 'item_index' => $iIndex + 1,
  55. 'item_title' => $aItem['title'],
  56. 'item_quantity' => $aItem['quantity'] . $sTxtQt
  57. );
  58. $sTitle = _t('_bx_credits_txt_checkout_to', $oSeller->getDisplayName());
  59. if($this->_bIsApi)
  60. return [
  61. 'title' => $sTitle,
  62. 'items' => $aTmplVarsItems,
  63. 'amount' => [
  64. 'value' => (float)$aData['amountm'],
  65. 'currency' => $aData['currency']['code'] ?? $aCurrency['code']
  66. ],
  67. 'rate' => $fRate != 1 ? $fRate : '',
  68. 'request_url' => '/api.php?r=' . $this->_oConfig->getName() . '/checkout&params[]='
  69. ];
  70. $this->addJs(array('checkout.js'));
  71. $this->addCss(array('checkout.css'));
  72. return $this->parseHtmlByName('checkout.html', array(
  73. 'sp' => $sStylePrefix,
  74. 'jo' => $sJsObject,
  75. 'title' => $sTitle,
  76. 'bx_repeat:items' => $aTmplVarsItems,
  77. 'amount' => $sCurrencySign . sprintf("%.2f", (float)($aData['amountm'])),
  78. 'bx_if:show_rate' => array(
  79. 'condition' => $fRate != 1,
  80. 'content' => array(
  81. 'sp' => $sStylePrefix,
  82. 'rate' => $fRate,
  83. )
  84. ),
  85. 'credits' => $this->getModule()->convertC2S($aData['amountc']),
  86. 'js_code' => $this->getJsCode('checkout')
  87. ));
  88. }
  89. public function getBlockBundles()
  90. {
  91. $CNF = &$this->_oConfig->CNF;
  92. $sStylePrefix = $this->_oConfig->getPrefix('style');
  93. $sModule = $this->_oConfig->getName();
  94. $iAuthor = $this->_oConfig->getAuthor();
  95. $aCurrency = $this->_oConfig->getCurrency();
  96. $aBundles = $this->_oDb->getBundle(['type' => 'all', 'active' => 1]);
  97. if(empty($aBundles) || !is_array($aBundles))
  98. return ($sMsg = _t('_Empty')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : MsgBox($sMsg);
  99. $oPayment = BxDolPayments::getInstance();
  100. $aTmplVarsBundles = [];
  101. foreach($aBundles as $aBundle) {
  102. $iBundle = $aBundle[$CNF['FIELD_ID']];
  103. $aJs = $oPayment->getAddToCartJs($iAuthor, $this->MODULE, $iBundle, 1, true);
  104. if(empty($aJs) || !is_array($aJs))
  105. continue;
  106. list($sJsCode, $sJsMethod) = $aJs;
  107. $aTmplVarsBundles[] = array_merge($aBundle, [
  108. 'title' => _t($aBundle[$CNF['FIELD_TITLE']]),
  109. 'description' => _t($aBundle[$CNF['FIELD_DESCRIPTION']]),
  110. 'currency_sign' => $aCurrency['sign'],
  111. 'currency_code' => $aCurrency['code'],
  112. ], !$this->_bIsApi ? [
  113. 'sp' => $sStylePrefix,
  114. 'bx_if:show_bonus' => [
  115. 'condition' => (int)$aBundle[$CNF['FIELD_BONUS']] > 0,
  116. 'content' => [
  117. 'sp' => $sStylePrefix,
  118. 'bonus' => (int)$aBundle[$CNF['FIELD_BONUS']]
  119. ]
  120. ],
  121. 'onclick' => $sJsMethod
  122. ] : [
  123. 'buttons' => [[
  124. 'title' => _t('_bx_credits_txt_purchase'),
  125. 'request_url' => $sModule . '/add_to_cart/&params[]=' . $iBundle . '&params[]=',
  126. ]]
  127. ]);
  128. }
  129. return $this->_bIsApi ? $aTmplVarsBundles : $this->parseHtmlByName('bundles.html', [
  130. 'sp' => $sStylePrefix,
  131. 'bx_repeat:bundles' => $aTmplVarsBundles,
  132. 'js_code' => $oPayment->getCartJs()
  133. ]);
  134. }
  135. public function getPopupSubscribe($oBuyer, $oSeller, $aData)
  136. {
  137. $CNF = &$this->_oConfig->CNF;
  138. $bDynamic = bx_is_dynamic_request();
  139. $sStylePrefix = $this->_oConfig->getPrefix('style');
  140. $sJsObject = $this->_oConfig->getJsObject('subscribe');
  141. $fRate = $this->_oConfig->getConversionRateUse();
  142. $aCurrency = $this->_oConfig->getCurrency();
  143. $sCurrencySign = $aCurrency['sign'];
  144. if(!empty($aData['currency']['sign']))
  145. $sCurrencySign = $aData['currency']['sign'];
  146. if(!empty($aData['period']) && !empty($aData['period_unit'])) {
  147. $sPeriod = _t('_bx_credits_txt_subscribe_period_unit_' . $aData['period_unit']);
  148. if((int)$aData['period'] > 1);
  149. $sPeriod = _t('_bx_credits_txt_subscribe_period_mask', $aData['period'], $sPeriod);
  150. }
  151. else
  152. $sPeriod = _t('_bx_credits_txt_subscribe_lifetime');
  153. $sInclude = '';
  154. $sInclude .= $this->addJs(array('subscribe.js'), $bDynamic);
  155. $sInclude .= $this->addCss(array('main.css', 'subscribe.css'), $bDynamic);
  156. $sContent = ($bDynamic ? $sInclude : '') . $this->parseHtmlByName('subscribe.html', array(
  157. 'sp' => $sStylePrefix,
  158. 'jo' => $sJsObject,
  159. 'title' => _t('_bx_credits_txt_subscribe_for', $oSeller->getDisplayName()),
  160. 'item_title' => $aData['title'],
  161. 'amount' => $sCurrencySign . sprintf("%.2f", (float)($aData['amountm'])),
  162. 'period' => $sPeriod,
  163. 'bx_if:show_rate' => array(
  164. 'condition' => $fRate != 1,
  165. 'content' => array(
  166. 'sp' => $sStylePrefix,
  167. 'rate' => $fRate,
  168. )
  169. ),
  170. 'bx_if:show_trial' => array(
  171. 'condition' => (int)$aData['trial'] > 0,
  172. 'content' => array(
  173. 'sp' => $sStylePrefix,
  174. 'trial' => _t('_bx_credits_txt_subscribe_trial_mask', $aData['trial'], _t('_bx_credits_txt_subscribe_period_unit_day'))
  175. )
  176. ),
  177. 'credits' => $this->getModule()->convertC2S($aData['amountc']),
  178. 'js_code' => $this->getJsCode('subscribe')
  179. ));
  180. return BxTemplFunctions::getInstance()->transBox($this->_oConfig->getHtmlIds('subscribe_popup'), $sContent);
  181. }
  182. }
  183. /** @} */