BxBaseStudioFormsGroupsRoles.php 15 KB

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