BxBaseVoteLikes.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. $aVote = $this->_getVote();
  64. $bVoted = false;
  65. return array_merge($aVote, [
  66. 'icon' => $this->_getEmojiDo($bVoted),
  67. 'title' => _t($this->_getTitleDo($bVoted)),
  68. ]);
  69. }
  70. public function getElement($aParams = array())
  71. {
  72. $sClass = $this->_sStylePrefix . '-' . $this->_sType;
  73. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  74. $sClass .= '-button-small';
  75. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  76. $sClass .= '-button';
  77. $aParams['class_element'] = isset($aParams['class_element']) ? $aParams['class_element'] : '';
  78. $aParams['class_element'] .= ' ' . $sClass;
  79. return parent::getElement($aParams);
  80. }
  81. /**
  82. * Internal methods.
  83. */
  84. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  85. {
  86. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  87. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  88. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  89. $bDisabled = !$isAllowedVote || ($bVoted && !$this->isUndo());
  90. $sClass = '';
  91. if($bShowDoVoteAsButton)
  92. $sClass = 'bx-btn';
  93. else if ($bShowDoVoteAsButtonSmall)
  94. $sClass = 'bx-btn bx-btn-small';
  95. if($bDisabled)
  96. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-vote-disabled';
  97. if($this->_bApi)
  98. return [
  99. 'is_undo' => $this->isUndo(),
  100. 'is_voted' => $bVoted,
  101. 'is_disabled' => $bDisabled,
  102. 'value' => $this->getValue(),
  103. 'icon' => $this->_getEmojiDo($bVoted),
  104. 'title' => _t($this->_getTitleDo($bVoted)),
  105. ];
  106. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
  107. 'class' => $this->_sStylePrefix . '-do-vote ' . $sClass,
  108. 'title' => _t($this->_getTitleDo($bVoted)),
  109. 'onclick' => !$bDisabled ? $this->getJsClick() : ''
  110. ));
  111. }
  112. protected function _getDoVoteLabel($aParams = array())
  113. {
  114. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  115. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  116. 'style_prefix' => $this->_sStylePrefix,
  117. 'bx_if:show_icon' => array(
  118. 'condition' => isset($aParams['show_do_vote_icon']) && (bool)$aParams['show_do_vote_icon'] === true,
  119. 'content' => array(
  120. 'style_prefix' => $this->_sStylePrefix,
  121. 'icon' => $this->_oTemplate->getImageAuto($this->{'_get' . bx_gen_method_name($this->_useIconAs($aParams)) . 'Do'}($bVoted))
  122. )
  123. ),
  124. 'bx_if:show_text' => array(
  125. 'condition' => isset($aParams['show_do_vote_label']) && (bool)$aParams['show_do_vote_label'] === true,
  126. 'content' => array(
  127. 'style_prefix' => $this->_sStylePrefix,
  128. 'text' => _t($this->_getTitleDo($bVoted))
  129. )
  130. )
  131. ));
  132. }
  133. protected function _getCounterLabel($iCount, $aParams = array())
  134. {
  135. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  136. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  137. 'style_prefix' => $this->_sStylePrefix,
  138. 'bx_if:show_icon' => array(
  139. 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
  140. 'content' => array(
  141. 'style_prefix' => $this->_sStylePrefix,
  142. 'icon' => isset($aParams['custom_icon']) && $aParams['custom_icon'] != '' ? $aParams['custom_icon'] : $this->_oTemplate->getImageAuto($this->_getIconDo($bVoted))
  143. )
  144. ),
  145. 'bx_if:show_text' => array(
  146. 'condition' => !isset($aParams['show_counter_label_text']) || (bool)$aParams['show_counter_label_text'] === true,
  147. 'content' => array(
  148. 'style_prefix' => $this->_sStylePrefix,
  149. 'text' => parent::_getCounterLabel($iCount, $aParams)
  150. )
  151. )
  152. ));
  153. }
  154. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  155. {
  156. $bResult = parent::_isShowDoVote($aParams, $isAllowedVote, $bCount);
  157. if(!$bResult)
  158. return $bResult;
  159. return $isAllowedVote || $bCount;
  160. }
  161. }
  162. /** @} */