BxBaseModTextTemplate.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 BaseText Base classes for text modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Module representation.
  13. */
  14. class BxBaseModTextTemplate extends BxBaseModGeneralTemplate
  15. {
  16. function __construct(&$oConfig, &$oDb)
  17. {
  18. parent::__construct($oConfig, $oDb);
  19. }
  20. function unit ($aData, $isCheckPrivateContent = true, $sTemplateName = 'unit.html', $aParams = array())
  21. {
  22. $sResult = $this->checkPrivacy ($aData, $isCheckPrivateContent, $this->getModule(), $sTemplateName);
  23. if($sResult)
  24. return $sResult;
  25. return $this->parseHtmlByName($sTemplateName, $this->getUnit($aData, $aParams));
  26. }
  27. function getUnitMetaItemLink($sContent, $aAttrs = array())
  28. {
  29. return $this->getUnitMetaItem('a', $sContent, $aAttrs);
  30. }
  31. function getUnitMetaItemText($sContent, $aAttrs = array())
  32. {
  33. if(!is_array($aAttrs))
  34. $aAttrs = array();
  35. $aAttrs['class'] = (!empty($aAttrs['class']) ? $aAttrs['class'] . ' ' : '') . 'bx-def-font-grayed';
  36. return $this->getUnitMetaItem('span', $sContent, $aAttrs);
  37. }
  38. function getUnitMetaItemCustom($sContent)
  39. {
  40. return $this->getUnitMetaItem('custom', $sContent);
  41. }
  42. function entryAuthor ($aData, $iProfileId = false, $sFuncAuthorDesc = 'getAuthorDesc', $sTemplateName = 'author.html', $sFuncAuthorAddon = 'getAuthorAddon')
  43. {
  44. $CNF = &$this->getModule()->_oConfig->CNF;
  45. if (!$iProfileId)
  46. $iProfileId = $aData[$CNF['FIELD_AUTHOR']];
  47. $oProfile = BxDolProfile::getInstance($iProfileId);
  48. if (!$oProfile)
  49. $oProfile = BxDolProfileUndefined::getInstance();
  50. if (!$oProfile)
  51. return '';
  52. $sName = $oProfile->getDisplayName();
  53. $sAddon = $sFuncAuthorAddon ? $this->$sFuncAuthorAddon($aData, $oProfile) : '';
  54. $aVars = array (
  55. 'author_url' => $oProfile->getUrl(),
  56. 'author_thumb_url' => $oProfile->getThumb(),
  57. 'author_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  58. 'author_title' => $sName,
  59. 'author_title_attr' => bx_html_attribute($sName),
  60. 'author_desc' => $sFuncAuthorDesc ? $this->$sFuncAuthorDesc($aData) : '',
  61. 'bx_if:addon' => array (
  62. 'condition' => (bool)$sAddon,
  63. 'content' => array (
  64. 'content' => $sAddon,
  65. ),
  66. ),
  67. );
  68. return $this->parseHtmlByName($sTemplateName, $aVars);
  69. }
  70. function getAuthorDesc ($aData)
  71. {
  72. return '';
  73. }
  74. function getAuthorAddon ($aData, $oProfile)
  75. {
  76. $CNF = &$this->getModule()->_oConfig->CNF;
  77. $sUrl = 'page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . $oProfile->id();
  78. $sUrl = BxDolPermalinks::getInstance()->permalink($sUrl);
  79. return _t($CNF['T']['txt_all_entries_by'], $sUrl, $oProfile->getDisplayName(), $this->getModule()->_oDb->getEntriesNumByAuthor($oProfile->id()));
  80. }
  81. public function entryBreadcrumb($aContentInfo, $aTmplVarsItems = array())
  82. {
  83. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  84. return $this->parseHtmlByName('breadcrumb.html', array(
  85. 'url_home' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']),
  86. 'icon_home' => $CNF['ICON'],
  87. 'bx_repeat:items' => $aTmplVarsItems
  88. ));
  89. }
  90. function entryAttachments ($aData, $aParams = array())
  91. {
  92. return $this->entryAttachmentsByStorage($this->getModule()->_oConfig->CNF['OBJECT_STORAGE'], $aData, $aParams);
  93. }
  94. function entryAttachmentsByStorage ($sStorage, $aData, $aParams = array())
  95. {
  96. if (!($a = $this->getAttachments($sStorage, $aData, $aParams)))
  97. return '';
  98. return $this->parseHtmlByName('attachments.html', array(
  99. 'bx_repeat:attachments' => $a,
  100. ));
  101. }
  102. protected function checkPrivacy ($aData, $isCheckPrivateContent, $oModule, $sTemplateName = '')
  103. {
  104. if ($isCheckPrivateContent && CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $oModule->checkAllowedView($aData))) {
  105. $aVars = array (
  106. 'summary' => $sMsg,
  107. );
  108. return $this->parseHtmlByName($sTemplateName ? str_replace('.html', '_private.html', $sTemplateName) : 'unit_private.html', $aVars);
  109. }
  110. return '';
  111. }
  112. protected function getUnitThumbAndGallery ($aData)
  113. {
  114. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  115. $sPhotoThumb = '';
  116. $sPhotoGallery = '';
  117. if(!empty($CNF['FIELD_THUMB']) && !empty($aData[$CNF['FIELD_THUMB']])) {
  118. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  119. if ($oImagesTranscoder)
  120. $sPhotoThumb = $oImagesTranscoder->getFileUrl($aData[$CNF['FIELD_THUMB']]);
  121. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  122. if ($oImagesTranscoder)
  123. $sPhotoGallery = $oImagesTranscoder->getFileUrl($aData[$CNF['FIELD_THUMB']]);
  124. else
  125. $sPhotoGallery = $sPhotoThumb;
  126. }
  127. return array($sPhotoThumb, $sPhotoGallery);
  128. }
  129. protected function getUnit ($aData, $aParams = array())
  130. {
  131. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  132. // get thumb url
  133. list($sPhotoThumb, $sPhotoGallery) = $this->getUnitThumbAndGallery($aData);
  134. // get entry url
  135. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aData[$CNF['FIELD_ID']]);
  136. $oProfile = BxDolProfile::getInstance($aData[$CNF['FIELD_AUTHOR']]);
  137. if (!$oProfile)
  138. $oProfile = BxDolProfileUndefined::getInstance();
  139. $sTitle = $this->getTitle($aData);
  140. $sText = $this->getText($aData);
  141. $sSummary = $this->getSummary($aData, $sTitle, $sText, $sUrl);
  142. $sSummaryPlain = isset($CNF['PARAM_CHARS_SUMMARY_PLAIN']) && $CNF['PARAM_CHARS_SUMMARY_PLAIN'] ? BxTemplFunctions::getInstance()->getStringWithLimitedLength(strip_tags($sSummary), (int)getParam($CNF['PARAM_CHARS_SUMMARY_PLAIN'])) : '';
  143. if(!empty($CNF['OBJECT_METATAGS'])) {
  144. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  145. // keywords
  146. if ($oMetatags->keywordsIsEnabled())
  147. $sText = $oMetatags->keywordsParse($aData[$CNF['FIELD_ID']], $sText);
  148. }
  149. $sTitle = bx_process_output($sTitle);
  150. $aTmplVarsMeta = array();
  151. if(!empty($CNF['OBJECT_MENU_SNIPPET_META'])) {
  152. $oMenuMeta = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SNIPPET_META'], $this);
  153. if($oMenuMeta) {
  154. $oMenuMeta->setContentId($aData[$CNF['FIELD_ID']]);
  155. $aTmplVarsMeta = array(
  156. 'meta' => $oMenuMeta->getCode()
  157. );
  158. }
  159. }
  160. // generate html
  161. return array (
  162. 'id' => $aData[$CNF['FIELD_ID']],
  163. 'content_url' => $sUrl,
  164. 'title' => $sTitle,
  165. 'title_attr' => bx_html_attribute($sTitle),
  166. 'summary' => $sSummary,
  167. 'text' => $sText,
  168. 'author' => $oProfile->getDisplayName(),
  169. 'author_url' => $oProfile->getUrl(),
  170. 'author_icon' => $oProfile->getIcon(),
  171. 'author_thumb' => $oProfile->getThumb(),
  172. 'author_avatar' => $oProfile->getAvatar(),
  173. 'entry_posting_date' => bx_time_js($aData[$CNF['FIELD_ADDED']], BX_FORMAT_DATE),
  174. 'module_name' => _t($CNF['T']['txt_sample_single']),
  175. 'ts' => $aData[$CNF['FIELD_ADDED']],
  176. 'bx_if:meta' => array(
  177. 'condition' => !empty($aTmplVarsMeta),
  178. 'content' => $aTmplVarsMeta
  179. ),
  180. 'bx_if:thumb' => array (
  181. 'condition' => $sPhotoThumb,
  182. 'content' => array (
  183. 'title' => $sTitle,
  184. 'summary_attr' => bx_html_attribute($sSummaryPlain),
  185. 'content_url' => $sUrl,
  186. 'thumb_url' => $sPhotoThumb ? $sPhotoThumb : '',
  187. 'gallery_url' => $sPhotoGallery ? $sPhotoGallery : '',
  188. 'strecher' => str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ', 40),
  189. ),
  190. ),
  191. 'bx_if:no_thumb' => array (
  192. 'condition' => !$sPhotoThumb,
  193. 'content' => array (
  194. 'content_url' => $sUrl,
  195. 'summary_plain' => $sSummaryPlain,
  196. 'strecher' => mb_strlen($sSummaryPlain) > 240 ? '' : str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ', round((240 - mb_strlen($sSummaryPlain)) / 6)),
  197. ),
  198. ),
  199. );
  200. }
  201. protected function getUnitMetaItem($sName, $sContent, $aAttrs = array(), $sTemplate = 'unit_meta_item.html')
  202. {
  203. $aTags = array('span', 'a', 'custom');
  204. $aTmplVarsAttrs = array();
  205. foreach($aAttrs as $sKey => $sValue)
  206. $aTmplVarsAttrs[] = array('key' => $sKey, 'value' => bx_html_attribute($sValue));
  207. $aTmplVars = array();
  208. foreach($aTags as $sTag) {
  209. $aTmplVarsTag = array();
  210. $bTmplVarsTag = $sTag == $sName;
  211. if($bTmplVarsTag)
  212. $aTmplVarsTag = array(
  213. 'content' => $sContent,
  214. 'bx_repeat:attrs' => $aTmplVarsAttrs
  215. );
  216. $aTmplVars['bx_if:' . $sTag] = array(
  217. 'condition' => $bTmplVarsTag,
  218. 'content' => $aTmplVarsTag
  219. );
  220. }
  221. return $this->parseHtmlByName($sTemplate, $aTmplVars);
  222. }
  223. protected function getTitle($aData)
  224. {
  225. $CNF = &$this->getModule()->_oConfig->CNF;
  226. return isset($aData[$CNF['FIELD_TITLE']]) ? $aData[$CNF['FIELD_TITLE']] : '';
  227. }
  228. protected function getText($aData)
  229. {
  230. $CNF = &$this->getModule()->_oConfig->CNF;
  231. return isset($aData[$CNF['FIELD_TEXT']]) ? $aData[$CNF['FIELD_TEXT']] : '';
  232. }
  233. protected function getSummary($aData, $sTitle = '', $sText = '', $sUrl = '')
  234. {
  235. $CNF = &$this->getModule()->_oConfig->CNF;
  236. if(empty($CNF['PARAM_CHARS_SUMMARY']))
  237. return '';
  238. // get summary
  239. $sLinkMore = ' <a title="' . bx_html_attribute(_t('_sys_read_more', $sTitle)) . '" href="' . $sUrl . '"><i class="sys-icon ellipsis-h"></i></a>';
  240. return strmaxtextlen($sText, (int)getParam($CNF['PARAM_CHARS_SUMMARY']), $sLinkMore);
  241. }
  242. protected function getAttachments ($sStorage, $aData, $aParams = array())
  243. {
  244. $CNF = &$this->getModule()->_oConfig->CNF;
  245. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  246. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  247. $aTranscodersVideo = false;
  248. if ($CNF['OBJECT_VIDEOS_TRANSCODERS'])
  249. $aTranscodersVideo = array (
  250. 'poster' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']),
  251. 'mp4' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']),
  252. 'webm' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['webm']),
  253. );
  254. $aGhostFiles = $oStorage->getGhosts ($aData[$CNF['FIELD_AUTHOR']], $aData[$CNF['FIELD_ID']]);
  255. if (!$aGhostFiles)
  256. return false;
  257. $sFilterField = isset($aParams['filter_field']) ? $aParams['filter_field'] : $CNF['FIELD_THUMB'];
  258. if(!empty($sFilterField) && isset($aData[$sFilterField]))
  259. foreach ($aGhostFiles as $k => $a) {
  260. // don't show thumbnail in attachments
  261. if ($a['id'] == $aData[$sFilterField])
  262. unset($aGhostFiles[$k]);
  263. }
  264. if(!$aGhostFiles)
  265. return false;
  266. $aAttachmnts = array();
  267. foreach ($aGhostFiles as $k => $a) {
  268. $isImage = $oTranscoder && (0 === strncmp('image/', $a['mime_type'], 6)); // preview for images, transcoder object for preview must be defined
  269. $isVideo = $aTranscodersVideo && (0 === strncmp('video/', $a['mime_type'], 6)); // preview for videos, transcoder object for video must be defined
  270. $sUrlOriginal = $oStorage->getFileUrlById($a['id']);
  271. $sImgPopupId = 'bx-messages-atachment-popup-' . $a['id'];
  272. // images are displayed with preview and popup upon clicking
  273. $a['bx_if:image'] = array (
  274. 'condition' => $isImage,
  275. 'content' => array (
  276. 'url_original' => $sUrlOriginal,
  277. 'attr_file_name' => bx_html_attribute($a['file_name']),
  278. 'popup_id' => $sImgPopupId,
  279. 'url_preview' => $isImage ? $oTranscoder->getFileUrl($a['id']) : '',
  280. 'popup' => BxTemplFunctions::getInstance()->transBox($sImgPopupId, '<img src="' . $sUrlOriginal . '" />', true, true),
  281. ),
  282. );
  283. // videos are displayed inline
  284. $a['bx_if:video'] = array (
  285. 'condition' => $isVideo,
  286. 'content' => array (
  287. 'video' => $isVideo && $aTranscodersVideo ? BxTemplFunctions::getInstance()->videoPlayer(
  288. $aTranscodersVideo['poster']->getFileUrl($a['id']),
  289. $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  290. $aTranscodersVideo['webm']->getFileUrl($a['id']),
  291. false, ''
  292. ) : '',
  293. ),
  294. );
  295. // non-images are displayed as text links to original file
  296. $a['bx_if:not_image'] = array (
  297. 'condition' => !$isImage && !$isVideo,
  298. 'content' => array (
  299. 'url_original' => $sUrlOriginal,
  300. 'attr_file_name' => bx_html_attribute($a['file_name']),
  301. 'file_name' => bx_process_output($a['file_name']),
  302. ),
  303. );
  304. $aAttachmnts[] = $a;
  305. }
  306. return $aAttachmnts;
  307. }
  308. }
  309. /** @} */