BxDolViewQuery.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. /**
  10. * @see BxDolView
  11. */
  12. class BxDolViewQuery extends BxDolDb
  13. {
  14. protected $_oModule;
  15. protected $_sTableTrack;
  16. protected $_sTriggerTable;
  17. protected $_sTriggerFieldId;
  18. protected $_sTriggerFieldCount;
  19. protected $_iPeriod;
  20. public function __construct(&$oModule)
  21. {
  22. parent::__construct();
  23. $this->_oModule = $oModule;
  24. $aSystem = $this->_oModule->getSystemInfo();
  25. $this->_sTableTrack = $aSystem['table_track'];
  26. $this->_sTriggerTable = $aSystem['trigger_table'];
  27. $this->_sTriggerFieldId = $aSystem['trigger_field_id'];
  28. $this->_sTriggerFieldCount = $aSystem['trigger_field_count'];
  29. $this->_iPeriod = (int)$aSystem['period'];
  30. }
  31. public function doView($iObjectId, $iAuthorId, $sAuthorIp)
  32. {
  33. $iAuthorNip = ip2long($sAuthorIp);
  34. if($iAuthorId)
  35. $sWhere = $this->prepare(" AND `viewer_id` = ? ", $iAuthorId);
  36. else
  37. $sWhere = $this->prepare(" AND `viewer_id` = '0' AND `viewer_nip` = ? ", $iAuthorNip);
  38. $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? $sWhere", $iObjectId);
  39. $iDate = (int)$this->getOne($sQuery);
  40. $iDateNow = time();
  41. if(!$iDate) {
  42. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET `object_id` = ?, `viewer_id` = ?, `viewer_nip` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, $iDateNow);
  43. return (int)$this->query($sQuery) > 0;
  44. }
  45. if(($iDateNow - $iDate) > $this->_iPeriod) {
  46. $sQuery = $this->prepare("UPDATE `{$this->_sTableTrack}` SET `date` = ? WHERE `object_id` = ? AND `viewer_id` = ? AND `viewer_nip` = ?", $iDateNow, $iObjectId, $iAuthorId, $iAuthorNip);
  47. return (int)$this->query($sQuery) > 0;
  48. }
  49. }
  50. public function deleteObjectViews($iObjectId)
  51. {
  52. $sQuery = $this->prepare("DELETE FROM {$this->_sTableTrack} WHERE `object_id` = ?", $iObjectId);
  53. if ($this->query ($sQuery))
  54. $this->query ("OPTIMIZE TABLE {$this->_sTableTrack}");
  55. }
  56. public function updateTriggerTable($iObjectId)
  57. {
  58. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + 1 WHERE `{$this->_sTriggerFieldId}` = ?", $iObjectId);
  59. return (int)$this->query($sQuery) > 0;
  60. }
  61. }
  62. /** @} */