BxBaseScore.php 14 KB

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