BxBaseStudioNavigationMenus.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. $aRes = array('grid' => $this->getCode(false), 'blink' => $aMenu['id']);
  88. else
  89. $aRes = array('msg' => _t('_adm_nav_err_menus_edit'));
  90. echoJson($aRes);
  91. }
  92. else {
  93. $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(
  94. 'form_id' => $oForm->aFormAttrs['id'],
  95. 'form' => $oForm->getCode(true),
  96. 'object' => $this->_sObject,
  97. 'action' => $sAction
  98. )));
  99. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  100. }
  101. }
  102. public function performActionDelete()
  103. {
  104. $iAffected = 0;
  105. $aIds = bx_get('ids');
  106. if(!$aIds || !is_array($aIds)) {
  107. echoJson(array());
  108. exit;
  109. }
  110. $aIdsAffected = array ();
  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))
  115. continue;
  116. if((int)$aMenu['deletable'] != 1)
  117. continue;
  118. if((int)$this->_delete($iId) <= 0)
  119. continue;
  120. BxDolStudioLanguagesUtils::getInstance()->deleteLanguageString($aMenu['title']);
  121. $aIdsAffected[] = $iId;
  122. $iAffected++;
  123. }
  124. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_adm_nav_err_menus_delete')));
  125. }
  126. function getJsObject()
  127. {
  128. return 'oBxDolStudioNavigationMenus';
  129. }
  130. function getCode($isDisplayHeader = true)
  131. {
  132. return $this->_oTemplate->parseHtmlByName('nav_menus.html', array(
  133. 'content' => parent::getCode($isDisplayHeader),
  134. 'js_object' => $this->getJsObject(),
  135. 'grid_object' => $this->_sObject,
  136. 'params_divider' => $this->sParamsDivider
  137. ));
  138. }
  139. protected function _addJsCss()
  140. {
  141. parent::_addJsCss();
  142. $this->_oTemplate->addJs(array('jquery.form.min.js', 'navigation_menus.js'));
  143. $oForm = new BxTemplStudioFormView(array());
  144. $oForm->addCssJs();
  145. }
  146. protected function _getCellTitle ($mixedValue, $sKey, $aField, $aRow)
  147. {
  148. $aValue = $this->_limitMaxLength(_t($aRow['title']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow, false);
  149. $sLink = sprintf($this->sUrlViewItems, $aRow['module'], $aRow['set_name']);
  150. $mixedValue = $this->_oTemplate->parseLink($sLink, $aValue[0], array(
  151. 'title' => _t('_adm_nav_txt_manage_items')
  152. )) . (isset($aValue[1]) ? $aValue[1] : '');
  153. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  154. }
  155. protected function _getCellItems ($mixedValue, $sKey, $aField, $aRow)
  156. {
  157. if(!empty($aRow['set_name'])) {
  158. $aSets = array();
  159. $this->oDb->getSets(array('type' => 'by_name', 'value' => $aRow['set_name']), $aSets, false);
  160. $sLink = sprintf($this->sUrlViewItems, $aRow['module'], $aRow['set_name']);
  161. $mixedValue = $this->_oTemplate->parseLink($sLink, _t('_adm_nav_txt_n_items', $aSets['items_count']), array(
  162. 'title' => _t('_adm_nav_txt_manage_items')
  163. ));
  164. }
  165. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  166. }
  167. protected function _getCellModule($mixedValue, $sKey, $aField, $aRow)
  168. {
  169. $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  170. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  171. }
  172. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  173. {
  174. if ($sType == 'single' && (int)$aRow['deletable'] != 1)
  175. return '';
  176. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  177. }
  178. protected function _getFilterControls ()
  179. {
  180. parent::_getFilterControls();
  181. return $this->getModulesSelectAll('getMenus') . $this->getSearchInput();
  182. }
  183. protected function _getFormObject($sAction, $aMenu = array())
  184. {
  185. bx_import('BxTemplStudioFormView');
  186. $aForm = array(
  187. 'form_attrs' => array(
  188. 'id' => 'adm-nav-menu-',
  189. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction,
  190. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  191. ),
  192. 'params' => array (
  193. 'db' => array(
  194. 'table' => 'sys_objects_menu',
  195. 'key' => 'id',
  196. 'uri' => '',
  197. 'uri_title' => '',
  198. 'submit_name' => 'do_submit'
  199. ),
  200. ),
  201. 'inputs' => array (
  202. 'id' => array(
  203. 'type' => 'hidden',
  204. 'name' => 'id',
  205. 'value' => isset($aMenu['id']) ? (int)$aMenu['id'] : 0,
  206. 'db' => array (
  207. 'pass' => 'Int',
  208. ),
  209. ),
  210. 'title' => array(
  211. 'type' => 'text_translatable',
  212. 'name' => 'title',
  213. 'caption' => _t('_adm_nav_txt_menus_title'),
  214. 'info' => _t('_adm_nav_dsc_menus_title'),
  215. 'value' => isset($aMenu['title']) ? $aMenu['title'] : '_adm_nav_txt_menu',
  216. 'required' => '1',
  217. 'db' => array (
  218. 'pass' => 'Xss',
  219. ),
  220. 'checker' => array (
  221. 'func' => 'LengthTranslatable',
  222. 'params' => array(3, 100, 'title'),
  223. 'error' => _t('_adm_nav_err_menus_title'),
  224. ),
  225. ),
  226. 'set_name' => array(
  227. 'type' => 'select',
  228. 'name' => 'set_name',
  229. 'caption' => _t('_adm_nav_txt_menus_set_name'),
  230. 'info' => _t('_adm_nav_dsc_menus_set_name'),
  231. 'value' => isset($aMenu['set_name']) ? $aMenu['set_name'] : '',
  232. 'values' => array(
  233. array('key' => '', 'value' => _t('_adm_nav_txt_menus_set_name_select')),
  234. array('key' => $this->sCreateNew, 'value' => _t('_adm_nav_txt_menus_set_name_new'))
  235. ),
  236. 'required' => '1',
  237. 'attrs' => array(
  238. 'id' => 'bx-form-field-set-name',
  239. 'onchange' => $this->getJsObject() . ".onSelectSet(this)"
  240. ),
  241. 'db' => array (
  242. 'pass' => 'Xss',
  243. ),
  244. 'checker' => array (
  245. 'func' => 'avail',
  246. 'params' => array(),
  247. 'error' => _t('_adm_nav_err_menus_set_name'),
  248. ),
  249. ),
  250. 'set_title' => array(
  251. 'type' => 'text',
  252. 'name' => 'set_title',
  253. 'caption' => _t('_adm_nav_txt_menus_set_title'),
  254. 'info' => _t('_adm_nav_dsc_menus_set_title'),
  255. 'value' => '',
  256. 'required' => '1',
  257. 'tr_attrs' => array(
  258. 'style' => 'display:none'
  259. ),
  260. 'db' => array (
  261. 'pass' => 'Xss',
  262. ),
  263. 'checker' => array (
  264. 'func' => 'Avail',
  265. 'params' => array(),
  266. 'error' => _t('_adm_nav_err_menus_set_title'),
  267. ),
  268. ),
  269. 'template_id' => array(
  270. 'type' => 'select',
  271. 'name' => 'template_id',
  272. 'caption' => _t('_adm_nav_txt_menus_style'),
  273. 'info' => _t('_adm_nav_dsc_menus_style'),
  274. 'value' => isset($aMenu['template_id']) ? $aMenu['template_id'] : '',
  275. 'values' => array(
  276. array('key' => '', 'value' => _t('_adm_nav_txt_menus_style_select'))
  277. ),
  278. 'required' => '1',
  279. 'db' => array (
  280. 'pass' => 'Xss',
  281. ),
  282. 'checker' => array (
  283. 'func' => 'avail',
  284. 'params' => array(),
  285. 'error' => _t('_adm_nav_err_menus_style'),
  286. ),
  287. ),
  288. 'controls' => array(
  289. 'name' => 'controls',
  290. 'type' => 'input_set',
  291. array(
  292. 'type' => 'submit',
  293. 'name' => 'do_submit',
  294. 'value' => _t('_adm_nav_btn_menus_add'),
  295. ),
  296. array (
  297. 'type' => 'reset',
  298. 'name' => 'close',
  299. 'value' => _t('_adm_nav_btn_menus_cancel'),
  300. 'attrs' => array(
  301. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  302. 'class' => 'bx-def-margin-sec-left',
  303. ),
  304. )
  305. )
  306. )
  307. );
  308. $aSets = array();
  309. $this->oDb->getSets(array('type' => 'all'), $aSets, false);
  310. foreach($aSets as $sSet)
  311. $aForm['inputs']['set_name']['values'][] = array('key' => $sSet['name'], 'value' => _t($sSet['title']));
  312. $aTemplates = array();
  313. $this->oDb->getTemplates(array('type' => 'all_visible_key_id'), $aTemplates, false);
  314. foreach($aTemplates as $aTemplate)
  315. $aForm['inputs']['template_id']['values'][] = array('key' => $aTemplate['id'], 'value' => _t($aTemplate['title']));
  316. switch($sAction){
  317. case 'add':
  318. unset($aForm['inputs']['id']);
  319. $aForm['form_attrs']['id'] .= 'create';
  320. break;
  321. case 'edit':
  322. $aForm['form_attrs']['id'] .= 'edit';
  323. $aForm['inputs']['set_title']['checker']['func'] = 'UniqueSet';
  324. if($aMenu['module'] != BX_DOL_STUDIO_MODULE_CUSTOM)
  325. $aForm['inputs']['template_id']['type'] = 'hidden';
  326. $aForm['inputs']['controls'][0]['value'] = _t('_adm_nav_btn_menus_save');
  327. break;
  328. }
  329. return new BxTemplStudioFormView($aForm);
  330. }
  331. protected function updateSetFields(&$oForm)
  332. {
  333. if($oForm->getCleanValue('set_name') != $this->sCreateNew)
  334. unset($oForm->aInputs['set_title']['checker']);
  335. else
  336. unset($oForm->aInputs['set_title']['tr_attrs']['style']);
  337. }
  338. }
  339. /** @} */