BxReputationTemplate.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 Reputation Reputation
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxReputationTemplate extends BxBaseModNotificationsTemplate
  12. {
  13. public function __construct(&$oConfig, &$oDb)
  14. {
  15. parent::__construct($oConfig, $oDb);
  16. }
  17. public function getBlockLevels()
  18. {
  19. $oFunctions = BxTemplFunctions::getInstance();
  20. $aLevels = $this->_oDb->getLevels([
  21. 'sample' => 'all',
  22. 'active' => true
  23. ]);
  24. $aTmplVarsLevels = [];
  25. foreach($aLevels as $aLevel) {
  26. list($sIconFont, $sIconUrl, $sIconA, $sIconHtml) = $oFunctions->getIcon($aLevel['icon']);
  27. $bIconFont = !empty($sIconFont);
  28. $bIconHtml = !empty($sIconHtml);
  29. $aTmplVarsLevels[] = array_merge([
  30. 'id' => $aLevel['id'],
  31. 'name' => $aLevel['name'],
  32. 'title' => _t($aLevel['title']),
  33. 'icon' => $aLevel['icon'],
  34. 'points_in' => (int)$aLevel['points_in'],
  35. 'points_out' => (int)$aLevel['points_out'],
  36. ], (!$this->_bIsApi ? [
  37. 'bx_if:icon' => [
  38. 'condition' => $bIconFont || $bIconHtml,
  39. 'content' => [
  40. 'bx_if:icon_font' => [
  41. 'condition' => $bIconFont,
  42. 'content' => [
  43. 'icon' => $sIconFont
  44. ]
  45. ],
  46. 'bx_if:icon_html' => [
  47. 'condition' => $bIconHtml,
  48. 'content' => [
  49. 'icon' => $sIconHtml
  50. ]
  51. ],
  52. ]
  53. ],
  54. 'points' => _t('_bx_reputation_txt_from_to', (int)$aLevel['points_in'], (int)$aLevel['points_out'])
  55. ] : [
  56. 'icon' => $bIconHtml ? $sIconHtml : ''
  57. ]));
  58. }
  59. return $this->parseHtmlByName('block_levels.html', [
  60. 'bx_repeat:levels' => $aTmplVarsLevels
  61. ]);
  62. }
  63. public function getBlockSummary($iProfileId)
  64. {
  65. $CNF = &$this->_oConfig->CNF;
  66. $oProfile = BxDolProfile::getInstance($iProfileId);
  67. if(!$oProfile)
  68. return false;
  69. $aProfileInfo = $this->_oDb->getProfiles(['sample' => 'id', 'id' => $iProfileId]);
  70. $bProfileInfo = !empty($aProfileInfo) && is_array($aProfileInfo);
  71. $aProfileLevels = $this->_oDb->getLevels([
  72. 'sample' => 'profile_id',
  73. 'profile_id' => $iProfileId
  74. ]);
  75. $oFunctions = BxTemplFunctions::getInstance();
  76. $aTmplVarsLevels = [];
  77. foreach($aProfileLevels as $aProfileLevel) {
  78. list($sIconFont, $sIconUrl, $sIconA, $sIconHtml) = $oFunctions->getIcon($aProfileLevel['icon']);
  79. $bIconFont = !empty($sIconFont);
  80. $bIconHtml = !empty($sIconHtml);
  81. $sTitle = _t($aProfileLevel['title']);
  82. $aTmplVarsLevels[] = !$this->_bIsApi ? [
  83. 'bx_if:icon' => [
  84. 'condition' => $bIconFont || $bIconHtml,
  85. 'content' => [
  86. 'bx_if:icon_font' => [
  87. 'condition' => $bIconFont,
  88. 'content' => [
  89. 'icon' => $sIconFont
  90. ]
  91. ],
  92. 'bx_if:icon_html' => [
  93. 'condition' => $bIconHtml,
  94. 'content' => [
  95. 'icon' => $sIconHtml
  96. ]
  97. ],
  98. ]
  99. ],
  100. 'title' => $sTitle
  101. ] : [
  102. 'name' => $aProfileLevel['name'],
  103. 'title' => $sTitle,
  104. 'icon' => $bIconHtml ? $sIconHtml : '',
  105. ];
  106. }
  107. $iProfilePoints = $bProfileInfo ? (int)$aProfileInfo['points'] : 0;
  108. if($this->_bIsApi)
  109. return [
  110. bx_api_get_block('reputation_summary', [
  111. 'author_data' => BxDolProfile::getData($iProfileId),
  112. 'points' => $iProfilePoints,
  113. 'levels' => $aTmplVarsLevels,
  114. ])
  115. ];
  116. return $this->parseHtmlByName('block_summary.html', [
  117. 'profile_image' => $oProfile->getUnit($iProfileId, ['template' => ['name' => 'unit_wo_info', 'size' => 'ava']]),
  118. 'profile_name' => $oProfile->getDisplayName(),
  119. 'points' => $iProfilePoints,
  120. 'history_url' => BxDolPermalinks::getInstance()->permalink($CNF['URL_HISTORY']),
  121. 'bx_repeat:levels' => $aTmplVarsLevels
  122. ]);
  123. }
  124. public function getBlockHistory($iProfileId, $iStart = 0, $iLimit = 0)
  125. {
  126. $CNF = &$this->_oConfig->CNF;
  127. $oModule = $this->getModule();
  128. if(!$iLimit)
  129. $iLimit = (int)getParam($CNF['PARAM_HISTORY_LIMIT']);
  130. $aItems = $this->_oDb->getEvents([
  131. 'sample' => 'owner_id',
  132. 'owner_id' => $iProfileId,
  133. 'start' => $iStart,
  134. 'limit' => $iLimit + 1
  135. ]);
  136. $aTmplVarsItems = [];
  137. foreach($aItems as $aItem) {
  138. $aTmplVarsItems[] = [
  139. 'unit' => $oModule->getUnitTitle($aItem['type']),
  140. 'action' => $oModule->getActionTitle($aItem['action']),
  141. 'points' => ($iPoints = (int)$aItem['points']) > 0 ? '+' . $iPoints : $iPoints,
  142. 'date' => bx_time_js($aItem['date'])
  143. ];
  144. }
  145. if($this->_bIsApi)
  146. return $aTmplVarsItems;
  147. $oPaginate = new BxTemplPaginate([
  148. 'start' => $iStart,
  149. 'per_page' => $iLimit,
  150. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}')"
  151. ]);
  152. $oPaginate->setNumFromDataArray($aTmplVarsItems);
  153. $sPaginate = $oPaginate->getSimplePaginate();
  154. return $this->parseHtmlByName('block_history.html', [
  155. 'bx_repeat:items' => $aTmplVarsItems,
  156. 'bx_if:show_paginate' => [
  157. 'condition' => !empty($sPaginate),
  158. 'content' => [
  159. 'paginate' => $sPaginate
  160. ]
  161. ]
  162. ]);
  163. }
  164. public function getBlockLeaderboard($iDays = 0, $iLimit = 0)
  165. {
  166. $CNF = &$this->_oConfig->CNF;
  167. if(!$iLimit)
  168. $iLimit = (int)getParam($CNF['PARAM_LEADERBOARD_LIMIT']);
  169. $bGrowth = $iDays > 0;
  170. if($bGrowth)
  171. $aItems = $this->_oDb->getEvents(['sample' => 'stats', 'days' => $iDays, 'limit' => $iLimit]);
  172. else
  173. $aItems = $this->_oDb->getProfiles(['sample' => 'stats', 'limit' => $iLimit]);
  174. $aTmplVarsProfiles = [];
  175. foreach($aItems as $iProfileId => $iPoints)
  176. if($iPoints != 0 && ($iProfileId = abs($iProfileId)) && ($oProfile = BxDolProfile::getInstance($iProfileId)) !== false)
  177. $aTmplVarsProfiles[] = [
  178. 'unit' => !$this->_bIsApi ? $oProfile->getUnit($iProfileId) : BxDolProfile::getData($oProfile),
  179. 'sign' => $bGrowth ? ($iPoints > 0 ? '+' : '-') : '',
  180. 'points' => $bGrowth ? abs($iPoints) : $iPoints
  181. ];
  182. if($this->_bIsApi)
  183. return [
  184. bx_api_get_block('reputation_leaderboard', [
  185. 'days' => $iDays,
  186. 'profiles' => $aTmplVarsProfiles
  187. ])
  188. ];
  189. $this->addCss(['main.css']);
  190. return $this->parseHtmlByName('block_leaderboard.html', [
  191. 'bx_repeat:profiles' => $aTmplVarsProfiles
  192. ]);
  193. }
  194. }
  195. /** @} */