BxDolViewQuery.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolDb');
  10. /**
  11. * @see BxDolView
  12. */
  13. class BxDolViewQuery extends BxDolDb
  14. {
  15. protected $_oModule;
  16. protected $_sTableTrack;
  17. protected $_sTriggerTable;
  18. protected $_sTriggerFieldId;
  19. protected $_sTriggerFieldCount;
  20. protected $_iPeriod;
  21. public function __construct(&$oModule)
  22. {
  23. parent::__construct();
  24. $this->_oModule = $oModule;
  25. $aSystem = $this->_oModule->getSystemInfo();
  26. $this->_sTableTrack = $aSystem['table_track'];
  27. $this->_sTriggerTable = $aSystem['trigger_table'];
  28. $this->_sTriggerFieldId = $aSystem['trigger_field_id'];
  29. $this->_sTriggerFieldCount = $aSystem['trigger_field_count'];
  30. $this->_iPeriod = (int)$aSystem['period'];
  31. }
  32. public function doView($iObjectId, $iAuthorId, $sAuthorIp)
  33. {
  34. $iAuthorNip = ip2long($sAuthorIp);
  35. if($iAuthorId)
  36. $sWhere = $this->prepare(" AND `viewer_id` = ? ", $iAuthorId);
  37. else
  38. $sWhere = $this->prepare(" AND `viewer_id` = '0' AND `viewer_nip` = ? ", $iAuthorNip);
  39. $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? $sWhere", $iObjectId);
  40. $iDate = (int)$this->getOne($sQuery);
  41. $iDateNow = time();
  42. if(!$iDate) {
  43. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET `object_id` = ?, `viewer_id` = ?, `viewer_nip` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, $iDateNow);
  44. return (int)$this->query($sQuery) > 0;
  45. }
  46. if(($iDateNow - $iDate) > $this->_iPeriod) {
  47. $sQuery = $this->prepare("UPDATE `{$this->_sTableTrack}` SET `date` = ? WHERE `object_id` = ? AND `viewer_id` = ? AND `viewer_nip` = ?", $iDateNow, $iObjectId, $iAuthorId, $iAuthorNip);
  48. return (int)$this->query($sQuery) > 0;
  49. }
  50. }
  51. function deleteObjectViews($iObjectId)
  52. {
  53. $sQuery = $this->prepare("DELETE FROM {$this->_sTableTrack} WHERE `object_id` = ?", $iObjectId);
  54. $this->query ($sQuery);
  55. $this->query ("OPTIMIZE TABLE {$this->_sTableTrack}");
  56. }
  57. public function updateTriggerTable($iObjectId) {
  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. /** @} */