1
0

BxBaseStudioWidget.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 = array(
  48. 'site' => array(
  49. 'name' => 'site',
  50. 'icon' => 'tmi-site.svg',
  51. 'link' => '{url_root}',
  52. 'title' => '_adm_tmi_cpt_site'
  53. ),
  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->setContent(BX_DOL_STUDIO_MT_LEFT, $this->getPageBreadcrumb());
  78. $oTopMenu->setSelected(BX_DOL_STUDIO_MT_CENTER, $this->aPage['name']);
  79. $oTopMenu->setContent(BX_DOL_STUDIO_MT_RIGHT, array(
  80. 'template' => 'menu_top_toolbar.html',
  81. 'menu_items' => $aItemsRight
  82. ));
  83. return '';
  84. }
  85. public function getPageCode($sPage = '', $bWrap = true)
  86. {
  87. $sResult = parent::getPageCode($sPage, $bWrap);
  88. if($sResult === false)
  89. return false;
  90. if(!empty($this->aPage['wid_type']) && !BxDolStudioRolesUtils::getInstance()->isActionAllowed('use ' . $this->aPage['wid_type'])) {
  91. $this->setError('_Access denied');
  92. return false;
  93. }
  94. if(empty($sPage))
  95. $sPage = $this->sPage;
  96. $sMethod = 'get' . bx_gen_method_name($sPage);
  97. if(method_exists($this, $sMethod)) {
  98. $mixedContent = $this->$sMethod();
  99. if(!$bWrap || in_array($sPage, $this->aPageCodeNoWrap))
  100. $sResult .= $mixedContent;
  101. else if(is_string($mixedContent))
  102. $sResult .= $this->getBlockCode(array(
  103. 'content' => $mixedContent
  104. ));
  105. else if(is_array($mixedContent))
  106. foreach($mixedContent as $sBlock)
  107. $sResult .= $this->getBlockCode(array(
  108. 'content' => $sBlock
  109. ));
  110. else if(is_a($mixedContent, 'BxDolPage'))
  111. $sResult .= $mixedContent->getCode();
  112. }
  113. return $sResult . BxTemplStudioLauncher::getInstance()->getPageJsCode(array(
  114. 'bInit' => false
  115. ));
  116. }
  117. /**
  118. * Block related methods
  119. */
  120. public function getBlocksLine($aBlocks)
  121. {
  122. $aTmplVarsBlocks = array();
  123. foreach ($aBlocks as $aBlock) {
  124. $aTmplVarsBlocks[] = array(
  125. 'content' => $this->getBlockCode($aBlock)
  126. );
  127. }
  128. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_blocks_line.html', array(
  129. 'count' => count($aTmplVarsBlocks),
  130. 'bx_repeat:blocks' => $aTmplVarsBlocks
  131. ));
  132. }
  133. public function getBlockCode($aBlock)
  134. {
  135. $sContent = '';
  136. if(!empty($aBlock['content']))
  137. $sContent = $aBlock['content'];
  138. else if(!empty($aBlock['items']))
  139. $sContent = $aBlock['items'];
  140. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_block.html', array(
  141. 'caption' => $this->getBlockCaption($aBlock),
  142. 'panel_top' => $this->getBlockPanelTop($aBlock),
  143. 'items' => $sContent,
  144. 'panel_bottom' => $this->getBlockPanelBottom($aBlock)
  145. ));
  146. }
  147. public function getBlockCaption($aBlock)
  148. {
  149. if(empty($aBlock) || !is_array($aBlock) || (empty($aBlock['caption']) && empty($aBlock['actions'])))
  150. return '';
  151. $aTmplActions = array();
  152. if(!empty($aBlock['actions']) && is_array($aBlock['actions']))
  153. foreach($aBlock['actions'] as $aAction) {
  154. $sCaption = is_array($aAction['caption']) ? call_user_func_array('_t', $aAction['caption']) : _t($aAction['caption']);
  155. $bOnClick = !empty($aAction['onclick']);
  156. $aOnClick = $bOnClick ? array('onclick' => $aAction['onclick']) : array();
  157. $aTmplActions[] = array(
  158. 'name' => $aAction['name'],
  159. 'url' => $aAction['url'],
  160. 'title' => $sCaption,
  161. 'bx_if:show_onclick' => array(
  162. 'condition' => $bOnClick,
  163. 'content' => $aOnClick
  164. ),
  165. 'caption' => $sCaption
  166. );
  167. }
  168. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_caption.html', array(
  169. 'caption' => is_array($aBlock['caption']) ? call_user_func_array('_t', $aBlock['caption']) : _t($aBlock['caption']),
  170. 'bx_if:show_actions' => array(
  171. 'condition' => !empty($aTmplActions),
  172. 'content' => array(
  173. 'bx_repeat:actions' => $aTmplActions
  174. )
  175. ),
  176. ));
  177. }
  178. public function getBlockPanelTop($aBlock)
  179. {
  180. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['panel_top']))
  181. return '';
  182. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_panel_top.html', array(
  183. 'content' => $aBlock['panel_top']
  184. ));
  185. }
  186. public function getBlockPanelBottom($aBlock)
  187. {
  188. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['panel_bottom']))
  189. return '';
  190. return BxDolStudioTemplate::getInstance()->parseHtmlByName('block_panel_bottom.html', array(
  191. 'content' => $aBlock['panel_bottom']
  192. ));
  193. }
  194. /**
  195. * Internal methods.
  196. */
  197. protected function getPageCaptionActions()
  198. {
  199. $sActions = $this->getPageActions();
  200. if(empty($sActions))
  201. return "";
  202. return BxDolStudioTemplate::getInstance()->parseHtmlByName('page_caption_actions.html', array(
  203. 'content' => $sActions
  204. ));
  205. }
  206. protected function getPageActions($iWidgetId = 0)
  207. {
  208. if(empty($this->aActions))
  209. return "";
  210. $aMarkers = array(
  211. 'widget_id' => $iWidgetId,
  212. );
  213. if(!empty($iWidgetId)) {
  214. $aWidget = BxDolStudioWidgetsQuery::getInstance()->getWidgets(array('type' => 'by_id', 'value' => $iWidgetId));
  215. if(!empty($aWidget) && is_array($aWidget))
  216. $aMarkers['widget_type'] = $aWidget['type'];
  217. }
  218. $aForm = array(
  219. 'form_attrs' => array(
  220. 'id' => 'adm-page-actions',
  221. 'name' => 'adm-page-actions',
  222. 'action' => '',
  223. 'method' => 'post',
  224. ),
  225. 'params' => array(),
  226. 'inputs' => array()
  227. );
  228. foreach($this->aActions as $aAction) {
  229. if($aAction['name'] == 'rearrange' && empty($iWidgetId))
  230. continue;
  231. $aInput = array(
  232. 'type' => $aAction['type'],
  233. 'name' => $aAction['name'],
  234. 'caption' => _t($aAction['caption'])
  235. );
  236. switch($aAction['type']) {
  237. case 'switcher':
  238. $aInput['checked'] = $aAction['checked'];
  239. $aInput['attrs']['onchange'] = bx_replace_markers($aAction['onchange'], $aMarkers);
  240. break;
  241. case 'select':
  242. $aInput['value'] = bx_replace_markers($aAction['value'], $aMarkers);
  243. $aInput['values'] = $aAction['values'];
  244. $aInput['attrs']['onchange'] = bx_replace_markers($aAction['onchange'], $aMarkers);
  245. break;
  246. }
  247. $aForm['inputs'][$aInput['name']] = $aInput;
  248. }
  249. $oForm = new BxTemplStudioFormView($aForm);
  250. return $oForm->getCode();
  251. }
  252. protected function getPageMenuObject($aMenu = array(), $aMarkers = array())
  253. {
  254. $oMenu = parent::getPageMenuObject($aMenu, $aMarkers);
  255. $oMenu->setInlineIcons(false);
  256. return $oMenu;
  257. }
  258. }
  259. /** @} */