BxBaseModTextTemplate.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. protected $_sUnitClassShowCase;
  17. function __construct(&$oConfig, &$oDb)
  18. {
  19. parent::__construct($oConfig, $oDb);
  20. $this->_sUnitClassShowCase = 'bx-base-unit-showcase bx-base-text-unit-showcase bx-def-margin-sec-bottom';
  21. }
  22. function unit ($aData, $isCheckPrivateContent = true, $sTemplateName = 'unit.html', $aParams = array())
  23. {
  24. $sResult = $this->checkPrivacy ($aData, $isCheckPrivateContent, $this->getModule(), $sTemplateName);
  25. if($sResult)
  26. return $sResult;
  27. $aParams['template_name'] = $sTemplateName;
  28. return $this->parseHtmlByName($sTemplateName, $this->getUnit($aData, $aParams));
  29. }
  30. function entryAuthor ($aData, $iProfileId = false, $sFuncAuthorDesc = 'getAuthorDesc', $sTemplateName = 'author.html', $sFuncAuthorAddon = 'getAuthorAddon')
  31. {
  32. $CNF = &$this->getModule()->_oConfig->CNF;
  33. if (!$iProfileId)
  34. $iProfileId = $aData[$CNF['FIELD_AUTHOR']];
  35. // if post is anonymous then don't show author block for regular members
  36. if ($aData[$CNF['FIELD_AUTHOR']] < 0 && !$this->getModule()->_isModerator())
  37. return '';
  38. $oProfile = BxDolProfile::getInstance($iProfileId >= 0 ? $iProfileId : -$iProfileId);
  39. if (!$oProfile || !$iProfileId)
  40. $oProfile = BxDolProfileUndefined::getInstance();
  41. if (!$oProfile)
  42. return '';
  43. $sName = $oProfile->getDisplayName();
  44. $sAddon = $sFuncAuthorAddon ? $this->$sFuncAuthorAddon($aData, $oProfile) : '';
  45. $aVars = array (
  46. 'author_url' => $oProfile->getUrl(),
  47. 'author_thumb_url' => $oProfile->getThumb(),
  48. 'author_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  49. 'author_title' => $sName,
  50. 'author_title_attr' => bx_html_attribute($sName),
  51. 'author_desc' => $sFuncAuthorDesc ? $this->$sFuncAuthorDesc($aData) : '',
  52. 'bx_if:addon' => array (
  53. 'condition' => (bool)$sAddon,
  54. 'content' => array (
  55. 'content' => $sAddon,
  56. ),
  57. ),
  58. );
  59. return $this->parseHtmlByName($sTemplateName, $aVars);
  60. }
  61. function entryContext ($aData, $iProfileId = false, $sFuncContextDesc = 'getContextDesc', $sTemplateName = 'context.html', $sFuncContextAddon = 'getContextAddon')
  62. {
  63. $CNF = &$this->getModule()->_oConfig->CNF;
  64. $iContextId = $aData[$CNF['FIELD_ALLOW_VIEW_TO']];
  65. if ($iContextId >= 0)
  66. return '';
  67. $iProfileId = - $iContextId;
  68. $oProfile = BxDolProfile::getInstance($iProfileId);
  69. if (!$oProfile)
  70. $oProfile = BxDolProfileUndefined::getInstance();
  71. if (!$oProfile)
  72. return '';
  73. $sName = $oProfile->getDisplayName();
  74. $sAddon = $sFuncContextAddon ? $this->$sFuncContextAddon($aData, $oProfile) : '';
  75. $aVars = array (
  76. 'author_url' => $oProfile->getUrl(),
  77. 'author_thumb_url' => $oProfile->getThumb(),
  78. 'author_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  79. 'author_title' => $sName,
  80. 'author_title_attr' => bx_html_attribute($sName),
  81. 'author_desc' => $sFuncContextDesc ? $this->$sFuncContextDesc($aData) : '',
  82. 'bx_if:addon' => array (
  83. 'condition' => (bool)$sAddon,
  84. 'content' => array (
  85. 'content' => $sAddon,
  86. ),
  87. ),
  88. );
  89. return $this->parseHtmlByName($sTemplateName, $aVars);
  90. }
  91. public function getTmplVarsText($aData)
  92. {
  93. $aVars = parent::getTmplVarsText($aData);
  94. $sImage = '';
  95. $mixedImage = $this->_getHeaderImage($aData);
  96. if($mixedImage !== false) {
  97. if(!empty($mixedImage['object']))
  98. $o = BxDolStorage::getObjectInstance($mixedImage['object']);
  99. else if(!empty($mixedImage['transcoder']))
  100. $o = BxDolTranscoder::getObjectInstance($mixedImage['transcoder']);
  101. if($o)
  102. $sImage = $o->getFileUrlById($mixedImage['id']);
  103. }
  104. $aVars['bx_if:show_image'] = array(
  105. 'condition' => !empty($sImage),
  106. 'content' => array(
  107. 'entry_image' => $sImage
  108. )
  109. );
  110. return $aVars;
  111. }
  112. function getAuthorDesc ($aData)
  113. {
  114. if (!($a = $this->getAuthorSnippetMenu($aData)) || !isset($a['meta']))
  115. return '';
  116. return $a['meta'];
  117. }
  118. function getContextDesc ($aData)
  119. {
  120. return '';
  121. }
  122. function getAuthorSnippetMenu ($aData)
  123. {
  124. $CNF = &$this->getModule()->_oConfig->CNF;
  125. if (!($oProfile = BxDolProfile::getInstance($aData[$CNF['FIELD_AUTHOR']])))
  126. return array();
  127. return bx_srv($oProfile->getModule(), 'get_snippet_menu_vars', array($oProfile->id()));
  128. }
  129. function getAuthorAddon ($aData, $oProfile)
  130. {
  131. $CNF = &$this->getModule()->_oConfig->CNF;
  132. $sUrl = 'page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . $oProfile->id();
  133. $sUrl = BxDolPermalinks::getInstance()->permalink($sUrl);
  134. return _t($CNF['T']['txt_all_entries_by'], $sUrl, $oProfile->getDisplayName(), $this->getModule()->_oDb->getEntriesNumByAuthor($oProfile->id()));
  135. }
  136. function getContextAddon ($aData, $oProfile)
  137. {
  138. $CNF = &$this->getModule()->_oConfig->CNF;
  139. $sUrl = 'page.php?i=' . $CNF['URI_ENTRIES_BY_CONTEXT'] . '&profile_id=' . $oProfile->id();
  140. $sUrl = BxDolPermalinks::getInstance()->permalink($sUrl);
  141. return _t($CNF['T']['txt_all_entries_in'], $sUrl, $oProfile->getDisplayName(), $this->getModule()->_oDb->getEntriesNumByContext($oProfile->id()));
  142. }
  143. public function entryBreadcrumb($aContentInfo, $aTmplVarsItems = array())
  144. {
  145. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  146. return $this->parseHtmlByName('breadcrumb.html', array(
  147. 'url_home' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']),
  148. 'icon_home' => $CNF['ICON'],
  149. 'bx_repeat:items' => $aTmplVarsItems
  150. ));
  151. }
  152. protected function checkPrivacy ($aData, $isCheckPrivateContent, $oModule, $sTemplateName = '')
  153. {
  154. if ($isCheckPrivateContent && CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $oModule->checkAllowedView($aData))) {
  155. $aVars = array (
  156. 'summary' => $sMsg,
  157. );
  158. return $this->parseHtmlByName($sTemplateName ? str_replace('.html', '_private.html', $sTemplateName) : 'unit_private.html', $aVars);
  159. }
  160. return '';
  161. }
  162. protected function getUnitThumbAndGallery ($aData)
  163. {
  164. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  165. $sPhotoThumb = '';
  166. $sPhotoGallery = '';
  167. if(!empty($CNF['FIELD_THUMB']) && !empty($aData[$CNF['FIELD_THUMB']])) {
  168. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  169. if ($oImagesTranscoder)
  170. $sPhotoThumb = $oImagesTranscoder->getFileUrl($aData[$CNF['FIELD_THUMB']]);
  171. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  172. if ($oImagesTranscoder)
  173. $sPhotoGallery = $oImagesTranscoder->getFileUrl($aData[$CNF['FIELD_THUMB']]);
  174. else
  175. $sPhotoGallery = $sPhotoThumb;
  176. }
  177. return array($sPhotoThumb, $sPhotoGallery);
  178. }
  179. protected function getUnit ($aData, $aParams = array())
  180. {
  181. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  182. // get thumb url
  183. list($sPhotoThumb, $sPhotoGallery) = $this->getUnitThumbAndGallery($aData);
  184. // get entry url
  185. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aData[$CNF['FIELD_ID']]);
  186. $oProfile = BxDolProfile::getInstanceMagic($aData[$CNF['FIELD_AUTHOR']]);
  187. $sTitle = $this->getTitle($aData);
  188. $sText = $this->getText($aData);
  189. $sSummary = $this->getSummary($aData, $sTitle, $sText, $sUrl);
  190. $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'])) : '';
  191. if(!empty($CNF['OBJECT_METATAGS'])) {
  192. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  193. $sText = $oMetatags->metaParse($aData[$CNF['FIELD_ID']], $sText);
  194. }
  195. $sTitle = bx_process_output($sTitle);
  196. $aTmplVarsMeta = array();
  197. if(!empty($CNF['OBJECT_MENU_SNIPPET_META'])) {
  198. $oMenuMeta = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SNIPPET_META'], $this);
  199. if($oMenuMeta) {
  200. $oMenuMeta->setContentId($aData[$CNF['FIELD_ID']]);
  201. $aTmplVarsMeta = array(
  202. 'meta' => $oMenuMeta->getCode()
  203. );
  204. }
  205. }
  206. // generate html
  207. return array (
  208. 'class' => $this->_getUnitClass($aData,(isset($aParams['template_name']) ? $aParams['template_name'] : '')),
  209. 'id' => $aData[$CNF['FIELD_ID']],
  210. 'content_url' => $sUrl,
  211. 'title' => $sTitle,
  212. 'title_attr' => bx_html_attribute($sTitle),
  213. 'summary' => $sSummary,
  214. 'text' => $sText,
  215. 'author' => $oProfile->getDisplayName(),
  216. 'author_url' => $oProfile->getUrl(),
  217. 'author_icon' => $oProfile->getIcon(),
  218. 'author_thumb' => $oProfile->getThumb(),
  219. 'author_avatar' => $oProfile->getAvatar(),
  220. 'entry_posting_date' => bx_time_js($aData[$CNF['FIELD_ADDED']], BX_FORMAT_DATE),
  221. 'module_name' => _t($CNF['T']['txt_sample_single']),
  222. 'ts' => $aData[$CNF['FIELD_ADDED']],
  223. 'bx_if:meta' => array(
  224. 'condition' => !empty($aTmplVarsMeta),
  225. 'content' => $aTmplVarsMeta
  226. ),
  227. 'bx_if:thumb' => array (
  228. 'condition' => $sPhotoThumb,
  229. 'content' => array (
  230. 'title' => $sTitle,
  231. 'summary_attr' => bx_html_attribute($sSummaryPlain),
  232. 'content_url' => $sUrl,
  233. 'thumb_url' => $sPhotoThumb ? $sPhotoThumb : '',
  234. 'gallery_url' => $sPhotoGallery ? $sPhotoGallery : '',
  235. 'strecher' => str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ', 40),
  236. ),
  237. ),
  238. 'bx_if:no_thumb' => array (
  239. 'condition' => !$sPhotoThumb,
  240. 'content' => array (
  241. 'content_url' => $sUrl,
  242. 'summary_plain' => $sSummaryPlain,
  243. 'strecher' => mb_strlen($sSummaryPlain) > 240 ? '' : str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ', round((240 - mb_strlen($sSummaryPlain)) / 6)),
  244. ),
  245. ),
  246. );
  247. }
  248. protected function getTitle($aData)
  249. {
  250. $CNF = &$this->getModule()->_oConfig->CNF;
  251. return isset($aData[$CNF['FIELD_TITLE']]) ? $aData[$CNF['FIELD_TITLE']] : '';
  252. }
  253. protected function getText($aData)
  254. {
  255. $CNF = &$this->getModule()->_oConfig->CNF;
  256. return isset($aData[$CNF['FIELD_TEXT']]) ? $aData[$CNF['FIELD_TEXT']] : '';
  257. }
  258. protected function getSummary($aData, $sTitle = '', $sText = '', $sUrl = '')
  259. {
  260. $CNF = &$this->getModule()->_oConfig->CNF;
  261. if(empty($CNF['PARAM_CHARS_SUMMARY']))
  262. return '';
  263. // get summary
  264. $sLinkMore = ' <a title="' . bx_html_attribute(_t('_sys_read_more', $sTitle)) . '" href="' . $sUrl . '"><i class="sys-icon ellipsis-h"></i></a>';
  265. return strmaxtextlen($sText, (int)getParam($CNF['PARAM_CHARS_SUMMARY']), $sLinkMore);
  266. }
  267. protected function _getUnitClass($aData, $sTemplateName = 'unit.html')
  268. {
  269. $sResult = '';
  270. switch($sTemplateName) {
  271. case 'unit_showcase.html':
  272. $sResult = $this->_sUnitClassShowCase;
  273. break;
  274. }
  275. return $sResult;
  276. }
  277. protected function _getHeaderImage($aData)
  278. {
  279. return $this->getModule()->getEntryImageData($aData);
  280. }
  281. }
  282. /** @} */