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