1
0

BxBaseMenuSetAclLevel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. $oForm->aInputs['profile_id']['value'] = $mixedProfileId;
  57. $oForm->aInputs['card']['value'] = 0;
  58. $oAcl = BxDolAcl::getInstance();
  59. $aAclLevels = $oAcl->getMembershipsBy(array('type' => 'all_active_not_automatic_pair'));
  60. foreach ($aAclLevels as $k => $s)
  61. $oForm->aInputs['level_id']['values'][] = ['key' => $k, 'value' => _t($s)];
  62. $aProfileAclLevel = $oAcl->getMemberMembershipInfo($mixedProfileId);
  63. $oForm->aInputs['level_id']['value'] = $aProfileAclLevel['id'];
  64. $sContent = $this->_oTemplate->parseHtmlByName('acl_set.html', [
  65. 'form' => $oForm->getCode(),
  66. 'form_id' => $oForm->getId(),
  67. 'profile_id' => $mixedProfileId,
  68. ]);
  69. if($this->_bContentOnly)
  70. return $sContent;
  71. return PopupBox('sys_acl_set_' . $mixedProfileId, _t('_sys_form_popup_acl_set'), $sContent, $this->_bHiddenByDefault);
  72. }
  73. }
  74. return parent::getCode ();
  75. }
  76. public function getMenuItems ()
  77. {
  78. $this->loadData();
  79. return parent::getMenuItems();
  80. }
  81. protected function loadData()
  82. {
  83. $mixedProfileId = $this->mixedProfileId;
  84. if(bx_get('profile_id') !== false)
  85. $mixedProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  86. $oAcl = BxDolAcl::getInstance();
  87. if (!$oAcl)
  88. return;
  89. $bBulk = is_array($mixedProfileId);
  90. if(!$bBulk) {
  91. $aProfileAclLevel = $oAcl->getMemberMembershipInfo($mixedProfileId);
  92. $this->setSelected('', $aProfileAclLevel['id']);
  93. }
  94. $aAclLevels = $oAcl->getMembershipsBy(array('type' => 'all_active_not_automatic_pair'));
  95. foreach ($aAclLevels as $k => $s)
  96. $aAclLevels[$k] = _t($s);
  97. $aItems = array();
  98. foreach ($aAclLevels as $iId => $sTitle) {
  99. $aItems[] = array(
  100. 'id' => $iId,
  101. 'name' => $iId,
  102. 'class' => '',
  103. 'title' => $sTitle,
  104. 'icon' => '',
  105. 'link' => 'javascript:void(0);',
  106. 'onclick' => "bx_set_acl_level(" . (!$bBulk ? $mixedProfileId : "'" . urlencode(json_encode($mixedProfileId)) . "'") . ", {$iId}, " . bx_js_string("$(this).parents('.bx-popup-applied:first')") . ");"
  107. );
  108. }
  109. $this->_aObject['menu_items'] = $aItems;
  110. }
  111. protected function setMembership($mixedProfileId, $iAclLevelId, $iAclLevelDuration = 0, $bAclCard = false)
  112. {
  113. bx_import('BxDolAcl');
  114. if(!is_array($mixedProfileId))
  115. $mixedProfileId = array($mixedProfileId);
  116. $iPerformerId = bx_get_logged_profile_id();
  117. $aCheck = checkActionModule($iPerformerId, 'set acl level', 'system', false);
  118. if(!isAdmin() && $aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  119. return array('code' => 1, 'msg' => $aCheck[CHECK_ACTION_MESSAGE]);
  120. $iSet = 0;
  121. $aCards = array();
  122. $oAcl = BxDolAcl::getInstance();
  123. foreach($mixedProfileId as $iProfileId) {
  124. if(!$oAcl->setMembership($iProfileId, $iAclLevelId, $iAclLevelDuration, true))
  125. continue;
  126. $iSet += 1;
  127. if($bAclCard) {
  128. $aCards['card_' . $iProfileId] = $oAcl->getProfileMembership($iProfileId);
  129. if(($aMembership = $oAcl->getMemberMembershipInfo($iProfileId))) {
  130. $aMembershipInfo = $oAcl->getMembershipInfo($aMembership['id']);
  131. $sContent = $this->_oTemplate->parseHtmlByName('menu_meta_item.html', [
  132. 'icon' => $this->_oTemplate->getImage($aMembershipInfo['icon'], ['class' => 'bx-acl-m-thumbnail']),
  133. 'caption' => _t($aMembership['name'])
  134. ]);
  135. $aCards['label_' . $iProfileId] = $this->_oTemplate->parseTag('span', $sContent, ['id' => 'sys-mi-acl-' . $iProfileId]);
  136. }
  137. }
  138. checkActionModule($iPerformerId, 'set acl level', 'system', true); // perform action
  139. }
  140. if(count($mixedProfileId) != $iSet)
  141. return array('code' => 2, 'msg' => _t('_error occured'));
  142. $aResult = array('code' => 0);
  143. if($bAclCard)
  144. $aResult['card'] = $aCards;
  145. return $aResult;
  146. }
  147. }
  148. /** @} */