BxDolViewQuery.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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) {
  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. if(($iDateNow - $iDate) > $this->_iPeriod) {
  57. $aBindings['date'] = $iDateNow;
  58. return (int)$this->query("UPDATE `{$this->_sTableTrack}` SET `date` = :date WHERE `object_id` = :object_id " . $sWhere, $aBindings) > 0;
  59. }
  60. }
  61. public function updateTriggerTable($iObjectId)
  62. {
  63. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + 1 WHERE `{$this->_sTriggerFieldId}` = ?", $iObjectId);
  64. return (int)$this->query($sQuery) > 0;
  65. }
  66. }
  67. /** @} */