BxBaseStudioFormsGroupsRoles.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 UnaView UNA Studio Representation classes
  7. * @ingroup UnaStudio
  8. * @{
  9. */
  10. bx_import('BxTemplStudioFormView');
  11. define('BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX', round(log(BX_DOL_INT_MAX, 2)));
  12. define('BX_DOL_MOD_GROUPS_ROLE_COMMON', 0);
  13. define('BX_DOL_MOD_GROUPS_ROLE_ADMINISTRATOR', 1);
  14. define('BX_DOL_MOD_GROUPS_ROLE_MODERATOR', 2);
  15. class BxBaseStudioFormsGroupsRoles extends BxDolStudioFormsGroupsRoles
  16. {
  17. protected $sUrlPage;
  18. public function __construct ($aOptions, $oTemplate = false)
  19. {
  20. parent::__construct ($aOptions, $oTemplate);
  21. $this->sUrlPage = BX_DOL_URL_STUDIO . 'builder_forms.php?page=groups_roles';
  22. }
  23. public function performActionAdd()
  24. {
  25. $sAction = 'add';
  26. if(!$this->canAdd()) {
  27. echoJson(array());
  28. exit;
  29. }
  30. $oForm = $this->_getPermissionsForm($sAction);
  31. if (!$oForm)
  32. return '';
  33. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?' . bx_encode_url_params($_GET, array('ids', '_r'));
  34. $oForm->initChecker();
  35. if ($oForm->isSubmitted())
  36. $this->initDataSwitchers($oForm->aInputs);
  37. if($oForm->isSubmittedAndValid()) {
  38. $iNewValue = $this->_getAvailableSetValue($this->sRolesDataList);
  39. if(!$this->canUseForSet($iNewValue)) {
  40. echoJson(array('msg' => _t('_adm_form_err_pre_values_create_forbidden', BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX)));
  41. return;
  42. }
  43. $aData = $oForm->getCleanValue('Data');
  44. $aAllActions = $this->_getDefaultActionsArray(0);
  45. foreach ($aAllActions as $sModule => $aActions)
  46. if (!isset($aData[$sModule])) $aData[$sModule] = [];
  47. $mixedResult = $oForm->insert([
  48. 'LKey2' => '',
  49. 'Value' => $iNewValue,
  50. 'Order' => $this->oDb->getValuesOrderMax($this->sRolesDataList) + 1,
  51. 'Key' => $this->sRolesDataList,
  52. 'Data' => serialize($aData),
  53. ]);
  54. if(is_numeric($mixedResult))
  55. $aRes = array('grid' => $this->getCode(false), 'blink' => $mixedResult);
  56. else
  57. $aRes = array('msg' => $mixedResult);
  58. echoJson($aRes);
  59. }
  60. else {
  61. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-groups-roles-add-popup', _t('_adm_form_txt_groups_roles_add_popup'), $this->_oTemplate->parseHtmlByName('form_add_groups_role.html', array(
  62. 'form_id' => $oForm->aFormAttrs['id'],
  63. 'form' => $oForm->getCode(true),
  64. 'object' => $this->_sObject,
  65. 'action' => $sAction
  66. )));
  67. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  68. }
  69. }
  70. public function performActionEdit()
  71. {
  72. $sAction = 'edit';
  73. $aIds = bx_get('ids');
  74. if(!$aIds || !is_array($aIds)) {
  75. $iId = (int)bx_get('id');
  76. if(!$iId)
  77. return echoJson(array());
  78. $aIds = array($iId);
  79. }
  80. $iId = (int)array_shift($aIds);
  81. $aItem = array();
  82. $this->oDb->getValues(array('type' => 'by_id', 'value' => $iId), $aItem, false);
  83. if(!is_array($aItem) || empty($aItem)) {
  84. echoJson([]);
  85. exit;
  86. }
  87. $oForm = $this->_getPermissionsForm($sAction, $aItem);
  88. $oForm->initChecker();
  89. if ($oForm->isSubmitted())
  90. $this->initDataSwitchers($oForm->aInputs);
  91. if($oForm->isSubmittedAndValid()) {
  92. $aData = $oForm->getCleanValue('Data');
  93. $aAllActions = $this->_getDefaultActionsArray($aItem['Value']);
  94. foreach ($aAllActions as $sModule => $aActions)
  95. if (!isset($aData[$sModule])) $aData[$sModule] = [];
  96. $mixedResult = $oForm->update($iId, ['Data' => serialize($aData)]);
  97. if(is_numeric($mixedResult)) {
  98. $this->onRoleChanged($aItem);
  99. $aRes = array('grid' => $this->getCode(false), 'blink' => $mixedResult);
  100. }
  101. else
  102. $aRes = array('msg' => $mixedResult);
  103. echoJson($aRes);
  104. }
  105. else {
  106. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-groups-roles-edit-popup', _t('_adm_form_txt_groups_roles_edit_popup', _t($aItem['LKey'])), $this->_oTemplate->parseHtmlByName('form_add_groups_role.html', array(
  107. 'form_id' => $oForm->aFormAttrs['id'],
  108. 'form' => $oForm->getCode(true),
  109. 'object' => $this->_sObject,
  110. 'action' => $sAction
  111. )));
  112. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  113. }
  114. }
  115. public function performActionDelete()
  116. {
  117. $this->_replaceMarkers ();
  118. $iAffected = 0;
  119. $aIds = bx_get('ids');
  120. if (!$aIds || !is_array($aIds)) {
  121. echoJson(array());
  122. exit;
  123. }
  124. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  125. $aIdsAffected = array ();
  126. foreach($aIds as $iId) {
  127. $aValue = array();
  128. $this->oDb->getValues(array('type' => 'by_id', 'value' => (int)$iId), $aValue);
  129. if(!is_array($aValue) || empty($aValue))
  130. continue;
  131. if((int)$this->_delete($iId) <= 0)
  132. continue;
  133. $oLanguage->deleteLanguageString($aValue['lkey']);
  134. $oLanguage->deleteLanguageString($aValue['lkey2']);
  135. $aIdsAffected[] = $iId;
  136. $iAffected++;
  137. }
  138. echo echoJson(array_merge(
  139. array(
  140. 'grid' => $this->getCode(false),
  141. ),
  142. $iAffected ? array() : array('msg' => _t("_sys_grid_delete_failed"))
  143. ));
  144. }
  145. protected function _getPermissionsForm($sFormAction, $aRole = [])
  146. {
  147. if (!empty($aRole['Data']) && !is_array($aRole['Data'])) $aRole['Data'] = unserialize($aRole['Data']);
  148. $aInputs = [];
  149. $aInputs['id'] = [
  150. 'type' => 'hidden',
  151. 'name' => 'id',
  152. 'value' => isset($aRole['id']) ? (int)$aRole['id'] : 0,
  153. 'db' => [
  154. 'pass' => 'Int',
  155. ],
  156. ];
  157. $aInputs['LKey'] = [
  158. 'type' => 'text_translatable',
  159. 'name' => 'LKey',
  160. 'caption' => _t('_adm_form_txt_groups_role_value'),
  161. 'info' => '',
  162. 'value' => isset($aRole['LKey']) ? $aRole['LKey'] : '',
  163. 'required' => '1',
  164. 'db' => [
  165. 'pass' => 'Xss',
  166. ],
  167. 'checker' => [
  168. 'func' => 'LengthTranslatable',
  169. 'params' => [1,100, 'LKey'],
  170. 'error' => _t('_adm_form_txt_groups_role_value_err'),
  171. ],
  172. ];
  173. $aAllActions = $this->_getDefaultActionsArray(isset($aRole['Value']) ? (int)$aRole['Value'] : 0);
  174. foreach ($aAllActions as $sModule => $aActionsList) {
  175. $oModule = BxDolModule::getInstance($sModule);
  176. $aInputs[$sModule.'_begin'] = [
  177. 'type' => 'block_header',
  178. 'caption' => BxDolModule::getTitle($oModule->_aModule['uri']),
  179. 'collapsed' => true,
  180. ];
  181. foreach ($aActionsList as $sAction => $bAllowedByDefault) {
  182. $bChecked = false;
  183. // if pemission is not set explicitly then consider the default value
  184. if ((empty($aRole) || !is_array($aRole['Data']) || !isset($aRole['Data'][$sModule])) && $bAllowedByDefault) $bChecked = true;
  185. // if pemission is set explicitly then use the value set
  186. if (!empty($aRole) && is_array($aRole['Data']) && isset($aRole['Data'][$sModule]) && isset($aRole['Data'][$sModule][$sAction]) && $aRole['Data'][$sModule][$sAction]) $bChecked = true;
  187. $aInputs[$sModule.'|'.$sAction] = [
  188. 'type' => 'switcher',
  189. 'name' => "Data[{$sModule}][{$sAction}]",
  190. 'caption' => _t('_adm_form_txt_groups_role_'.$sAction),
  191. 'value' => 1,
  192. 'checked' => $bChecked,
  193. ];
  194. }
  195. }
  196. $aInputs['end'] = [
  197. 'type' => 'block_end',
  198. ];
  199. $aInputs['controls'] = [
  200. 'name' => 'controls',
  201. 'type' => 'input_set',
  202. [
  203. 'type' => 'submit',
  204. 'name' => 'do_submit',
  205. 'value' => _t('_adm_form_btn_labels_submit'),
  206. ],
  207. [
  208. 'type' => 'reset',
  209. 'name' => 'close',
  210. 'value' => _t('_adm_form_btn_labels_cancel'),
  211. 'attrs' => [
  212. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  213. 'class' => 'bx-def-margin-sec-left',
  214. ],
  215. ]
  216. ];
  217. $aForm = array(
  218. 'form_attrs' => array(
  219. 'id' => 'adm-form-groups-role-edit',
  220. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&module=' . $this->sModule . '&a=' . $sFormAction,
  221. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  222. ),
  223. 'params' => array (
  224. 'db' => array(
  225. 'table' => 'sys_form_pre_values',
  226. 'key' => 'id',
  227. 'uri' => '',
  228. 'uri_title' => '',
  229. 'submit_name' => 'do_submit'
  230. ),
  231. ),
  232. 'inputs' => $aInputs
  233. );
  234. return new BxTemplStudioFormView($aForm);
  235. }
  236. protected function _getCellActionsList($mixedValue, $sKey, $aField, $aRow)
  237. {
  238. $mPermissions = $aRow['Data'] ? unserialize($aRow['Data']) : false;
  239. $aDefaultActionsByModules = $this->_getDefaultActionsArray($aRow['Value']);
  240. $iAllowedPermissions = 0;
  241. foreach ($aDefaultActionsByModules as $sModule => $aDefaultActions) {
  242. foreach ($aDefaultActions as $sAction => $bAllowedByDefault) {
  243. // if pemission is not set explicitly then consider the default value
  244. if ((!is_array($mPermissions) || !isset($mPermissions[$sModule])) && $bAllowedByDefault) $iAllowedPermissions++;
  245. // if pemission is set explicitly then use the value set
  246. if (is_array($mPermissions) && isset($mPermissions[$sModule]) && isset($mPermissions[$sModule][$sAction]) && $mPermissions[$sModule][$sAction]) $iAllowedPermissions++;
  247. }
  248. }
  249. $sLink = 'javascript: glGrids.'.$this->_sObject.'.action(\'edit\', {id: '.$aRow['id'].'});';
  250. $mixedValue = $this->_oTemplate->parseLink($sLink, _t('_adm_prm_txt_n_actions', $iAllowedPermissions), array(
  251. 'title' => _t('_adm_prm_txt_manage_actions')
  252. ));
  253. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  254. }
  255. protected function _addJsCss()
  256. {
  257. parent::_addJsCss();
  258. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_groups_roles.js'));
  259. $oForm = new BxTemplStudioFormView(array());
  260. $oForm->addCssJs();
  261. }
  262. protected function canAdd()
  263. {
  264. return $this->sModule != '';
  265. }
  266. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  267. {
  268. if(!$this->canAdd())
  269. $isDisabled = true;
  270. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  271. }
  272. protected function _getFilterControls ()
  273. {
  274. parent::_getFilterControls();
  275. return $this->getModulesSelectOne('', false, false);
  276. }
  277. public function getModulesSelectOneArray($sGetItemsMethod, $bShowCustom = true, $bShowSystem = true)
  278. {
  279. $aModules = $this->getModules($bShowCustom, $bShowSystem);
  280. foreach ($aModules as $sModuleUri => $sModuleName) {
  281. $oModule = BxDolModule::getInstance($sModuleUri);
  282. if ($oModule) {
  283. $sRolesDataList = isset($oModule->_oConfig->CNF['OBJECT_PRE_LIST_ROLES']) ? $oModule->_oConfig->CNF['OBJECT_PRE_LIST_ROLES'] : '';
  284. if ($sRolesDataList) {
  285. $aRoles = BxBaseFormView::getDataItems($sRolesDataList);
  286. $aModules[$sModuleUri] = $sModuleName . ' (' . ($aRoles ? count($aRoles) : 0) . ')';
  287. continue;
  288. }
  289. }
  290. unset($aModules[$sModuleUri]);
  291. }
  292. $aInputModules = array(
  293. 'type' => 'select',
  294. 'name' => 'module',
  295. 'attrs' => array(
  296. 'id' => 'bx-grid-module-' . $this->_sObject,
  297. 'aria-label' => _t('_adm_grid_lbl_module'),
  298. 'onChange' => 'javascript:' . $this->getJsObject() . '.onChangeModule()'
  299. ),
  300. 'value' => $this->sModule,
  301. 'values' => $aModules
  302. );
  303. return $aInputModules;
  304. }
  305. protected function _getDefaultActionsArray($iRole) {
  306. $bAdminOrModerator = $iRole == BX_DOL_MOD_GROUPS_ROLE_ADMINISTRATOR || $iRole == BX_DOL_MOD_GROUPS_ROLE_MODERATOR;
  307. $aDefaultActions = [
  308. $this->sModule => [
  309. 'invite' => $bAdminOrModerator,
  310. 'manage_fans' => $bAdminOrModerator,
  311. 'manage_roles' => $iRole == BX_DOL_MOD_GROUPS_ROLE_ADMINISTRATOR,
  312. 'edit' => $iRole == BX_DOL_MOD_GROUPS_ROLE_ADMINISTRATOR,
  313. 'change_cover' => $iRole == BX_DOL_MOD_GROUPS_ROLE_ADMINISTRATOR,
  314. ]
  315. ];
  316. // if timeline is installed then handle it as a special case
  317. $oTimeline = BxDolModule::getInstance('bx_timeline');
  318. if ($oTimeline) {
  319. $aDefaultActions['bx_timeline'] = [
  320. 'post' => 1,
  321. 'edit_any' => $bAdminOrModerator,
  322. 'delete_any' => $bAdminOrModerator,
  323. 'pin' => $bAdminOrModerator,
  324. ];
  325. }
  326. $aModules = $this->getModules(false, false);
  327. foreach ($aModules as $sModuleUri => $sModuleName) {
  328. $oModule = BxDolModule::getInstance($sModuleUri);
  329. if ($oModule) {
  330. if ((bx_srv('system', 'is_module_content', [$sModuleUri]) || bx_srv('system', 'is_module_context', [$sModuleUri])) && (!BxDolRequest::serviceExists($sModuleUri, 'act_as_profile') || !bx_srv($sModuleUri, 'act_as_profile'))) {
  331. $aActions = [
  332. 'post' => 1,
  333. 'edit_any' => $bAdminOrModerator,
  334. 'delete_any' => $bAdminOrModerator,
  335. ];
  336. if (isset($aDefaultActions[$sModuleUri])) {
  337. $aDefaultActions[$sModuleUri] = array_merge($aDefaultActions[$sModuleUri], $aActions);
  338. } else {
  339. $aDefaultActions[$sModuleUri] = $aActions;
  340. }
  341. }
  342. }
  343. }
  344. return $aDefaultActions;
  345. }
  346. protected function _getAvailableSetValue($sList)
  347. {
  348. $aValues = array();
  349. $this->oDb->getValues(array('type' =>'by_key_key_value', 'value' => $sList), $aValues, false);
  350. ksort($aValues);
  351. $iValue = 1;
  352. foreach($aValues as $aValue) {
  353. if((int)$aValue['value'] == 0)
  354. continue;
  355. if((int)$aValue['value'] != $iValue)
  356. break;
  357. $iValue++;
  358. }
  359. return $iValue;
  360. }
  361. protected function canUseForSet($mixedValue)
  362. {
  363. return is_numeric($mixedValue) && (int)$mixedValue >= 1 && (int)$mixedValue <= BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX;
  364. }
  365. function getJsObject()
  366. {
  367. return 'oBxDolStudioFormsGroupsRoles';
  368. }
  369. function initDataSwitchers(&$aInputs) {
  370. foreach ($aInputs as $sName => $aInput) {
  371. if ($aInput['type'] != 'switcher') continue;
  372. list ($sModule, $sAction) = explode('|', $sName);
  373. $aInputs[$sName]['checked'] = isset($_POST['Data']) && isset($_POST['Data'][$sModule]) && isset($_POST['Data'][$sModule][$sAction]);
  374. }
  375. }
  376. function getCode($isDisplayHeader = true)
  377. {
  378. return $this->_oTemplate->parseHtmlByName('forms_groups_roles.html', array(
  379. 'content' => parent::getCode($isDisplayHeader),
  380. 'js_object' => $this->getJsObject(),
  381. 'grid_object' => $this->_sObject,
  382. 'params_divider' => $this->sParamsDivider
  383. ));
  384. }
  385. protected function onRoleChanged($aRole)
  386. {
  387. if(!empty($aRole) && isset($aRole['key']) && ($sKey = $aRole['key'])) {
  388. $this->oDb->cleanCache('sys_form_pre_values_with_key_' . $sKey);
  389. $this->oDb->cleanCache('sys_form_pre_values_' . $sKey);
  390. }
  391. }
  392. }
  393. /** @} */