BxDolVoteQuery.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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::__construct();
  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. $iDate = time() - $this->_iPostTimeout;
  41. $iAuthorNip = ip2long($sAuthorIp);
  42. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_nip` = ? AND `date` > ?", $iObjectId, $iAuthorNip, $iDate);
  43. return (int)$this->getOne($sQuery) == 0;
  44. }
  45. public function getVote($iObjectId)
  46. {
  47. $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);
  48. $aResult = $this->getRow($sQuery);
  49. if(empty($aResult) || !is_array($aResult))
  50. $aResult = array('count' => 0, 'sum' => 0, 'rate' => 0);
  51. return $aResult;
  52. }
  53. public function isVoted($iObjectId, $iAuthorId)
  54. {
  55. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  56. return (int)$this->getOne($sQuery) != 0;
  57. }
  58. public function putVote($iObjectId, $iAuthorId, $sAuthorIp, $iValue, $bUndo = false)
  59. {
  60. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  61. $bExists = (int)$this->getOne($sQuery) != 0;
  62. if(!$bExists && $bUndo)
  63. return false;
  64. if(!$bExists)
  65. $sQuery = $this->prepare("INSERT INTO {$this->_sTable} SET `object_id` = ?, `count` = '1', `sum` = ?", $iObjectId, $iValue);
  66. else
  67. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` " . ($bUndo ? "-" : "+") . " 1, `sum` = `sum` " . ($bUndo ? "-" : "+") . " ? WHERE `object_id` = ?", $iValue, $iObjectId);
  68. if((int)$this->query($sQuery) == 0)
  69. return false;
  70. if($bUndo)
  71. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  72. else {
  73. $iNow = time();
  74. $iAuthorNip = ip2long($sAuthorIp);
  75. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `author_nip` = ?, `value` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, $iValue, $iNow);
  76. }
  77. return (int)$this->query($sQuery) > 0;
  78. }
  79. public function getSqlParts($sMainTable, $sMainField)
  80. {
  81. if(empty($sMainTable) || empty($sMainField))
  82. return array();
  83. return array (
  84. 'fields' => ",`{$this->_sTable}`.`count` as `vote_count`, (`{$this->_sTable}`.`sum` / `{$this->_sTable}`.`count`) AS `vote_rate` ",
  85. 'join' => " LEFT JOIN `{$this->_sTable}` ON (`{$this->_sTable}`.`object_id` = `{$sMainTable}`.`{$sMainField}`) ",
  86. );
  87. }
  88. public function getVotedBy($iObjectId)
  89. {
  90. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableTrack}` WHERE `object_id`=?", $iObjectId);
  91. return $this->getColumn($sQuery);
  92. }
  93. public function deleteObjectVotes($iObjectId)
  94. {
  95. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `object_id` = ?", $iObjectId);
  96. $this->query ($sQuery);
  97. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  98. $sQuery = $this->prepare("DELETE FROM {$this->_sTableTrack} WHERE `object_id` = ?", $iObjectId);
  99. $this->query ($sQuery);
  100. $this->query ("OPTIMIZE TABLE {$this->_sTableTrack}");
  101. }
  102. public function updateTriggerTable($iObjectId) {
  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. /** @} */