1
0

BxBaseVoteLikes.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 = [
  23. 'use_icon_as' => 'icon',
  24. 'show_do_vote_as_button' => false,
  25. 'show_do_vote_as_button_small' => false,
  26. 'show_do_vote_icon' => true,
  27. 'show_do_vote_label' => false,
  28. 'show_counter' => true,
  29. 'show_counter_only' => true,
  30. 'show_counter_empty' => false,
  31. 'show_counter_label_icon' => false,
  32. 'show_counter_label_text' => true,
  33. 'show_legend' => false,
  34. 'show_script' => true
  35. ];
  36. $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
  37. 'show_counter' => true,
  38. ]);
  39. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('vote_do_vote_label_likes.html');
  40. $this->_sTmplContentCounterLabel = $this->_oTemplate->getHtml('vote_counter_label_likes.html');
  41. }
  42. public function getJsClick($iValue = 0)
  43. {
  44. if(empty($iValue))
  45. $iValue = $this->getValue();
  46. return parent::getJsClick($iValue);
  47. }
  48. public function getCounter($aParams = [])
  49. {
  50. $aParams = array_merge($this->_aElementDefaults, $aParams);
  51. $sClass = '';
  52. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  53. $sClass = 'bx-btn-small-height';
  54. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  55. $sClass = 'bx-btn-height';
  56. $aParams['class_counter'] = isset($aParams['class_counter']) ? $aParams['class_counter'] : '';
  57. $aParams['class_counter'] .= ' ' . $sClass;
  58. return parent::getCounter($aParams);
  59. }
  60. public function getCounterAPI($aParams = [])
  61. {
  62. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  63. return $this->_getVote();
  64. }
  65. public function getElement($aParams = array())
  66. {
  67. $sClass = $this->_sStylePrefix . '-' . $this->_sType;
  68. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  69. $sClass .= '-button-small';
  70. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  71. $sClass .= '-button';
  72. $aParams['class_element'] = isset($aParams['class_element']) ? $aParams['class_element'] : '';
  73. $aParams['class_element'] .= ' ' . $sClass;
  74. return parent::getElement($aParams);
  75. }
  76. /**
  77. * Internal methods.
  78. */
  79. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  80. {
  81. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  82. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  83. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  84. $bDisabled = !$isAllowedVote || ($bVoted && !$this->isUndo());
  85. $sClass = '';
  86. if($bShowDoVoteAsButton)
  87. $sClass = 'bx-btn';
  88. else if ($bShowDoVoteAsButtonSmall)
  89. $sClass = 'bx-btn bx-btn-small';
  90. if($bDisabled)
  91. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-vote-disabled';
  92. if($this->_bApi)
  93. return [
  94. 'is_undo' => $this->isUndo(),
  95. 'is_voted' => $bVoted,
  96. 'is_disabled' => $bDisabled,
  97. 'value' => $this->getValue(),
  98. 'title' => _t($this->_getTitleDo($bVoted)),
  99. ];
  100. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
  101. 'class' => $this->_sStylePrefix . '-do-vote ' . $sClass,
  102. 'title' => _t($this->_getTitleDo($bVoted)),
  103. 'onclick' => !$bDisabled ? $this->getJsClick() : ''
  104. ));
  105. }
  106. protected function _getDoVoteLabel($aParams = array())
  107. {
  108. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  109. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  110. 'style_prefix' => $this->_sStylePrefix,
  111. 'bx_if:show_icon' => array(
  112. 'condition' => isset($aParams['show_do_vote_icon']) && (bool)$aParams['show_do_vote_icon'] === true,
  113. 'content' => array(
  114. 'style_prefix' => $this->_sStylePrefix,
  115. 'icon' => $this->_oTemplate->getImageAuto($this->{'_get' . bx_gen_method_name($this->_useIconAs($aParams)) . 'Do'}($bVoted))
  116. )
  117. ),
  118. 'bx_if:show_text' => array(
  119. 'condition' => isset($aParams['show_do_vote_label']) && (bool)$aParams['show_do_vote_label'] === true,
  120. 'content' => array(
  121. 'style_prefix' => $this->_sStylePrefix,
  122. 'text' => _t($this->_getTitleDo($bVoted))
  123. )
  124. )
  125. ));
  126. }
  127. protected function _getCounterLabel($iCount, $aParams = array())
  128. {
  129. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  130. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  131. 'style_prefix' => $this->_sStylePrefix,
  132. 'bx_if:show_icon' => array(
  133. 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
  134. 'content' => array(
  135. 'style_prefix' => $this->_sStylePrefix,
  136. 'icon' => isset($aParams['custom_icon']) && $aParams['custom_icon'] != '' ? $aParams['custom_icon'] : $this->_oTemplate->getImageAuto($this->_getIconDo($bVoted))
  137. )
  138. ),
  139. 'bx_if:show_text' => array(
  140. 'condition' => !isset($aParams['show_counter_label_text']) || (bool)$aParams['show_counter_label_text'] === true,
  141. 'content' => array(
  142. 'style_prefix' => $this->_sStylePrefix,
  143. 'text' => parent::_getCounterLabel($iCount, $aParams)
  144. )
  145. )
  146. ));
  147. }
  148. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  149. {
  150. $bResult = parent::_isShowDoVote($aParams, $isAllowedVote, $bCount);
  151. if(!$bResult)
  152. return $bResult;
  153. return $isAllowedVote || $bCount;
  154. }
  155. }
  156. /** @} */