BxDolScoreQuery.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 BxDolScore
  11. */
  12. class BxDolScoreQuery extends BxDolObjectQuery
  13. {
  14. protected $_sTriggerFieldScore;
  15. protected $_sTriggerFieldCup;
  16. protected $_sTriggerFieldCdown;
  17. protected $_iPostTimeout;
  18. public function __construct(&$oModule)
  19. {
  20. parent::__construct($oModule);
  21. $aSystem = $this->_oModule->getSystemInfo();
  22. $this->_sTriggerFieldScore = $aSystem['trigger_field_score'];
  23. $this->_sTriggerFieldCup = $aSystem['trigger_field_cup'];
  24. $this->_sTriggerFieldCdown = $aSystem['trigger_field_cdown'];
  25. $this->_iPostTimeout = (int)$aSystem['post_timeout'];
  26. $this->_sMethodGetEntry = 'getScore';
  27. }
  28. public function getPerformedBy($iObjectId, $aParams = [], $iStart = 0, $iPerPage = 0)
  29. {
  30. $aBindings = [
  31. 'object_id' => $iObjectId
  32. ];
  33. $sWhereClause = " AND `object_id`=:object_id";
  34. if(!empty($aParams['type'])) {
  35. $sWhereClause .= " AND `type`=:type";
  36. $aBindings['type'] = $aParams['type'];
  37. }
  38. $sLimitClause = "";
  39. if(!empty($aParams['per_page']))
  40. $sLimitClause = $this->prepareAsString(" LIMIT ?, ?", $aParams['start'], $aParams['per_page']);
  41. $sQuery = "SELECT
  42. `author_id` AS `id`,
  43. `type` AS `vote_type`,
  44. `date` AS `vote_date`
  45. FROM `{$this->_sTableTrack}`
  46. WHERE 1" . $sWhereClause . $sLimitClause;
  47. return $this->getAll($sQuery, $aBindings);
  48. }
  49. public function isPostTimeoutEnded($iObjectId, $iAuthorId, $sAuthorIp)
  50. {
  51. if($this->_iPostTimeout == 0)
  52. return true;
  53. $aBindings = array(
  54. 'object_id' => $iObjectId,
  55. 'date' => time() - $this->_iPostTimeout
  56. );
  57. $sWhereClause = " AND `object_id` = :object_id AND `date` > :date";
  58. if(!empty($iAuthorId)) {
  59. $aBindings['author_id'] = $iAuthorId;
  60. $sWhereClause .= " AND `author_id` = :author_id";
  61. }
  62. else {
  63. $aBindings['author_nip'] = bx_get_ip_hash($sAuthorIp);
  64. $sWhereClause .= " AND `author_nip` = :author_nip";
  65. }
  66. return (int)$this->getOne("SELECT `object_id` FROM `" . $this->_sTableTrack . "` WHERE 1" . $sWhereClause, $aBindings) == 0;
  67. }
  68. public function getScore($iObjectId)
  69. {
  70. $aResult = $this->getRow("SELECT `count_up`, `count_down`, `count_up` - `count_down` AS `score` FROM {$this->_sTable} WHERE `object_id` = :object_id LIMIT 1", array(
  71. 'object_id' => $iObjectId
  72. ));
  73. if(empty($aResult) || !is_array($aResult))
  74. $aResult = array('count_up' => 0, 'count_down' => 0, 'score' => 0);
  75. return $aResult;
  76. }
  77. public function putVote($iObjectId, $iAuthorId, $sAuthorIp, $aData, $bUndo = false)
  78. {
  79. $bExists = (int)$this->getOne("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = :object_id LIMIT 1", ['object_id' => $iObjectId]) != 0;
  80. if(!$bExists && $bUndo)
  81. return false;
  82. $sType = $aData['type'];
  83. if(!$bExists)
  84. $sQuery = $this->prepare("INSERT INTO {$this->_sTable} SET `object_id` = ?, `count_" . $sType . "` = '1'", $iObjectId);
  85. else
  86. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count_" . $sType . "` = `count_" . $sType . "` " . ($bUndo ? "-" : "+") . " 1 WHERE `object_id` = ?", $iObjectId);
  87. if((int)$this->query($sQuery) == 0)
  88. return false;
  89. if($bUndo)
  90. return $this->_deleteTrack($iObjectId, $iAuthorId);
  91. if((int)$this->query("INSERT INTO `{$this->_sTableTrack}` SET " . $this->arrayToSQL([
  92. 'object_id' => $iObjectId,
  93. 'author_id' => $iAuthorId,
  94. 'author_nip' => bx_get_ip_hash($sAuthorIp),
  95. 'type' => $sType,
  96. 'date' => time()
  97. ])) > 0)
  98. return $this->lastId();
  99. return false;
  100. }
  101. public function getLegend($iObjectId)
  102. {
  103. $sQuery = $this->prepare("SELECT `type` AS `type`, COUNT(`type`) AS `count` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? GROUP BY `type`", $iObjectId);
  104. return $this->getAllWithKey($sQuery, 'type');
  105. }
  106. public function getSqlParts($sMainTable, $sMainField)
  107. {
  108. $aResult = parent::getSqlParts($sMainTable, $sMainField);
  109. if(empty($aResult))
  110. return $aResult;
  111. $aResult['fields'] = ", `{$this->_sTable}`.`count_up` AS `score_cup`, `{$this->_sTable}`.`count_down` AS `score_cdown`, (`{$this->_sTable}`.`count_up` - `{$this->_sTable}`.`count_down`) AS `score` ";
  112. return $aResult;
  113. }
  114. protected function _deleteTrack($iObjectId, $iAuthorId)
  115. {
  116. $iId = (int)$this->getOne("SELECT `id` FROM `{$this->_sTableTrack}` WHERE `object_id`=:object_id AND `author_id`=:author_id LIMIT 1", [
  117. 'object_id' => $iObjectId,
  118. 'author_id' => $iAuthorId
  119. ]);
  120. if((int)$this->query("DELETE FROM `{$this->_sTableTrack}` WHERE `id`=:id LIMIT 1", ['id' => $iId]) > 0)
  121. return $iId;
  122. return false;
  123. }
  124. protected function _updateTriggerTable($iObjectId, $aEntry)
  125. {
  126. $aSet = array(
  127. $this->_sTriggerFieldScore => $aEntry['score'],
  128. $this->_sTriggerFieldCup => $aEntry['count_up'],
  129. $this->_sTriggerFieldCdown => $aEntry['count_down']
  130. );
  131. return (int)$this->query("UPDATE `{$this->_sTriggerTable}` SET " . $this->arrayToSQL($aSet) . " WHERE `{$this->_sTriggerFieldId}` = :object_id", array(
  132. 'object_id' => $iObjectId
  133. )) > 0;
  134. }
  135. protected function _deleteAuthorEntriesTableMain($aTrack)
  136. {
  137. return $this->query("UPDATE `{$this->_sTable}` SET `count_" . $aTrack['type'] . "`=`count_" . $aTrack['type'] . "`-1 WHERE `object_id`=:object_id", array(
  138. 'object_id' => $aTrack['object_id']
  139. ));
  140. }
  141. protected function _deleteAuthorEntriesTableTrigger($aTrack)
  142. {
  143. $aScore = $this->getScore($aTrack['object_id']);
  144. return $this->_updateTriggerTable($aTrack['object_id'], $aScore);
  145. }
  146. }
  147. /** @} */