BxFilesTemplate.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 Files Files
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module representation.
  13. */
  14. class BxFilesTemplate extends BxBaseModTextTemplate
  15. {
  16. function __construct(&$oConfig, &$oDb)
  17. {
  18. $this->MODULE = 'bx_files';
  19. parent::__construct($oConfig, $oDb);
  20. }
  21. protected function getUnitThumbAndGallery ($aData)
  22. {
  23. $aFile = BxDolModule::getInstance($this->MODULE)->getContentFile($aData);
  24. if (!$aFile['is_image'])
  25. return array('', '');
  26. $sPhotoThumb = '';
  27. if ($oImagesTranscoder = BxDolTranscoderImage::getObjectInstance(BxDolModule::getInstance($this->MODULE)->_oConfig->CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']))
  28. $sPhotoThumb = $oImagesTranscoder->getFileUrl($aFile['id']);
  29. return array($sPhotoThumb, $sPhotoThumb);
  30. }
  31. function unit ($aData, $isCheckPrivateContent = true, $sTemplateName = 'unit.html', $aParams = array())
  32. {
  33. $oModule = $this->getModule();
  34. $sResult = $this->checkPrivacy ($aData, $isCheckPrivateContent, $oModule, $sTemplateName);
  35. if($sResult)
  36. return $sResult;
  37. $CNF = &$oModule->_oConfig->CNF;
  38. $aFile = $oModule->getContentFile($aData);
  39. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  40. if(isset($CNF['FIELD_TITLE']) && empty($aData[$CNF['FIELD_TITLE']]))
  41. $aData[$CNF['FIELD_TITLE']] = _t('_sys_txt_no_title');
  42. $aParams['template_name'] = $sTemplateName;
  43. $aVars = $this->getUnit($aData, $aParams);
  44. $aVars['icon'] = $oStorage ? $oStorage->getFontIconNameByFileName($aFile['file_name']) : 'far file';
  45. $aVars['bx_if:no_thumb']['content']['icon'] = $aVars['icon'];
  46. $aVars['bx_if:inline_menu']['condition'] = isset($aParams['show_inline_menu']) && $aParams['show_inline_menu'];
  47. $aVars['bx_if:inline_menu']['content'] = [];
  48. if ($aVars['bx_if:inline_menu']['condition']) {
  49. bx_import('BxTemplMenu');
  50. $oMenu = BxTemplMenu::getObjectInstance('bx_files_view_inline', $this);
  51. if ($oMenu) {
  52. $oMenu->setContentId($aData[$CNF['FIELD_ID']]);
  53. $oMenu->setBookmarked($this->_oDb->isFileBookmarked($aData[$CNF['FIELD_ID']], bx_get_logged_profile_id()));
  54. $oMenu->setAllowEditOptions($aData[$CNF['FIELD_AUTHOR']] == bx_get_logged_profile_id());
  55. $aVars['bx_if:inline_menu']['content']['menu'] = $oMenu->getCode();
  56. }
  57. }
  58. $sAltContentUrlHandler = '';
  59. if ($oModule->_oDb->getParam($CNF['PARAM_LINK_TO_PREVIEW'])) {
  60. $sAltContentUrlHandler = '$.get(\'' . BX_DOL_URL_ROOT . $oModule->_oConfig->getBaseUri() . 'entry_preview/' . $aData[$CNF['FIELD_ID']] . '\', processJsonData, \'json\'); return false;';
  61. }
  62. $aVars['alt_content_url_handler'] = $sAltContentUrlHandler;
  63. $aVars['bx_if:no_thumb']['content']['alt_content_url_handler'] = $sAltContentUrlHandler;
  64. $aVars['bx_if:thumb']['content']['alt_content_url_handler'] = $sAltContentUrlHandler;
  65. return $this->parseHtmlByName($sTemplateName, $aVars);
  66. }
  67. public function entryFilePreview ($aData)
  68. {
  69. $oModule = BxDolModule::getInstance($this->MODULE);
  70. $CNF = $oModule->_oConfig->CNF;
  71. $sNoPreview = MsgBox(_t('_bx_files_txt_preview_not_available'));
  72. if (!($aFile = $oModule->getContentFile($aData)))
  73. return $sNoPreview;
  74. if (!($oFileHandler = BxDolFileHandler::getObjectInstanceByFile($aFile['file_name'])))
  75. return $sNoPreview;
  76. if (!($oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE'])))
  77. return $sNoPreview;
  78. if (!($sFileUrl = $oStorage->getFileUrlById($aFile['id'])))
  79. return $sNoPreview;
  80. return $oFileHandler->display($sFileUrl, $aFile);
  81. }
  82. public function entryText ($aData, $sTemplateName = 'entry-text.html')
  83. {
  84. return $this->parseHtmlByName($sTemplateName, $this->getTmplVarsText($aData));
  85. }
  86. public function entryInfoPopup($aData) {
  87. $CNF = &$this->getModule()->_oConfig->CNF;
  88. $aFile = $this->getModule()->getContentFile($aData);
  89. $oAuthor = BxDolProfile::getInstance($aData[$CNF['FIELD_AUTHOR']]);
  90. if (!$oAuthor) $oAuthor = BxDolProfileUndefined::getInstance();
  91. $aForm = [
  92. 'params' => [
  93. 'view_mode' => true,
  94. ],
  95. 'inputs' => [
  96. 'size' => [
  97. 'caption' => _t('_bx_files_entry_info_size'),
  98. 'type' => 'value',
  99. 'value' => _t_format_size($aFile['size']),
  100. ],
  101. 'date' => [
  102. 'caption' => _t('_bx_files_entry_info_date'),
  103. 'type' => 'date_time',
  104. 'value' => $aData[$CNF['FIELD_ADDED']],
  105. ],
  106. 'author' => [
  107. 'caption' => _t('_bx_files_entry_info_author'),
  108. 'type' => 'textarea',
  109. 'html' => 1,
  110. 'value' => '<a href="'.$oAuthor->getUrl().'">'.$oAuthor->getDisplayName().'</a>',
  111. ],
  112. ],
  113. ];
  114. if ($aData[$CNF['FIELD_ALLOW_VIEW_TO']] < 0) {
  115. $oProfile = BxDolProfile::getInstance(-$aData[$CNF['FIELD_ALLOW_VIEW_TO']]);
  116. if ($oProfile) {
  117. $aForm['inputs']['context'] = [
  118. 'caption' => _t('_bx_files_entry_info_context'),
  119. 'type' => 'textarea',
  120. 'html' => 1,
  121. 'value' => '<a href="' . $oProfile->getUrl() . '">' . $oProfile->getDisplayName() . '</a>',
  122. ];
  123. }
  124. }
  125. $oForm = new BxTemplFormView($aForm);
  126. return $this->parseHtmlByName('entry-popup.html', [
  127. 'content' => $oForm->getCode(),
  128. 'bx_repeat:init_scripts' => [],
  129. ]);
  130. }
  131. public function getTmplVarsText($aData)
  132. {
  133. $aVars = parent::getTmplVarsText($aData);
  134. $aVars = array_merge($aVars, array(
  135. 'entry_preview' => $this->entryFilePreview($aData),
  136. 'bx_if:show_content' => array(
  137. 'condition' => !empty($aVars['entry_title']) || !empty($aVars['entry_text']),
  138. 'content' => array(
  139. 'entry_title' => $aVars['entry_title'],
  140. 'entry_text' => $aVars['entry_text']
  141. )
  142. )
  143. ));
  144. return $aVars;
  145. }
  146. }
  147. /** @} */