BxBaseAcl.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Acl representation.
  11. * @see BxDolAcl
  12. */
  13. class BxBaseAcl extends BxDolAcl
  14. {
  15. public function __construct ()
  16. {
  17. parent::__construct ();
  18. }
  19. public function getProfileMembership ($iProfileId)
  20. {
  21. $aLevel = $this->getMemberMembershipInfo($iProfileId, 0, true);
  22. if(empty($aLevel) || !is_array($aLevel))
  23. return $this->_bIsApi ? [] : '';
  24. $oTemplate = BxDolTemplate::getInstance();
  25. $aLevelInfo = $this->getMembershipInfo($aLevel['id']);
  26. $bOwner = $bCanView = false;
  27. if(($iLoggedPid = bx_get_logged_profile_id()) && ($oLogged = BxDolProfile::getInstance($iLoggedPid)) !== false) {
  28. $bOwner = BxDolProfile::getInstance($iProfileId)->getAccountId() == $oLogged->getAccountId();
  29. $aCheck = checkActionModule($iLoggedPid, 'show membership private info', 'system', false);
  30. $bCanView = $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  31. }
  32. if($this->_bIsApi) {
  33. $sIconS = $aLevelInfo['icon'];
  34. $sIconD = $oTemplate->getImage($sIconS, ['wrap_in_tag' => false]);
  35. return [
  36. 'profile' => BxDolProfile::getData($iProfileId),
  37. 'level' => [
  38. 'id' => $aLevel['id'],
  39. 'name' => !empty($aLevelInfo['name']) ? _t($aLevelInfo['name']) : '',
  40. 'description' => !empty($aLevelInfo['description']) ? _t($aLevelInfo['description']) : '',
  41. 'icon' => strcmp($sIconS, $sIconD) != 0 ? $sIconD : BxDolIconset::getObjectInstance()->getIcon($sIconD),
  42. 'date_starts' => (int)$aLevel['date_starts'],
  43. 'date_expires' => (int)$aLevel['date_expires'],
  44. ]
  45. ];
  46. }
  47. $aTmplVarsPrivateInfo = [];
  48. $bTmplVarsPrivateInfo = (($bOwner || $bCanView) && !empty($aLevel['date_starts']));
  49. if($bTmplVarsPrivateInfo)
  50. $aTmplVarsPrivateInfo = array(
  51. 'date_start' => bx_time_js($aLevel['date_starts']),
  52. 'date_expire' => (int)$aLevel['date_expires'] > 0 ? bx_time_js($aLevel['date_expires']) : _t('_sys_acl_expire_never'),
  53. 'bx_if:show_state' => array(
  54. 'condition' => !empty($aLevel['state']),
  55. 'content' => array(
  56. 'state' => _t('_sys_acl_state_' . $aLevel['state'])
  57. )
  58. )
  59. );
  60. $aTmplVarsActions = [];
  61. if($bOwner) {
  62. $aDowngradable = $this->getMemberships(false, true, true, true);
  63. unset($aDowngradable[MEMBERSHIP_ID_STANDARD]);
  64. if(in_array($aLevel['id'], array_keys($aDowngradable)))
  65. $aTmplVarsActions[] = [
  66. 'href' => 'javascript:void(0)',
  67. 'bx_if:show_onclick' => [
  68. 'condition' => true,
  69. 'content' => [
  70. 'onclick' => 'javascript:bx_reset_acl_level(' . $iProfileId . ', this)'
  71. ]
  72. ],
  73. 'content' => _t('_sys_acl_downgrade')
  74. ];
  75. }
  76. $sTmplName = 'acl_membership.html';
  77. $aTmplVars = [
  78. 'html_id' => 'sys-acl-profile-' . $iProfileId,
  79. 'level' => _t($aLevel['name']),
  80. 'thumbnail' => $oTemplate->getImage($aLevelInfo['icon'], ['class' => 'bx-acl-m-thumbnail']),
  81. 'bx_if:show_private_info' => [
  82. 'condition' => $bTmplVarsPrivateInfo,
  83. 'content' => $aTmplVarsPrivateInfo
  84. ],
  85. 'bx_if:show_actions' => [
  86. 'condition' => !empty($aTmplVarsActions),
  87. 'content' => [
  88. 'bx_repeat:actions' => $aTmplVarsActions
  89. ]
  90. ]
  91. ];
  92. $sContent = $oTemplate->parseHtmlByName($sTmplName, $aTmplVars);
  93. /**
  94. * @hooks
  95. * @hookdef hook-system-page_output_block_acl_level 'system', 'page_output_block_acl_level' - hook to override profile membership page block
  96. * - $unit_name - equals `system`
  97. * - $action - equals `page_output_block_acl_level`
  98. * - $object_id - not used
  99. * - $sender_id - not used
  100. * - $extra_params - array of additional params with the following array keys:
  101. * - `block_owner` - [int] profile id to show membership level for
  102. * - `block_code` - [string] by ref, block code, can be overridden in hook processing
  103. * @hook @ref hook-system-page_output_block_acl_level
  104. */
  105. bx_alert('system', 'page_output_block_acl_level', 0, false, [
  106. 'block_owner' => $iProfileId,
  107. 'block_tmpl_name' => $sTmplName,
  108. 'block_tmpl_vars' => $aTmplVars,
  109. 'block_code' => &$sContent,
  110. 'block_code_ref' => &$sContent,
  111. ]);
  112. $oTemplate->addCss(['acl.css']);
  113. return $sContent;
  114. }
  115. /**
  116. * Print code for membership status
  117. * $iProfileId - ID of profile
  118. * $offer_upgrade - will this code be printed at [c]ontrol [p]anel
  119. */
  120. function GetMembershipStatus($iProfileId, $bOfferUpgrade = true)
  121. {
  122. $aMembershipInfo = $this->getMemberMembershipInfo($iProfileId);
  123. $sViewMembershipActions = "<br />(<a onclick=\"javascript:window.open('explanation.php?explain=membership&amp;type=".$aMembershipInfo['ID']."', '', 'width=660, height=500, menubar=no, status=no, resizable=no, scrollbars=yes, toolbar=no, location=no');\" href=\"javascript:void(0);\">"._t("_VIEW_MEMBERSHIP_ACTIONS")."</a>)<br />";
  124. // Show colored membership name
  125. $ret = '';
  126. if ( $aMembershipInfo['ID'] == MEMBERSHIP_ID_STANDARD || $aMembershipInfo['ID'] == MEMBERSHIP_ID_AUTHENTICATED) {
  127. $ret .= _t( "_MEMBERSHIP_STANDARD" ). $sViewMembershipActions;
  128. if ( $bOfferUpgrade )
  129. $ret .= " ". _t( "_MEMBERSHIP_UPGRADE_FROM_STANDARD" );
  130. } else {
  131. $ret .= "<font color=\"red\">{$aMembershipInfo['Name']}</font>$sViewMembershipActions";
  132. $days_left = (int)( ($aMembershipInfo['DateExpires'] - time()) / (24 * 3600) );
  133. if(!is_null($aMembershipInfo['DateExpires'])) {
  134. $ret .= ( $days_left > 0 ) ? _t( "_MEMBERSHIP_EXPIRES_IN_DAYS", $days_left ) : _t( "_MEMBERSHIP_EXPIRES_TODAY", date( "H:i", $aMembershipInfo['DateExpires'] ), date( "H:i" ) );
  135. } else {
  136. $ret.= _t("_MEMBERSHIP_EXPIRES_NEVER");
  137. }
  138. }
  139. return $ret;
  140. }
  141. }
  142. /** @} */