BxBaseModGeneralTemplate.php 12 KB

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