BxBaseModGroupsGridPricesView.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 echoJson(array());
  62. $aItem = $this->_oModule->_oDb->getPrices(array('type' => 'by_id', 'value' => $aIds[0]));
  63. if(!is_array($aItem) || empty($aItem) || (float)$aItem['price'] != 0)
  64. return echoJson(array());
  65. $aResult = array();
  66. if($this->_oModule->setRole($this->_iGroupProfileId, $this->_iClient, $aItem['role_id'], array('period' => $aItem['period'], 'period_unit' => $aItem['period_unit'])))
  67. $aResult = array('grid' => $this->getCode(false), 'blink' => $aItem['id'], 'msg' => _t($CNF['T']['msg_performed']));
  68. else
  69. $aResult = array('msg' => _t($CNF['T']['err_cannot_perform']));
  70. return echoJson($aResult);
  71. }
  72. public function getCode($isDisplayHeader = true)
  73. {
  74. return parent::getCode($isDisplayHeader) . $this->getJsCode();
  75. }
  76. public function getJsCode()
  77. {
  78. if(empty($this->_aJsCodes) || !is_array($this->_aJsCodes))
  79. return '';
  80. return implode('', $this->_aJsCodes);
  81. }
  82. protected function _getCellRoleId($mixedValue, $sKey, $aField, $aRow)
  83. {
  84. return parent::_getCellDefault($this->_aRoles[$mixedValue], $sKey, $aField, $aRow);
  85. }
  86. protected function _getActionChoose ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  87. {
  88. if((float)$aRow['price'] != 0 || $this->_oModule->getRole($this->_iGroupProfileId, $this->_iClient) === (int)$aRow['role_id'])
  89. return '';
  90. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  91. }
  92. protected function _getActionBuy ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  93. {
  94. $CNF = &$this->_oModule->_oConfig->CNF;
  95. if((float)$aRow['price'] == 0 || !$this->_bTypeSingle || ($this->_bTypeRecurring && !$this->_isLifetime($aRow)))
  96. return '';
  97. $aJs = $this->_oPayment->getAddToCartJs($this->_iSeller, $this->_sModule, $aRow['id'], 1, true);
  98. if(!empty($aJs) && is_array($aJs)) {
  99. list($sJsCode, $sJsMethod) = $aJs;
  100. $sJsCodeCheckSum = md5($sJsCode);
  101. if(!isset($this->_aJsCodes[$sJsCodeCheckSum]))
  102. $this->_aJsCodes[$sJsCodeCheckSum] = $sJsCode;
  103. $a['attr'] = array(
  104. 'title' => bx_html_attribute(_t($CNF['T']['txt_buy_title'])),
  105. 'onclick' => $sJsMethod
  106. );
  107. }
  108. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  109. }
  110. protected function _getActionSubscribe ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  111. {
  112. $CNF = &$this->_oModule->_oConfig->CNF;
  113. if((float)$aRow['price'] == 0 || !$this->_bTypeRecurring || ($this->_bTypeSingle && $this->_isLifetime($aRow)))
  114. return '';
  115. $aJs = $this->_oPayment->getSubscribeJs($this->_iSeller, '', $this->_sModule, $aRow['id'], 1);
  116. if(!empty($aJs) && is_array($aJs)) {
  117. list($sJsCode, $sJsMethod) = $aJs;
  118. $sJsCodeCheckSum = md5($sJsCode);
  119. if(!isset($this->_aJsCodes[$sJsCodeCheckSum]))
  120. $this->_aJsCodes[$sJsCodeCheckSum] = $sJsCode;
  121. $a['attr'] = array(
  122. 'title' => bx_html_attribute(_t($CNF['T']['txt_subscribe_title'])),
  123. 'onclick' => $sJsMethod
  124. );
  125. }
  126. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  127. }
  128. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  129. {
  130. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString("AND `profile_id`=? ", $this->_iGroupProfileId);
  131. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);;
  132. }
  133. protected function _isLifetime($aRow)
  134. {
  135. return empty($aRow['period']) && empty($aRow['period_unit']);
  136. }
  137. }
  138. /** @} */