BxBaseModGeneralTemplate.php 29 KB

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