BxDolVoteQuery.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 BxDolVoteQuery extends BxDolObjectQuery
  13. {
  14. protected $_sTriggerFieldRate;
  15. protected $_iPostTimeout;
  16. public function __construct(&$oModule)
  17. {
  18. parent::__construct($oModule);
  19. $aSystem = $this->_oModule->getSystemInfo();
  20. $this->_sTriggerFieldRate = $aSystem['trigger_field_rate'];
  21. $this->_iPostTimeout = (int)$aSystem['post_timeout'];
  22. $this->_sMethodGetEntry = 'getVote';
  23. }
  24. public function isPostTimeoutEnded($iObjectId, $iAuthorId, $sAuthorIp)
  25. {
  26. if($this->_iPostTimeout == 0)
  27. return true;
  28. $aBindings = array(
  29. 'object_id' => $iObjectId,
  30. 'date' => time() - $this->_iPostTimeout
  31. );
  32. $sWhereClause = " AND `object_id` = :object_id AND `date` > :date";
  33. if(!empty($iAuthorId)) {
  34. $aBindings['author_id'] = $iAuthorId;
  35. $sWhereClause .= " AND `author_id` = :author_id";
  36. }
  37. else {
  38. $aBindings['author_nip'] = ip2long($sAuthorIp);
  39. $sWhereClause .= " AND `author_nip` = :author_nip";
  40. }
  41. return (int)$this->getOne("SELECT `object_id` FROM `" . $this->_sTableTrack . "` WHERE 1" . $sWhereClause, $aBindings) == 0;
  42. }
  43. public function getVote($iObjectId)
  44. {
  45. $sQuery = $this->prepare("SELECT `count` as `count`, `sum` as `sum`, ROUND(`sum` / `count`, 2) AS `rate` FROM {$this->_sTable} WHERE `object_id` = ? LIMIT 1", $iObjectId);
  46. $aResult = $this->getRow($sQuery);
  47. if(empty($aResult) || !is_array($aResult))
  48. $aResult = array('count' => 0, 'sum' => 0, 'rate' => 0);
  49. return $aResult;
  50. }
  51. public function putVote($iObjectId, $iAuthorId, $sAuthorIp, $aData, $bUndo = false)
  52. {
  53. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  54. $bExists = (int)$this->getOne($sQuery) != 0;
  55. if(!$bExists && $bUndo)
  56. return false;
  57. $iValue = $aData['value'];
  58. if(!$bExists)
  59. $sQuery = $this->prepare("INSERT INTO {$this->_sTable} SET `object_id` = ?, `count` = '1', `sum` = ?", $iObjectId, $iValue);
  60. else
  61. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` " . ($bUndo ? "-" : "+") . " 1, `sum` = `sum` " . ($bUndo ? "-" : "+") . " ? WHERE `object_id` = ?", $iValue, $iObjectId);
  62. if((int)$this->query($sQuery) == 0)
  63. return false;
  64. if($bUndo)
  65. return $this->_deleteTrack($iObjectId, $iAuthorId);
  66. $iNow = time();
  67. $iAuthorNip = ip2long($sAuthorIp);
  68. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `author_nip` = ?, `value` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, $iValue, $iNow);
  69. if((int)$this->query($sQuery) > 0)
  70. return $this->lastId();
  71. return false;
  72. }
  73. public function getLegend($iObjectId)
  74. {
  75. $sQuery = $this->prepare("SELECT `value` AS `value`, COUNT(`value`) AS `count` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? GROUP BY `value`", $iObjectId);
  76. return $this->getAllWithKey($sQuery, 'value');
  77. }
  78. public function getSqlParts($sMainTable, $sMainField)
  79. {
  80. $aResult = parent::getSqlParts($sMainTable, $sMainField);
  81. if(empty($aResult))
  82. return $aResult;
  83. $aResult['fields'] = ", `{$this->_sTable}`.`count` as `vote_count`, (`{$this->_sTable}`.`sum` / `{$this->_sTable}`.`count`) AS `vote_rate` ";
  84. return $aResult;
  85. }
  86. public function updateTriggerTableValue($iObjectId, $iValue)
  87. {
  88. return false;
  89. }
  90. protected function _deleteTrack($iObjectId, $iAuthorId)
  91. {
  92. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  93. $iId = (int)$this->getOne($sQuery);
  94. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `id` = ? LIMIT 1", $iId);
  95. if((int)$this->query($sQuery) > 0)
  96. return $iId;
  97. return false;
  98. }
  99. protected function _updateTriggerTable($iObjectId, $aEntry)
  100. {
  101. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ?, `{$this->_sTriggerFieldRate}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $aEntry['rate'], $iObjectId);
  102. return (int)$this->query($sQuery) > 0;
  103. }
  104. protected function _deleteAuthorEntriesTableMain($aTrack)
  105. {
  106. return $this->query("UPDATE `{$this->_sTable}` SET `count`=`count`-1, `sum`=`sum`-:value WHERE `object_id`=:object_id", array(
  107. 'object_id' => $aTrack['object_id'],
  108. 'value' => $aTrack['value']
  109. ));
  110. }
  111. protected function _deleteAuthorEntriesTableTrigger($aTrack)
  112. {
  113. $aVote = $this->getVote($aTrack['object_id']);
  114. return $this->_updateTriggerTable($aTrack['object_id'], $aVote);
  115. }
  116. }
  117. /** @} */