BxBaseStudioNavigationMenus.php 17 KB

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