BxBaseVoteReactions.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 BxBaseVoteReactions extends BxDolVoteReactions
  13. {
  14. protected static $_sTmplContentDoVoteLabelReactions;
  15. protected static $_sTmplContentCounterWrapper;
  16. protected static $_sTmplContentCounterLabel;
  17. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  18. {
  19. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  20. $this->_sJsClsName .= 'Reactions';
  21. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  22. $this->_aHtmlIds = array_merge($this->_aHtmlIds, array(
  23. 'main' => 'bx-vr-' . $sHtmlId,
  24. 'do_menu' => 'bx-vr-do-menu-' . $sHtmlId,
  25. 'do_popup' => 'bx-vr-do-popup-' . $sHtmlId
  26. ));
  27. $this->_aElementDefaults = array(
  28. 'show_do_vote_as_button' => false,
  29. 'show_do_vote_as_button_small' => false,
  30. 'show_do_vote_icon' => true,
  31. 'show_do_vote_label' => false,
  32. 'show_counter' => false,
  33. 'show_counter_empty' => true,
  34. 'show_legend' => false
  35. );
  36. if(empty(self::$_sTmplContentDoVoteLabelReactions))
  37. self::$_sTmplContentDoVoteLabelReactions = $this->_oTemplate->getHtml('vote_do_vote_reactions_label.html');
  38. if(empty(self::$_sTmplContentCounterWrapper))
  39. self::$_sTmplContentCounterWrapper = $this->_oTemplate->getHtml('vote_counter_wrapper_reactions.html');
  40. if(empty(self::$_sTmplContentCounterLabel))
  41. self::$_sTmplContentCounterLabel = $this->_oTemplate->getHtml('vote_counter_label_reactions.html');
  42. }
  43. public function getJsClick($iValue = 0)
  44. {
  45. if(empty($iValue))
  46. $iValue = $this->getValue();
  47. return $this->getJsObjectName() . '.toggleDoPopup(this, ' . $iValue . ')';
  48. }
  49. public function getJsClickDo($sReaction, $iValue = 0)
  50. {
  51. if(empty($iValue))
  52. $iValue = $this->getValue();
  53. return $this->getJsObjectName() . '.vote(this, ' . $iValue . ', \'' . $sReaction . '\')';
  54. }
  55. public function getJsClickCounter($aParams = array())
  56. {
  57. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  58. return $this->getJsObjectName() . '.toggleByPopup(this, \'' . $sReaction . '\')';
  59. }
  60. public function getCounter($aParams = array())
  61. {
  62. $aVote = $this->_getVote();
  63. $aReactions = $this->getReactions();
  64. $sClass = isset($aParams['class_counter']) ? $aParams['class_counter'] : '';
  65. if(isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true)
  66. $sClass .= ' bx-btn-small-height';
  67. else if(isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true)
  68. $sClass .= ' bx-btn-height';
  69. $sClass .= ' bx-def-margin-sec-right';
  70. $aParams['id_counter'] = '';
  71. $sResult = '';
  72. foreach($aReactions as $sName) {
  73. $aParams['class_counter'] = ((int)$aVote['count_' . $sName] == 0 ? ' bx-vc-hidden' : '') . ' ' . $sName . ' ' . $sClass;
  74. $aParams['reaction'] = $sName;
  75. $aParams['vote'] = array(
  76. 'count' => $aVote['count_' . $sName],
  77. 'sum' => $aVote['sum_' . $sName],
  78. 'rate' => $aVote['rate_' . $sName],
  79. );
  80. $sResult .= parent::getCounter($aParams);
  81. }
  82. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterWrapper(), array(
  83. 'html_id' => $this->_aHtmlIds['counter'],
  84. 'style_prefix' => $this->_sStylePrefix,
  85. 'type' => $this->_sType,
  86. 'counter' => $sResult
  87. ));
  88. }
  89. public function getElement($aParams = array())
  90. {
  91. $sClass = $this->_sStylePrefix . '-' . $this->_sType;
  92. if(isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true)
  93. $sClass .= '-button-small';
  94. else if(isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true)
  95. $sClass .= '-button';
  96. $aParams['class_element'] = isset($aParams['class_element']) ? $aParams['class_element'] : '';
  97. $aParams['class_element'] .= ' ' . $sClass;
  98. return parent::getElement($aParams);
  99. }
  100. /**
  101. * Internal methods.
  102. */
  103. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  104. {
  105. $bResult = parent::_isShowDoVote($aParams, $isAllowedVote, $bCount);
  106. if(!$bResult)
  107. return $bResult;
  108. return $isAllowedVote || $bCount;
  109. }
  110. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  111. {
  112. $bUndo = $this->isUndo();
  113. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  114. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  115. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  116. $bDisabled = !$isAllowedVote || ($bVoted && !$bUndo);
  117. $sClass = '';
  118. if($bShowDoVoteAsButton)
  119. $sClass = ' bx-btn';
  120. else if ($bShowDoVoteAsButtonSmall)
  121. $sClass = ' bx-btn bx-btn-small';
  122. $sJsClick = '';
  123. if(!$bDisabled) {
  124. if($bVoted && $bUndo) {
  125. $sClass = ' ' . $this->_sStylePrefix . '-voted' . $sClass;
  126. $sJsClick = $this->getJsClickDo($aParams['track']['reaction']);
  127. }
  128. else
  129. $sJsClick = $this->getJsClick();
  130. }
  131. else
  132. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : ' ' . $this->_sStylePrefix . '-disabled';
  133. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
  134. 'class' => $this->_sStylePrefix . '-do-vote' . $sClass,
  135. 'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
  136. 'onclick' => $sJsClick,
  137. ));
  138. }
  139. protected function _getDoVoteLabel($aParams = array())
  140. {
  141. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  142. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoVoteLabel(), array(
  143. 'bx_if:show_icon' => array(
  144. 'condition' => isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true,
  145. 'content' => array(
  146. 'name' => $this->_getIconDoWithTrack($bVoted, $aParams['track'])
  147. )
  148. ),
  149. 'bx_if:show_text' => array(
  150. 'condition' => isset($aParams['show_do_vote_label']) && $aParams['show_do_vote_label'] == true,
  151. 'content' => array(
  152. 'text' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track']))
  153. )
  154. )
  155. ));
  156. }
  157. public function _getDoVotePopup($iValue = 0)
  158. {
  159. if(empty($iValue))
  160. $iValue = $this->getValue();
  161. $oMenu = BxTemplMenu::getObjectInstance($this->_sMenuDoVote);
  162. if(!$oMenu)
  163. return '';
  164. $oMenu->setParams(array(
  165. 'object' => &$this,
  166. 'value' => $iValue,
  167. ));
  168. return $oMenu->getCode();
  169. }
  170. protected function _getLabelCounter($iCount, $aParams = array())
  171. {
  172. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  173. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  174. 'name' => $this->_aDataList[$sReaction]['icon'],
  175. 'text' => parent::_getLabelCounter($iCount)
  176. ));
  177. }
  178. protected function _getVotedBy($aParams = array())
  179. {
  180. $aTmplUsers = array();
  181. $aUserIds = $this->_oQuery->getPerformed(array('type' => 'by', 'object_id' => $this->getId(), 'reaction' => $aParams['reaction']));
  182. foreach($aUserIds as $iUserId) {
  183. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  184. $aTmplUsers[] = array(
  185. 'style_prefix' => $this->_sStylePrefix,
  186. 'user_unit' => $sUserUnit
  187. );
  188. }
  189. if(empty($aTmplUsers))
  190. $aTmplUsers = MsgBox(_t('_Empty'));
  191. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  192. 'style_prefix' => $this->_sStylePrefix,
  193. 'bx_repeat:list' => $aTmplUsers
  194. ));
  195. }
  196. protected function _getTmplContentDoVoteLabel()
  197. {
  198. return self::$_sTmplContentDoVoteLabelReactions;
  199. }
  200. protected function _getTmplContentCounterWrapper()
  201. {
  202. return self::$_sTmplContentCounterWrapper;
  203. }
  204. protected function _getTmplContentCounterLabel()
  205. {
  206. return self::$_sTmplContentCounterLabel;
  207. }
  208. }
  209. /** @} */