BxDolVoteReactions.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 $_sDataList; //--- Reactions list name.
  15. protected $_aDataList; //--- Reactions list with all reactions.
  16. protected $_sDefault; //--- Default reaction name.
  17. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  18. {
  19. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  20. $this->_oQuery = new BxDolVoteReactionsQuery($this);
  21. $this->_sType = BX_DOL_VOTE_TYPE_REACTIONS;
  22. $this->_sDataList = 'sys_vote_reactions';
  23. $this->_aDataList = array();
  24. $this->_sDefault = '';
  25. }
  26. public function init($iId)
  27. {
  28. if(!parent::init($iId))
  29. return false;
  30. $aReactions = BxDolFormQuery::getDataItems($this->_sDataList, false, BX_DATA_VALUES_ALL);
  31. foreach($aReactions as $sReaction => $aReaction) {
  32. $aData = !empty($aReaction['Data']) ? unserialize($aReaction['Data']) : array();
  33. $this->_aDataList[$sReaction] = array(
  34. 'name' => $sReaction,
  35. 'title' => $aReaction['LKey'],
  36. 'title_aux' => $aReaction['LKey2'],
  37. 'icon' => isset($aData['icon']) ? $aData['icon'] : '',
  38. 'weight' => isset($aData['weight']) ? $aData['weight'] : 1,
  39. );
  40. }
  41. if(!empty($this->_aDataList)) {
  42. $aNames = array_keys($this->_aDataList);
  43. $this->_sDefault = array_shift($aNames);
  44. }
  45. }
  46. /**
  47. * Interface functions for outer usage
  48. */
  49. public function getValue()
  50. {
  51. return (int)$this->_aSystem['min_value'];
  52. }
  53. public function getReaction($sName)
  54. {
  55. if(empty($this->_aDataList))
  56. $this->getReactions();
  57. return !empty($this->_aDataList[$sName]) ? $this->_aDataList[$sName] : false;
  58. }
  59. public function getReactions($bFullInfo = false)
  60. {
  61. return $bFullInfo ? $this->_aDataList : array_keys($this->_aDataList);
  62. }
  63. public function getTrackBy($aParams)
  64. {
  65. return $this->_oQuery->getTrackBy($aParams);
  66. }
  67. /**
  68. * Actions functions
  69. */
  70. public function actionGetDoVotePopup()
  71. {
  72. if(!$this->isEnabled())
  73. return '';
  74. return $this->_getDoVotePopup((int)bx_get('value'));
  75. }
  76. public function actionGetVotedBy()
  77. {
  78. if (!$this->isEnabled())
  79. return '';
  80. $sReaction = bx_get('reaction');
  81. $sReaction = $sReaction !== false ? bx_process_input($sReaction) : $this->_sDefault;
  82. return $this->_getVotedBy(array('reaction' => $sReaction));
  83. }
  84. /**
  85. * Internal functions
  86. */
  87. protected function _isDuplicate($iObjectId, $iAuthorId, $iAuthorIp, $bVoted)
  88. {
  89. return $bVoted && !$this->isUndo();
  90. }
  91. protected function _isCount($aVote = array())
  92. {
  93. if(empty($aVote))
  94. $aVote = $this->_getVote();
  95. foreach($aVote as $sKey => $mixedValue)
  96. if(substr($sKey, 0, 5) == 'count' && (int)$mixedValue != 0)
  97. return true;
  98. return false;
  99. }
  100. protected function _getVoteData()
  101. {
  102. $aData = parent::_getVoteData();
  103. if($aData === false)
  104. return false;
  105. $sReaction = bx_get('reaction');
  106. if($sReaction === false)
  107. return false;
  108. $aData['reaction'] = bx_process_input($sReaction);
  109. return $aData;
  110. }
  111. protected function _returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bVoted)
  112. {
  113. $sReaction = $aData['reaction'];
  114. $bUndo = $this->isUndo();
  115. $bDisabled = $bVoted && !$bUndo;
  116. $aVote = $this->_getVote($iObjectId, true);
  117. $aTrack = $bVoted ? $this->_getTrack($iObjectId, $iAuthorId) : array();
  118. $sJsClick = '';
  119. if(!$bDisabled)
  120. $sJsClick = $bVoted && $bUndo ? $this->getJsClickDo($sReaction) : $this->getJsClick();
  121. $iCount = (int)$aVote['count_' . $sReaction];
  122. return array(
  123. 'code' => 0,
  124. 'reaction' => $sReaction,
  125. 'rate' => $aVote['rate_' . $sReaction],
  126. 'count' => $iCount,
  127. 'countf' => $iCount > 0 ? $this->_getLabelCounter($iCount, array('reaction' => $sReaction)) : '',
  128. 'label_icon' => $this->_getIconDoWithTrack($bVoted, $aTrack),
  129. 'label_title' => _t($this->_getTitleDoWithTrack($bVoted, $aTrack)),
  130. 'label_click' => $sJsClick,
  131. 'disabled' => $bVoted && !$this->isUndo(),
  132. );
  133. }
  134. protected function _getIconDoWithTrack($bVoted, $aTrack = array())
  135. {
  136. $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
  137. return $this->_aDataList[$sReaction]['icon'];
  138. }
  139. protected function _getTitleDoWithTrack($bVoted, $aTrack = array())
  140. {
  141. $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
  142. return $this->_aDataList[$sReaction]['title'];
  143. }
  144. protected function _getTitleDoBy($aParams = array())
  145. {
  146. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  147. return _t('_vote_do_by_reactions', _t($this->_aDataList[$sReaction]['title']));
  148. }
  149. }
  150. /** @} */