BxBaseModGeneralTemplate.php 32 KB

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