BxBaseModGeneralTemplate.php 12 KB

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