BxBaseModGeneralTemplate.php 12 KB

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