BxDolViewQuery.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. public function __construct(&$oModule)
  16. {
  17. parent::__construct($oModule);
  18. $this->_sTableTrackFieldAuthor = 'viewer_id';
  19. $aSystem = $this->_oModule->getSystemInfo();
  20. $this->_iPeriod = (int)$aSystem['period'];
  21. }
  22. public function isPerformed($iObjectId, $iAuthorId)
  23. {
  24. $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `viewer_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  25. return (int)$this->getOne($sQuery) > 0;
  26. }
  27. public function getPerformedBy($iObjectId)
  28. {
  29. $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", $iObjectId);
  30. return $this->getAll($sQuery);
  31. }
  32. public function getView($iObjectId)
  33. {
  34. return array('count' => $this->getObjectCount($iObjectId));
  35. }
  36. public function doView($iObjectId, $iAuthorId, $sAuthorIp)
  37. {
  38. $iAuthorNip = ip2long($sAuthorIp);
  39. $aBindings = array(
  40. 'object_id' => $iObjectId
  41. );
  42. if($iAuthorId) {
  43. $aBindings['viewer_id'] = $iAuthorId;
  44. $sWhere = " AND `viewer_id` = :viewer_id ";
  45. }
  46. else {
  47. $aBindings['viewer_nip'] = $iAuthorNip;
  48. $sWhere = " AND `viewer_id` = '0' AND `viewer_nip` = :viewer_nip ";
  49. }
  50. $iDate = (int)$this->getOne("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = :object_id " . $sWhere, $aBindings);
  51. $iDateNow = time();
  52. if(!$iDate || ($iDateNow - $iDate) > $this->_iPeriod) {
  53. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET `object_id` = ?, `viewer_id` = ?, `viewer_nip` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, $iDateNow);
  54. return (int)$this->query($sQuery) > 0;
  55. }
  56. }
  57. public function updateTriggerTable($iObjectId)
  58. {
  59. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + 1 WHERE `{$this->_sTriggerFieldId}` = ?", $iObjectId);
  60. return (int)$this->query($sQuery) > 0;
  61. }
  62. }
  63. /** @} */