BxBaseFunctions.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolTemplate');
  10. class BxBaseFunctions extends BxDol implements iBxDolSingleton
  11. {
  12. protected $_oTemplate;
  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 getTemplateIcon($sName)
  97. {
  98. $sUrl = $this->_oTemplate->getIconUrl($sName);
  99. return !empty($sUrl) ? $sUrl : $this->_oTemplate->getIconUrl('spacer.gif');
  100. }
  101. function getTemplateImage($sName)
  102. {
  103. $sUrl = $this->_oTemplate->getImageUrl($sName);
  104. return !empty($sUrl) ? $sUrl : $this->_oTemplate->getImageUrl('spacer.gif');
  105. }
  106. function sysIcon ($sIcon, $sName, $sUrl = '', $iWidth = 0)
  107. {
  108. return '<div class="sys_icon">' . ($sUrl ? '<a title="'.$sName.'" href="'.$sUrl.'">' : '') . '<img alt="'.$sName.'" src="'.$sIcon.'" '.($iWidth ? 'width='.$iWidth : '').' />' . ($sUrl ? '</a>' : '') . '</div>';
  109. }
  110. /**
  111. * Outputs holder html for dynamically loaded RSS.
  112. * It automatically adds necessary js, css files and make injection into HTML HEAD section.
  113. * @param $mixedRssId - system rss name, or current block id (if inserted into builder page)
  114. * @param $iRssNum - numbr of rss items to disolay
  115. * @param $iMemberId - optional member id
  116. */
  117. function getRssHolder ($mixedRssId, $iRssNum, $iMemberId = 0)
  118. {
  119. if (!isset($GLOBALS['gbBxSysIsRssInitialized']) || !$GLOBALS['gbBxSysIsRssInitialized']) {
  120. $this->_oTemplate->addCss(array(
  121. 'rss.css',
  122. ));
  123. $this->_oTemplate->addJs(array(
  124. 'jquery.dolRSSFeed.js',
  125. 'jquery.jfeed.js'
  126. ));
  127. $this->_oTemplate->addInjection ('injection_head', 'text', '
  128. <script type="text/javascript" language="javascript">
  129. $(document).ready( function() {
  130. $("div.RSSAggrCont").dolRSSFeed();
  131. });
  132. </script>');
  133. $GLOBALS['gbBxSysIsRssInitialized'] = true;
  134. }
  135. return $this->_oTemplate->parseHtmlByName('rss_holder.html', array (
  136. 'rss_id' => $mixedRssId,
  137. 'rss_num' => $iRssNum,
  138. 'member_id' => $iMemberId,
  139. ));
  140. }
  141. /**
  142. * TODO: remake for new system
  143. *
  144. * Function will generate list of installed languages list;
  145. *
  146. * @return : Html presentation data;
  147. */
  148. function getLangSwitcher()
  149. {
  150. bx_import('BxDolLanguagesQuery');
  151. $aLangs = BxDolLanguagesQuery::getInstance()->getLanguages();
  152. if(count($aLangs) < 2)
  153. return '';
  154. $sOutputCode = '';
  155. foreach( $aLangs as $sName => $sLang ) {
  156. $sFlag = $this->_oTemplate->getIconUrl('sys_fl_' . $sName . '.gif');
  157. $aTemplateKeys = array (
  158. 'bx_if:item_img' => array (
  159. 'condition' => ( $sFlag ),
  160. 'content' => array (
  161. 'item_img_src' => $sFlag,
  162. 'item_img_alt' => $sName,
  163. 'item_img_width' => 18,
  164. 'item_img_height' => 12,
  165. ),
  166. ),
  167. 'item_link' => BX_DOL_URL_ROOT . 'index.php?lang=' . $sName,
  168. 'item_onclick' => null,
  169. 'item_title' => $sLang,
  170. 'extra_info' => null,
  171. );
  172. $sOutputCode .= $this->_oTemplate->parseHtmlByName( 'member_menu_sub_item.html', $aTemplateKeys );
  173. }
  174. return $sOutputCode;
  175. }
  176. /**
  177. * functions for limiting maximal string length
  178. */
  179. function getStringWithLimitedLength($sString, $iWidth = 45, $isPopupOnOverflow = false, $bReturnString = true)
  180. {
  181. if (empty($sString) || mb_strlen($sString, 'UTF-8') <= $iWidth)
  182. return $bReturnString ? $sString : array($sString);
  183. $sResult = '';
  184. $aWords = mb_split("[\s\r\n]", $sString);
  185. $iPosition = 0;
  186. $iWidthReal = $iWidth - 3;
  187. $iWidthMin = $iWidth - 15;
  188. foreach($aWords as $sWord) {
  189. $sWord = trim($sWord);
  190. $iWord = mb_strlen($sWord, 'UTF-8');
  191. if ($iPosition + $iWord > $iWidthReal)
  192. break;
  193. // add word and continue
  194. $sResult .= ' ' . $sWord;
  195. $iPosition += 1 + $iWord;
  196. }
  197. // last word is too long, cut it
  198. if(!$iPosition || $iPosition < $iWidthMin)
  199. $sResult .= ' ' . mb_substr($sWord, 0, $iWidthReal - $iPosition - $iWord, 'UTF-8');
  200. $sResult = trim($sResult);
  201. // add tripple dot
  202. if(!$isPopupOnOverflow) {
  203. $sResult .= '...';
  204. return $bReturnString ? $sResult : array($sResult);
  205. }
  206. // add button width popup
  207. $sId = 'bx-str-limit-' . rand(1, PHP_INT_MAX);
  208. $sPopup = '<img class="bx-str-limit" onclick="$(\'#' . $sId . '\').dolPopup({pointer:{el:$(this), offset:\'10 1\'}})" src="' . $this->getTemplateImage('str-limit.png') . '"/>';
  209. $sPopup .= '<div id="' . $sId . '" style="display:none;">' . BxTemplFunctions::getInstance()->transBox('', '<div class="bx-def-padding bx-def-color-bg-block">'.$sString.'</div>') . '</div>';
  210. return $bReturnString ? $sResult . $sPopup : array($sResult, $sPopup);
  211. }
  212. /**
  213. * Display design box with specified title, template, content and menu.
  214. * @param $sTitle - design box title, please note that some templates don't use title.
  215. * @param $sContent - design box content.
  216. * @param $iTemplateNum - number of design box template, use predefined contants only, default is BX_DB_DEF.
  217. * @param $mixedMenu - design box menu, it can be:
  218. * - object: instance of BxTemplMenu class
  219. * - string: menu object identifier
  220. * - array: array of menu links to create menu from
  221. * @return string
  222. *
  223. * @see BX_DB_CONTENT_ONLY
  224. * @see BX_DB_DEF
  225. * @see BX_DB_EMPTY
  226. * @see BX_DB_NO_CAPTION
  227. * @see BX_DB_PADDING_CONTENT_ONLY
  228. * @see BX_DB_PADDING_DEF
  229. * @see BX_DB_PADDING_NO_CAPTION
  230. */
  231. function designBoxContent ($sTitle, $sContent, $iTemplateNum = BX_DB_DEF, $mixedMenu = false)
  232. {
  233. return $this->_oTemplate->parseHtmlByName('designbox_' . (int)$iTemplateNum . '.html', array(
  234. 'title' => $sTitle,
  235. 'designbox_content' => $sContent,
  236. 'caption_item' => $this->designBoxMenu ($mixedMenu, array (array('menu' => 1))),
  237. ));
  238. }
  239. function designBoxMenu ($mixedMenu, $aButtons = array ())
  240. {
  241. $sCode = '';
  242. $aButtonMenu = false;
  243. if ($mixedMenu) {
  244. $sMenu = '';
  245. if (is_string($mixedMenu)) {
  246. bx_import('BxTemplMenu');
  247. $oMenu = BxTemplMenu::getObjectInstance($mixedMenu);
  248. $sMenu = $oMenu ? $oMenu->getCode () : $mixedMenu;
  249. } elseif (is_object($mixedMenu) && is_a($mixedMenu, 'BxTemplMenu')) {
  250. $sMenu = $mixedMenu->getCode();
  251. } elseif (is_array($mixedMenu)) {
  252. if (isset($mixedMenu['template']) && isset($mixedMenu['menu_items']))
  253. $aMenu = $mixedMenu;
  254. else
  255. $aMenu = array ('template' => 'menu_vertical.html', 'menu_items' => $mixedMenu);
  256. bx_import('BxTemplMenu');
  257. $oMenu = new BxTemplMenu($aMenu, $this->_oTemplate);
  258. $sMenu = $oMenu->getCode ();
  259. }
  260. if ($sMenu) {
  261. $sId = 'bx-menu-db-' . time() . rand(0, PHP_INT_MAX);
  262. $sCode .= BxTemplFunctions::getInstance()->transBox($sId, '<div class="bx-def-padding bx-def-color-bg-block">' . $sMenu . '</div>', true);
  263. $aButtonMenu = array ('icon' => 'reorder', 'onclick' => "bx_menu_popup_inline('#" . $sId . "', this)");
  264. }
  265. }
  266. if ($aButtons) {
  267. $sCode .= '<div class="bx-db-menu"><div class="bx-db-menu-tab bx-db-menu-tab-btn">';
  268. foreach ($aButtons as $aButton) {
  269. if (isset($aButton['menu']) && $aButton['menu']) {
  270. if (!$aButtonMenu)
  271. continue;
  272. $aButton = $aButtonMenu;
  273. }
  274. $aAttrs = array ();
  275. if (!empty($aButton['onclick']))
  276. $aAttrs['onclick'] = $aButton['onclick'];
  277. if (!empty($aButton['class']))
  278. $aAttrs['class'] = $aButton['class'];
  279. $sAttrs = bx_convert_array2attrs ($aAttrs, 'bx-def-margin-sec-left');
  280. $sCode .= '<a href="javascript:void(0);" ' . $sAttrs . '>';
  281. $sCode .= !empty($aButton['icon']) ? '<i class="sys-icon ' . $aButton['icon'] . ' bx-def-font-h2"></i>' : '';
  282. $sCode .= !empty($aButton['title']) ? $aButton['title'] : '';
  283. $sCode .= '</a>';
  284. }
  285. $sCode .= '</div></div>';
  286. }
  287. return $sCode;
  288. }
  289. /**
  290. * Get logo HTML.
  291. * @return string
  292. */
  293. function getMainLogo()
  294. {
  295. bx_import('BxDolConfig');
  296. $sAlt = getParam('sys_site_logo_alt') ? getParam('sys_site_logo_alt') : getParam('site_title');
  297. $sLogo = $sAlt;
  298. $iFileId = (int)getParam('sys_site_logo');
  299. if ($iFileId) {
  300. bx_import('BxDolStorage');
  301. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  302. $sFileUrl = $oStorage->getFileUrlById($iFileId);
  303. if ($sFileUrl)
  304. $sLogo = '<img src="' . $sFileUrl . '" id="bx-logo" class="bx-def-margin-sec" alt="' . bx_html_attribute($sAlt, BX_ESCAPE_STR_QUOTE) . '" />';
  305. }
  306. return '<a class="bx-def-font-contrasted" href="' . BX_DOL_URL_ROOT . '" title="' . bx_html_attribute($sAlt, BX_ESCAPE_STR_QUOTE) . '">' . $sLogo . '</a>';
  307. }
  308. /**
  309. * Get HTML code for meta icons.
  310. * @return HTML string to insert into HEAD section
  311. */
  312. function getMetaIcons()
  313. {
  314. $iId = (int)getParam('sys_site_icon');
  315. $sImageUrlFav = $sImageUrlFcb = $sImageUrlApl = '';
  316. if(!empty($iId)) {
  317. bx_import('BxDolImageTranscoder');
  318. // favicon icon
  319. $oTranscoder = BxDolImageTranscoder::getObjectInstance(BX_DOL_TRANSCODER_OBJ_ICON_FAVICON);
  320. $sImageUrlFav = $oTranscoder->getImageUrl($iId);
  321. // facebook icon
  322. $oTranscoder = BxDolImageTranscoder::getObjectInstance(BX_DOL_TRANSCODER_OBJ_ICON_FACEBOOK);
  323. $sImageUrlFcb = $oTranscoder->getImageUrl($iId);
  324. // apple touch icon
  325. $oTranscoder = BxDolImageTranscoder::getObjectInstance(BX_DOL_TRANSCODER_OBJ_ICON_APPLE);
  326. $sImageUrlApl = $oTranscoder->getImageUrl($iId);
  327. }
  328. if(empty($sImageUrlFav))
  329. $sImageUrlFav = $this->_oTemplate->getIconUrl('favicon.png');
  330. if(empty($sImageUrlFcb))
  331. $sImageUrlFcb = $this->_oTemplate->getIconUrl('facebook-icon.png');
  332. if(empty($sImageUrlApl))
  333. $sImageUrlApl = $this->_oTemplate->getIconUrl('apple-touch-icon.png');
  334. $sRet = '';
  335. $sRet .= '<link rel="icon" sizes="16x16" type="image/png" href="' . $sImageUrlFav . '" />';
  336. $sRet .= '<link rel="image_src" sizes="100x100" href="' . $sImageUrlFcb . '" />';
  337. $sRet .= '<link rel="apple-touch-icon" sizes="152x152" href="' . $sImageUrlApl . '" />';
  338. return $sRet;
  339. }
  340. /**
  341. * Output time wrapped in <time> tag in HTML.
  342. * Then time is autoformatted using JS upon page load, this is aumatically converted to user's timezone and
  343. * updated in realtime in case of short periods of 'from now' time format.
  344. *
  345. * Short version of this function:
  346. * @see bx_time_js
  347. *
  348. * @param $iUnixTimestamp time as unixtimestamp
  349. * @param $sFormatIdentifier output format identifier
  350. * @see BX_FORMAT_DATE
  351. * @see BX_FORMAT_TIME
  352. * @see BX_FORMAT_DATE_TIME
  353. * @param $bForceFormat force provided format and don't use "from now" time autoformat.
  354. */
  355. function timeForJs ($iUnixTimestamp, $sFormatIdentifier = BX_FORMAT_DATE, $bForceFormat = false)
  356. {
  357. $sDateUTC = bx_time_utc ($iUnixTimestamp);
  358. return '<time datetime="' . $sDateUTC . '" data-bx-format="' . getParam($sFormatIdentifier) . '" data-bx-autoformat="' . ($bForceFormat ? 0 : getParam('sys_format_timeago')) . '">' . $sDateUTC . '</time>';
  359. }
  360. function statusOnOff ($mixed)
  361. {
  362. if ($mixed)
  363. return '<i class="sys-icon circle col-green1"></i> ' . _t('_sys_on');
  364. return '<i class="sys-icon circle col-red2"></i> ' . _t('_sys_off');
  365. }
  366. }
  367. /** @} */