BxBaseModTextTemplate.php 12 KB

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