1
0

BxBaseVoteStars.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 BxDolVote
  11. */
  12. class BxBaseVoteStars extends BxDolVoteStars
  13. {
  14. protected $_sTmplNameDoVoteStars;
  15. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  16. {
  17. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  18. $this->_sJsClsName .= 'Stars';
  19. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  20. $this->_aHtmlIds = array_merge($this->_aHtmlIds, array(
  21. 'main' => 'bx-vote-stars-' . $sHtmlId,
  22. 'legend_stars' => 'bx-vote-stars-legend-' . $sHtmlId,
  23. ));
  24. $this->_aElementDefaults = [
  25. 'show_do_vote_legend' => false,
  26. 'show_counter' => true,
  27. 'show_counter_empty' => false,
  28. 'show_legend' => false,
  29. 'show_script' => true
  30. ];
  31. $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
  32. 'show_counter' => true,
  33. ]);
  34. $this->_sTmplNameDoVoteStars = 'vote_do_vote_stars.html';
  35. }
  36. public function getLegend($aParams = array())
  37. {
  38. $sJsObject = $this->getJsObjectName();
  39. $iMinValue = $this->getMinValue();
  40. $iMaxValue = $this->getMaxValue();
  41. $aLegend = $this->_oQuery->getLegend($this->_iId);
  42. $aTmplVarsItems = array();
  43. for($i = $iMaxValue; $i >= $iMinValue; $i--) {
  44. $aTmplVarsStars = $aTmplVarsSlider = array();
  45. for($j = $iMinValue; $j <= $iMaxValue; $j++) {
  46. $aTmplVarsStars[] = array(
  47. 'style_prefix' => $this->_sStylePrefix,
  48. 'value' => $i
  49. );
  50. $aTmplVarsSlider[] = array(
  51. 'style_prefix' => $this->_sStylePrefix
  52. );
  53. }
  54. $aTmplVarsItems[] = array(
  55. 'style_prefix' => $this->_sStylePrefix,
  56. 'value' => $i,
  57. 'bx_repeat:stars' => $aTmplVarsStars,
  58. 'bx_repeat:slider' => $aTmplVarsSlider,
  59. 'label' => isset($aLegend[$i]['count']) ? (int)$aLegend[$i]['count'] : 0
  60. );
  61. }
  62. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameLegend, array(
  63. 'style_prefix' => $this->_sStylePrefix,
  64. 'html_id' => $this->_aHtmlIds['legend_stars'],
  65. 'type' => $this->_sType,
  66. 'bx_repeat:items' => $aTmplVarsItems
  67. ));
  68. }
  69. public function getCounterAPI($aParams = [])
  70. {
  71. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  72. return $this->_getVote();
  73. }
  74. /**
  75. * Internal methods.
  76. */
  77. protected function _getTmplVarsElement($aParams = array())
  78. {
  79. $aTmplVars = parent::_getTmplVarsElement($aParams);
  80. $aVote = $this->_getVote();
  81. $aTmplVars['bx_if:show_vote_data'] = array(
  82. 'condition' => true,
  83. 'content' => array(
  84. 'rate' => $aVote['rate'],
  85. 'count' => $aVote['count']
  86. )
  87. );
  88. return $aTmplVars;
  89. }
  90. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  91. {
  92. $sJsObject = $this->getJsObjectName();
  93. $iMinValue = $this->getMinValue();
  94. $iMaxValue = $this->getMaxValue();
  95. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  96. $bDisabled = !$isAllowedVote || ($bVoted && !$this->isUndo());
  97. if($this->_bApi)
  98. return [
  99. 'is_undo' => $this->isUndo(),
  100. 'is_voted' => $bVoted,
  101. 'is_disabled' => $bDisabled,
  102. 'value_min' => $iMinValue,
  103. 'value_max' => $iMaxValue,
  104. 'value' => $this->getValue($bVoted),
  105. ];
  106. $aTmplVarsStars = $aTmplVarsLegend = $aTmplVarsSlider = $aTmplVarsButtons = array();
  107. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  108. $aTmplVarsStars[] = array(
  109. 'style_prefix' => $this->_sStylePrefix,
  110. 'value' => $i
  111. );
  112. $aTmplVarsLegend[] = array(
  113. 'style_prefix' => $this->_sStylePrefix,
  114. 'value' => $i
  115. );
  116. $aTmplVarsSlider[] = array(
  117. 'style_prefix' => $this->_sStylePrefix
  118. );
  119. if($isAllowedVote)
  120. $aTmplVarsButtons[] = array(
  121. 'style_prefix' => $this->_sStylePrefix,
  122. 'js_object' => $sJsObject,
  123. 'value' => $i
  124. );
  125. }
  126. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
  127. 'style_prefix' => $this->_sStylePrefix,
  128. 'bx_repeat:stars' => $aTmplVarsStars,
  129. 'bx_if:show_legend' => array(
  130. 'condition' => isset($aParams['show_do_vote_legend']) && $aParams['show_do_vote_legend'] === true,
  131. 'content' => array(
  132. 'style_prefix' => $this->_sStylePrefix,
  133. 'bx_repeat:legend' => $aTmplVarsLegend,
  134. )
  135. ),
  136. 'bx_repeat:slider' => $aTmplVarsSlider,
  137. 'bx_repeat:buttons' => $aTmplVarsButtons,
  138. ));
  139. }
  140. protected function _isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount)
  141. {
  142. return isset($aParams['show_legend']) && $aParams['show_legend'] === true && $isAllowedVoteView;
  143. }
  144. }
  145. /** @} */