BxBaseStudioWidget.php 12 KB

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