BxBaseStudioWidget.php 14 KB

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