BxDolReportQuery.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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)
  35. {
  36. $sQuery = $this->prepare("SELECT `author_id`, `type`, `text` FROM `{$this->_sTableTrack}` WHERE `object_id`=? ORDER BY `date` DESC", $iObjectId);
  37. return $this->getAll($sQuery);
  38. }
  39. public function getReport($iObjectId)
  40. {
  41. $sQuery = $this->prepare("SELECT `count` as `count` FROM {$this->_sTable} WHERE `object_id` = ? LIMIT 1", $iObjectId);
  42. $aResult = $this->getRow($sQuery);
  43. if(empty($aResult) || !is_array($aResult))
  44. $aResult = array('count' => 0);
  45. return $aResult;
  46. }
  47. public function putReport($iObjectId)
  48. {
  49. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  50. $bExists = (int)$this->getOne($sQuery) != 0;
  51. if(!$bExists)
  52. $sQuery = $this->prepare("INSERT INTO `{$this->_sTable}` SET `object_id` = ?, `count` = '1'", $iObjectId);
  53. else
  54. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` + 1 WHERE `object_id` = ?", $iObjectId);
  55. return (int)$this->query($sQuery) != 0;
  56. }
  57. }
  58. /** @} */