BxBaseModGeneralTemplate.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. $sJsClass = $this->_oConfig->getJsClass($sType);
  33. if(!empty($aParams['js_class'])) {
  34. $sJsClass = $aParams['js_class'];
  35. unset($aParams['js_class']);
  36. }
  37. $sJsObject = $this->_oConfig->getJsObject($sType);
  38. if(!empty($aParams['js_object'])) {
  39. $sJsObject = $aParams['js_object'];
  40. unset($aParams['js_object']);
  41. }
  42. $sBaseUri = $this->_oConfig->getBaseUri();
  43. $aParams = array_merge(array(
  44. 'sActionUri' => $sBaseUri,
  45. 'sActionUrl' => BX_DOL_URL_ROOT . $sBaseUri,
  46. 'sObjName' => $sJsObject,
  47. 'aHtmlIds' => array(),
  48. 'oRequestParams' => array()
  49. ), $aParams);
  50. $sContent = bx_replace_markers($sMask, array_merge(array(
  51. 'var' => 'var',
  52. 'object' => $sJsObject,
  53. 'class' => $sJsClass,
  54. 'params' => json_encode($aParams)
  55. ), $aMaskMarkers));
  56. return ($mixedWrap === true || (is_array($mixedWrap) && isset($mixedWrap['wrap']) && $mixedWrap['wrap'] === true)) ? $this->_wrapInTagJsCode($sContent) : $sContent;
  57. }
  58. public function getTitleAuto($aData, $iMaxLen = 20, $sEllipsisSign = '...')
  59. {
  60. $CNF = &$this->getModule()->_oConfig->CNF;
  61. if(isset($aData[$CNF['FIELD_TITLE']]))
  62. return $this->getTitle($aData);
  63. $sResult = $this->getText($aData);
  64. if(strlen($sResult) > 0 && $iMaxLen > 0)
  65. $sResult = strmaxtextlen($sResult, $iMaxLen, $sEllipsisSign);
  66. return $sResult;
  67. }
  68. public function getTitle($aData, $mixedProcessOutput = BX_DATA_TEXT)
  69. {
  70. $CNF = &$this->getModule()->_oConfig->CNF;
  71. if(!isset($aData[$CNF['FIELD_TITLE']]))
  72. return '';
  73. $sResult = $aData[$CNF['FIELD_TITLE']];
  74. if($mixedProcessOutput !== false && !empty($sResult))
  75. $sResult = bx_process_output($sResult, (int)$mixedProcessOutput);
  76. return $sResult;
  77. }
  78. public function getText($aData, $mixedProcessOutput = BX_DATA_HTML)
  79. {
  80. $CNF = &$this->getModule()->_oConfig->CNF;
  81. if(!isset($aData[$CNF['FIELD_TEXT']]))
  82. return '';
  83. $sResult = $aData[$CNF['FIELD_TEXT']];
  84. if($mixedProcessOutput !== false && !empty($sResult))
  85. $sResult = bx_process_output($sResult, (int)$mixedProcessOutput);
  86. return $sResult;
  87. }
  88. protected function getSummary($aData, $sTitle = '', $sText = '', $sUrl = '')
  89. {
  90. $CNF = &$this->getModule()->_oConfig->CNF;
  91. if(empty($CNF['PARAM_CHARS_SUMMARY']))
  92. return '';
  93. // get summary
  94. $sLinkMore = ' <a title="' . bx_html_attribute(_t('_sys_read_more', $sTitle)) . '" href="' . $sUrl . '"><i class="sys-icon ellipsis-h"></i></a>';
  95. return strmaxtextlen($sText, (int)getParam($CNF['PARAM_CHARS_SUMMARY']), $sLinkMore);
  96. }
  97. public function getProfileLink($mixedProfile)
  98. {
  99. if(!is_array($mixedProfile))
  100. $mixedProfile = $this->getModule()->getProfileInfo((int)$mixedProfile);
  101. return $this->getLink('link', array(
  102. 'href' => $mixedProfile['link'],
  103. 'title' => bx_html_attribute(!empty($mixedProfile['title']) ? $mixedProfile['title'] : $mixedProfile['name']),
  104. 'content' => $mixedProfile['name']
  105. ));
  106. }
  107. public function getLink($sTemplate, $aParams)
  108. {
  109. return $this->parseHtmlByName($sTemplate . '.html', array(
  110. 'href' => $aParams['href'],
  111. 'title' => $aParams['title'],
  112. 'content' => $aParams['content']
  113. ));
  114. }
  115. function getContextAddon ($aData, $oProfile)
  116. {
  117. $CNF = &$this->getModule()->_oConfig->CNF;
  118. $sUrl = 'page.php?i=' . $CNF['URI_ENTRIES_BY_CONTEXT'] . '&profile_id=' . $oProfile->id();
  119. $bActAsProfile = BxDolService::call($oProfile->getModule(), 'act_as_profile');
  120. if ($bActAsProfile)
  121. $sUrl = 'page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . $oProfile->id();
  122. $sUrl = BxDolPermalinks::getInstance()->permalink($sUrl);
  123. return _t($CNF['T']['txt_all_entries_in'], $sUrl, $oProfile->getDisplayName(), $this->getModule()->_oDb->getEntriesNumByContext($oProfile->id()));
  124. }
  125. function getContextDesc ($aData)
  126. {
  127. return '';
  128. }
  129. public function getTmplVarsText($aData)
  130. {
  131. $CNF = &$this->getModule()->_oConfig->CNF;
  132. $aVars = $aData;
  133. $aVars['entry_title'] = $this->getTitle($aData);
  134. $aVars['entry_text'] = $this->getText($aData);
  135. $aVars['badges'] = $this->getModule()->serviceGetBadges($aData[$CNF['FIELD_ID']]);
  136. if (!empty($CNF['OBJECT_METATAGS'])) {
  137. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  138. // keywords
  139. if ($oMetatags->keywordsIsEnabled()) {
  140. $aFields = array_merge($oMetatags->metaFields($aData, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']), array('entry_title', 'entry_text'));
  141. foreach ($aFields as $sField)
  142. $aVars[$sField] = $oMetatags->keywordsParse($aData[$CNF['FIELD_ID']], $aVars[$sField]);
  143. }
  144. // mentions
  145. if ($oMetatags->mentionsIsEnabled()) {
  146. $aFields = array_merge($oMetatags->metaFields($aData, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'], true), array('entry_text'));
  147. foreach ($aFields as $sField)
  148. $aVars[$sField] = $oMetatags->mentionsParse($aData[$CNF['FIELD_ID']], $aVars[$sField]);
  149. }
  150. // location
  151. $aVars['location'] = $oMetatags->locationsIsEnabled() ? $oMetatags->locationsString($aData[$CNF['FIELD_ID']]) : '';
  152. }
  153. unset($aVars['recipients']);
  154. return $aVars;
  155. }
  156. public function entryBreadcrumb($aContentInfo, $aTmplVarsItems = array())
  157. {
  158. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  159. return $this->parseHtmlByName('breadcrumb.html', array(
  160. 'url_home' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']),
  161. 'icon_home' => $CNF['ICON'],
  162. 'bx_repeat:items' => $aTmplVarsItems
  163. ));
  164. }
  165. public function entryText ($aData, $sTemplateName = 'entry-text.html')
  166. {
  167. $aVars = $this->getTmplVarsText($aData);
  168. if (empty($aVars['entry_text']))
  169. return false;
  170. return $this->parseHtmlByName($sTemplateName, $aVars);
  171. }
  172. public function entryLocation ($iContentId)
  173. {
  174. $CNF = &$this->getModule()->_oConfig->CNF;
  175. if (empty($CNF['OBJECT_METATAGS']))
  176. return '';
  177. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  178. if (!($sLocationString = $oMetatags->locationsString($iContentId)))
  179. return '';
  180. $aVars = array (
  181. 'location' => $sLocationString
  182. );
  183. return $this->parseHtmlByName('entry-location.html', $aVars);
  184. }
  185. public function entryInfo($aData, $aValues = array())
  186. {
  187. $CNF = $this->_oConfig->CNF;
  188. $aValuesDefault = array();
  189. if (isset($aData[$CNF['FIELD_ADDED']]))
  190. $aValuesDefault[] = array(
  191. 'title' => _t('_sys_txt_field_created'),
  192. 'value' => bx_time_js($aData[$CNF['FIELD_ADDED']]),
  193. );
  194. if (isset($aData[$CNF['FIELD_CHANGED']]))
  195. $aValuesDefault[] = array(
  196. 'title' => _t('_sys_txt_field_updated'),
  197. 'value' => bx_time_js($aData[$CNF['FIELD_CHANGED']]),
  198. );
  199. $aValues = array_merge($aValuesDefault, $aValues);
  200. return $this->parseHtmlByName('entry-info.html', array(
  201. 'bx_repeat:info' => $aValues,
  202. ));
  203. }
  204. public function getFavoriteList($oProfile, $iStart, $iPerPage, $aParams)
  205. {
  206. $CNF = $this->_oConfig->CNF;
  207. $bEmptyMessage = false;
  208. if(isset($aParams['empty_message'])) {
  209. $bEmptyMessage = (bool)$aParams['empty_message'];
  210. unset($aParams['empty_message']);
  211. }
  212. $oFavorite = BxDolFavorite::getObjectInstance($CNF['OBJECT_FAVORITES'], 0, true);
  213. $aListsData = $oFavorite->getQueryObject()->getList(array('type' => 'active', 'author_id' => $oProfile->id(), 'need_default' => true, 'start' => $iStart, 'limit' => $iPerPage + 1));
  214. $iNum = count($aListsData);
  215. if ($iNum > $iPerPage)
  216. $aListsData = array_slice($aListsData, 0, $iPerPage);
  217. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_LIST_VIEW']);
  218. $aListsTmpl = array();
  219. foreach($aListsData as $iListId => $sName) {
  220. $aParams['list_id'] = $iListId;
  221. if ($oPrivacy->check($iListId) || $iListId == 0){
  222. $aTmp = $this->getModule()->_serviceBrowse ('favorite', array_merge(array('user' => $oProfile->id()), $aParams), BX_DB_PADDING_DEF, $bEmptyMessage, false);
  223. if ($aTmp && $aTmp['content']){
  224. $aListsTmpl[] = array(
  225. 'title' => $sName,
  226. 'content_url' => $this->getModule()->_getFavoriteListUrl($iListId, $oProfile->id()),
  227. 'content' => $aTmp['content']
  228. );
  229. }
  230. }
  231. else{
  232. $aListsTmpl[] = array(
  233. 'title' => 'private list',
  234. 'content_url' => 'javascript:',
  235. 'content' => ''
  236. );
  237. }
  238. }
  239. $oPaginate = new BxTemplPaginate(array(
  240. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}', " . bx_js_string(json_encode($aParams)) . ", 'list_start', 'list_per_page');",
  241. 'num' => $iNum,
  242. 'per_page' => $iPerPage,
  243. 'start' => $iStart,
  244. ));
  245. if (count($aListsTmpl) > 0)
  246. return $this->parseHtmlByName('favorite-lists.html', array('bx_repeat:items' => $aListsTmpl)) . $oPaginate->getSimplePaginate() . $oFavorite->getJsScript();
  247. return false;
  248. }
  249. public function getFavoritesListInfo($aList, $oProfile)
  250. {
  251. $CNF = $this->_oConfig->CNF;
  252. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_LIST_VIEW']);
  253. $sTitle = "";
  254. if ($aList['allow_view_favorite_list_to'] < 0){
  255. $oProfileContext = BxDolProfile::getInstance(abs($aList['allow_view_favorite_list_to']));
  256. $sTitle = $oProfileContext->getDisplayName();
  257. }
  258. else{
  259. if (empty($aList['allow_view_favorite_list_to']))
  260. $aList['allow_view_favorite_list_to'] = 3;
  261. $aPrivaciInfo = $oPrivacy->getGroupsBy(array('type'=>'id' , 'id'=> $aList['allow_view_favorite_list_to']));
  262. $sTitle = _t($aPrivaciInfo['title']);
  263. }
  264. $aListsTmpl = array();
  265. if (!empty($aList['created'])){
  266. $aListsTmpl[] = array('title' => _t('_sys_form_favorite_list_title_created'), 'value' => bx_time_js($aList['created']));
  267. }
  268. $aListsTmpl = array_merge($aListsTmpl,
  269. array(
  270. array('title' => _t('_sys_form_favorite_list_title_updated'), 'value' => bx_time_js($aList['updated'])),
  271. array('title' => _t('_sys_form_favorite_list_title_count'), 'value' => $aList['count']),
  272. array('title' => _t('_sys_form_favorite_list_title_visibility'), 'value' => $sTitle)
  273. )
  274. );
  275. return $this->parseHtmlByName('favorite-list-info.html', array('bx_repeat:items' => $aListsTmpl, 'author' => $oProfile->getUnit()));
  276. }
  277. function entryAllActions($sActions)
  278. {
  279. if(empty($sActions))
  280. return '';
  281. return $this->parseHtmlByName('entry-all-actions.html', array (
  282. 'actions' => $sActions
  283. ));
  284. }
  285. function entryAttachments ($aData, $aParams = array())
  286. {
  287. return $this->entryAttachmentsByStorage($this->getModule()->_oConfig->CNF['OBJECT_STORAGE'], $aData, $aParams);
  288. }
  289. function entryAttachmentsByStorage ($mixedStorage, $aData, $aParams = array())
  290. {
  291. if(!is_array($mixedStorage))
  292. $mixedStorage = array($mixedStorage);
  293. $aResult = array();
  294. foreach($mixedStorage as $sStorage) {
  295. $aAttachments = $this->getAttachments($sStorage, $aData, $aParams);
  296. if(empty($aAttachments) || !is_array($aAttachments))
  297. continue;
  298. $aResult = array_merge($aResult, $aAttachments);
  299. }
  300. if(empty($aResult) || !is_array($aResult))
  301. return '';
  302. $this->addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flickity/|flickity.css');
  303. $this->addJs(array(
  304. 'flickity/flickity.pkgd.min.js',
  305. 'pdfjs/build/pdf.min.js',
  306. ));
  307. return $this->parseHtmlByName('attachments.html', array(
  308. 'bx_repeat:attachments' => $aResult,
  309. 'pdfjs_worker_url' => $this->getJsUrl('pdfjs/build/pdf.worker.min.js'),
  310. ));
  311. }
  312. function entryContext ($aData, $iProfileId = false, $sFuncContextDesc = 'getContextDesc', $sTemplateName = 'context.html', $sFuncContextAddon = 'getContextAddon')
  313. {
  314. $CNF = &$this->getModule()->_oConfig->CNF;
  315. $iContextId = $aData[$CNF['FIELD_ALLOW_VIEW_TO']];
  316. if ($iContextId >= 0)
  317. return '';
  318. $iProfileId = - $iContextId;
  319. $oProfile = BxDolProfile::getInstance($iProfileId);
  320. if (!$oProfile)
  321. $oProfile = BxDolProfileUndefined::getInstance();
  322. if (!$oProfile)
  323. return '';
  324. $sName = $oProfile->getDisplayName();
  325. $sAddon = $sFuncContextAddon ? $this->$sFuncContextAddon($aData, $oProfile) : '';
  326. $aVars = array (
  327. 'author_url' => $oProfile->getUrl(),
  328. 'author_thumb_url' => $oProfile->getThumb(),
  329. 'author_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  330. 'author_title' => $sName,
  331. 'author_title_attr' => bx_html_attribute($sName),
  332. 'author_desc' => $sFuncContextDesc ? $this->$sFuncContextDesc($aData) : '',
  333. 'bx_if:addon' => array (
  334. 'condition' => (bool)$sAddon,
  335. 'content' => array (
  336. 'content' => $sAddon,
  337. ),
  338. ),
  339. );
  340. return $this->parseHtmlByName($sTemplateName, $aVars);
  341. }
  342. protected function getAttachmentsImagesTranscoders ($sStorage = '')
  343. {
  344. $CNF = &$this->getModule()->_oConfig->CNF;
  345. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  346. $oTranscoderPreview = isset($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) && $CNF['OBJECT_IMAGES_TRANSCODER_PICTURE'] ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) : null;
  347. return array($oTranscoder, $oTranscoderPreview);
  348. }
  349. protected function getAttachmentsVideoTranscoders ($sStorage = '')
  350. {
  351. $CNF = &$this->getModule()->_oConfig->CNF;
  352. if (isset($CNF['OBJECT_VIDEOS_TRANSCODERS']) && $CNF['OBJECT_VIDEOS_TRANSCODERS'])
  353. return array (
  354. 'poster' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']),
  355. 'mp4' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']),
  356. 'mp4_hd' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4_hd']),
  357. );
  358. return false;
  359. }
  360. protected function getAttachments ($sStorage, $aData, $aParams = array())
  361. {
  362. $CNF = &$this->getModule()->_oConfig->CNF;
  363. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  364. list($oTranscoder, $oTranscoderPreview) = $this->getAttachmentsImagesTranscoders($sStorage);
  365. $oTranscoderSound = isset($CNF['OBJECT_SOUNDS_TRANSCODER']) && $CNF['OBJECT_SOUNDS_TRANSCODER'] ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_SOUNDS_TRANSCODER']) : null;
  366. $aTranscodersVideo = $this->getAttachmentsVideoTranscoders($sStorage);
  367. $aGhostFiles = $oStorage->getGhosts ($this->getModule()->serviceGetContentOwnerProfileId($aData[$CNF['FIELD_ID']]), $aData[$CNF['FIELD_ID']]);
  368. if (!$aGhostFiles)
  369. return false;
  370. $sFilterField = isset($aParams['filter_field']) ? $aParams['filter_field'] : $CNF['FIELD_THUMB'];
  371. if(!empty($sFilterField) && isset($aData[$sFilterField]))
  372. foreach ($aGhostFiles as $k => $a) {
  373. // don't show thumbnail in attachments
  374. if ($a['id'] == $aData[$sFilterField])
  375. unset($aGhostFiles[$k]);
  376. }
  377. if(!$aGhostFiles)
  378. return false;
  379. $aAttachmnts = array();
  380. foreach ($aGhostFiles as $k => $a) {
  381. $isImage = $oTranscoder && (0 === strncmp('image/', $a['mime_type'], 6)) && $oTranscoder->isMimeTypeSupported($a['mime_type']); // preview for images, transcoder object for preview must be defined
  382. $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
  383. $isSound = $oTranscoderSound && (0 === strncmp('audio/', $a['mime_type'], 6)) && $oTranscoderSound->isMimeTypeSupported($a['mime_type']); // preview for sounds, transcoder object for sounds must be defined
  384. $sUrlOriginal = $oStorage->getFileUrlById($a['id']);
  385. $sImgPopupId = 'bx-messages-atachment-popup-' . $a['id'];
  386. // images are displayed with preview and popup upon clicking
  387. $a['bx_if:image'] = array (
  388. 'condition' => $isImage,
  389. 'content' => array (
  390. 'url_original' => $isImage && $oTranscoderPreview ? $oTranscoderPreview->getFileUrl($a['id']) : $sUrlOriginal,
  391. 'attr_file_name' => bx_html_attribute($a['file_name']),
  392. 'popup_id' => $sImgPopupId,
  393. 'url_preview' => $isImage ? $oTranscoder->getFileUrl($a['id']) : '',
  394. 'popup' => BxTemplFunctions::getInstance()->transBox($sImgPopupId, '<img src="' . $sUrlOriginal . '" />', true, true),
  395. ),
  396. );
  397. $sVideoUrlHd = '';
  398. if($isVideo) {
  399. $sVideoUrl = $oStorage->getFileUrlById($a['id']);
  400. $aVideoSize = $aTranscodersVideo['mp4_hd']->getVideoSize($sVideoUrl);
  401. if(!empty($aVideoSize) && is_array($aVideoSize) && (int)$aVideoSize['h'] >= 720)
  402. $sVideoUrlHd = $aTranscodersVideo['mp4_hd']->getFileUrl($a['id']);
  403. }
  404. // videos are displayed inline
  405. $a['bx_if:video'] = array (
  406. 'condition' => $isVideo,
  407. 'content' => array (
  408. 'video' => $isVideo && $aTranscodersVideo ? BxTemplFunctions::getInstance()->videoPlayer(
  409. $aTranscodersVideo['poster']->getFileUrl($a['id']),
  410. $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  411. $sVideoUrlHd,
  412. false, ''
  413. ) : '',
  414. ),
  415. );
  416. // sounds are displayed inline
  417. $a['bx_if:sound'] = array (
  418. 'condition' => $isSound,
  419. 'content' => array (
  420. 'sound' => $isSound && $oTranscoderSound && ($oPlayer = BxDolPlayer::getObjectInstance()) ? $this->parseHtmlByName('attachment_sound.html', array(
  421. 'file_name' => $a['file_name'],
  422. 'file_url' => $oTranscoderSound ? $oTranscoderSound->getFileUrl($a['id']) : '',
  423. 'player' => $oTranscoderSound && $oTranscoderSound->isFileReady($a['id']) ?
  424. $oPlayer->getCodeAudio (BX_PLAYER_STANDARD, array(
  425. 'mp3' => $oTranscoderSound->getFileUrl($a['id']),
  426. )) : _t('_sys_txt_err_sound_not_transcoded_yet'),
  427. )) : '',
  428. ),
  429. );
  430. // non-images are displayed as text links to original file
  431. $a['bx_if:not_image'] = array (
  432. 'condition' => !$isImage && !$isVideo && !$isSound,
  433. 'content' => array (
  434. 'url_original' => $sUrlOriginal,
  435. 'attr_file_name' => bx_html_attribute($a['file_name']),
  436. 'file_name' => bx_process_output($a['file_name']),
  437. ),
  438. );
  439. $aAttachmnts[] = $a;
  440. }
  441. return $aAttachmnts;
  442. }
  443. public function embedVideo($iFileId)
  444. {
  445. $CNF = $this->getModule()->_oConfig->CNF;
  446. list($oPlayer, $oStorage, $aContentInfo, $a) = $this->_embedChecks('OBJECT_STORAGE_VIDEOS', $iFileId);
  447. // check if file is really video
  448. $aTranscodersVideo = $this->getAttachmentsVideoTranscoders();
  449. if (!$aTranscodersVideo || (0 !== strncmp('video/', $a['mime_type'], 6)) || !$aTranscodersVideo['poster']->isMimeTypeSupported($a['mime_type'])) {
  450. $this->displayErrorOccured('', BX_PAGE_EMBED);
  451. exit;
  452. }
  453. // check HD video version
  454. $sVideoUrlHd = '';
  455. $sVideoUrl = $oStorage->getFileUrlById($a['id']);
  456. $aVideoSize = $aTranscodersVideo['mp4_hd']->getVideoSize($sVideoUrl);
  457. if (!empty($aVideoSize) && is_array($aVideoSize) && (int)$aVideoSize['h'] >= 720)
  458. $sVideoUrlHd = $aTranscodersVideo['mp4_hd']->getFileUrl($a['id']);
  459. // generate player code
  460. $sCode = $oPlayer->getCodeVideo (BX_PLAYER_EMBED, array(
  461. 'poster' => $aTranscodersVideo['poster']->getFileUrl($a['id']),
  462. 'mp4' => array(
  463. 'sd' => $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  464. 'hd' => $sVideoUrlHd
  465. ),
  466. ));
  467. // display page
  468. $oTemplate = BxDolTemplate::getInstance();
  469. $oTemplate->setPageNameIndex (BX_PAGE_EMBED);
  470. $oTemplate->setPageHeader ($a['file_name']);
  471. $oTemplate->setPageContent ('page_main_code', $sCode);
  472. $oTemplate->getPageCode();
  473. exit;
  474. }
  475. public function embedSound($iFileId)
  476. {
  477. $CNF = $this->getModule()->_oConfig->CNF;
  478. list($oPlayer, $oStorage, $aContentInfo, $a) = $this->_embedChecks('OBJECT_STORAGE_SOUNDS', $iFileId);
  479. // check if file is really audio
  480. $oTranscoderSound = isset($CNF['OBJECT_SOUNDS_TRANSCODER']) ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_SOUNDS_TRANSCODER']) : null;
  481. if (!$oTranscoderSound || (0 !== strncmp('audio/', $a['mime_type'], 6)) || !$oTranscoderSound->isMimeTypeSupported($a['mime_type'])) {
  482. $this->displayErrorOccured('', BX_PAGE_EMBED);
  483. exit;
  484. }
  485. if (!$oTranscoderSound->isFileReady($a['id'])) {
  486. $oTranscoderSound->getFileUrl($a['id']); // queue for encoding
  487. $this->displayMsg('_sys_txt_err_sound_not_transcoded_yet', true, BX_PAGE_EMBED);
  488. exit;
  489. }
  490. // generate player code
  491. $sCode = $oPlayer->getCodeAudio (BX_PLAYER_EMBED, array(
  492. 'mp3' => $oTranscoderSound->getFileUrl($a['id']),
  493. ));
  494. // display page
  495. $oTemplate = BxDolTemplate::getInstance();
  496. $oTemplate->setPageNameIndex (BX_PAGE_EMBED);
  497. $oTemplate->setPageHeader ($a['file_name']);
  498. $oTemplate->setPageContent ('page_main_code', $sCode);
  499. $oTemplate->getPageCode();
  500. exit;
  501. }
  502. protected function _embedChecks($sStorageKey, $iFileId)
  503. {
  504. // general checks
  505. $oPlayer = BxDolPlayer::getObjectInstance();
  506. $CNF = $this->getModule()->_oConfig->CNF;
  507. $sStorage = isset($CNF[$sStorageKey]) ? $CNF[$sStorageKey] : false;
  508. if (!$oPlayer || !$sStorage || !($oStorage = BxDolStorage::getObjectInstance($sStorage))) {
  509. $this->displayErrorOccured('', BX_PAGE_EMBED);
  510. exit;
  511. }
  512. // privacy check
  513. $aGhost = $oStorage->getGhost($iFileId);
  514. $aContentInfo = $aGhost && $aGhost['content_id'] ? $this->getModule()->_oDb->getContentInfoById($aGhost['content_id']) : false;
  515. if (!$aGhost || ($aGhost['content_id'] && !$aContentInfo)) {
  516. $this->displayPageNotFound('', BX_PAGE_EMBED);
  517. exit;
  518. }
  519. if (!$aGhost['content_id']) {
  520. // if file is not associated with content yet, then only admin and owner can view it
  521. if (!isAdmin() && CHECK_ACTION_RESULT_ALLOWED !== $this->getModule()->checkAllowedEditAnyEntry() && $aGhost['profile_id'] != bx_get_logged_profile_id()) {
  522. $this->displayAccessDenied('', BX_PAGE_EMBED);
  523. exit;
  524. }
  525. }
  526. else {
  527. // if file is associated with content, then check entry privacy
  528. if (CHECK_ACTION_RESULT_ALLOWED !== $this->getModule()->checkAllowedView($aContentInfo)) {
  529. $this->displayAccessDenied('', BX_PAGE_EMBED);
  530. exit;
  531. }
  532. }
  533. // get file info
  534. if (!($a = $oStorage->getFile($iFileId))) {
  535. $this->displayPageNotFound('', BX_PAGE_EMBED);
  536. exit;
  537. }
  538. return array($oPlayer, $oStorage, $aContentInfo, $a);
  539. }
  540. public function addCssJs()
  541. {
  542. $this->addCss('main.css');
  543. }
  544. }
  545. /** @} */