1
0

BxDolReportQuery.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. return $this->getAll("SELECT * FROM `{$this->_sTableTrack}` WHERE `object_id` = :object_id ORDER BY `date` DESC" . $sLimitClause, [
  38. 'object_id' => $iObjectId
  39. ]);
  40. }
  41. public function getReport($iObjectId)
  42. {
  43. $sQuery = $this->prepare("SELECT `count` as `count` FROM {$this->_sTable} WHERE `object_id` = ? LIMIT 1", $iObjectId);
  44. $aResult = $this->getRow($sQuery);
  45. if(empty($aResult) || !is_array($aResult))
  46. $aResult = array('count' => 0);
  47. return $aResult;
  48. }
  49. public function getReportsCountByStatus($iStatus)
  50. {
  51. $sQuery = $this->prepare("SELECT COUNT(*) FROM {$this->_sTableTrack} WHERE `status` = ?", $iStatus);
  52. return $this->getOne($sQuery);
  53. }
  54. public function putReport($iObjectId, $iAuthorId, $bUndo = false)
  55. {
  56. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  57. $bExists = (int)$this->getOne($sQuery) != 0;
  58. if(!$bExists && $bUndo)
  59. return false;
  60. if(!$bExists)
  61. $sQuery = $this->prepare("INSERT INTO `{$this->_sTable}` SET `object_id` = ?, `count` = '1'", $iObjectId);
  62. else
  63. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` " . ($bUndo ? "-" : "+") . " 1 WHERE `object_id` = ?", $iObjectId);
  64. if((int)$this->query($sQuery) == 0)
  65. return false;
  66. if($bUndo)
  67. return $this->_deleteTrack($iObjectId, $iAuthorId);
  68. return true;
  69. }
  70. public function clearReports($iObjectId)
  71. {
  72. $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `object_id` = ?", $iObjectId);
  73. $this->query($sQuery);
  74. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ?", $iObjectId);
  75. $this->query($sQuery);
  76. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = 0 WHERE `{$this->_sTriggerFieldId}` = ?", $iObjectId);
  77. $this->query($sQuery);
  78. }
  79. protected function _deleteTrack($iObjectId, $iAuthorId)
  80. {
  81. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  82. $iId = (int)$this->getOne($sQuery);
  83. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `id` = ? LIMIT 1", $iId);
  84. if((int)$this->query($sQuery) > 0)
  85. return $iId;
  86. return false;
  87. }
  88. public function changeStatusReport($iId, $iStatus, $iProfileId)
  89. {
  90. if ($iStatus != BX_DOL_REPORT_STASUS_IN_PROCESS)
  91. $sQuery = $this->prepare("UPDATE `{$this->_sTableTrack}` SET `status` = ? WHERE `id` = ?", $iStatus, $iId);
  92. else
  93. $sQuery = $this->prepare("UPDATE `{$this->_sTableTrack}` SET `status` = ?, `checked_by` = ? WHERE `id` = ?", $iStatus, $iProfileId, $iId);
  94. $this->query($sQuery);
  95. }
  96. }
  97. /** @} */