BxDolViewQuery.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 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 doView($iObjectId, $iAuthorId, $sAuthorIp)
  22. {
  23. $iAuthorNip = ip2long($sAuthorIp);
  24. $aBindings = array(
  25. 'object_id' => $iObjectId
  26. );
  27. if($iAuthorId) {
  28. $aBindings['viewer_id'] = $iAuthorId;
  29. $sWhere = " AND `viewer_id` = :viewer_id ";
  30. }
  31. else {
  32. $aBindings['viewer_nip'] = $iAuthorNip;
  33. $sWhere = " AND `viewer_id` = '0' AND `viewer_nip` = :viewer_nip ";
  34. }
  35. $iDate = (int)$this->getOne("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = :object_id " . $sWhere, $aBindings);
  36. $iDateNow = time();
  37. if(!$iDate) {
  38. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET `object_id` = ?, `viewer_id` = ?, `viewer_nip` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, $iDateNow);
  39. return (int)$this->query($sQuery) > 0;
  40. }
  41. if(($iDateNow - $iDate) > $this->_iPeriod) {
  42. $sQuery = $this->prepare("UPDATE `{$this->_sTableTrack}` SET `date` = ? WHERE `object_id` = ? AND `viewer_id` = ? AND `viewer_nip` = ?", $iDateNow, $iObjectId, $iAuthorId, $iAuthorNip);
  43. return (int)$this->query($sQuery) > 0;
  44. }
  45. }
  46. public function updateTriggerTable($iObjectId)
  47. {
  48. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + 1 WHERE `{$this->_sTriggerFieldId}` = ?", $iObjectId);
  49. return (int)$this->query($sQuery) > 0;
  50. }
  51. }
  52. /** @} */