BxBaseStudioRolesLevels.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. class BxBaseStudioRolesLevels extends BxDolStudioRolesLevels
  11. {
  12. public static $iBinMB = 1048576;
  13. protected $sUrlPage;
  14. public function __construct($aOptions, $oTemplate = false)
  15. {
  16. parent::__construct($aOptions, $oTemplate);
  17. $this->sUrlPage = BX_DOL_URL_STUDIO . 'builder_roles.php?page=rlevels';
  18. }
  19. public function performActionAdd()
  20. {
  21. $sAction = 'add';
  22. $oForm = $this->_getFormObject($sAction);
  23. $oForm->initChecker();
  24. if($oForm->isSubmittedAndValid()) {
  25. if(($iId = $this->_getAvailableId()) === false)
  26. return echoJson(array('msg' => _t('_adm_prm_err_role_id')));
  27. $sLanguage = BxDolStudioLanguagesUtils::getInstance()->getCurrentLangName(false);
  28. $sName = BxDolForm::getSubmittedValue('title-' . $sLanguage, $oForm->aFormAttrs['method']);
  29. $sName = uriGenerate(strtolower($sName), 'sys_std_roles', 'name', 'role');
  30. $iId = (int)$oForm->insert(array('id' => $iId, 'name' => $sName, 'active' => 1, 'order' => $this->oDb->getRoleOrderMax() + 1));
  31. if($iId != 0) {
  32. $aRole = $this->oDb->getRoles(array('type' => 'by_id', 'id' => (int)$iId));
  33. if(($iActionsFrom = (int)$oForm->getCleanValue('actions')) > 0) {
  34. $aActions = $this->oDb->getActions(array('type' => 'by_role_id', 'role_id' => $iActionsFrom));
  35. foreach($aActions as $aAction)
  36. $this->oDb->switchAction($iId, $aAction['id'], true);
  37. }
  38. // create system event
  39. bx_alert('roles', 'added', $iId, 0, array('role' => $aRole));
  40. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  41. }
  42. else
  43. $aRes = array('msg' => _t('_adm_rl_err_role_create'));
  44. return echoJson($aRes);
  45. }
  46. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-rl-role-add-popup', _t('_adm_rl_txt_role_create_popup'), $this->_oTemplate->parseHtmlByName('rl_role.html', array(
  47. 'form_id' => $oForm->aFormAttrs['id'],
  48. 'form' => $oForm->getCode(true),
  49. 'object' => $this->_sObject,
  50. 'action' => $sAction
  51. )));
  52. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  53. }
  54. public function performActionEdit()
  55. {
  56. $sAction = 'edit';
  57. $aIds = bx_get('ids');
  58. if(!$aIds || !is_array($aIds)) {
  59. $iId = (int)bx_get('id');
  60. if(!$iId) {
  61. echoJson(array());
  62. exit;
  63. }
  64. $aIds = array($iId);
  65. }
  66. $iId = $aIds[0];
  67. $aRole = $this->oDb->getRoles(array('type' => 'by_id', 'id' => $iId));
  68. if(empty($aRole) || !is_array($aRole))
  69. return echoJson(array());
  70. $oForm = $this->_getFormObject($sAction, $aRole);
  71. $oForm->initChecker();
  72. if($oForm->isSubmittedAndValid()) {
  73. if($oForm->update($iId) !== false) {
  74. $aRole = $this->oDb->getRoles(array('type' => 'by_id', 'id' => (int)$iId));
  75. // create system event
  76. bx_alert('roles', 'edited', $iId, 0, array('role' => $aRole));
  77. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  78. }
  79. else
  80. $aRes = array('msg' => _t('_adm_rl_err_role_edit'));
  81. return echoJson($aRes);
  82. }
  83. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-rl-role-edit-popup', _t('_adm_rl_txt_role_edit_popup', _t($aRole['title'])), $this->_oTemplate->parseHtmlByName('rl_role.html', array(
  84. 'form_id' => $oForm->aFormAttrs['id'],
  85. 'form' => $oForm->getCode(true),
  86. 'object' => $this->_sObject,
  87. 'action' => $sAction
  88. )));
  89. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  90. }
  91. public function getJsObject()
  92. {
  93. return 'oBxDolStudioRolesLevels';
  94. }
  95. public function getCode($isDisplayHeader = true)
  96. {
  97. return $this->_oTemplate->parseHtmlByName('rl_roles_levels.html', array(
  98. 'content' => parent::getCode($isDisplayHeader),
  99. 'js_object' => $this->getJsObject(),
  100. 'page_url' => $this->sUrlPage,
  101. 'grid_object' => $this->_sObject,
  102. 'params_divider' => $this->sParamsDivider
  103. ));
  104. }
  105. protected function _addJsCss()
  106. {
  107. parent::_addJsCss();
  108. $this->_oTemplate->addJs(array('jquery.form.min.js', 'roles_levels.js'));
  109. $oForm = new BxTemplStudioFormView(array());
  110. $oForm->addCssJs();
  111. }
  112. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  113. {
  114. if(in_array($aRow['id'], array(BX_DOL_STUDIO_ROLE_MASTER)))
  115. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  116. return parent::_getCellSwitcher($mixedValue, $sKey, $aField, $aRow);
  117. }
  118. protected function _getCellActionsList ($mixedValue, $sKey, $aField, $aRow)
  119. {
  120. $aActions = $this->oDb->getActions(array('type' => 'by_role_id', 'role_id' => $aRow['id']));
  121. $sLink = BX_DOL_URL_STUDIO . 'builder_roles.php?page=ractions&role=' . $aRow['id'];
  122. $mixedValue = $this->_oTemplate->parseLink($sLink, _t('_adm_rl_txt_n_actions', count($aActions)), array(
  123. 'title' => _t('_adm_rl_txt_manage_actions')
  124. ));
  125. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  126. }
  127. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  128. {
  129. if(in_array($aRow['id'], array($this->aNonDeletable)))
  130. return '';
  131. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  132. }
  133. protected function _getFormObject($sAction, $aRole = array())
  134. {
  135. bx_import('BxTemplStudioFormView');
  136. $aForm = array(
  137. 'form_attrs' => array(
  138. 'id' => 'adm-rl-role-',
  139. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction,
  140. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT,
  141. 'enctype' => 'multipart/form-data',
  142. ),
  143. 'params' => array (
  144. 'db' => array(
  145. 'table' => 'sys_std_roles',
  146. 'key' => 'id',
  147. 'uri' => '',
  148. 'uri_title' => '',
  149. 'submit_name' => 'do_submit'
  150. ),
  151. ),
  152. 'inputs' => array (
  153. 'id' => array(
  154. 'type' => 'hidden',
  155. 'name' => 'id',
  156. 'value' => isset($aRole['id']) ? (int)$aRole['id'] : 0,
  157. 'db' => array (
  158. 'pass' => 'Int',
  159. ),
  160. ),
  161. 'title' => array(
  162. 'type' => 'text_translatable',
  163. 'name' => 'title',
  164. 'caption' => _t('_adm_rl_txt_role_title'),
  165. 'value' => isset($aRole['title']) ? $aRole['title'] : '_adm_rl_txt_role',
  166. 'required' => '1',
  167. 'db' => array (
  168. 'pass' => 'Xss',
  169. ),
  170. 'checker' => array (
  171. 'func' => 'LengthTranslatable',
  172. 'params' => array(3, 100, 'title'),
  173. 'error' => _t('_adm_rl_err_role_title'),
  174. ),
  175. ),
  176. 'description' => array(
  177. 'type' => 'textarea_translatable',
  178. 'name' => 'description',
  179. 'caption' => _t('_adm_rl_txt_role_description'),
  180. 'value' => isset($aRole['description']) ? $aRole['description'] : '_adm_rl_txt_role',
  181. 'code' => 1,
  182. 'db' => array (
  183. 'pass' => 'XssHtml',
  184. )
  185. ),
  186. 'actions' => array(
  187. 'type' => 'select',
  188. 'name' => 'actions',
  189. 'caption' => _t('_adm_rl_txt_role_actions_copy'),
  190. 'values' => array(
  191. array('key' => 0, 'value' => _t('_sys_txt_empty'))
  192. ),
  193. ),
  194. 'controls' => array(
  195. 'name' => 'controls',
  196. 'type' => 'input_set',
  197. array(
  198. 'type' => 'submit',
  199. 'name' => 'do_submit',
  200. 'value' => _t('_adm_rl_btn_role_create'),
  201. ),
  202. array (
  203. 'type' => 'reset',
  204. 'name' => 'close',
  205. 'value' => _t('_adm_rl_btn_role_cancel'),
  206. 'attrs' => array(
  207. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  208. 'class' => 'bx-def-margin-sec-left',
  209. ),
  210. )
  211. )
  212. )
  213. );
  214. $aRoles = $this->oDb->getRoles(array('type' => 'all_pair'));
  215. foreach($aRoles as $iId => $sTitle)
  216. $aForm['inputs']['actions']['values'][] = array('key' => $iId, 'value' => _t($sTitle));
  217. switch($sAction) {
  218. case 'add':
  219. unset($aForm['inputs']['id']);
  220. $aForm['form_attrs']['id'] .= 'create';
  221. break;
  222. case 'edit':
  223. unset($aForm['inputs']['actions']);
  224. $aForm['form_attrs']['id'] .= 'edit';
  225. $aForm['inputs']['controls'][0]['value'] = _t('_adm_rl_btn_role_save');
  226. break;
  227. }
  228. return new BxTemplStudioFormView($aForm);
  229. }
  230. protected function _getAvailableId()
  231. {
  232. $aRoles = $this->oDb->getRoles(array('type' =>'all_order_id'));
  233. $iId = 1;
  234. foreach($aRoles as $aRole) {
  235. if($iId != (int)$aRole['id'])
  236. break;
  237. $iId++;
  238. }
  239. return $iId <= BX_DOL_STUDIO_ROLES_ROLE_ID_INT_MAX ? $iId : false;
  240. }
  241. }
  242. /** @} */