BxBaseStudioWidget.php 11 KB

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