BxBaseVoteReactions.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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-vote-reactions-' . $sHtmlId,
  24. 'do_menu' => 'bx-vote-do-menu-' . $sHtmlId,
  25. 'do_popup' => 'bx-vote-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. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-vote-disabled';
  125. else
  126. $sJsClick = $bVoted && $bUndo ? $this->getJsClickDo($aParams['track']['reaction']) : $this->getJsClick();
  127. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
  128. 'class' => $this->_sStylePrefix . '-do-vote ' . $sClass,
  129. 'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
  130. 'onclick' => $sJsClick
  131. ));
  132. }
  133. protected function _getDoVoteLabel($aParams = array())
  134. {
  135. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  136. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoVoteLabel(), array(
  137. 'bx_if:show_icon' => array(
  138. 'condition' => isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true,
  139. 'content' => array(
  140. 'name' => $this->_getIconDoWithTrack($bVoted, $aParams['track'])
  141. )
  142. ),
  143. 'bx_if:show_text' => array(
  144. 'condition' => isset($aParams['show_do_vote_label']) && $aParams['show_do_vote_label'] == true,
  145. 'content' => array(
  146. 'text' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track']))
  147. )
  148. )
  149. ));
  150. }
  151. public function _getDoVotePopup($iValue = 0)
  152. {
  153. if(empty($iValue))
  154. $iValue = $this->getValue();
  155. $sJsObject = $this->getJsObjectName();
  156. $aReactions = $this->getReactions(true);
  157. $aMenu = array();
  158. foreach($aReactions as $sName => $aReaction) {
  159. $aMenu[] = array(
  160. 'id' => $sName,
  161. 'name' => $sName,
  162. 'class' => '',
  163. 'link' => 'javascript:void(0)',
  164. 'onclick' => 'javascript:' . $this->getJsClickDo($sName, $iValue),
  165. 'target' => '_self',
  166. 'title' => _t($aReaction['title']),
  167. 'icon' => $aReaction['icon'],
  168. 'active' => 1
  169. );
  170. }
  171. $oMenu = new BxTemplMenu(array('template' => 'menu_buttons_icon_hor.html', 'menu_id'=> $this->_aHtmlIds['do_menu'], 'menu_items' => $aMenu));
  172. return $oMenu->getCode();
  173. }
  174. protected function _getLabelCounter($iCount, $aParams = array())
  175. {
  176. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  177. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  178. 'name' => $this->_aDataList[$sReaction]['icon'],
  179. 'text' => parent::_getLabelCounter($iCount)
  180. ));
  181. }
  182. protected function _getVotedBy($aParams = array())
  183. {
  184. $aTmplUsers = array();
  185. $aUserIds = $this->_oQuery->getPerformed(array('type' => 'by', 'object_id' => $this->getId(), 'reaction' => $aParams['reaction']));
  186. foreach($aUserIds as $iUserId) {
  187. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  188. $aTmplUsers[] = array(
  189. 'style_prefix' => $this->_sStylePrefix,
  190. 'user_unit' => $sUserUnit
  191. );
  192. }
  193. if(empty($aTmplUsers))
  194. $aTmplUsers = MsgBox(_t('_Empty'));
  195. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  196. 'style_prefix' => $this->_sStylePrefix,
  197. 'bx_repeat:list' => $aTmplUsers
  198. ));
  199. }
  200. protected function _getTmplContentDoVoteLabel()
  201. {
  202. return self::$_sTmplContentDoVoteLabelReactions;
  203. }
  204. protected function _getTmplContentCounterWrapper()
  205. {
  206. return self::$_sTmplContentCounterWrapper;
  207. }
  208. protected function _getTmplContentCounterLabel()
  209. {
  210. return self::$_sTmplContentCounterLabel;
  211. }
  212. }
  213. /** @} */