BxBaseModGeneralTemplate.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Module representation.
  13. */
  14. class BxBaseModGeneralTemplate extends BxDolModuleTemplate
  15. {
  16. protected $MODULE;
  17. protected $_oModule;
  18. function __construct(&$oConfig, &$oDb)
  19. {
  20. parent::__construct($oConfig, $oDb);
  21. $this->addCss ('main.css');
  22. }
  23. public function getModule()
  24. {
  25. if (!$this->_oModule) {
  26. $sName = $this->_oConfig->getName();
  27. $this->_oModule = BxDolModule::getInstance($sName);
  28. }
  29. return $this->_oModule;
  30. }
  31. public function getJsCode($sType, $aParams = array(), $bWrap = true)
  32. {
  33. $sBaseUri = $this->_oConfig->getBaseUri();
  34. $sJsClass = $this->_oConfig->getJsClass($sType);
  35. $sJsObject = $this->_oConfig->getJsObject($sType);
  36. $aParams = array_merge(array(
  37. 'sActionUri' => $sBaseUri,
  38. 'sActionUrl' => BX_DOL_URL_ROOT . $sBaseUri,
  39. 'sObjName' => $sJsObject,
  40. 'aHtmlIds' => array(),
  41. 'oRequestParams' => array()
  42. ), $aParams);
  43. $sContent = "var " . $sJsObject . " = new " . $sJsClass . "(" . json_encode($aParams) . ");";
  44. return !$bWrap ? $sContent : $this->_wrapInTagJsCode($sContent);
  45. }
  46. function getUnitMetaItemLink($sContent, $aAttrs = array())
  47. {
  48. return $this->getUnitMetaItem('a', $sContent, $aAttrs);
  49. }
  50. function getUnitMetaItemText($sContent, $aAttrs = array())
  51. {
  52. if(!is_array($aAttrs))
  53. $aAttrs = array();
  54. $aAttrs['class'] = (!empty($aAttrs['class']) ? $aAttrs['class'] . ' ' : '') . 'bx-def-font-grayed';
  55. return $this->getUnitMetaItem('span', $sContent, $aAttrs);
  56. }
  57. function getUnitMetaItemCustom($sContent)
  58. {
  59. return $this->getUnitMetaItem('custom', $sContent);
  60. }
  61. function entryText ($aData, $sTemplateName = 'entry-text.html')
  62. {
  63. $CNF = &$this->getModule()->_oConfig->CNF;
  64. $aVars = $aData;
  65. $aVars['entry_title'] = !empty($CNF['FIELD_TITLE']) && isset($aData[$CNF['FIELD_TITLE']]) ? bx_process_output($aData[$CNF['FIELD_TITLE']]) : '';
  66. $aVars['entry_text'] = !empty($CNF['FIELD_TEXT']) && isset($aData[$CNF['FIELD_TEXT']]) ? bx_process_output($aData[$CNF['FIELD_TEXT']], BX_DATA_HTML) : '';
  67. if (!empty($CNF['OBJECT_METATAGS'])) {
  68. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  69. // keywords
  70. if ($oMetatags->keywordsIsEnabled()) {
  71. $aFields = array_merge($oMetatags->keywordsFields($aData, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']), array('entry_title', 'entry_text'));
  72. foreach ($aFields as $sField)
  73. $aVars[$sField] = $oMetatags->keywordsParse($aData[$CNF['FIELD_ID']], $aVars[$sField]);
  74. }
  75. // location
  76. $aVars['location'] = $oMetatags->locationsIsEnabled() ? $oMetatags->locationsString($aData[$CNF['FIELD_ID']]) : '';
  77. }
  78. if (empty($aVars['entry_text']))
  79. return false;
  80. unset($aVars['recipients']);
  81. return $this->parseHtmlByName($sTemplateName, $aVars);
  82. }
  83. function entryLocation ($iContentId)
  84. {
  85. $CNF = &$this->getModule()->_oConfig->CNF;
  86. if (empty($CNF['OBJECT_METATAGS']))
  87. return '';
  88. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  89. if (!($sLocationString = $oMetatags->locationsString($iContentId)))
  90. return '';
  91. $aVars = array (
  92. 'location' => $sLocationString
  93. );
  94. return $this->parseHtmlByName('entry-location.html', $aVars);
  95. }
  96. public function entryInfo($aData, $aValues = array())
  97. {
  98. $CNF = $this->_oConfig->CNF;
  99. $aValuesDefault = array();
  100. if (isset($aData[$CNF['FIELD_ADDED']]))
  101. $aValuesDefault[] = array(
  102. 'title' => _t('_sys_txt_field_created'),
  103. 'value' => bx_time_js($aData[$CNF['FIELD_ADDED']]),
  104. );
  105. if (isset($aData[$CNF['FIELD_CHANGED']]))
  106. $aValuesDefault[] = array(
  107. 'title' => _t('_sys_txt_field_updated'),
  108. 'value' => bx_time_js($aData[$CNF['FIELD_CHANGED']]),
  109. );
  110. $aValues = array_merge($aValuesDefault, $aValues);
  111. return $this->parseHtmlByName('entry-info.html', array(
  112. 'bx_repeat:info' => $aValues,
  113. ));
  114. }
  115. function entryAllActions ($sActionsEntity, $sActionsSocial)
  116. {
  117. if(empty($sActionsEntity) && empty($sActionsSocial))
  118. return '';
  119. return $this->parseHtmlByName('entry-all-actions.html', array (
  120. 'actions_entity' => $sActionsEntity,
  121. 'actions_social' => $sActionsSocial,
  122. ));
  123. }
  124. function entryAttachments ($aData, $aParams = array())
  125. {
  126. return $this->entryAttachmentsByStorage($this->getModule()->_oConfig->CNF['OBJECT_STORAGE'], $aData, $aParams);
  127. }
  128. function entryAttachmentsByStorage ($sStorage, $aData, $aParams = array())
  129. {
  130. if (!($a = $this->getAttachments($sStorage, $aData, $aParams)))
  131. return '';
  132. return $this->parseHtmlByName('attachments.html', array(
  133. 'bx_repeat:attachments' => $a,
  134. ));
  135. }
  136. protected function getUnitMetaItem($sName, $sContent, $aAttrs = array(), $sTemplate = 'unit_meta_item.html')
  137. {
  138. if(empty($sContent))
  139. return '';
  140. $aTags = array('span', 'a', 'custom');
  141. $aTmplVarsAttrs = array();
  142. foreach($aAttrs as $sKey => $sValue)
  143. $aTmplVarsAttrs[] = array('key' => $sKey, 'value' => bx_html_attribute($sValue));
  144. $aTmplVars = array();
  145. foreach($aTags as $sTag) {
  146. $aTmplVarsTag = array();
  147. $bTmplVarsTag = $sTag == $sName;
  148. if($bTmplVarsTag)
  149. $aTmplVarsTag = array(
  150. 'content' => $sContent,
  151. 'bx_repeat:attrs' => $aTmplVarsAttrs
  152. );
  153. $aTmplVars['bx_if:' . $sTag] = array(
  154. 'condition' => $bTmplVarsTag,
  155. 'content' => $aTmplVarsTag
  156. );
  157. }
  158. return $this->parseHtmlByName($sTemplate, $aTmplVars);
  159. }
  160. protected function getAttachments ($sStorage, $aData, $aParams = array())
  161. {
  162. $CNF = &$this->getModule()->_oConfig->CNF;
  163. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  164. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  165. $oTranscoderPreview = isset($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) && $CNF['OBJECT_IMAGES_TRANSCODER_PICTURE'] ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) : null;
  166. $aTranscodersVideo = false;
  167. if (isset($CNF['OBJECT_VIDEOS_TRANSCODERS']) && $CNF['OBJECT_VIDEOS_TRANSCODERS'])
  168. $aTranscodersVideo = array (
  169. 'poster' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']),
  170. 'mp4' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']),
  171. 'webm' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['webm']),
  172. );
  173. $aGhostFiles = $oStorage->getGhosts ($this->getModule()->serviceGetContentOwnerProfileId($aData[$CNF['FIELD_ID']]), $aData[$CNF['FIELD_ID']]);
  174. if (!$aGhostFiles)
  175. return false;
  176. $sFilterField = isset($aParams['filter_field']) ? $aParams['filter_field'] : $CNF['FIELD_THUMB'];
  177. if(!empty($sFilterField) && isset($aData[$sFilterField]))
  178. foreach ($aGhostFiles as $k => $a) {
  179. // don't show thumbnail in attachments
  180. if ($a['id'] == $aData[$sFilterField])
  181. unset($aGhostFiles[$k]);
  182. }
  183. if(!$aGhostFiles)
  184. return false;
  185. $aAttachmnts = array();
  186. foreach ($aGhostFiles as $k => $a) {
  187. $isImage = $oTranscoder && (0 === strncmp('image/', $a['mime_type'], 6)); // preview for images, transcoder object for preview must be defined
  188. $isVideo = $aTranscodersVideo && (0 === strncmp('video/', $a['mime_type'], 6)); // preview for videos, transcoder object for video must be defined
  189. $sUrlOriginal = $oStorage->getFileUrlById($a['id']);
  190. $sImgPopupId = 'bx-messages-atachment-popup-' . $a['id'];
  191. // images are displayed with preview and popup upon clicking
  192. $a['bx_if:image'] = array (
  193. 'condition' => $isImage,
  194. 'content' => array (
  195. 'url_original' => $isImage && $oTranscoderPreview ? $oTranscoderPreview->getFileUrl($a['id']) : $sUrlOriginal,
  196. 'attr_file_name' => bx_html_attribute($a['file_name']),
  197. 'popup_id' => $sImgPopupId,
  198. 'url_preview' => $isImage ? $oTranscoder->getFileUrl($a['id']) : '',
  199. 'popup' => BxTemplFunctions::getInstance()->transBox($sImgPopupId, '<img src="' . $sUrlOriginal . '" />', true, true),
  200. ),
  201. );
  202. // videos are displayed inline
  203. $a['bx_if:video'] = array (
  204. 'condition' => $isVideo,
  205. 'content' => array (
  206. 'video' => $isVideo && $aTranscodersVideo ? BxTemplFunctions::getInstance()->videoPlayer(
  207. $aTranscodersVideo['poster']->getFileUrl($a['id']),
  208. $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  209. $aTranscodersVideo['webm']->getFileUrl($a['id']),
  210. false, ''
  211. ) : '',
  212. ),
  213. );
  214. // non-images are displayed as text links to original file
  215. $a['bx_if:not_image'] = array (
  216. 'condition' => !$isImage && !$isVideo,
  217. 'content' => array (
  218. 'url_original' => $sUrlOriginal,
  219. 'attr_file_name' => bx_html_attribute($a['file_name']),
  220. 'file_name' => bx_process_output($a['file_name']),
  221. ),
  222. );
  223. $aAttachmnts[] = $a;
  224. }
  225. return $aAttachmnts;
  226. }
  227. }
  228. /** @} */