BxCreditsConfig.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 BxCreditsConfig extends BxBaseModGeneralConfig
  12. {
  13. protected $_iAuthor;
  14. protected $_aCurrency;
  15. protected $_sCheckoutSessionKey;
  16. function __construct($aModule)
  17. {
  18. parent::__construct($aModule);
  19. $this->CNF = array (
  20. // module icon
  21. 'ICON' => 'copyright',
  22. // database tables
  23. 'TABLE_BUNDLES' => $aModule['db_prefix'] . 'bundles',
  24. 'TABLE_ORDERS' => $aModule['db_prefix'] . 'orders',
  25. 'TABLE_ORDERS_DELETED' => $aModule['db_prefix'] . 'orders_deleted',
  26. 'TABLE_PROFILES' => $aModule['db_prefix'] . 'profiles',
  27. 'TABLE_HISTORY' => $aModule['db_prefix'] . 'history',
  28. 'TABLE_WITHDRAWALS' => $aModule['db_prefix'] . 'withdrawals',
  29. // database fields
  30. 'FIELD_ID' => 'id',
  31. 'FIELD_AUTHOR' => '',
  32. 'FIELD_ADDED' => 'added',
  33. 'FIELD_CHANGED' => '',
  34. 'FIELD_NAME' => 'name',
  35. 'FIELD_TITLE' => 'title',
  36. 'FIELD_DESCRIPTION' => 'description',
  37. 'FIELD_AMOUNT' => 'amount',
  38. 'FIELD_BONUS' => 'bonus',
  39. 'FIELD_PRICE' => 'price',
  40. 'FIELD_STATUS' => 'active',
  41. 'FIELD_C_CLEARED' => 'cleared',
  42. 'FIELD_C_AMOUNT' => 'amount',
  43. 'FIELD_C_MESSAGE' => 'message',
  44. 'FIELD_H_ID' => 'id',
  45. 'FIELD_H_DIRECTION' => 'direction',
  46. 'FIELD_H_DATE' => 'date',
  47. 'FIELD_H_CLEARED' => 'cleared',
  48. // page URIs
  49. 'URI_HOME' => 'credits-home',
  50. 'URL_HOME' => 'page.php?i=credits-home',
  51. 'URL_CHECKOUT' => 'page.php?i=credits-checkout',
  52. 'URL_ORDERS_COMMON' => 'page.php?i=credits-orders',
  53. 'URL_ORDERS_ADMINISTRATION' => 'page.php?i=credits-orders-administration',
  54. 'URL_HISTORY_COMMON' => 'page.php?i=credits-history-common',
  55. 'URL_HISTORY_ADMINISTRATION' => 'page.php?i=credits-history-administration',
  56. 'URL_WITHDRAWALS_COMMON' => 'page.php?i=credits-withdrawals-common',
  57. 'URL_WITHDRAWALS_ADMINISTRATION' => 'page.php?i=credits-withdrawals-administration',
  58. // some params
  59. 'PARAM_WITHDRAW' => 'bx_credits_enable_withdraw',
  60. 'PARAM_WITHDRAW_EMAIL' => 'bx_credits_withdraw_email',
  61. 'PARAM_WITHDRAW_CLEARING' => 'bx_credits_withdraw_clearing',
  62. 'PARAM_WITHDRAW_MINIMUM' => 'bx_credits_withdraw_minimum',
  63. 'PARAM_WITHDRAW_REMAINING' => 'bx_credits_withdraw_remaining',
  64. 'PARAM_PRECISION' => 'bx_credits_precision',
  65. 'PARAM_CR_USE' => 'bx_credits_conversion_rate_use',
  66. 'PARAM_CR_WITHDRAW' => 'bx_credits_conversion_rate_withdraw',
  67. 'PARAM_PROVIDER_NAME' => 'credits',
  68. 'PARAM_PROVIDER_ENABLE' => 'bx_credits_enable_provider',
  69. 'PARAM_CODE' => 'bx_credits_code',
  70. 'PARAM_ICON' => 'bx_credits_icon',
  71. // objects
  72. 'OBJECT_FORM_CREDIT' => 'bx_credits_credit',
  73. 'OBJECT_FORM_CREDIT_DISPLAY_GRANT' => 'bx_credits_credit_grant',
  74. 'OBJECT_FORM_CREDIT_DISPLAY_SEND' => 'bx_credits_credit_send',
  75. 'OBJECT_FORM_CREDIT_DISPLAY_WITHDRAW_REQUEST' => 'bx_credits_credit_withdraw_request',
  76. 'OBJECT_FORM_BUNDLE' => 'bx_credits_bundle',
  77. 'OBJECT_FORM_BUNDLE_DISPLAY_ADD' => 'bx_credits_bundle_add',
  78. 'OBJECT_FORM_BUNDLE_DISPLAY_EDIT' => 'bx_credits_bundle_edit',
  79. 'OBJECT_FORM_PROFILE' => 'bx_credits_profile',
  80. 'OBJECT_FORM_PROFILE_DISPLAY_EDIT' => 'bx_credits_profile_edit',
  81. 'OBJECT_MENU_SUBMENU' => 'bx_credits_submenu',
  82. 'OBJECT_MENU_MANAGE_SUBMENU' => 'bx_credits_manage_submenu',
  83. 'OBJECT_GRID_BUNDLES' => 'bx_credits_bundles',
  84. 'OBJECT_GRID_ORDERS_ADMINISTRATION' => 'bx_credits_orders_administration',
  85. 'OBJECT_GRID_ORDERS_COMMON' => 'bx_credits_orders_common',
  86. 'OBJECT_GRID_HISTORY_ADMINISTRATION' => 'bx_credits_history_administration',
  87. 'OBJECT_GRID_HISTORY_COMMON' => 'bx_credits_history_common',
  88. 'OBJECT_GRID_WITHDRAWALS_ADMINISTRATION' => 'bx_credits_withdrawals_administration',
  89. 'OBJECT_GRID_WITHDRAWALS_COMMON' => 'bx_credits_withdrawals_common',
  90. 'OBJECT_GRID_PROFILES_ADMINISTRATION' => 'bx_credits_profiles_administration',
  91. // email templates
  92. 'ETEMPLATE_GRANTED' => 'bx_credits_granted',
  93. 'ETEMPLATE_RECEIVED' => 'bx_credits_received',
  94. 'ETEMPLATE_PURCHASED' => 'bx_credits_purchased',
  95. 'ETEMPLATE_IN' => 'bx_credits_in',
  96. 'ETEMPLATE_OUT' => 'bx_credits_out',
  97. 'ETEMPLATE_WITHDRAW_REQUESTED' => 'bx_credits_withdraw_requested',
  98. 'ETEMPLATE_WITHDRAW_CANCELED' => 'bx_credits_withdraw_canceled',
  99. 'ETEMPLATE_WITHDRAW_SENT' => 'bx_credits_withdraw_sent',
  100. // some language keys
  101. 'T' => array (
  102. 'txt_sample_single' => '_bx_credits_txt_sample_single',
  103. 'grant_popup' => '_bx_credits_grid_popup_title_htr_grant',
  104. 'send_popup' => '_bx_credits_grid_popup_title_htr_send',
  105. 'withdraw_request_popup' => '_bx_credits_grid_popup_title_wdw_withdraw_request',
  106. ),
  107. );
  108. $this->_aJsClasses = array_merge($this->_aJsClasses, array(
  109. 'studio' => 'BxCreditsStudio',
  110. 'checkout' => 'BxCreditsCheckout',
  111. 'subscribe' => 'BxCreditsSubscribe',
  112. 'withdraw' => 'BxCreditsWithdraw',
  113. ));
  114. $this->_aJsObjects = array_merge($this->_aJsObjects, array(
  115. 'studio' => 'oBxCreditsStudio',
  116. 'checkout' => 'oBxCreditsCheckout',
  117. 'subscribe' => 'oBxCreditsSubscribe',
  118. 'withdraw' => 'oBxCreditsWithdraw',
  119. ));
  120. $sPrefix = str_replace('_', '-', $this->_sName);
  121. $this->_aHtmlIds = array(
  122. 'add_bundle_popup' => $sPrefix . '-add-bundle-popup',
  123. 'edit_bundle_popup' => $sPrefix . '-edit-bundle-popup',
  124. 'send_popup' => $sPrefix . '-send-popup',
  125. 'grant_popup' => $sPrefix . '-grant-popup',
  126. 'withdraw_request_popup' => $sPrefix . '-withdraw-request-popup',
  127. 'withdraw_confirm_popup' => $sPrefix . '-withdraw-confirm-popup',
  128. 'withdraw_field_rate' => $sPrefix . '-wff-rate',
  129. 'withdraw_field_amount' => $sPrefix . '-wff-amount',
  130. 'withdraw_field_result' => $sPrefix . '-wff-result',
  131. 'edit_profile_popup' => $sPrefix . '-edit-profile-popup',
  132. 'subscribe_popup' => $sPrefix . '-subscribe-popup',
  133. );
  134. $this->_aPrefixes = array(
  135. 'style' => 'bx-credits',
  136. );
  137. $oPayments = BxDolPayments::getInstance();
  138. $this->_iAuthor = (int)$oPayments->getOption('site_admin');
  139. $this->_aCurrency = array(
  140. 'code' => $oPayments->getOption('default_currency_code'),
  141. 'sign' => $oPayments->getOption('default_currency_sign')
  142. );
  143. $this->_sCheckoutSessionKey = $this->_sName . '_checkout';
  144. }
  145. public function getHtmlIds($sKey = '')
  146. {
  147. if(empty($sKey))
  148. return $this->_aHtmlIds;
  149. return isset($this->_aHtmlIds[$sKey]) ? $this->_aHtmlIds[$sKey] : '';
  150. }
  151. public function getAuthor()
  152. {
  153. return $this->_iAuthor;
  154. }
  155. public function getCurrency()
  156. {
  157. return $this->_aCurrency;
  158. }
  159. public function isWithdraw()
  160. {
  161. return getParam($this->CNF['PARAM_WITHDRAW']) == 'on';
  162. }
  163. public function getWithdrawEmail()
  164. {
  165. $sEmail = getParam($this->CNF['PARAM_WITHDRAW_EMAIL']);
  166. if(empty($sEmail))
  167. $sEmail = getParam('site_email');
  168. return $sEmail;
  169. }
  170. public function getWithdrawClearing()
  171. {
  172. return (int)getParam($this->CNF['PARAM_WITHDRAW_CLEARING']);
  173. }
  174. public function getWithdrawMinimum()
  175. {
  176. return (int)getParam($this->CNF['PARAM_WITHDRAW_MINIMUM']);
  177. }
  178. public function getWithdrawRemaining()
  179. {
  180. return (int)getParam($this->CNF['PARAM_WITHDRAW_REMAINING']);
  181. }
  182. public function getTransferTypesForClearing()
  183. {
  184. return [
  185. BX_CREDITS_TRANSFER_TYPE_PURCHASE,
  186. BX_CREDITS_TRANSFER_TYPE_CHECKOUT,
  187. BX_CREDITS_TRANSFER_TYPE_GRANT,
  188. BX_CREDITS_TRANSFER_TYPE_SEND,
  189. BX_CREDITS_TRANSFER_TYPE_SERVICE
  190. ];
  191. }
  192. public function getTransferTypesForSpending()
  193. {
  194. return [
  195. BX_CREDITS_TRANSFER_TYPE_CHECKOUT,
  196. BX_CREDITS_TRANSFER_TYPE_SEND,
  197. BX_CREDITS_TRANSFER_TYPE_WITHDRAW,
  198. BX_CREDITS_TRANSFER_TYPE_SERVICE
  199. ];
  200. }
  201. public function getPrecision()
  202. {
  203. return (int)getParam($this->CNF['PARAM_PRECISION']);
  204. }
  205. public function getConversionRateUse()
  206. {
  207. return $this->_getConversionRate('use');
  208. }
  209. public function getConversionRateWithdraw()
  210. {
  211. return $this->_getConversionRate('withdraw');
  212. }
  213. public function getCheckoutUrl()
  214. {
  215. return bx_absolute_url(BxDolPermalinks::getInstance()->permalink($this->CNF['URL_CHECKOUT']));
  216. }
  217. public function getBundleUrl($aBundle)
  218. {
  219. return bx_absolute_url(BxDolPermalinks::getInstance()->permalink($this->CNF['URL_HOME']));
  220. }
  221. public function getBundleName($sName)
  222. {
  223. return uriGenerate($sName, $this->CNF['TABLE_BUNDLES'], $this->CNF['FIELD_NAME'], ['empty' => 'cat', 'divider' => '_', 'lowercase' => false]);
  224. }
  225. public function getBundleDescription($aBundle)
  226. {
  227. if(($sDsc = _t($aBundle[$this->CNF['FIELD_DESCRIPTION']]) ?: false))
  228. return $sDsc;
  229. if(($iBns = (int)$aBundle[$this->CNF['FIELD_BONUS']] ?: false))
  230. return _t('_bx_credits_txt_n_plus_m_for_free', $aBundle[$this->CNF['FIELD_AMOUNT']], $iBns);
  231. return $aBundle[$this->CNF['FIELD_AMOUNT']];
  232. }
  233. public function getCheckoutData()
  234. {
  235. return BxDolSession::getInstance()->getUnsetValue($this->_sCheckoutSessionKey);
  236. }
  237. public function setCheckoutData($aData)
  238. {
  239. return BxDolSession::getInstance()->setValue($this->_sCheckoutSessionKey, $aData);
  240. }
  241. public function getOrder($iLength = 16)
  242. {
  243. return strtoupper(genRndPwd($iLength, false));
  244. }
  245. public function constructCheckoutCustomData()
  246. {
  247. $aParams = func_get_args();
  248. return urlencode(base64_encode(implode('|', $aParams)));
  249. }
  250. public function deconstructCheckoutCustomData($sData)
  251. {
  252. return explode('|', base64_decode(urldecode($sData)));
  253. }
  254. /**
  255. * Convert Credits to Money
  256. */
  257. public function convertC2M($fCredits, $fRate = false, $iPrecision = false)
  258. {
  259. if($fRate === false)
  260. $fRate = $this->getConversionRateUse();
  261. if($iPrecision === false)
  262. $iPrecision = $this->getPrecision();
  263. return round((float)$fCredits * $fRate, $iPrecision);
  264. }
  265. /**
  266. * Convert Money to Credits
  267. */
  268. public function convertM2C($fMoney, $fRate = false, $iPrecision = false)
  269. {
  270. if($fRate === false)
  271. $fRate = $this->getConversionRateUse();
  272. if($iPrecision === false)
  273. $iPrecision = $this->getPrecision();
  274. return round((float)$fMoney / $fRate, $iPrecision);
  275. }
  276. /*
  277. * Internal methods
  278. */
  279. protected function _getConversionRate($sType)
  280. {
  281. $fRate = (float)getParam($this->CNF['PARAM_CR_' . strtoupper($sType)]);
  282. return $fRate > 0 && $fRate <= 1 ? $fRate : 1;
  283. }
  284. }
  285. /** @} */