BxBaseStudioWidget.php 11 KB

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