BxAclModule.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. $iSellerId = $this->_oConfig->getOwner();
  78. $aItems = $this->_oDb->getPrices(array('type' => 'all_full'));
  79. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_VIEW']);
  80. $aResult = array();
  81. foreach($aItems as $aItem)
  82. $aResult[] = array(
  83. 'id' => $aItem['id'],
  84. 'author_id' => $iSellerId,
  85. 'name' => $aItem['name'],
  86. 'title' => _t('_bx_acl_txt_cart_item_title', _t($aItem['level_name']), $aItem['period'], $aItem['period_unit']),
  87. 'description' => _t($aItem['level_description']),
  88. 'url' => $sUrl,
  89. 'price_single' => $aItem['price'],
  90. 'price_recurring' => $aItem['price'],
  91. 'trial_recurring' => $aItem['trial']
  92. );
  93. return $aResult;
  94. }
  95. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  96. {
  97. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  98. }
  99. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  100. {
  101. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  102. }
  103. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  104. {
  105. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_SINGLE);
  106. }
  107. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  108. {
  109. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_ACL_LICENSE_TYPE_RECURRING);
  110. }
  111. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  112. {
  113. //TODO: Do something if it's necessary.
  114. return true;
  115. }
  116. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  117. {
  118. $aItem = $this->serviceGetCartItem($iItemId);
  119. if(empty($aItem) || !is_array($aItem))
  120. return array();
  121. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  122. $iPeriod = (int)$aItemInfo['period'];
  123. $sPeriodUnit = $aItemInfo['period_unit'];
  124. if($sType == BX_ACL_LICENSE_TYPE_RECURRING && (int)$aItemInfo['trial'] > 0) {
  125. $iPeriod = (int)$aItemInfo['trial'];
  126. $sPeriodUnit = 'day';
  127. }
  128. if(!BxDolAcl::getInstance()->setMembership($iClientId, $aItemInfo['level_id'], array('period' => $iPeriod, 'period_unit' => $sPeriodUnit), false, $sLicense))
  129. return array();
  130. return $aItem;
  131. }
  132. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  133. {
  134. $aItemInfo = $this->_oDb->getPrices(array('type' => 'by_id', 'value' => $iItemId));
  135. if(empty($aItemInfo) || !is_array($aItemInfo))
  136. return false;
  137. return BxDolAcl::getInstance()->unsetMembership($iClientId, $aItemInfo['level_id'], $sLicense);
  138. }
  139. /**
  140. * COMMON METHODS
  141. */
  142. public function getUserId()
  143. {
  144. return isLogged() ? bx_get_logged_profile_id() : 0;
  145. }
  146. public function getUserInfo($iUserId = 0)
  147. {
  148. $oProfile = BxDolProfile::getInstance($iUserId);
  149. if (!$oProfile)
  150. $oProfile = BxDolProfileUndefined::getInstance();
  151. return array(
  152. $oProfile->getDisplayName(),
  153. $oProfile->getUrl(),
  154. $oProfile->getThumb(),
  155. $oProfile->getUnit()
  156. );
  157. }
  158. }
  159. /** @} */