BxDolVoteReactions.php 7.2 KB

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