BxBaseModTextTemplate.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 entryAuthor ($aData, $iProfileId = false, $sFuncAuthorDesc = 'getAuthorDesc', $sTemplateName = 'author.html', $sFuncAuthorAddon = 'getAuthorAddon')
  28. {
  29. $CNF = &$this->getModule()->_oConfig->CNF;
  30. if (!$iProfileId)
  31. $iProfileId = $aData[$CNF['FIELD_AUTHOR']];
  32. $oProfile = BxDolProfile::getInstance($iProfileId);
  33. if (!$oProfile)
  34. $oProfile = BxDolProfileUndefined::getInstance();
  35. if (!$oProfile)
  36. return '';
  37. $sName = $oProfile->getDisplayName();
  38. $sAddon = $sFuncAuthorAddon ? $this->$sFuncAuthorAddon($aData, $oProfile) : '';
  39. $aVars = array (
  40. 'author_url' => $oProfile->getUrl(),
  41. 'author_thumb_url' => $oProfile->getThumb(),
  42. 'author_title' => $sName,
  43. 'author_title_attr' => bx_html_attribute($sName),
  44. 'author_desc' => $sFuncAuthorDesc ? $this->$sFuncAuthorDesc($aData) : '',
  45. 'bx_if:addon' => array (
  46. 'condition' => (bool)$sAddon,
  47. 'content' => array (
  48. 'content' => $sAddon,
  49. ),
  50. ),
  51. );
  52. return $this->parseHtmlByName($sTemplateName, $aVars);
  53. }
  54. function getAuthorDesc ($aData)
  55. {
  56. return '';
  57. }
  58. function getAuthorAddon ($aData, $oProfile)
  59. {
  60. $CNF = &$this->getModule()->_oConfig->CNF;
  61. $sUrl = 'page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . $oProfile->id();
  62. $sUrl = BxDolPermalinks::getInstance()->permalink($sUrl);
  63. return _t($CNF['T']['txt_all_entries_by'], $sUrl, $oProfile->getDisplayName(), $this->getModule()->_oDb->getEntriesNumByAuthor($oProfile->id()));
  64. }
  65. public function entryBreadcrumb($aContentInfo, $aTmplVarsItems = array())
  66. {
  67. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  68. return $this->parseHtmlByName('breadcrumb.html', array(
  69. 'url_home' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']),
  70. 'icon_home' => $CNF['ICON'],
  71. 'bx_repeat:items' => $aTmplVarsItems
  72. ));
  73. }
  74. function entryAttachments ($aData)
  75. {
  76. if (!($a = $this->getAttachments($this->getModule()->_oConfig->CNF['OBJECT_STORAGE'], $aData)))
  77. return '';
  78. return $this->parseHtmlByName('attachments.html', array(
  79. 'bx_repeat:attachments' => $a,
  80. ));
  81. }
  82. function entryAttachmentsByStorage ($sStorage, $aData)
  83. {
  84. if (!($a = $this->getAttachments($sStorage, $aData)))
  85. return '';
  86. return $this->parseHtmlByName('attachments.html', array(
  87. 'bx_repeat:attachments' => $a,
  88. ));
  89. }
  90. protected function checkPrivacy ($aData, $isCheckPrivateContent, $oModule, $sTemplateName = '')
  91. {
  92. if ($isCheckPrivateContent && CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $oModule->checkAllowedView($aData))) {
  93. $aVars = array (
  94. 'summary' => $sMsg,
  95. );
  96. return $this->parseHtmlByName($sTemplateName ? str_replace('.html', '_private.html', $sTemplateName) : 'unit_private.html', $aVars);
  97. }
  98. return '';
  99. }
  100. protected function getUnitThumbAndGallery ($aData)
  101. {
  102. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  103. $sPhotoThumb = '';
  104. $sPhotoGallery = '';
  105. if(!empty($CNF['FIELD_THUMB']) && $aData[$CNF['FIELD_THUMB']]) {
  106. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  107. if ($oImagesTranscoder)
  108. $sPhotoThumb = $oImagesTranscoder->getFileUrl($aData[$CNF['FIELD_THUMB']]);
  109. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  110. if ($oImagesTranscoder)
  111. $sPhotoGallery = $oImagesTranscoder->getFileUrl($aData[$CNF['FIELD_THUMB']]);
  112. else
  113. $sPhotoGallery = $sPhotoThumb;
  114. }
  115. return array($sPhotoThumb, $sPhotoGallery);
  116. }
  117. protected function getUnit ($aData, $aParams = array())
  118. {
  119. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  120. // get thumb url
  121. list($sPhotoThumb, $sPhotoGallery) = $this->getUnitThumbAndGallery($aData);
  122. // get entry url
  123. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aData[$CNF['FIELD_ID']]);
  124. $oProfile = BxDolProfile::getInstance($aData[$CNF['FIELD_AUTHOR']]);
  125. if (!$oProfile)
  126. $oProfile = BxDolProfileUndefined::getInstance();
  127. $sTitle = $this->getTitle($aData);
  128. $sText = $this->getText($aData);
  129. $sSummary = $this->getSummary($aData, $sTitle, $sText, $sUrl);
  130. $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'])) : '';
  131. if(!empty($CNF['OBJECT_METATAGS'])) {
  132. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  133. // keywords
  134. if ($oMetatags->keywordsIsEnabled())
  135. $sText = $oMetatags->keywordsParse($aData[$CNF['FIELD_ID']], $sText);
  136. }
  137. $sTitle = bx_process_output($sTitle);
  138. // generate html
  139. return array (
  140. 'id' => $aData[$CNF['FIELD_ID']],
  141. 'content_url' => $sUrl,
  142. 'title' => $sTitle,
  143. 'title_attr' => bx_html_attribute($sTitle),
  144. 'summary' => $sSummary,
  145. 'text' => $sText,
  146. 'author' => $oProfile->getDisplayName(),
  147. 'author_url' => $oProfile->getUrl(),
  148. 'entry_posting_date' => bx_time_js($aData[$CNF['FIELD_ADDED']], BX_FORMAT_DATE),
  149. 'module_name' => _t($CNF['T']['txt_sample_single']),
  150. 'ts' => $aData[$CNF['FIELD_ADDED']],
  151. 'bx_if:thumb' => array (
  152. 'condition' => $sPhotoThumb,
  153. 'content' => array (
  154. 'title' => $sTitle,
  155. 'summary_attr' => bx_html_attribute($sSummaryPlain),
  156. 'content_url' => $sUrl,
  157. 'thumb_url' => $sPhotoThumb ? $sPhotoThumb : '',
  158. 'gallery_url' => $sPhotoGallery ? $sPhotoGallery : '',
  159. 'strecher' => str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ', 40),
  160. ),
  161. ),
  162. 'bx_if:no_thumb' => array (
  163. 'condition' => !$sPhotoThumb,
  164. 'content' => array (
  165. 'content_url' => $sUrl,
  166. 'summary_plain' => $sSummaryPlain,
  167. 'strecher' => mb_strlen($sSummaryPlain) > 240 ? '' : str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ', round((240 - mb_strlen($sSummaryPlain)) / 6)),
  168. ),
  169. ),
  170. );
  171. }
  172. protected function getTitle($aData)
  173. {
  174. $CNF = &$this->getModule()->_oConfig->CNF;
  175. return isset($aData[$CNF['FIELD_TITLE']]) ? $aData[$CNF['FIELD_TITLE']] : '';
  176. }
  177. protected function getText($aData)
  178. {
  179. $CNF = &$this->getModule()->_oConfig->CNF;
  180. return isset($aData[$CNF['FIELD_TEXT']]) ? $aData[$CNF['FIELD_TEXT']] : '';
  181. }
  182. protected function getSummary($aData, $sTitle = '', $sText = '', $sUrl = '')
  183. {
  184. $CNF = &$this->getModule()->_oConfig->CNF;
  185. if(empty($CNF['PARAM_CHARS_SUMMARY']))
  186. return '';
  187. // get summary
  188. $sLinkMore = ' <a title="' . bx_html_attribute(_t('_sys_read_more', $sTitle)) . '" href="' . $sUrl . '"><i class="sys-icon ellipsis-h"></i></a>';
  189. return strmaxtextlen($sText, (int)getParam($CNF['PARAM_CHARS_SUMMARY']), $sLinkMore);
  190. }
  191. protected function getAttachments ($sStorage, $aData)
  192. {
  193. $CNF = &$this->getModule()->_oConfig->CNF;
  194. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  195. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  196. $aTranscodersVideo = false;
  197. if ($CNF['OBJECT_VIDEOS_TRANSCODERS'])
  198. $aTranscodersVideo = array (
  199. 'poster' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']),
  200. 'mp4' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']),
  201. 'webm' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['webm']),
  202. );
  203. $aGhostFiles = $oStorage->getGhosts ($aData[$CNF['FIELD_AUTHOR']], $aData[$CNF['FIELD_ID']]);
  204. if (!$aGhostFiles)
  205. return false;
  206. if(isset($aData[$CNF['FIELD_THUMB']]))
  207. foreach ($aGhostFiles as $k => $a) {
  208. // don't show thumbnail in attachments
  209. if ($a['id'] == $aData[$CNF['FIELD_THUMB']])
  210. unset($aGhostFiles[$k]);
  211. }
  212. if(!$aGhostFiles)
  213. return false;
  214. foreach ($aGhostFiles as $k => $a) {
  215. $isImage = $oTranscoder && (0 === strncmp('image/', $a['mime_type'], 6)); // preview for images, transcoder object for preview must be defined
  216. $isVideo = $aTranscodersVideo && (0 === strncmp('video/', $a['mime_type'], 6)); // preview for videos, transcoder object for video must be defined
  217. $sUrlOriginal = $oStorage->getFileUrlById($a['id']);
  218. $sImgPopupId = 'bx-messages-atachment-popup-' . $a['id'];
  219. // images are displayed with preview and popup upon clicking
  220. $aGhostFiles[$k]['bx_if:image'] = array (
  221. 'condition' => $isImage,
  222. 'content' => array (
  223. 'url_original' => $sUrlOriginal,
  224. 'attr_file_name' => bx_html_attribute($a['file_name']),
  225. 'popup_id' => $sImgPopupId,
  226. 'url_preview' => $isImage ? $oTranscoder->getFileUrl($a['id']) : '',
  227. 'popup' => BxTemplFunctions::getInstance()->transBox($sImgPopupId, '<img src="' . $sUrlOriginal . '" />', true, true),
  228. ),
  229. );
  230. // videos are displayed inline
  231. $aGhostFiles[$k]['bx_if:video'] = array (
  232. 'condition' => $isVideo,
  233. 'content' => array (
  234. 'video' => $isVideo && $aTranscodersVideo ? BxTemplFunctions::getInstance()->videoPlayer(
  235. $aTranscodersVideo['poster']->getFileUrl($a['id']),
  236. $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  237. $aTranscodersVideo['webm']->getFileUrl($a['id']),
  238. false, ''
  239. ) : '',
  240. ),
  241. );
  242. // non-images are displayed as text links to original file
  243. $aGhostFiles[$k]['bx_if:not_image'] = array (
  244. 'condition' => !$isImage && !$isVideo,
  245. 'content' => array (
  246. 'url_original' => $sUrlOriginal,
  247. 'attr_file_name' => bx_html_attribute($a['file_name']),
  248. 'file_name' => bx_process_output($a['file_name']),
  249. ),
  250. );
  251. }
  252. return $aGhostFiles;
  253. }
  254. }
  255. /** @} */