BxBaseStudioWidget.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. class BxBaseStudioWidget extends BxDolStudioWidget
  11. {
  12. protected $bPageMenuTitle;
  13. protected $bPageMenuIconsInline;
  14. protected $aPageCodeNoWrap;
  15. public function __construct($mixedPageName)
  16. {
  17. parent::__construct($mixedPageName);
  18. $this->bPageMenuTitle = true;
  19. $this->bPageMenuIconsInline = true;
  20. $this->aPageCodeNoWrap = [];
  21. }
  22. public function getPageCss()
  23. {
  24. return array_merge(parent::getPageCss(), array(
  25. 'launcher.css'
  26. ));
  27. }
  28. public function getPageJs()
  29. {
  30. return array_merge(parent::getPageJs(), array(
  31. 'launcher.js'
  32. ));
  33. }
  34. public function getPageCaption()
  35. {
  36. if(empty($this->aPage) || !is_array($this->aPage))
  37. return '';
  38. $sResult = parent::getPageCaption();
  39. $this->updateHistory();
  40. $oTemplate = BxDolStudioTemplate::getInstance();
  41. $oFunctions = BxTemplStudioFunctions::getInstance();
  42. $sActions = '';
  43. $bActions = false;
  44. if(false && ($sActions = $this->getPageCaptionActions()) && ($bActions = strlen($sActions)) > 0) //--- Hidden for now.
  45. $sActions = $oFunctions->transBox('bx-std-pcap-menu-popup-actions', $sActions, true);
  46. $sHelp = '';
  47. $bHelp = false;
  48. if(false && ($sHelp = $this->getPageCaptionHelp()) && ($bHelp = strlen($sHelp)) > 0) //--- Hidden for now.
  49. $sHelp = $oFunctions->transBox('bx-std-pcap-menu-popup-help', $sHelp, true);
  50. if($bActions || $bHelp)
  51. $oTemplate->addInjection('injection_header', 'text', $sActions . $sHelp);
  52. //--- Menu Right ---//
  53. $aItemsRight = [];
  54. if($bHelp)
  55. $aItemsRight['help'] = [
  56. 'name' => 'help',
  57. 'icon' => 'tmi-help.svg',
  58. 'onclick' => BX_DOL_STUDIO_PAGE_JS_OBJECT . ".togglePopup('help', this)",
  59. 'title' => '_adm_txt_show_help'
  60. ];
  61. if($this->_bShowHeaderRightAssistant)
  62. $aItemsRight['assistant'] = [
  63. 'name' => 'assistant',
  64. 'icon' => 'tmi-assistant.svg',
  65. 'link' => 'javascript:void(0);',
  66. 'onclick' => BX_DOL_STUDIO_PAGE_JS_OBJECT . ".togglePopup('assistant', this)",
  67. 'title' => '_adm_tmi_cpt_assistant'
  68. ];
  69. if($bActions)
  70. $aItemsRight['actions'] = [
  71. 'name' => 'actions',
  72. 'icon' => 'tmi-actions.svg',
  73. 'onclick' => BX_DOL_STUDIO_PAGE_JS_OBJECT . ".togglePopup('actions', this)",
  74. 'title' => '_adm_txt_show_actions'
  75. ];
  76. if($this->_bShowHeaderRightSite)
  77. $aItemsRight['site'] = [
  78. 'name' => 'site',
  79. 'icon' => 'tmi-site.svg',
  80. 'link' => '{url_root}',
  81. 'title' => '_adm_tmi_cpt_site'
  82. ];
  83. $aItemsRight['account'] = [
  84. 'name' => 'account',
  85. 'icon' => 'tmi-account.svg',
  86. 'link' => 'javascript:void(0);',
  87. 'onclick' => 'bx_menu_popup_inline(\'#bx-std-pcap-menu-popup-account\', this);',
  88. 'title' => '_adm_tmi_cpt_account'
  89. ];
  90. $oTopMenu = BxTemplStudioMenuTop::getInstance();
  91. $oTopMenu->setPageName($this->aPage['name']);
  92. $oTopMenu->setContent(BX_DOL_STUDIO_MT_LEFT, $this->getPageBreadcrumb());
  93. $oTopMenu->setSelected(BX_DOL_STUDIO_MT_CENTER, $this->aPage['name']);
  94. $oTopMenu->setContent(BX_DOL_STUDIO_MT_RIGHT, array(
  95. 'template' => 'menu_top_toolbar.html',
  96. 'menu_items' => $aItemsRight
  97. ));
  98. return $sResult;
  99. }
  100. public function getPageMenu($aMenu = [], $aMarkers = [])
  101. {
  102. $sMenu = parent::getPageMenu($aMenu, $aMarkers);
  103. if(!$sMenu || !$this->bPageMenuTitle)
  104. return $sMenu;
  105. $oTemplate = BxDolStudioTemplate::getInstance();
  106. $bActions = false;
  107. if(($sActions = $this->getPageCaptionActions()) && ($bActions = strlen($sActions)) > 0)
  108. $oTemplate->addInjection('injection_header', 'text', BxTemplStudioFunctions::getInstance()->transBox('bx-std-pmenu-popup-actions', $sActions, true));
  109. return $oTemplate->parseHtmlByName('page_menu.html', [
  110. 'title' => _t($this->aPage['caption']),
  111. 'bx_if:show_actions' => [
  112. 'condition' => $bActions,
  113. 'content' => [
  114. 'onclick' => BX_DOL_STUDIO_PAGE_JS_OBJECT . ".togglePopup('actions', this)",
  115. ]
  116. ],
  117. 'menu' => $sMenu
  118. ]);
  119. }
  120. public function getPageCode($sPage = '', $bWrap = true)
  121. {
  122. $sResult = parent::getPageCode($sPage, $bWrap);
  123. if($sResult === false)
  124. return false;
  125. if(!empty($this->aPage['wid_type']) && !BxDolStudioRolesUtils::getInstance()->isActionAllowed('use ' . $this->aPage['wid_type'])) {
  126. $this->setError('_Access denied');
  127. return false;
  128. }
  129. if(empty($sPage))
  130. $sPage = $this->sPage;
  131. $sMethod = 'get' . bx_gen_method_name($sPage);
  132. if(method_exists($this, $sMethod)) {
  133. $mixedContent = $this->$sMethod();
  134. if(!$bWrap || in_array($sPage, $this->aPageCodeNoWrap))
  135. $sResult .= $mixedContent;
  136. else if(is_string($mixedContent))
  137. $sResult .= $this->getBlockCode(array(
  138. 'content' => $mixedContent
  139. ));
  140. else if(is_array($mixedContent))
  141. foreach($mixedContent as $sBlock)
  142. $sResult .= $this->getBlockCode(array(
  143. 'content' => $sBlock
  144. ));
  145. else if(is_a($mixedContent, 'BxDolPage'))
  146. $sResult .= $mixedContent->getCode();
  147. }
  148. return $sResult . BxTemplStudioLauncher::getInstance()->getPageJsCode(array(
  149. 'bInit' => false
  150. ));
  151. }
  152. /**
  153. * Block related methods
  154. */
  155. public function getBlocksLine($aBlocks)
  156. {
  157. $aTmplVarsBlocks = array();
  158. foreach ($aBlocks as $aBlock) {
  159. $aTmplVarsBlocks[] = array(
  160. 'content' => $this->getBlockCode($aBlock)
  161. );
  162. }
  163. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_blocks_line.html', array(
  164. 'count' => count($aTmplVarsBlocks),
  165. 'bx_repeat:blocks' => $aTmplVarsBlocks
  166. ));
  167. }
  168. public function getBlockCode($aBlock)
  169. {
  170. $aTypeI2S = [
  171. BX_DB_CONTENT_ONLY => 'cnt',
  172. BX_DB_DEF => 'cnt-ttl-bg bg-white rounded-2xl',
  173. BX_DB_NO_CAPTION => 'cnt-bg bg-white rounded-2xl'
  174. ];
  175. $iType = isset($aBlock['type'], $aTypeI2S[$aBlock['type']]) ? $aBlock['type'] : BX_DB_NO_CAPTION;
  176. $sContent = '';
  177. if(!empty($aBlock['content']))
  178. $sContent = $aBlock['content'];
  179. else if(!empty($aBlock['items']))
  180. $sContent = $aBlock['items'];
  181. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_block.html', array(
  182. 'type' => $aTypeI2S[$iType],
  183. 'caption' => !in_array($iType, [BX_DB_CONTENT_ONLY, BX_DB_NO_CAPTION]) ? $this->getBlockCaption($aBlock) : '',
  184. 'panel_top' => $this->getBlockPanelTop($aBlock),
  185. 'items' => $sContent,
  186. 'panel_bottom' => $this->getBlockPanelBottom($aBlock)
  187. ));
  188. }
  189. public function getBlockCaption($aBlock)
  190. {
  191. if(empty($aBlock) || !is_array($aBlock) || (empty($aBlock['caption']) && empty($aBlock['actions'])))
  192. return '';
  193. $aTmplActions = array();
  194. if(!empty($aBlock['actions']) && is_array($aBlock['actions']))
  195. foreach($aBlock['actions'] as $aAction) {
  196. $sCaption = is_array($aAction['caption']) ? call_user_func_array('_t', $aAction['caption']) : _t($aAction['caption']);
  197. $bOnClick = !empty($aAction['onclick']);
  198. $aOnClick = $bOnClick ? array('onclick' => $aAction['onclick']) : array();
  199. $aTmplActions[] = array(
  200. 'name' => $aAction['name'],
  201. 'url' => $aAction['url'],
  202. 'title' => $sCaption,
  203. 'bx_if:show_onclick' => array(
  204. 'condition' => $bOnClick,
  205. 'content' => $aOnClick
  206. ),
  207. 'caption' => $sCaption
  208. );
  209. }
  210. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_caption.html', array(
  211. 'caption' => is_array($aBlock['caption']) ? call_user_func_array('_t', $aBlock['caption']) : _t($aBlock['caption']),
  212. 'bx_if:show_actions' => array(
  213. 'condition' => !empty($aTmplActions),
  214. 'content' => array(
  215. 'bx_repeat:actions' => $aTmplActions
  216. )
  217. ),
  218. ));
  219. }
  220. public function getBlockPanelTop($aBlock)
  221. {
  222. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['panel_top']))
  223. return '';
  224. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_panel_top.html', array(
  225. 'content' => $aBlock['panel_top']
  226. ));
  227. }
  228. public function getBlockPanelBottom($aBlock)
  229. {
  230. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['panel_bottom']))
  231. return '';
  232. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_panel_bottom.html', array(
  233. 'content' => $aBlock['panel_bottom']
  234. ));
  235. }
  236. /**
  237. * Internal methods.
  238. */
  239. protected function getPageCaptionActions()
  240. {
  241. $sActions = $this->getPageActions();
  242. if(empty($sActions))
  243. return "";
  244. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_caption_actions.html', array(
  245. 'content' => $sActions
  246. ));
  247. }
  248. protected function getPageActions($iWidgetId = 0)
  249. {
  250. if(empty($this->aActions))
  251. return "";
  252. $aMarkers = array(
  253. 'widget_id' => $iWidgetId,
  254. );
  255. if(!empty($iWidgetId)) {
  256. $aWidget = BxDolStudioWidgetsQuery::getInstance()->getWidgets(array('type' => 'by_id', 'value' => $iWidgetId));
  257. if(!empty($aWidget) && is_array($aWidget))
  258. $aMarkers['widget_type'] = $aWidget['type'];
  259. }
  260. $aForm = array(
  261. 'form_attrs' => array(
  262. 'id' => 'adm-page-actions',
  263. 'name' => 'adm-page-actions',
  264. 'action' => '',
  265. 'method' => 'post',
  266. ),
  267. 'params' => array(),
  268. 'inputs' => array()
  269. );
  270. foreach($this->aActions as $aAction) {
  271. if($aAction['name'] == 'rearrange' && (empty($iWidgetId) || getParam('sys_std_show_launcher_left') != 'on'))
  272. continue;
  273. $aInput = array(
  274. 'type' => $aAction['type'],
  275. 'name' => $aAction['name'],
  276. 'caption' => _t($aAction['caption'])
  277. );
  278. switch($aAction['type']) {
  279. case 'switcher':
  280. $aInput['checked'] = $aAction['checked'];
  281. $aInput['attrs']['onchange'] = bx_replace_markers($aAction['onchange'], $aMarkers);
  282. break;
  283. case 'select':
  284. $aInput['value'] = bx_replace_markers($aAction['value'], $aMarkers);
  285. $aInput['values'] = $aAction['values'];
  286. $aInput['attrs']['onchange'] = bx_replace_markers($aAction['onchange'], $aMarkers);
  287. break;
  288. }
  289. $aForm['inputs'][$aInput['name']] = $aInput;
  290. }
  291. $oForm = new BxTemplStudioFormView($aForm);
  292. return $oForm->getCode();
  293. }
  294. protected function getPageMenuObject($aMenu = array(), $aMarkers = array())
  295. {
  296. $oMenu = parent::getPageMenuObject($aMenu, $aMarkers);
  297. $oMenu->setInlineIcons($this->bPageMenuIconsInline);
  298. return $oMenu;
  299. }
  300. }
  301. /** @} */