BxBaseModGeneralTemplate.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. public function getUnitMetaItemLink($sContent, $aAttrs = array())
  47. {
  48. return $this->getUnitMetaItem('a', $sContent, $aAttrs);
  49. }
  50. public function getUnitMetaItemText($sContent, $aAttrs = array())
  51. {
  52. return $this->getUnitMetaItem('span', $sContent, $aAttrs);
  53. }
  54. public function getUnitMetaItemButton($sContent, $aAttrs = array())
  55. {
  56. return $this->getUnitMetaItem('button', $sContent, $aAttrs);
  57. }
  58. public function getUnitMetaItemButtonSmall($sContent, $aAttrs = array())
  59. {
  60. return $this->getUnitMetaItem('sbutton', $sContent, $aAttrs);
  61. }
  62. public function getUnitMetaItemCustom($sContent)
  63. {
  64. return $this->getUnitMetaItem('custom', $sContent);
  65. }
  66. function entryText ($aData, $sTemplateName = 'entry-text.html')
  67. {
  68. $CNF = &$this->getModule()->_oConfig->CNF;
  69. $aVars = $aData;
  70. $aVars['entry_title'] = !empty($CNF['FIELD_TITLE']) && isset($aData[$CNF['FIELD_TITLE']]) ? bx_process_output($aData[$CNF['FIELD_TITLE']]) : '';
  71. $aVars['entry_text'] = !empty($CNF['FIELD_TEXT']) && isset($aData[$CNF['FIELD_TEXT']]) ? bx_process_output($aData[$CNF['FIELD_TEXT']], BX_DATA_HTML) : '';
  72. if (!empty($CNF['OBJECT_METATAGS'])) {
  73. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  74. // keywords
  75. if ($oMetatags->keywordsIsEnabled()) {
  76. $aFields = array_merge($oMetatags->metaFields($aData, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']), array('entry_title', 'entry_text'));
  77. foreach ($aFields as $sField)
  78. $aVars[$sField] = $oMetatags->keywordsParse($aData[$CNF['FIELD_ID']], $aVars[$sField]);
  79. }
  80. // mentions
  81. if ($oMetatags->mentionsIsEnabled()) {
  82. $aFields = array_merge($oMetatags->metaFields($aData, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'], true), array('entry_text'));
  83. foreach ($aFields as $sField)
  84. $aVars[$sField] = $oMetatags->mentionsParse($aData[$CNF['FIELD_ID']], $aVars[$sField]);
  85. }
  86. // location
  87. $aVars['location'] = $oMetatags->locationsIsEnabled() ? $oMetatags->locationsString($aData[$CNF['FIELD_ID']]) : '';
  88. }
  89. if (empty($aVars['entry_text']))
  90. return false;
  91. unset($aVars['recipients']);
  92. return $this->parseHtmlByName($sTemplateName, $aVars);
  93. }
  94. function entryLocation ($iContentId)
  95. {
  96. $CNF = &$this->getModule()->_oConfig->CNF;
  97. if (empty($CNF['OBJECT_METATAGS']))
  98. return '';
  99. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  100. if (!($sLocationString = $oMetatags->locationsString($iContentId)))
  101. return '';
  102. $aVars = array (
  103. 'location' => $sLocationString
  104. );
  105. return $this->parseHtmlByName('entry-location.html', $aVars);
  106. }
  107. public function entryInfo($aData, $aValues = array())
  108. {
  109. $CNF = $this->_oConfig->CNF;
  110. $aValuesDefault = array();
  111. if (isset($aData[$CNF['FIELD_ADDED']]))
  112. $aValuesDefault[] = array(
  113. 'title' => _t('_sys_txt_field_created'),
  114. 'value' => bx_time_js($aData[$CNF['FIELD_ADDED']]),
  115. );
  116. if (isset($aData[$CNF['FIELD_CHANGED']]))
  117. $aValuesDefault[] = array(
  118. 'title' => _t('_sys_txt_field_updated'),
  119. 'value' => bx_time_js($aData[$CNF['FIELD_CHANGED']]),
  120. );
  121. $aValues = array_merge($aValuesDefault, $aValues);
  122. return $this->parseHtmlByName('entry-info.html', array(
  123. 'bx_repeat:info' => $aValues,
  124. ));
  125. }
  126. function entryAllActions ($sActionsEntity, $sActionsSocial)
  127. {
  128. if(empty($sActionsEntity) && empty($sActionsSocial))
  129. return '';
  130. return $this->parseHtmlByName('entry-all-actions.html', array (
  131. 'actions_entity' => $sActionsEntity,
  132. 'actions_social' => $sActionsSocial,
  133. ));
  134. }
  135. function entryAttachments ($aData, $aParams = array())
  136. {
  137. return $this->entryAttachmentsByStorage($this->getModule()->_oConfig->CNF['OBJECT_STORAGE'], $aData, $aParams);
  138. }
  139. function entryAttachmentsByStorage ($sStorage, $aData, $aParams = array())
  140. {
  141. if (!($a = $this->getAttachments($sStorage, $aData, $aParams)))
  142. return '';
  143. return $this->parseHtmlByName('attachments.html', array(
  144. 'bx_repeat:attachments' => $a,
  145. ));
  146. }
  147. protected function getUnitMetaItem($sName, $sContent, $aAttrs = array(), $sTemplate = 'unit_meta_item.html')
  148. {
  149. if(empty($sContent))
  150. return '';
  151. if(!is_array($aAttrs))
  152. $aAttrs = array();
  153. $aTags = array('span', 'a', 'button', 'sbutton', 'custom');
  154. $sTmplVarsClass = '';
  155. if(!empty($aAttrs['class'])) {
  156. $sTmplVarsClass = $aAttrs['class'];
  157. unset($aAttrs['class']);
  158. }
  159. $aTmplVarsAttrs = array();
  160. foreach($aAttrs as $sKey => $sValue)
  161. $aTmplVarsAttrs[] = array('key' => $sKey, 'value' => bx_html_attribute($sValue));
  162. $aTmplVars = array();
  163. foreach($aTags as $sTag) {
  164. $aTmplVarsTag = array();
  165. $bTmplVarsTag = $sTag == $sName;
  166. if($bTmplVarsTag)
  167. $aTmplVarsTag = array(
  168. 'class' => $sTmplVarsClass,
  169. 'content' => $sContent,
  170. 'bx_repeat:attrs' => $aTmplVarsAttrs
  171. );
  172. $aTmplVars['bx_if:' . $sTag] = array(
  173. 'condition' => $bTmplVarsTag,
  174. 'content' => $aTmplVarsTag
  175. );
  176. }
  177. return $this->parseHtmlByName($sTemplate, $aTmplVars);
  178. }
  179. protected function getAttachments ($sStorage, $aData, $aParams = array())
  180. {
  181. $CNF = &$this->getModule()->_oConfig->CNF;
  182. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  183. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  184. $oTranscoderPreview = isset($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) && $CNF['OBJECT_IMAGES_TRANSCODER_PICTURE'] ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) : null;
  185. $aTranscodersVideo = false;
  186. if (isset($CNF['OBJECT_VIDEOS_TRANSCODERS']) && $CNF['OBJECT_VIDEOS_TRANSCODERS'])
  187. $aTranscodersVideo = array (
  188. 'poster' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']),
  189. 'mp4' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']),
  190. 'webm' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['webm']),
  191. );
  192. $aGhostFiles = $oStorage->getGhosts ($this->getModule()->serviceGetContentOwnerProfileId($aData[$CNF['FIELD_ID']]), $aData[$CNF['FIELD_ID']]);
  193. if (!$aGhostFiles)
  194. return false;
  195. $sFilterField = isset($aParams['filter_field']) ? $aParams['filter_field'] : $CNF['FIELD_THUMB'];
  196. if(!empty($sFilterField) && isset($aData[$sFilterField]))
  197. foreach ($aGhostFiles as $k => $a) {
  198. // don't show thumbnail in attachments
  199. if ($a['id'] == $aData[$sFilterField])
  200. unset($aGhostFiles[$k]);
  201. }
  202. if(!$aGhostFiles)
  203. return false;
  204. $aAttachmnts = array();
  205. foreach ($aGhostFiles as $k => $a) {
  206. $isImage = $oTranscoder && (0 === strncmp('image/', $a['mime_type'], 6)) && $oTranscoder->isMimeTypeSupported($a['mime_type']); // preview for images, transcoder object for preview must be defined
  207. $isVideo = $aTranscodersVideo && (0 === strncmp('video/', $a['mime_type'], 6)) && $aTranscodersVideo['poster']->isMimeTypeSupported($a['mime_type']); // preview for videos, transcoder object for video must be defined
  208. $sUrlOriginal = $oStorage->getFileUrlById($a['id']);
  209. $sImgPopupId = 'bx-messages-atachment-popup-' . $a['id'];
  210. // images are displayed with preview and popup upon clicking
  211. $a['bx_if:image'] = array (
  212. 'condition' => $isImage,
  213. 'content' => array (
  214. 'url_original' => $isImage && $oTranscoderPreview ? $oTranscoderPreview->getFileUrl($a['id']) : $sUrlOriginal,
  215. 'attr_file_name' => bx_html_attribute($a['file_name']),
  216. 'popup_id' => $sImgPopupId,
  217. 'url_preview' => $isImage ? $oTranscoder->getFileUrl($a['id']) : '',
  218. 'popup' => BxTemplFunctions::getInstance()->transBox($sImgPopupId, '<img src="' . $sUrlOriginal . '" />', true, true),
  219. ),
  220. );
  221. // videos are displayed inline
  222. $a['bx_if:video'] = array (
  223. 'condition' => $isVideo,
  224. 'content' => array (
  225. 'video' => $isVideo && $aTranscodersVideo ? BxTemplFunctions::getInstance()->videoPlayer(
  226. $aTranscodersVideo['poster']->getFileUrl($a['id']),
  227. $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  228. $aTranscodersVideo['webm']->getFileUrl($a['id']),
  229. false, ''
  230. ) : '',
  231. ),
  232. );
  233. // non-images are displayed as text links to original file
  234. $a['bx_if:not_image'] = array (
  235. 'condition' => !$isImage && !$isVideo,
  236. 'content' => array (
  237. 'url_original' => $sUrlOriginal,
  238. 'attr_file_name' => bx_html_attribute($a['file_name']),
  239. 'file_name' => bx_process_output($a['file_name']),
  240. ),
  241. );
  242. $aAttachmnts[] = $a;
  243. }
  244. return $aAttachmnts;
  245. }
  246. }
  247. /** @} */