BxDolVoteReactionsQuery.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * @see BxDolVote
  11. */
  12. class BxDolVoteReactionsQuery extends BxDolVoteQuery
  13. {
  14. public function __construct(&$oModule)
  15. {
  16. parent::__construct($oModule);
  17. }
  18. public function getVote($iObjectId)
  19. {
  20. $aReactions = $this->_oModule->getReactions();
  21. $aReactionsGot = $this->getAllWithKey('SELECT * FROM ' . $this->_sTable . ' WHERE `object_id` = :object_id', 'reaction', array('object_id' => $iObjectId));
  22. $aResult = array();
  23. foreach($aReactions as $sName) {
  24. $iCount = $iSum = 0;
  25. if(!empty($aReactionsGot[$sName])) {
  26. $iCount = (int)$aReactionsGot[$sName]['count'];
  27. $iSum = (int)$aReactionsGot[$sName]['sum'];
  28. }
  29. $aResult['count_' . $sName] = $iCount;
  30. $aResult['sum_' . $sName] = $iSum;
  31. $aResult['rate_' . $sName] = $iCount != 0 ? $iSum / $iCount : 0;
  32. }
  33. return $aResult;
  34. }
  35. public function putVote($iObjectId, $iAuthorId, $sAuthorIp, $aData, $bUndo = false)
  36. {
  37. $sReaction = $aData['reaction'];
  38. $aReaction = $this->_oModule->getReaction($sReaction);
  39. if($aReaction === false)
  40. return false;
  41. $iValue = (int)$aData['value'];
  42. $fValueFinal = $iValue * (float)$aReaction['weight'];
  43. $sQuery = "SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = :object_id AND `reaction` = :reaction LIMIT 1";
  44. $bExists = (int)$this->getOne($sQuery, array('object_id' => $iObjectId, 'reaction' => $sReaction)) != 0;
  45. if(!$bExists && $bUndo)
  46. return false;
  47. if(!$bExists)
  48. $sQuery = $this->prepare("INSERT INTO {$this->_sTable} SET `object_id` = ?, `reaction` = ?, `count` = '1', `sum` = ?", $iObjectId, $sReaction, $fValueFinal);
  49. else
  50. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` " . ($bUndo ? "-" : "+") . " 1, `sum` = `sum` " . ($bUndo ? "-" : "+") . " ? WHERE `object_id` = ? AND `reaction` = ?", $fValueFinal, $iObjectId, $sReaction);
  51. if((int)$this->query($sQuery) == 0)
  52. return false;
  53. if($bUndo)
  54. return $this->_deleteTrack($iObjectId, $iAuthorId);
  55. $iNow = time();
  56. $iAuthorNip = ip2long($sAuthorIp);
  57. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `author_nip` = ?, `reaction` = ?, `value` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, $sReaction, $iValue, $iNow);
  58. if((int)$this->query($sQuery) > 0)
  59. return $this->lastId();
  60. return false;
  61. }
  62. public function getSqlParts($sMainTable, $sMainField)
  63. {
  64. if(empty($this->_sTable) || empty($sMainTable) || empty($sMainField))
  65. return array();
  66. $sFields = $sJoin = '';
  67. $aReactions = $this->_oModule->getReactions();
  68. foreach($aReactions as $iIndex => $sName) {
  69. $sFields .= ", `t{$iIndex}`.`count` as `vote_count_{$sName}`, (`t{$iIndex}`.`sum` / `t{$iIndex}`.`count`) AS `vote_rate_{$sName}` ";
  70. $sJoin .= " LEFT JOIN `{$this->_sTable}` AS `t{$iIndex}` ON (`t{$iIndex}`.`object_id` = `{$sMainTable}`.`{$sMainField}` AND `t{$iIndex}`.`reaction` = '{$sName}') ";
  71. }
  72. return array (
  73. 'fields' => $sFields,
  74. 'join' => $sJoin,
  75. );
  76. }
  77. public function getPerformed($aParams = array())
  78. {
  79. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  80. $aBindings = array();
  81. $sSelectClause = '*';
  82. $sWhereClause = '';
  83. $sLimitClause = isset($aParams['start']) && !empty($aParams['per_page']) ? "LIMIT " . $aParams['start'] . ", " . $aParams['per_page'] : "";
  84. if(!empty($aParams['type']))
  85. switch($aParams['type']) {
  86. case 'by':
  87. $aMethod['name'] = 'getColumn';
  88. $aBindings['object_id'] = $aParams['object_id'];
  89. $aBindings['reaction'] = $aParams['reaction'];
  90. $sSelectClause = '`author_id`';
  91. $sWhereClause = 'AND `object_id` = :object_id AND `reaction`=:reaction';
  92. break;
  93. }
  94. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $this->_sTableTrack . "` WHERE 1 " . $sWhereClause . $sLimitClause;
  95. $aMethod['params'][] = $aBindings;
  96. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  97. }
  98. /**
  99. * Update trigger table.
  100. * @param integer $iObjectId - object ID;
  101. * @param array $aEntry - contains data received from BxDolVoteReactionsQuery::getVote method;
  102. * @return boolean - operation result.
  103. */
  104. protected function _updateTriggerTable($iObjectId, $aEntry)
  105. {
  106. $iCounted = $iTotalCount = $fTotalRate = 0;
  107. $aReactions = $this->_oModule->getReactions();
  108. foreach($aReactions as $sName) {
  109. $iCount = (int)$aEntry['count_' . $sName];
  110. if($iCount == 0)
  111. continue;
  112. $iTotalCount += $iCount;
  113. $fTotalRate += (float)$aEntry['rate_' . $sName];
  114. $iCounted += 1;
  115. }
  116. return (int)$this->query("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}`=:count, `{$this->_sTriggerFieldRate}`=:rate WHERE `{$this->_sTriggerFieldId}`=:id", array(
  117. 'count' => $iTotalCount,
  118. 'rate' => $iCounted != 0 ? $fTotalRate / $iCounted : 0,
  119. 'id' => $iObjectId
  120. )) > 0;
  121. }
  122. protected function _deleteAuthorEntriesTableMain($aTrack)
  123. {
  124. $aReaction = $this->_oModule->getReaction($aTrack['reaction']);
  125. return $this->query("UPDATE `{$this->_sTable}` SET `count`=`count`-1, `sum`=`sum`-:value WHERE `object_id`=:object_id AND `reaction`=:reaction", array(
  126. 'value' => (int)$aTrack['value'] * (float)$aReaction['weight'],
  127. 'object_id' => $aTrack['object_id'],
  128. 'reaction' => $aTrack['reaction']
  129. ));
  130. }
  131. }
  132. /** @} */