BxDolReportQuery.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 putReport($iObjectId, $iAuthorId, $bUndo = false)
  49. {
  50. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  51. $bExists = (int)$this->getOne($sQuery) != 0;
  52. if(!$bExists && $bUndo)
  53. return false;
  54. if(!$bExists)
  55. $sQuery = $this->prepare("INSERT INTO `{$this->_sTable}` SET `object_id` = ?, `count` = '1'", $iObjectId);
  56. else
  57. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` " . ($bUndo ? "-" : "+") . " 1 WHERE `object_id` = ?", $iObjectId);
  58. if((int)$this->query($sQuery) == 0)
  59. return false;
  60. if($bUndo)
  61. return $this->_deleteTrack($iObjectId, $iAuthorId);
  62. return true;
  63. }
  64. public function clearReports($iObjectId)
  65. {
  66. $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `object_id` = ?", $iObjectId);
  67. $this->query($sQuery);
  68. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ?", $iObjectId);
  69. $this->query($sQuery);
  70. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = 0 WHERE `{$this->_sTriggerFieldId}` = ?", $iObjectId);
  71. $this->query($sQuery);
  72. }
  73. protected function _deleteTrack($iObjectId, $iAuthorId)
  74. {
  75. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  76. $iId = (int)$this->getOne($sQuery);
  77. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `id` = ? LIMIT 1", $iId);
  78. if((int)$this->query($sQuery) > 0)
  79. return $iId;
  80. return false;
  81. }
  82. public function changeStatusReport($iId, $iStatus, $iProfileId)
  83. {
  84. if ($iStatus != BX_DOL_REPORT_STASUS_IN_PROCESS)
  85. $sQuery = $this->prepare("UPDATE `{$this->_sTableTrack}` SET `status` = ? WHERE `id` = ?", $iStatus, $iId);
  86. else
  87. $sQuery = $this->prepare("UPDATE `{$this->_sTableTrack}` SET `status` = ?, `checked_by` = ? WHERE `id` = ?", $iStatus, $iProfileId, $iId);
  88. $this->query($sQuery);
  89. }
  90. }
  91. /** @} */