BxDonationsTemplate.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Donations Donations
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxDonationsTemplate extends BxBaseModGeneralTemplate
  12. {
  13. function __construct(&$oConfig, &$oDb)
  14. {
  15. parent::__construct($oConfig, $oDb);
  16. $this->aMethodsToCallAddJsCss[] = 'getBlockMake';
  17. }
  18. public function displayEmptyOwner()
  19. {
  20. return MsgBox(_t('_bx_donations_msg_empty_owner'));
  21. }
  22. public function addCssJs()
  23. {
  24. parent::addCssJs();
  25. $this->addJs(array('main.js'));
  26. }
  27. public function getIncludeCssJs()
  28. {
  29. return '';
  30. }
  31. public function getBlockMake($sSelected = 'single')
  32. {
  33. $CNF = &$this->_oConfig->CNF;
  34. $oPayment = BxDolPayments::getInstance();
  35. $iPaymentSeller = $this->_oConfig->getOwner();
  36. $sPaymentModule = $this->_oConfig->getName();
  37. $aJsCodes = [];
  38. $sJsObject = $this->_oConfig->getJsObject('main');
  39. $sHtmlIdBTypeLink = $this->_oConfig->getHtmlIds('link_billing_type');
  40. $bShowTitle = $this->_oConfig->isShowTitle();
  41. $bEnableOther = $this->_oConfig->isEnableOther();
  42. $aPeriodUnits = $this->_oConfig->getPeriodUnits();
  43. $aMenuBillingTypes = array();
  44. $aTmplVarsBillingTypes = array();
  45. $aBillingTypes = $this->_oConfig->getBillingTypes();
  46. foreach($aBillingTypes as $sBillingType) {
  47. $aTypes = $this->_oDb->getTypes(array('type' => 'by_btype_' . $sBillingType, 'active' => 1));
  48. if(empty($aTypes) || !is_array($aTypes))
  49. continue;
  50. $aTmplVarsTypes = array();
  51. foreach($aTypes as $aType) {
  52. $sDuration = '';
  53. switch($sBillingType) {
  54. case BX_DONATIONS_BTYPE_SINGLE:
  55. $aJs = $oPayment->getAddToCartJs($iPaymentSeller, $sPaymentModule, $aType[$CNF['FIELD_ID']], 1, true);
  56. if(empty($aJs) || !is_array($aJs))
  57. continue 2;
  58. break;
  59. case BX_DONATIONS_BTYPE_RECURRING:
  60. $aJs = $oPayment->getSubscribeJs($iPaymentSeller, '', $sPaymentModule, $aType[$CNF['FIELD_ID']], 1);
  61. if(empty($aJs) || !is_array($aJs))
  62. continue 2;
  63. if((int)$aType[$CNF['FIELD_PERIOD']] > 1)
  64. $sDuration .= $aType[$CNF['FIELD_PERIOD']] . ' ';
  65. $sDuration .= _t($aPeriodUnits[$aType[$CNF['FIELD_PERIOD_UNIT']]]);
  66. break;
  67. }
  68. list($sJsCode, $sJsMethod) = $aJs;
  69. $sJsCodeHash = md5($sJsCode);
  70. if(!isset($aJsCodes[$sJsCodeHash]))
  71. $aJsCodes[$sJsCodeHash] = $sJsCode;
  72. $sAmount = _t_format_currency($aType[$CNF['FIELD_AMOUNT']], getParam($CNF['PARAM_AMOUNT_PRECISION']));
  73. $sAmount = _t('_bx_donations_txt_amount_' . $sBillingType, $sAmount, $sDuration);
  74. $aTmplVarsTypes[] = array(
  75. 'onclick' => $sJsMethod,
  76. 'bx_if:show_title' => array(
  77. 'condition' => $bShowTitle,
  78. 'content' => array(
  79. 'title' => _t($aType[$CNF['FIELD_TITLE']])
  80. )
  81. ),
  82. 'amount' => $sAmount
  83. );
  84. }
  85. if($bEnableOther)
  86. $aTmplVarsTypes[] = array(
  87. 'onclick' => $sJsObject . ".other(this, '" . $sBillingType . "')",
  88. 'bx_if:show_title' => array(
  89. 'condition' => $bShowTitle,
  90. 'content' => array(
  91. 'title' => _t('_bx_donations_txt_other_title')
  92. )
  93. ),
  94. 'amount' => _t('_bx_donations_txt_other_value')
  95. );
  96. $aTmplVarsBillingTypes[$sBillingType] = array(
  97. 'class' => $sBillingType,
  98. 'bx_repeat:types' => $aTmplVarsTypes
  99. );
  100. $aMenuBillingTypes[] = array('name' => $sHtmlIdBTypeLink . $sBillingType, 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:' . $sJsObject . ".changeType(this, '" . $sBillingType . "')", 'target' => '_self', 'title' => _t('_bx_donations_txt_do_' . $sBillingType));
  101. }
  102. $iTmplVarsBillingTypes = count($aTmplVarsBillingTypes);
  103. if($iTmplVarsBillingTypes > 1 && isset($aTmplVarsBillingTypes[$sSelected]))
  104. $aTmplVarsBillingTypes[$sSelected]['class'] .= ' active';
  105. else if($iTmplVarsBillingTypes > 0)
  106. $aTmplVarsBillingTypes[key($aTmplVarsBillingTypes)]['class'] .= ' active';
  107. $aTmplVarsMenu = array();
  108. if(count($aMenuBillingTypes) > 1) {
  109. $oMenuBillingTypes = new BxTemplMenu(array('template' => 'menu_buttons_hor.html', 'menu_id'=> $this->_oConfig->getHtmlIds('menu_billing_types'), 'menu_items' => $aMenuBillingTypes));
  110. $oMenuBillingTypes->setSelected('', $sHtmlIdBTypeLink . $sSelected);
  111. $aTmplVarsMenu = array(
  112. 'menu_billing_type' => $oMenuBillingTypes->getCode(),
  113. );
  114. }
  115. $sTmplName = 'block_make.html';
  116. $aTmplVars = array(
  117. 'bx_if:show_menu' => array(
  118. 'condition' => !empty($aTmplVarsMenu),
  119. 'content' => $aTmplVarsMenu
  120. ),
  121. 'bx_repeat:billing_types' => array_values($aTmplVarsBillingTypes),
  122. 'js_code' => implode('', $aJsCodes) . $this->getJsCode('main')
  123. );
  124. $sResult = null;
  125. /**
  126. * @hooks
  127. * @hookdef hook-bx_donations-donate 'bx_donations', 'donate' - hook to override the content of donate block
  128. * - $unit_name - equals `bx_donations`
  129. * - $action - equals `donate`
  130. * - $object_id - not used
  131. * - $sender_id - not used
  132. * - $extra_params - array of additional params with the following array keys:
  133. * - `tmpl_name` - [string] by ref, template name, can be overridden in hook processing
  134. * - `tmpl_vars` - [array] by ref, template parsable variables as key&value pairs, can be overridden in hook processing
  135. * - `override_result` - [string] by ref, string with HTML code to be output, if null the default template parsing function will be used, can be overridden in hook processing
  136. * @hook @ref hook-bx_donations-donate
  137. */
  138. bx_alert($this->_oConfig->getName(), 'donate', 0, 0, [
  139. 'tmpl_name' => &$sTmplName,
  140. 'tmpl_vars' => &$aTmplVars,
  141. 'tmpl_name_ref' => &$sTmplName,
  142. 'tmpl_vars_ref' => &$aTmplVars,
  143. 'override_result' => &$sResult
  144. ]);
  145. if($sResult !== null)
  146. return $sResult;
  147. return $this->parseHtmlByName($sTmplName, $aTmplVars);
  148. }
  149. public function getJsCode($sType, $aParams = array(), $bWrap = true)
  150. {
  151. $aParams = array_merge(array(
  152. 'aHtmlIds' => $this->_oConfig->getHtmlIds()
  153. ), $aParams);
  154. return parent::getJsCode($sType, $aParams, $bWrap);
  155. }
  156. }
  157. /** @} */