BxBaseModGroupsGridPricesView.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 BaseGroups Base classes for groups modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once('BxBaseModGroupsGridPrices.php');
  12. class BxBaseModGroupsGridPricesView extends BxBaseModGroupsGridPrices
  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 $_oPayment;
  20. protected $_bTypeSingle;
  21. protected $_bTypeRecurring;
  22. protected $_iSeller;
  23. protected $_iClient;
  24. protected $_iClientRole;
  25. public function __construct ($aOptions, $oTemplate = false)
  26. {
  27. parent::__construct ($aOptions, $oTemplate);
  28. $this->_aJsCodes = array();
  29. }
  30. public function setClientId($iClientId = 0)
  31. {
  32. $this->_iClient = $iClientId;
  33. if(empty($this->_iClient))
  34. $this->_iClient = bx_get_logged_profile_id();
  35. $this->_iClientRole = 0;
  36. if(!empty($this->_iGroupProfileId))
  37. $this->_iClientRole = $this->_oModule->getRole($this->_iGroupProfileId, $this->_iClient);
  38. }
  39. public function setSellerId($iSellerId = 0)
  40. {
  41. $CNF = &$this->_oModule->_oConfig->CNF;
  42. $this->_iSeller = $iSellerId;
  43. if(empty($this->_iSeller) && !empty($this->_aGroupContentInfo) && is_array($this->_aGroupContentInfo))
  44. $this->_iSeller = $this->_aGroupContentInfo[$CNF['FIELD_AUTHOR']];
  45. if(empty($this->_oPayment))
  46. $this->_oPayment = BxDolPayments::getInstance();
  47. $this->_bTypeSingle = $this->_oPayment->isAcceptingPayments($this->_iSeller, BX_PAYMENT_TYPE_SINGLE);
  48. $this->_bTypeRecurring = $this->_oPayment->isAcceptingPayments($this->_iSeller, BX_PAYMENT_TYPE_RECURRING);
  49. }
  50. public function setProfileId($iProfileId)
  51. {
  52. parent::setProfileId($iProfileId);
  53. $this->setClientId();
  54. $this->setSellerId();
  55. }
  56. public function performActionChoose()
  57. {
  58. $CNF = &$this->_oModule->_oConfig->CNF;
  59. $aIds = $this->_getIds();
  60. if($aIds === false)
  61. return $this->_bIsApi ? [] : echoJson([]);
  62. $aItem = $this->_oModule->_oDb->getPrices(['type' => 'by_id', 'value' => $aIds[0]]);
  63. if(!is_array($aItem) || empty($aItem) || (float)$aItem['price'] != 0)
  64. return $this->_bIsApi ? [] : echoJson([]);
  65. $aResult = [];
  66. if($this->_oModule->setRole($this->_iGroupProfileId, $this->_iClient, $aItem['role_id'], ['period' => $aItem['period'], 'period_unit' => $aItem['period_unit']]))
  67. $aResult = ['grid' => $this->getCode(false), 'blink' => $aItem['id'], 'msg' => _t($CNF['T']['msg_performed'])];
  68. else
  69. $aResult = ['msg' => _t($CNF['T']['err_cannot_perform'])];
  70. return $this->_bIsApi ? [] : echoJson($aResult);
  71. }
  72. public function performActionBuy()
  73. {
  74. if(!$this->_bIsApi)
  75. return echoJson([]);
  76. $aIds = $this->_getIds();
  77. if($aIds === false)
  78. return [];
  79. $aResult = $this->_oPayment->addToCart($this->_iSeller, $this->_sModule, array_shift($aIds), 1, true);
  80. if(isset($aResult['code']) && (int)$aResult['code'] != 0)
  81. return [bx_api_get_msg($aResult['message'])];
  82. return [];
  83. }
  84. public function performActionSubscribe()
  85. {
  86. if(!$this->_bIsApi)
  87. return echoJson([]);
  88. $aIds = $this->_getIds();
  89. if($aIds === false)
  90. return [];
  91. //TODO: Payment Provider selector should be realized.
  92. $aResult = $this->_oPayment->subscribeWithAddons($this->_iSeller, 'stripe_v3', $this->_sModule, array_shift($aIds), 1, true);
  93. if(isset($aResult['code']) && (int)$aResult['code'] != 0)
  94. return [bx_api_get_msg($aResult['message'])];
  95. return [];
  96. }
  97. public function getCode($isDisplayHeader = true)
  98. {
  99. return parent::getCode($isDisplayHeader) . $this->getJsCode();
  100. }
  101. public function getJsCode()
  102. {
  103. if(empty($this->_aJsCodes) || !is_array($this->_aJsCodes))
  104. return '';
  105. return implode('', $this->_aJsCodes);
  106. }
  107. protected function _getCellRoleId($mixedValue, $sKey, $aField, $aRow)
  108. {
  109. return parent::_getCellDefault($this->_aRoles[$mixedValue], $sKey, $aField, $aRow);
  110. }
  111. protected function _getActionChoose ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  112. {
  113. if((float)$aRow['price'] != 0 || $this->_oModule->getRole($this->_iGroupProfileId, $this->_iClient) === (int)$aRow['role_id'])
  114. return $this->_bIsApi ? [] : '';
  115. if($this->_bIsApi)
  116. return array_merge($a, ['name' => $sKey, 'type' => 'callback', 'on_callback' => 'hide']);
  117. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  118. }
  119. protected function _getActionBuy ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  120. {
  121. $CNF = &$this->_oModule->_oConfig->CNF;
  122. if((float)$aRow['price'] == 0 || !$this->_bTypeSingle || ($this->_bTypeRecurring && !$this->_isLifetime($aRow)))
  123. return $this->_bIsApi ? [] : '';
  124. if($this->_bIsApi)
  125. return array_merge($a, [
  126. 'name' => $sKey,
  127. 'type' => 'callback',
  128. 'on_callback' => 'redirect',
  129. 'redirect_url' => bx_api_get_relative_url($this->_oPayment->getCartUrl($this->_iSeller))
  130. ]);
  131. $aJs = $this->_oPayment->getAddToCartJs($this->_iSeller, $this->_sModule, $aRow['id'], 1, true);
  132. if(!empty($aJs) && is_array($aJs)) {
  133. list($sJsCode, $sJsMethod) = $aJs;
  134. $sJsCodeCheckSum = md5($sJsCode);
  135. if(!isset($this->_aJsCodes[$sJsCodeCheckSum]))
  136. $this->_aJsCodes[$sJsCodeCheckSum] = $sJsCode;
  137. $a['attr'] = array(
  138. 'title' => bx_html_attribute(_t($CNF['T']['txt_buy_title'])),
  139. 'onclick' => $sJsMethod
  140. );
  141. }
  142. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  143. }
  144. protected function _getActionSubscribe ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  145. {
  146. $CNF = &$this->_oModule->_oConfig->CNF;
  147. if((float)$aRow['price'] == 0 || !$this->_bTypeRecurring || ($this->_bTypeSingle && $this->_isLifetime($aRow)))
  148. return $this->_bIsApi ? [] : '';
  149. if($this->_bIsApi)
  150. return array_merge($a, [
  151. 'name' => $sKey,
  152. 'type' => 'object',
  153. 'object_name' => 'stripe_v3',
  154. 'seller_id' => $this->_iSeller,
  155. 'items' => [$this->_oPayment->getCartItemDescriptor($this->_iSeller, $this->_oModule->_oConfig->getId(), $aRow['id'], 1)],
  156. ]);
  157. $aJs = $this->_oPayment->getSubscribeJs($this->_iSeller, '', $this->_sModule, $aRow['id'], 1);
  158. if(!empty($aJs) && is_array($aJs)) {
  159. list($sJsCode, $sJsMethod) = $aJs;
  160. $sJsCodeCheckSum = md5($sJsCode);
  161. if(!isset($this->_aJsCodes[$sJsCodeCheckSum]))
  162. $this->_aJsCodes[$sJsCodeCheckSum] = $sJsCode;
  163. $a['attr'] = array(
  164. 'title' => bx_html_attribute(_t($CNF['T']['txt_subscribe_title'])),
  165. 'onclick' => $sJsMethod
  166. );
  167. }
  168. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  169. }
  170. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  171. {
  172. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString("AND `profile_id`=? ", $this->_iGroupProfileId);
  173. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);;
  174. }
  175. protected function _isLifetime($aRow)
  176. {
  177. return empty($aRow['period']) && empty($aRow['period_unit']);
  178. }
  179. }
  180. /** @} */