BxBaseFunctions.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. class BxBaseFunctions extends BxDol 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. $iId = !empty($sName) ? $sName : time();
  87. return
  88. ($isPlaceInCenter ? '<div class="login_ajax_wrap">' : '') .
  89. $this->_oTemplate->parseHtmlByName('popup_trans.html', array(
  90. 'id' => $iId,
  91. 'wrapper_style' => $isHiddenByDefault ? 'display:none;' : '',
  92. 'content' => $sContent
  93. )) .
  94. ($isPlaceInCenter ? '</div>' : '');
  95. }
  96. function slideBox($sName, $sContent, $isHiddenByDefault = false)
  97. {
  98. $iId = !empty($sName) ? $sName : time();
  99. return $this->_oTemplate->parseHtmlByName('popup_slide.html', array(
  100. 'id' => $iId,
  101. 'wrapper_style' => $isHiddenByDefault ? 'display:none;' : '',
  102. 'content' => $sContent
  103. ));
  104. }
  105. function getTemplateIcon($sName)
  106. {
  107. $sUrl = $this->_oTemplate->getIconUrl($sName);
  108. return !empty($sUrl) ? $sUrl : $this->_oTemplate->getIconUrl('spacer.gif');
  109. }
  110. function getTemplateImage($sName)
  111. {
  112. $sUrl = $this->_oTemplate->getImageUrl($sName);
  113. return !empty($sUrl) ? $sUrl : $this->_oTemplate->getImageUrl('spacer.gif');
  114. }
  115. function sysIcon ($sIcon, $sName, $sUrl = '', $iWidth = 0)
  116. {
  117. return '<div class="sys_icon">' . ($sUrl ? '<a title="'.$sName.'" href="'.$sUrl.'">' : '') . '<img alt="'.$sName.'" src="'.$sIcon.'" '.($iWidth ? 'width='.$iWidth : '').' />' . ($sUrl ? '</a>' : '') . '</div>';
  118. }
  119. /**
  120. * functions for limiting maximal string length
  121. */
  122. function getStringWithLimitedLength($sString, $iWidth = 45, $isPopupOnOverflow = false, $bReturnString = true)
  123. {
  124. if (empty($sString) || mb_strlen($sString, 'UTF-8') <= $iWidth)
  125. return $bReturnString ? $sString : array($sString);
  126. $sResult = '';
  127. $aWords = mb_split("[\s\r\n]", $sString);
  128. $iPosition = 0;
  129. $iWidthReal = $iWidth - 3;
  130. $iWidthMin = $iWidth - 15;
  131. foreach($aWords as $sWord) {
  132. $sWord = trim($sWord);
  133. $iWord = mb_strlen($sWord, 'UTF-8');
  134. if ($iPosition + $iWord > $iWidthReal)
  135. break;
  136. // add word and continue
  137. $sResult .= ' ' . $sWord;
  138. $iPosition += 1 + $iWord;
  139. }
  140. // last word is too long, cut it
  141. if(!$iPosition || $iPosition < $iWidthMin)
  142. $sResult .= ' ' . mb_substr($sWord, 0, $iWidthReal - $iPosition - $iWord, 'UTF-8');
  143. $sResult = trim($sResult);
  144. // add tripple dot
  145. if(!$isPopupOnOverflow) {
  146. $sResult .= '...';
  147. return $bReturnString ? $sResult : array($sResult);
  148. }
  149. // add button width popup
  150. $sId = 'bx-str-limit-' . rand(1, PHP_INT_MAX);
  151. $sPopup = '<img class="bx-str-limit" onclick="$(\'#' . $sId . '\').dolPopup({pointer:{el:$(this), offset:\'10 1\'}})" src="' . $this->getTemplateImage('str-limit.png') . '"/>';
  152. $sPopup .= '<div id="' . $sId . '" style="display:none;">' . BxTemplFunctions::getInstance()->transBox('', '<div class="bx-def-padding bx-def-color-bg-block">'.$sString.'</div>') . '</div>';
  153. return $bReturnString ? $sResult . $sPopup : array($sResult, $sPopup);
  154. }
  155. /**
  156. * Display design box with specified title, template, content and menu.
  157. * @param $sTitle - design box title, please note that some templates don't use title.
  158. * @param $sContent - design box content.
  159. * @param $iTemplateNum - number of design box template, use predefined contants only, default is BX_DB_DEF.
  160. * @param $mixedMenu - design box menu, it can be:
  161. * - object: instance of BxTemplMenu class
  162. * - string: menu object identifier
  163. * - array: array of menu links to create menu from
  164. * @return string
  165. *
  166. * @see BX_DB_CONTENT_ONLY
  167. * @see BX_DB_DEF
  168. * @see BX_DB_EMPTY
  169. * @see BX_DB_NO_CAPTION
  170. * @see BX_DB_PADDING_CONTENT_ONLY
  171. * @see BX_DB_PADDING_DEF
  172. * @see BX_DB_PADDING_NO_CAPTION
  173. */
  174. function designBoxContent ($sTitle, $sContent, $iTemplateNum = BX_DB_DEF, $mixedMenu = false)
  175. {
  176. return $this->_oTemplate->parseHtmlByName('designbox_' . (int)$iTemplateNum . '.html', array(
  177. 'title' => $sTitle,
  178. 'designbox_content' => $sContent,
  179. 'caption_item' => $this->designBoxMenu ($mixedMenu, array (array('menu' => 1))),
  180. ));
  181. }
  182. function designBoxMenu ($mixedMenu, $aButtons = array ())
  183. {
  184. $sCode = '';
  185. $aButtonMenu = false;
  186. if ($mixedMenu) {
  187. $sMenu = '';
  188. if (is_string($mixedMenu)) {
  189. $oMenu = BxTemplMenu::getObjectInstance($mixedMenu);
  190. $sMenu = $oMenu ? $oMenu->getCode () : $mixedMenu;
  191. } elseif (is_object($mixedMenu) && is_a($mixedMenu, 'BxTemplMenu')) {
  192. $sMenu = $mixedMenu->getCode();
  193. } elseif (is_array($mixedMenu)) {
  194. if (isset($mixedMenu['template']) && isset($mixedMenu['menu_items']))
  195. $aMenu = $mixedMenu;
  196. else
  197. $aMenu = array ('template' => 'menu_vertical.html', 'menu_items' => $mixedMenu);
  198. $oMenu = new BxTemplMenu($aMenu, $this->_oTemplate);
  199. $sMenu = $oMenu->getCode ();
  200. }
  201. if ($sMenu) {
  202. $sId = 'bx-menu-db-' . time() . rand(0, PHP_INT_MAX);
  203. $sCode .= $this->slideBox($sId, '<div class="bx-def-padding">' . $sMenu . '</div>', true);
  204. $aButtonMenu = array ('icon-a' => $this->_sDesignBoxIcon, 'onclick' => "bx_menu_slide('#" . $sId . "', this)");
  205. }
  206. }
  207. if ($aButtons) {
  208. $sCode .= '<div class="bx-db-menu"><div class="bx-db-menu-tab bx-db-menu-tab-btn">';
  209. foreach ($aButtons as $aButton) {
  210. if (isset($aButton['menu']) && $aButton['menu']) {
  211. if (!$aButtonMenu)
  212. continue;
  213. $aButton = $aButtonMenu;
  214. }
  215. $aAttrs = array ();
  216. if (!empty($aButton['onclick']))
  217. $aAttrs['onclick'] = $aButton['onclick'];
  218. if (!empty($aButton['class']))
  219. $aAttrs['class'] = $aButton['class'];
  220. $sAttrs = bx_convert_array2attrs ($aAttrs, 'bx-def-margin-sec-left');
  221. $sCode .= '<a href="javascript:void(0);" ' . $sAttrs . '>';
  222. $sCode .= !empty($aButton['icon']) ? '<i class="sys-icon ' . $aButton['icon'] . ' bx-def-font-h2"></i>' : '';
  223. $sCode .= !empty($aButton['icon-a']) ? '<i class="sys-icon-a" data-rotate="down" data-icon="' . $aButton['icon-a'] . '"></i>' : '';
  224. $sCode .= !empty($aButton['title']) ? $aButton['title'] : '';
  225. $sCode .= '</a>';
  226. }
  227. $sCode .= '</div></div>';
  228. }
  229. return $sCode;
  230. }
  231. /**
  232. * Get logo URL.
  233. * @return string
  234. */
  235. function getMainLogoUrl()
  236. {
  237. $iFileId = (int)getParam('sys_site_logo');
  238. if (!$iFileId)
  239. return false;
  240. $oStorage = BxDolTranscoder::getObjectInstance('sys_custom_images');
  241. $sFileUrl = $oStorage->getFileUrl($iFileId);
  242. if (!$sFileUrl)
  243. return false;
  244. if (getParam('sys_site_logo_width') > 0)
  245. $sFileUrl = bx_append_url_params($sFileUrl, array('x' => getParam('sys_site_logo_width')));
  246. if (getParam('sys_site_logo_height') > 0)
  247. $sFileUrl = bx_append_url_params($sFileUrl, array('y' => getParam('sys_site_logo_height')));
  248. return $sFileUrl;
  249. }
  250. /**
  251. * Get logo HTML.
  252. * @return string
  253. */
  254. function getMainLogo()
  255. {
  256. $sAlt = getParam('sys_site_logo_alt') ? getParam('sys_site_logo_alt') : getParam('site_title');
  257. $sLogo = $sAlt;
  258. if ($sFileUrl = $this->getMainLogoUrl()) {
  259. $sMaxWidth = getParam('sys_site_logo_width') > 0 ? 'max-width:' . round(getParam('sys_site_logo_width')/16, 3) . 'rem;' : '';
  260. $sMaxHeight = getParam('sys_site_logo_height') > 0 ? 'max-height:' . round(getParam('sys_site_logo_height')/16, 3) . 'rem;' : '';
  261. $sLogo = '<img style="' . $sMaxWidth . $sMaxHeight . '" src="' . $sFileUrl . '" id="bx-logo" alt="' . bx_html_attribute($sAlt, BX_ESCAPE_STR_QUOTE) . '" />';
  262. }
  263. return '<a class="bx-def-font-contrasted" href="' . BX_DOL_URL_ROOT . '" title="' . bx_html_attribute($sAlt, BX_ESCAPE_STR_QUOTE) . '">' . $sLogo . '</a>';
  264. }
  265. /**
  266. * Get HTML code for meta icons.
  267. * @return HTML string to insert into HEAD section
  268. */
  269. function getMetaIcons()
  270. {
  271. $iId = (int)getParam('sys_site_icon');
  272. $sImageUrlFav = $sImageUrlFcb = $sImageUrlApl = '';
  273. if(!empty($iId)) {
  274. // favicon icon
  275. $oTranscoder = BxDolTranscoderImage::getObjectInstance(BX_DOL_TRANSCODER_OBJ_ICON_FAVICON);
  276. $sImageUrlFav = $oTranscoder->getFileUrl($iId);
  277. // facebook icon
  278. $oTranscoder = BxDolTranscoderImage::getObjectInstance(BX_DOL_TRANSCODER_OBJ_ICON_FACEBOOK);
  279. $sImageUrlFcb = $oTranscoder->getFileUrl($iId);
  280. // apple touch icon
  281. $oTranscoder = BxDolTranscoderImage::getObjectInstance(BX_DOL_TRANSCODER_OBJ_ICON_APPLE);
  282. $sImageUrlApl = $oTranscoder->getFileUrl($iId);
  283. }
  284. if(empty($sImageUrlFav))
  285. $sImageUrlFav = $this->_oTemplate->getIconUrl('favicon.png');
  286. if(empty($sImageUrlFcb))
  287. $sImageUrlFcb = $this->_oTemplate->getIconUrl('facebook-icon.png');
  288. if(empty($sImageUrlApl))
  289. $sImageUrlApl = $this->_oTemplate->getIconUrl('apple-touch-icon.png');
  290. $sRet = '';
  291. $sRet .= '<link rel="icon" sizes="16x16" type="image/png" href="' . $sImageUrlFav . '" />';
  292. $sRet .= '<link rel="image_src" sizes="100x100" href="' . $sImageUrlFcb . '" />';
  293. $sRet .= '<link rel="apple-touch-icon" sizes="152x152" href="' . $sImageUrlApl . '" />';
  294. return $sRet;
  295. }
  296. /**
  297. * Output time wrapped in <time> tag in HTML.
  298. * Then time is autoformatted using JS upon page load, this is aumatically converted to user's timezone and
  299. * updated in realtime in case of short periods of 'from now' time format.
  300. *
  301. * Short version of this function:
  302. * @see bx_time_js
  303. *
  304. * @param $iUnixTimestamp time as unixtimestamp
  305. * @param $sFormatIdentifier output format identifier
  306. * @see BX_FORMAT_DATE
  307. * @see BX_FORMAT_TIME
  308. * @see BX_FORMAT_DATE_TIME
  309. * @param $bForceFormat force provided format and don't use "from now" time autoformat.
  310. */
  311. function timeForJs ($iUnixTimestamp, $sFormatIdentifier = BX_FORMAT_DATE, $bForceFormat = false)
  312. {
  313. $sDateUTC = bx_time_utc ($iUnixTimestamp);
  314. return '<time datetime="' . $sDateUTC . '" data-bx-format="' . getParam($sFormatIdentifier) . '" data-bx-autoformat="' . ($bForceFormat ? 0 : getParam('sys_format_timeago')) . '">' . $sDateUTC . '</time>';
  315. }
  316. function statusOnOff ($mixed, $isMsg = false)
  317. {
  318. if ((is_bool($mixed) && !$mixed) || (is_string($mixed) && 'fail' == $mixed))
  319. return '<i class="sys-icon circle col-red2"></i> ' . ($isMsg ? _t('_sys_off') : '');
  320. elseif (is_string($mixed) && 'warn' == $mixed)
  321. return '<i class="sys-icon circle col-red3"></i> ' . ($isMsg ? _t('_sys_warn') : '');
  322. elseif (is_string($mixed) && 'undef' == $mixed)
  323. return '<i class="sys-icon circle col-gray"></i> ' . ($isMsg ? _t('_undefined') : '');
  324. else
  325. return '<i class="sys-icon circle col-green1"></i> ' . ($isMsg ? _t('_sys_on') : '');
  326. }
  327. /**
  328. * Ouputs HTML5 video player.
  329. * @param $sUrlPoster video poster image
  330. * @param $sUrlMP4 .mp4 video
  331. * @param $sUrlWebM .webm video
  332. * @param $aAttrs custom attributes, defaults are: controls="" preload="none" autobuffer=""
  333. * @param $sStyles custom styles, defaults are: width:100%; height:auto;
  334. */
  335. function videoPlayer ($sUrlPoster, $sUrlMP4, $sUrlWebM = '', $aAttrs = false, $sStyles = 'width:100%; height:auto;')
  336. {
  337. $aAttrsDefaults = array(
  338. 'controls' => '',
  339. 'preload' => 'none',
  340. 'autobuffer' => '',
  341. );
  342. $aAttrs = array_merge($aAttrsDefaults, is_array($aAttrs) ? $aAttrs : array());
  343. $sAttrs = bx_convert_array2attrs($aAttrs, '', $sStyles);
  344. return '<video ' . $sAttrs . ' poster="' . $sUrlPoster . '">
  345. ' . ($sUrlWebM ? '<source type="video/webm; codecs="vp8, vorbis" src="' . $sUrlWebM . '" />' : '') . '
  346. ' . ($sUrlMP4 ? '<source type="video/mp4" src="' . $sUrlMP4 . '" />' : '') . '
  347. </video>';
  348. }
  349. }
  350. /** @} */