BxAclModule.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. bx_import('BxDolAcl');
  12. define('BX_ACL_LICENSE_TYPE_SINGLE', 'single'); //--- one-time payment license
  13. define('BX_ACL_LICENSE_TYPE_RECURRING', 'recurring'); //--- recurring payment license
  14. class BxAclModule extends BxDolModule
  15. {
  16. /**
  17. * Constructor
  18. */
  19. function __construct($aModule)
  20. {
  21. parent::__construct($aModule);
  22. $this->_oConfig->init($this->_oDb);
  23. }
  24. /**
  25. * ACTION METHODS
  26. */
  27. /**
  28. * SERVICE METHODS
  29. */
  30. public function serviceGetBlockView()
  31. {
  32. $sGrid = $this->_oConfig->getGridObject('view');
  33. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  34. if(!$oGrid)
  35. return '';
  36. return array(
  37. 'content' => $oGrid->getCode()
  38. );
  39. }
  40. public function serviceGetMembershipActions($iProfileId)
  41. {
  42. if($iProfileId != $this->getUserId())
  43. return '';
  44. return $this->_oTemplate->displayMembershipActions($iProfileId);
  45. }
  46. /**
  47. * Integration with Payment based modules.
  48. */
  49. public function serviceGetPaymentData()
  50. {
  51. return $this->_aModule;
  52. }
  53. public function serviceGetCartItem($iItemId)
  54. {
  55. $CNF = &$this->_oConfig->CNF;
  56. if(!$iItemId)
  57. return array();
  58. $aItem = $this->_oDb->getPrices(array('type' => 'by_id_full', 'value' => $iItemId));
  59. if(empty($aItem) || !is_array($aItem))
  60. return array();
  61. return array (
  62. 'id' => $aItem['id'],
  63. 'author_id' => $this->_oConfig->getOwner(),
  64. 'name' => $aItem['name'],
  65. 'title' => _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']),
  66. 'description' => _t($aItem['level_description']),
  67. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']),
  68. 'price_single' => $aItem['price'],
  69. 'price_recurring' => $aItem['price'],
  70. );
  71. }
  72. public function serviceGetCartItems($iSellerId)
  73. {
  74. $CNF = &$this->_oConfig->CNF;
  75. $iSellerId = $this->_oConfig->getOwner();
  76. $aItems = $this->_oDb->getPrices(array('type' => 'all_full'));
  77. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']);
  78. $aResult = array();
  79. foreach($aItems as $aItem)
  80. $aResult[] = array(
  81. 'id' => $aItem['id'],
  82. 'author_id' => $iSellerId,
  83. 'name' => $aItem['name'],
  84. 'title' => _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']),
  85. 'description' => _t($aItem['level_description']),
  86. 'url' => $sUrl,
  87. 'price_single' => $aItem['price'],
  88. 'price_recurring' => $aItem['price'],
  89. );
  90. return $aResult;
  91. }
  92. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  93. {
  94. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  95. }
  96. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  97. {
  98. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  99. }
  100. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  101. {
  102. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  103. }
  104. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  105. {
  106. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  107. }
  108. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  109. {
  110. $aItem = $this->serviceGetCartItem($iItemId);
  111. if(empty($aItem) || !is_array($aItem))
  112. return array();
  113. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  114. if(!BxDolAcl::getInstance()->setMembership($iClientId, $aItemInfo['level_id'], array('period' => $aItemInfo['period'], 'period_unit' => $aItemInfo['period_unit']), false, $sLicense))
  115. return array();
  116. return $aItem;
  117. }
  118. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  119. {
  120. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  121. if(empty($aItemInfo) || !is_array($aItemInfo))
  122. return false;
  123. return BxDolAcl::getInstance()->unsetMembership($iClientId, $aItemInfo['level_id'], $sLicense);
  124. }
  125. /**
  126. * COMMON METHODS
  127. */
  128. public function getUserId()
  129. {
  130. return isLogged() ? bx_get_logged_profile_id() : 0;
  131. }
  132. public function getUserInfo($iUserId = 0)
  133. {
  134. $oProfile = BxDolProfile::getInstance($iUserId);
  135. if (!$oProfile)
  136. $oProfile = BxDolProfileUndefined::getInstance();
  137. return array(
  138. $oProfile->getDisplayName(),
  139. $oProfile->getUrl(),
  140. $oProfile->getThumb(),
  141. $oProfile->getUnit()
  142. );
  143. }
  144. }
  145. /** @} */