BxBaseModGroupsGridPricesView.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. $CNF = &$this->_oModule->_oConfig->CNF;
  29. $this->_aJsCodes = array();
  30. $this->_oPayment = BxDolPayments::getInstance();
  31. $this->_iSeller = $this->_aGroupContentInfo[$CNF['FIELD_AUTHOR']];
  32. $this->_iClient = bx_get_logged_profile_id();
  33. $this->_iClientRole = $this->_oModule->getRole($this->_iGroupProfileId, $this->_iClient);
  34. $this->_bTypeSingle = $this->_oPayment->isAcceptingPayments($this->_iSeller, BX_PAYMENT_TYPE_SINGLE);
  35. $this->_bTypeRecurring = $this->_oPayment->isAcceptingPayments($this->_iSeller, BX_PAYMENT_TYPE_RECURRING);
  36. }
  37. public function performActionChoose()
  38. {
  39. $CNF = &$this->_oModule->_oConfig->CNF;
  40. $aIds = $this->_getIds();
  41. if($aIds === false)
  42. return echoJson(array());
  43. $aItem = $this->_oModule->_oDb->getPrices(array('type' => 'by_id', 'value' => $aIds[0]));
  44. if(!is_array($aItem) || empty($aItem) || (float)$aItem['price'] != 0)
  45. return echoJson(array());
  46. $aResult = array();
  47. if($this->_oModule->setRole($this->_iGroupProfileId, $this->_iClient, $aItem['role_id'], array('period' => $aItem['period'], 'period_unit' => $aItem['period_unit'])))
  48. $aResult = array('grid' => $this->getCode(false), 'blink' => $aItem['id'], 'msg' => _t($CNF['T']['msg_performed']));
  49. else
  50. $aResult = array('msg' => _t($CNF['T']['err_cannot_perform']));
  51. return echoJson($aResult);
  52. }
  53. public function getCode($isDisplayHeader = true)
  54. {
  55. return parent::getCode($isDisplayHeader) . $this->getJsCode();
  56. }
  57. public function getJsCode()
  58. {
  59. if(empty($this->_aJsCodes) || !is_array($this->_aJsCodes))
  60. return '';
  61. return implode('', $this->_aJsCodes);
  62. }
  63. protected function _getCellRoleId($mixedValue, $sKey, $aField, $aRow)
  64. {
  65. return parent::_getCellDefault($this->_aRoles[$mixedValue], $sKey, $aField, $aRow);
  66. }
  67. protected function _getActionChoose ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  68. {
  69. if((float)$aRow['price'] != 0 || $this->_oModule->getRole($this->_iGroupProfileId, $this->_iClient) === (int)$aRow['role_id'])
  70. return '';
  71. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  72. }
  73. protected function _getActionBuy ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  74. {
  75. $CNF = &$this->_oModule->_oConfig->CNF;
  76. if((float)$aRow['price'] == 0 || !$this->_bTypeSingle || $this->_bTypeRecurring)
  77. return '';
  78. $aJs = $this->_oPayment->getAddToCartJs($this->_iSeller, $this->_sModule, $aRow['id'], 1, true);
  79. if(!empty($aJs) && is_array($aJs)) {
  80. list($sJsCode, $sJsMethod) = $aJs;
  81. $sJsCodeCheckSum = md5($sJsCode);
  82. if(!isset($this->_aJsCodes[$sJsCodeCheckSum]))
  83. $this->_aJsCodes[$sJsCodeCheckSum] = $sJsCode;
  84. $a['attr'] = array(
  85. 'title' => bx_html_attribute(_t($CNF['T']['txt_buy_title'])),
  86. 'onclick' => $sJsMethod
  87. );
  88. }
  89. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  90. }
  91. protected function _getActionSubscribe ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  92. {
  93. $CNF = &$this->_oModule->_oConfig->CNF;
  94. if((float)$aRow['price'] == 0 || !$this->_bTypeRecurring)
  95. return '';
  96. $aJs = $this->_oPayment->getSubscribeJs($this->_iSeller, '', $this->_sModule, $aRow['id'], 1);
  97. if(!empty($aJs) && is_array($aJs)) {
  98. list($sJsCode, $sJsMethod) = $aJs;
  99. $sJsCodeCheckSum = md5($sJsCode);
  100. if(!isset($this->_aJsCodes[$sJsCodeCheckSum]))
  101. $this->_aJsCodes[$sJsCodeCheckSum] = $sJsCode;
  102. $a['attr'] = array(
  103. 'title' => bx_html_attribute(_t($CNF['T']['txt_subscribe_title'])),
  104. 'onclick' => $sJsMethod
  105. );
  106. }
  107. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  108. }
  109. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  110. {
  111. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString("AND `profile_id`=? ", $this->_iGroupProfileId);
  112. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);;
  113. }
  114. }
  115. /** @} */