BxBaseFunctions.php 16 KB

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