BxBaseStudioWidget.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 = !$this->_bShowHeaderBreadcrumb;
  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. if(!$this->bPageMenuTitle && $aMenu === false)
  103. return '';
  104. $sActions = $this->bPageMenuTitle ? $this->getPageCaptionActions() : '';
  105. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_menu.html', [
  106. 'bx_if:show_title' => [
  107. 'condition' => $this->bPageMenuTitle,
  108. 'content' => [
  109. 'title' => _t($this->aPage['caption']),
  110. 'bx_if:show_actions' => [
  111. 'condition' => (bool)$sActions,
  112. 'content' => [
  113. 'onclick' => $sActions,
  114. ]
  115. ],
  116. ]
  117. ],
  118. 'menu' => parent::getPageMenu($aMenu, $aMarkers)
  119. ]);
  120. }
  121. public function getPageCode($sPage = '', $bWrap = true)
  122. {
  123. $sResult = parent::getPageCode($sPage, $bWrap);
  124. if($sResult === false)
  125. return false;
  126. if(!empty($this->aPage['wid_type']) && !BxDolStudioRolesUtils::getInstance()->isActionAllowed('use ' . $this->aPage['wid_type'])) {
  127. $this->setError('_Access denied');
  128. return false;
  129. }
  130. if(empty($sPage))
  131. $sPage = $this->sPage;
  132. $sMethod = 'get' . bx_gen_method_name($sPage);
  133. if(method_exists($this, $sMethod)) {
  134. $mixedContent = $this->$sMethod();
  135. if(!$bWrap || in_array($sPage, $this->aPageCodeNoWrap))
  136. $sResult .= $mixedContent;
  137. else if(is_string($mixedContent))
  138. $sResult .= $this->getBlockCode([
  139. 'content' => $mixedContent
  140. ]);
  141. else if(is_array($mixedContent)) {
  142. if(isset($mixedContent['content']))
  143. $sResult .= $this->getBlockCode($mixedContent);
  144. else
  145. foreach($mixedContent as $mixedBlock)
  146. $sResult .= $this->getBlockCode(is_array($mixedBlock) ? $mixedBlock : [
  147. 'content' => $mixedBlock
  148. ]);
  149. }
  150. else if(is_a($mixedContent, 'BxDolPage'))
  151. $sResult .= $mixedContent->getCode();
  152. }
  153. return $sResult . BxTemplStudioLauncher::getInstance()->getPageJsCode(array(
  154. 'bInit' => false
  155. ));
  156. }
  157. /**
  158. * Block related methods
  159. */
  160. public function getBlocksLine($aBlocks)
  161. {
  162. $aTmplVarsBlocks = array();
  163. foreach ($aBlocks as $aBlock) {
  164. $aTmplVarsBlocks[] = array(
  165. 'content' => $this->getBlockCode($aBlock)
  166. );
  167. }
  168. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_blocks_line.html', array(
  169. 'count' => count($aTmplVarsBlocks),
  170. 'bx_repeat:blocks' => $aTmplVarsBlocks
  171. ));
  172. }
  173. public function getBlockCode($aBlock)
  174. {
  175. $aTypeI2S = [
  176. BX_DB_CONTENT_ONLY => 'cnt',
  177. BX_DB_DEF => 'cnt-ttl-bg',
  178. BX_DB_NO_CAPTION => 'cnt-bg'
  179. ];
  180. $iType = isset($aBlock['type'], $aTypeI2S[$aBlock['type']]) ? $aBlock['type'] : BX_DB_DEF;
  181. $sContent = '';
  182. if(!empty($aBlock['content']))
  183. $sContent = $aBlock['content'];
  184. else if(!empty($aBlock['items']))
  185. $sContent = $aBlock['items'];
  186. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_block.html', array(
  187. 'type' => $aTypeI2S[$iType],
  188. 'caption' => !in_array($iType, [BX_DB_CONTENT_ONLY, BX_DB_NO_CAPTION]) ? $this->getBlockCaption($aBlock) : '',
  189. 'panel_top' => $this->getBlockPanelTop($aBlock),
  190. 'items' => $sContent,
  191. 'panel_bottom' => $this->getBlockPanelBottom($aBlock)
  192. ));
  193. }
  194. public function getBlockCaption($aBlock)
  195. {
  196. if(($sK = 'caption') && empty($aBlock[$sK])) {
  197. if(($sPageCaption = $this->aPage['subcaption'] ?? false))
  198. $aBlock[$sK] = $sPageCaption;
  199. else if(($sPageCaption = $this->aPage['caption'] ?? false)) {
  200. $_sPageCaption = _t($sPageCaption);
  201. if(strcmp($sPageCaption, $_sPageCaption) !== 0)
  202. $aBlock[$sK] = $_sPageCaption;
  203. }
  204. }
  205. if(empty($aBlock) || !is_array($aBlock) || (empty($aBlock['caption']) && empty($aBlock['actions'])))
  206. return '';
  207. $oTemplate = BxDolStudioTemplate::getInstance();
  208. $aTmplActions = array();
  209. if(!empty($aBlock['actions']) && is_array($aBlock['actions']))
  210. foreach($aBlock['actions'] as $aAction) {
  211. $aTmplVarsIcon = $aTmplVarsIconUrl = $aTmplVarsIconHtml = [];
  212. if(($sIcon = $aAction['icon'] ?? false)) {
  213. list($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $oTemplate->getTemplateFunctions()->getIcon($sIcon);
  214. if($sIcon)
  215. $aTmplVarsIcon = [
  216. 'icon' => BxDolIconset::getObjectInstance()->getIcon($sIcon)
  217. ];
  218. else if($sIconUrl)
  219. $aTmplVarsIconUrl = [
  220. 'icon_url' => $sIconUrl,
  221. ];
  222. else if($sIconHtml)
  223. $aTmplVarsIconHtml = [
  224. 'icon_html' => $sIconHtml
  225. ];
  226. }
  227. $sCaption = is_array($aAction['caption']) ? call_user_func_array('_t', $aAction['caption']) : _t($aAction['caption']);
  228. $bTitleOnly = $aAction['title_only'] ?? false;
  229. $bOnClick = !empty($aAction['onclick']);
  230. $aOnClick = $bOnClick ? array('onclick' => $aAction['onclick']) : array();
  231. $aTmplActions[] = [
  232. 'name' => $aAction['name'],
  233. 'url' => $aAction['url'],
  234. 'title' => bx_html_attribute($sCaption),
  235. 'bx_if:show_onclick' => [
  236. 'condition' => $bOnClick,
  237. 'content' => $aOnClick
  238. ],
  239. 'bx_if:show_bc_icon' => [
  240. 'condition' => !empty($aTmplVarsIcon),
  241. 'content' => $aTmplVarsIcon
  242. ],
  243. 'bx_if:show_bc_icon_url' => [
  244. 'condition' => !empty($aTmplVarsIconUrl),
  245. 'content' => $aTmplVarsIconUrl
  246. ],
  247. 'bx_if:show_bc_icon_html' => [
  248. 'condition' => !empty($aTmplVarsIconHtml),
  249. 'content' => $aTmplVarsIconHtml
  250. ],
  251. 'bx_if:show_bc_text' => [
  252. 'condition' => (bool)$sCaption && !$bTitleOnly,
  253. 'content' => [
  254. 'caption' => $sCaption
  255. ]
  256. ],
  257. ];
  258. }
  259. return $oTemplate->parseHtmlByName('block_caption.html', [
  260. 'caption' => is_array($aBlock['caption']) ? call_user_func_array('_t', $aBlock['caption']) : _t($aBlock['caption']),
  261. 'bx_if:show_actions' => [
  262. 'condition' => !empty($aTmplActions),
  263. 'content' => [
  264. 'bx_repeat:actions' => $aTmplActions
  265. ]
  266. ],
  267. ]);
  268. }
  269. public function getBlockPanelTop($aBlock)
  270. {
  271. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['panel_top']))
  272. return '';
  273. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_panel_top.html', array(
  274. 'content' => $aBlock['panel_top']
  275. ));
  276. }
  277. public function getBlockPanelBottom($aBlock)
  278. {
  279. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['panel_bottom']))
  280. return '';
  281. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_panel_bottom.html', array(
  282. 'content' => $aBlock['panel_bottom']
  283. ));
  284. }
  285. /**
  286. * Internal methods.
  287. */
  288. protected function getPageActions($iWidgetId = 0)
  289. {
  290. if(empty($this->aActions))
  291. return "";
  292. $aMarkers = array(
  293. 'widget_id' => $iWidgetId,
  294. );
  295. if(!empty($iWidgetId)) {
  296. $aWidget = BxDolStudioWidgetsQuery::getInstance()->getWidgets(array('type' => 'by_id', 'value' => $iWidgetId));
  297. if(!empty($aWidget) && is_array($aWidget))
  298. $aMarkers['widget_type'] = $aWidget['type'];
  299. }
  300. $aForm = array(
  301. 'form_attrs' => array(
  302. 'id' => 'adm-page-actions',
  303. 'name' => 'adm-page-actions',
  304. 'action' => '',
  305. 'method' => 'post',
  306. ),
  307. 'params' => array(),
  308. 'inputs' => array()
  309. );
  310. foreach($this->aActions as $aAction) {
  311. if($aAction['name'] == 'rearrange' && (empty($iWidgetId) || getParam('sys_std_show_launcher_left') != 'on'))
  312. continue;
  313. $aInput = array(
  314. 'type' => $aAction['type'],
  315. 'name' => $aAction['name'],
  316. 'caption' => _t($aAction['caption'])
  317. );
  318. switch($aAction['type']) {
  319. case 'switcher':
  320. $aInput['checked'] = $aAction['checked'];
  321. $aInput['attrs']['onchange'] = bx_replace_markers($aAction['onchange'], $aMarkers);
  322. break;
  323. case 'select':
  324. $aInput['value'] = bx_replace_markers($aAction['value'], $aMarkers);
  325. $aInput['values'] = $aAction['values'];
  326. $aInput['attrs']['onchange'] = bx_replace_markers($aAction['onchange'], $aMarkers);
  327. break;
  328. }
  329. $aForm['inputs'][$aInput['name']] = $aInput;
  330. }
  331. $oForm = new BxTemplStudioFormView($aForm);
  332. return $oForm->getCode();
  333. }
  334. protected function getPageMenuObject($aMenu = array(), $aMarkers = array())
  335. {
  336. $oMenu = parent::getPageMenuObject($aMenu, $aMarkers);
  337. $oMenu->setInlineIcons($this->bPageMenuIconsInline);
  338. return $oMenu;
  339. }
  340. }
  341. /** @} */