BxDolScoreQuery.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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)
  29. {
  30. $sQuery = "SELECT
  31. `author_id` AS `id`,
  32. `type` AS `vote_type`,
  33. `date` AS `vote_date`
  34. FROM `{$this->_sTableTrack}`
  35. WHERE `object_id`=:object_id";
  36. return $this->getAll($sQuery, array(
  37. 'object_id' => $iObjectId
  38. ));
  39. }
  40. public function isPostTimeoutEnded($iObjectId, $sAuthorIp)
  41. {
  42. if($this->_iPostTimeout == 0)
  43. return true;
  44. return (int)$this->getOne("SELECT `object_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = :object_id AND `author_nip` = :author_nip AND `date` > :date", array(
  45. 'object_id' => $iObjectId,
  46. 'author_nip' => ip2long($sAuthorIp),
  47. 'date' => time() - $this->_iPostTimeout
  48. )) == 0;
  49. }
  50. public function getScore($iObjectId)
  51. {
  52. $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(
  53. 'object_id' => $iObjectId
  54. ));
  55. if(empty($aResult) || !is_array($aResult))
  56. $aResult = array('count_up' => 0, 'count_down' => 0, 'score' => 0);
  57. return $aResult;
  58. }
  59. public function putVote($iObjectId, $iAuthorId, $sAuthorIp, $sType)
  60. {
  61. $bExists = (int)$this->getOne("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = :object_id LIMIT 1", array('object_id' => $iObjectId)) != 0;
  62. if(!$bExists)
  63. $sQuery = $this->prepare("INSERT INTO {$this->_sTable} SET `object_id` = ?, `count_" . $sType . "` = '1'", $iObjectId);
  64. else
  65. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count_" . $sType . "` = `count_" . $sType . "` + 1 WHERE `object_id` = ?", $iObjectId);
  66. if((int)$this->query($sQuery) == 0)
  67. return false;
  68. if((int)$this->query("INSERT INTO `{$this->_sTableTrack}` SET " . $this->arrayToSQL(array(
  69. 'object_id' => $iObjectId,
  70. 'author_id' => $iAuthorId,
  71. 'author_nip' => ip2long($sAuthorIp),
  72. 'type' => $sType,
  73. 'date' => time()
  74. ))) > 0)
  75. return $this->lastId();
  76. return false;
  77. }
  78. public function getLegend($iObjectId)
  79. {
  80. $sQuery = $this->prepare("SELECT `type` AS `type`, COUNT(`type`) AS `count` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? GROUP BY `type`", $iObjectId);
  81. return $this->getAllWithKey($sQuery, 'type');
  82. }
  83. public function getSqlParts($sMainTable, $sMainField)
  84. {
  85. $aResult = parent::getSqlParts($sMainTable, $sMainField);
  86. if(empty($aResult))
  87. return $aResult;
  88. $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` ";
  89. return $aResult;
  90. }
  91. protected function _updateTriggerTable($iObjectId, $aEntry)
  92. {
  93. $aSet = array(
  94. $this->_sTriggerFieldScore => $aEntry['score'],
  95. $this->_sTriggerFieldCup => $aEntry['count_up'],
  96. $this->_sTriggerFieldCdown => $aEntry['count_down']
  97. );
  98. return (int)$this->query("UPDATE `{$this->_sTriggerTable}` SET " . $this->arrayToSQL($aSet) . " WHERE `{$this->_sTriggerFieldId}` = :object_id", array(
  99. 'object_id' => $iObjectId
  100. )) > 0;
  101. }
  102. protected function _deleteAuthorEntriesTableMain($aTrack)
  103. {
  104. return $this->query("UPDATE `{$this->_sTable}` SET `count_" . $aTrack['type'] . "`=`count_" . $aTrack['type'] . "`-1 WHERE `object_id`=:object_id", array(
  105. 'object_id' => $aTrack['object_id']
  106. ));
  107. }
  108. protected function _deleteAuthorEntriesTableTrigger($aTrack)
  109. {
  110. $aScore = $this->getScore($aTrack['object_id']);
  111. return $this->_updateTriggerTable($aTrack['object_id'], $aScore);
  112. }
  113. }
  114. /** @} */