BxReputationTemplate.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. $bManage = $aParams['manage'] ?? false;
  223. if($bGrowth)
  224. $aItems = $this->_oDb->getEventsStats([
  225. 'context_id' => $iContextId,
  226. 'days' => $iDays,
  227. 'username' => $sUsername,
  228. 'limit' => $iLimit
  229. ]);
  230. else
  231. $aItems = $this->_oDb->getProfiles([
  232. 'sample' => 'stats',
  233. 'context_id' => $iContextId,
  234. 'username' => $sUsername,
  235. 'limit' => $iLimit
  236. ]);
  237. $aTmplVarsProfiles = [];
  238. foreach($aItems as $iProfileId => $aItem)
  239. if(($iPoints = (int)$aItem['points']) != 0 && ($iProfileId = abs($iProfileId)) && ($oProfile = BxDolProfile::getInstance($iProfileId)) !== false)
  240. $aTmplVarsProfiles[] = [
  241. 'position' => $aItem['position'] ?? 0,
  242. 'unit' => !$this->_bIsApi ? $oProfile->getUnit($iProfileId) : BxDolProfile::getData($oProfile),
  243. 'sign' => $bGrowth ? ($iPoints > 0 ? '+' : '-') : '',
  244. 'points' => $bGrowth ? abs($iPoints) : $iPoints
  245. ];
  246. if($this->_bIsApi)
  247. return array_merge([
  248. 'days' => $iDays,
  249. 'profiles' => $aTmplVarsProfiles,
  250. ], $bFilters ? [
  251. 'request_url' => '/api.php?r=' . $this->_oConfig->getName() . '/get_leaderboard/&params[]=',
  252. 'filter_form' => ['name' => 'filterform', 'data' => $this->getFiltersLeaderboard($aParams)]
  253. ] : []);
  254. $aResult = [
  255. 'content' => $this->parseHtmlByName('block_leaderboard.html', [
  256. 'html_id' => $this->_oConfig->getHtmlIds('leaderboard') . $sName,
  257. 'bx_repeat:profiles' => $aTmplVarsProfiles,
  258. 'bx_if:show_empty' => [
  259. 'condition' => !$aTmplVarsProfiles,
  260. 'content' => [
  261. 'empty' => MsgBox(_t('_Empty'))
  262. ]
  263. ]
  264. ])
  265. ];
  266. if($bFilters) {
  267. $this->addJs(['leaderboard.js']);
  268. $aResult['content'] .= $this->getJsCode('leaderboard', [
  269. 'sName' => $sName,
  270. 'iContextId' => $iContextId,
  271. 'oRequestParams' => ['days' => $iDays]
  272. ]);
  273. if(!isset($aResult['buttons']))
  274. $aResult['buttons'] = [];
  275. $aResult['buttons'][] = ['title' => _t('_bx_reputation_txt_filters'), 'href' => 'javascript:void(0)', 'onclick' => 'javascript:' . $this->_oConfig->getJsObject('leaderboard') . '.changeLeaderboardFilters(this)'];
  276. }
  277. if($bManage && $this->getModule()->checkAllowedManage($iContextId) === CHECK_ACTION_RESULT_ALLOWED)
  278. $aResult['buttons'][] = [
  279. 'title' => _t('_bx_reputation_txt_manage'),
  280. 'href' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_MANAGE'], ['context_id' => $iContextId])
  281. ];
  282. $this->addCss(['main.css']);
  283. return $aResult;
  284. }
  285. public function getFiltersLeaderboard($aParams)
  286. {
  287. $sJsObject = $this->_oConfig->getJsObject('leaderboard');
  288. $aForm = [
  289. 'form_attrs' => [
  290. 'name' => $this->_oConfig->getName() . '_filters',
  291. ],
  292. 'inputs' => [
  293. 'days' => [
  294. 'name' => 'days',
  295. 'type' => 'select',
  296. 'caption' => _t('_bx_reputation_form_filters_input_days'),
  297. 'values' => [
  298. ['key' => 'd-0', 'value' => _t('_bx_reputation_form_filters_input_days_0')],
  299. ['key' => 'd-7', 'value' => _t('_bx_reputation_form_filters_input_days_7')],
  300. ['key' => 'd-30', 'value' => _t('_bx_reputation_form_filters_input_days_30')]
  301. ],
  302. 'value' => 'd-0',
  303. ],
  304. 'username' => [
  305. 'name' => 'username',
  306. 'type' => 'text',
  307. 'caption' => _t('_bx_reputation_form_filters_input_username'),
  308. 'value' => '',
  309. ],
  310. 'controls' => [
  311. 'name' => 'controls',
  312. 'type' => 'input_set',
  313. [
  314. 'name' => 'apply',
  315. 'type' => 'button',
  316. 'value' => _t('_bx_reputation_form_filters_input_do_apply'),
  317. 'attrs' => ['onclick' => $sJsObject . '.onFiltersApply(this)']
  318. ],
  319. [
  320. 'name' => 'reset',
  321. 'type' => 'button',
  322. 'value' => _t('_bx_reputation_form_filters_input_do_reset'),
  323. 'attrs' => ['onclick' => $sJsObject . '.onFiltersReset(this)', 'class' => 'bx-def-margin-sec-left']
  324. ]
  325. ],
  326. ]
  327. ];
  328. $oForm = new BxTemplFormView($aForm);
  329. if($this->_bIsApi) {
  330. unset($oForm->aInputs['controls']);
  331. return $oForm->getCodeApi();
  332. }
  333. $sViewFiltersPopupId = $this->_oConfig->getHtmlIds('filters_popup');
  334. $sViewFiltersPopupContent = $this->parseHtmlByName('popup_filters.html', [
  335. 'js_object' => $sJsObject,
  336. 'content' => $oForm->genRows()
  337. ]);
  338. return BxTemplFunctions::getInstance()->transBox($sViewFiltersPopupId, $sViewFiltersPopupContent, true);
  339. }
  340. }
  341. /** @} */