BxDolReportQuery.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 BxDolReport
  11. */
  12. class BxDolReportQuery extends BxDolObjectQuery
  13. {
  14. public function __construct(&$oModule)
  15. {
  16. parent::__construct($oModule);
  17. $this->_sMethodGetEntry = 'getReport';
  18. }
  19. public function getSqlParts($sMainTable, $sMainField)
  20. {
  21. $aResult = parent::getSqlParts($sMainTable, $sMainField);
  22. if(empty($aResult))
  23. return $aResult;
  24. $aResult['fields'] = ", `{$this->_sTable}`.`count` as `report_count` ";
  25. return $aResult;
  26. }
  27. public function getDataById($iId)
  28. {
  29. $sQuery = $this->prepare("SELECT * FROM `{$this->_sTableTrack}` WHERE `id` = ?", $iId);
  30. return $this->getRow($sQuery);
  31. }
  32. public function getPerformedBy($iObjectId, $iStart = 0, $iPerPage = 0)
  33. {
  34. $sLimitClause = "";
  35. if(!empty($iPerPage))
  36. $sLimitClause = $this->prepareAsString(" LIMIT ?, ?", $iStart, $iPerPage);
  37. $sQuery = $this->prepare("SELECT `author_id`, `type`, `text`, `id`, `date` FROM `{$this->_sTableTrack}` WHERE `object_id`=? ORDER BY `date` DESC" . $sLimitClause, $iObjectId);
  38. return $this->getAll($sQuery);
  39. }
  40. public function getReport($iObjectId)
  41. {
  42. $sQuery = $this->prepare("SELECT `count` as `count` FROM {$this->_sTable} WHERE `object_id` = ? LIMIT 1", $iObjectId);
  43. $aResult = $this->getRow($sQuery);
  44. if(empty($aResult) || !is_array($aResult))
  45. $aResult = array('count' => 0);
  46. return $aResult;
  47. }
  48. public function getReportsCountByStatus($iStatus)
  49. {
  50. $sQuery = $this->prepare("SELECT COUNT(*) FROM {$this->_sTableTrack} WHERE `status` = ?", $iStatus);
  51. return $this->getOne($sQuery);
  52. }
  53. public function putReport($iObjectId, $iAuthorId, $bUndo = false)
  54. {
  55. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  56. $bExists = (int)$this->getOne($sQuery) != 0;
  57. if(!$bExists && $bUndo)
  58. return false;
  59. if(!$bExists)
  60. $sQuery = $this->prepare("INSERT INTO `{$this->_sTable}` SET `object_id` = ?, `count` = '1'", $iObjectId);
  61. else
  62. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` " . ($bUndo ? "-" : "+") . " 1 WHERE `object_id` = ?", $iObjectId);
  63. if((int)$this->query($sQuery) == 0)
  64. return false;
  65. if($bUndo)
  66. return $this->_deleteTrack($iObjectId, $iAuthorId);
  67. return true;
  68. }
  69. public function clearReports($iObjectId)
  70. {
  71. $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `object_id` = ?", $iObjectId);
  72. $this->query($sQuery);
  73. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ?", $iObjectId);
  74. $this->query($sQuery);
  75. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = 0 WHERE `{$this->_sTriggerFieldId}` = ?", $iObjectId);
  76. $this->query($sQuery);
  77. }
  78. protected function _deleteTrack($iObjectId, $iAuthorId)
  79. {
  80. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  81. $iId = (int)$this->getOne($sQuery);
  82. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `id` = ? LIMIT 1", $iId);
  83. if((int)$this->query($sQuery) > 0)
  84. return $iId;
  85. return false;
  86. }
  87. public function changeStatusReport($iId, $iStatus, $iProfileId)
  88. {
  89. if ($iStatus != BX_DOL_REPORT_STASUS_IN_PROCESS)
  90. $sQuery = $this->prepare("UPDATE `{$this->_sTableTrack}` SET `status` = ? WHERE `id` = ?", $iStatus, $iId);
  91. else
  92. $sQuery = $this->prepare("UPDATE `{$this->_sTableTrack}` SET `status` = ?, `checked_by` = ? WHERE `id` = ?", $iStatus, $iProfileId, $iId);
  93. $this->query($sQuery);
  94. }
  95. }
  96. /** @} */