1
0

BxAclGridView.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 PaidLevels Paid Levels
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once('BxAclGridLevels.php');
  12. class BxAclGridView extends BxAclGridLevels
  13. {
  14. /**
  15. * Array with check_sum => JS_code pairs of all JS codes
  16. * which should be added to the page.
  17. */
  18. protected $_aJsCodes;
  19. protected $_iLogged;
  20. protected $_iOwner;
  21. protected $_oPayment;
  22. protected $_bTypeSingle;
  23. protected $_bTypeRecurring;
  24. public function __construct ($aOptions, $oTemplate = false)
  25. {
  26. parent::__construct ($aOptions, $oTemplate);
  27. $this->_aJsCodes = array();
  28. $this->_iLogged = bx_get_logged_profile_id();
  29. $this->_iOwner = $this->_oModule->_oConfig->getOwner();
  30. $this->_oPayment = BxDolPayments::getInstance();
  31. $this->_bTypeSingle = $this->_oPayment->isAcceptingPayments($this->_iOwner, BX_PAYMENT_TYPE_SINGLE);
  32. $this->_bTypeRecurring = !$this->_oPayment->isCreditsOnly() && $this->_oPayment->isAcceptingPayments($this->_iOwner, BX_PAYMENT_TYPE_RECURRING);
  33. }
  34. public function performActionChoose()
  35. {
  36. if(!$this->_iLogged)
  37. return $this->_bIsApi ? [] : echoJson([]);
  38. $aIds = $this->_getIds();
  39. if($aIds === false)
  40. return $this->_bIsApi ? [] : echoJson([]);
  41. $aItem = $this->_oModule->_oDb->getPrices(['type' => 'by_id', 'value' => $aIds[0]]);
  42. if(!is_array($aItem) || empty($aItem) || (float)$aItem['price'] != 0)
  43. return $this->_bIsApi ? [] : echoJson([]);
  44. $oAcl = BxDolAcl::getInstance();
  45. $iUserId = bx_get_logged_profile_id();
  46. $aMembershipInfo = $oAcl->getMemberMembershipInfo($iUserId);
  47. $aResult = [];
  48. if($oAcl->setMembership($iUserId, $aItem['level_id'], ['period' => $aItem['period'], 'period_unit' => $aItem['period_unit']], true)) {
  49. $this->_oModule->cancelSubscription($iUserId, $aMembershipInfo);
  50. $aResult = ['grid' => $this->getCode(false), 'blink' => $aItem['id'], 'msg' => _t('_bx_acl_msg_performed')];
  51. }
  52. else
  53. $aResult = ['msg' => _t('_bx_acl_err_cannot_perform')];
  54. return $this->_bIsApi ? [] : echoJson($aResult);
  55. }
  56. public function performActionBuy()
  57. {
  58. if(!$this->_iLogged)
  59. return $this->_bIsApi ? [] : echoJson([]);
  60. if(!$this->_bIsApi)
  61. return echoJson([]);
  62. $aIds = $this->_getIds();
  63. if($aIds === false || !is_array($aIds))
  64. return [];
  65. if(($sDescriptor = array_shift($aIds))) {
  66. $aDescriptor = $this->_oPayment->getActiveObject()->_oConfig->descriptorS2A($sDescriptor);
  67. if($aDescriptor && is_array($aDescriptor) && (int)$aDescriptor[0] == $this->_iOwner) {
  68. $aResult = $this->_oPayment->addToCart($this->_iOwner, $this->MODULE, (int)$aDescriptor[2], 1, true);
  69. if(isset($aResult['code']) && (int)$aResult['code'] != 0)
  70. return [bx_api_get_msg($aResult['message'])];
  71. }
  72. }
  73. return [];
  74. }
  75. public function performActionSubscribe()
  76. {
  77. if(!$this->_iLogged)
  78. return $this->_bIsApi ? [] : echoJson([]);
  79. if(!$this->_bIsApi)
  80. return echoJson([]);
  81. $aIds = $this->_getIds();
  82. if($aIds === false)
  83. return [];
  84. //TODO: Payment Provider selector should be realized.
  85. $aResult = $this->_oPayment->subscribeWithAddons($this->_iOwner, 'stripe_v3', $this->MODULE, array_shift($aIds), 1, true);
  86. if(isset($aResult['code']) && (int)$aResult['code'] != 0)
  87. return [bx_api_get_msg($aResult['message'])];
  88. return [];
  89. }
  90. public function getCode($isDisplayHeader = true)
  91. {
  92. return parent::getCode($isDisplayHeader) . $this->getJsCode();
  93. }
  94. public function getJsCode()
  95. {
  96. if(empty($this->_aJsCodes) || !is_array($this->_aJsCodes))
  97. return '';
  98. return implode('', $this->_aJsCodes);
  99. }
  100. public function decodeDataAPI($aData)
  101. {
  102. $aResults = parent::decodeDataAPI($aData);
  103. foreach($aResults as $iKey => $aResult)
  104. if(($iMemLevelId = $aData[$iKey]['level_id'] ?? false) !== false)
  105. $aResults[$iKey]['level_id'] = $iMemLevelId;
  106. return $aResults;
  107. }
  108. protected function _getCellLevelIcon($mixedValue, $sKey, $aField, $aRow)
  109. {
  110. $mixedValue = $this->_oModule->_oTemplate->displayLevelIcon($mixedValue);
  111. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  112. }
  113. protected function _getCellLevelName($mixedValue, $sKey, $aField, $aRow)
  114. {
  115. if(!$this->_bIsApi)
  116. $mixedValue = $this->_oModule->_oTemplate->parseLink('javascript:void(0)', $mixedValue, [
  117. 'onclick' => $this->_oModule->_oConfig->getJsObject('main') . '.viewActions(this, ' . $aRow['level_id'] . ')'
  118. ]);
  119. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  120. }
  121. protected function _getActionChoose ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  122. {
  123. if((float)$aRow['price'] != 0 || BxDolAcl::getInstance()->isMemberLevelInSet(array($aRow['level_id'])))
  124. return $this->_bIsApi ? [] : '';
  125. if($this->_bIsApi)
  126. return array_merge($a, ['name' => $sKey, 'type' => 'callback', 'on_callback' => 'hide']);
  127. if(!$this->_iLogged)
  128. $a['attr'] = array_merge($a['attr'], [
  129. 'onclick' => "window.open('" . $this->_getLoginUrl() . "','_self');"
  130. ]);
  131. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  132. }
  133. protected function _getActionBuy ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  134. {
  135. $CNF = &$this->_oModule->_oConfig->CNF;
  136. if((float)$aRow['price'] == 0 || !$this->_bTypeSingle || ($this->_bTypeRecurring && getParam($CNF['PARAM_RECURRING_PRIORITIZE']) == 'on' && !$this->_isLifetime($aRow)))
  137. return $this->_bIsApi ? [] : '';
  138. if($this->_bIsApi)
  139. return array_merge($a, [
  140. 'name' => $sKey,
  141. 'type' => 'callback',
  142. 'on_callback' => 'redirect',
  143. 'redirect_url' => bx_api_get_relative_url($this->_oPayment->getCartUrl($this->_iOwner)),
  144. 'payment_type' => BX_PAYMENT_TYPE_SINGLE,
  145. 'seller_id' => $this->_iOwner,
  146. 'items' => [$this->_oPayment->getCartItemDescriptor($this->_iOwner, $this->_oModule->_oConfig->getId(), $aRow['id'], 1)],
  147. ]);
  148. if($this->_iLogged) {
  149. $aJs = $this->_oPayment->getAddToCartJs($this->_iOwner, $this->MODULE, $aRow['id'], 1, true);
  150. if(!empty($aJs) && is_array($aJs)) {
  151. list($sJsCode, $sJsMethod) = $aJs;
  152. $sJsCodeCheckSum = md5($sJsCode);
  153. if(!isset($this->_aJsCodes[$sJsCodeCheckSum]))
  154. $this->_aJsCodes[$sJsCodeCheckSum] = $sJsCode;
  155. $a['attr'] = array(
  156. 'title' => bx_html_attribute(_t('_bx_acl_grid_action_buy_title')),
  157. 'onclick' => $sJsMethod
  158. );
  159. }
  160. }
  161. else
  162. $a['attr'] = array_merge($a['attr'], [
  163. 'onclick' => "window.open('" . $this->_getLoginUrl() . "','_self');"
  164. ]);
  165. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  166. }
  167. protected function _getActionSubscribe ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  168. {
  169. if((float)$aRow['price'] == 0 || !$this->_bTypeRecurring || ($this->_bTypeSingle && $this->_isLifetime($aRow)))
  170. return $this->_bIsApi ? [] : '';
  171. if($this->_bIsApi)
  172. return array_merge($a, [
  173. 'name' => $sKey,
  174. 'type' => 'object',
  175. 'object_name' => 'stripe_v3',
  176. 'payment_type' => BX_PAYMENT_TYPE_RECURRING,
  177. 'seller_id' => $this->_iOwner,
  178. 'items' => [$this->_oPayment->getCartItemDescriptor($this->_iOwner, $this->_oModule->_oConfig->getId(), $aRow['id'], 1)],
  179. ]);
  180. if($this->_iLogged) {
  181. $aJs = $this->_oPayment->getSubscribeJs($this->_iOwner, '', $this->MODULE, $aRow['id'], 1);
  182. if(!empty($aJs) && is_array($aJs)) {
  183. list($sJsCode, $sJsMethod) = $aJs;
  184. $sJsCodeCheckSum = md5($sJsCode);
  185. if(!isset($this->_aJsCodes[$sJsCodeCheckSum]))
  186. $this->_aJsCodes[$sJsCodeCheckSum] = $sJsCode;
  187. $a['attr'] = array(
  188. 'title' => bx_html_attribute(_t('_bx_acl_grid_action_subscribe_title')),
  189. 'onclick' => $sJsMethod
  190. );
  191. }
  192. }
  193. else
  194. $a['attr'] = array_merge($a['attr'], [
  195. 'onclick' => "window.open('" . $this->_getLoginUrl() . "','_self');"
  196. ]);
  197. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  198. }
  199. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  200. {
  201. if($this->_iLogged && ($oLogged = BxDolProfile::getInstance($this->_iLogged)) !== false)
  202. $this->_aOptions['source'] .= "AND `tl`.`UnavailableTo` NOT LIKE " . $this->_oModule->_oDb->escape('%|' . $oLogged->getModule() . '|%') . " ";
  203. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);;
  204. }
  205. protected function _isLifetime($aRow)
  206. {
  207. return empty($aRow['period']) && empty($aRow['period_unit']);
  208. }
  209. protected function _getLoginUrl()
  210. {
  211. return bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=login'));
  212. }
  213. }
  214. /** @} */