BxDolReportQuery.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 isPerformed($iObjectId, $iAuthorId)
  28. {
  29. /*
  30. * 'false' is returned everytime to allow multiple reports for everybody.
  31. */
  32. return false;
  33. }
  34. public function getPerformedBy($iObjectId, $iStart = 0, $iPerPage = 0)
  35. {
  36. $sLimitClause = "";
  37. if(!empty($iPerPage))
  38. $sLimitClause = $this->prepareAsString(" LIMIT ?, ?", $iStart, $iPerPage);
  39. $sQuery = $this->prepare("SELECT `author_id`, `type`, `text` FROM `{$this->_sTableTrack}` WHERE `object_id`=? ORDER BY `date` DESC" . $sLimitClause, $iObjectId);
  40. return $this->getAll($sQuery);
  41. }
  42. public function getReport($iObjectId)
  43. {
  44. $sQuery = $this->prepare("SELECT `count` as `count` FROM {$this->_sTable} WHERE `object_id` = ? LIMIT 1", $iObjectId);
  45. $aResult = $this->getRow($sQuery);
  46. if(empty($aResult) || !is_array($aResult))
  47. $aResult = array('count' => 0);
  48. return $aResult;
  49. }
  50. public function putReport($iObjectId)
  51. {
  52. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  53. $bExists = (int)$this->getOne($sQuery) != 0;
  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` + 1 WHERE `object_id` = ?", $iObjectId);
  58. return (int)$this->query($sQuery) != 0;
  59. }
  60. public function clearReports($iObjectId)
  61. {
  62. $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `object_id` = ?", $iObjectId);
  63. $this->query($sQuery);
  64. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ?", $iObjectId);
  65. $this->query($sQuery);
  66. }
  67. }
  68. /** @} */