BxBaseVoteLikes.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, BxDolVoteLikes
  11. */
  12. class BxBaseVoteLikes extends BxDolVoteLikes
  13. {
  14. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  15. {
  16. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  17. $this->_sJsClsName .= 'Likes';
  18. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  19. $this->_aHtmlIds = array_merge($this->_aHtmlIds, array(
  20. 'main' => 'bx-vote-likes-' . $sHtmlId
  21. ));
  22. $this->_aElementDefaults = array(
  23. 'show_do_vote_as_button' => false,
  24. 'show_do_vote_as_button_small' => false,
  25. 'show_do_vote_icon' => true,
  26. 'show_do_vote_label' => false,
  27. 'show_counter' => true,
  28. 'show_counter_empty' => false,
  29. 'show_counter_label_icon' => false,
  30. 'show_counter_label_text' => true,
  31. 'show_legend' => false,
  32. 'show_script' => true
  33. );
  34. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('vote_do_vote_label_likes.html');
  35. $this->_sTmplContentCounterLabel = $this->_oTemplate->getHtml('vote_counter_label_likes.html');
  36. }
  37. public function getJsClick($iValue = 0)
  38. {
  39. if(empty($iValue))
  40. $iValue = $this->getValue();
  41. return parent::getJsClick($iValue);
  42. }
  43. public function getCounter($aParams = array())
  44. {
  45. $sClass = '';
  46. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  47. $sClass = 'bx-btn-small-height';
  48. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  49. $sClass = 'bx-btn-height';
  50. $aParams['class_counter'] = isset($aParams['class_counter']) ? $aParams['class_counter'] : '';
  51. $aParams['class_counter'] .= ' ' . $sClass;
  52. return parent::getCounter($aParams);
  53. }
  54. public function getElement($aParams = array())
  55. {
  56. $sClass = $this->_sStylePrefix . '-' . $this->_sType;
  57. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  58. $sClass .= '-button-small';
  59. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  60. $sClass .= '-button';
  61. $aParams['class_element'] = isset($aParams['class_element']) ? $aParams['class_element'] : '';
  62. $aParams['class_element'] .= ' ' . $sClass;
  63. return parent::getElement($aParams);
  64. }
  65. /**
  66. * Internal methods.
  67. */
  68. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  69. {
  70. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  71. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  72. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  73. $bDisabled = !$isAllowedVote || ($bVoted && !$this->isUndo());
  74. $sClass = '';
  75. if($bShowDoVoteAsButton)
  76. $sClass = 'bx-btn';
  77. else if ($bShowDoVoteAsButtonSmall)
  78. $sClass = 'bx-btn bx-btn-small';
  79. if($bDisabled)
  80. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-vote-disabled';
  81. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
  82. 'class' => $this->_sStylePrefix . '-do-vote ' . $sClass,
  83. 'title' => _t($this->_getTitleDo($bVoted)),
  84. 'onclick' => !$bDisabled ? $this->getJsClick() : ''
  85. ));
  86. }
  87. protected function _getDoVoteLabel($aParams = array())
  88. {
  89. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  90. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  91. 'style_prefix' => $this->_sStylePrefix,
  92. 'bx_if:show_icon' => array(
  93. 'condition' => isset($aParams['show_do_vote_icon']) && (bool)$aParams['show_do_vote_icon'] === true,
  94. 'content' => array(
  95. 'style_prefix' => $this->_sStylePrefix,
  96. 'name' => $this->_getIconDo($bVoted)
  97. )
  98. ),
  99. 'bx_if:show_text' => array(
  100. 'condition' => isset($aParams['show_do_vote_label']) && (bool)$aParams['show_do_vote_label'] === true,
  101. 'content' => array(
  102. 'style_prefix' => $this->_sStylePrefix,
  103. 'text' => _t($this->_getTitleDo($bVoted))
  104. )
  105. )
  106. ));
  107. }
  108. protected function _getCounterLabel($iCount, $aParams = array())
  109. {
  110. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  111. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  112. 'style_prefix' => $this->_sStylePrefix,
  113. 'bx_if:show_icon' => array(
  114. 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
  115. 'content' => array(
  116. 'style_prefix' => $this->_sStylePrefix,
  117. 'name' => isset($aParams['custom_icon']) && $aParams['custom_icon'] != '' ? $aParams['custom_icon'] : BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getFontIconAsHtml($this->_getIconDo($bVoted))
  118. )
  119. ),
  120. 'bx_if:show_text' => array(
  121. 'condition' => !isset($aParams['show_counter_label_text']) || (bool)$aParams['show_counter_label_text'] === true,
  122. 'content' => array(
  123. 'style_prefix' => $this->_sStylePrefix,
  124. 'text' => parent::_getCounterLabel($iCount, $aParams)
  125. )
  126. )
  127. ));
  128. }
  129. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  130. {
  131. $bResult = parent::_isShowDoVote($aParams, $isAllowedVote, $bCount);
  132. if(!$bResult)
  133. return $bResult;
  134. return $isAllowedVote || $bCount;
  135. }
  136. }
  137. /** @} */