1
0

BxAclGridView.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 $_iOwner;
  20. protected $_oPayment;
  21. protected $_bTypeSingle;
  22. protected $_bTypeRecurring;
  23. public function __construct ($aOptions, $oTemplate = false)
  24. {
  25. parent::__construct ($aOptions, $oTemplate);
  26. $this->_aJsCodes = array();
  27. $this->_iOwner = $this->_oModule->_oConfig->getOwner();
  28. $this->_oPayment = BxDolPayments::getInstance();
  29. $this->_bTypeSingle = $this->_oPayment->isAcceptingPayments($this->_iOwner, BX_PAYMENT_TYPE_SINGLE);
  30. $this->_bTypeRecurring = !$this->_oPayment->isCreditsOnly() && $this->_oPayment->isAcceptingPayments($this->_iOwner, BX_PAYMENT_TYPE_RECURRING);
  31. }
  32. public function performActionChoose()
  33. {
  34. $CNF = &$this->_oModule->_oConfig->CNF;
  35. $aIds = $this->_getIds();
  36. if($aIds === false)
  37. return echoJson(array());
  38. $aItem = $this->_oModule->_oDb->getPrices(array('type' => 'by_id', 'value' => $aIds[0]));
  39. if(!is_array($aItem) || empty($aItem) || (float)$aItem['price'] != 0)
  40. return echoJson(array());
  41. $aResult = array();
  42. $iUserId = bx_get_logged_profile_id();
  43. if(BxDolAcl::getInstance()->setMembership($iUserId, $aItem['level_id'], array('period' => $aItem['period'], 'period_unit' => $aItem['period_unit']), true))
  44. $aResult = array('grid' => $this->getCode(false), 'blink' => $aItem['id'], 'msg' => _t('_bx_acl_msg_performed'));
  45. else
  46. $aResult = array('msg' => _t('_bx_acl_err_cannot_perform'));
  47. return echoJson($aResult);
  48. }
  49. public function getCode($isDisplayHeader = true)
  50. {
  51. return parent::getCode($isDisplayHeader) . $this->getJsCode();
  52. }
  53. public function getJsCode()
  54. {
  55. if(empty($this->_aJsCodes) || !is_array($this->_aJsCodes))
  56. return '';
  57. return implode('', $this->_aJsCodes);
  58. }
  59. protected function _getCellLevelIcon($mixedValue, $sKey, $aField, $aRow)
  60. {
  61. $mixedValue = $this->_oModule->_oTemplate->displayLevelIcon($mixedValue);
  62. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  63. }
  64. protected function _getActionChoose ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  65. {
  66. if((float)$aRow['price'] != 0 || BxDolAcl::getInstance()->isMemberLevelInSet(array($aRow['level_id'])))
  67. return '';
  68. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  69. }
  70. protected function _getActionBuy ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  71. {
  72. $CNF = &$this->_oModule->_oConfig->CNF;
  73. if((float)$aRow['price'] == 0 || !$this->_bTypeSingle || ($this->_bTypeRecurring && getParam($CNF['PARAM_RECURRING_PRIORITIZE']) == 'on'))
  74. return '';
  75. $aJs = $this->_oPayment->getAddToCartJs($this->_iOwner, $this->MODULE, $aRow['id'], 1, true);
  76. if(!empty($aJs) && is_array($aJs)) {
  77. list($sJsCode, $sJsMethod) = $aJs;
  78. $sJsCodeCheckSum = md5($sJsCode);
  79. if(!isset($this->_aJsCodes[$sJsCodeCheckSum]))
  80. $this->_aJsCodes[$sJsCodeCheckSum] = $sJsCode;
  81. $a['attr'] = array(
  82. 'title' => bx_html_attribute(_t('_bx_acl_grid_action_buy_title')),
  83. 'onclick' => $sJsMethod
  84. );
  85. }
  86. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  87. }
  88. protected function _getActionSubscribe ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  89. {
  90. if((float)$aRow['price'] == 0 || !$this->_bTypeRecurring)
  91. return '';
  92. $aJs = $this->_oPayment->getSubscribeJs($this->_iOwner, '', $this->MODULE, $aRow['id'], 1);
  93. if(!empty($aJs) && is_array($aJs)) {
  94. list($sJsCode, $sJsMethod) = $aJs;
  95. $sJsCodeCheckSum = md5($sJsCode);
  96. if(!isset($this->_aJsCodes[$sJsCodeCheckSum]))
  97. $this->_aJsCodes[$sJsCodeCheckSum] = $sJsCode;
  98. $a['attr'] = array(
  99. 'title' => bx_html_attribute(_t('_bx_acl_grid_action_subscribe_title')),
  100. 'onclick' => $sJsMethod
  101. );
  102. }
  103. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  104. }
  105. }
  106. /** @} */