BxBaseModGeneralTemplate.php 11 KB

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