BxBaseScore.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 = array(
  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->_sTmplNameLegend = 'score_legend.html';
  47. $this->_sTmplNameByList = 'score_by_list.html';
  48. $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('score_element_block.html');
  49. $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('score_element_inline.html');
  50. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('score_do_vote_label.html');
  51. $this->_sTmplContentCounterLabel = $this->_oTemplate->getHtml('score_counter_label.html');
  52. }
  53. public function getJsObjectName()
  54. {
  55. return $this->_sJsObjName;
  56. }
  57. public function getJsScript($aParams = array())
  58. {
  59. $sJsObjName = $this->getJsObjectName();
  60. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  61. $aParamsJs = array(
  62. 'sObjName' => $sJsObjName,
  63. 'sSystem' => $this->getSystemName(),
  64. 'iAuthorId' => $this->_getAuthorId(),
  65. 'iObjId' => $this->getId(),
  66. 'sRootUrl' => BX_DOL_URL_ROOT,
  67. 'sStylePrefix' => $this->_sStylePrefix,
  68. 'aHtmlIds' => $this->_aHtmlIds,
  69. 'aRequestParams' => $this->_prepareRequestParamsData($aParams)
  70. );
  71. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new BxDolScore(" . json_encode($aParamsJs) . ");";
  72. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  73. }
  74. public function getJsClick($sType)
  75. {
  76. return $this->getJsObjectName() . '.vote' . ucfirst($sType) . '(this)';
  77. }
  78. public function getJsClickCounter()
  79. {
  80. return $this->getJsObjectName() . '.toggleByPopup(this)';
  81. }
  82. public function getCounter($aParams = [])
  83. {
  84. $aParams = array_merge($this->_aElementDefaults, $aParams);
  85. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  86. $bShowEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  87. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  88. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  89. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  90. $bAllowedViewViewVoters = $this->isAllowedVoteViewVoters();
  91. $aScore = $this->_oQuery->getScore($this->getId());
  92. $iCup = (int)$aScore['count_up'];
  93. $iCdown = (int)(int)$aScore['count_down'];
  94. $bEmpty = $iCup == 0 && $iCdown == 0;
  95. $sClass = 'sys-action-counter';
  96. if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
  97. $sClass .= ' sys-ac-only';
  98. $sClass .= ' ' . $this->_sStylePrefix . '-counter';
  99. if($bShowDoVoteAsButtonSmall)
  100. $sClass .= ' bx-btn-small-height';
  101. if($bShowDoVoteAsButton)
  102. $sClass .= ' bx-btn-height';
  103. $bLink = $bAllowedViewViewVoters && !$bEmpty;
  104. $sCounter = !$bEmpty || $bShowEmpty ? $this->_getCounterLabel($iCup - $iCdown, $aParams) : '';
  105. $aTmplVarsText = [
  106. 'id' => $this->_aHtmlIds['counter'],
  107. 'class' => $sClass,
  108. 'content' => $sCounter,
  109. 'title' => _t($this->_getTitleDoBy()),
  110. ];
  111. $aTmplVarsLink = array_merge($aTmplVarsText, [
  112. 'onclick' => $bLink ? 'javascript:' . $this->getJsClickCounter() : ''
  113. ]);
  114. return $this->_oTemplate->parseHtmlByName('score_counter.html', array(
  115. 'bx_if:show_link' => array(
  116. 'condition' => $bLink,
  117. 'content' => $aTmplVarsLink
  118. ),
  119. 'bx_if:show_text' => array(
  120. 'condition' => !$bLink,
  121. 'content' => $aTmplVarsText
  122. ),
  123. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  124. ));
  125. }
  126. public function getLegend($aParams = array())
  127. {
  128. $aLegend = $this->_oQuery->getLegend($this->_iId);
  129. $aTypes = array(
  130. BX_DOL_SCORE_DO_UP,
  131. BX_DOL_SCORE_DO_DOWN
  132. );
  133. $aTmplVarsItems = array();
  134. foreach($aTypes as $sType)
  135. $aTmplVarsItems[] = array(
  136. 'style_prefix' => $this->_sStylePrefix,
  137. 'value' => $this->_getLabelDo($sType, $aParams),
  138. 'label' => isset($aLegend[$sType]['count']) ? (int)$aLegend[$sType]['count'] : 0
  139. );
  140. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameLegend, array(
  141. 'style_prefix' => $this->_sStylePrefix,
  142. 'html_id' => $this->_aHtmlIds['legend'],
  143. 'bx_repeat:items' => $aTmplVarsItems
  144. ));
  145. }
  146. public function getElementBlock($aParams = array())
  147. {
  148. $aParams['usage'] = BX_DOL_SCORE_USAGE_BLOCK;
  149. return $this->getElement($aParams);
  150. }
  151. public function getElementInline($aParams = array())
  152. {
  153. $aParams['usage'] = BX_DOL_SCORE_USAGE_INLINE;
  154. return $this->getElement($aParams);
  155. }
  156. public function getElement($aParams = array())
  157. {
  158. $aParams = array_merge($this->_aElementDefaults, $aParams);
  159. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  160. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  161. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  162. $iObjectId = $this->getId();
  163. $iAuthorId = $this->_getAuthorId();
  164. $iAuthorIp = $this->_getAuthorIp();
  165. $aScore = $this->_oQuery->getScore($iObjectId);
  166. $bCount = (int)$aScore['count_up'] != 0 || (int)$aScore['count_down'] != 0;
  167. $isAllowedVote = $this->isAllowedVote();
  168. $isAllowedVoteView = $this->isAllowedVoteView();
  169. $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId, $iAuthorIp) ? true : false;
  170. //--- Do Vote
  171. $bTmplVarsDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
  172. $aTmplVarsDoVoteUp = $aTmplVarsDoVoteDown = array();
  173. if($bTmplVarsDoVote)
  174. $aTmplVarsDoVoteUp = array(
  175. 'style_prefix' => $this->_sStylePrefix,
  176. 'do_vote' => $this->_getDoVote(BX_DOL_SCORE_DO_UP, $aParams, $isAllowedVote),
  177. );
  178. if($bTmplVarsDoVote)
  179. $aTmplVarsDoVoteDown = array(
  180. 'style_prefix' => $this->_sStylePrefix,
  181. 'do_vote' => $this->_getDoVote(BX_DOL_SCORE_DO_DOWN, $aParams, $isAllowedVote),
  182. );
  183. //--- Counter
  184. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  185. $aTmplVarsCounter = array();
  186. if($bTmplVarsCounter)
  187. $aTmplVarsCounter = array(
  188. 'style_prefix' => $this->_sStylePrefix,
  189. 'bx_if:show_hidden' => array(
  190. 'condition' => !$bShowCounterEmpty && !$bCount,
  191. 'content' => array()
  192. ),
  193. 'counter' => $this->getCounter(array_merge($aParams, [
  194. 'show_counter_only' => false,
  195. 'show_script' => false
  196. ]))
  197. );
  198. //--- Legend
  199. $bTmplVarsLegend = $this->_isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  200. $aTmplVarsLegend = array();
  201. if($bTmplVarsLegend)
  202. $aTmplVarsLegend = array(
  203. 'legend' => $this->getLegend($aParams)
  204. );
  205. if(!$bTmplVarsDoVote && !$bTmplVarsCounter && !$bTmplVarsLegend)
  206. return '';
  207. $sTmplName = $this->{'_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_SCORE_USAGE_DEFAULT)}();
  208. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  209. 'style_prefix' => $this->_sStylePrefix,
  210. 'html_id' => $this->_aHtmlIds['main'],
  211. 'class' => $this->_sStylePrefix . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
  212. 'score' => $aScore['score'],
  213. 'cup' => $aScore['count_up'],
  214. 'cdown' => $aScore['count_down'],
  215. 'bx_if:show_do_vote_up' => array(
  216. 'condition' => $bTmplVarsDoVote,
  217. 'content' => $aTmplVarsDoVoteUp
  218. ),
  219. 'bx_if:show_counter' => array(
  220. 'condition' => $bTmplVarsCounter,
  221. 'content' => $aTmplVarsCounter
  222. ),
  223. 'bx_if:show_do_vote_down' => array(
  224. 'condition' => $bTmplVarsDoVote,
  225. 'content' => $aTmplVarsDoVoteDown
  226. ),
  227. 'bx_if:show_legend' => array(
  228. 'condition' => $bTmplVarsLegend,
  229. 'content' => $aTmplVarsLegend
  230. ),
  231. 'script' => $this->getJsScript($aParams)
  232. ));
  233. }
  234. protected function _getDoVote($sType, $aParams = array(), $isAllowedVote = true)
  235. {
  236. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  237. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  238. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  239. $bDisabled = !$isAllowedVote || $bVoted;
  240. $sClass = '';
  241. if($bShowDoVoteAsButton)
  242. $sClass = 'bx-btn';
  243. else if ($bShowDoVoteAsButtonSmall)
  244. $sClass = 'bx-btn bx-btn-small';
  245. if($bDisabled)
  246. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-score-disabled';
  247. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDo($sType, $aParams), array(
  248. 'class' => $this->_sStylePrefix . '-do-vote ' . $this->_sStylePrefix . '-dv-' . $sType . ' ' . $sClass,
  249. 'title' => _t($this->_getTitleDo($sType)),
  250. 'onclick' => !$bDisabled ? $this->getJsClick($sType) : ''
  251. ));
  252. }
  253. protected function _getCounterLabel($iCount, $aParams = array())
  254. {
  255. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  256. 'style_prefix' => $this->_sStylePrefix,
  257. 'bx_if:show_icon' => array(
  258. 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
  259. 'content' => array(
  260. 'style_prefix' => $this->_sStylePrefix,
  261. 'icon' => $this->_oTemplate->getImageAuto($this->_getIconDo())
  262. )
  263. ),
  264. 'bx_if:show_text' => array(
  265. 'condition' => !isset($aParams['show_counter_label_text']) || (bool)$aParams['show_counter_label_text'] === true,
  266. 'content' => array(
  267. 'style_prefix' => $this->_sStylePrefix,
  268. 'text' => _t('_sys_score_counter', $iCount)
  269. )
  270. )
  271. ));
  272. }
  273. protected function _getLabelDo($sType, $aParams = array())
  274. {
  275. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  276. 'style_prefix' => $this->_sStylePrefix,
  277. 'bx_if:show_icon' => array(
  278. 'condition' => isset($aParams['show_do_vote_icon']) && (bool)$aParams['show_do_vote_icon'] === true,
  279. 'content' => array(
  280. 'style_prefix' => $this->_sStylePrefix,
  281. 'icon' => $this->_oTemplate->getImageAuto($this->_getIconDo($sType))
  282. )
  283. ),
  284. 'bx_if:show_text' => array(
  285. 'condition' => isset($aParams['show_do_vote_label']) && (bool)$aParams['show_do_vote_label'] === true,
  286. 'content' => array(
  287. 'style_prefix' => $this->_sStylePrefix,
  288. 'text' => _t($this->_getTitleDo($sType))
  289. )
  290. )
  291. ));
  292. }
  293. protected function _getVotedBy($aParams)
  294. {
  295. $aParams = array_merge($this->_aElementDefaults, $aParams, [
  296. 'show_do_vote_icon' => true,
  297. 'show_do_vote_label' => false
  298. ]);
  299. $aTmplUsers = [];
  300. $aUsers = $this->_oQuery->getPerformedBy($this->getId());
  301. foreach($aUsers as $aUser) {
  302. list($sUserName, $sUserLink, $sUserIcon, $sUserUnit, $sUserUnitWoInfo) = $this->_getAuthorInfo($aUser['id']);
  303. $aTmplUsers[] = [
  304. 'style_prefix' => $this->_sStylePrefix,
  305. 'user_name' => $sUserName,
  306. 'user_link' => $sUserLink,
  307. 'user_unit' => $sUserUnitWoInfo,
  308. 'vote' => $this->_getLabelDo($aUser['vote_type'], $aParams),
  309. 'date' => bx_time_js($aUser['vote_date']),
  310. ];
  311. }
  312. if(empty($aTmplUsers))
  313. $aTmplUsers = MsgBox(_t('_Empty'));
  314. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, [
  315. 'style_prefix' => $this->_sStylePrefix,
  316. 'bx_repeat:list' => $aTmplUsers
  317. ]);
  318. }
  319. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  320. {
  321. $bShowDoVote = !isset($aParams['show_do_vote']) || (bool)$aParams['show_do_vote'] === true;
  322. return $bShowDoVote && ($isAllowedVote || $bCount);
  323. }
  324. protected function _isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount)
  325. {
  326. return isset($aParams['show_counter']) && (bool)$aParams['show_counter'] === true && $isAllowedVoteView && ($isAllowedVote || $bCount);
  327. }
  328. protected function _isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount)
  329. {
  330. return isset($aParams['show_legend']) && (bool)$aParams['show_legend'] === true && $isAllowedVoteView;
  331. }
  332. }
  333. /** @} */