1
0

BxBaseFunctions.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 $_sDesignBoxIcon = 'chevron';
  13. protected function __construct($oTemplate)
  14. {
  15. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  16. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  17. parent::__construct();
  18. $this->_oTemplate = $oTemplate ? $oTemplate : BxDolTemplate::getInstance();
  19. }
  20. /**
  21. * Prevent cloning the instance
  22. */
  23. public function __clone()
  24. {
  25. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  26. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  27. }
  28. public static function getInstance()
  29. {
  30. if(!isset($GLOBALS['bxDolClasses']['BxTemplFunctions']))
  31. $GLOBALS['bxDolClasses']['BxTemplFunctions'] = new BxTemplFunctions();
  32. return $GLOBALS['bxDolClasses']['BxTemplFunctions'];
  33. }
  34. function TemplPageAddComponent($sKey)
  35. {
  36. switch( $sKey ) {
  37. case 'something':
  38. return false; // return here additional components
  39. default:
  40. return false; // if you have not such component, return false!
  41. }
  42. }
  43. function msgBox($sText, $iTimer = 0, $sOnClose = "")
  44. {
  45. $iId = time() . mt_rand(1, 1000);
  46. return $this->_oTemplate->parseHtmlByName('messageBox.html', array(
  47. 'id' => $iId,
  48. 'msgText' => $sText,
  49. 'bx_if:timer' => array(
  50. 'condition' => $iTimer > 0,
  51. 'content' => array(
  52. 'id' => $iId,
  53. 'time' => 1000 * $iTimer,
  54. 'on_close' => $sOnClose,
  55. )
  56. )
  57. ));
  58. }
  59. /**
  60. * Get standard popup box with title.
  61. *
  62. * @param string $sName - unique name
  63. * @param string $sTitle - translated title
  64. * @param string $sContent - content of the box
  65. * @return HTML string
  66. */
  67. function popupBox($sName, $sTitle, $sContent, $isHiddenByDefault = false)
  68. {
  69. $iId = !empty($sName) ? $sName : time();
  70. return $this->_oTemplate->parseHtmlByName('popup_box.html', array(
  71. 'id' => $iId,
  72. 'wrapper_style' => $isHiddenByDefault ? 'display:none;' : '',
  73. 'title' => $sTitle,
  74. 'content' => $sContent
  75. ));
  76. }
  77. /**
  78. * Get popup box without title.
  79. *
  80. * @param string $sName - unique name
  81. * @param string $sContent - content of the box
  82. * @return HTML string
  83. */
  84. function transBox($sName, $sContent, $isHiddenByDefault = false, $isPlaceInCenter = false)
  85. {
  86. return $this->simpleBox($sName, $sContent, $isHiddenByDefault, $isPlaceInCenter, 'popup_trans.html');
  87. }
  88. function slideBox($sName, $sContent, $isHiddenByDefault = false)
  89. {
  90. return $this->simpleBox($sName, $sContent, $isHiddenByDefault, false, 'popup_slide.html');
  91. }
  92. function inlineBox($sName, $sContent, $isHiddenByDefault = false)
  93. {
  94. return $this->simpleBox($sName, $sContent, $isHiddenByDefault, false, 'popup_inline.html');
  95. }
  96. protected function simpleBox($sName, $sContent, $isHiddenByDefault, $isPlaceInCenter, $sTemplate) {
  97. $iId = !empty($sName) ? $sName : time();
  98. $sContent = $this->_oTemplate->parseHtmlByName($sTemplate, array(
  99. 'id' => $iId,
  100. 'wrapper_style' => $isHiddenByDefault ? 'display:none;' : '',
  101. 'content' => $sContent
  102. ));
  103. if($isPlaceInCenter)
  104. $sContent = '<div class="login_ajax_wrap">' . $sContent . '</div>';
  105. return $sContent;
  106. }
  107. function getTemplateIcon($sName)
  108. {
  109. $sUrl = $this->_oTemplate->getIconUrl($sName);
  110. return !empty($sUrl) ? $sUrl : $this->_oTemplate->getIconUrl('spacer.gif');
  111. }
  112. function getTemplateImage($sName)
  113. {
  114. $sUrl = $this->_oTemplate->getImageUrl($sName);
  115. return !empty($sUrl) ? $sUrl : $this->_oTemplate->getImageUrl('spacer.gif');
  116. }
  117. function sysIcon ($sIcon, $sName, $sUrl = '', $iWidth = 0)
  118. {
  119. return '<div class="sys_icon">' . ($sUrl ? '<a title="'.$sName.'" href="'.$sUrl.'">' : '') . '<img alt="'.$sName.'" src="'.$sIcon.'" '.($iWidth ? 'width='.$iWidth : '').' />' . ($sUrl ? '</a>' : '') . '</div>';
  120. }
  121. /**
  122. * functions for limiting maximal string length
  123. */
  124. function getStringWithLimitedLength($sString, $iWidth = 45, $isPopupOnOverflow = false, $bReturnString = true)
  125. {
  126. if (empty($sString) || mb_strlen($sString, 'UTF-8') <= $iWidth)
  127. return $bReturnString ? $sString : array($sString);
  128. $sResult = '';
  129. $aWords = mb_split("[\s\r\n]", $sString);
  130. $iPosition = 0;
  131. $iWidthReal = $iWidth - 3;
  132. $iWidthMin = $iWidth - 15;
  133. foreach($aWords as $sWord) {
  134. $sWord = trim($sWord);
  135. $iWord = mb_strlen($sWord, 'UTF-8');
  136. if ($iPosition + $iWord > $iWidthReal)
  137. break;
  138. // add word and continue
  139. $sResult .= ' ' . $sWord;
  140. $iPosition += 1 + $iWord;
  141. }
  142. // last word is too long, cut it
  143. if(!$iPosition || $iPosition < $iWidthMin)
  144. $sResult .= ' ' . mb_substr($sWord, 0, $iWidthReal - $iPosition - $iWord, 'UTF-8');
  145. $sResult = trim($sResult);
  146. // add tripple dot
  147. if(!$isPopupOnOverflow) {
  148. $sResult .= '...';
  149. return $bReturnString ? $sResult : array($sResult);
  150. }
  151. // add button width popup
  152. $sId = 'bx-str-limit-' . rand(1, PHP_INT_MAX);
  153. $sPopup = '<img class="bx-str-limit" onclick="$(\'#' . $sId . '\').dolPopup({pointer:{el:$(this), offset:\'10 1\'}})" src="' . $this->getTemplateImage('str-limit.png') . '"/>';
  154. $sPopup .= '<div id="' . $sId . '" style="display:none;">' . BxTemplFunctions::getInstance()->transBox('', '<div class="bx-def-padding bx-def-color-bg-block">'.$sString.'</div>') . '</div>';
  155. return $bReturnString ? $sResult . $sPopup : array($sResult, $sPopup);
  156. }
  157. /**
  158. * Display design box with specified title, template, content and menu.
  159. * @param $sTitle - design box title, please note that some templates don't use title.
  160. * @param $sContent - design box content.
  161. * @param $iTemplateNum - number of design box template, use predefined contants only, default is BX_DB_DEF.
  162. * @param $mixedMenu - design box menu, it can be:
  163. * - object: instance of BxTemplMenu class
  164. * - string: menu object identifier
  165. * - array: array of menu links to create menu from
  166. * @return string
  167. *
  168. * @see BX_DB_CONTENT_ONLY
  169. * @see BX_DB_DEF
  170. * @see BX_DB_EMPTY
  171. * @see BX_DB_NO_CAPTION
  172. * @see BX_DB_PADDING_CONTENT_ONLY
  173. * @see BX_DB_PADDING_DEF
  174. * @see BX_DB_PADDING_NO_CAPTION
  175. */
  176. function designBoxContent ($sTitle, $sContent, $iTemplateNum = BX_DB_DEF, $mixedMenu = false)
  177. {
  178. return $this->_oTemplate->parseHtmlByName('designbox_' . (int)$iTemplateNum . '.html', array(
  179. 'title' => $sTitle,
  180. 'designbox_content' => $sContent,
  181. 'caption_item' => $this->designBoxMenu ($mixedMenu, array (array('menu' => 1))),
  182. ));
  183. }
  184. function designBoxMenu ($mixedMenu, $aButtons = array ())
  185. {
  186. $sCode = '';
  187. $aButtonMenu = false;
  188. if ($mixedMenu) {
  189. $sMenu = '';
  190. if (is_string($mixedMenu)) {
  191. $oMenu = BxTemplMenu::getObjectInstance($mixedMenu);
  192. $sMenu = $oMenu ? $oMenu->getCode () : $mixedMenu;
  193. } elseif (is_object($mixedMenu) && is_a($mixedMenu, 'BxTemplMenu')) {
  194. $sMenu = $mixedMenu->getCode();
  195. } elseif (is_array($mixedMenu)) {
  196. if (isset($mixedMenu['template']) && isset($mixedMenu['menu_items']))
  197. $aMenu = $mixedMenu;
  198. else
  199. $aMenu = array ('template' => 'menu_vertical.html', 'menu_items' => $mixedMenu);
  200. $oMenu = new BxTemplMenu($aMenu, $this->_oTemplate);
  201. $sMenu = $oMenu->getCode ();
  202. }
  203. if ($sMenu) {
  204. $sId = 'bx-menu-db-' . time() . rand(0, PHP_INT_MAX);
  205. $sCode .= $this->slideBox($sId, $sMenu, true);
  206. $aButtonMenu = array ('icon-a' => $this->_sDesignBoxIcon, 'onclick' => "bx_menu_slide('#" . $sId . "', this)");
  207. }
  208. }
  209. if ($aButtons) {
  210. $sCode .= '<div class="bx-db-menu"><div class="bx-db-menu-tab bx-db-menu-tab-btn">';
  211. foreach ($aButtons as $aButton) {
  212. if (isset($aButton['menu']) && $aButton['menu']) {
  213. if (!$aButtonMenu)
  214. continue;
  215. $aButton = $aButtonMenu;
  216. }
  217. $aAttrs = array ();
  218. if (!empty($aButton['onclick']))
  219. $aAttrs['onclick'] = $aButton['onclick'];
  220. if (!empty($aButton['class']))
  221. $aAttrs['class'] = $aButton['class'];
  222. $sAttrs = bx_convert_array2attrs ($aAttrs, 'bx-def-margin-sec-left');
  223. $sCode .= '<a href="javascript:void(0);" ' . $sAttrs . '>';
  224. $sCode .= !empty($aButton['icon']) ? '<i class="sys-icon ' . $aButton['icon'] . ' bx-def-font-h2"></i>' : '';
  225. $sCode .= !empty($aButton['icon-a']) ? '<i class="sys-icon-a" data-rotate="down" data-icon="' . $aButton['icon-a'] . '"></i>' : '';
  226. $sCode .= !empty($aButton['title']) ? $aButton['title'] : '';
  227. $sCode .= '</a>';
  228. }
  229. $sCode .= '</div></div>';
  230. }
  231. return $sCode;
  232. }
  233. /**
  234. * Get logo URL.
  235. * @return string
  236. */
  237. function getMainLogoUrl()
  238. {
  239. $oDesigns = BxDolDesigns::getInstance();
  240. $iFileId = (int)$oDesigns->getSiteLogo();
  241. if (!$iFileId)
  242. return false;
  243. $oStorage = BxDolTranscoder::getObjectInstance('sys_custom_images');
  244. $sFileUrl = $oStorage->getFileUrl($iFileId);
  245. if (!$sFileUrl)
  246. return false;
  247. $iLogoWidth = (int)$oDesigns->getSiteLogoWidth();
  248. if($iLogoWidth > 0)
  249. $sFileUrl = bx_append_url_params($sFileUrl, array('x' => $iLogoWidth));
  250. $iLogoHeight = (int)$oDesigns->getSiteLogoHeight();
  251. if($iLogoHeight > 0)
  252. $sFileUrl = bx_append_url_params($sFileUrl, array('y' => $iLogoHeight));
  253. return $sFileUrl;
  254. }
  255. /**
  256. * Get logo HTML.
  257. * @return string
  258. */
  259. function getMainLogo()
  260. {
  261. $oDesigns = BxDolDesigns::getInstance();
  262. $sAlt = $oDesigns->getSiteLogoAlt();
  263. if(empty($sAlt))
  264. $sAlt = getParam('site_title');
  265. $sLogo = '<span>' . $sAlt . '</span>';
  266. $sFileUrl = $this->getMainLogoUrl();
  267. if (!empty($sFileUrl)) {
  268. $iLogoWidth = (int)$oDesigns->getSiteLogoWidth();
  269. $sMaxWidth = $iLogoWidth > 0 ? 'max-width:' . round($iLogoWidth/16, 3) . 'rem;' : '';
  270. $iLogoHeight = (int)$oDesigns->getSiteLogoHeight();
  271. $sMaxHeight = $iLogoHeight > 0 ? 'max-height:' . round($iLogoHeight/16, 3) . 'rem;' : '';
  272. $sLogo = '<img style="' . $sMaxWidth . $sMaxHeight . '" src="' . $sFileUrl . '" id="bx-logo" alt="' . bx_html_attribute($sAlt, BX_ESCAPE_STR_QUOTE) . '" />';
  273. }
  274. return '<a class="bx-def-font-contrasted" href="' . BX_DOL_URL_ROOT . '" title="' . bx_html_attribute($sAlt, BX_ESCAPE_STR_QUOTE) . '">' . $sLogo . '</a>';
  275. }
  276. /**
  277. * Get HTML code for meta icons.
  278. * @return HTML string to insert into HEAD section
  279. */
  280. function getMetaIcons()
  281. {
  282. $iId = (int)getParam('sys_site_icon');
  283. $sImageUrlFav = $sImageUrlFcb = $sImageUrlApl = '';
  284. if(!empty($iId)) {
  285. // favicon icon
  286. $oTranscoder = BxDolTranscoderImage::getObjectInstance(BX_DOL_TRANSCODER_OBJ_ICON_FAVICON);
  287. $sImageUrlFav = $oTranscoder->getFileUrl($iId);
  288. // facebook icon
  289. $oTranscoder = BxDolTranscoderImage::getObjectInstance(BX_DOL_TRANSCODER_OBJ_ICON_FACEBOOK);
  290. $sImageUrlFcb = $oTranscoder->getFileUrl($iId);
  291. // apple touch icon
  292. $oTranscoder = BxDolTranscoderImage::getObjectInstance(BX_DOL_TRANSCODER_OBJ_ICON_APPLE);
  293. $sImageUrlApl = $oTranscoder->getFileUrl($iId);
  294. }
  295. if(empty($sImageUrlFav))
  296. $sImageUrlFav = $this->_oTemplate->getIconUrl('favicon.png');
  297. if(empty($sImageUrlFcb))
  298. $sImageUrlFcb = $this->_oTemplate->getIconUrl('facebook-icon.png');
  299. if(empty($sImageUrlApl))
  300. $sImageUrlApl = $this->_oTemplate->getIconUrl('apple-touch-icon.png');
  301. $sRet = '';
  302. $sRet .= '<link rel="shortcut icon" type="image/x-icon" href="' . $sImageUrlFav . '" />';
  303. $sRet .= '<link rel="image_src" sizes="100x100" href="' . $sImageUrlFcb . '" />';
  304. $sRet .= '<link rel="apple-touch-icon" sizes="152x152" href="' . $sImageUrlApl . '" />';
  305. return $sRet;
  306. }
  307. /**
  308. * Output time wrapped in <time> tag in HTML.
  309. * Then time is autoformatted using JS upon page load, this is aumatically converted to user's timezone and
  310. * updated in realtime in case of short periods of 'from now' time format.
  311. *
  312. * Short version of this function:
  313. * @see bx_time_js
  314. *
  315. * @param $iUnixTimestamp time as unixtimestamp
  316. * @param $sFormatIdentifier output format identifier
  317. * @see BX_FORMAT_DATE
  318. * @see BX_FORMAT_TIME
  319. * @see BX_FORMAT_DATE_TIME
  320. * @param $bForceFormat force provided format and don't use "from now" time autoformat.
  321. */
  322. function timeForJs ($iUnixTimestamp, $sFormatIdentifier = BX_FORMAT_DATE, $bForceFormat = false)
  323. {
  324. $sDateUTC = bx_time_utc ($iUnixTimestamp);
  325. return '<time datetime="' . $sDateUTC . '" data-bx-format="' . getParam($sFormatIdentifier) . '" data-bx-autoformat="' . ($bForceFormat ? 0 : getParam('sys_format_timeago')) . '">' . $sDateUTC . '</time>';
  326. }
  327. function statusOnOff ($mixed, $isMsg = false)
  328. {
  329. if ((is_bool($mixed) && !$mixed) || (is_string($mixed) && 'fail' == $mixed))
  330. return '<i class="sys-icon circle col-red2"></i> ' . ($isMsg ? _t('_sys_off') : '');
  331. elseif (is_string($mixed) && 'warn' == $mixed)
  332. return '<i class="sys-icon circle col-red3"></i> ' . ($isMsg ? _t('_sys_warn') : '');
  333. elseif (is_string($mixed) && 'undef' == $mixed)
  334. return '<i class="sys-icon circle col-gray"></i> ' . ($isMsg ? _t('_undefined') : '');
  335. else
  336. return '<i class="sys-icon circle col-green1"></i> ' . ($isMsg ? _t('_sys_on') : '');
  337. }
  338. /**
  339. * Ouputs HTML5 video player.
  340. * @param $sUrlPoster video poster image
  341. * @param $sUrlMP4 .mp4 video
  342. * @param $sUrlWebM .webm video
  343. * @param $aAttrs custom attributes, defaults are: controls="" preload="none" autobuffer=""
  344. * @param $sStyles custom styles, defaults are: width:100%; height:auto;
  345. */
  346. function videoPlayer ($sUrlPoster, $sUrlMP4, $sUrlWebM = '', $aAttrs = false, $sStyles = 'width:100%; height:auto;')
  347. {
  348. $aAttrsDefaults = array(
  349. 'controls' => '',
  350. 'preload' => 'none',
  351. 'autobuffer' => '',
  352. );
  353. $aAttrs = array_merge($aAttrsDefaults, is_array($aAttrs) ? $aAttrs : array());
  354. $sAttrs = bx_convert_array2attrs($aAttrs, '', $sStyles);
  355. return '<video ' . $sAttrs . ' poster="' . $sUrlPoster . '">
  356. ' . ($sUrlWebM ? '<source type="video/webm; codecs="vp8, vorbis" src="' . $sUrlWebM . '" />' : '') . '
  357. ' . ($sUrlMP4 ? '<source type="video/mp4" src="' . $sUrlMP4 . '" />' : '') . '
  358. </video>';
  359. }
  360. }
  361. /** @} */