BxBaseStudioNavigationMenus.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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('BxDolStudioUtils');
  11. class BxBaseStudioNavigationMenus extends BxDolStudioNavigationMenus
  12. {
  13. private $sCreateNew = 'sys_create_new';
  14. protected $sUrlViewItems;
  15. function __construct($aOptions, $oTemplate = false)
  16. {
  17. parent::__construct($aOptions, $oTemplate);
  18. $this->_aOptions['actions_single']['edit']['attr']['title'] = _t('_adm_nav_btn_menus_edit');
  19. $this->_aOptions['actions_single']['delete']['attr']['title'] = _t('_adm_nav_btn_menus_delete');
  20. $this->sUrlViewItems = BX_DOL_URL_STUDIO . 'builder_menu.php?page=items&module=%s&set=%s';
  21. }
  22. public function performActionAdd()
  23. {
  24. $sAction = 'add';
  25. $oForm = $this->_getFormObject($sAction);
  26. if($oForm->isSubmitted() && isset($oForm->aInputs['set_name']))
  27. $this->updateSetFields($oForm);
  28. $oForm->initChecker();
  29. if($oForm->isSubmittedAndValid()) {
  30. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  31. $sLanguage = $oLanguage->getCurrentLangName(false);
  32. $sObject = BxDolForm::getSubmittedValue('title-' . $sLanguage, $oForm->aFormAttrs['method']);
  33. $sObject = uriGenerate($sObject, 'sys_objects_menu', 'object', ['empty' => 'object']);
  34. //--- New Set Creation
  35. if($oForm->getCleanValue('set_name') == $this->sCreateNew) {
  36. $sSetTitleValue = $oForm->getCleanValue('set_title');
  37. $sSetName = uriGenerate($sSetTitleValue, 'sys_menu_sets', 'set_name', ['empty' => 'set']);
  38. $sSetTitleKey = '_adm_nav_txt_set_' . $sSetName;
  39. if($this->oDb->addSet(array('set_name' => $sSetName, 'module' => BX_DOL_STUDIO_MODULE_CUSTOM, 'title' => $sSetTitleKey, 'deletable' => 1))) {
  40. $oLanguage->addLanguageString($sSetTitleKey, $sSetTitleValue);
  41. BxDolForm::setSubmittedValue('set_name', $sSetName, $oForm->aFormAttrs['method']);
  42. }
  43. }
  44. unset($oForm->aInputs['set_title']);
  45. $iId = (int)$oForm->insert(array('object' => $sObject, 'module' => BX_DOL_STUDIO_MODULE_CUSTOM, 'deletable' => 1, 'active' => 1));
  46. if($iId != 0)
  47. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  48. else
  49. $aRes = array('msg' => _t('_adm_nav_err_menus_create'));
  50. echoJson($aRes);
  51. }
  52. else {
  53. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-nav-menu-create-popup', _t('_adm_nav_txt_menus_create_popup'), $this->_oTemplate->parseHtmlByName('nav_add_menu.html', array(
  54. 'form_id' => $oForm->aFormAttrs['id'],
  55. 'form' => $oForm->getCode(true),
  56. 'object' => $this->_sObject,
  57. 'action' => $sAction
  58. )));
  59. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  60. }
  61. }
  62. public function performActionEdit()
  63. {
  64. $sAction = 'edit';
  65. $aMenu = $this->_getItem('getMenus');
  66. if($aMenu === false) {
  67. echoJson(array());
  68. exit;
  69. }
  70. $oForm = $this->_getFormObject($sAction, $aMenu);
  71. if($oForm->isSubmitted() && isset($oForm->aInputs['set_name']))
  72. $this->updateSetFields($oForm);
  73. $oForm->initChecker();
  74. if($oForm->isSubmittedAndValid()) {
  75. //--- New Set Creation
  76. if($oForm->getCleanValue('set_name') == $this->sCreateNew) {
  77. $sSetTitleValue = $oForm->getCleanValue('set_title');
  78. $sSetName = 'custom_' . $this->getSystemName($sSetTitleValue);
  79. $sSetTitleKey = '_adm_nav_txt_set_' . $sSetName;
  80. if($this->oDb->addSet(array('set_name' => $sSetName, 'module' => BX_DOL_STUDIO_MODULE_CUSTOM, 'title' => $sSetTitleKey, 'deletable' => 1))) {
  81. BxDolStudioLanguagesUtils::getInstance()->addLanguageString($sSetTitleKey, $sSetTitleValue);
  82. BxDolForm::setSubmittedValue('set_name', $sSetName, $oForm->aFormAttrs['method']);
  83. }
  84. }
  85. unset($oForm->aInputs['set_title']);
  86. if($oForm->update($aMenu['id']) !== false) {
  87. bx_content_cache_del_by_prefix('menu_' . $aMenu['object']);
  88. $aRes = array('grid' => $this->getCode(false), 'blink' => $aMenu['id']);
  89. } else {
  90. $aRes = array('msg' => _t('_adm_nav_err_menus_edit'));
  91. }
  92. echoJson($aRes);
  93. }
  94. else {
  95. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-nav-menu-edit-popup', _t('_adm_nav_txt_menus_edit_popup', _t($aMenu['title'])), $this->_oTemplate->parseHtmlByName('nav_add_menu.html', array(
  96. 'form_id' => $oForm->aFormAttrs['id'],
  97. 'form' => $oForm->getCode(true),
  98. 'object' => $this->_sObject,
  99. 'action' => $sAction
  100. )));
  101. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  102. }
  103. }
  104. public function performActionDelete()
  105. {
  106. $iAffected = 0;
  107. $aIds = bx_get('ids');
  108. if(!$aIds || !is_array($aIds))
  109. return echoJson([]);
  110. $aIdsAffected = [];
  111. foreach($aIds as $iId) {
  112. $aMenu = array();
  113. $iMenu = $this->oDb->getMenus(array('type' => 'by_id', 'value' => (int)$iId), $aMenu);
  114. if($iMenu != 1 || empty($aMenu) || !$this->_isDeletable($aMenu))
  115. continue;
  116. if((int)$this->_delete($iId) <= 0)
  117. continue;
  118. BxDolStudioLanguagesUtils::getInstance()->deleteLanguageString($aMenu['title']);
  119. $aIdsAffected[] = $iId;
  120. $iAffected++;
  121. }
  122. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_adm_nav_err_menus_delete')));
  123. }
  124. function getJsObject()
  125. {
  126. return 'oBxDolStudioNavigationMenus';
  127. }
  128. function getCode($isDisplayHeader = true)
  129. {
  130. return $this->_oTemplate->parseHtmlByName('nav_menus.html', array(
  131. 'content' => parent::getCode($isDisplayHeader),
  132. 'js_object' => $this->getJsObject(),
  133. 'grid_object' => $this->_sObject,
  134. 'params_divider' => $this->sParamsDivider
  135. ));
  136. }
  137. protected function _addJsCss()
  138. {
  139. parent::_addJsCss();
  140. $this->_oTemplate->addJs(array('jquery.form.min.js', 'navigation_menus.js'));
  141. $oForm = new BxTemplStudioFormView(array());
  142. $oForm->addCssJs();
  143. }
  144. protected function _getCellTitle ($mixedValue, $sKey, $aField, $aRow)
  145. {
  146. $aValue = $this->_limitMaxLength(_t($aRow['title']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow, false);
  147. $sLink = sprintf($this->sUrlViewItems, $aRow['module'], $aRow['set_name']);
  148. $mixedValue = $this->_oTemplate->parseLink($sLink, $aValue[0], array(
  149. 'title' => _t('_adm_nav_txt_manage_items')
  150. )) . (isset($aValue[1]) ? $aValue[1] : '');
  151. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  152. }
  153. protected function _getCellItems ($mixedValue, $sKey, $aField, $aRow)
  154. {
  155. if(!empty($aRow['set_name'])) {
  156. $aSets = array();
  157. $this->oDb->getSets(array('type' => 'by_name', 'value' => $aRow['set_name']), $aSets, false);
  158. $sLink = sprintf($this->sUrlViewItems, $aRow['module'], $aRow['set_name']);
  159. $mixedValue = $this->_oTemplate->parseLink($sLink, _t('_adm_nav_txt_n_items', $aSets['items_count']), array(
  160. 'title' => _t('_adm_nav_txt_manage_items')
  161. ));
  162. }
  163. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  164. }
  165. protected function _getCellModule($mixedValue, $sKey, $aField, $aRow)
  166. {
  167. $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  168. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  169. }
  170. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  171. {
  172. if($sType == 'single' && !$this->_isDeletable($aRow))
  173. return '';
  174. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  175. }
  176. protected function _getFilterControls ()
  177. {
  178. parent::_getFilterControls();
  179. return $this->getModulesSelectAll('getMenus') . $this->getSearchInput();
  180. }
  181. protected function _getFormObject($sAction, $aMenu = array())
  182. {
  183. bx_import('BxTemplStudioFormView');
  184. $aForm = array(
  185. 'form_attrs' => array(
  186. 'id' => 'adm-nav-menu-',
  187. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('grid.php', [
  188. 'o' => $this->_sObject,
  189. 'a' => $sAction,
  190. $this->_aOptions['filter_get'] => $this->_sFilter
  191. ]),
  192. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  193. ),
  194. 'params' => array (
  195. 'db' => array(
  196. 'table' => 'sys_objects_menu',
  197. 'key' => 'id',
  198. 'uri' => '',
  199. 'uri_title' => '',
  200. 'submit_name' => 'do_submit'
  201. ),
  202. ),
  203. 'inputs' => array (
  204. 'id' => array(
  205. 'type' => 'hidden',
  206. 'name' => 'id',
  207. 'value' => isset($aMenu['id']) ? (int)$aMenu['id'] : 0,
  208. 'db' => array (
  209. 'pass' => 'Int',
  210. ),
  211. ),
  212. 'title' => array(
  213. 'type' => 'text_translatable',
  214. 'name' => 'title',
  215. 'caption' => _t('_adm_nav_txt_menus_title'),
  216. 'info' => _t('_adm_nav_dsc_menus_title'),
  217. 'value' => isset($aMenu['title']) ? $aMenu['title'] : '_adm_nav_txt_menu',
  218. 'required' => '1',
  219. 'db' => array (
  220. 'pass' => 'Xss',
  221. ),
  222. 'checker' => array (
  223. 'func' => 'LengthTranslatable',
  224. 'params' => array(3, 100, 'title'),
  225. 'error' => _t('_adm_nav_err_menus_title'),
  226. ),
  227. ),
  228. 'title_public' => array(
  229. 'type' => 'text_translatable',
  230. 'name' => 'title_public',
  231. 'caption' => _t('_adm_nav_txt_menus_title_public'),
  232. 'info' => '',
  233. 'value' => $aMenu['title_public'] ?? '',
  234. 'required' => '0',
  235. 'db' => array (
  236. 'pass' => 'Xss',
  237. )
  238. ),
  239. 'set_name' => array(
  240. 'type' => 'select',
  241. 'name' => 'set_name',
  242. 'caption' => _t('_adm_nav_txt_menus_set_name'),
  243. 'info' => _t('_adm_nav_dsc_menus_set_name'),
  244. 'value' => isset($aMenu['set_name']) ? $aMenu['set_name'] : '',
  245. 'values' => array(
  246. array('key' => '', 'value' => _t('_adm_nav_txt_menus_set_name_select')),
  247. array('key' => $this->sCreateNew, 'value' => _t('_adm_nav_txt_menus_set_name_new'))
  248. ),
  249. 'required' => '1',
  250. 'attrs' => array(
  251. 'id' => 'bx-form-field-set-name',
  252. 'onchange' => $this->getJsObject() . ".onSelectSet(this)"
  253. ),
  254. 'db' => array (
  255. 'pass' => 'Xss',
  256. ),
  257. 'checker' => array (
  258. 'func' => 'avail',
  259. 'params' => array(),
  260. 'error' => _t('_adm_nav_err_menus_set_name'),
  261. ),
  262. ),
  263. 'set_title' => array(
  264. 'type' => 'text',
  265. 'name' => 'set_title',
  266. 'caption' => _t('_adm_nav_txt_menus_set_title'),
  267. 'info' => _t('_adm_nav_dsc_menus_set_title'),
  268. 'value' => '',
  269. 'required' => '1',
  270. 'tr_attrs' => array(
  271. 'style' => 'display:none'
  272. ),
  273. 'db' => array (
  274. 'pass' => 'Xss',
  275. ),
  276. 'checker' => array (
  277. 'func' => 'Avail',
  278. 'params' => array(),
  279. 'error' => _t('_adm_nav_err_menus_set_title'),
  280. ),
  281. ),
  282. 'template_id' => array(
  283. 'type' => 'select',
  284. 'name' => 'template_id',
  285. 'caption' => _t('_adm_nav_txt_menus_style'),
  286. 'info' => _t('_adm_nav_dsc_menus_style'),
  287. 'value' => isset($aMenu['template_id']) ? $aMenu['template_id'] : '',
  288. 'values' => array(
  289. array('key' => '', 'value' => _t('_adm_nav_txt_menus_style_select'))
  290. ),
  291. 'required' => '1',
  292. 'db' => array (
  293. 'pass' => 'Xss',
  294. ),
  295. 'checker' => array (
  296. 'func' => 'avail',
  297. 'params' => array(),
  298. 'error' => _t('_adm_nav_err_menus_style'),
  299. ),
  300. ),
  301. 'persistent' => array(
  302. 'type' => 'text',
  303. 'name' => 'persistent',
  304. 'caption' => _t('_adm_nav_txt_menus_persistent'),
  305. 'info' => _t('_adm_nav_dsc_menus_persistent'),
  306. 'value' => isset($aMenu['persistent']) ? $aMenu['persistent'] : 0,
  307. 'required' => '0',
  308. 'db' => array (
  309. 'pass' => 'Int',
  310. )
  311. ),
  312. 'controls' => array(
  313. 'name' => 'controls',
  314. 'type' => 'input_set',
  315. array(
  316. 'type' => 'submit',
  317. 'name' => 'do_submit',
  318. 'value' => _t('_adm_nav_btn_menus_add'),
  319. ),
  320. array (
  321. 'type' => 'reset',
  322. 'name' => 'close',
  323. 'value' => _t('_adm_nav_btn_menus_cancel'),
  324. 'attrs' => array(
  325. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  326. 'class' => 'bx-def-margin-sec-left',
  327. ),
  328. )
  329. )
  330. )
  331. );
  332. $aSets = array();
  333. $this->oDb->getSets(array('type' => 'all'), $aSets, false);
  334. foreach($aSets as $sSet)
  335. $aForm['inputs']['set_name']['values'][] = array('key' => $sSet['name'], 'value' => _t($sSet['title']));
  336. $aTemplates = array();
  337. $this->oDb->getTemplates(array('type' => 'all_visible_key_id'), $aTemplates, false);
  338. foreach($aTemplates as $aTemplate)
  339. $aForm['inputs']['template_id']['values'][] = array('key' => $aTemplate['id'], 'value' => _t($aTemplate['title']));
  340. switch($sAction){
  341. case 'add':
  342. unset($aForm['inputs']['id']);
  343. $aForm['form_attrs']['id'] .= 'create';
  344. break;
  345. case 'edit':
  346. $aForm['form_attrs']['id'] .= 'edit';
  347. $aForm['inputs']['set_title']['checker']['func'] = 'UniqueSet';
  348. if($aMenu['module'] != BX_DOL_STUDIO_MODULE_CUSTOM)
  349. $aForm['inputs']['template_id']['type'] = 'hidden';
  350. $aForm['inputs']['controls'][0]['value'] = _t('_adm_nav_btn_menus_save');
  351. break;
  352. }
  353. return new BxTemplStudioFormView($aForm);
  354. }
  355. protected function updateSetFields(&$oForm)
  356. {
  357. if($oForm->getCleanValue('set_name') != $this->sCreateNew)
  358. unset($oForm->aInputs['set_title']['checker']);
  359. else
  360. unset($oForm->aInputs['set_title']['tr_attrs']['style']);
  361. }
  362. protected function _isDeletable(&$aRow)
  363. {
  364. return (int)$aRow['deletable'] != 0;
  365. }
  366. }
  367. /** @} */