BxResourcesTemplate.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * @defgroup Resources Resources
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. /*
  11. * Module representation.
  12. */
  13. class BxResourcesTemplate extends BxBaseModTextTemplate
  14. {
  15. function __construct(&$oConfig, &$oDb)
  16. {
  17. $this->MODULE = 'bx_resources';
  18. parent::__construct($oConfig, $oDb);
  19. }
  20. public function getTitle($aData, $mixedProcessOutput = BX_DATA_TEXT)
  21. {
  22. $CNF = &$this->_oConfig->CNF;
  23. $aData[$CNF['FIELD_TITLE']] = $aData[$CNF['FIELD_TITLE']] ?: _t('_bx_resources_txt_title_empty');
  24. return parent::getTitle($aData, $mixedProcessOutput);
  25. }
  26. public function getUnitLink ($aData, $aParams = [])
  27. {
  28. $CNF = &$this->_oConfig->CNF;
  29. $sLink = $aData[$CNF['FIELD_LINK']] ?? '';
  30. if(!$sLink) {
  31. $sLink = parent::getUnitLink($aData, $aParams);
  32. if($this->_bIsApi)
  33. $sLink = bx_api_get_relative_url($sLink);
  34. }
  35. return $sLink;
  36. }
  37. public function getUnitTitle ($aData, $aParams = [])
  38. {
  39. $CNF = &$this->_oConfig->CNF;
  40. $aData[$CNF['FIELD_TITLE']] = $aData[$CNF['FIELD_TITLE']] ?: $aData[$CNF['FIELD_LINK']];
  41. return parent::getUnitTitle($aData, $aParams);
  42. }
  43. protected function getUnit ($aData, $aParams = [])
  44. {
  45. $aTmplVars = parent::getUnit ($aData, $aParams);
  46. if(empty($aTmplVars) || !is_array($aTmplVars))
  47. return $aTmplVars;
  48. if($aTmplVars['bx_if:no_thumb']['condition'])
  49. $aTmplVars['bx_if:no_thumb']['condition'] = false;
  50. $aTmplVars['content_url_attrs'] = bx_convert_array2attrs(['target' => '_blank']);
  51. return $aTmplVars;
  52. }
  53. /**
  54. * Use Gallery image for both because currently there is no Unit types with small thumbnails.
  55. */
  56. protected function getUnitThumbAndGallery ($aData)
  57. {
  58. list($sPhotoThumb, $sPhotoGallery) = parent::getUnitThumbAndGallery($aData);
  59. return array($sPhotoGallery, $sPhotoGallery);
  60. }
  61. public function entryText ($aData, $sTemplateName = 'entry-text.html')
  62. {
  63. $aVars = $this->getTmplVarsText($aData);
  64. return $this->_bIsApi ? $aVars : $this->parseHtmlByName($sTemplateName, $aVars);
  65. }
  66. public function getEntriesList($iContextId)
  67. {
  68. $CNF = &$this->_oConfig->CNF;
  69. $sJsObject = $this->_oConfig->getJsObject('main');
  70. $oPermalinks = BxDolPermalinks::getInstance();
  71. $aCategories = BxDolFormQuery::getDataItems($CNF['OBJECT_CATEGORY']);
  72. $oModule = $this->getModule();
  73. $bAllowAdd = $oModule->isAllowAdd($iContextId);
  74. $bAllowManage = $oModule->isAllowManageByContext($iContextId);
  75. $aLists = $this->_oDb->getLists($iContextId);
  76. $aTmplVarsLists = [];
  77. foreach($aLists as $aList) {
  78. $sListTitle = $aList[$CNF['FIELD_LIST_TITLE']] ?: _t('_bx_resources_txt_list_default');
  79. $aResources = $this->_oDb->getResources($iContextId, $aList[$CNF['FIELD_LIST_ID']], true);
  80. $aTmplVarsEntries = [];
  81. foreach($aResources as $aResource) {
  82. $aTmplVarsEntries[] = [
  83. 'js_object' => $sJsObject,
  84. 'id' => $aResource[$CNF['FIELD_ID']],
  85. 'title' => $this->getUnitLink($aResource),
  86. 'category' => $aCategories[$aResource[$CNF['FIELD_CATEGORY']]] ?? '',
  87. 'created' => bx_time_js($aResource[$CNF['FIELD_ADDED']]),
  88. 'badges' => $oModule->serviceGetBadges($aResource[$CNF['FIELD_ID']], true),
  89. 'url' => bx_absolute_url($oPermalinks->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aResource[$CNF['FIELD_ID']])),
  90. ];
  91. }
  92. $aTmplVarsLists[] = [
  93. 'js_object' => $sJsObject,
  94. 'html_id_list' => $this->_oConfig->getHtmlIds('list') . $aList['id'],
  95. 'bx_if:allow_edit_list' => [
  96. 'condition' => $bAllowAdd,
  97. 'content' => [
  98. 'js_object' => $sJsObject,
  99. 'context_id' => $iContextId,
  100. 'list_id' => $aList[$CNF['FIELD_LIST_ID']],
  101. 'title' => $sListTitle
  102. ]
  103. ],
  104. 'bx_if:deny_edit_list' => [
  105. 'condition' => !$bAllowAdd,
  106. 'content' => [
  107. 'title' => $sListTitle,
  108. ]
  109. ],
  110. 'bx_if:allow_delete_list' => [
  111. 'condition' => $bAllowManage,
  112. 'content' => [
  113. 'context_id' => $iContextId,
  114. 'list_id' => $aList[$CNF['FIELD_LIST_ID']],
  115. 'js_object' => $sJsObject,
  116. ]
  117. ],
  118. 'bx_if:allow_add' => [
  119. 'condition' => $bAllowAdd,
  120. 'content' => [
  121. 'context_id' => $iContextId,
  122. 'list_id' => $aList[$CNF['FIELD_LIST_ID']],
  123. 'js_object' => $sJsObject,
  124. ]
  125. ],
  126. 'bx_repeat:entries' => $aTmplVarsEntries,
  127. ];
  128. }
  129. $this->addCssJs();
  130. $this->addJs([
  131. 'jquery-ui/jquery-ui.min.js',
  132. 'main.js',
  133. 'modules/base/general/js/|forms.js'
  134. ]);
  135. return $this->getJsCode('main', ['t_confirm_block_deletion' => _t('_bx_resources_txt_msg_confirm_list_deletion')]) . $this->parseHtmlByName('browse_entries.html', [
  136. 'html_id_lists' => $this->_oConfig->getHtmlIds('lists'),
  137. 'bx_repeat:lists' => $aTmplVarsLists,
  138. 'bx_if:allow_add_list' => [
  139. 'condition' => $bAllowAdd,
  140. 'content' => [
  141. 'js_object' => $sJsObject,
  142. 'context_id' => $iContextId,
  143. ]
  144. ],
  145. 'bx_if:allow_add_entry' => [
  146. 'condition' => $bAllowAdd,
  147. 'content' => [
  148. 'js_object' => $sJsObject,
  149. 'context_id' => $iContextId,
  150. ]
  151. ],
  152. ]);
  153. }
  154. public function getAttachLinks($iContentId)
  155. {
  156. $CNF = &$this->getModule()->_oConfig->CNF;
  157. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  158. $aResults = [];
  159. if(($sLink = $aContentInfo[$CNF['FIELD_LINK']] ?: false))
  160. $aResults[] = [
  161. 'url' => $sLink,
  162. 'title' => $aContentInfo[$CNF['FIELD_TITLE']],
  163. 'text' => $aContentInfo[$CNF['FIELD_TEXT']],
  164. 'thumbnail' => ''
  165. ];
  166. return $aResults;
  167. }
  168. }
  169. /** @} */