BxAclGridView.php 9.4 KB

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