BxDolViewQuery.php 2.6 KB

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