BxBaseStudioFunctions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 BxBaseStudioFunctions extends BxBaseFunctions implements iBxDolSingleton
  11. {
  12. /**
  13. * Studio draws a block's title icon as an inline Lucide SVG (like its menus), whatever the site's default iconset is.
  14. */
  15. public function designBoxContent ($sTitle, $sContent, $iTemplateNum = BX_DB_DEF, $mixedMenu = false, $mixedButtons = [])
  16. {
  17. if(is_array($sTitle) && !empty($sTitle[2]) && preg_match('/^[a-z0-9-]+$/', $sTitle[2]) && ($oIconset = BxDolIconset::getObjectInstance('sys_lucide')) && ($sSvg = $oIconset->getIconHtml($sTitle[2])) !== false)
  18. $sTitle[2] = $sSvg;
  19. return parent::designBoxContent($sTitle, $sContent, $iTemplateNum, $mixedMenu, $mixedButtons);
  20. }
  21. /**
  22. * A Studio block may hand the caption ready-made markup (a segmented switcher, say) instead of a menu object.
  23. */
  24. public function designBoxMenu ($mixedMenu, $mixedButtons = [])
  25. {
  26. if(is_string($mixedMenu) && strncmp(ltrim($mixedMenu), '<', 1) === 0)
  27. return $mixedMenu;
  28. return parent::designBoxMenu($mixedMenu, $mixedButtons);
  29. }
  30. function __construct($oTemplate = false)
  31. {
  32. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  33. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  34. parent::__construct($oTemplate ? $oTemplate : BxDolStudioTemplate::getInstance());
  35. }
  36. public static function getInstance()
  37. {
  38. if (!isset($GLOBALS['bxDolClasses']['BxBaseStudioFunctions']))
  39. $GLOBALS['bxDolClasses']['BxBaseStudioFunctions'] = new BxTemplStudioFunctions();
  40. return $GLOBALS['bxDolClasses']['BxBaseStudioFunctions'];
  41. }
  42. public function getLogo()
  43. {
  44. return bx_idn_to_utf8(BX_DOL_URL_ROOT, true);
  45. }
  46. public function getLoginForm()
  47. {
  48. $oTemplate = BxDolStudioTemplate::getInstance();
  49. $sUrlRelocate = bx_get('relocate');
  50. if (empty($sUrlRelocate) || basename($sUrlRelocate) == 'index.php')
  51. $sUrlRelocate = '';
  52. $sHtml = $oTemplate->parseHtmlByName('login_form.html', array (
  53. 'role' => BX_DOL_ROLE_ADMIN,
  54. 'csrf_token' => BxDolForm::getCsrfToken(),
  55. 'relocate_url' => bx_html_attribute($sUrlRelocate),
  56. 'action_url' => BX_DOL_URL_ROOT . 'member.php',
  57. 'forgot_password_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=forgot-password')),
  58. ));
  59. $sHtml = $oTemplate->parseHtmlByName('login.html', array (
  60. 'form' => $this->transBox('bx-std-login-form-box', $sHtml, true),
  61. ));
  62. $oTemplate->setPageNameIndex(BX_PAGE_CLEAR);
  63. $oTemplate->setPageParams(array(
  64. 'css_name' => array('forms.css', 'login.css'),
  65. 'js_name' => array('jquery-ui/jquery-ui.min.js', 'jquery.form.min.js', 'jquery.dolPopup.js', 'login.js'),
  66. 'header' => _t('_adm_page_cpt_login'),
  67. ));
  68. $oTemplate->setPageContent ('page_main_code', $sHtml);
  69. $oTemplate->getPageCode();
  70. }
  71. public function getWidget($mixedWidget, $aParams = array())
  72. {
  73. $oTemplate = BxDolStudioTemplate::getInstance();
  74. $aNotices = array();
  75. if(!empty($aParams['notices']) && is_array($aParams['notices']))
  76. $aNotices = $aParams['notices'];
  77. $aMarkers = array(
  78. 'url_root' => BX_DOL_URL_ROOT,
  79. 'url_studio' => BX_DOL_URL_STUDIO,
  80. 'url_studio_icons' => BX_DOL_URL_STUDIO_BASE . 'images/icons/'
  81. );
  82. if(!is_array($mixedWidget))
  83. $mixedWidget = BxDolStudioWidgetsQuery::getInstance()->getWidgets(array('type' => 'by_id', 'value' => (int)$mixedWidget));
  84. if(!empty($mixedWidget['type']) && !BxDolStudioRolesUtils::getInstance()->isActionAllowed('use ' . $mixedWidget['type']))
  85. return '';
  86. $sCaption = _t($mixedWidget['caption']);
  87. $aTmplVarsActionsLeft = $aTmplVarsActionsRight = [];
  88. if(!empty($mixedWidget['cnt_actions'])) {
  89. $aService = unserialize($mixedWidget['cnt_actions']);
  90. $aActions = bx_srv_ii($aService['module'], $aService['method'], array_merge(array($mixedWidget), $aService['params']), $aService['class']);
  91. foreach($aActions as $iIndex => $aAction) {
  92. if(!empty($aAction['check_func'])) {
  93. $sCheckFunc = bx_gen_method_name($aAction['check_func']);
  94. if(method_exists($this, $sCheckFunc) && !$this->$sCheckFunc($mixedWidget))
  95. continue;
  96. }
  97. $sActionIcon = $aAction['icon'];
  98. $bActionIcon = strpos($sActionIcon, '.') === false;
  99. $bActionIconInline = !$bActionIcon;
  100. if($bActionIconInline && ($sActionIcon = $oTemplate->getIconContent($sActionIcon)) === false) {
  101. $sActionIcon = $oTemplate->getIconUrl($sActionIcon);
  102. $bActionIconInline = false;
  103. }
  104. if($bActionIcon)
  105. $sActionIconHtml = '<i class="sys-icon ' . $sActionIcon . '" aria-hidden="true"></i>';
  106. else if($bActionIconInline)
  107. $sActionIconHtml = $sActionIcon;
  108. else
  109. $sActionIconHtml = '<img src="' . $sActionIcon . '" alt="" />';
  110. /*
  111. * A control with a URL is a link, one that only runs a script is a button. Every widget has a control with the
  112. * same caption (Settings), so the accessible name adds the app it belongs to.
  113. */
  114. $sActionCaption = _t($aAction['caption']);
  115. $bActionLink = !empty($aAction['url']);
  116. $aTmplVarsControl = [
  117. 'name' => !empty($aAction['name']) ? $aAction['name'] : $mixedWidget['id'] . '-' . $iIndex,
  118. 'url' => $bActionLink ? bx_replace_markers($aAction['url'], $aMarkers) : '',
  119. 'onclick' => !empty($aAction['click']) ? 'onclick="' . bx_html_attribute($aAction['click']) . '"' : '',
  120. 'caption' => bx_html_attribute($sActionCaption),
  121. 'label' => bx_html_attribute(_t('_adm_txt_widget_action_label', $sActionCaption, $sCaption)),
  122. 'icon' => $sActionIconHtml,
  123. ];
  124. $aTmplVarsAction = [
  125. 'action' => $oTemplate->parseHtmlByName('widget_action.html', [
  126. 'bx_if:show_link' => ['condition' => $bActionLink, 'content' => $aTmplVarsControl],
  127. 'bx_if:show_button' => ['condition' => !$bActionLink, 'content' => $aTmplVarsControl],
  128. ]),
  129. ];
  130. if(in_array($aAction['name'], ['settings']))
  131. $aTmplVarsActionsLeft[] = $aTmplVarsAction;
  132. else
  133. $aTmplVarsActionsRight[] = $aTmplVarsAction;
  134. }
  135. }
  136. $sIcon = BxDolStudioUtils::getWidgetIcon($mixedWidget);
  137. $bIcon = strpos($sIcon, '.') === false && strcmp(substr($sIcon, 0, 10), 'data:image') != 0;
  138. $sNotices = !empty($aNotices[$mixedWidget['id']]) ? $aNotices[$mixedWidget['id']] : '';
  139. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($mixedWidget['module']);
  140. $bEnabled = empty($aModule) || !is_array($aModule) || (int)$aModule['enabled'] == 1;
  141. $sStyles = 'animation-delay: -.' . rand(1 , 75) . 's; animation-duration: .' . rand(15 , 20) . 's';
  142. return $oTemplate->parseHtmlByName('widget.html', array(
  143. 'id' => $mixedWidget['id'],
  144. 'name' => strtolower($sCaption),
  145. 'url' => !empty($mixedWidget['url']) ? bx_replace_markers($mixedWidget['url'], $aMarkers) : 'javascript:void(0)',
  146. 'bx_if:show_click_icon' => array(
  147. 'condition' => !empty($mixedWidget['click']),
  148. 'content' => array(
  149. 'content' => 'javascript:' . $mixedWidget['click'],
  150. )
  151. ),
  152. // right-click (or a long press) on the tile opens the app's context menu, fetched from its page on first use
  153. 'bx_if:show_context_menu' => array(
  154. 'condition' => !empty($mixedWidget['page_name']),
  155. 'content' => array(
  156. 'page' => !empty($mixedWidget['page_name']) ? $mixedWidget['page_name'] : '',
  157. 'id' => (int)$mixedWidget['id'],
  158. )
  159. ),
  160. 'bx_if:show_notice' => array(
  161. 'condition' => !empty($sNotices),
  162. 'content' => array(
  163. 'content' => $sNotices
  164. )
  165. ),
  166. 'bx_if:show_actions_left' => array(
  167. 'condition' => !empty($aTmplVarsActionsLeft),
  168. 'content' => array(
  169. 'bx_repeat:actions' => $aTmplVarsActionsLeft,
  170. )
  171. ),
  172. 'bx_repeat:actions_right' => $aTmplVarsActionsRight,
  173. 'move_left' => bx_html_attribute(_t('_adm_txt_widget_move_left', $sCaption)),
  174. 'move_right' => bx_html_attribute(_t('_adm_txt_widget_move_right', $sCaption)),
  175. 'bx_if:icon' => array (
  176. 'condition' => $bIcon,
  177. 'content' => array('icon' => $sIcon),
  178. ),
  179. 'bx_if:image' => array (
  180. 'condition' => !$bIcon,
  181. 'content' => array('icon_url' => $sIcon),
  182. ),
  183. 'caption' => $sCaption,
  184. 'caption_attr' => bx_html_attribute($sCaption),
  185. 'widget_disabled_class' => !$bEnabled ? 'bx-std-widget-icon-disabled' : '',
  186. 'widget_styles' => $sStyles
  187. ));
  188. }
  189. /*
  190. * Note. For multi upload form field add [] at the end of the field name in $mParams parameter.
  191. */
  192. public function getDefaultGhostTemplate($mParams, $sTemplateName = 'form_ghost_template.html')
  193. {
  194. if (!is_array($mParams))
  195. $mParams = ['name' => $mParams];
  196. return BxDolStudioTemplate::getInstance()->parseHtmlByName($sTemplateName, $mParams);
  197. }
  198. protected function getInjHeadLiveUpdates()
  199. {
  200. return '';
  201. }
  202. protected function getInjFooterPopupMenus()
  203. {
  204. $sResult = '';
  205. $oAccounMenu = BxDolMenu::getObjectInstance('sys_studio_account_popup');
  206. if($oAccounMenu)
  207. $sResult .= $this->transBox('bx-std-pcap-menu-popup-account', ['content' => $oAccounMenu->getCode(), 'wrapper_attrs' => 'aria-label="' . bx_html_attribute(_t('_adm_tmi_cpt_account')) . '"'], true);
  208. return $sResult;
  209. }
  210. }
  211. /** @} */