BxDolVoteReactions.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 = 'default';
  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. 'color' => isset($aData['color']) ? $aData['color'] : '',
  43. 'weight' => isset($aData['weight']) ? $aData['weight'] : 1,
  44. );
  45. }
  46. if(!empty($this->_aDataList)) {
  47. $this->_aDataList[$this->_sDefault] = reset($this->_aDataList);
  48. $this->_aDataList[$this->_sDefault]['color'] = '';
  49. }
  50. }
  51. /**
  52. * Interface functions for outer usage
  53. */
  54. public function getValue()
  55. {
  56. return (int)$this->_aSystem['min_value'];
  57. }
  58. public function getDefault()
  59. {
  60. return $this->_sDefault;
  61. }
  62. public function getReaction($sName)
  63. {
  64. if(empty($this->_aDataList))
  65. $this->getReactions();
  66. return !empty($this->_aDataList[$sName]) ? $this->_aDataList[$sName] : false;
  67. }
  68. public function getReactions($bFullInfo = false)
  69. {
  70. return $bFullInfo ? $this->_aDataList : array_keys($this->_aDataList);
  71. }
  72. public function getTrackBy($aParams)
  73. {
  74. return $this->_oQuery->getTrackBy($aParams);
  75. }
  76. public function getIcon($sReaction, $bWithColor = true)
  77. {
  78. $aReaction = isset($this->_aDataList[$sReaction]) ? $this->_aDataList[$sReaction] : $this->_aDataList[$this->_sDefault];
  79. return $aReaction['icon'] . ($bWithColor && !empty($aReaction['color']) ? ' ' . $aReaction['color'] : '');
  80. }
  81. /**
  82. * Actions functions
  83. */
  84. public function actionGetDoVotePopup()
  85. {
  86. if(!$this->isEnabled())
  87. return '';
  88. return $this->_getDoVotePopup((int)bx_get('value'));
  89. }
  90. public function actionGetVotedBy()
  91. {
  92. if(!$this->isEnabled())
  93. return '';
  94. $aParams = array();
  95. $sReaction = bx_get('reaction');
  96. if($sReaction !== false) {
  97. $sReaction = bx_process_input($sReaction);
  98. $aReactions = $this->getReactions();
  99. if(!in_array($sReaction, $aReactions))
  100. $sReaction = $this->_sDefault;
  101. $aParams['reaction'] = $sReaction;
  102. }
  103. return $this->_getVotedBy($aParams);
  104. }
  105. /**
  106. * Internal functions
  107. */
  108. protected function _isDuplicate($iObjectId, $iAuthorId, $iAuthorIp, $bVoted)
  109. {
  110. return $bVoted && !$this->isUndo();
  111. }
  112. protected function _isCount($aVote = array())
  113. {
  114. if(empty($aVote))
  115. $aVote = $this->_getVote();
  116. foreach($aVote as $sKey => $mixedValue)
  117. if(substr($sKey, 0, 5) == 'count' && (int)$mixedValue != 0)
  118. return true;
  119. return false;
  120. }
  121. protected function _getVoteData()
  122. {
  123. $aData = parent::_getVoteData();
  124. if($aData === false)
  125. return false;
  126. $sReaction = bx_get('reaction');
  127. if($sReaction === false)
  128. return false;
  129. $aData['reaction'] = bx_process_input($sReaction);
  130. return $aData;
  131. }
  132. protected function _returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bVoted, $aParams = array())
  133. {
  134. $aReactions = $this->getReactions();
  135. $sReaction = $aData['reaction'];
  136. $bUndo = $this->isUndo();
  137. $bDisabled = $bVoted && !$bUndo;
  138. $aVote = $this->_getVote($iObjectId, true);
  139. $aTrack = $bVoted ? $this->_getTrack($iObjectId, $iAuthorId) : array();
  140. $sJsClick = '';
  141. if(!$bDisabled)
  142. $sJsClick = $bVoted && $bUndo ? $this->getJsClickDo($sReaction) : $this->getJsClick();
  143. $iTotalC = $iTotalS = 0;
  144. foreach($aReactions as $sName) {
  145. $iTotalC += (int)$aVote['count_' . $sName];
  146. $iTotalS += (int)$aVote['sum_' . $sName];
  147. }
  148. $fTotalR = $iTotalC != 0 ? round($iTotalS / $iTotalC, 2) : 0;
  149. $iCount = (int)$aVote['count_' . $sReaction];
  150. $aResult = array(
  151. 'code' => 0,
  152. 'reaction' => $sReaction,
  153. 'rate' => $aVote['rate_' . $sReaction],
  154. 'count' => $iCount,
  155. 'countf' => $iCount > 0 ? $this->_getCounterLabel($iCount, array('reaction' => $sReaction)) : '',
  156. 'label_icon' => $this->_getIconDoWithTrack($bVoted, $aTrack),
  157. 'label_title' => _t($this->_getTitleDoWithTrack($bVoted, $aTrack)),
  158. 'label_click' => $sJsClick,
  159. 'disabled' => $bVoted && !$this->isUndo(),
  160. 'total' => array(
  161. 'rate' => $fTotalR,
  162. 'count' => $iTotalC,
  163. 'countf' => $iTotalC > 0 ? $this->_getCounterLabel($iTotalC, array('show_counter_label_icon' => false, 'reaction' => '')) : '',
  164. )
  165. );
  166. return $aResult;
  167. }
  168. protected function _getIconDoWithTrack($bVoted, $aTrack = array())
  169. {
  170. $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
  171. return $this->getIcon($sReaction);
  172. }
  173. protected function _getTitleDoWithTrack($bVoted, $aTrack = array())
  174. {
  175. $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
  176. return $this->_aDataList[$sReaction]['title'];
  177. }
  178. protected function _getTitleDoBy($aParams = array())
  179. {
  180. if(isset($aParams['show_counter_style']) && $aParams['show_counter_style'] == self::$_sCounterStyleCompound)
  181. return _t('_vote_do_by_reactions');
  182. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  183. return _t('_vote_do_by_x_reaction', _t($this->_aDataList[$sReaction]['title']));
  184. }
  185. }
  186. /** @} */