BxDolFavoriteQuery.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA 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. return array('count' => $this->getObjectCount($iObjectId));
  32. }
  33. public function doFavorite($iObjectId, $iAuthorId)
  34. {
  35. $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  36. if((int)$this->getOne($sQuery) > 0)
  37. return true;
  38. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `date` = ?", $iObjectId, $iAuthorId, time());
  39. return (int)$this->query($sQuery) > 0;
  40. }
  41. public function undoFavorite($iObjectId, $iAuthorId)
  42. {
  43. $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  44. if((int)$this->getOne($sQuery) == 0)
  45. return true;
  46. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  47. return (int)$this->query($sQuery) > 0;
  48. }
  49. }
  50. /** @} */