BxBaseStudioWidget.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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(array(
  144. 'content' => $mixedContent
  145. ));
  146. else if(is_array($mixedContent))
  147. foreach($mixedContent as $sBlock)
  148. $sResult .= $this->getBlockCode(array(
  149. 'content' => $sBlock
  150. ));
  151. else if(is_a($mixedContent, 'BxDolPage'))
  152. $sResult .= $mixedContent->getCode();
  153. }
  154. return $sResult . BxTemplStudioLauncher::getInstance()->getPageJsCode(array(
  155. 'bInit' => false
  156. ));
  157. }
  158. /**
  159. * Block related methods
  160. */
  161. public function getBlocksLine($aBlocks)
  162. {
  163. $aTmplVarsBlocks = array();
  164. foreach ($aBlocks as $aBlock) {
  165. $aTmplVarsBlocks[] = array(
  166. 'content' => $this->getBlockCode($aBlock)
  167. );
  168. }
  169. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_blocks_line.html', array(
  170. 'count' => count($aTmplVarsBlocks),
  171. 'bx_repeat:blocks' => $aTmplVarsBlocks
  172. ));
  173. }
  174. public function getBlockCode($aBlock)
  175. {
  176. $aTypeI2S = [
  177. BX_DB_CONTENT_ONLY => 'cnt',
  178. BX_DB_DEF => 'cnt-ttl-bg',
  179. BX_DB_NO_CAPTION => 'cnt-bg'
  180. ];
  181. $iType = isset($aBlock['type'], $aTypeI2S[$aBlock['type']]) ? $aBlock['type'] : BX_DB_NO_CAPTION;
  182. $sContent = '';
  183. if(!empty($aBlock['content']))
  184. $sContent = $aBlock['content'];
  185. else if(!empty($aBlock['items']))
  186. $sContent = $aBlock['items'];
  187. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_block.html', array(
  188. 'type' => $aTypeI2S[$iType],
  189. 'caption' => !in_array($iType, [BX_DB_CONTENT_ONLY, BX_DB_NO_CAPTION]) ? $this->getBlockCaption($aBlock) : '',
  190. 'panel_top' => $this->getBlockPanelTop($aBlock),
  191. 'items' => $sContent,
  192. 'panel_bottom' => $this->getBlockPanelBottom($aBlock)
  193. ));
  194. }
  195. public function getBlockCaption($aBlock)
  196. {
  197. if(empty($aBlock) || !is_array($aBlock) || (empty($aBlock['caption']) && empty($aBlock['actions'])))
  198. return '';
  199. $aTmplActions = array();
  200. if(!empty($aBlock['actions']) && is_array($aBlock['actions']))
  201. foreach($aBlock['actions'] as $aAction) {
  202. $sCaption = is_array($aAction['caption']) ? call_user_func_array('_t', $aAction['caption']) : _t($aAction['caption']);
  203. $bOnClick = !empty($aAction['onclick']);
  204. $aOnClick = $bOnClick ? array('onclick' => $aAction['onclick']) : array();
  205. $aTmplActions[] = array(
  206. 'name' => $aAction['name'],
  207. 'url' => $aAction['url'],
  208. 'title' => $sCaption,
  209. 'bx_if:show_onclick' => array(
  210. 'condition' => $bOnClick,
  211. 'content' => $aOnClick
  212. ),
  213. 'caption' => $sCaption
  214. );
  215. }
  216. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_caption.html', array(
  217. 'caption' => is_array($aBlock['caption']) ? call_user_func_array('_t', $aBlock['caption']) : _t($aBlock['caption']),
  218. 'bx_if:show_actions' => array(
  219. 'condition' => !empty($aTmplActions),
  220. 'content' => array(
  221. 'bx_repeat:actions' => $aTmplActions
  222. )
  223. ),
  224. ));
  225. }
  226. public function getBlockPanelTop($aBlock)
  227. {
  228. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['panel_top']))
  229. return '';
  230. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_panel_top.html', array(
  231. 'content' => $aBlock['panel_top']
  232. ));
  233. }
  234. public function getBlockPanelBottom($aBlock)
  235. {
  236. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['panel_bottom']))
  237. return '';
  238. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_panel_bottom.html', array(
  239. 'content' => $aBlock['panel_bottom']
  240. ));
  241. }
  242. /**
  243. * Internal methods.
  244. */
  245. protected function getPageCaptionActions()
  246. {
  247. $sActions = $this->getPageActions();
  248. if(empty($sActions))
  249. return "";
  250. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_caption_actions.html', array(
  251. 'content' => $sActions
  252. ));
  253. }
  254. protected function getPageActions($iWidgetId = 0)
  255. {
  256. if(empty($this->aActions))
  257. return "";
  258. $aMarkers = array(
  259. 'widget_id' => $iWidgetId,
  260. );
  261. if(!empty($iWidgetId)) {
  262. $aWidget = BxDolStudioWidgetsQuery::getInstance()->getWidgets(array('type' => 'by_id', 'value' => $iWidgetId));
  263. if(!empty($aWidget) && is_array($aWidget))
  264. $aMarkers['widget_type'] = $aWidget['type'];
  265. }
  266. $aForm = array(
  267. 'form_attrs' => array(
  268. 'id' => 'adm-page-actions',
  269. 'name' => 'adm-page-actions',
  270. 'action' => '',
  271. 'method' => 'post',
  272. ),
  273. 'params' => array(),
  274. 'inputs' => array()
  275. );
  276. foreach($this->aActions as $aAction) {
  277. if($aAction['name'] == 'rearrange' && (empty($iWidgetId) || getParam('sys_std_show_launcher_left') != 'on'))
  278. continue;
  279. $aInput = array(
  280. 'type' => $aAction['type'],
  281. 'name' => $aAction['name'],
  282. 'caption' => _t($aAction['caption'])
  283. );
  284. switch($aAction['type']) {
  285. case 'switcher':
  286. $aInput['checked'] = $aAction['checked'];
  287. $aInput['attrs']['onchange'] = bx_replace_markers($aAction['onchange'], $aMarkers);
  288. break;
  289. case 'select':
  290. $aInput['value'] = bx_replace_markers($aAction['value'], $aMarkers);
  291. $aInput['values'] = $aAction['values'];
  292. $aInput['attrs']['onchange'] = bx_replace_markers($aAction['onchange'], $aMarkers);
  293. break;
  294. }
  295. $aForm['inputs'][$aInput['name']] = $aInput;
  296. }
  297. $oForm = new BxTemplStudioFormView($aForm);
  298. return $oForm->getCode();
  299. }
  300. protected function getPageMenuObject($aMenu = array(), $aMarkers = array())
  301. {
  302. $oMenu = parent::getPageMenuObject($aMenu, $aMarkers);
  303. $oMenu->setInlineIcons($this->bPageMenuIconsInline);
  304. return $oMenu;
  305. }
  306. }
  307. /** @} */