BxBaseStudioFormsGroupsRoles.php 16 KB

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