BxBaseStudioNavigationMenus.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentView Trident Studio Representation classes
  7. * @ingroup TridentStudio
  8. * @{
  9. */
  10. class BxBaseStudioNavigationMenus extends BxDolStudioNavigationMenus
  11. {
  12. private $sCreateNew = 'sys_create_new';
  13. protected $sUrlViewItems;
  14. function __construct($aOptions, $oTemplate = false)
  15. {
  16. parent::__construct($aOptions, $oTemplate);
  17. $this->_aOptions['actions_single']['edit']['attr']['title'] = _t('_adm_nav_btn_menus_edit');
  18. $this->_aOptions['actions_single']['delete']['attr']['title'] = _t('_adm_nav_btn_menus_delete');
  19. $this->sUrlViewItems = BX_DOL_URL_STUDIO . 'builder_menu.php?page=items&module=%s&set=%s';
  20. }
  21. public function performActionAdd()
  22. {
  23. $sAction = 'add';
  24. $oForm = $this->_getFormObject($sAction);
  25. if($oForm->isSubmitted() && isset($oForm->aInputs['set_name']))
  26. $this->updateSetFields($oForm);
  27. $oForm->initChecker();
  28. if($oForm->isSubmittedAndValid()) {
  29. bx_import('BxDolStudioUtils');
  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', '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', '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. bx_import('BxDolStudioUtils');
  76. //--- New Set Creation
  77. if($oForm->getCleanValue('set_name') == $this->sCreateNew) {
  78. $sSetTitleValue = $oForm->getCleanValue('set_title');
  79. $sSetName = 'custom_' . $this->getSystemName($sSetTitleValue);
  80. $sSetTitleKey = '_adm_nav_txt_set_' . $sSetName;
  81. if($this->oDb->addSet(array('set_name' => $sSetName, 'module' => BX_DOL_STUDIO_MODULE_CUSTOM, 'title' => $sSetTitleKey, 'deletable' => 1))) {
  82. BxDolStudioLanguagesUtils::getInstance()->addLanguageString($sSetTitleKey, $sSetTitleValue);
  83. BxDolForm::setSubmittedValue('set_name', $sSetName, $oForm->aFormAttrs['method']);
  84. }
  85. }
  86. unset($oForm->aInputs['set_title']);
  87. if($oForm->update($aMenu['id']) !== false)
  88. $aRes = array('grid' => $this->getCode(false), 'blink' => $aMenu['id']);
  89. else
  90. $aRes = array('msg' => _t('_adm_nav_err_menus_edit'));
  91. echoJson($aRes);
  92. }
  93. else {
  94. $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(
  95. 'form_id' => $oForm->aFormAttrs['id'],
  96. 'form' => $oForm->getCode(true),
  97. 'object' => $this->_sObject,
  98. 'action' => $sAction
  99. )));
  100. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  101. }
  102. }
  103. public function performActionDelete()
  104. {
  105. $iAffected = 0;
  106. $aIds = bx_get('ids');
  107. if(!$aIds || !is_array($aIds)) {
  108. echoJson(array());
  109. exit;
  110. }
  111. $aIdsAffected = array ();
  112. foreach($aIds as $iId) {
  113. $aMenu = array();
  114. $iMenu = $this->oDb->getMenus(array('type' => 'by_id', 'value' => (int)$iId), $aMenu);
  115. if($iMenu != 1 || empty($aMenu))
  116. continue;
  117. if((int)$aMenu['deletable'] != 1)
  118. continue;
  119. if((int)$this->_delete($iId) <= 0)
  120. continue;
  121. BxDolStudioLanguagesUtils::getInstance()->deleteLanguageString($aMenu['title']);
  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. $mixedValue = $this->_oTemplate->parseHtmlByName('bx_a.html', array(
  151. 'href' => sprintf($this->sUrlViewItems, $aRow['module'], $aRow['set_name']),
  152. 'title' => _t('_adm_nav_txt_manage_items'),
  153. 'bx_repeat:attrs' => array(),
  154. 'content' => $aValue[0]
  155. )) . (isset($aValue[1]) ? $aValue[1] : '');
  156. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  157. }
  158. protected function _getCellItems ($mixedValue, $sKey, $aField, $aRow)
  159. {
  160. if(!empty($aRow['set_name'])) {
  161. $aSets = array();
  162. $this->oDb->getSets(array('type' => 'by_name', 'value' => $aRow['set_name']), $aSets, false);
  163. $mixedValue = $this->_oTemplate->parseHtmlByName('bx_a.html', array(
  164. 'href' => sprintf($this->sUrlViewItems, $aRow['module'], $aRow['set_name']),
  165. 'title' => _t('_adm_nav_txt_manage_items'),
  166. 'bx_repeat:attrs' => array(),
  167. 'content' => _t('_adm_nav_txt_n_items', $aSets['items_count'])
  168. ));
  169. }
  170. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  171. }
  172. protected function _getCellModule($mixedValue, $sKey, $aField, $aRow)
  173. {
  174. $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  175. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  176. }
  177. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  178. {
  179. if ($sType == 'single' && (int)$aRow['deletable'] != 1)
  180. return '';
  181. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  182. }
  183. protected function _getFilterControls ()
  184. {
  185. parent::_getFilterControls();
  186. return $this->getModulesSelectAll('getMenus') . $this->getSearchInput();
  187. }
  188. protected function _getFormObject($sAction, $aMenu = array())
  189. {
  190. bx_import('BxTemplStudioFormView');
  191. $aForm = array(
  192. 'form_attrs' => array(
  193. 'id' => 'adm-nav-menu-',
  194. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction,
  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. 'set_name' => array(
  232. 'type' => 'select',
  233. 'name' => 'set_name',
  234. 'caption' => _t('_adm_nav_txt_menus_set_name'),
  235. 'info' => _t('_adm_nav_dsc_menus_set_name'),
  236. 'value' => isset($aMenu['set_name']) ? $aMenu['set_name'] : '',
  237. 'values' => array(
  238. array('key' => '', 'value' => _t('_adm_nav_txt_menus_set_name_select')),
  239. array('key' => $this->sCreateNew, 'value' => _t('_adm_nav_txt_menus_set_name_new'))
  240. ),
  241. 'required' => '1',
  242. 'attrs' => array(
  243. 'id' => 'bx-form-field-set-name',
  244. 'onchange' => $this->getJsObject() . ".onSelectSet(this)"
  245. ),
  246. 'db' => array (
  247. 'pass' => 'Xss',
  248. ),
  249. 'checker' => array (
  250. 'func' => 'avail',
  251. 'params' => array(),
  252. 'error' => _t('_adm_nav_err_menus_set_name'),
  253. ),
  254. ),
  255. 'set_title' => array(
  256. 'type' => 'text',
  257. 'name' => 'set_title',
  258. 'caption' => _t('_adm_nav_txt_menus_set_title'),
  259. 'info' => _t('_adm_nav_dsc_menus_set_title'),
  260. 'value' => '',
  261. 'required' => '1',
  262. 'tr_attrs' => array(
  263. 'style' => 'display:none'
  264. ),
  265. 'db' => array (
  266. 'pass' => 'Xss',
  267. ),
  268. 'checker' => array (
  269. 'func' => 'Avail',
  270. 'params' => array(),
  271. 'error' => _t('_adm_nav_err_menus_set_title'),
  272. ),
  273. ),
  274. 'template_id' => array(
  275. 'type' => 'select',
  276. 'name' => 'template_id',
  277. 'caption' => _t('_adm_nav_txt_menus_style'),
  278. 'info' => _t('_adm_nav_dsc_menus_style'),
  279. 'value' => isset($aMenu['template_id']) ? $aMenu['template_id'] : '',
  280. 'values' => array(
  281. array('key' => '', 'value' => _t('_adm_nav_txt_menus_style_select'))
  282. ),
  283. 'required' => '1',
  284. 'db' => array (
  285. 'pass' => 'Xss',
  286. ),
  287. 'checker' => array (
  288. 'func' => 'avail',
  289. 'params' => array(),
  290. 'error' => _t('_adm_nav_err_menus_style'),
  291. ),
  292. ),
  293. 'controls' => array(
  294. 'name' => 'controls',
  295. 'type' => 'input_set',
  296. array(
  297. 'type' => 'submit',
  298. 'name' => 'do_submit',
  299. 'value' => _t('_adm_nav_btn_menus_add'),
  300. ),
  301. array (
  302. 'type' => 'reset',
  303. 'name' => 'close',
  304. 'value' => _t('_adm_nav_btn_menus_cancel'),
  305. 'attrs' => array(
  306. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  307. 'class' => 'bx-def-margin-sec-left',
  308. ),
  309. )
  310. )
  311. )
  312. );
  313. $aSets = array();
  314. $this->oDb->getSets(array('type' => 'all'), $aSets, false);
  315. foreach($aSets as $sSet)
  316. $aForm['inputs']['set_name']['values'][] = array('key' => $sSet['name'], 'value' => _t($sSet['title']));
  317. $aTemplates = array();
  318. $this->oDb->getTemplates(array('type' => 'all_visible_key_id'), $aTemplates, false);
  319. foreach($aTemplates as $aTemplate)
  320. $aForm['inputs']['template_id']['values'][] = array('key' => $aTemplate['id'], 'value' => _t($aTemplate['title']));
  321. switch($sAction){
  322. case 'add':
  323. unset($aForm['inputs']['id']);
  324. $aForm['form_attrs']['id'] .= 'create';
  325. break;
  326. case 'edit':
  327. $aForm['form_attrs']['id'] .= 'edit';
  328. $aForm['inputs']['set_title']['checker']['func'] = 'UniqueSet';
  329. $iTemplateId = (int)$aForm['inputs']['template_id']['value'];
  330. if(!empty($iTemplateId) && !array_key_exists($iTemplateId, $aTemplates))
  331. $aForm['inputs']['template_id']['type'] = 'hidden';
  332. $aForm['inputs']['controls'][0]['value'] = _t('_adm_nav_btn_menus_save');
  333. break;
  334. }
  335. return new BxTemplStudioFormView($aForm);
  336. }
  337. protected function updateSetFields(&$oForm)
  338. {
  339. if($oForm->getCleanValue('set_name') != $this->sCreateNew)
  340. unset($oForm->aInputs['set_title']['checker']);
  341. else
  342. unset($oForm->aInputs['set_title']['tr_attrs']['style']);
  343. }
  344. }
  345. /** @} */