BxBaseModGeneralTemplate.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. function getContextAddon ($aData, $oProfile)
  99. {
  100. $CNF = &$this->getModule()->_oConfig->CNF;
  101. $sUrl = 'page.php?i=' . $CNF['URI_ENTRIES_BY_CONTEXT'] . '&profile_id=' . $oProfile->id();
  102. $sUrl = BxDolPermalinks::getInstance()->permalink($sUrl);
  103. return _t($CNF['T']['txt_all_entries_in'], $sUrl, $oProfile->getDisplayName(), $this->getModule()->_oDb->getEntriesNumByContext($oProfile->id()));
  104. }
  105. function getContextDesc ($aData)
  106. {
  107. return '';
  108. }
  109. public function getTmplVarsText($aData)
  110. {
  111. $CNF = &$this->getModule()->_oConfig->CNF;
  112. $aVars = $aData;
  113. $aVars['entry_title'] = $this->getTitle($aData);
  114. $aVars['entry_text'] = $this->getText($aData);
  115. $aVars['badges'] = $this->getModule()->serviceGetBadges($aData[$CNF['FIELD_ID']]);
  116. if (!empty($CNF['OBJECT_METATAGS'])) {
  117. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  118. // keywords
  119. if ($oMetatags->keywordsIsEnabled()) {
  120. $aFields = array_merge($oMetatags->metaFields($aData, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']), array('entry_title', 'entry_text'));
  121. foreach ($aFields as $sField)
  122. $aVars[$sField] = $oMetatags->keywordsParse($aData[$CNF['FIELD_ID']], $aVars[$sField]);
  123. }
  124. // mentions
  125. if ($oMetatags->mentionsIsEnabled()) {
  126. $aFields = array_merge($oMetatags->metaFields($aData, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'], true), array('entry_text'));
  127. foreach ($aFields as $sField)
  128. $aVars[$sField] = $oMetatags->mentionsParse($aData[$CNF['FIELD_ID']], $aVars[$sField]);
  129. }
  130. // location
  131. $aVars['location'] = $oMetatags->locationsIsEnabled() ? $oMetatags->locationsString($aData[$CNF['FIELD_ID']]) : '';
  132. }
  133. unset($aVars['recipients']);
  134. return $aVars;
  135. }
  136. public function entryBreadcrumb($aContentInfo, $aTmplVarsItems = array())
  137. {
  138. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  139. return $this->parseHtmlByName('breadcrumb.html', array(
  140. 'url_home' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']),
  141. 'icon_home' => $CNF['ICON'],
  142. 'bx_repeat:items' => $aTmplVarsItems
  143. ));
  144. }
  145. public function entryText ($aData, $sTemplateName = 'entry-text.html')
  146. {
  147. $aVars = $this->getTmplVarsText($aData);
  148. if (empty($aVars['entry_text']))
  149. return false;
  150. return $this->parseHtmlByName($sTemplateName, $aVars);
  151. }
  152. public function entryLocation ($iContentId)
  153. {
  154. $CNF = &$this->getModule()->_oConfig->CNF;
  155. if (empty($CNF['OBJECT_METATAGS']))
  156. return '';
  157. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  158. if (!($sLocationString = $oMetatags->locationsString($iContentId)))
  159. return '';
  160. $aVars = array (
  161. 'location' => $sLocationString
  162. );
  163. return $this->parseHtmlByName('entry-location.html', $aVars);
  164. }
  165. public function entryInfo($aData, $aValues = array())
  166. {
  167. $CNF = $this->_oConfig->CNF;
  168. $aValuesDefault = array();
  169. if (isset($aData[$CNF['FIELD_ADDED']]))
  170. $aValuesDefault[] = array(
  171. 'title' => _t('_sys_txt_field_created'),
  172. 'value' => bx_time_js($aData[$CNF['FIELD_ADDED']]),
  173. );
  174. if (isset($aData[$CNF['FIELD_CHANGED']]))
  175. $aValuesDefault[] = array(
  176. 'title' => _t('_sys_txt_field_updated'),
  177. 'value' => bx_time_js($aData[$CNF['FIELD_CHANGED']]),
  178. );
  179. $aValues = array_merge($aValuesDefault, $aValues);
  180. return $this->parseHtmlByName('entry-info.html', array(
  181. 'bx_repeat:info' => $aValues,
  182. ));
  183. }
  184. function entryAllActions($sActions)
  185. {
  186. if(empty($sActions))
  187. return '';
  188. return $this->parseHtmlByName('entry-all-actions.html', array (
  189. 'actions' => $sActions
  190. ));
  191. }
  192. function entryAttachments ($aData, $aParams = array())
  193. {
  194. return $this->entryAttachmentsByStorage($this->getModule()->_oConfig->CNF['OBJECT_STORAGE'], $aData, $aParams);
  195. }
  196. function entryAttachmentsByStorage ($mixedStorage, $aData, $aParams = array())
  197. {
  198. if(!is_array($mixedStorage))
  199. $mixedStorage = array($mixedStorage);
  200. $aResult = array();
  201. foreach($mixedStorage as $sStorage) {
  202. $aAttachments = $this->getAttachments($sStorage, $aData, $aParams);
  203. if(empty($aAttachments) || !is_array($aAttachments))
  204. continue;
  205. $aResult = array_merge($aResult, $aAttachments);
  206. }
  207. if(empty($aResult) || !is_array($aResult))
  208. return '';
  209. $this->addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flickity/|flickity.css');
  210. $this->addJs('flickity/flickity.pkgd.min.js');
  211. return $this->parseHtmlByName('attachments.html', array(
  212. 'bx_repeat:attachments' => $aResult,
  213. ));
  214. }
  215. function entryContext ($aData, $iProfileId = false, $sFuncContextDesc = 'getContextDesc', $sTemplateName = 'context.html', $sFuncContextAddon = 'getContextAddon')
  216. {
  217. $CNF = &$this->getModule()->_oConfig->CNF;
  218. $iContextId = $aData[$CNF['FIELD_ALLOW_VIEW_TO']];
  219. if ($iContextId >= 0)
  220. return '';
  221. $iProfileId = - $iContextId;
  222. $oProfile = BxDolProfile::getInstance($iProfileId);
  223. if (!$oProfile)
  224. $oProfile = BxDolProfileUndefined::getInstance();
  225. if (!$oProfile)
  226. return '';
  227. $sName = $oProfile->getDisplayName();
  228. $sAddon = $sFuncContextAddon ? $this->$sFuncContextAddon($aData, $oProfile) : '';
  229. $aVars = array (
  230. 'author_url' => $oProfile->getUrl(),
  231. 'author_thumb_url' => $oProfile->getThumb(),
  232. 'author_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  233. 'author_title' => $sName,
  234. 'author_title_attr' => bx_html_attribute($sName),
  235. 'author_desc' => $sFuncContextDesc ? $this->$sFuncContextDesc($aData) : '',
  236. 'bx_if:addon' => array (
  237. 'condition' => (bool)$sAddon,
  238. 'content' => array (
  239. 'content' => $sAddon,
  240. ),
  241. ),
  242. );
  243. return $this->parseHtmlByName($sTemplateName, $aVars);
  244. }
  245. protected function getAttachmentsImagesTranscoders ($sStorage = '')
  246. {
  247. $CNF = &$this->getModule()->_oConfig->CNF;
  248. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  249. $oTranscoderPreview = isset($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) && $CNF['OBJECT_IMAGES_TRANSCODER_PICTURE'] ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) : null;
  250. return array($oTranscoder, $oTranscoderPreview);
  251. }
  252. protected function getAttachmentsVideoTranscoders ($sStorage = '')
  253. {
  254. $CNF = &$this->getModule()->_oConfig->CNF;
  255. if (isset($CNF['OBJECT_VIDEOS_TRANSCODERS']) && $CNF['OBJECT_VIDEOS_TRANSCODERS'])
  256. return array (
  257. 'poster' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']),
  258. 'mp4' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']),
  259. 'mp4_hd' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4_hd']),
  260. );
  261. return false;
  262. }
  263. protected function getAttachments ($sStorage, $aData, $aParams = array())
  264. {
  265. $CNF = &$this->getModule()->_oConfig->CNF;
  266. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  267. list($oTranscoder, $oTranscoderPreview) = $this->getAttachmentsImagesTranscoders($sStorage);
  268. $aTranscodersVideo = $this->getAttachmentsVideoTranscoders($sStorage);
  269. $aGhostFiles = $oStorage->getGhosts ($this->getModule()->serviceGetContentOwnerProfileId($aData[$CNF['FIELD_ID']]), $aData[$CNF['FIELD_ID']]);
  270. if (!$aGhostFiles)
  271. return false;
  272. $sFilterField = isset($aParams['filter_field']) ? $aParams['filter_field'] : $CNF['FIELD_THUMB'];
  273. if(!empty($sFilterField) && isset($aData[$sFilterField]))
  274. foreach ($aGhostFiles as $k => $a) {
  275. // don't show thumbnail in attachments
  276. if ($a['id'] == $aData[$sFilterField])
  277. unset($aGhostFiles[$k]);
  278. }
  279. if(!$aGhostFiles)
  280. return false;
  281. $aAttachmnts = array();
  282. foreach ($aGhostFiles as $k => $a) {
  283. $isImage = $oTranscoder && (0 === strncmp('image/', $a['mime_type'], 6)) && $oTranscoder->isMimeTypeSupported($a['mime_type']); // preview for images, transcoder object for preview must be defined
  284. $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
  285. $sUrlOriginal = $oStorage->getFileUrlById($a['id']);
  286. $sImgPopupId = 'bx-messages-atachment-popup-' . $a['id'];
  287. // images are displayed with preview and popup upon clicking
  288. $a['bx_if:image'] = array (
  289. 'condition' => $isImage,
  290. 'content' => array (
  291. 'url_original' => $isImage && $oTranscoderPreview ? $oTranscoderPreview->getFileUrl($a['id']) : $sUrlOriginal,
  292. 'attr_file_name' => bx_html_attribute($a['file_name']),
  293. 'popup_id' => $sImgPopupId,
  294. 'url_preview' => $isImage ? $oTranscoder->getFileUrl($a['id']) : '',
  295. 'popup' => BxTemplFunctions::getInstance()->transBox($sImgPopupId, '<img src="' . $sUrlOriginal . '" />', true, true),
  296. ),
  297. );
  298. $sVideoUrlHd = '';
  299. if($isVideo) {
  300. $sVideoUrl = $oStorage->getFileUrlById($a['id']);
  301. $aVideoSize = $aTranscodersVideo['mp4_hd']->getVideoSize($sVideoUrl);
  302. if(!empty($aVideoSize) && is_array($aVideoSize) && (int)$aVideoSize['h'] >= 720)
  303. $sVideoUrlHd = $aTranscodersVideo['mp4_hd']->getFileUrl($a['id']);
  304. }
  305. // videos are displayed inline
  306. $a['bx_if:video'] = array (
  307. 'condition' => $isVideo,
  308. 'content' => array (
  309. 'video' => $isVideo && $aTranscodersVideo ? BxTemplFunctions::getInstance()->videoPlayer(
  310. $aTranscodersVideo['poster']->getFileUrl($a['id']),
  311. $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  312. $sVideoUrlHd,
  313. false, ''
  314. ) : '',
  315. ),
  316. );
  317. // non-images are displayed as text links to original file
  318. $a['bx_if:not_image'] = array (
  319. 'condition' => !$isImage && !$isVideo,
  320. 'content' => array (
  321. 'url_original' => $sUrlOriginal,
  322. 'attr_file_name' => bx_html_attribute($a['file_name']),
  323. 'file_name' => bx_process_output($a['file_name']),
  324. ),
  325. );
  326. $aAttachmnts[] = $a;
  327. }
  328. return $aAttachmnts;
  329. }
  330. public function embedVideo($iFileId)
  331. {
  332. // general checks
  333. $oPlayer = BxDolPlayer::getObjectInstance();
  334. $CNF = $this->getModule()->_oConfig->CNF;
  335. $sStorage = isset($CNF['OBJECT_STORAGE_VIDEOS']) ? $CNF['OBJECT_STORAGE_VIDEOS'] : false;
  336. if (!$oPlayer || !$sStorage || !($oStorage = BxDolStorage::getObjectInstance($sStorage))) {
  337. $this->displayErrorOccured('', BX_PAGE_EMBED);
  338. exit;
  339. }
  340. // privacy check
  341. $aGhost = $oStorage->getGhost($iFileId);
  342. $aContentInfo = $aGhost && $aGhost['content_id'] ? $this->getModule()->_oDb->getContentInfoById($aGhost['content_id']) : false;
  343. if (!$aGhost || ($aGhost['content_id'] && !$aContentInfo)) {
  344. $this->displayPageNotFound('', BX_PAGE_EMBED);
  345. exit;
  346. }
  347. if (!$aGhost['content_id']) {
  348. // if file is not associated with content yet, then only admin and owner can view it
  349. if (!isAdmin() && CHECK_ACTION_RESULT_ALLOWED !== $this->getModule()->checkAllowedEditAnyEntry() && $aGhost['profile_id'] != bx_get_logged_profile_id()) {
  350. $this->displayAccessDenied('', BX_PAGE_EMBED);
  351. exit;
  352. }
  353. }
  354. else {
  355. // if file is associated with content, then check entry privacy
  356. if (CHECK_ACTION_RESULT_ALLOWED !== $this->getModule()->checkAllowedView($aContentInfo)) {
  357. $this->displayAccessDenied('', BX_PAGE_EMBED);
  358. exit;
  359. }
  360. }
  361. // get file info
  362. if (!($a = $oStorage->getFile($iFileId))) {
  363. $this->displayPageNotFound('', BX_PAGE_EMBED);
  364. exit;
  365. }
  366. // check if file is really video
  367. $aTranscodersVideo = $this->getAttachmentsVideoTranscoders($sStorage);
  368. if (!$aTranscodersVideo || (0 !== strncmp('video/', $a['mime_type'], 6)) || !$aTranscodersVideo['poster']->isMimeTypeSupported($a['mime_type'])) {
  369. $this->displayErrorOccured('', BX_PAGE_EMBED);
  370. exit;
  371. }
  372. // check HD video version
  373. $sVideoUrlHd = '';
  374. $sVideoUrl = $oStorage->getFileUrlById($a['id']);
  375. $aVideoSize = $aTranscodersVideo['mp4_hd']->getVideoSize($sVideoUrl);
  376. if (!empty($aVideoSize) && is_array($aVideoSize) && (int)$aVideoSize['h'] >= 720)
  377. $sVideoUrlHd = $aTranscodersVideo['mp4_hd']->getFileUrl($a['id']);
  378. // generate player code
  379. $sCode = $oPlayer->getCodeVideo (BX_PLAYER_EMBED, array(
  380. 'poster' => $aTranscodersVideo['poster']->getFileUrl($a['id']),
  381. 'mp4' => array(
  382. 'sd' => $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  383. 'hd' => $sVideoUrlHd
  384. ),
  385. ));
  386. // display page
  387. $oTemplate = BxDolTemplate::getInstance();
  388. $oTemplate->setPageNameIndex (BX_PAGE_EMBED);
  389. $oTemplate->setPageHeader ($a['file_name']);
  390. $oTemplate->setPageContent ('page_main_code', $sCode);
  391. $oTemplate->getPageCode();
  392. exit;
  393. }
  394. public function addCssJs()
  395. {
  396. $this->addCss('main.css');
  397. }
  398. }
  399. /** @} */