1
0

BxDolViewQuery.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 BxDolView
  11. */
  12. class BxDolViewQuery extends BxDolObjectQuery
  13. {
  14. protected $_iPeriod;
  15. protected $_iPerPageDefault;
  16. public function __construct(&$oModule)
  17. {
  18. parent::__construct($oModule);
  19. $this->_sTableTrackFieldAuthor = 'viewer_id';
  20. $aSystem = $this->_oModule->getSystemInfo();
  21. $this->_iPeriod = (int)$aSystem['period'];
  22. $this->_iPerPageDefault = $aSystem['per_page_default'];
  23. }
  24. public function isPerformed($iObjectId, $iAuthorId)
  25. {
  26. $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `viewer_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  27. return (int)$this->getOne($sQuery) > 0;
  28. }
  29. public function getPerformedBy($iObjectId, $iStart = 0, $iPerPage = 0)
  30. {
  31. $sLimitClause = "";
  32. if(empty($iPerPage))
  33. $iPerPage = $this->_iPerPageDefault;
  34. if(!empty($iPerPage))
  35. $sLimitClause = $this->prepareAsString(" LIMIT ?, ?", $iStart, $iPerPage);
  36. $sQuery = $this->prepare("SELECT `viewer_id` AS `id`, COUNT(`viewer_id`) AS `count` FROM `{$this->_sTableTrack}` WHERE `object_id`=? GROUP BY `viewer_id` ORDER BY `date` DESC" . $sLimitClause, $iObjectId);
  37. return $this->getAll($sQuery);
  38. }
  39. public function getView($iObjectId)
  40. {
  41. return array('count' => $this->getObjectCount($iObjectId));
  42. }
  43. public function doView($iObjectId, $iAuthorId, $sAuthorIp)
  44. {
  45. $iAuthorNip = bx_get_ip_hash($sAuthorIp);
  46. $aBindings = array(
  47. 'object_id' => $iObjectId
  48. );
  49. if($iAuthorId) {
  50. $aBindings['viewer_id'] = $iAuthorId;
  51. $sWhere = " AND `viewer_id` = :viewer_id ";
  52. }
  53. else {
  54. $aBindings['viewer_nip'] = $iAuthorNip;
  55. $sWhere = " AND `viewer_id` = '0' AND `viewer_nip` = :viewer_nip ";
  56. }
  57. $iDate = (int)$this->getOne("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = :object_id " . $sWhere . " ORDER BY `date` DESC LIMIT 1", $aBindings);
  58. $iDateNow = time();
  59. if(!$iDate || ($iDateNow - $iDate) > $this->_iPeriod) {
  60. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET `object_id` = ?, `viewer_id` = ?, `viewer_nip` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, $iDateNow);
  61. return (int)$this->query($sQuery) > 0;
  62. }
  63. }
  64. public function updateTriggerTable($iObjectId)
  65. {
  66. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + 1 WHERE `{$this->_sTriggerFieldId}` = ?", $iObjectId);
  67. return (int)$this->query($sQuery) > 0;
  68. }
  69. }
  70. /** @} */