BxReputationTemplate.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 getBlockActions($iStart = 0, $iLimit = 0)
  18. {
  19. $CNF = &$this->_oConfig->CNF;
  20. if(!$iLimit)
  21. $iLimit = (int)getParam($CNF['PARAM_ACTIONS_LIMIT']);
  22. $aHandlers = $this->_oDb->getHandlers([
  23. 'type' => 'all',
  24. 'active' => true,
  25. 'start' => $iStart,
  26. 'limit' => $iLimit + 1
  27. ]);
  28. $aTmplVarsActions = [];
  29. foreach($aHandlers as $aHandler) {
  30. $aTmplVarsActions[] = [
  31. 'action' => _t('_bx_reputation_txt_unit_action_value', $this->_oModule->getUnitTitle($aHandler['alert_unit']), $this->_oModule->getActionTitle($aHandler['alert_action'])),
  32. 'points_active' => ($iPa = (int)$aHandler['points_active']) > 0 ? '+' . $iPa : '-' . abs($iPa),
  33. 'points_passive' => ($iPp = (int)$aHandler['points_passive']) > 0 ? '+' . $iPp : '-' . abs($iPp)
  34. ];
  35. }
  36. if($this->_bIsApi)
  37. return [
  38. bx_api_get_block('reputation_actions', $aTmplVarsActions)
  39. ];
  40. $oPaginate = new BxTemplPaginate([
  41. 'start' => $iStart,
  42. 'per_page' => $iLimit,
  43. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}')"
  44. ]);
  45. $oPaginate->setNumFromDataArray($aTmplVarsActions);
  46. $sPaginate = $oPaginate->getSimplePaginate();
  47. $this->addCss(['main.css']);
  48. return $this->parseHtmlByName('block_actions.html', [
  49. 'bx_repeat:actions' => $aTmplVarsActions,
  50. 'bx_if:show_paginate' => [
  51. 'condition' => !empty($sPaginate),
  52. 'content' => [
  53. 'paginate' => $sPaginate
  54. ]
  55. ]
  56. ]);
  57. }
  58. public function getBlockLevels()
  59. {
  60. $oFunctions = BxTemplFunctions::getInstance();
  61. $aLevels = $this->_oDb->getLevels([
  62. 'sample' => 'all',
  63. 'active' => true
  64. ]);
  65. $aTmplVarsLevels = [];
  66. foreach($aLevels as $aLevel) {
  67. list($sIconFont, $sIconUrl, $sIconA, $sIconHtml) = $oFunctions->getIcon($aLevel['icon']);
  68. $bIconFont = !empty($sIconFont);
  69. $bIconHtml = !empty($sIconHtml);
  70. $aTmplVarsLevels[] = array_merge([
  71. 'id' => $aLevel['id'],
  72. 'name' => $aLevel['name'],
  73. 'title' => _t($aLevel['title']),
  74. 'icon' => $aLevel['icon'],
  75. 'points_in' => (int)$aLevel['points_in'],
  76. 'points_out' => (int)$aLevel['points_out'],
  77. ], (!$this->_bIsApi ? [
  78. 'bx_if:icon' => [
  79. 'condition' => $bIconFont || $bIconHtml,
  80. 'content' => [
  81. 'bx_if:icon_font' => [
  82. 'condition' => $bIconFont,
  83. 'content' => [
  84. 'icon' => $sIconFont
  85. ]
  86. ],
  87. 'bx_if:icon_html' => [
  88. 'condition' => $bIconHtml,
  89. 'content' => [
  90. 'icon' => $sIconHtml
  91. ]
  92. ],
  93. ]
  94. ],
  95. 'points' => _t('_bx_reputation_txt_from_to', (int)$aLevel['points_in'], (int)$aLevel['points_out'])
  96. ] : [
  97. 'icon' => $bIconHtml ? $sIconHtml : ''
  98. ]));
  99. }
  100. if($this->_bIsApi)
  101. return [
  102. bx_api_get_block('reputation_levels', $aTmplVarsLevels)
  103. ];
  104. return $this->parseHtmlByName('block_levels.html', [
  105. 'bx_repeat:levels' => $aTmplVarsLevels
  106. ]);
  107. }
  108. public function getBlockSummary($iProfileId)
  109. {
  110. $CNF = &$this->_oConfig->CNF;
  111. $oProfile = BxDolProfile::getInstance($iProfileId);
  112. if(!$oProfile)
  113. return false;
  114. $aProfileInfo = $this->_oDb->getProfiles(['sample' => 'id', 'id' => $iProfileId]);
  115. $bProfileInfo = !empty($aProfileInfo) && is_array($aProfileInfo);
  116. $aProfileLevels = $this->_oDb->getLevels([
  117. 'sample' => 'profile_id',
  118. 'profile_id' => $iProfileId
  119. ]);
  120. $oFunctions = BxTemplFunctions::getInstance();
  121. $aTmplVarsLevels = [];
  122. foreach($aProfileLevels as $aProfileLevel) {
  123. list($sIconFont, $sIconUrl, $sIconA, $sIconHtml) = $oFunctions->getIcon($aProfileLevel['icon']);
  124. $bIconFont = !empty($sIconFont);
  125. $bIconHtml = !empty($sIconHtml);
  126. $sTitle = _t($aProfileLevel['title']);
  127. $aTmplVarsLevels[] = !$this->_bIsApi ? [
  128. 'bx_if:icon' => [
  129. 'condition' => $bIconFont || $bIconHtml,
  130. 'content' => [
  131. 'bx_if:icon_font' => [
  132. 'condition' => $bIconFont,
  133. 'content' => [
  134. 'icon' => $sIconFont
  135. ]
  136. ],
  137. 'bx_if:icon_html' => [
  138. 'condition' => $bIconHtml,
  139. 'content' => [
  140. 'icon' => $sIconHtml
  141. ]
  142. ],
  143. ]
  144. ],
  145. 'title' => $sTitle
  146. ] : [
  147. 'name' => $aProfileLevel['name'],
  148. 'title' => $sTitle,
  149. 'icon' => $bIconHtml ? $sIconHtml : '',
  150. ];
  151. }
  152. $iProfilePoints = $bProfileInfo ? (int)$aProfileInfo['points'] : 0;
  153. if($this->_bIsApi)
  154. return [
  155. bx_api_get_block('reputation_summary', [
  156. 'author_data' => BxDolProfile::getData($iProfileId),
  157. 'points' => $iProfilePoints,
  158. 'levels' => $aTmplVarsLevels,
  159. ])
  160. ];
  161. return $this->parseHtmlByName('block_summary.html', [
  162. 'profile_image' => $oProfile->getUnit($iProfileId, ['template' => ['name' => 'unit_wo_info', 'size' => 'ava']]),
  163. 'profile_name' => $oProfile->getDisplayName(),
  164. 'points' => $iProfilePoints,
  165. 'history_url' => BxDolPermalinks::getInstance()->permalink($CNF['URL_HISTORY']),
  166. 'bx_repeat:levels' => $aTmplVarsLevels
  167. ]);
  168. }
  169. public function getBlockHistory($iProfileId, $iStart = 0, $iLimit = 0)
  170. {
  171. $CNF = &$this->_oConfig->CNF;
  172. $oModule = $this->getModule();
  173. if(!$iLimit)
  174. $iLimit = (int)getParam($CNF['PARAM_HISTORY_LIMIT']);
  175. $aItems = $this->_oDb->getEvents([
  176. 'sample' => 'owner_id',
  177. 'owner_id' => $iProfileId,
  178. 'start' => $iStart,
  179. 'limit' => $iLimit + 1
  180. ]);
  181. $aTmplVarsItems = [];
  182. foreach($aItems as $aItem) {
  183. $aTmplVarsItems[] = [
  184. 'unit' => $oModule->getUnitTitle($aItem['type']),
  185. 'action' => $oModule->getActionTitle($aItem['action']),
  186. 'points' => ($iPoints = (int)$aItem['points']) > 0 ? '+' . $iPoints : '-' . abs($iPoints),
  187. 'date' => $this->_bIsApi ? $aItem['date'] : bx_time_js($aItem['date'])
  188. ];
  189. }
  190. if($this->_bIsApi)
  191. return [
  192. bx_api_get_block('reputation_history', $aTmplVarsItems)
  193. ];
  194. $oPaginate = new BxTemplPaginate([
  195. 'start' => $iStart,
  196. 'per_page' => $iLimit,
  197. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}')"
  198. ]);
  199. $oPaginate->setNumFromDataArray($aTmplVarsItems);
  200. $sPaginate = $oPaginate->getSimplePaginate();
  201. $this->addCss(['main.css']);
  202. return $this->parseHtmlByName('block_history.html', [
  203. 'bx_repeat:items' => $aTmplVarsItems,
  204. 'bx_if:show_paginate' => [
  205. 'condition' => !empty($sPaginate),
  206. 'content' => [
  207. 'paginate' => $sPaginate
  208. ]
  209. ]
  210. ]);
  211. }
  212. public function getBlockLeaderboard($iDays = 0, $iLimit = 0)
  213. {
  214. $CNF = &$this->_oConfig->CNF;
  215. if(!$iLimit)
  216. $iLimit = (int)getParam($CNF['PARAM_LEADERBOARD_LIMIT']);
  217. $bGrowth = $iDays > 0;
  218. if($bGrowth)
  219. $aItems = $this->_oDb->getEvents(['sample' => 'stats', 'days' => $iDays, 'limit' => $iLimit]);
  220. else
  221. $aItems = $this->_oDb->getProfiles(['sample' => 'stats', 'limit' => $iLimit]);
  222. $aTmplVarsProfiles = [];
  223. foreach($aItems as $iProfileId => $iPoints)
  224. if($iPoints != 0 && ($iProfileId = abs($iProfileId)) && ($oProfile = BxDolProfile::getInstance($iProfileId)) !== false)
  225. $aTmplVarsProfiles[] = [
  226. 'unit' => !$this->_bIsApi ? $oProfile->getUnit($iProfileId) : BxDolProfile::getData($oProfile),
  227. 'sign' => $bGrowth ? ($iPoints > 0 ? '+' : '-') : '',
  228. 'points' => $bGrowth ? abs($iPoints) : $iPoints
  229. ];
  230. if($this->_bIsApi)
  231. return [
  232. bx_api_get_block('reputation_leaderboard', [
  233. 'days' => $iDays,
  234. 'profiles' => $aTmplVarsProfiles
  235. ])
  236. ];
  237. $this->addCss(['main.css']);
  238. return $this->parseHtmlByName('block_leaderboard.html', [
  239. 'bx_repeat:profiles' => $aTmplVarsProfiles
  240. ]);
  241. }
  242. }
  243. /** @} */