BxDolScoreQuery.php 5.1 KB

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