BxBaseStudioFormsGroupsRoles.php 16 KB

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