BxBaseModGeneralTemplate.php 33 KB

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