BxDevBuilderPage.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 Developer Developer
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxDevBuilderPage extends BxTemplStudioBuilderPage
  12. {
  13. protected $sActionPageExport = 'page_export';
  14. protected $oModule;
  15. protected $aParams;
  16. function __construct($aParams)
  17. {
  18. parent::__construct(isset($aParams['type']) ? $aParams['type'] : '', isset($aParams['page']) ? $aParams['page'] : '');
  19. $this->aParams = $aParams;
  20. $this->sBaseUrl = $this->aParams['url'];
  21. $this->sTypeUrl = $this->sBaseUrl . '&bp_type=%s';
  22. $this->sPageUrl = $this->sTypeUrl . '&bp_page=%s';
  23. $this->oModule = BxDolModule::getInstance('bx_developer');
  24. $this->oModule->_oTemplate->addStudioCss(array('builder_page.css'));
  25. }
  26. function getBlockPanelTop($aBlock)
  27. {
  28. return BxBaseStudioWidget::getBlockPanelTop(
  29. array('panel_top' => $this->oModule->_oTemplate->parseHtmlByName('bp_block_panel_top.html', $this->_getTmplVarsBlockPanelTop()))
  30. );
  31. }
  32. protected function actionPageCreate()
  33. {
  34. $sFormObject = $this->oModule->_oConfig->getObject('form_bp_page');
  35. $sFormDisplay = $this->oModule->_oConfig->getObject('form_display_bp_page_add');
  36. $oForm = BxDolForm::getObjectInstance($sFormObject, $sFormDisplay, $this->oModule->_oTemplate);
  37. $oForm->aFormAttrs['action'] = sprintf($this->sPageUrl, $this->sType, $this->sPage) . '&bp_action=' . $this->sActionPageCreate;
  38. $oForm->aInputs['module']['values'] = array_merge(array('' => _t('_bx_dev_bp_txt_select_module')), BxDolStudioUtils::getModules());
  39. $oForm->aInputs['layout_id']['values'] = array(
  40. array('key' => '', 'value' => _t('_bx_dev_bp_txt_page_layout_id_select'))
  41. );
  42. $aLayouts = array();
  43. $this->oDb->getLayouts(array('type' => 'all'), $aLayouts, false);
  44. foreach($aLayouts as $aLayout)
  45. $oForm->aInputs['layout_id']['values'][] = array('key' => $aLayout['id'], 'value' => _t($aLayout['title']));
  46. $oForm->initChecker();
  47. if($oForm->isSubmittedAndValid()) {
  48. $sObject = $oForm->getCleanValue('object');
  49. $sModule = $oForm->getCleanValue('module');
  50. if(($iId = (int)$oForm->insert()) != 0)
  51. return array('eval' => $this->getPageJsObject() . '.onCreatePage(\'' . $sModule . '\', \'' . $sObject . '\')');
  52. else
  53. return array('msg' => _t('_bx_dev_bp_err_page_create'));
  54. }
  55. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($this->aHtmlIds['add_popup_id'], _t('_bx_dev_bp_txt_page_create_popup'), $this->oModule->_oTemplate->parseHtmlByName('bp_add_page.html', array(
  56. 'form_id' => $oForm->aFormAttrs['id'],
  57. 'form' => $oForm->getCode(true)
  58. )));
  59. return array('popup' => $sContent);
  60. }
  61. protected function actionPageExport()
  62. {
  63. $sContentInsert = $sContentDelete = "";
  64. $aPage = array();
  65. $this->oDb->getPages(array('type' => 'by_object', 'value' => $this->sPage), $aPage, false);
  66. if(empty($aPage) || !is_array($aPage))
  67. return array();
  68. $sContentInsert .= ($this->oModule->_oDb->getQueryInsert('sys_objects_page', array($aPage), "Dumping data for '" . $aPage['object'] . "' page"));
  69. $sContentDelete .= ($this->oModule->_oDb->getQueryDelete('sys_objects_page', 'object', array($aPage), "Deleting data for '" . $aPage['object'] . "' page"));
  70. $aBlocks = array();
  71. $this->oDb->getBlocks(array('type' => 'by_object', 'value' => $aPage['object']), $aBlocks, false);
  72. $sContentInsert .= $this->oModule->_oDb->getQueryInsert('sys_pages_blocks', $aBlocks, false, array('id', 'name'));
  73. $sContentDelete .= $this->oModule->_oDb->getQueryDelete('sys_pages_blocks', 'object', array($aPage), false);
  74. $aForm = array(
  75. 'form_attrs' => array(),
  76. 'inputs' => array (
  77. 'insert' => array(
  78. 'type' => 'textarea',
  79. 'name' => 'insert',
  80. 'caption' => _t('_bx_dev_bp_txt_page_export_insert'),
  81. 'value' => $sContentInsert,
  82. ),
  83. 'delete' => array(
  84. 'type' => 'textarea',
  85. 'name' => 'delete',
  86. 'caption' => _t('_bx_dev_bp_txt_page_export_delete'),
  87. 'value' => $sContentDelete,
  88. ),
  89. 'done' => array (
  90. 'type' => 'button',
  91. 'name' => 'done',
  92. 'value' => _t('_bx_dev_bp_btn_page_done'),
  93. 'attrs' => array(
  94. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  95. ),
  96. )
  97. )
  98. );
  99. $oForm = new BxTemplStudioFormView($aForm);
  100. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('bx-dev-bp-page-export-popup', _t('_bx_dev_bp_txt_page_export_popup', _t($aPage['title'])), $this->oModule->_oTemplate->parseHtmlByName('bp_export.html', array(
  101. 'content' => $oForm->getCode()
  102. )));
  103. return array('popup' => $sContent);
  104. }
  105. protected function actionBlockEdit()
  106. {
  107. $sFormObject = $this->oModule->_oConfig->getObject('form_bp_block');
  108. $sFormDisplay = $this->oModule->_oConfig->getObject('form_display_bp_block_edit');
  109. $iId = (int)bx_get('id');
  110. if(!$iId)
  111. return array();
  112. $aBlock = array();
  113. $this->oDb->getBlocks(array('type' => 'by_id', 'value' => $iId), $aBlock, false);
  114. if(empty($aBlock) || !is_array($aBlock))
  115. return array('msg' => _t('_bx_dev_bp_err_block_not_found'));
  116. $oForm = BxDolForm::getObjectInstance($sFormObject, $sFormDisplay, $this->oModule->_oTemplate);
  117. $this->onLoadBlock($oForm, $aBlock);
  118. $oForm->initChecker($aBlock);
  119. if($oForm->isSubmittedAndValid()) {
  120. $this->onSaveBlock($oForm, $aBlock);
  121. if($oForm->update($iId) !== false)
  122. return array('eval' => $this->getPageJsObject() . '.onEditBlock(oData)');
  123. else
  124. return array('msg' => _t('_bx_dev_bp_err_block_edit'));
  125. }
  126. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($this->aHtmlIds['edit_block_popup_id'], _t('_bx_dev_bp_txt_block_edit_popup', _t($aBlock['title'])), $this->oModule->_oTemplate->parseHtmlByName('bp_add_block.html', array(
  127. 'form_id' => $oForm->aFormAttrs['id'],
  128. 'form' => $oForm->getCode(true)
  129. )));
  130. return array('popup' => $sContent);
  131. }
  132. protected function actionBlockDelete()
  133. {
  134. $sJsObject = $this->getPageJsObject();
  135. $iId = (int)bx_get('id');
  136. $aBlock = array();
  137. $this->oDb->getBlocks(array('type' => 'by_id', 'value' => $iId), $aBlock, false);
  138. if(empty($aBlock) || !is_array($aBlock))
  139. return array('msg' => _t('_bx_dev_bp_err_block_not_found'));
  140. if(!$this->oDb->deleteBlocks(array('type' => 'by_id', 'value' => $iId)))
  141. return array('msg' => _t('_bx_dev_bp_err_block_delete'));
  142. $this->onBlockDelete($aBlock);
  143. return array('eval' => $sJsObject . '.onDeleteBlock(' . $iId . ', oData)');
  144. }
  145. protected function getSettingsOptions($aPage = array(), $bCreate = true, $bInputsOnly = false)
  146. {
  147. $aForm = array(
  148. 'form_attrs' => array(
  149. 'id' => 'adm-bp-settings-seo',
  150. ),
  151. 'params' => array (
  152. 'remove_form' => '1',
  153. 'csrf' => array(
  154. 'disable' => true
  155. )
  156. ),
  157. 'inputs' => array(
  158. 'object' => array(
  159. 'type' => 'text',
  160. 'name' => 'object',
  161. 'caption' => _t('_bx_dev_bp_txt_page_object'),
  162. 'info' => '',
  163. 'value' => $this->aPageRebuild['object'],
  164. 'required' => '',
  165. 'attrs' => array(
  166. 'disabled' => 'disabled'
  167. ),
  168. )
  169. )
  170. );
  171. $aForm['inputs'] += parent::getSettingsOptions($aPage, $bCreate, true);
  172. $aForm['inputs']['title_system']['type'] = 'text';
  173. $aForm['inputs']['title_system']['caption'] = _t('_bx_dev_bp_txt_page_title_system');
  174. $aForm['inputs']['title']['type'] = 'text';
  175. $aUri = array(
  176. 'uri' => array(
  177. 'type' => 'text',
  178. 'name' => 'uri',
  179. 'caption' => _t('_bx_dev_bp_txt_page_uri'),
  180. 'info' => '',
  181. 'value' => $this->aPageRebuild['uri'],
  182. 'required' => '',
  183. 'db' => array (
  184. 'pass' => 'Xss',
  185. ),
  186. )
  187. );
  188. $aForm['inputs'] = bx_array_insert_before($aUri, $aForm['inputs'], 'url');
  189. $aForm['inputs']['url']['caption'] = _t('_bx_dev_bp_txt_page_url');
  190. $aForm['inputs']['url']['value'] = isset($aPage['url']) ? $aPage['url'] : '';
  191. $aForm['inputs']['url']['db'] = array (
  192. 'pass' => 'Xss',
  193. );
  194. unset($aForm['inputs']['url']['attrs']['disabled']);
  195. $aForm['inputs']['deletable'] = array(
  196. 'type' => 'switcher',
  197. 'name' => 'deletable',
  198. 'caption' => _t('_bx_dev_bp_txt_page_deletable'),
  199. 'info' => '',
  200. 'value' => '1',
  201. 'checked' => (int)$this->aPageRebuild['deletable'] == 1,
  202. 'required' => '',
  203. 'db' => array (
  204. 'pass' => 'Int',
  205. )
  206. );
  207. $aForm['inputs']['override_class_name'] = array(
  208. 'type' => 'text',
  209. 'name' => 'override_class_name',
  210. 'caption' => _t('_bx_dev_bp_txt_page_override_class_name'),
  211. 'info' => '',
  212. 'value' => $this->aPageRebuild['override_class_name'],
  213. 'required' => '0',
  214. 'db' => array (
  215. 'pass' => 'Xss',
  216. )
  217. );
  218. $aForm['inputs']['override_class_file'] = array(
  219. 'type' => 'text',
  220. 'name' => 'override_class_file',
  221. 'caption' => _t('_bx_dev_bp_txt_page_override_class_file'),
  222. 'info' => '',
  223. 'value' => $this->aPageRebuild['override_class_file'],
  224. 'required' => '0',
  225. 'db' => array (
  226. 'pass' => 'Xss',
  227. )
  228. );
  229. if($bInputsOnly)
  230. return $aForm['inputs'];
  231. $oForm = new BxTemplStudioFormView($aForm);
  232. return $oForm->getCode();
  233. }
  234. protected function getSettingsCache($aPage = array(), $bCreate = true, $bInputsOnly = false)
  235. {
  236. $aForm = array(
  237. 'form_attrs' => array(
  238. 'id' => 'adm-bp-settings-cache',
  239. ),
  240. 'params' => array (
  241. 'remove_form' => '1',
  242. 'csrf' => array(
  243. 'disable' => true
  244. )
  245. ),
  246. 'inputs' => parent::getSettingsCache($aPage, $bCreate, true)
  247. );
  248. unset($aForm['inputs']['cache_lifetime']['attrs']);
  249. $aForm['inputs']['cache_editable'] = array(
  250. 'type' => 'switcher',
  251. 'name' => 'cache_editable',
  252. 'caption' => _t('_bx_dev_bp_txt_page_cache_editable'),
  253. 'info' => '',
  254. 'value' => '1',
  255. 'checked' => (int)$this->aPageRebuild['cache_editable'] == 1,
  256. 'required' => '',
  257. 'db' => array (
  258. 'pass' => 'Int',
  259. )
  260. );
  261. if($bInputsOnly)
  262. return $aForm['inputs'];
  263. $oForm = new BxTemplStudioFormView($aForm);
  264. return $oForm->getCode();
  265. }
  266. protected function getBlockModule($aBlock)
  267. {
  268. return $aBlock['module'] != BX_DOL_STUDIO_BP_SKELETONS ? $aBlock['module'] : $this->aPageRebuild['module'];
  269. }
  270. protected function getBlockContent($aBlock)
  271. {
  272. $aFields = array();
  273. switch($aBlock['type']) {
  274. case BX_DOL_STUDIO_BP_BLOCK_SERVICE:
  275. $aFields = array(
  276. 'content' => array(
  277. 'type' => 'textarea',
  278. 'name' => 'content',
  279. 'caption' => _t('_bx_dev_bp_txt_block_content_service'),
  280. 'info' => '',
  281. 'value' => $aBlock['content'],
  282. 'required' => '0',
  283. 'db' => array (
  284. 'pass' => 'Xss',
  285. ),
  286. )
  287. );
  288. break;
  289. case BX_DOL_STUDIO_BP_BLOCK_LANG:
  290. $aFields = array(
  291. 'content' => array(
  292. 'type' => 'text',
  293. 'name' => 'content',
  294. 'caption' => _t('_bx_dev_bp_txt_block_content_lang'),
  295. 'info' => _t('_bx_dev_bp_dsc_block_content_lang'),
  296. 'value' => $aBlock['content'],
  297. 'required' => '0',
  298. 'db' => array (
  299. 'pass' => 'Xss',
  300. ),
  301. ),
  302. );
  303. break;
  304. default:
  305. $aFields = parent::getBlockContent($aBlock);
  306. break;
  307. }
  308. return $aFields;
  309. }
  310. protected function getBlocks($sModule)
  311. {
  312. $aBlocks = parent::getBlocks($sModule);
  313. if($sModule == BX_DOL_STUDIO_BP_SKELETONS) {
  314. $aBlock = array();
  315. $this->oDb->getBlocks(array('type' => 'skeleton_by_type', 'value' => 'service'), $aBlock, false);
  316. if(!empty($aBlock) && is_array($aBlock))
  317. $aBlocks[] = $aBlock;
  318. }
  319. return $aBlocks;
  320. }
  321. protected function onLoadBlock(&$oForm, &$aBlock)
  322. {
  323. $oForm->aFormAttrs['action'] = sprintf($this->sPageUrl, $this->sType, $this->sPage) . '&bp_action=' . $this->sActionBlockEdit;
  324. $oForm->aInputs['module']['values'] = array_merge(array('' => _t('_bx_dev_bp_txt_select_module')), BxDolStudioUtils::getModules());
  325. $aDBoxes = array();
  326. $this->oDb->getDesignBoxes(array('type' => 'ordered'), $aDBoxes, false);
  327. foreach($aDBoxes as $aDBox)
  328. $oForm->aInputs['designbox_id']['values'][] = array('key' => $this->sSelectKeyPrefix . $aDBox['id'], 'value' => _t($aDBox['title']));
  329. $oForm->aInputs['visible_for']['value'] = $aBlock['visible_for_levels'] == BX_DOL_INT_MAX ? BX_DOL_STUDIO_VISIBLE_ALL : BX_DOL_STUDIO_VISIBLE_SELECTED;
  330. $oForm->aInputs['visible_for']['values'] = array(
  331. array('key' => BX_DOL_STUDIO_VISIBLE_ALL, 'value' => _t('_bx_dev_bp_txt_block_visible_for_all')),
  332. array('key' => BX_DOL_STUDIO_VISIBLE_SELECTED, 'value' => _t('_bx_dev_bp_txt_block_visible_for_selected')),
  333. );
  334. $oForm->aInputs['visible_for']['attrs']['onchange'] = $this->getPageJsObject() . '.onChangeVisibleFor(this)';
  335. $oForm->aInputs['visible_for_levels']['tr_attrs']['style'] = $aBlock['visible_for_levels'] == BX_DOL_INT_MAX ? 'display:none' : '';
  336. BxDolStudioUtils::getVisibilityValues($aBlock['visible_for_levels'], $oForm->aInputs['visible_for_levels']['values'], $oForm->aInputs['visible_for_levels']['value']);
  337. $aBlock['visible_for_levels'] = $oForm->aInputs['visible_for_levels']['value'];
  338. $oForm->aInputs = $this->addInArray($oForm->aInputs, 'visible_for_levels', $this->getBlockContent($aBlock));
  339. $oForm->aInputs['controls'][0]['value'] = _t('_bx_dev_bp_btn_block_save');
  340. $oForm->aInputs['controls'][2]['attrs']['onclick'] = $this->getPageJsObject() . ".deleteBlock(" . $aBlock['id'] . ")";
  341. if($aBlock['type'] == BX_DOL_STUDIO_BP_BLOCK_SERVICE)
  342. $aBlock['content'] = BxDevFunctions::unserializeString($aBlock['content']);
  343. $aBlock['designbox_id'] = $this->sSelectKeyPrefix . $aBlock['designbox_id'];
  344. }
  345. protected function onSaveBlock(&$oForm, &$aBlock)
  346. {
  347. parent::onSaveBlock($oForm, $aBlock);
  348. if($aBlock['type'] == BX_DOL_STUDIO_BP_BLOCK_SERVICE && isset($oForm->aInputs['content']))
  349. $this->onSaveBlockService($oForm, $aBlock);
  350. }
  351. protected function onSaveBlockLang(&$oForm, &$aBlock)
  352. {
  353. $sValue = $oForm->getCleanValue('content');
  354. BxDolForm::setSubmittedValue('content', $sValue, $oForm->aFormAttrs['method']);
  355. }
  356. protected function onSaveBlockService(&$oForm, &$aBlock)
  357. {
  358. $sValue = $oForm->getCleanValue('content');
  359. $sValue = BxDevFunctions::serializeString($sValue);
  360. BxDolForm::setSubmittedValue('content', $sValue, $oForm->aFormAttrs['method']);
  361. }
  362. protected function _getTmplVarsBlockPanelTopActions()
  363. {
  364. $sJsObject = $this->getPageJsObject();
  365. $oPermalinks = BxDolPermalinks::getInstance();
  366. return array(
  367. 'js_object' => $sJsObject,
  368. 'url_view' => BX_DOL_URL_ROOT . $oPermalinks->permalink($this->aPageRebuild['url']),
  369. 'action_page_export' => $this->sActionPageExport,
  370. 'action_page_edit' => $this->sActionPageEdit,
  371. 'action_block_create' => $this->sActionBlockCreate,
  372. );
  373. }
  374. }
  375. /** @} */