BxBaseModTextTemplate.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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, $aParams = array())
  75. {
  76. return $this->entryAttachmentsByStorage($this->getModule()->_oConfig->CNF['OBJECT_STORAGE'], $aData, $aParams);
  77. }
  78. function entryAttachmentsByStorage ($sStorage, $aData, $aParams = array())
  79. {
  80. if (!($a = $this->getAttachments($sStorage, $aData, $aParams)))
  81. return '';
  82. return $this->parseHtmlByName('attachments.html', array(
  83. 'bx_repeat:attachments' => $a,
  84. ));
  85. }
  86. protected function checkPrivacy ($aData, $isCheckPrivateContent, $oModule, $sTemplateName = '')
  87. {
  88. if ($isCheckPrivateContent && CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $oModule->checkAllowedView($aData))) {
  89. $aVars = array (
  90. 'summary' => $sMsg,
  91. );
  92. return $this->parseHtmlByName($sTemplateName ? str_replace('.html', '_private.html', $sTemplateName) : 'unit_private.html', $aVars);
  93. }
  94. return '';
  95. }
  96. protected function getUnitThumbAndGallery ($aData)
  97. {
  98. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  99. $sPhotoThumb = '';
  100. $sPhotoGallery = '';
  101. if(!empty($CNF['FIELD_THUMB']) && !empty($aData[$CNF['FIELD_THUMB']])) {
  102. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  103. if ($oImagesTranscoder)
  104. $sPhotoThumb = $oImagesTranscoder->getFileUrl($aData[$CNF['FIELD_THUMB']]);
  105. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  106. if ($oImagesTranscoder)
  107. $sPhotoGallery = $oImagesTranscoder->getFileUrl($aData[$CNF['FIELD_THUMB']]);
  108. else
  109. $sPhotoGallery = $sPhotoThumb;
  110. }
  111. return array($sPhotoThumb, $sPhotoGallery);
  112. }
  113. protected function getUnit ($aData, $aParams = array())
  114. {
  115. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  116. // get thumb url
  117. list($sPhotoThumb, $sPhotoGallery) = $this->getUnitThumbAndGallery($aData);
  118. // get entry url
  119. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aData[$CNF['FIELD_ID']]);
  120. $oProfile = BxDolProfile::getInstance($aData[$CNF['FIELD_AUTHOR']]);
  121. if (!$oProfile)
  122. $oProfile = BxDolProfileUndefined::getInstance();
  123. $sTitle = $this->getTitle($aData);
  124. $sText = $this->getText($aData);
  125. $sSummary = $this->getSummary($aData, $sTitle, $sText, $sUrl);
  126. $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'])) : '';
  127. if(!empty($CNF['OBJECT_METATAGS'])) {
  128. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  129. // keywords
  130. if ($oMetatags->keywordsIsEnabled())
  131. $sText = $oMetatags->keywordsParse($aData[$CNF['FIELD_ID']], $sText);
  132. }
  133. $sTitle = bx_process_output($sTitle);
  134. // generate html
  135. return array (
  136. 'id' => $aData[$CNF['FIELD_ID']],
  137. 'content_url' => $sUrl,
  138. 'title' => $sTitle,
  139. 'title_attr' => bx_html_attribute($sTitle),
  140. 'summary' => $sSummary,
  141. 'text' => $sText,
  142. 'author' => $oProfile->getDisplayName(),
  143. 'author_url' => $oProfile->getUrl(),
  144. 'entry_posting_date' => bx_time_js($aData[$CNF['FIELD_ADDED']], BX_FORMAT_DATE),
  145. 'module_name' => _t($CNF['T']['txt_sample_single']),
  146. 'ts' => $aData[$CNF['FIELD_ADDED']],
  147. 'bx_if:thumb' => array (
  148. 'condition' => $sPhotoThumb,
  149. 'content' => array (
  150. 'title' => $sTitle,
  151. 'summary_attr' => bx_html_attribute($sSummaryPlain),
  152. 'content_url' => $sUrl,
  153. 'thumb_url' => $sPhotoThumb ? $sPhotoThumb : '',
  154. 'gallery_url' => $sPhotoGallery ? $sPhotoGallery : '',
  155. 'strecher' => str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ', 40),
  156. ),
  157. ),
  158. 'bx_if:no_thumb' => array (
  159. 'condition' => !$sPhotoThumb,
  160. 'content' => array (
  161. 'content_url' => $sUrl,
  162. 'summary_plain' => $sSummaryPlain,
  163. 'strecher' => mb_strlen($sSummaryPlain) > 240 ? '' : str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ', round((240 - mb_strlen($sSummaryPlain)) / 6)),
  164. ),
  165. ),
  166. );
  167. }
  168. protected function getTitle($aData)
  169. {
  170. $CNF = &$this->getModule()->_oConfig->CNF;
  171. return isset($aData[$CNF['FIELD_TITLE']]) ? $aData[$CNF['FIELD_TITLE']] : '';
  172. }
  173. protected function getText($aData)
  174. {
  175. $CNF = &$this->getModule()->_oConfig->CNF;
  176. return isset($aData[$CNF['FIELD_TEXT']]) ? $aData[$CNF['FIELD_TEXT']] : '';
  177. }
  178. protected function getSummary($aData, $sTitle = '', $sText = '', $sUrl = '')
  179. {
  180. $CNF = &$this->getModule()->_oConfig->CNF;
  181. if(empty($CNF['PARAM_CHARS_SUMMARY']))
  182. return '';
  183. // get summary
  184. $sLinkMore = ' <a title="' . bx_html_attribute(_t('_sys_read_more', $sTitle)) . '" href="' . $sUrl . '"><i class="sys-icon ellipsis-h"></i></a>';
  185. return strmaxtextlen($sText, (int)getParam($CNF['PARAM_CHARS_SUMMARY']), $sLinkMore);
  186. }
  187. protected function getAttachments ($sStorage, $aData, $aParams = array())
  188. {
  189. $CNF = &$this->getModule()->_oConfig->CNF;
  190. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  191. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  192. $aTranscodersVideo = false;
  193. if ($CNF['OBJECT_VIDEOS_TRANSCODERS'])
  194. $aTranscodersVideo = array (
  195. 'poster' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']),
  196. 'mp4' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']),
  197. 'webm' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['webm']),
  198. );
  199. $aGhostFiles = $oStorage->getGhosts ($aData[$CNF['FIELD_AUTHOR']], $aData[$CNF['FIELD_ID']]);
  200. if (!$aGhostFiles)
  201. return false;
  202. $sFilterField = isset($aParams['filter_field']) ? $aParams['filter_field'] : $CNF['FIELD_THUMB'];
  203. if(!empty($sFilterField) && isset($aData[$sFilterField]))
  204. foreach ($aGhostFiles as $k => $a) {
  205. // don't show thumbnail in attachments
  206. if ($a['id'] == $aData[$sFilterField])
  207. unset($aGhostFiles[$k]);
  208. }
  209. if(!$aGhostFiles)
  210. return false;
  211. $aAttachmnts = array();
  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. $a['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. $a['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. $a['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. $aAttachmnts[] = $a;
  250. }
  251. return $aAttachmnts;
  252. }
  253. }
  254. /** @} */