BxBaseStudioWidget.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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($this->_bShowHeaderRightSite)
  49. $aItemsRight['site'] = [
  50. 'name' => 'site',
  51. 'icon' => 'tmi-site.svg',
  52. 'link' => '{url_root}',
  53. 'title' => '_adm_tmi_cpt_site'
  54. ];
  55. if($bHelp)
  56. $aItemsRight['help'] = array(
  57. 'name' => 'help',
  58. 'icon' => 'tmi-help.svg',
  59. 'onclick' => BX_DOL_STUDIO_PAGE_JS_OBJECT . ".togglePopup('help', this)",
  60. 'title' => '_adm_txt_show_help'
  61. );
  62. if($bActions)
  63. $aItemsRight['actions'] = array(
  64. 'name' => 'actions',
  65. 'icon' => 'tmi-actions.svg',
  66. 'onclick' => BX_DOL_STUDIO_PAGE_JS_OBJECT . ".togglePopup('actions', this)",
  67. 'title' => '_adm_txt_show_actions'
  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. $sContent = '';
  137. if(!empty($aBlock['content']))
  138. $sContent = $aBlock['content'];
  139. else if(!empty($aBlock['items']))
  140. $sContent = $aBlock['items'];
  141. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_block.html', array(
  142. 'caption' => $this->getBlockCaption($aBlock),
  143. 'panel_top' => $this->getBlockPanelTop($aBlock),
  144. 'items' => $sContent,
  145. 'panel_bottom' => $this->getBlockPanelBottom($aBlock)
  146. ));
  147. }
  148. public function getBlockCaption($aBlock)
  149. {
  150. if(empty($aBlock) || !is_array($aBlock) || (empty($aBlock['caption']) && empty($aBlock['actions'])))
  151. return '';
  152. $aTmplActions = array();
  153. if(!empty($aBlock['actions']) && is_array($aBlock['actions']))
  154. foreach($aBlock['actions'] as $aAction) {
  155. $sCaption = is_array($aAction['caption']) ? call_user_func_array('_t', $aAction['caption']) : _t($aAction['caption']);
  156. $bOnClick = !empty($aAction['onclick']);
  157. $aOnClick = $bOnClick ? array('onclick' => $aAction['onclick']) : array();
  158. $aTmplActions[] = array(
  159. 'name' => $aAction['name'],
  160. 'url' => $aAction['url'],
  161. 'title' => $sCaption,
  162. 'bx_if:show_onclick' => array(
  163. 'condition' => $bOnClick,
  164. 'content' => $aOnClick
  165. ),
  166. 'caption' => $sCaption
  167. );
  168. }
  169. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_caption.html', array(
  170. 'caption' => is_array($aBlock['caption']) ? call_user_func_array('_t', $aBlock['caption']) : _t($aBlock['caption']),
  171. 'bx_if:show_actions' => array(
  172. 'condition' => !empty($aTmplActions),
  173. 'content' => array(
  174. 'bx_repeat:actions' => $aTmplActions
  175. )
  176. ),
  177. ));
  178. }
  179. public function getBlockPanelTop($aBlock)
  180. {
  181. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['panel_top']))
  182. return '';
  183. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_panel_top.html', array(
  184. 'content' => $aBlock['panel_top']
  185. ));
  186. }
  187. public function getBlockPanelBottom($aBlock)
  188. {
  189. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['panel_bottom']))
  190. return '';
  191. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_panel_bottom.html', array(
  192. 'content' => $aBlock['panel_bottom']
  193. ));
  194. }
  195. /**
  196. * Internal methods.
  197. */
  198. protected function getPageCaptionActions()
  199. {
  200. $sActions = $this->getPageActions();
  201. if(empty($sActions))
  202. return "";
  203. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_caption_actions.html', array(
  204. 'content' => $sActions
  205. ));
  206. }
  207. protected function getPageActions($iWidgetId = 0)
  208. {
  209. if(empty($this->aActions))
  210. return "";
  211. $aMarkers = array(
  212. 'widget_id' => $iWidgetId,
  213. );
  214. if(!empty($iWidgetId)) {
  215. $aWidget = BxDolStudioWidgetsQuery::getInstance()->getWidgets(array('type' => 'by_id', 'value' => $iWidgetId));
  216. if(!empty($aWidget) && is_array($aWidget))
  217. $aMarkers['widget_type'] = $aWidget['type'];
  218. }
  219. $aForm = array(
  220. 'form_attrs' => array(
  221. 'id' => 'adm-page-actions',
  222. 'name' => 'adm-page-actions',
  223. 'action' => '',
  224. 'method' => 'post',
  225. ),
  226. 'params' => array(),
  227. 'inputs' => array()
  228. );
  229. foreach($this->aActions as $aAction) {
  230. if($aAction['name'] == 'rearrange' && (empty($iWidgetId) || getParam('sys_std_show_launcher_left') != 'on'))
  231. continue;
  232. $aInput = array(
  233. 'type' => $aAction['type'],
  234. 'name' => $aAction['name'],
  235. 'caption' => _t($aAction['caption'])
  236. );
  237. switch($aAction['type']) {
  238. case 'switcher':
  239. $aInput['checked'] = $aAction['checked'];
  240. $aInput['attrs']['onchange'] = bx_replace_markers($aAction['onchange'], $aMarkers);
  241. break;
  242. case 'select':
  243. $aInput['value'] = bx_replace_markers($aAction['value'], $aMarkers);
  244. $aInput['values'] = $aAction['values'];
  245. $aInput['attrs']['onchange'] = bx_replace_markers($aAction['onchange'], $aMarkers);
  246. break;
  247. }
  248. $aForm['inputs'][$aInput['name']] = $aInput;
  249. }
  250. $oForm = new BxTemplStudioFormView($aForm);
  251. return $oForm->getCode();
  252. }
  253. protected function getPageMenuObject($aMenu = array(), $aMarkers = array())
  254. {
  255. $oMenu = parent::getPageMenuObject($aMenu, $aMarkers);
  256. $oMenu->setInlineIcons(false);
  257. return $oMenu;
  258. }
  259. }
  260. /** @} */