BxBaseVoteStars.php 5.7 KB

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