BxDolFavoriteQuery.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 BxDolFavorite
  11. */
  12. class BxDolFavoriteQuery extends BxDolObjectQuery
  13. {
  14. public function __construct(&$oModule)
  15. {
  16. parent::__construct($oModule);
  17. $this->_sMethodGetEntry = 'getFavorite';
  18. }
  19. public function isPerformed($iObjectId, $iAuthorId)
  20. {
  21. $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  22. return (int)$this->getOne($sQuery) > 0;
  23. }
  24. public function getPerformedBy($iObjectId)
  25. {
  26. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableTrack}` WHERE `object_id`=? ORDER BY `date` DESC", $iObjectId);
  27. return $this->getAll($sQuery);
  28. }
  29. public function getFavorite($iObjectId)
  30. {
  31. $sQuery = $this->prepare("SELECT `count` as `count` FROM {$this->_sTable} WHERE `object_id` = ? LIMIT 1", $iObjectId);
  32. $aResult = $this->getRow($sQuery);
  33. if(empty($aResult) || !is_array($aResult))
  34. $aResult = array('count' => 0);
  35. return $aResult;
  36. }
  37. public function doFavorite($iObjectId, $iAuthorId, $sAuthorIp)
  38. {
  39. $iAuthorNip = ip2long($sAuthorIp);
  40. $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  41. if((int)$this->getOne($sQuery) > 0)
  42. return true;
  43. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `author_nip` = ?, `date` = ?", $iObjectId, $iAuthorId, $iAuthorNip, time());
  44. if((int)$this->query($sQuery) == 0)
  45. return false;
  46. $sQuery = $this->prepare("SELECT `object_id` FROM `{$this->_sTable}` WHERE `object_id` = ? LIMIT 1", $iObjectId);
  47. if((int)$this->getOne($sQuery) == 0)
  48. $sQuery = $this->prepare("INSERT INTO `{$this->_sTable}` SET `object_id` = ?, `count` = '1'", $iObjectId);
  49. else
  50. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` + 1 WHERE `object_id` = ?", $iObjectId);
  51. return (int)$this->query($sQuery) > 0;
  52. }
  53. public function undoFavorite($iObjectId, $iAuthorId)
  54. {
  55. $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  56. if((int)$this->getOne($sQuery) == 0)
  57. return true;
  58. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  59. if((int)$this->query($sQuery) == 0)
  60. return false;
  61. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` - 1 WHERE `object_id` = ?", $iObjectId);
  62. return (int)$this->query($sQuery) > 0;
  63. }
  64. public function updateTriggerTable($iObjectId)
  65. {
  66. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + 1 WHERE `{$this->_sTriggerFieldId}` = ?", $iObjectId);
  67. return (int)$this->query($sQuery) > 0;
  68. }
  69. }
  70. /** @} */