BxDolScoreQuery.php 5.3 KB

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