BxBaseModGeneralTemplate.php 11 KB

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