BxBaseScore.php 13 KB

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