1
0

BxBaseModGeneralTemplate.php 34 KB

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