BxDolViewQuery.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. bx_import('BxDolDb');
  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::BxDolDb();
  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. function deleteObjectViews($iObjectId)
  51. {
  52. $sQuery = $this->prepare("DELETE FROM {$this->_sTableTrack} WHERE `object_id` = ?", $iObjectId);
  53. $this->query ($sQuery);
  54. $this->query ("OPTIMIZE TABLE {$this->_sTableTrack}");
  55. }
  56. public function updateTriggerTable($iObjectId) {
  57. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + 1 WHERE `{$this->_sTriggerFieldId}` = ?", $iObjectId);
  58. return (int)$this->query($sQuery) > 0;
  59. }
  60. }