BxBaseScore.php 16 KB

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