BxBaseModGeneralTemplate.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. $aMaskMarkers = array();
  26. if(is_array($mixedWrap)) {
  27. if(!empty($mixedWrap['mask']))
  28. $sMask = $mixedWrap['mask'];
  29. if(!empty($mixedWrap['mask_markers']) && is_array($mixedWrap['mask_markers']))
  30. $aMaskMarkers = $mixedWrap['mask_markers'];
  31. }
  32. $sBaseUri = $this->_oConfig->getBaseUri();
  33. $sJsClass = $this->_oConfig->getJsClass($sType);
  34. $sJsObject = $this->_oConfig->getJsObject($sType);
  35. $aParams = array_merge(array(
  36. 'sActionUri' => $sBaseUri,
  37. 'sActionUrl' => BX_DOL_URL_ROOT . $sBaseUri,
  38. 'sObjName' => $sJsObject,
  39. 'aHtmlIds' => array(),
  40. 'oRequestParams' => array()
  41. ), $aParams);
  42. $sContent = bx_replace_markers($sMask, array_merge(array(
  43. 'var' => 'var',
  44. 'object' => $sJsObject,
  45. 'class' => $sJsClass,
  46. 'params' => json_encode($aParams)
  47. ), $aMaskMarkers));
  48. return ($mixedWrap === true || (is_array($mixedWrap) && isset($mixedWrap['wrap']) && $mixedWrap['wrap'] === true)) ? $this->_wrapInTagJsCode($sContent) : $sContent;
  49. }
  50. public function getTitleAuto($aData, $iMaxLen = 20, $sEllipsisSign = '...')
  51. {
  52. $CNF = &$this->getModule()->_oConfig->CNF;
  53. if(isset($aData[$CNF['FIELD_TITLE']]))
  54. return $this->getTitle($aData);
  55. $sResult = $this->getText($aData);
  56. if(strlen($sResult) > 0 && $iMaxLen > 0)
  57. $sResult = strmaxtextlen($sResult, $iMaxLen, $sEllipsisSign);
  58. return $sResult;
  59. }
  60. public function getTitle($aData, $mixedProcessOutput = BX_DATA_TEXT)
  61. {
  62. $CNF = &$this->getModule()->_oConfig->CNF;
  63. if(!isset($aData[$CNF['FIELD_TITLE']]))
  64. return '';
  65. $sResult = $aData[$CNF['FIELD_TITLE']];
  66. if($mixedProcessOutput !== false && !empty($sResult))
  67. $sResult = bx_process_output($sResult, (int)$mixedProcessOutput);
  68. return $sResult;
  69. }
  70. public function getText($aData, $mixedProcessOutput = BX_DATA_HTML)
  71. {
  72. $CNF = &$this->getModule()->_oConfig->CNF;
  73. if(!isset($aData[$CNF['FIELD_TEXT']]))
  74. return '';
  75. $sResult = $aData[$CNF['FIELD_TEXT']];
  76. if($mixedProcessOutput !== false && !empty($sResult))
  77. $sResult = bx_process_output($sResult, (int)$mixedProcessOutput);
  78. return $sResult;
  79. }
  80. public function getProfileLink($mixedProfile)
  81. {
  82. if(!is_array($mixedProfile))
  83. $mixedProfile = $this->getModule()->getProfileInfo((int)$mixedProfile);
  84. return $this->getLink('link', array(
  85. 'href' => $mixedProfile['link'],
  86. 'title' => bx_html_attribute(!empty($mixedProfile['title']) ? $mixedProfile['title'] : $mixedProfile['name']),
  87. 'content' => $mixedProfile['name']
  88. ));
  89. }
  90. public function getLink($sTemplate, $aParams)
  91. {
  92. return $this->parseHtmlByName($sTemplate . '.html', array(
  93. 'href' => $aParams['href'],
  94. 'title' => $aParams['title'],
  95. 'content' => $aParams['content']
  96. ));
  97. }
  98. public function getTmplVarsText($aData)
  99. {
  100. $CNF = &$this->getModule()->_oConfig->CNF;
  101. $aVars = $aData;
  102. $aVars['entry_title'] = $this->getTitle($aData);
  103. $aVars['entry_text'] = $this->getText($aData);
  104. if (!empty($CNF['OBJECT_METATAGS'])) {
  105. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  106. // keywords
  107. if ($oMetatags->keywordsIsEnabled()) {
  108. $aFields = array_merge($oMetatags->metaFields($aData, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']), array('entry_title', 'entry_text'));
  109. foreach ($aFields as $sField)
  110. $aVars[$sField] = $oMetatags->keywordsParse($aData[$CNF['FIELD_ID']], $aVars[$sField]);
  111. }
  112. // mentions
  113. if ($oMetatags->mentionsIsEnabled()) {
  114. $aFields = array_merge($oMetatags->metaFields($aData, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'], true), array('entry_text'));
  115. foreach ($aFields as $sField)
  116. $aVars[$sField] = $oMetatags->mentionsParse($aData[$CNF['FIELD_ID']], $aVars[$sField]);
  117. }
  118. // location
  119. $aVars['location'] = $oMetatags->locationsIsEnabled() ? $oMetatags->locationsString($aData[$CNF['FIELD_ID']]) : '';
  120. }
  121. unset($aVars['recipients']);
  122. return $aVars;
  123. }
  124. public function entryBreadcrumb($aContentInfo, $aTmplVarsItems = array())
  125. {
  126. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  127. return $this->parseHtmlByName('breadcrumb.html', array(
  128. 'url_home' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']),
  129. 'icon_home' => $CNF['ICON'],
  130. 'bx_repeat:items' => $aTmplVarsItems
  131. ));
  132. }
  133. public function entryText ($aData, $sTemplateName = 'entry-text.html')
  134. {
  135. $aVars = $this->getTmplVarsText($aData);
  136. if (empty($aVars['entry_text']))
  137. return false;
  138. return $this->parseHtmlByName($sTemplateName, $aVars);
  139. }
  140. public function entryLocation ($iContentId)
  141. {
  142. $CNF = &$this->getModule()->_oConfig->CNF;
  143. if (empty($CNF['OBJECT_METATAGS']))
  144. return '';
  145. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  146. if (!($sLocationString = $oMetatags->locationsString($iContentId)))
  147. return '';
  148. $aVars = array (
  149. 'location' => $sLocationString
  150. );
  151. return $this->parseHtmlByName('entry-location.html', $aVars);
  152. }
  153. public function entryInfo($aData, $aValues = array())
  154. {
  155. $CNF = $this->_oConfig->CNF;
  156. $aValuesDefault = array();
  157. if (isset($aData[$CNF['FIELD_ADDED']]))
  158. $aValuesDefault[] = array(
  159. 'title' => _t('_sys_txt_field_created'),
  160. 'value' => bx_time_js($aData[$CNF['FIELD_ADDED']]),
  161. );
  162. if (isset($aData[$CNF['FIELD_CHANGED']]))
  163. $aValuesDefault[] = array(
  164. 'title' => _t('_sys_txt_field_updated'),
  165. 'value' => bx_time_js($aData[$CNF['FIELD_CHANGED']]),
  166. );
  167. $aValues = array_merge($aValuesDefault, $aValues);
  168. return $this->parseHtmlByName('entry-info.html', array(
  169. 'bx_repeat:info' => $aValues,
  170. ));
  171. }
  172. function entryAllActions($sActions)
  173. {
  174. if(empty($sActions))
  175. return '';
  176. return $this->parseHtmlByName('entry-all-actions.html', array (
  177. 'actions' => $sActions
  178. ));
  179. }
  180. function entryAttachments ($aData, $aParams = array())
  181. {
  182. return $this->entryAttachmentsByStorage($this->getModule()->_oConfig->CNF['OBJECT_STORAGE'], $aData, $aParams);
  183. }
  184. function entryAttachmentsByStorage ($mixedStorage, $aData, $aParams = array())
  185. {
  186. if(!is_array($mixedStorage))
  187. $mixedStorage = array($mixedStorage);
  188. $aResult = array();
  189. foreach($mixedStorage as $sStorage) {
  190. $aAttachments = $this->getAttachments($sStorage, $aData, $aParams);
  191. if(empty($aAttachments) || !is_array($aAttachments))
  192. continue;
  193. $aResult = array_merge($aResult, $aAttachments);
  194. }
  195. if(empty($aResult) || !is_array($aResult))
  196. return '';
  197. $this->addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flickity/|flickity.css');
  198. $this->addJs('flickity/flickity.pkgd.min.js');
  199. return $this->parseHtmlByName('attachments.html', array(
  200. 'bx_repeat:attachments' => $aResult,
  201. ));
  202. }
  203. protected function getAttachmentsImagesTranscoders ($sStorage = '')
  204. {
  205. $CNF = &$this->getModule()->_oConfig->CNF;
  206. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  207. $oTranscoderPreview = isset($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) && $CNF['OBJECT_IMAGES_TRANSCODER_PICTURE'] ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) : null;
  208. return array($oTranscoder, $oTranscoderPreview);
  209. }
  210. protected function getAttachmentsVideoTranscoders ($sStorage = '')
  211. {
  212. $CNF = &$this->getModule()->_oConfig->CNF;
  213. if (isset($CNF['OBJECT_VIDEOS_TRANSCODERS']) && $CNF['OBJECT_VIDEOS_TRANSCODERS'])
  214. return array (
  215. 'poster' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']),
  216. 'mp4' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']),
  217. 'mp4_hd' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4_hd']),
  218. );
  219. return false;
  220. }
  221. protected function getAttachments ($sStorage, $aData, $aParams = array())
  222. {
  223. $CNF = &$this->getModule()->_oConfig->CNF;
  224. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  225. list($oTranscoder, $oTranscoderPreview) = $this->getAttachmentsImagesTranscoders($sStorage);
  226. $aTranscodersVideo = $this->getAttachmentsVideoTranscoders($sStorage);
  227. $aGhostFiles = $oStorage->getGhosts ($this->getModule()->serviceGetContentOwnerProfileId($aData[$CNF['FIELD_ID']]), $aData[$CNF['FIELD_ID']]);
  228. if (!$aGhostFiles)
  229. return false;
  230. $sFilterField = isset($aParams['filter_field']) ? $aParams['filter_field'] : $CNF['FIELD_THUMB'];
  231. if(!empty($sFilterField) && isset($aData[$sFilterField]))
  232. foreach ($aGhostFiles as $k => $a) {
  233. // don't show thumbnail in attachments
  234. if ($a['id'] == $aData[$sFilterField])
  235. unset($aGhostFiles[$k]);
  236. }
  237. if(!$aGhostFiles)
  238. return false;
  239. $aAttachmnts = array();
  240. foreach ($aGhostFiles as $k => $a) {
  241. $isImage = $oTranscoder && (0 === strncmp('image/', $a['mime_type'], 6)) && $oTranscoder->isMimeTypeSupported($a['mime_type']); // preview for images, transcoder object for preview must be defined
  242. $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
  243. $sUrlOriginal = $oStorage->getFileUrlById($a['id']);
  244. $sImgPopupId = 'bx-messages-atachment-popup-' . $a['id'];
  245. // images are displayed with preview and popup upon clicking
  246. $a['bx_if:image'] = array (
  247. 'condition' => $isImage,
  248. 'content' => array (
  249. 'url_original' => $isImage && $oTranscoderPreview ? $oTranscoderPreview->getFileUrl($a['id']) : $sUrlOriginal,
  250. 'attr_file_name' => bx_html_attribute($a['file_name']),
  251. 'popup_id' => $sImgPopupId,
  252. 'url_preview' => $isImage ? $oTranscoder->getFileUrl($a['id']) : '',
  253. 'popup' => BxTemplFunctions::getInstance()->transBox($sImgPopupId, '<img src="' . $sUrlOriginal . '" />', true, true),
  254. ),
  255. );
  256. $sVideoUrlHd = '';
  257. if($isVideo) {
  258. $sVideoUrl = $oStorage->getFileUrlById($a['id']);
  259. $aVideoSize = $aTranscodersVideo['mp4_hd']->getVideoSize($sVideoUrl);
  260. if(!empty($aVideoSize) && is_array($aVideoSize) && (int)$aVideoSize['h'] >= 720)
  261. $sVideoUrlHd = $aTranscodersVideo['mp4_hd']->getFileUrl($a['id']);
  262. }
  263. // videos are displayed inline
  264. $a['bx_if:video'] = array (
  265. 'condition' => $isVideo,
  266. 'content' => array (
  267. 'video' => $isVideo && $aTranscodersVideo ? BxTemplFunctions::getInstance()->videoPlayer(
  268. $aTranscodersVideo['poster']->getFileUrl($a['id']),
  269. $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  270. $sVideoUrlHd,
  271. false, ''
  272. ) : '',
  273. ),
  274. );
  275. // non-images are displayed as text links to original file
  276. $a['bx_if:not_image'] = array (
  277. 'condition' => !$isImage && !$isVideo,
  278. 'content' => array (
  279. 'url_original' => $sUrlOriginal,
  280. 'attr_file_name' => bx_html_attribute($a['file_name']),
  281. 'file_name' => bx_process_output($a['file_name']),
  282. ),
  283. );
  284. $aAttachmnts[] = $a;
  285. }
  286. return $aAttachmnts;
  287. }
  288. public function embedVideo($iFileId)
  289. {
  290. // general checks
  291. $oPlayer = BxDolPlayer::getObjectInstance();
  292. $CNF = $this->getModule()->_oConfig->CNF;
  293. $sStorage = isset($CNF['OBJECT_STORAGE_VIDEOS']) ? $CNF['OBJECT_STORAGE_VIDEOS'] : false;
  294. if (!$oPlayer || !$sStorage || !($oStorage = BxDolStorage::getObjectInstance($sStorage))) {
  295. $this->displayErrorOccured('', BX_PAGE_EMBED);
  296. exit;
  297. }
  298. // privacy check
  299. $aGhost = $oStorage->getGhost($iFileId);
  300. $aContentInfo = $aGhost && $aGhost['content_id'] ? $this->getModule()->_oDb->getContentInfoById($aGhost['content_id']) : false;
  301. if (!$aGhost || ($aGhost['content_id'] && !$aContentInfo)) {
  302. $this->displayPageNotFound('', BX_PAGE_EMBED);
  303. exit;
  304. }
  305. if (!$aGhost['content_id']) {
  306. // if file is not associated with content yet, then only admin and owner can view it
  307. if (!isAdmin() && CHECK_ACTION_RESULT_ALLOWED !== $this->getModule()->checkAllowedEditAnyEntry() && $aGhost['profile_id'] != bx_get_logged_profile_id()) {
  308. $this->displayAccessDenied('', BX_PAGE_EMBED);
  309. exit;
  310. }
  311. }
  312. else {
  313. // if file is associated with content, then check entry privacy
  314. if (CHECK_ACTION_RESULT_ALLOWED !== $this->getModule()->checkAllowedView($aContentInfo)) {
  315. $this->displayAccessDenied('', BX_PAGE_EMBED);
  316. exit;
  317. }
  318. }
  319. // get file info
  320. if (!($a = $oStorage->getFile($iFileId))) {
  321. $this->displayPageNotFound('', BX_PAGE_EMBED);
  322. exit;
  323. }
  324. // check if file is really video
  325. $aTranscodersVideo = $this->getAttachmentsVideoTranscoders($sStorage);
  326. if (!$aTranscodersVideo || (0 !== strncmp('video/', $a['mime_type'], 6)) || !$aTranscodersVideo['poster']->isMimeTypeSupported($a['mime_type'])) {
  327. $this->displayErrorOccured('', BX_PAGE_EMBED);
  328. exit;
  329. }
  330. // check HD video version
  331. $sVideoUrlHd = '';
  332. $sVideoUrl = $oStorage->getFileUrlById($a['id']);
  333. $aVideoSize = $aTranscodersVideo['mp4_hd']->getVideoSize($sVideoUrl);
  334. if (!empty($aVideoSize) && is_array($aVideoSize) && (int)$aVideoSize['h'] >= 720)
  335. $sVideoUrlHd = $aTranscodersVideo['mp4_hd']->getFileUrl($a['id']);
  336. // generate player code
  337. $sCode = $oPlayer->getCodeVideo (BX_PLAYER_EMBED, array(
  338. 'poster' => $aTranscodersVideo['poster']->getFileUrl($a['id']),
  339. 'mp4' => array(
  340. 'sd' => $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  341. 'hd' => $sVideoUrlHd
  342. ),
  343. ));
  344. // display page
  345. $oTemplate = BxDolTemplate::getInstance();
  346. $oTemplate->setPageNameIndex (BX_PAGE_EMBED);
  347. $oTemplate->setPageHeader ($a['file_name']);
  348. $oTemplate->setPageContent ('page_main_code', $sCode);
  349. $oTemplate->getPageCode();
  350. exit;
  351. }
  352. public function addCssJs()
  353. {
  354. $this->addCss('main.css');
  355. }
  356. }
  357. /** @} */