BxBaseMenuSetAclLevel.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * Menu to set acl level for profile
  11. */
  12. class BxBaseMenuSetAclLevel extends BxTemplMenu
  13. {
  14. protected $_bWithDuration;
  15. protected $_bHiddenByDefault;
  16. protected $_bContentOnly;
  17. protected $mixedProfileId;
  18. public function __construct ($aObject, $oTemplate)
  19. {
  20. parent::__construct ($aObject, $oTemplate);
  21. $this->_bWithDuration = true;
  22. $this->_bHiddenByDefault = true;
  23. $this->_bContentOnly = false;
  24. }
  25. public function setWithDuration($bWithDuration)
  26. {
  27. $this->_bWithDuration = $bWithDuration;
  28. }
  29. public function setHiddenByDefault($bHiddenByDefault)
  30. {
  31. $this->_bHiddenByDefault = $bHiddenByDefault;
  32. }
  33. public function setContentOnly($bContentOnly)
  34. {
  35. $this->_bContentOnly = $bContentOnly;
  36. }
  37. public function getCode ($mixedProfileId = 0)
  38. {
  39. $this->mixedProfileId = $mixedProfileId;
  40. if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && ($mixedProfileId = bx_get('profile_id', 'post')) && ($iAclLevelId = bx_get('level_id', 'post'))) {
  41. $mixedProfileId = urldecode($mixedProfileId);
  42. if(!is_numeric($mixedProfileId))
  43. $mixedProfileId = json_decode($mixedProfileId, true);
  44. echoJson($this->setMembership($mixedProfileId, $iAclLevelId, bx_get('duration', 'post') !== false ? (int)bx_get('duration', 'post') : 0, (int)bx_get('card', 'post') > 0));
  45. exit;
  46. }
  47. $mixedProfileId = $this->mixedProfileId;
  48. if(bx_get('profile_id') !== false)
  49. $mixedProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  50. if($this->_bWithDuration && !is_array($mixedProfileId)) {
  51. $oForm = BxDolForm::getObjectInstance('sys_acl', 'sys_acl_set');
  52. if($oForm) {
  53. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . bx_append_url_params('menu.php', [
  54. 'o' => 'sys_set_acl_level',
  55. ]);
  56. $oProfile = BxDolProfile::getInstance($mixedProfileId);
  57. $bProfile = $oProfile !== false;
  58. $oForm->aInputs['profile_id']['value'] = $bProfile ? $oProfile->id() : $mixedProfileId;
  59. $oForm->aInputs['card']['value'] = 0;
  60. $oAcl = BxDolAcl::getInstance();
  61. $aAclLevels = $oAcl->getMembershipsBy(['type' => 'all_active_not_automatic_pair', 'available_to' => $bProfile ? $oProfile->getModule() : '']);
  62. foreach ($aAclLevels as $k => $s)
  63. $oForm->aInputs['level_id']['values'][] = ['key' => $k, 'value' => _t($s)];
  64. $aProfileAclLevel = $oAcl->getMemberMembershipInfo($mixedProfileId);
  65. $oForm->aInputs['level_id']['value'] = $aProfileAclLevel['id'];
  66. $sContent = $this->_oTemplate->parseHtmlByName('acl_set.html', [
  67. 'form' => $oForm->getCode(),
  68. 'form_id' => $oForm->getId(),
  69. 'profile_id' => $mixedProfileId,
  70. ]);
  71. if($this->_bContentOnly)
  72. return $sContent;
  73. return PopupBox('sys_acl_set_' . $mixedProfileId, _t('_sys_form_popup_acl_set'), $sContent, $this->_bHiddenByDefault);
  74. }
  75. }
  76. return parent::getCode ();
  77. }
  78. public function getMenuItems ()
  79. {
  80. $this->loadData();
  81. return parent::getMenuItems();
  82. }
  83. protected function loadData()
  84. {
  85. $mixedProfileId = $this->mixedProfileId;
  86. if(bx_get('profile_id') !== false)
  87. $mixedProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  88. $oAcl = BxDolAcl::getInstance();
  89. if (!$oAcl)
  90. return;
  91. $bBulk = is_array($mixedProfileId);
  92. if(!$bBulk) {
  93. $aProfileAclLevel = $oAcl->getMemberMembershipInfo($mixedProfileId);
  94. $this->setSelected('', $aProfileAclLevel['id']);
  95. }
  96. $aAclLevels = $oAcl->getMembershipsBy(array('type' => 'all_active_not_automatic_pair'));
  97. foreach ($aAclLevels as $k => $s)
  98. $aAclLevels[$k] = _t($s);
  99. $aItems = array();
  100. foreach ($aAclLevels as $iId => $sTitle) {
  101. $aItems[] = array(
  102. 'id' => $iId,
  103. 'name' => $iId,
  104. 'class' => '',
  105. 'title' => $sTitle,
  106. 'icon' => '',
  107. 'link' => 'javascript:void(0);',
  108. 'onclick' => "bx_set_acl_level(" . (!$bBulk ? $mixedProfileId : "'" . urlencode(json_encode($mixedProfileId)) . "'") . ", {$iId}, " . bx_js_string("$(this).parents('.bx-popup-applied:first')") . ");"
  109. );
  110. }
  111. $this->_aObject['menu_items'] = $aItems;
  112. }
  113. protected function setMembership($mixedProfileId, $iAclLevelId, $iAclLevelDuration = 0, $bAclCard = false)
  114. {
  115. bx_import('BxDolAcl');
  116. if(!is_array($mixedProfileId))
  117. $mixedProfileId = array($mixedProfileId);
  118. $iPerformerId = bx_get_logged_profile_id();
  119. $aCheck = checkActionModule($iPerformerId, 'set acl level', 'system', false);
  120. if(!isAdmin() && $aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  121. return array('code' => 1, 'msg' => $aCheck[CHECK_ACTION_MESSAGE]);
  122. $iSet = 0;
  123. $aCards = array();
  124. $oAcl = BxDolAcl::getInstance();
  125. foreach($mixedProfileId as $iProfileId) {
  126. if(!$oAcl->setMembership($iProfileId, $iAclLevelId, $iAclLevelDuration, true))
  127. continue;
  128. $iSet += 1;
  129. if($bAclCard) {
  130. $aCards['card_' . $iProfileId] = $oAcl->getProfileMembership($iProfileId);
  131. if(($aMembership = $oAcl->getMemberMembershipInfo($iProfileId))) {
  132. $aMembershipInfo = $oAcl->getMembershipInfo($aMembership['id']);
  133. $sContent = $this->_oTemplate->parseHtmlByName('menu_meta_item.html', [
  134. 'icon' => $this->_oTemplate->getImage($aMembershipInfo['icon'], ['class' => 'bx-acl-m-thumbnail']),
  135. 'caption' => _t($aMembership['name'])
  136. ]);
  137. $aCards['label_' . $iProfileId] = $this->_oTemplate->parseTag('span', $sContent, ['id' => 'sys-mi-acl-' . $iProfileId]);
  138. }
  139. }
  140. checkActionModule($iPerformerId, 'set acl level', 'system', true); // perform action
  141. }
  142. if(count($mixedProfileId) != $iSet)
  143. return array('code' => 2, 'msg' => _t('_error occured'));
  144. $aResult = array('code' => 0);
  145. if($bAclCard)
  146. $aResult['card'] = $aCards;
  147. return $aResult;
  148. }
  149. }
  150. /** @} */