BxDolVoteQuery.php 4.9 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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolDb');
  10. /**
  11. * @see BxDolVote
  12. */
  13. class BxDolVoteQuery extends BxDolDb
  14. {
  15. protected $_oModule;
  16. protected $_sTable;
  17. protected $_sTableTrack;
  18. protected $_sTriggerTable;
  19. protected $_sTriggerFieldId;
  20. protected $_sTriggerFieldRate;
  21. protected $_sTriggerFieldCount;
  22. protected $_iPostTimeout;
  23. public function __construct(&$oModule)
  24. {
  25. parent::BxDolDb();
  26. $this->_oModule = $oModule;
  27. $aSystem = $this->_oModule->getSystemInfo();
  28. $this->_sTable = $aSystem['table_main'];
  29. $this->_sTableTrack = $aSystem['table_track'];
  30. $this->_sTriggerTable = $aSystem['trigger_table'];
  31. $this->_sTriggerFieldId = $aSystem['trigger_field_id'];
  32. $this->_sTriggerFieldRate = $aSystem['trigger_field_rate'];
  33. $this->_sTriggerFieldCount = $aSystem['trigger_field_count'];
  34. $this->_iPostTimeout = (int)$aSystem['post_timeout'];
  35. }
  36. public function isPostTimeoutEnded($iObjectId, $sAuthorIp)
  37. {
  38. if($this->_iPostTimeout == 0)
  39. return true;
  40. $iAuthorNip = ip2long($sAuthorIp);
  41. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_nip` = ? AND `date` > (UNIX_TIMESTAMP() - ?)", $iObjectId, $iAuthorNip, $this->_iPostTimeout);
  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. $iAuthorNip = ip2long($sAuthorIp);
  73. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `author_nip` = ?, `value` = ?, `date` = UNIX_TIMESTAMP()", $iObjectId, $iAuthorId, $iAuthorNip, $iValue);
  74. }
  75. return (int)$this->query($sQuery) > 0;
  76. }
  77. public function getSqlParts($sMainTable, $sMainField)
  78. {
  79. if(empty($sMainTable) || empty($sMainField))
  80. return array();
  81. return array (
  82. 'fields' => ",`{$this->_sTable}`.`count` as `vote_count`, (`{$this->_sTable}`.`sum` / `{$this->_sTable}`.`count`) AS `vote_rate` ",
  83. 'join' => $this->prepare(" LEFT JOIN `{$this->_sTable}` ON (`{$this->_sTable}`.`object_id` = ?) ", "`{$sMainTable}`.`{$sMainField}`"),
  84. );
  85. }
  86. public function getVotedBy($iObjectId)
  87. {
  88. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableTrack}` WHERE `object_id`=?", $iObjectId);
  89. return $this->getColumn($sQuery);
  90. }
  91. public function deleteObjectVotes($iObjectId)
  92. {
  93. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `object_id` = ?", $iObjectId);
  94. $this->query ($sQuery);
  95. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  96. $sQuery = $this->prepare("DELETE FROM {$this->_sTableTrack} WHERE `object_id` = ?", $iObjectId);
  97. $this->query ($sQuery);
  98. $this->query ("OPTIMIZE TABLE {$this->_sTableTrack}");
  99. }
  100. public function updateTriggerTable($iObjectId) {
  101. $aVote = $this->getVote($iObjectId);
  102. if(empty($aVote) || !is_array($aVote))
  103. return false;
  104. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ?, `{$this->_sTriggerFieldRate}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aVote['count'], $aVote['rate'], $iObjectId);
  105. return (int)$this->query($sQuery) > 0;
  106. }
  107. }
  108. /** @} */