BxReputationTemplate.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 = !$this->_bIsApi ? (int)getParam($CNF['PARAM_ACTIONS_LIMIT']) : 9999;
  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 $aTmplVarsActions;
  38. $oPaginate = new BxTemplPaginate([
  39. 'start' => $iStart,
  40. 'per_page' => $iLimit,
  41. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}')"
  42. ]);
  43. $oPaginate->setNumFromDataArray($aTmplVarsActions);
  44. $sPaginate = $oPaginate->getSimplePaginate();
  45. $this->addCss(['main.css']);
  46. return $this->parseHtmlByName('block_actions.html', [
  47. 'bx_repeat:actions' => $aTmplVarsActions,
  48. 'bx_if:show_paginate' => [
  49. 'condition' => !empty($sPaginate),
  50. 'content' => [
  51. 'paginate' => $sPaginate
  52. ]
  53. ]
  54. ]);
  55. }
  56. public function getBlockLevels()
  57. {
  58. $oFunctions = BxTemplFunctions::getInstance();
  59. $aLevels = $this->_oDb->getLevels([
  60. 'sample' => 'all',
  61. 'active' => true
  62. ]);
  63. $aTmplVarsLevels = [];
  64. foreach($aLevels as $aLevel) {
  65. list($sIconFont, $sIconUrl, $sIconA, $sIconHtml) = $oFunctions->getIcon($aLevel['icon']);
  66. $bIconFont = !empty($sIconFont);
  67. $bIconHtml = !empty($sIconHtml);
  68. $aTmplVarsLevels[] = array_merge([
  69. 'id' => $aLevel['id'],
  70. 'name' => $aLevel['name'],
  71. 'title' => _t($aLevel['title']),
  72. 'icon' => $aLevel['icon'],
  73. 'points_in' => (int)$aLevel['points_in'],
  74. 'points_out' => (int)$aLevel['points_out'],
  75. ], (!$this->_bIsApi ? [
  76. 'bx_if:icon' => [
  77. 'condition' => $bIconFont || $bIconHtml,
  78. 'content' => [
  79. 'bx_if:icon_font' => [
  80. 'condition' => $bIconFont,
  81. 'content' => [
  82. 'icon' => $sIconFont
  83. ]
  84. ],
  85. 'bx_if:icon_html' => [
  86. 'condition' => $bIconHtml,
  87. 'content' => [
  88. 'icon' => $sIconHtml
  89. ]
  90. ],
  91. ]
  92. ],
  93. 'points' => _t('_bx_reputation_txt_from_to', (int)$aLevel['points_in'], (int)$aLevel['points_out'])
  94. ] : [
  95. 'icon' => $bIconHtml ? $sIconHtml : ''
  96. ]));
  97. }
  98. return $this->_bIsApi ? $aTmplVarsLevels : $this->parseHtmlByName('block_levels.html', [
  99. 'bx_repeat:levels' => $aTmplVarsLevels
  100. ]);
  101. }
  102. public function getBlockSummary($iProfileId, $iContextId = 0)
  103. {
  104. $CNF = &$this->_oConfig->CNF;
  105. $oProfile = BxDolProfile::getInstance($iProfileId);
  106. if(!$oProfile)
  107. return false;
  108. $aProfileInfo = $this->_oDb->getProfiles([
  109. 'sample' => 'profile_id',
  110. 'profile_id' => $iProfileId,
  111. 'context_id' => $iContextId
  112. ]);
  113. $bProfileInfo = !empty($aProfileInfo) && is_array($aProfileInfo);
  114. $aProfileLevels = $this->_oDb->getLevels([
  115. 'sample' => 'profile_id',
  116. 'profile_id' => $iProfileId,
  117. 'context_id' => $iContextId
  118. ]);
  119. $oFunctions = BxTemplFunctions::getInstance();
  120. $aTmplVarsLevels = [];
  121. foreach($aProfileLevels as $aProfileLevel) {
  122. list($sIconFont, $sIconUrl, $sIconA, $sIconHtml) = $oFunctions->getIcon($aProfileLevel['icon']);
  123. $bIconFont = !empty($sIconFont);
  124. $bIconHtml = !empty($sIconHtml);
  125. $sTitle = _t($aProfileLevel['title']);
  126. $aTmplVarsLevels[] = !$this->_bIsApi ? [
  127. 'bx_if:icon' => [
  128. 'condition' => $bIconFont || $bIconHtml,
  129. 'content' => [
  130. 'bx_if:icon_font' => [
  131. 'condition' => $bIconFont,
  132. 'content' => [
  133. 'icon' => $sIconFont
  134. ]
  135. ],
  136. 'bx_if:icon_html' => [
  137. 'condition' => $bIconHtml,
  138. 'content' => [
  139. 'icon' => $sIconHtml
  140. ]
  141. ],
  142. ]
  143. ],
  144. 'title' => $sTitle
  145. ] : [
  146. 'name' => $aProfileLevel['name'],
  147. 'title' => $sTitle,
  148. 'icon' => $bIconHtml ? $sIconHtml : '',
  149. ];
  150. }
  151. $iProfilePoints = $bProfileInfo ? (int)$aProfileInfo['points'] : 0;
  152. if($this->_bIsApi)
  153. return [
  154. 'author_data' => BxDolProfile::getData($iProfileId),
  155. 'points' => $iProfilePoints,
  156. 'levels' => $aTmplVarsLevels,
  157. 'actions_list' => $this->getBlockActions(),
  158. 'levels_list' => $this->getBlockLevels()
  159. ];
  160. return $this->parseHtmlByName('block_summary.html', [
  161. 'profile_image' => $oProfile->getUnit($iProfileId, ['template' => ['name' => 'unit_wo_info', 'size' => 'ava']]),
  162. 'profile_name' => $oProfile->getDisplayName(),
  163. 'points' => $iProfilePoints,
  164. 'history_url' => BxDolPermalinks::getInstance()->permalink($CNF['URL_HISTORY']),
  165. 'bx_repeat:levels' => $aTmplVarsLevels
  166. ]);
  167. }
  168. public function getBlockHistory($iProfileId, $iContextId = 0, $iStart = 0, $iLimit = 0)
  169. {
  170. $CNF = &$this->_oConfig->CNF;
  171. $oModule = $this->getModule();
  172. if(!$iLimit)
  173. $iLimit = !$this->_bIsApi ? (int)getParam($CNF['PARAM_HISTORY_LIMIT']) : 9999;
  174. $aItems = $this->_oDb->getEvents([
  175. 'sample' => 'owner_id',
  176. 'owner_id' => $iProfileId,
  177. 'context_id' => ($iContextId = (int)$iContextId) ? $iContextId : false,
  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($aParams)
  213. {
  214. $CNF = &$this->_oConfig->CNF;
  215. $iContextId = $aParams['context_id'] ?? 0;
  216. $iDays = $aParams['days'] ?? 0;
  217. $sUsername = !empty($aParams['username']) ? $aParams['username'] : false;
  218. $iLimit = !empty($aParams['limit']) ? (int)$aParams['limit'] : (int)getParam($CNF['PARAM_LEADERBOARD_LIMIT']);
  219. $bGrowth = $iDays > 0;
  220. $sName = $aParams['name'] ?? $iContextId . '-' . $iDays;
  221. $bFilters = $aParams['filters'] ?? false;
  222. if($bGrowth)
  223. $aItems = $this->_oDb->getEventsStats([
  224. 'context_id' => $iContextId,
  225. 'days' => $iDays,
  226. 'username' => $sUsername,
  227. 'limit' => $iLimit
  228. ]);
  229. else
  230. $aItems = $this->_oDb->getProfiles([
  231. 'sample' => 'stats',
  232. 'context_id' => $iContextId,
  233. 'username' => $sUsername,
  234. 'limit' => $iLimit
  235. ]);
  236. $aTmplVarsProfiles = [];
  237. foreach($aItems as $iProfileId => $aItem)
  238. if(($iPoints = (int)$aItem['points']) != 0 && ($iProfileId = abs($iProfileId)) && ($oProfile = BxDolProfile::getInstance($iProfileId)) !== false)
  239. $aTmplVarsProfiles[] = [
  240. 'position' => $aItem['position'] ?? 0,
  241. 'unit' => !$this->_bIsApi ? $oProfile->getUnit($iProfileId) : BxDolProfile::getData($oProfile),
  242. 'sign' => $bGrowth ? ($iPoints > 0 ? '+' : '-') : '',
  243. 'points' => $bGrowth ? abs($iPoints) : $iPoints
  244. ];
  245. if($this->_bIsApi)
  246. return array_merge([
  247. 'days' => $iDays,
  248. 'profiles' => $aTmplVarsProfiles,
  249. ], $bFilters ? [
  250. 'request_url' => '/api.php?r=' . $this->_oConfig->getName() . '/get_leaderboard/&params[]=',
  251. 'filter_form' => ['name' => 'filterform', 'data' => $this->getFiltersLeaderboard($aParams)]
  252. ] : []);
  253. $aResult = [
  254. 'content' => $this->parseHtmlByName('block_leaderboard.html', [
  255. 'html_id' => $this->_oConfig->getHtmlIds('leaderboard') . $sName,
  256. 'bx_repeat:profiles' => $aTmplVarsProfiles,
  257. 'bx_if:show_empty' => [
  258. 'condition' => !$aTmplVarsProfiles,
  259. 'content' => [
  260. 'empty' => MsgBox(_t('_Empty'))
  261. ]
  262. ]
  263. ])
  264. ];
  265. if($bFilters) {
  266. $this->addJs(['leaderboard.js']);
  267. $aResult['content'] .= $this->getJsCode('leaderboard', [
  268. 'sName' => $sName,
  269. 'iContextId' => $iContextId,
  270. 'oRequestParams' => ['days' => $iDays]
  271. ]);
  272. $aResult['buttons'] = [
  273. ['title' => _t('_bx_reputation_txt_filters'), 'href' => 'javascript:void(0)', 'onclick' => 'javascript:' . $this->_oConfig->getJsObject('leaderboard') . '.changeLeaderboardFilters(this)']
  274. ];
  275. }
  276. $this->addCss(['main.css']);
  277. return $aResult;
  278. }
  279. public function getFiltersLeaderboard($aParams)
  280. {
  281. $sJsObject = $this->_oConfig->getJsObject('leaderboard');
  282. $aForm = [
  283. 'form_attrs' => [
  284. 'name' => $this->_oConfig->getName() . '_filters',
  285. ],
  286. 'inputs' => [
  287. 'days' => [
  288. 'name' => 'days',
  289. 'type' => 'select',
  290. 'caption' => _t('_bx_reputation_form_filters_input_days'),
  291. 'values' => [
  292. ['key' => 'd-0', 'value' => _t('_bx_reputation_form_filters_input_days_0')],
  293. ['key' => 'd-7', 'value' => _t('_bx_reputation_form_filters_input_days_7')],
  294. ['key' => 'd-30', 'value' => _t('_bx_reputation_form_filters_input_days_30')]
  295. ],
  296. 'value' => 'd-0',
  297. ],
  298. 'username' => [
  299. 'name' => 'username',
  300. 'type' => 'text',
  301. 'caption' => _t('_bx_reputation_form_filters_input_username'),
  302. 'value' => '',
  303. ],
  304. 'controls' => [
  305. 'name' => 'controls',
  306. 'type' => 'input_set',
  307. [
  308. 'name' => 'apply',
  309. 'type' => 'button',
  310. 'value' => _t('_bx_reputation_form_filters_input_do_apply'),
  311. 'attrs' => ['onclick' => $sJsObject . '.onFiltersApply(this)']
  312. ],
  313. [
  314. 'name' => 'reset',
  315. 'type' => 'button',
  316. 'value' => _t('_bx_reputation_form_filters_input_do_reset'),
  317. 'attrs' => ['onclick' => $sJsObject . '.onFiltersReset(this)', 'class' => 'bx-def-margin-sec-left']
  318. ]
  319. ],
  320. ]
  321. ];
  322. $oForm = new BxTemplFormView($aForm);
  323. if($this->_bIsApi) {
  324. unset($oForm->aInputs['controls']);
  325. return $oForm->getCodeApi();
  326. }
  327. $sViewFiltersPopupId = $this->_oConfig->getHtmlIds('filters_popup');
  328. $sViewFiltersPopupContent = $this->parseHtmlByName('popup_filters.html', [
  329. 'js_object' => $sJsObject,
  330. 'content' => $oForm->genRows()
  331. ]);
  332. return BxTemplFunctions::getInstance()->transBox($sViewFiltersPopupId, $sViewFiltersPopupContent, true);
  333. }
  334. }
  335. /** @} */