BxBaseScore.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * @see BxDolScore
  11. */
  12. class BxBaseScore extends BxDolScore
  13. {
  14. protected $_bCssJsAdded;
  15. protected $_sJsObjName;
  16. protected $_sStylePrefix;
  17. protected $_aHtmlIds;
  18. protected $_sTmplNameLegend;
  19. protected $_sTmplNameByList;
  20. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  21. {
  22. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  23. $this->_bCssJsAdded = false;
  24. $this->_sJsObjName = 'oScore' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  25. $this->_sStylePrefix = 'bx-score';
  26. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  27. $this->_aHtmlIds = array(
  28. 'main' => 'bx-score-' . $sHtmlId,
  29. 'counter' => 'bx-score-counter-' . $sHtmlId,
  30. 'by_popup' => 'bx-score-by-popup-' . $sHtmlId,
  31. 'legend' => 'bx-score-legend-' . $sHtmlId,
  32. );
  33. $this->_aElementDefaults = [
  34. 'show_do_vote_as_button' => false,
  35. 'show_do_vote_as_button_small' => false,
  36. 'show_do_vote_icon' => true,
  37. 'show_do_vote_label' => false,
  38. 'show_counter' => true,
  39. 'show_counter_only' => true,
  40. 'show_counter_empty' => true,
  41. 'show_counter_label_icon' => false,
  42. 'show_counter_label_text' => true,
  43. 'show_legend' => false,
  44. 'show_script' => true
  45. ];
  46. $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
  47. 'show_counter' => true,
  48. ]);
  49. $this->_sTmplNameLegend = 'score_legend.html';
  50. $this->_sTmplNameByList = 'score_by_list.html';
  51. $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('score_element_block.html');
  52. $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('score_element_inline.html');
  53. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('score_do_vote_label.html');
  54. $this->_sTmplContentCounterLabel = $this->_oTemplate->getHtml('score_counter_label.html');
  55. }
  56. public function getJsObjectName()
  57. {
  58. return $this->_sJsObjName;
  59. }
  60. public function getJsScript($aParams = array())
  61. {
  62. $sJsObjName = $this->getJsObjectName();
  63. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  64. $aParamsJs = array(
  65. 'sObjName' => $sJsObjName,
  66. 'sSystem' => $this->getSystemName(),
  67. 'iAuthorId' => $this->_getAuthorId(),
  68. 'iObjId' => $this->getId(),
  69. 'sRootUrl' => BX_DOL_URL_ROOT,
  70. 'sStylePrefix' => $this->_sStylePrefix,
  71. 'aHtmlIds' => $this->_aHtmlIds,
  72. 'aRequestParams' => $this->_prepareRequestParamsData($aParams)
  73. );
  74. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new BxDolScore(" . json_encode($aParamsJs) . ");";
  75. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  76. }
  77. public function getJsClick($sType)
  78. {
  79. return $this->getJsObjectName() . '.vote' . ucfirst($sType) . '(this)';
  80. }
  81. public function getJsClickCounter()
  82. {
  83. return $this->getJsObjectName() . '.toggleByPopup(this)';
  84. }
  85. public function getCounter($aParams = [])
  86. {
  87. $aParams = array_merge($this->_aElementDefaults, $aParams);
  88. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  89. $bShowEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  90. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  91. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  92. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  93. $bAllowedViewViewVoters = $this->isAllowedVoteViewVoters();
  94. $aScore = $this->_oQuery->getScore($this->getId());
  95. $iCup = (int)$aScore['count_up'];
  96. $iCdown = (int)(int)$aScore['count_down'];
  97. $bEmpty = $iCup == 0 && $iCdown == 0;
  98. $sClass = 'sys-action-counter';
  99. if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
  100. $sClass .= ' sys-ac-only';
  101. $sClass .= ' ' . $this->_sStylePrefix . '-counter';
  102. if($bShowDoVoteAsButtonSmall)
  103. $sClass .= ' bx-btn-small-height';
  104. if($bShowDoVoteAsButton)
  105. $sClass .= ' bx-btn-height';
  106. $bLink = $bAllowedViewViewVoters && !$bEmpty;
  107. $sCounter = !$bEmpty || $bShowEmpty ? $this->_getCounterLabel($iCup - $iCdown, $aParams) : '';
  108. $aTmplVarsText = [
  109. 'id' => $this->_aHtmlIds['counter'],
  110. 'class' => $sClass,
  111. 'content' => $sCounter,
  112. 'title' => _t($this->_getTitleDoBy()),
  113. ];
  114. $aTmplVarsLink = array_merge($aTmplVarsText, [
  115. 'onclick' => $bLink ? 'javascript:' . $this->getJsClickCounter() : ''
  116. ]);
  117. return $this->_oTemplate->parseHtmlByName('score_counter.html', array(
  118. 'bx_if:show_link' => array(
  119. 'condition' => $bLink,
  120. 'content' => $aTmplVarsLink
  121. ),
  122. 'bx_if:show_text' => array(
  123. 'condition' => !$bLink,
  124. 'content' => $aTmplVarsText
  125. ),
  126. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  127. ));
  128. }
  129. public function getCounterAPI($aParams = [])
  130. {
  131. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  132. $aScore = $this->_getVote();
  133. return array_merge($aScore, [
  134. BX_DOL_SCORE_DO_UP => [
  135. 'icon' => $this->_getIconDo(BX_DOL_SCORE_DO_UP),
  136. 'title' => _t($this->_getTitleDo(BX_DOL_SCORE_DO_UP)),
  137. ],
  138. BX_DOL_SCORE_DO_DOWN => [
  139. 'icon' => $this->_getIconDo(BX_DOL_SCORE_DO_DOWN),
  140. 'title' => _t($this->_getTitleDo(BX_DOL_SCORE_DO_DOWN))
  141. ]
  142. ]);
  143. }
  144. public function getLegend($aParams = array())
  145. {
  146. $aLegend = $this->_oQuery->getLegend($this->_iId);
  147. $aTypes = array(
  148. BX_DOL_SCORE_DO_UP,
  149. BX_DOL_SCORE_DO_DOWN
  150. );
  151. $aTmplVarsItems = array();
  152. foreach($aTypes as $sType)
  153. $aTmplVarsItems[] = array(
  154. 'style_prefix' => $this->_sStylePrefix,
  155. 'value' => $this->_getLabelDo($sType, $aParams),
  156. 'label' => isset($aLegend[$sType]['count']) ? (int)$aLegend[$sType]['count'] : 0
  157. );
  158. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameLegend, array(
  159. 'style_prefix' => $this->_sStylePrefix,
  160. 'html_id' => $this->_aHtmlIds['legend'],
  161. 'bx_repeat:items' => $aTmplVarsItems
  162. ));
  163. }
  164. public function getElementBlock($aParams = array())
  165. {
  166. $aParams['usage'] = BX_DOL_SCORE_USAGE_BLOCK;
  167. return $this->getElement($aParams);
  168. }
  169. public function getElementInline($aParams = array())
  170. {
  171. $aParams['usage'] = BX_DOL_SCORE_USAGE_INLINE;
  172. return $this->getElement($aParams);
  173. }
  174. public function getElement($aParams = array())
  175. {
  176. $aParams = array_merge($this->_aElementDefaults, $aParams);
  177. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  178. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  179. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  180. $iObjectId = $this->getId();
  181. $iAuthorId = $this->_getAuthorId();
  182. $iAuthorIp = $this->_getAuthorIp();
  183. $aScore = $this->_oQuery->getScore($iObjectId);
  184. $bCount = (int)$aScore['count_up'] != 0 || (int)$aScore['count_down'] != 0;
  185. $isAllowedVote = $this->isAllowedVote();
  186. $isAllowedVoteView = $this->isAllowedVoteView();
  187. $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId, $iAuthorIp) ? true : false;
  188. //--- Do Vote
  189. $bTmplVarsDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
  190. $aTmplVarsDoVoteUp = $aTmplVarsDoVoteDown = array();
  191. if($bTmplVarsDoVote)
  192. $aTmplVarsDoVoteUp = array(
  193. 'style_prefix' => $this->_sStylePrefix,
  194. 'do_vote' => $this->_getDoVote(BX_DOL_SCORE_DO_UP, $aParams, $isAllowedVote),
  195. );
  196. if($bTmplVarsDoVote)
  197. $aTmplVarsDoVoteDown = array(
  198. 'style_prefix' => $this->_sStylePrefix,
  199. 'do_vote' => $this->_getDoVote(BX_DOL_SCORE_DO_DOWN, $aParams, $isAllowedVote),
  200. );
  201. //--- Counter
  202. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  203. $aTmplVarsCounter = array();
  204. if($bTmplVarsCounter)
  205. $aTmplVarsCounter = array(
  206. 'style_prefix' => $this->_sStylePrefix,
  207. 'bx_if:show_hidden' => array(
  208. 'condition' => !$bShowCounterEmpty && !$bCount,
  209. 'content' => array()
  210. ),
  211. 'counter' => $this->getCounter(array_merge($aParams, [
  212. 'show_counter_only' => false,
  213. 'show_script' => false
  214. ]))
  215. );
  216. //--- Legend
  217. $bTmplVarsLegend = $this->_isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  218. $aTmplVarsLegend = array();
  219. if($bTmplVarsLegend)
  220. $aTmplVarsLegend = array(
  221. 'legend' => $this->getLegend($aParams)
  222. );
  223. if(!$bTmplVarsDoVote && !$bTmplVarsCounter && !$bTmplVarsLegend)
  224. return '';
  225. $sTmplName = $this->{'_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_SCORE_USAGE_DEFAULT)}();
  226. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  227. 'style_prefix' => $this->_sStylePrefix,
  228. 'html_id' => $this->_aHtmlIds['main'],
  229. 'class' => $this->_sStylePrefix . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
  230. 'score' => $aScore['score'],
  231. 'cup' => $aScore['count_up'],
  232. 'cdown' => $aScore['count_down'],
  233. 'bx_if:show_do_vote_up' => array(
  234. 'condition' => $bTmplVarsDoVote,
  235. 'content' => $aTmplVarsDoVoteUp
  236. ),
  237. 'bx_if:show_counter' => array(
  238. 'condition' => $bTmplVarsCounter,
  239. 'content' => $aTmplVarsCounter
  240. ),
  241. 'bx_if:show_do_vote_down' => array(
  242. 'condition' => $bTmplVarsDoVote,
  243. 'content' => $aTmplVarsDoVoteDown
  244. ),
  245. 'bx_if:show_legend' => array(
  246. 'condition' => $bTmplVarsLegend,
  247. 'content' => $aTmplVarsLegend
  248. ),
  249. 'script' => $this->getJsScript($aParams)
  250. ));
  251. }
  252. public function getElementAPI($aParams = [])
  253. {
  254. if(!($this->_bApi = bx_is_api()))
  255. return;
  256. if(!$this->isEnabled())
  257. return bx_api_get_msg('_sys_score_err_not_enabled');
  258. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  259. $iObjectId = $this->getId();
  260. $iAuthorId = $this->_getAuthorId();
  261. $bCount = $this->_isCount();
  262. $isAllowedVote = $this->isAllowedVote();
  263. $isAllowedVoteView = $this->isAllowedVoteView();
  264. $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId);
  265. $aParams['track'] = $aParams['is_voted'] ? $this->_getTrack($iObjectId, $iAuthorId) : [];
  266. //--- Do Vote
  267. $bDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
  268. $aDoVoteUp = $bDoVote ? $this->_getDoVote(BX_DOL_SCORE_DO_UP, $aParams, $isAllowedVote) : [];
  269. $aDoVoteDown = $bDoVote ? $this->_getDoVote(BX_DOL_SCORE_DO_DOWN, $aParams, $isAllowedVote) : [];
  270. //--- Counter
  271. $bCounter = $this->_isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  272. $aCounter = $bCounter ? $this->getCounterAPI(array_merge($aParams, [
  273. 'show_counter_only' => false,
  274. 'show_script' => false
  275. ])) : [];
  276. //--- Legend
  277. $bLegend = $this->_isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  278. $aLegend = $bLegend ? $this->getLegend($aParams) : [];
  279. if(!$bDoVote && !$bCounter && !$bLegend)
  280. return bx_api_get_msg('');
  281. return [
  282. 'type' => 'scores',
  283. 'system' => $this->_sSystem,
  284. 'object_id' => $this->_iId,
  285. 'params' => $aParams,
  286. 'action' => [
  287. BX_DOL_SCORE_DO_UP => $aDoVoteUp,
  288. BX_DOL_SCORE_DO_DOWN => $aDoVoteDown
  289. ],
  290. 'counter' => $aCounter,
  291. 'legend' => $aLegend,
  292. ];
  293. }
  294. protected function _getDoVote($sType, $aParams = [], $isAllowedVote = true)
  295. {
  296. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  297. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  298. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  299. $bDisabled = !$isAllowedVote || $bVoted;
  300. $sClass = '';
  301. if($bShowDoVoteAsButton)
  302. $sClass = 'bx-btn';
  303. else if ($bShowDoVoteAsButtonSmall)
  304. $sClass = 'bx-btn bx-btn-small';
  305. if($bDisabled)
  306. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-score-disabled';
  307. if($this->_bApi)
  308. return [
  309. 'is_voted' => $bVoted,
  310. 'is_disabled' => $bDisabled,
  311. 'icon' => $this->_getIconDo($sType),
  312. 'title' => _t($this->_getTitleDo($sType)),
  313. ];
  314. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDo($sType, $aParams), array(
  315. 'class' => $this->_sStylePrefix . '-do-vote ' . $this->_sStylePrefix . '-dv-' . $sType . ' ' . $sClass,
  316. 'title' => _t($this->_getTitleDo($sType)),
  317. 'onclick' => !$bDisabled ? $this->getJsClick($sType) : ''
  318. ));
  319. }
  320. protected function _getCounterLabel($iCount, $aParams = array())
  321. {
  322. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  323. 'style_prefix' => $this->_sStylePrefix,
  324. 'bx_if:show_icon' => array(
  325. 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
  326. 'content' => array(
  327. 'style_prefix' => $this->_sStylePrefix,
  328. 'icon' => $this->_oTemplate->getImageAuto($this->_getIconDo())
  329. )
  330. ),
  331. 'bx_if:show_text' => array(
  332. 'condition' => !isset($aParams['show_counter_label_text']) || (bool)$aParams['show_counter_label_text'] === true,
  333. 'content' => array(
  334. 'style_prefix' => $this->_sStylePrefix,
  335. 'text' => _t('_sys_score_counter', $iCount)
  336. )
  337. )
  338. ));
  339. }
  340. protected function _getLabelDo($sType, $aParams = array())
  341. {
  342. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  343. 'style_prefix' => $this->_sStylePrefix,
  344. 'bx_if:show_icon' => array(
  345. 'condition' => isset($aParams['show_do_vote_icon']) && (bool)$aParams['show_do_vote_icon'] === true,
  346. 'content' => array(
  347. 'style_prefix' => $this->_sStylePrefix,
  348. 'icon' => $this->_oTemplate->getImageAuto($this->_getIconDo($sType))
  349. )
  350. ),
  351. 'bx_if:show_text' => array(
  352. 'condition' => isset($aParams['show_do_vote_label']) && (bool)$aParams['show_do_vote_label'] === true,
  353. 'content' => array(
  354. 'style_prefix' => $this->_sStylePrefix,
  355. 'text' => _t($this->_getTitleDo($sType))
  356. )
  357. )
  358. ));
  359. }
  360. protected function _getVotedBy($aParams)
  361. {
  362. $aParams = array_merge($this->_aElementDefaults, $aParams, [
  363. 'show_do_vote_icon' => true,
  364. 'show_do_vote_label' => false
  365. ]);
  366. $aTmplUsers = [];
  367. $aUsers = $this->_oQuery->getPerformedBy($this->getId());
  368. foreach($aUsers as $aUser) {
  369. list($sUserName, $sUserLink, $sUserIcon, $sUserUnit, $sUserUnitWoInfo) = $this->_getAuthorInfo($aUser['id']);
  370. $aTmplUsers[] = [
  371. 'style_prefix' => $this->_sStylePrefix,
  372. 'user_name' => $sUserName,
  373. 'user_link' => $sUserLink,
  374. 'user_unit' => $sUserUnitWoInfo,
  375. 'vote' => $this->_getLabelDo($aUser['vote_type'], $aParams),
  376. 'date' => bx_time_js($aUser['vote_date']),
  377. ];
  378. }
  379. if(empty($aTmplUsers))
  380. $aTmplUsers = MsgBox(_t('_Empty'));
  381. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, [
  382. 'style_prefix' => $this->_sStylePrefix,
  383. 'bx_repeat:list' => $aTmplUsers
  384. ]);
  385. }
  386. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  387. {
  388. $bShowDoVote = !isset($aParams['show_do_vote']) || (bool)$aParams['show_do_vote'] === true;
  389. return $bShowDoVote && ($isAllowedVote || $bCount);
  390. }
  391. protected function _isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount)
  392. {
  393. return isset($aParams['show_counter']) && (bool)$aParams['show_counter'] === true && $isAllowedVoteView && ($isAllowedVote || $bCount);
  394. }
  395. protected function _isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount)
  396. {
  397. return isset($aParams['show_legend']) && (bool)$aParams['show_legend'] === true && $isAllowedVoteView;
  398. }
  399. }
  400. /** @} */