BxBaseModGeneralMenuSnippetMeta.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. class BxBaseModGeneralMenuSnippetMeta extends BxTemplMenuUnitMeta
  12. {
  13. protected $_bShowZeros;
  14. protected $_sModule;
  15. protected $_oModule;
  16. protected $_iContentId;
  17. protected $_aContentInfo;
  18. public function __construct($aObject, $oTemplate = false)
  19. {
  20. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  21. if(empty($oTemplate))
  22. $oTemplate = $this->_oModule->_oTemplate;
  23. parent::__construct($aObject, $oTemplate);
  24. $this->_sStylePrefix = 'bx-base-general-unit-meta';
  25. $this->_bShowZeros = false;
  26. }
  27. public function getCode()
  28. {
  29. if(empty($this->_aContentInfo) || !is_array($this->_aContentInfo))
  30. return '';
  31. return parent::getCode();
  32. }
  33. public function setContentId($iContentId)
  34. {
  35. $this->_iContentId = (int)$iContentId;
  36. $this->_aContentInfo = $this->_oModule->_oDb->getContentInfoById($this->_iContentId);
  37. if($this->_aContentInfo)
  38. $this->addMarkers(array('content_id' => (int)$this->_iContentId));
  39. }
  40. protected function _getMenuItemAPI($aItem, $mixedType = 'text', $aData = [])
  41. {
  42. $sDisplayType = 'link';
  43. $sContentType = 'text';
  44. if(is_array($mixedType)) {
  45. if(isset($mixedType['display']))
  46. $sDisplayType = $mixedType['display'];
  47. if(isset($mixedType['content']))
  48. $sContentType = $mixedType['content'];
  49. }
  50. else
  51. $sContentType = $mixedType;
  52. $aItemData = array_merge([
  53. 'id' => $aItem['id'],
  54. 'name' => $aItem['name'],
  55. 'display_type' => $sDisplayType,
  56. 'content_type' => $sContentType,
  57. 'title' => $aItem['title'],
  58. 'link' => !empty($aItem['link']) && $aItem['link'] != 'javascript:void(0)' ? $aItem['link'] : ''
  59. ], $aData);
  60. return $aItemData;
  61. }
  62. protected function _getMenuItemAuthor($aItem)
  63. {
  64. $CNF = &$this->_oModule->_oConfig->CNF;
  65. if($this->_bIsApi)
  66. return $this->_getMenuItemAPI($aItem, 'profile', [
  67. 'data' => BxDolProfile::getData($this->_aContentInfo[$CNF['FIELD_AUTHOR']])
  68. ]);
  69. $oProfile = BxDolProfile::getInstanceMagic($this->_aContentInfo[$CNF['FIELD_AUTHOR']]);
  70. return $this->getUnitMetaItemLink($oProfile->getDisplayName(), array(
  71. 'href' => $oProfile->getUrl(),
  72. 'class' => 'bx-base-text-unit-author'
  73. ));
  74. }
  75. protected function _getMenuItemDate($aItem)
  76. {
  77. $CNF = &$this->_oModule->_oConfig->CNF;
  78. if($this->_bIsApi)
  79. return $this->_getMenuItemAPI($aItem, 'time', [
  80. 'title' => $this->_aContentInfo[$CNF['FIELD_ADDED']]
  81. ]);
  82. if ($aItem['icon'] == '')
  83. return $this->getUnitMetaItemText(bx_time_js($this->_aContentInfo[$CNF['FIELD_ADDED']], BX_FORMAT_DATE));
  84. else
  85. return $this->getUnitMetaItemExtended(bx_time_js($this->_aContentInfo[$CNF['FIELD_ADDED']], BX_FORMAT_DATE), $aItem['icon'], '');
  86. }
  87. protected function _getMenuItemCategory($aItem)
  88. {
  89. $CNF = &$this->_oModule->_oConfig->CNF;
  90. if(empty($CNF['OBJECT_CATEGORY']) || empty($CNF['FIELD_CATEGORY']) || empty($this->_aContentInfo[$CNF['FIELD_CATEGORY']]))
  91. return false;
  92. $oCategory = BxDolCategory::getObjectInstance($CNF['OBJECT_CATEGORY']);
  93. if(!$oCategory)
  94. return false;
  95. $sTitle = $oCategory->getCategoryTitle($this->_aContentInfo[$CNF['FIELD_CATEGORY']]);
  96. if($this->_bIsApi)
  97. return $this->_getMenuItemAPI($aItem, 'text', [
  98. 'title' => $sTitle,
  99. 'link' => $oCategory->getCategoryUrl($this->_aContentInfo[$CNF['FIELD_CATEGORY']])
  100. ]);
  101. return $this->getUnitMetaItemCustom($oCategory->getCategoryLink($sTitle, $this->_aContentInfo[$CNF['FIELD_CATEGORY']]));
  102. }
  103. protected function _getMenuItemTags($aItem)
  104. {
  105. $CNF = &$this->_oModule->_oConfig->CNF;
  106. if(empty($CNF['OBJECT_METATAGS']))
  107. return false;
  108. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  109. if(!$oMetatags || !$oMetatags->keywordsIsEnabled())
  110. return false;
  111. if($this->_bIsApi)
  112. return false;
  113. return $this->getUnitMetaItemCustom($oMetatags->getKeywordsList($this->_aContentInfo[$CNF['FIELD_ID']], 3));
  114. }
  115. protected function _getMenuItemViews($aItem)
  116. {
  117. $CNF = &$this->_oModule->_oConfig->CNF;
  118. if(empty($CNF['FIELD_VIEWS']) || (empty($this->_aContentInfo[$CNF['FIELD_VIEWS']]) && !$this->_bShowZeros))
  119. return false;
  120. $sTitle = _t('_view_n_views', $this->_aContentInfo[$CNF['FIELD_VIEWS']]);
  121. if($this->_bIsApi)
  122. return $this->_getMenuItemAPI($aItem, 'text', [
  123. 'title' => $sTitle
  124. ]);
  125. return $this->getUnitMetaItemText($sTitle);
  126. }
  127. protected function _getMenuItemVotes($aItem)
  128. {
  129. $CNF = &$this->_oModule->_oConfig->CNF;
  130. if(empty($CNF['OBJECT_VOTES']) || (empty($this->_aContentInfo[$CNF['FIELD_VOTES']]) && !$this->_bShowZeros))
  131. return false;
  132. $sTitle = _t('_vote_n_votes', $this->_aContentInfo[$CNF['FIELD_VOTES']]);
  133. if($this->_bIsApi)
  134. return $this->_getMenuItemAPI($aItem, 'text', [
  135. 'title' => $sTitle
  136. ]);
  137. return $this->getUnitMetaItemText($sTitle);
  138. }
  139. protected function _getMenuItemRating($aItem)
  140. {
  141. $CNF = &$this->_oModule->_oConfig->CNF;
  142. if(empty($CNF['OBJECT_VOTES_STARS']))
  143. return false;
  144. if($this->_bIsApi)
  145. return false;
  146. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_STARS'], $this->_aContentInfo[$CNF['FIELD_ID']]);
  147. if(!$oVotes)
  148. return false;
  149. return $this->getUnitMetaItemCustom($oVotes->getElementInline(array('show_counter' => true)));
  150. }
  151. protected function _getMenuItemReactions($aItem)
  152. {
  153. $CNF = &$this->_oModule->_oConfig->CNF;
  154. if(empty($CNF['OBJECT_REACTIONS']))
  155. return false;
  156. if($this->_bIsApi)
  157. return false;
  158. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_REACTIONS'], $this->_aContentInfo[$CNF['FIELD_ID']]);
  159. if(!$oVotes)
  160. return false;
  161. return $this->getUnitMetaItemCustom($oVotes->getElementInline(array('show_counter' => false)));
  162. }
  163. protected function _getMenuItemComments($aItem)
  164. {
  165. $CNF = &$this->_oModule->_oConfig->CNF;
  166. if(empty($CNF['OBJECT_COMMENTS']) || empty($CNF['FIELD_COMMENTS']) || (empty($this->_aContentInfo[$CNF['FIELD_COMMENTS']]) && !$this->_bShowZeros))
  167. return false;
  168. $oComments = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $this->_aContentInfo[$CNF['FIELD_ID']]);
  169. if(!$oComments || !$oComments->isEnabled())
  170. return false;
  171. $sTitle = _t('_cmt_txt_n_comments', $oComments->getCommentsCountAll(0, true));
  172. $sUrl = $oComments->getListUrl();
  173. if($this->_bIsApi)
  174. return $this->_getMenuItemAPI($aItem, 'text', [
  175. 'title' => $sTitle,
  176. 'link' => $sUrl
  177. ]);
  178. return $this->getUnitMetaItemLink($sTitle, [
  179. 'href' => $sUrl
  180. ]);
  181. }
  182. protected function _getMenuItemDefault($aItem)
  183. {
  184. $sResult = false;
  185. $a = array(
  186. 'res' => &$sResult,
  187. 'menu' => $this->_sObject,
  188. 'menu_object' => $this,
  189. 'item' => $aItem,
  190. 'module' => $this->_sModule,
  191. 'content_id' => $this->_iContentId,
  192. 'content_data' => $this->_aContentInfo,
  193. );
  194. bx_alert($this->_sModule, 'menu_custom_item', 0, 0, $a);
  195. bx_alert('menu', 'menu_custom_item', 0, 0, $a);
  196. if (false !== $sResult)
  197. return $sResult;
  198. return parent::_getMenuItemDefault($aItem);
  199. }
  200. }
  201. /** @} */