BxBaseModGeneralTemplate.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. function entryAllActions($sActions)
  298. {
  299. if(empty($sActions))
  300. return '';
  301. return $this->parseHtmlByName('entry-all-actions.html', array (
  302. 'actions' => $sActions
  303. ));
  304. }
  305. function entryAttachments ($aData, $aParams = array())
  306. {
  307. return $this->entryAttachmentsByStorage($this->getModule()->_oConfig->CNF['OBJECT_STORAGE'], $aData, $aParams);
  308. }
  309. function entryAttachmentsByStorage ($mixedStorage, $aData, $aParams = array())
  310. {
  311. if(!is_array($mixedStorage))
  312. $mixedStorage = array($mixedStorage);
  313. $aResult = array();
  314. if (bx_is_api()){
  315. foreach($mixedStorage as $sStorage) {
  316. $aAttachments = $this->getAttachments($sStorage, $aData, $aParams);
  317. if ($aAttachments){
  318. foreach($aAttachments as $aAttachment) {
  319. $aItem = [
  320. 'file_name' => $aAttachment['file_name'],
  321. 'mime_type' => $aAttachment['mime_type'],
  322. 'ext' => $aAttachment['ext'],
  323. 'type' => 'other',
  324. 'size' => $aAttachment['size'],
  325. 'url' => $aAttachment['bx_if:not_image']['content']['url_original']
  326. ];
  327. if ($aAttachment['bx_if:image']['condition'] == 1){
  328. $aItem['type'] = 'image';
  329. $aItem['data'] = bx_api_get_image($sStorage, $aAttachment['id']);
  330. }
  331. if ($aAttachment['bx_if:video']['condition'] == 1){
  332. $aItem['type'] = 'video';
  333. $aItem['data'] = bx_api_get_image($sStorage, $aAttachment['id']);
  334. }
  335. if ($aAttachment['bx_if:sound']['condition'] == 1){
  336. $aItem['type'] = 'sound';
  337. $aItem['data'] = bx_api_get_image($sStorage, $aAttachment['id']);
  338. }
  339. $aResult[] = $aItem;
  340. }
  341. }
  342. }
  343. return [bx_api_get_block('entity_attachments', $aResult)];
  344. }
  345. foreach($mixedStorage as $sStorage) {
  346. $aAttachments = $this->getAttachments($sStorage, $aData, $aParams);
  347. if(empty($aAttachments) || !is_array($aAttachments))
  348. continue;
  349. $aResult = array_merge($aResult, $aAttachments);
  350. }
  351. if(empty($aResult) || !is_array($aResult))
  352. return '';
  353. $this->addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flickity/|flickity.css');
  354. $this->addJs(array(
  355. 'flickity/flickity.pkgd.min.js',
  356. 'pdfjs/build/pdf.min.js',
  357. ));
  358. return $this->parseHtmlByName('attachments.html', array(
  359. 'bx_repeat:attachments' => $aResult,
  360. 'pdfjs_worker_url' => $this->getJsUrl('pdfjs/build/pdf.worker.min.js'),
  361. ));
  362. }
  363. function entryContext ($aData, $iProfileId = false, $sFuncContextDesc = 'getContextDesc', $sTemplateName = 'context.html', $sFuncContextAddon = 'getContextAddon')
  364. {
  365. $CNF = &$this->getModule()->_oConfig->CNF;
  366. $iContextId = $aData[$CNF['FIELD_ALLOW_VIEW_TO']];
  367. if ($iContextId >= 0)
  368. return '';
  369. $iProfileId = - $iContextId;
  370. $oProfile = BxDolProfile::getInstance($iProfileId);
  371. if (!$oProfile)
  372. $oProfile = BxDolProfileUndefined::getInstance();
  373. if (!$oProfile)
  374. return '';
  375. $sName = $oProfile->getDisplayName();
  376. $sAddon = $sFuncContextAddon ? $this->$sFuncContextAddon($aData, $oProfile) : '';
  377. $aVars = array (
  378. 'author_url' => $oProfile->getUrl(),
  379. 'author_thumb_url' => $oProfile->getThumb(),
  380. 'author_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  381. 'author_title' => $sName,
  382. 'author_title_attr' => bx_html_attribute($sName),
  383. 'author_desc' => $sFuncContextDesc ? $this->$sFuncContextDesc($aData) : '',
  384. 'bx_if:addon' => array (
  385. 'condition' => (bool)$sAddon,
  386. 'content' => array (
  387. 'content' => $sAddon,
  388. ),
  389. ),
  390. );
  391. return $this->parseHtmlByName($sTemplateName, $aVars);
  392. }
  393. protected function getAttachmentsImagesTranscoders ($sStorage = '')
  394. {
  395. $CNF = &$this->getModule()->_oConfig->CNF;
  396. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  397. $oTranscoderView = !empty($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) : null;
  398. return array($oTranscoder, $oTranscoderView);
  399. }
  400. protected function getAttachmentsVideoTranscoders ($sStorage = '')
  401. {
  402. $CNF = &$this->getModule()->_oConfig->CNF;
  403. if (isset($CNF['OBJECT_VIDEOS_TRANSCODERS']) && $CNF['OBJECT_VIDEOS_TRANSCODERS'])
  404. return array (
  405. 'poster' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']),
  406. 'mp4' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']),
  407. 'mp4_hd' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4_hd']),
  408. );
  409. return false;
  410. }
  411. public function getAttachmentsData ($sStorage, $aData, $aParams = array())
  412. {
  413. return $this->getAttachments($sStorage, $aData, $aParams );
  414. }
  415. protected function getAttachments ($sStorage, $aData, $aParams = array())
  416. {
  417. $CNF = &$this->getModule()->_oConfig->CNF;
  418. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  419. list($oTranscoder, $oTranscoderPreview) = $this->getAttachmentsImagesTranscoders($sStorage);
  420. $oTranscoderSound = isset($CNF['OBJECT_SOUNDS_TRANSCODER']) && $CNF['OBJECT_SOUNDS_TRANSCODER'] ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_SOUNDS_TRANSCODER']) : null;
  421. $aTranscodersVideo = $this->getAttachmentsVideoTranscoders($sStorage);
  422. $aGhostFiles = $oStorage->getGhosts ($this->getModule()->serviceGetContentOwnerProfileId($aData[$CNF['FIELD_ID']]), $aData[$CNF['FIELD_ID']]);
  423. if (!$aGhostFiles)
  424. return false;
  425. $sFilterField = isset($aParams['filter_field']) ? $aParams['filter_field'] : $CNF['FIELD_THUMB'];
  426. if(!empty($sFilterField) && isset($aData[$sFilterField]))
  427. foreach ($aGhostFiles as $k => $a) {
  428. // don't show thumbnail in attachments
  429. if ($a['id'] == $aData[$sFilterField])
  430. unset($aGhostFiles[$k]);
  431. }
  432. if(!$aGhostFiles)
  433. return false;
  434. $aAttachmnts = array();
  435. foreach ($aGhostFiles as $k => $a) {
  436. $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
  437. $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
  438. $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
  439. $sUrlOriginal = $oStorage->getFileUrlById($a['id']);
  440. $sImgPopupId = 'bx-messages-atachment-popup-' . $a['id'];
  441. // images are displayed with preview and popup upon clicking
  442. $a['bx_if:image'] = array (
  443. 'condition' => $isImage,
  444. 'content' => array (
  445. 'url_original' => $isImage && $oTranscoderPreview ? $oTranscoderPreview->getFileUrl($a['id']) : $sUrlOriginal,
  446. 'attr_file_name' => bx_html_attribute($a['file_name']),
  447. 'popup_id' => $sImgPopupId,
  448. 'url_preview' => $isImage ? $oTranscoder->getFileUrl($a['id']) : '',
  449. 'popup' => BxTemplFunctions::getInstance()->transBox($sImgPopupId, '<img src="' . $sUrlOriginal . '" />', true, true),
  450. ),
  451. );
  452. $sVideoUrlHd = '';
  453. if($isVideo) {
  454. $sVideoUrl = $oStorage->getFileUrlById($a['id']);
  455. $aVideoFile = $oStorage->getFile($a['id']);
  456. if (!empty($aVideoFile['dimensions']) && $aTranscodersVideo['mp4_hd']->isProcessHD($aVideoFile['dimensions']))
  457. $sVideoUrlHd = $aTranscodersVideo['mp4_hd']->getFileUrl($a['id']);
  458. }
  459. // videos are displayed inline
  460. $a['bx_if:video'] = array (
  461. 'condition' => $isVideo,
  462. 'content' => array (
  463. 'video' => $isVideo && $aTranscodersVideo ? BxTemplFunctions::getInstance()->videoPlayer(
  464. $aTranscodersVideo['poster']->getFileUrl($a['id']),
  465. $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  466. $sVideoUrlHd,
  467. false, ''
  468. ) : '',
  469. ),
  470. );
  471. // sounds are displayed inline
  472. $a['bx_if:sound'] = array (
  473. 'condition' => $isSound,
  474. 'content' => array (
  475. 'sound' => $isSound && $oTranscoderSound && ($oPlayer = BxDolPlayer::getObjectInstance()) ? $this->parseHtmlByName('attachment_sound.html', array(
  476. 'file_name' => $a['file_name'],
  477. 'file_url' => $oTranscoderSound ? $oTranscoderSound->getFileUrl($a['id']) : '',
  478. 'player' => $oTranscoderSound && $oTranscoderSound->isFileReady($a['id']) ?
  479. $oPlayer->getCodeAudio (BX_PLAYER_STANDARD, array(
  480. 'mp3' => $oTranscoderSound->getFileUrl($a['id']),
  481. )) : _t('_sys_txt_err_sound_not_transcoded_yet'),
  482. )) : '',
  483. ),
  484. );
  485. // non-images are displayed as text links to original file
  486. $a['bx_if:not_image'] = array (
  487. 'condition' => !$isImage && !$isVideo && !$isSound,
  488. 'content' => array (
  489. 'url_original' => $sUrlOriginal,
  490. 'attr_file_name' => bx_html_attribute($a['file_name']),
  491. 'file_name' => bx_process_output($a['file_name']),
  492. ),
  493. );
  494. $aAttachmnts[] = $a;
  495. }
  496. return $aAttachmnts;
  497. }
  498. public function embedVideo($iFileId)
  499. {
  500. $CNF = $this->getModule()->_oConfig->CNF;
  501. list($oPlayer, $oStorage, $aContentInfo, $a) = $this->_embedChecks('OBJECT_STORAGE_VIDEOS', $iFileId);
  502. // check if file is really video
  503. $aTranscodersVideo = $this->getAttachmentsVideoTranscoders();
  504. if (!$aTranscodersVideo || (0 !== strncmp('video/', $a['mime_type'], 6)) || !$aTranscodersVideo['poster']->isMimeTypeSupported($a['mime_type'])) {
  505. $this->displayErrorOccured('', BX_PAGE_EMBED);
  506. exit;
  507. }
  508. // check HD video version
  509. $sVideoUrlHd = '';
  510. $sVideoUrl = $oStorage->getFileUrlById($a['id']);
  511. $aVideoFile = $oStorage->getFile($a['id']);
  512. if (!empty($aVideoFile['dimensions']) && $aTranscodersVideo['mp4_hd']->isProcessHD($aVideoFile['dimensions']))
  513. $sVideoUrlHd = $aTranscodersVideo['mp4_hd']->getFileUrl($a['id']);
  514. // generate player code
  515. $sCode = $oPlayer->getCodeVideo (BX_PLAYER_EMBED, array(
  516. 'poster' => $aTranscodersVideo['poster']->getFileUrl($a['id']),
  517. 'mp4' => array(
  518. 'sd' => $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  519. 'hd' => $sVideoUrlHd
  520. ),
  521. ));
  522. // display page
  523. $oTemplate = BxDolTemplate::getInstance();
  524. $oTemplate->setPageNameIndex (BX_PAGE_EMBED);
  525. $oTemplate->setPageHeader ($a['file_name']);
  526. $oTemplate->setPageContent ('page_main_code', $sCode);
  527. $oTemplate->getPageCode();
  528. exit;
  529. }
  530. public function embedSound($iFileId)
  531. {
  532. $CNF = $this->getModule()->_oConfig->CNF;
  533. list($oPlayer, $oStorage, $aContentInfo, $a) = $this->_embedChecks('OBJECT_STORAGE_SOUNDS', $iFileId);
  534. // check if file is really audio
  535. $oTranscoderSound = isset($CNF['OBJECT_SOUNDS_TRANSCODER']) ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_SOUNDS_TRANSCODER']) : null;
  536. if (!$oTranscoderSound || (0 !== strncmp('audio/', $a['mime_type'], 6)) || !$oTranscoderSound->isMimeTypeSupported($a['mime_type'])) {
  537. $this->displayErrorOccured('', BX_PAGE_EMBED);
  538. exit;
  539. }
  540. if (!$oTranscoderSound->isFileReady($a['id'])) {
  541. $oTranscoderSound->getFileUrl($a['id']); // queue for encoding
  542. $this->displayMsg('_sys_txt_err_sound_not_transcoded_yet', true, BX_PAGE_EMBED);
  543. exit;
  544. }
  545. // generate player code
  546. $sCode = $oPlayer->getCodeAudio (BX_PLAYER_EMBED, array(
  547. 'mp3' => $oTranscoderSound->getFileUrl($a['id']),
  548. ));
  549. // display page
  550. $oTemplate = BxDolTemplate::getInstance();
  551. $oTemplate->setPageNameIndex (BX_PAGE_EMBED);
  552. $oTemplate->setPageHeader ($a['file_name']);
  553. $oTemplate->setPageContent ('page_main_code', $sCode);
  554. $oTemplate->getPageCode();
  555. exit;
  556. }
  557. protected function _embedChecks($sStorageKey, $iFileId)
  558. {
  559. // general checks
  560. $oPlayer = BxDolPlayer::getObjectInstance();
  561. $CNF = $this->getModule()->_oConfig->CNF;
  562. $sStorage = isset($CNF[$sStorageKey]) ? $CNF[$sStorageKey] : false;
  563. if (!$oPlayer || !$sStorage || !($oStorage = BxDolStorage::getObjectInstance($sStorage))) {
  564. $this->displayErrorOccured('', BX_PAGE_EMBED);
  565. exit;
  566. }
  567. // privacy check
  568. $aGhost = $oStorage->getGhost($iFileId);
  569. $aContentInfo = $aGhost && $aGhost['content_id'] ? $this->getModule()->_oDb->getContentInfoById($aGhost['content_id']) : false;
  570. if (!$aGhost || ($aGhost['content_id'] && !$aContentInfo)) {
  571. $this->displayPageNotFound('', BX_PAGE_EMBED);
  572. exit;
  573. }
  574. if (!$aGhost['content_id']) {
  575. // if file is not associated with content yet, then only admin and owner can view it
  576. if (!isAdmin() && CHECK_ACTION_RESULT_ALLOWED !== $this->getModule()->checkAllowedEditAnyEntry() && $aGhost['profile_id'] != bx_get_logged_profile_id()) {
  577. $this->displayAccessDenied('', BX_PAGE_EMBED);
  578. exit;
  579. }
  580. }
  581. else {
  582. // if file is associated with content, then check entry privacy
  583. if (CHECK_ACTION_RESULT_ALLOWED !== $this->getModule()->checkAllowedView($aContentInfo)) {
  584. $this->displayAccessDenied('', BX_PAGE_EMBED);
  585. exit;
  586. }
  587. }
  588. // get file info
  589. if (!($a = $oStorage->getFile($iFileId))) {
  590. $this->displayPageNotFound('', BX_PAGE_EMBED);
  591. exit;
  592. }
  593. return array($oPlayer, $oStorage, $aContentInfo, $a);
  594. }
  595. function _getImageSettings($sSettings)
  596. {
  597. $sCoverSettings = '';
  598. $aCoverData = json_decode($sSettings, true);
  599. if (!empty($aCoverData)){
  600. $sCoverSettings = 'background-position: ' . $aCoverData['x'] . '% ' . $aCoverData['y'] . '%';
  601. }
  602. return $sCoverSettings;
  603. }
  604. function _prepareImage($aData, $sUniqId, $sUploader, $sStorage, $sField, $bAllowTweak)
  605. {
  606. $CNF = &$this->_oConfig->CNF;
  607. $oUploader = null;
  608. $sUploadersButtons = $sUploadersJs = '';
  609. $aUploaders = $sUploader;
  610. $sUploadersJs = '';
  611. $sJsName = '';
  612. foreach ($aUploaders as $sUploaderObject) {
  613. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $sStorage, $sUniqId, $this);
  614. $sGhostTemplate = '{file_id}';
  615. $aParamsJs = array_merge($oUploader->getUploaderJsParams(),
  616. [
  617. 'content_id' => $aData['id'],
  618. 'storage_private' => '0',
  619. 'is_init_ghosts' => 0,
  620. 'is_init_reordering' => 0
  621. ]
  622. );
  623. $sUploadersJs .= $oUploader->getUploaderJs($sGhostTemplate, false, $aParamsJs);
  624. $sJsName = $oUploader->getNameJsInstanceUploader();
  625. }
  626. $aParamsButtons = [
  627. 'content_id' => $aData['id'],
  628. 'storage_private' => '0',
  629. 'btn_class' => '',
  630. 'button_title' => '',
  631. 'attrs' => "class='hidden'"
  632. ];
  633. $this->addJs(['BxDolUploader.js']);
  634. $sAddCode = $this->parseHtmlByName('image_tweak.html', [
  635. 'id' => $aData['id'],
  636. 'js_object' => $sJsName,
  637. 'unique_id' => $sUniqId,
  638. 'id' => $aData['id'],
  639. 'allow_tweak' => $bAllowTweak,
  640. 'image_exists' => $aData[$sField] == 0 ? 'bx-image-edit-buttons-no-image' : '',
  641. 'field' => $sField,
  642. 'action_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri(),
  643. 'uploader' => $oUploader->getUploaderButton($aParamsButtons),
  644. 'uploader_js' => $sUploadersJs,
  645. ]);
  646. $this->addJsTranslation(['_sys_uploader_image_reposition_info']);
  647. return $sAddCode;
  648. }
  649. public function addCssJs()
  650. {
  651. $this->addCss('main.css');
  652. }
  653. }
  654. /** @} */