BxBaseFunctions.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. class BxBaseFunctions extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $_oTemplate;
  12. protected $_sDesignBoxMenuTmplDefault;
  13. protected $_sDesignBoxMenuIcon;
  14. protected $_sDesignBoxMenuIconType;
  15. protected $_sDesignBoxMenuClick;
  16. protected function __construct($oTemplate)
  17. {
  18. parent::__construct();
  19. $this->_oTemplate = $oTemplate ? $oTemplate : BxDolTemplate::getInstance();
  20. $this->_sDesignBoxMenuTmplDefault = 'menu_block_submenu_ver.html';
  21. $this->_sDesignBoxMenuIcon = 'ellipsis-v';
  22. $this->_sDesignBoxMenuIconType = 'icon';
  23. $this->_sDesignBoxMenuClick = "bx_menu_popup_inline('#{design_box_menu}', this, " . json_encode(array(
  24. 'moveToDocRoot' => false
  25. )) . ")";
  26. }
  27. /**
  28. * Prevent cloning the instance
  29. */
  30. public function __clone()
  31. {
  32. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  33. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  34. }
  35. public static function getInstanceWithTemplate($oTemplate)
  36. {
  37. $sClassName = 'BxTemplFunctions';
  38. if ($oTemplate){
  39. $sClassName .= get_class($oTemplate);
  40. }
  41. if(!isset($GLOBALS['bxDolClasses'][$sClassName]))
  42. $GLOBALS['bxDolClasses'][$sClassName] = new BxTemplFunctions($oTemplate);
  43. return $GLOBALS['bxDolClasses'][$sClassName];
  44. }
  45. public static function getInstance()
  46. {
  47. return self::getInstanceWithTemplate(null);
  48. }
  49. function TemplPageAddComponent($sKey)
  50. {
  51. $mixedResult = false; // if you have not such component, return false!
  52. switch($sKey) {
  53. case 'sys_header_width':
  54. $mixedResult = 'bx-def-page-width';
  55. break;
  56. case 'sys_toolbar_search':
  57. $oSearch = new BxTemplSearch();
  58. $oSearch->setLiveSearch(true);
  59. $mixedResult = $this->_oTemplate->parseHtmlByName('_page_toolbar_search.html', [
  60. 'sys_site_search' => $oSearch->getForm(BX_DB_PADDING_DEF, false, true) . $oSearch->getResultsContainer()
  61. ]);
  62. break;
  63. }
  64. return $mixedResult;
  65. }
  66. function msgBox($sText, $iTimer = 0, $sOnClose = "")
  67. {
  68. $iId = time() . mt_rand(1, 1000);
  69. if($iTimer > 0)
  70. BxDolTemplate::getInstance()->addJs(array('jquery.anim.js'));
  71. return $this->_oTemplate->parseHtmlByName('messageBox.html', array(
  72. 'id' => $iId,
  73. 'msgText' => $sText,
  74. 'bx_if:timer' => array(
  75. 'condition' => $iTimer > 0,
  76. 'content' => array(
  77. 'id' => $iId,
  78. 'time' => 1000 * $iTimer,
  79. 'on_close' => $sOnClose,
  80. )
  81. )
  82. ));
  83. }
  84. /**
  85. * Get standard popup box with title.
  86. *
  87. * @param string $sName - unique name
  88. * @param string $sTitle - translated title
  89. * @param string $sContent - content of the box
  90. * @return HTML string
  91. */
  92. function popupBox($sName, $sTitle, $sContent, $isHiddenByDefault = false)
  93. {
  94. $iId = !empty($sName) ? $sName : time();
  95. return $this->_oTemplate->parseHtmlByName('popup_box.html', array(
  96. 'id' => $iId,
  97. 'wrapper_style' => $isHiddenByDefault ? 'display:none;' : '',
  98. 'title' => $sTitle,
  99. 'content' => $sContent
  100. ));
  101. }
  102. /**
  103. * Get popup box without title.
  104. *
  105. * @param string $sName - unique name
  106. * @param string $sContent - content of the box
  107. * @return HTML string
  108. */
  109. function transBox($sName, $sContent, $isHiddenByDefault = false, $isPlaceInCenter = false)
  110. {
  111. return $this->simpleBox($sName, $sContent, $isHiddenByDefault, $isPlaceInCenter, 'popup_trans.html');
  112. }
  113. function slideBox($sName, $sContent, $isHiddenByDefault = false)
  114. {
  115. return $this->simpleBox($sName, $sContent, $isHiddenByDefault, false, 'popup_slide.html');
  116. }
  117. function inlineBox($sName, $sContent, $isHiddenByDefault = false)
  118. {
  119. return $this->simpleBox($sName, $sContent, $isHiddenByDefault, false, 'popup_inline.html');
  120. }
  121. protected function simpleBox($sName, $sContent, $isHiddenByDefault, $isPlaceInCenter, $sTemplate)
  122. {
  123. $iId = !empty($sName) ? $sName : time();
  124. if(!is_array($sContent))
  125. $sContent = array('content' => $sContent);
  126. $sContent = $this->_oTemplate->parseHtmlByName($sTemplate, array_merge(array(
  127. 'id' => $iId,
  128. 'wrapper_class' => '',
  129. 'wrapper_style' => $isHiddenByDefault ? 'display:none;' : '',
  130. 'content' => ''
  131. ), $sContent));
  132. if($isPlaceInCenter)
  133. $sContent = '<div class="login_ajax_wrap">' . $sContent . '</div>';
  134. return $sContent;
  135. }
  136. function simpleBoxContent($sContent, $bWithIndent = true)
  137. {
  138. if(!$bWithIndent)
  139. return $sContent;
  140. return $this->_oTemplate->parseHtmlByName('popup_content_indent.html', array(
  141. 'content' => $sContent
  142. ));
  143. }
  144. function getIcon($sCode, $aAttrs = array())
  145. {
  146. $sIconFont = false;
  147. $sIconA = false;
  148. $sIconUrl = false;
  149. $sIconHtml = false;
  150. $sIconFontWithHtml = false;
  151. $sClass = '';
  152. if(!empty($aAttrs['class'])) {
  153. $sClass = ' ' . $aAttrs['class'] .' ';
  154. unset($aAttrs['class']);
  155. }
  156. $sAttrs = '';
  157. foreach($aAttrs as $sKey => $sValue)
  158. $sAttrs .= ' ' . $sKey . '="' . bx_html_attribute($sValue) . '"';
  159. if (!empty($sCode)) {
  160. if (is_numeric($sCode) && (int)$sCode > 0) {
  161. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  162. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$sCode) : false;
  163. }
  164. else {
  165. if (strpos($sCode, '&lt;img') !== false || strpos($sCode, '<img') !== false) {
  166. if(strpos($sCode, '&lt;img') !== false)
  167. $sIconHtml = htmlspecialchars_decode($sCode);
  168. else
  169. $sIconHtml = $sCode;
  170. //--- Process Inline SVG
  171. } else if (strpos($sCode, '&lt;svg') !== false || strpos($sCode, '<svg') !== false) {
  172. if(strpos($sCode, '&lt;svg') !== false)
  173. $sIconHtml = htmlspecialchars_decode($sCode);
  174. else
  175. $sIconHtml = $sCode;
  176. $sClass .= 'sys-icon sys-icon-svg ';
  177. $sIconHtmlClear = strip_tags($sIconHtml, '<svg>');
  178. if ($sClass != '' && strpos($sIconHtmlClear, 'class="') !== false)
  179. $sIconHtml = str_replace('class="', 'class="' . $sClass, $sIconHtml);
  180. else
  181. $sIconHtml = str_replace('<svg', '<svg class="' . $sClass . '" ', $sIconHtml);
  182. if ($sAttrs != '')
  183. $sIconHtml = str_replace('<svg', '<svg ' . $sAttrs . ' ', $sIconHtml);
  184. }
  185. else {
  186. $sEmojIsRegex =
  187. '/[\x{0080}-\x{02AF}'
  188. .'\x{0300}-\x{03FF}'
  189. .'\x{0600}-\x{06FF}'
  190. .'\x{0C00}-\x{0C7F}'
  191. .'\x{1DC0}-\x{1DFF}'
  192. .'\x{1E00}-\x{1EFF}'
  193. .'\x{2000}-\x{209F}'
  194. .'\x{20D0}-\x{214F}'
  195. .'\x{2190}-\x{23FF}'
  196. .'\x{2460}-\x{25FF}'
  197. .'\x{2600}-\x{27EF}'
  198. .'\x{2900}-\x{29FF}'
  199. .'\x{2B00}-\x{2BFF}'
  200. .'\x{2C60}-\x{2C7F}'
  201. .'\x{2E00}-\x{2E7F}'
  202. .'\x{3000}-\x{303F}'
  203. .'\x{A490}-\x{A4CF}'
  204. .'\x{E000}-\x{F8FF}'
  205. .'\x{FE00}-\x{FE0F}'
  206. .'\x{FE30}-\x{FE4F}'
  207. .'\x{1F000}-\x{1F02F}'
  208. .'\x{1F0A0}-\x{1F0FF}'
  209. .'\x{1F100}-\x{1F64F}'
  210. .'\x{1F680}-\x{1F6FF}'
  211. .'\x{1F910}-\x{1F96B}'
  212. .'\x{1F980}-\x{1F9E0}]/u';
  213. //--- Process Emoji
  214. if(preg_match($sEmojIsRegex, $sCode, $aTmp))
  215. $sIconHtml = $this->_oTemplate->parseHtmlByName('icon_emoji.html', array(
  216. 'icon' => $sCode,
  217. 'class' => $sClass,
  218. 'attrs' => $sAttrs
  219. ));
  220. else {
  221. if (strpos($sCode, '.') === false) {
  222. //--- Process animated icon
  223. if (strncmp($sCode, 'a:', 2) === 0)
  224. $sIconA = substr($sCode, 2);
  225. //--- Process font icons
  226. else {
  227. $sIconFont = $sCode;
  228. $sIconFontWithHtml = $this->getFontIconAsHtml($sIconFont, $sClass, $sAttrs);
  229. }
  230. }
  231. else {
  232. //--- Process common image
  233. if((!preg_match('/^https?:\/\//', $sCode)))
  234. $sIconUrl = $this->_oTemplate->getIconUrl($sCode);
  235. else
  236. $sIconUrl = $sCode;
  237. }
  238. }
  239. }
  240. }
  241. }
  242. return array ($sIconFont, $sIconUrl, $sIconA, $sIconHtml, $sIconFontWithHtml);
  243. }
  244. function getFontIconAsHtml($sIconFont, $sClass = '', $sAttrs = '')
  245. {
  246. return '<i class="sys-icon ' . $sIconFont .' ' . $sClass . '"' . $sAttrs . '></i>';
  247. }
  248. function getIconAsHtml($sCode, $aAttrs = array())
  249. {
  250. $aIcons = $this->getIcon($sCode, $aAttrs);
  251. return $aIcons[3] . $aIcons[4];
  252. }
  253. function getIconPreview($iId, $sIconImage = '', $sIcon = '')
  254. {
  255. $bIconImage = !empty($sIconImage);
  256. $aIcons = $this->getIcon($sIcon);
  257. $sIconHtml = $aIcons[2] . $aIcons[3] . $aIcons[4];
  258. $bIconHtml = !empty($sIconHtml) && !$bIconImage;
  259. return $this->_oTemplate->parseHtmlByName('item_icon_preview.html', [
  260. 'id' => $iId,
  261. 'bx_if:show_icon_empty' => [
  262. 'condition' => !$bIconImage && !$bIconHtml,
  263. 'content' => []
  264. ],
  265. 'bx_if:show_icon_image' => [
  266. 'condition' => $bIconImage,
  267. 'content' => [
  268. 'url' => $sIconImage,
  269. 'id' => $iId
  270. ]
  271. ],
  272. 'bx_if:show_icon_html' => [
  273. 'condition' => $bIconHtml,
  274. 'content' => [
  275. 'icon' => $sIconHtml
  276. ]
  277. ]
  278. ]);
  279. }
  280. function getTemplateIcon($sName)
  281. {
  282. $sUrl = $this->_oTemplate->getIconUrl($sName);
  283. return !empty($sUrl) ? $sUrl : $this->_oTemplate->getIconUrl('spacer.gif');
  284. }
  285. function getTemplateImage($sName)
  286. {
  287. $sUrl = $this->_oTemplate->getImageUrl($sName);
  288. return !empty($sUrl) ? $sUrl : $this->_oTemplate->getImageUrl('spacer.gif');
  289. }
  290. function sysIcon ($sIcon, $sName, $sUrl = '', $iWidth = 0)
  291. {
  292. return '<div class="sys_icon">' . ($sUrl ? '<a title="'.$sName.'" href="'.$sUrl.'">' : '') . '<img alt="'.$sName.'" src="'.$sIcon.'" '.($iWidth ? 'width='.$iWidth : '').' />' . ($sUrl ? '</a>' : '') . '</div>';
  293. }
  294. /**
  295. * functions for limiting maximal string length
  296. */
  297. function getStringWithLimitedLength($mixedString, $iWidth = 45, $isPopupOnOverflow = false, $bReturnString = true, $sPopupString = null)
  298. {
  299. if(is_array($mixedString))
  300. list($sStrPlane, $sStrOriginal) = $mixedString;
  301. else
  302. $sStrPlane = $sStrOriginal = $mixedString;
  303. if(empty($sStrPlane) || mb_strlen($sStrPlane, 'UTF-8') <= $iWidth)
  304. return $bReturnString ? $sStrPlane : [$sStrPlane];
  305. $sResult = '';
  306. $aWords = mb_split("[\s\r\n]", $sStrPlane);
  307. $iPosition = 0;
  308. $iWidthReal = $iWidth - 3;
  309. $iWidthMin = $iWidth - 15;
  310. foreach($aWords as $sWord) {
  311. $sWord = trim($sWord);
  312. $iWord = mb_strlen($sWord, 'UTF-8');
  313. if ($iPosition + $iWord > $iWidthReal)
  314. break;
  315. // add word and continue
  316. $sResult .= ' ' . $sWord;
  317. $iPosition += 1 + $iWord;
  318. }
  319. // last word is too long, cut it
  320. if(!$iPosition || $iPosition < $iWidthMin)
  321. $sResult .= ' ' . mb_substr($sWord, 0, $iWidthReal - $iPosition - $iWord, 'UTF-8');
  322. $sResult = trim($sResult);
  323. // add tripple dot
  324. if(!$isPopupOnOverflow) {
  325. $sResult .= '...';
  326. return $bReturnString ? $sResult : [$sResult];
  327. }
  328. // add button width popup
  329. $sId = 'bx-str-limit-' . rand(1, PHP_INT_MAX);
  330. $sPopup = '<a class="bx-str-limit pl-2" href="javascript:void(0)" onclick="$(\'#' . $sId . '\').dolPopup({pointer:{el:$(this), offset:\'10 1\'}})"><i class="sys-icon ellipsis-h"></i></a>';
  331. $sPopup .= BxTemplFunctions::getInstance()->transBox($sId, '<div class="bx-def-padding">' . ($sPopupString ?? $sStrOriginal) . '</div>', true);
  332. return $bReturnString ? $sResult . $sPopup : [$sResult, $sPopup];
  333. }
  334. /**
  335. * Display design box with specified title, template, content and menu.
  336. * @param $sTitle - design box title, please note that some templates don't use title.
  337. * @param $sContent - design box content.
  338. * @param $iTemplateNum - number of design box template, use predefined contants only, default is BX_DB_DEF.
  339. * @param $mixedMenu - design box menu, it can be:
  340. * - object: instance of BxTemplMenu class
  341. * - string: menu object identifier
  342. * - array: array of menu links to create menu from
  343. * @param $mixedButtons - design box menu representation, it can be:
  344. * - false: design box menu will be used as horizontal menu (tabs)
  345. * - array: array of menu links to create menu from. If empty array is used and 'design box menu' isn't empty, then 'design box menu' will be added as one of menu items automatically. If non-empty array is used and 'design box menu' isn't empty then it should be added as one of menu items. Use array('menu' => 1) to define menu item for 'design box menu'.
  346. * @return string
  347. *
  348. * @see BX_DB_CONTENT_ONLY
  349. * @see BX_DB_DEF
  350. * @see BX_DB_EMPTY
  351. * @see BX_DB_NO_CAPTION
  352. * @see BX_DB_PADDING_CONTENT_ONLY
  353. * @see BX_DB_PADDING_DEF
  354. * @see BX_DB_PADDING_NO_CAPTION
  355. */
  356. function designBoxContent ($sTitle, $sContent, $iTemplateNum = BX_DB_DEF, $mixedMenu = false, $mixedButtons = [])
  357. {
  358. $bNoTitle = in_array($iTemplateNum, [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION]);
  359. $sMenu = $this->designBoxMenu($mixedMenu, $mixedButtons);
  360. $aTmplVarsDbMenu = [];
  361. if($bNoTitle && $sMenu)
  362. $aTmplVarsDbMenu = [
  363. 'caption_item' => $sMenu
  364. ];
  365. return $this->_oTemplate->parseHtmlByName('designbox_' . (int)$iTemplateNum . '.html', array_merge([
  366. 'title' => $sTitle,
  367. 'designbox_content' => $sContent,
  368. ], ($bNoTitle ? [
  369. 'bx_if:show_db_menu' => [
  370. 'condition' => !empty($aTmplVarsDbMenu),
  371. 'content' => $aTmplVarsDbMenu
  372. ]
  373. ] : [
  374. 'caption_item' => $sMenu
  375. ]))
  376. );
  377. }
  378. function designBoxMenu ($mixedMenu, $mixedButtons = [])
  379. {
  380. $bUseTabs = is_bool($mixedButtons) && $mixedButtons === true;
  381. $sClass = $sMenu = '';
  382. if(!empty($mixedMenu)) {
  383. if(is_string($mixedMenu)) {
  384. if(($oMenu = BxTemplMenu::getObjectInstance($mixedMenu)) !== false) {
  385. $oMenu->setTemplateById($bUseTabs ? BX_DB_MENU_TEMPLATE_TABS : BX_DB_MENU_TEMPLATE_POPUP);
  386. $sMenu = $oMenu->getCode();
  387. }
  388. else
  389. $sMenu = $mixedMenu;
  390. }
  391. else if(is_array($mixedMenu)) {
  392. if(isset($mixedMenu['template']) && isset($mixedMenu['menu_items']))
  393. $aMenu = $mixedMenu;
  394. else
  395. $aMenu = array('template' => $this->_sDesignBoxMenuTmplDefault, 'menu_items' => $mixedMenu);
  396. if(($oMenu = new BxTemplMenu($aMenu, $this->_oTemplate)) !== false) {
  397. $oMenu->setTemplateById($bUseTabs ? BX_DB_MENU_TEMPLATE_TABS : BX_DB_MENU_TEMPLATE_POPUP);
  398. $sMenu = $oMenu->getCode();
  399. }
  400. else
  401. $sMenu = '';
  402. }
  403. else if(is_object($mixedMenu) && is_a($mixedMenu, 'BxTemplMenu')) {
  404. $mixedMenu->setTemplateById($bUseTabs ? BX_DB_MENU_TEMPLATE_TABS : BX_DB_MENU_TEMPLATE_POPUP);
  405. if(($mixedMenu instanceof BxBaseMenuMoreAuto) && $mixedMenu->isMoreAuto())
  406. $sClass = ' bx-db-menu-tab-more-auto';
  407. $sMenu = $mixedMenu->getCode();
  408. }
  409. }
  410. $bMenu = !empty($sMenu);
  411. $sResult = '';
  412. if($bUseTabs && $bMenu)
  413. $sResult = $sMenu;
  414. else if(is_array($mixedButtons)) {
  415. $sPopup = '';
  416. if($bMenu) {
  417. $aButton = array();
  418. if(empty($mixedButtons))
  419. list($aButton, $sPopup) = $this->_designBoxMenuButton($sMenu);
  420. //--- For backward compatibility
  421. else if(!empty($mixedButtons['menu']) && is_array($mixedButtons['menu'])) {
  422. list($aButton, $sPopup) = $this->_designBoxMenuButton($sMenu, $mixedButtons['menu']);
  423. unset($mixedButtons['menu']);
  424. }
  425. if(!empty($aButton))
  426. $mixedButtons[] = $aButton;
  427. }
  428. foreach($mixedButtons as $aButton) {
  429. if($bMenu && isset($aButton['menu'])) {
  430. if(is_numeric($aButton['menu']) && (int)$aButton['menu'] == 1)
  431. list($aButton, $sPopup) = $this->_designBoxMenuButton($sMenu);
  432. else if(is_array($aButton['menu']))
  433. list($aButton, $sPopup) = $this->_designBoxMenuButton($sMenu, $aButton['menu']);
  434. if(isset($aButton['menu']))
  435. continue;
  436. }
  437. $aAttrs = [];
  438. if(!empty($aButton['href']))
  439. $aAttrs['href'] = $aButton['href'];
  440. if(!empty($aButton['onclick'])) {
  441. $aAttrs['href'] = "javascript:void(0);";
  442. $aAttrs['onclick'] = $aButton['onclick'];
  443. }
  444. $aAttrs['class'] = 'bx-btn bx-btn-small';
  445. if(!empty($aButton['class']))
  446. $aAttrs['class'] .= ' ' . trim($aButton['class']);
  447. $bTmplVarsButtonIcon = !empty($aButton['icon']);
  448. $aTmplVarsButtonIcon = !$bTmplVarsButtonIcon ? array() : array(
  449. 'icon' => $aButton['icon']
  450. );
  451. $bTmplVarsButtonIconA = !empty($aButton['icon-a']);
  452. $aTmplVarsButtonIconA = !$bTmplVarsButtonIconA ? array() : array(
  453. 'icon_a' => $aButton['icon-a']
  454. );
  455. $bTmplVarsButtonTitle = !empty($aButton['title']);
  456. $aTmplVarsButtonTitle = !$bTmplVarsButtonTitle ? array() : array(
  457. 'title' => $aButton['title']
  458. );
  459. $sResult .= $this->_oTemplate->parseHtmlByName('designbox_menu_button.html', array(
  460. 'attrs' => bx_convert_array2attrs($aAttrs),
  461. 'bx_if:show_icon' => array(
  462. 'condition' => $bTmplVarsButtonIcon,
  463. 'content' => $aTmplVarsButtonIcon
  464. ),
  465. 'bx_if:show_icon_a' => array(
  466. 'condition' => $bTmplVarsButtonIconA,
  467. 'content' => $aTmplVarsButtonIconA
  468. ),
  469. 'bx_if:show_title' => array(
  470. 'condition' => $bTmplVarsButtonTitle,
  471. 'content' => $aTmplVarsButtonTitle
  472. )
  473. ));
  474. }
  475. $sResult .= $sPopup;
  476. }
  477. if(!empty($sResult))
  478. $sResult = $this->_oTemplate->parseHtmlByName('designbox_menu.html', array(
  479. 'class' => $sClass,
  480. 'content' => $sResult
  481. ));
  482. return $sResult;
  483. }
  484. protected function _designBoxMenuId ()
  485. {
  486. return 'bx-menu-db-' . time() . rand(0, PHP_INT_MAX);
  487. }
  488. protected function _designBoxMenuButton ($sMenu, $aParams = array())
  489. {
  490. $sId = $this->_designBoxMenuId();
  491. $aButton = array($this->_sDesignBoxMenuIconType => $this->_sDesignBoxMenuIcon, 'onclick' => $this->_sDesignBoxMenuClick);
  492. if(!empty($aParams)) {
  493. if(!empty($aParams['id']))
  494. $sId = $aParams['id'];
  495. $aButton = array_merge($aButton, $aParams);
  496. }
  497. $aButton['onclick'] = bx_replace_markers($aButton['onclick'], array(
  498. 'design_box_menu' => $sId
  499. ));
  500. $sMenu = $this->_oTemplate->parseHtmlByName('designbox_menu_popup.html', array(
  501. 'content' => $sMenu
  502. ));
  503. return array($aButton, $this->transBox($sId, $sMenu, true));
  504. }
  505. /**
  506. * Get logo URL.
  507. * @return string
  508. */
  509. function getMainLogoUrl()
  510. {
  511. return BxDolDesigns::getInstance()->getSiteLogoUrl();
  512. }
  513. /**
  514. * Get mark URL.
  515. * @return string
  516. */
  517. function getMainMarkUrl()
  518. {
  519. return BxDolDesigns::getInstance()->getSiteMarkUrl();
  520. }
  521. /**
  522. * Get logo HTML.
  523. * @return string
  524. */
  525. function getMainLogo($aParams = array())
  526. {
  527. $oDesigns = BxDolDesigns::getInstance();
  528. $sAlt = $oDesigns->getSiteLogoAlt();
  529. if(empty($sAlt))
  530. $sAlt = getParam('site_title');
  531. $aLogoImages = [];
  532. if(($mixedFileUrl = $this->getMainLogoUrl()) && !empty($mixedFileUrl)) {
  533. if(!is_array($mixedFileUrl))
  534. $mixedFileUrl = ['light' => $mixedFileUrl];
  535. foreach($mixedFileUrl as $sName => $sFileUrl) {
  536. $iLogoWidth = (int)$oDesigns->getSiteLogoWidth();
  537. $sWidth = $iLogoWidth > 0 ? 'width:' . round($iLogoWidth/16, 3) . 'rem;' : '';
  538. $iLogoHeight = (int)$oDesigns->getSiteLogoHeight();
  539. $sHeight = $iLogoHeight > 0 ? 'height:' . round($iLogoHeight/16, 3) . 'rem;' : '';
  540. $aLogoImages[] = [
  541. 'id' => 'bx-logo',
  542. 'src' => $sFileUrl,
  543. 'alt' => bx_html_attribute($sAlt, BX_ESCAPE_STR_QUOTE),
  544. 'style' => $sWidth . ' ' . $sHeight
  545. ];
  546. }
  547. }
  548. $bLogoImages = !empty($aLogoImages) && is_array($aLogoImages);
  549. $aAttrs = array(
  550. 'class' => 'bx-def-font-contrasted',
  551. 'href' => BX_DOL_URL_ROOT,
  552. 'title' => bx_html_attribute($sAlt, BX_ESCAPE_STR_QUOTE)
  553. );
  554. if(!empty($aParams['attrs']) && is_array($aParams['attrs']))
  555. $aAttrs = array_merge($aAttrs, $aParams['attrs']);
  556. $sTmplName = 'logo_main.html';
  557. $aTmplVars = [
  558. 'attrs' => bx_convert_array2attrs($aAttrs),
  559. 'bx_if:show_title' => [
  560. 'condition' => !$bLogoImages,
  561. 'content' => [
  562. 'logo' => $sAlt,
  563. ]
  564. ],
  565. 'bx_if:show_logo' => [
  566. 'condition' => $bLogoImages,
  567. 'content' => [
  568. 'bx_repeat:images' => $aLogoImages,
  569. ]
  570. ],
  571. ];
  572. bx_alert('system', 'get_logo', 0, 0, [
  573. 'tmpl_name' => &$sTmplName,
  574. 'tmpl_vars' => &$aTmplVars
  575. ]);
  576. return $this->_oTemplate->parseHtmlByName($sTmplName, $aTmplVars);
  577. }
  578. /**
  579. * Get HTML code for manifests.
  580. * @return HTML string to insert into HEAD section
  581. */
  582. function getManifests()
  583. {
  584. return '<link rel="manifest" href="' . BX_DOL_URL_ROOT . 'manifest.json.php" crossorigin="use-credentials" />';
  585. }
  586. /**
  587. * Get HTML code for meta icons.
  588. * @return HTML string to insert into HEAD section
  589. */
  590. function getMetaIcons()
  591. {
  592. // favicon icon
  593. $sImageUrlFav = '';
  594. if(($iId = (int)getParam('sys_site_icon')) != 0)
  595. $sImageUrlFav = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_FILES)->getFileUrlById($iId);
  596. // svg icon
  597. $sImageUrlSvg = '';
  598. if(($iId = (int)getParam('sys_site_icon_svg')) != 0)
  599. $sImageUrlSvg = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES)->getFileUrlById($iId);
  600. if(empty($sImageUrlFav) && empty($sImageUrlSvg))
  601. $sImageUrlFav = $sImageUrlSvg = $this->_oTemplate->getIconUrl('favicon.svg');
  602. // apple device icon
  603. $sImageUrlApl = '';
  604. if(($iId = (int)getParam('sys_site_icon_apple')) != 0)
  605. $sImageUrlApl = BxDolTranscoderImage::getObjectInstance(BX_DOL_TRANSCODER_OBJ_ICON_APPLE)->getFileUrl($iId);
  606. if(empty($sImageUrlApl))
  607. $sImageUrlApl = $this->_oTemplate->getIconUrl('apple-touch-icon.png');
  608. /*
  609. * TODO:
  610. * 1. Remove commented code later if it won't be used.
  611. * 2. Remove 'sys_icon_favicon' and 'sys_icon_facebook' transcoders and related transcoder filters.
  612. *
  613. // facebook icon
  614. $sImageUrlFcb = '';
  615. $oTranscoder = BxDolTranscoderImage::getObjectInstance(BX_DOL_TRANSCODER_OBJ_ICON_FACEBOOK);
  616. $sImageUrlFcb = $oTranscoder->getFileUrl($iId);
  617. if(empty($sImageUrlFcb))
  618. $sImageUrlFcb = $this->_oTemplate->getIconUrl('facebook-icon.png');
  619. */
  620. $sRet = '';
  621. if($sImageUrlFav)
  622. $sRet .= '<link rel="icon" href="' . $sImageUrlFav . '" sizes="any" />';
  623. if($sImageUrlSvg)
  624. $sRet .= '<link rel="icon" href="' . $sImageUrlSvg . '" type="image/svg+xml" />';
  625. $sRet .= '<link rel="apple-touch-icon" href="' . $sImageUrlApl . '" />';
  626. /*
  627. $sRet .= '<link rel="image_src" sizes="100x100" href="' . $sImageUrlFcb . '" />';
  628. */
  629. return $sRet;
  630. }
  631. function getInjectionHead()
  632. {
  633. return $this->getInjection('getInjHead');
  634. }
  635. function getInjectionHeader()
  636. {
  637. return $this->getInjection('getInjHeader');
  638. }
  639. function getInjectionFooter()
  640. {
  641. return $this->getInjection('getInjFooter');
  642. }
  643. public function getPopupAlert()
  644. {
  645. return $this->transBox('bx-popup-alert', $this->_oTemplate->parseHtmlByName('popup_trans_alert_cnt.html', array()), true);
  646. }
  647. public function getPopupConfirm()
  648. {
  649. return $this->transBox('bx-popup-confirm', $this->_oTemplate->parseHtmlByName('popup_trans_confirm_cnt.html', array()), true);
  650. }
  651. public function getPopupPrompt()
  652. {
  653. $sInputText = 'bx-popup-prompt-value';
  654. $aInputText = array(
  655. 'type' => 'text',
  656. 'name' => $sInputText,
  657. 'attrs' => array(
  658. 'id' => $sInputText,
  659. ),
  660. 'value' => '',
  661. 'caption' => ''
  662. );
  663. $oForm = new BxTemplFormView(array(), $this->_oTemplate);
  664. return $this->transBox('bx-popup-prompt', $this->_oTemplate->parseHtmlByName('popup_trans_prompt_cnt.html', array(
  665. 'input' => $oForm->genRow($aInputText)
  666. )), true);
  667. }
  668. /**
  669. * Output time wrapped in <time> tag in HTML.
  670. * Then time is autoformatted using JS upon page load, this is aumatically converted to user's timezone and
  671. * updated in realtime in case of short periods of 'from now' time format.
  672. *
  673. * Short version of this function:
  674. * @see bx_time_js
  675. *
  676. * @param $iUnixTimestamp time as unixtimestamp
  677. * @param $sFormatIdentifier output format identifier
  678. * @see BX_FORMAT_DATE
  679. * @see BX_FORMAT_TIME
  680. * @see BX_FORMAT_DATE_TIME
  681. * @param $bForceFormat force provided format and don't use "from now" time autoformat.
  682. */
  683. function timeForJs ($iUnixTimestamp, $sFormatIdentifier = BX_FORMAT_DATE, $bForceFormat = false)
  684. {
  685. $sDateUTC = bx_time_utc ($iUnixTimestamp);
  686. return $this->timeForJsFullDate ($sDateUTC, $sFormatIdentifier, $bForceFormat);
  687. }
  688. /**
  689. * Same as @see timeForJs but instead of unxtimestamp full date format is used (ex: 2005-08-15T15:52:01) as passec date param
  690. */
  691. function timeForJsFullDate ($sDateUTC, $sFormatIdentifier = BX_FORMAT_DATE, $bForceFormat = false, $bUTC = false)
  692. {
  693. return '<time datetime="' . $sDateUTC . '" data-bx-format="' . getParam($sFormatIdentifier) . '" data-bx-autoformat="' . ($bForceFormat ? 0 : getParam('sys_format_timeago')) . '" data-bx-utc="' . ($bUTC ? 1 : 0) . '">' . $sDateUTC . '</time>';
  694. }
  695. function statusOnOff ($mixed, $isMsg = false)
  696. {
  697. if ((is_bool($mixed) && !$mixed) || (is_string($mixed) && 'fail' == $mixed))
  698. return '<i class="sys-icon circle col-red2"></i> ' . ($isMsg ? _t('_sys_off') : '');
  699. elseif (is_string($mixed) && 'warn' == $mixed)
  700. return '<i class="sys-icon circle col-red3"></i> ' . ($isMsg ? _t('_sys_warn') : '');
  701. elseif (is_string($mixed) && 'undef' == $mixed)
  702. return '<i class="sys-icon circle col-gray"></i> ' . ($isMsg ? _t('_undefined') : '');
  703. else
  704. return '<i class="sys-icon circle col-green1"></i> ' . ($isMsg ? _t('_sys_on') : '');
  705. }
  706. /**
  707. * Ouputs HTML5 video player.
  708. * @param $sUrlPoster video poster image
  709. * @param $sUrlMP4 .mp4 video
  710. * @param $sUrlMP4Hd .mp4 video in better quality
  711. * @param $aAttrs custom attributes, defaults are: controls="" preload="none" autobuffer=""
  712. * @param $sStyles custom styles, defaults are: width:100%; height:auto;
  713. */
  714. function videoPlayer ($sUrlPoster, $sUrlMP4, $sUrlMP4Hd = '', $aAttrs = false, $sStyles = false, $bDynamicMode = false)
  715. {
  716. $oPlayer = BxDolPlayer::getObjectInstance();
  717. if(!$oPlayer)
  718. return '';
  719. if($sStyles === false)
  720. $sStyles = 'width:100%; height:auto;';
  721. return $oPlayer->getCodeVideo (BX_PLAYER_STANDARD, array(
  722. 'poster' => $sUrlPoster,
  723. 'mp4' => array('sd' => $sUrlMP4, 'hd' => $sUrlMP4Hd),
  724. 'attrs' => $aAttrs,
  725. 'styles' => $sStyles,
  726. ), $bDynamicMode);
  727. }
  728. protected function getInjection($sPrefix)
  729. {
  730. $sContent = '';
  731. $aMethods = get_class_methods($this);
  732. foreach($aMethods as $sMethod)
  733. if(preg_match("/^(" . $sPrefix . ")[A-Z].+$/", $sMethod))
  734. $sContent .= $this->$sMethod();
  735. return $sContent;
  736. }
  737. protected function getInjHeadLiveUpdates()
  738. {
  739. $sContent = '';
  740. if(($oLiveUpdates = BxDolLiveUpdates::getInstance()) !== false)
  741. $sContent .= $oLiveUpdates->init();
  742. return $sContent;
  743. }
  744. protected function getInjHeaderPushNotifications()
  745. {
  746. $sResult = '';
  747. if(($oPush = BxDolPush::getObjectInstance()) !== false)
  748. $sResult = $oPush->getCode('page_header');
  749. return $sResult;
  750. }
  751. protected function getInjHeaderPopupLoading()
  752. {
  753. return $this->transBox('bx-popup-loading', $this->_oTemplate->parsePageByName('popup_loading.html', array()), true);
  754. }
  755. protected function getInjFooterMenuLoading()
  756. {
  757. return $this->_oTemplate->parsePageByName('menu_loading.html', array());
  758. }
  759. protected function getInjFooterPopupMenus()
  760. {
  761. $sContent = '';
  762. $oSearch = new BxTemplSearch();
  763. $oSearch->setLiveSearch(true);
  764. $sContent .= $this->_oTemplate->parsePageByName('search.html', array(
  765. 'search_form' => $oSearch->getForm(BX_DB_CONTENT_ONLY),
  766. 'results' => $oSearch->getResultsContainer(),
  767. ));
  768. $sContent .= $this->_oTemplate->getMenu ('sys_site');
  769. if(isLogged()) {
  770. $sContent .= $this->_oTemplate->getMenu ('sys_add_content');
  771. $sContent .= $this->_oTemplate->getMenu ('sys_account_popup');
  772. }
  773. return $sContent;
  774. }
  775. protected function getInjFooterPopupApps()
  776. {
  777. if($this->_oTemplate->getPageType() != BX_PAGE_TYPE_APPLICATION)
  778. return '';
  779. $oMenu = BxDolMenu::getObjectInstance('sys_homepage');
  780. if(!$oMenu)
  781. return '';
  782. $this->_oTemplate->addJs(['popper.js']);
  783. return $this->_oTemplate->parsePageByName('menu_apps.html', [
  784. 'name' => 'sys-menu-apps',
  785. 'title' => _t('_apps'),
  786. 'bx_repeat:menu_items' => $oMenu->getMenuItems(),
  787. ]);
  788. }
  789. protected function getInjFooterPopups()
  790. {
  791. $sContent = '';
  792. $sContent .= $this->getPopupAlert();
  793. $sContent .= $this->getPopupConfirm();
  794. $sContent .= $this->getPopupPrompt();
  795. return $sContent;
  796. }
  797. protected function getInjFooterEmbed()
  798. {
  799. // Load embed files
  800. $oEmbed = BxDolEmbed::getObjectInstance(false);
  801. if ($oEmbed)
  802. return $oEmbed->addJsCss() . $oEmbed->addProcessLinkMethod();
  803. return '';
  804. }
  805. }
  806. /** @} */