1
0

BxBaseStudioWidget.php 11 KB

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