BxDolVoteQuery.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. /**
  10. * @see BxDolVote
  11. */
  12. class BxDolVoteQuery extends BxDolDb
  13. {
  14. protected $_oModule;
  15. protected $_sTable;
  16. protected $_sTableTrack;
  17. protected $_sTriggerTable;
  18. protected $_sTriggerFieldId;
  19. protected $_sTriggerFieldRate;
  20. protected $_sTriggerFieldCount;
  21. protected $_iPostTimeout;
  22. public function __construct(&$oModule)
  23. {
  24. parent::__construct();
  25. $this->_oModule = $oModule;
  26. $aSystem = $this->_oModule->getSystemInfo();
  27. $this->_sTable = $aSystem['table_main'];
  28. $this->_sTableTrack = $aSystem['table_track'];
  29. $this->_sTriggerTable = $aSystem['trigger_table'];
  30. $this->_sTriggerFieldId = $aSystem['trigger_field_id'];
  31. $this->_sTriggerFieldRate = $aSystem['trigger_field_rate'];
  32. $this->_sTriggerFieldCount = $aSystem['trigger_field_count'];
  33. $this->_iPostTimeout = (int)$aSystem['post_timeout'];
  34. }
  35. public function isPostTimeoutEnded($iObjectId, $sAuthorIp)
  36. {
  37. if($this->_iPostTimeout == 0)
  38. return true;
  39. $iDate = time() - $this->_iPostTimeout;
  40. $iAuthorNip = ip2long($sAuthorIp);
  41. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_nip` = ? AND `date` > ?", $iObjectId, $iAuthorNip, $iDate);
  42. return (int)$this->getOne($sQuery) == 0;
  43. }
  44. public function getVote($iObjectId)
  45. {
  46. $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);
  47. $aResult = $this->getRow($sQuery);
  48. if(empty($aResult) || !is_array($aResult))
  49. $aResult = array('count' => 0, 'sum' => 0, 'rate' => 0);
  50. return $aResult;
  51. }
  52. public function isVoted($iObjectId, $iAuthorId)
  53. {
  54. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  55. return (int)$this->getOne($sQuery) != 0;
  56. }
  57. public function putVote($iObjectId, $iAuthorId, $sAuthorIp, $iValue, $bUndo = false)
  58. {
  59. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  60. $bExists = (int)$this->getOne($sQuery) != 0;
  61. if(!$bExists && $bUndo)
  62. return false;
  63. if(!$bExists)
  64. $sQuery = $this->prepare("INSERT INTO {$this->_sTable} SET `object_id` = ?, `count` = '1', `sum` = ?", $iObjectId, $iValue);
  65. else
  66. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` " . ($bUndo ? "-" : "+") . " 1, `sum` = `sum` " . ($bUndo ? "-" : "+") . " ? WHERE `object_id` = ?", $iValue, $iObjectId);
  67. if((int)$this->query($sQuery) == 0)
  68. return false;
  69. if($bUndo)
  70. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  71. else {
  72. $iNow = time();
  73. $iAuthorNip = ip2long($sAuthorIp);
  74. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `author_nip` = ?, `value` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, $iValue, $iNow);
  75. }
  76. return (int)$this->query($sQuery) > 0;
  77. }
  78. public function getSqlParts($sMainTable, $sMainField)
  79. {
  80. if(empty($sMainTable) || empty($sMainField))
  81. return array();
  82. return array (
  83. 'fields' => ",`{$this->_sTable}`.`count` as `vote_count`, (`{$this->_sTable}`.`sum` / `{$this->_sTable}`.`count`) AS `vote_rate` ",
  84. 'join' => " LEFT JOIN `{$this->_sTable}` ON (`{$this->_sTable}`.`object_id` = `{$sMainTable}`.`{$sMainField}`) ",
  85. );
  86. }
  87. public function getVotedBy($iObjectId)
  88. {
  89. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableTrack}` WHERE `object_id`=?", $iObjectId);
  90. return $this->getColumn($sQuery);
  91. }
  92. public function deleteObjectVotes($iObjectId)
  93. {
  94. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `object_id` = ?", $iObjectId);
  95. $this->query ($sQuery);
  96. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  97. $sQuery = $this->prepare("DELETE FROM {$this->_sTableTrack} WHERE `object_id` = ?", $iObjectId);
  98. $this->query ($sQuery);
  99. $this->query ("OPTIMIZE TABLE {$this->_sTableTrack}");
  100. }
  101. public function updateTriggerTable($iObjectId)
  102. {
  103. $aVote = $this->getVote($iObjectId);
  104. if(empty($aVote) || !is_array($aVote))
  105. return false;
  106. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ?, `{$this->_sTriggerFieldRate}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aVote['count'], $aVote['rate'], $iObjectId);
  107. return (int)$this->query($sQuery) > 0;
  108. }
  109. }
  110. /** @} */