1
0

BxBaseScore.php 13 KB

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