BxBaseModTextTemplate.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  43. 'author_title' => $sName,
  44. 'author_title_attr' => bx_html_attribute($sName),
  45. 'author_desc' => $sFuncAuthorDesc ? $this->$sFuncAuthorDesc($aData) : '',
  46. 'bx_if:addon' => array (
  47. 'condition' => (bool)$sAddon,
  48. 'content' => array (
  49. 'content' => $sAddon,
  50. ),
  51. ),
  52. );
  53. return $this->parseHtmlByName($sTemplateName, $aVars);
  54. }
  55. function getAuthorDesc ($aData)
  56. {
  57. return '';
  58. }
  59. function getAuthorAddon ($aData, $oProfile)
  60. {
  61. $CNF = &$this->getModule()->_oConfig->CNF;
  62. $sUrl = 'page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . $oProfile->id();
  63. $sUrl = BxDolPermalinks::getInstance()->permalink($sUrl);
  64. return _t($CNF['T']['txt_all_entries_by'], $sUrl, $oProfile->getDisplayName(), $this->getModule()->_oDb->getEntriesNumByAuthor($oProfile->id()));
  65. }
  66. public function entryBreadcrumb($aContentInfo, $aTmplVarsItems = array())
  67. {
  68. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  69. return $this->parseHtmlByName('breadcrumb.html', array(
  70. 'url_home' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']),
  71. 'icon_home' => $CNF['ICON'],
  72. 'bx_repeat:items' => $aTmplVarsItems
  73. ));
  74. }
  75. function entryAttachments ($aData, $aParams = array())
  76. {
  77. return $this->entryAttachmentsByStorage($this->getModule()->_oConfig->CNF['OBJECT_STORAGE'], $aData, $aParams);
  78. }
  79. function entryAttachmentsByStorage ($sStorage, $aData, $aParams = array())
  80. {
  81. if (!($a = $this->getAttachments($sStorage, $aData, $aParams)))
  82. return '';
  83. return $this->parseHtmlByName('attachments.html', array(
  84. 'bx_repeat:attachments' => $a,
  85. ));
  86. }
  87. protected function checkPrivacy ($aData, $isCheckPrivateContent, $oModule, $sTemplateName = '')
  88. {
  89. if ($isCheckPrivateContent && CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $oModule->checkAllowedView($aData))) {
  90. $aVars = array (
  91. 'summary' => $sMsg,
  92. );
  93. return $this->parseHtmlByName($sTemplateName ? str_replace('.html', '_private.html', $sTemplateName) : 'unit_private.html', $aVars);
  94. }
  95. return '';
  96. }
  97. protected function getUnitThumbAndGallery ($aData)
  98. {
  99. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  100. $sPhotoThumb = '';
  101. $sPhotoGallery = '';
  102. if(!empty($CNF['FIELD_THUMB']) && !empty($aData[$CNF['FIELD_THUMB']])) {
  103. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  104. if ($oImagesTranscoder)
  105. $sPhotoThumb = $oImagesTranscoder->getFileUrl($aData[$CNF['FIELD_THUMB']]);
  106. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  107. if ($oImagesTranscoder)
  108. $sPhotoGallery = $oImagesTranscoder->getFileUrl($aData[$CNF['FIELD_THUMB']]);
  109. else
  110. $sPhotoGallery = $sPhotoThumb;
  111. }
  112. return array($sPhotoThumb, $sPhotoGallery);
  113. }
  114. protected function getUnit ($aData, $aParams = array())
  115. {
  116. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  117. // get thumb url
  118. list($sPhotoThumb, $sPhotoGallery) = $this->getUnitThumbAndGallery($aData);
  119. // get entry url
  120. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aData[$CNF['FIELD_ID']]);
  121. $oProfile = BxDolProfile::getInstance($aData[$CNF['FIELD_AUTHOR']]);
  122. if (!$oProfile)
  123. $oProfile = BxDolProfileUndefined::getInstance();
  124. $sTitle = $this->getTitle($aData);
  125. $sText = $this->getText($aData);
  126. $sSummary = $this->getSummary($aData, $sTitle, $sText, $sUrl);
  127. $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'])) : '';
  128. if(!empty($CNF['OBJECT_METATAGS'])) {
  129. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  130. // keywords
  131. if ($oMetatags->keywordsIsEnabled())
  132. $sText = $oMetatags->keywordsParse($aData[$CNF['FIELD_ID']], $sText);
  133. }
  134. $sTitle = bx_process_output($sTitle);
  135. // generate html
  136. return array (
  137. 'id' => $aData[$CNF['FIELD_ID']],
  138. 'content_url' => $sUrl,
  139. 'title' => $sTitle,
  140. 'title_attr' => bx_html_attribute($sTitle),
  141. 'summary' => $sSummary,
  142. 'text' => $sText,
  143. 'author' => $oProfile->getDisplayName(),
  144. 'author_url' => $oProfile->getUrl(),
  145. 'author_icon' => $oProfile->getIcon(),
  146. 'author_thumb' => $oProfile->getThumb(),
  147. 'author_avatar' => $oProfile->getAvatar(),
  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, $aParams = array())
  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. $sFilterField = isset($aParams['filter_field']) ? $aParams['filter_field'] : $CNF['FIELD_THUMB'];
  207. if(!empty($sFilterField) && isset($aData[$sFilterField]))
  208. foreach ($aGhostFiles as $k => $a) {
  209. // don't show thumbnail in attachments
  210. if ($a['id'] == $aData[$sFilterField])
  211. unset($aGhostFiles[$k]);
  212. }
  213. if(!$aGhostFiles)
  214. return false;
  215. $aAttachmnts = array();
  216. foreach ($aGhostFiles as $k => $a) {
  217. $isImage = $oTranscoder && (0 === strncmp('image/', $a['mime_type'], 6)); // preview for images, transcoder object for preview must be defined
  218. $isVideo = $aTranscodersVideo && (0 === strncmp('video/', $a['mime_type'], 6)); // preview for videos, transcoder object for video must be defined
  219. $sUrlOriginal = $oStorage->getFileUrlById($a['id']);
  220. $sImgPopupId = 'bx-messages-atachment-popup-' . $a['id'];
  221. // images are displayed with preview and popup upon clicking
  222. $a['bx_if:image'] = array (
  223. 'condition' => $isImage,
  224. 'content' => array (
  225. 'url_original' => $sUrlOriginal,
  226. 'attr_file_name' => bx_html_attribute($a['file_name']),
  227. 'popup_id' => $sImgPopupId,
  228. 'url_preview' => $isImage ? $oTranscoder->getFileUrl($a['id']) : '',
  229. 'popup' => BxTemplFunctions::getInstance()->transBox($sImgPopupId, '<img src="' . $sUrlOriginal . '" />', true, true),
  230. ),
  231. );
  232. // videos are displayed inline
  233. $a['bx_if:video'] = array (
  234. 'condition' => $isVideo,
  235. 'content' => array (
  236. 'video' => $isVideo && $aTranscodersVideo ? BxTemplFunctions::getInstance()->videoPlayer(
  237. $aTranscodersVideo['poster']->getFileUrl($a['id']),
  238. $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  239. $aTranscodersVideo['webm']->getFileUrl($a['id']),
  240. false, ''
  241. ) : '',
  242. ),
  243. );
  244. // non-images are displayed as text links to original file
  245. $a['bx_if:not_image'] = array (
  246. 'condition' => !$isImage && !$isVideo,
  247. 'content' => array (
  248. 'url_original' => $sUrlOriginal,
  249. 'attr_file_name' => bx_html_attribute($a['file_name']),
  250. 'file_name' => bx_process_output($a['file_name']),
  251. ),
  252. );
  253. $aAttachmnts[] = $a;
  254. }
  255. return $aAttachmnts;
  256. }
  257. }
  258. /** @} */