BxBaseModGeneralTemplate.php 11 KB

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