BxAclModule.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. $this->_oTemplate->addCss(array('view.css'));
  37. return array(
  38. 'content' => $oGrid->getCode()
  39. );
  40. }
  41. public function serviceGetMembershipActions($iProfileId)
  42. {
  43. if($iProfileId != $this->getUserId())
  44. return '';
  45. return $this->_oTemplate->displayMembershipActions($iProfileId);
  46. }
  47. /**
  48. * Integration with Payment based modules.
  49. */
  50. public function serviceGetPaymentData()
  51. {
  52. return $this->_aModule;
  53. }
  54. public function serviceGetCartItem($iItemId)
  55. {
  56. $CNF = &$this->_oConfig->CNF;
  57. if(!$iItemId)
  58. return array();
  59. $aItem = $this->_oDb->getPrices(array('type' => 'by_id_full', 'value' => $iItemId));
  60. if(empty($aItem) || !is_array($aItem))
  61. return array();
  62. return array (
  63. 'id' => $aItem['id'],
  64. 'author_id' => $this->_oConfig->getOwner(),
  65. 'name' => $aItem['name'],
  66. 'title' => _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']),
  67. 'description' => _t($aItem['level_description']),
  68. 'url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']),
  69. 'price_single' => $aItem['price'],
  70. 'price_recurring' => $aItem['price'],
  71. 'trial_recurring' => $aItem['trial']
  72. );
  73. }
  74. public function serviceGetCartItems($iSellerId)
  75. {
  76. $CNF = &$this->_oConfig->CNF;
  77. $iSellerIdSetting = $this->_oConfig->getOwner();
  78. if(empty($iSellerId) || ($iSellerId != $iSellerIdSetting && !isAdmin()))
  79. return array();
  80. $aItems = $this->_oDb->getPrices(array('type' => 'all_full'));
  81. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']);
  82. $aResult = array();
  83. foreach($aItems as $aItem)
  84. $aResult[] = array(
  85. 'id' => $aItem['id'],
  86. 'author_id' => $iSellerIdSetting,
  87. 'name' => $aItem['name'],
  88. 'title' => _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']),
  89. 'description' => _t($aItem['level_description']),
  90. 'url' => $sUrl,
  91. 'price_single' => $aItem['price'],
  92. 'price_recurring' => $aItem['price'],
  93. 'trial_recurring' => $aItem['trial']
  94. );
  95. return $aResult;
  96. }
  97. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  98. {
  99. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  100. }
  101. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  102. {
  103. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  104. }
  105. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  106. {
  107. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  108. }
  109. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  110. {
  111. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  112. }
  113. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  114. {
  115. //TODO: Do something if it's necessary.
  116. return true;
  117. }
  118. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  119. {
  120. $aItem = $this->serviceGetCartItem($iItemId);
  121. if(empty($aItem) || !is_array($aItem))
  122. return array();
  123. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  124. $iPeriod = (int)$aItemInfo['period'];
  125. $sPeriodUnit = $aItemInfo['period_unit'];
  126. if($sType == BX_ACL_LICENSE_TYPE_RECURRING && (int)$aItemInfo['trial'] > 0) {
  127. $iPeriod = (int)$aItemInfo['trial'];
  128. $sPeriodUnit = 'day';
  129. }
  130. if(!BxDolAcl::getInstance()->setMembership($iClientId, $aItemInfo['level_id'], array('period' => $iPeriod, 'period_unit' => $sPeriodUnit), false, $sLicense))
  131. return array();
  132. return $aItem;
  133. }
  134. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  135. {
  136. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  137. if(empty($aItemInfo) || !is_array($aItemInfo))
  138. return false;
  139. return BxDolAcl::getInstance()->unsetMembership($iClientId, $aItemInfo['level_id'], $sLicense);
  140. }
  141. /**
  142. * COMMON METHODS
  143. */
  144. public function getUserId()
  145. {
  146. return isLogged() ? bx_get_logged_profile_id() : 0;
  147. }
  148. public function getUserInfo($iUserId = 0)
  149. {
  150. $oProfile = BxDolProfile::getInstance($iUserId);
  151. if (!$oProfile)
  152. $oProfile = BxDolProfileUndefined::getInstance();
  153. return array(
  154. $oProfile->getDisplayName(),
  155. $oProfile->getUrl(),
  156. $oProfile->getThumb(),
  157. $oProfile->getUnit()
  158. );
  159. }
  160. }
  161. /** @} */