BxDolScoreQuery.php 4.4 KB

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