BxBaseStudioNavigationMenus.php 16 KB

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