BxDolVoteReactions.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Like based votes for any content
  11. */
  12. class BxDolVoteReactions extends BxTemplVote
  13. {
  14. protected static $_sCounterStyleDivided = 'divided';
  15. protected static $_sCounterStyleCompound = 'compound';
  16. protected $_sMenuDoVote; //--- Do vote (reaction) menu name.
  17. protected $_sDataList; //--- Reactions list name.
  18. protected $_aDataList; //--- Reactions list with all reactions.
  19. protected $_sDefault; //--- Default reaction name.
  20. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  21. {
  22. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  23. $this->_oQuery = new BxDolVoteReactionsQuery($this);
  24. $this->_sType = BX_DOL_VOTE_TYPE_REACTIONS;
  25. $this->_sMenuDoVote = 'sys_vote_reactions_do';
  26. $this->_sDataList = 'sys_vote_reactions';
  27. $this->_aDataList = array();
  28. $this->_sDefault = '';
  29. }
  30. public function init($iId)
  31. {
  32. if(!parent::init($iId))
  33. return false;
  34. $aReactions = BxDolFormQuery::getDataItems($this->_sDataList, false, BX_DATA_VALUES_ALL);
  35. foreach($aReactions as $sReaction => $aReaction) {
  36. $aData = !empty($aReaction['Data']) ? unserialize($aReaction['Data']) : array();
  37. $this->_aDataList[$sReaction] = array(
  38. 'name' => $sReaction,
  39. 'title' => $aReaction['LKey'],
  40. 'title_aux' => $aReaction['LKey2'],
  41. 'icon' => isset($aData['icon']) ? $aData['icon'] : '',
  42. 'weight' => isset($aData['weight']) ? $aData['weight'] : 1,
  43. );
  44. }
  45. if(!empty($this->_aDataList)) {
  46. $aNames = array_keys($this->_aDataList);
  47. $this->_sDefault = array_shift($aNames);
  48. }
  49. }
  50. /**
  51. * Interface functions for outer usage
  52. */
  53. public function getValue()
  54. {
  55. return (int)$this->_aSystem['min_value'];
  56. }
  57. public function getReaction($sName)
  58. {
  59. if(empty($this->_aDataList))
  60. $this->getReactions();
  61. return !empty($this->_aDataList[$sName]) ? $this->_aDataList[$sName] : false;
  62. }
  63. public function getReactions($bFullInfo = false)
  64. {
  65. return $bFullInfo ? $this->_aDataList : array_keys($this->_aDataList);
  66. }
  67. public function getTrackBy($aParams)
  68. {
  69. return $this->_oQuery->getTrackBy($aParams);
  70. }
  71. /**
  72. * Actions functions
  73. */
  74. public function actionGetDoVotePopup()
  75. {
  76. if(!$this->isEnabled())
  77. return '';
  78. return $this->_getDoVotePopup((int)bx_get('value'));
  79. }
  80. public function actionGetVotedBy()
  81. {
  82. if(!$this->isEnabled())
  83. return '';
  84. $aParams = array();
  85. $sReaction = bx_get('reaction');
  86. if($sReaction !== false) {
  87. $sReaction = bx_process_input($sReaction);
  88. $aReactions = $this->getReactions();
  89. if(!in_array($sReaction, $aReactions))
  90. $sReaction = $this->_sDefault;
  91. $aParams['reaction'] = $sReaction;
  92. }
  93. return $this->_getVotedBy($aParams);
  94. }
  95. /**
  96. * Internal functions
  97. */
  98. protected function _isDuplicate($iObjectId, $iAuthorId, $iAuthorIp, $bVoted)
  99. {
  100. return $bVoted && !$this->isUndo();
  101. }
  102. protected function _isCount($aVote = array())
  103. {
  104. if(empty($aVote))
  105. $aVote = $this->_getVote();
  106. foreach($aVote as $sKey => $mixedValue)
  107. if(substr($sKey, 0, 5) == 'count' && (int)$mixedValue != 0)
  108. return true;
  109. return false;
  110. }
  111. protected function _getVoteData()
  112. {
  113. $aData = parent::_getVoteData();
  114. if($aData === false)
  115. return false;
  116. $sReaction = bx_get('reaction');
  117. if($sReaction === false)
  118. return false;
  119. $aData['reaction'] = bx_process_input($sReaction);
  120. return $aData;
  121. }
  122. protected function _returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bVoted, $aParams = array())
  123. {
  124. $aReactions = $this->getReactions();
  125. $sReaction = $aData['reaction'];
  126. $bUndo = $this->isUndo();
  127. $bDisabled = $bVoted && !$bUndo;
  128. $aVote = $this->_getVote($iObjectId, true);
  129. $aTrack = $bVoted ? $this->_getTrack($iObjectId, $iAuthorId) : array();
  130. $sJsClick = '';
  131. if(!$bDisabled)
  132. $sJsClick = $bVoted && $bUndo ? $this->getJsClickDo($sReaction) : $this->getJsClick();
  133. $iTotalC = $iTotalS = 0;
  134. foreach($aReactions as $sName) {
  135. $iTotalC += (int)$aVote['count_' . $sName];
  136. $iTotalS += (int)$aVote['sum_' . $sName];
  137. }
  138. $fTotalR = $iTotalC != 0 ? round($iTotalS / $iTotalC, 2) : 0;
  139. $iCount = (int)$aVote['count_' . $sReaction];
  140. $aResult = array(
  141. 'code' => 0,
  142. 'reaction' => $sReaction,
  143. 'rate' => $aVote['rate_' . $sReaction],
  144. 'count' => $iCount,
  145. 'countf' => $iCount > 0 ? $this->_getCounterLabel($iCount, array('reaction' => $sReaction)) : '',
  146. 'label_icon' => $this->_getIconDoWithTrack($bVoted, $aTrack),
  147. 'label_title' => _t($this->_getTitleDoWithTrack($bVoted, $aTrack)),
  148. 'label_click' => $sJsClick,
  149. 'disabled' => $bVoted && !$this->isUndo(),
  150. 'total' => array(
  151. 'rate' => $fTotalR,
  152. 'count' => $iTotalC,
  153. 'countf' => $iTotalC > 0 ? $this->_getCounterLabel($iTotalC, array('show_counter_label_icon' => false, 'reaction' => '')) : '',
  154. )
  155. );
  156. return $aResult;
  157. }
  158. protected function _getIconDoWithTrack($bVoted, $aTrack = array())
  159. {
  160. $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
  161. return $this->_aDataList[$sReaction]['icon'];
  162. }
  163. protected function _getTitleDoWithTrack($bVoted, $aTrack = array())
  164. {
  165. $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
  166. return $this->_aDataList[$sReaction]['title'];
  167. }
  168. protected function _getTitleDoBy($aParams = array())
  169. {
  170. if(isset($aParams['show_counter_style']) && $aParams['show_counter_style'] == self::$_sCounterStyleCompound)
  171. return _t('_vote_do_by_reactions');
  172. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  173. return _t('_vote_do_by_x_reaction', _t($this->_aDataList[$sReaction]['title']));
  174. }
  175. }
  176. /** @} */