BxDolFavoriteQuery.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. protected $_sTableLists;
  15. public function __construct(&$oModule)
  16. {
  17. parent::__construct($oModule);
  18. $aSystem = $this->_oModule->getSystemInfo();
  19. $this->_sTableLists = isset($aSystem['table_lists']) ? $aSystem['table_lists'] : '';
  20. $this->_sMethodGetEntry = 'getFavorite';
  21. }
  22. public function isPerformed($iObjectId, $iAuthorId)
  23. {
  24. $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  25. return (int)$this->getOne($sQuery) > 0;
  26. }
  27. public function getPerformedBy($iObjectId, $iStart = 0, $iPerPage = 0)
  28. {
  29. $sLimitClause = "";
  30. if(!empty($iPerPage))
  31. $sLimitClause = $this->prepareAsString(" LIMIT ?, ?", $iStart, $iPerPage);
  32. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableTrack}` WHERE `object_id`=? ORDER BY `date` DESC" . $sLimitClause, $iObjectId);
  33. return $this->getAll($sQuery);
  34. }
  35. public function getFavorite($iObjectId)
  36. {
  37. return array('count' => $this->getObjectCount($iObjectId));
  38. }
  39. public function clearFavorite($iObjectId, $iAuthorId)
  40. {
  41. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ?", $iObjectId, $iAuthorId);
  42. return (int)$this->query($sQuery) > 0;
  43. }
  44. public function doFavorite($iObjectId, $iAuthorId, $iListId)
  45. {
  46. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `date` = ?, `list_id` = ?", $iObjectId, $iAuthorId, time(), $iListId);
  47. return (int)$this->query($sQuery) > 0;
  48. }
  49. public function getList($aParams)
  50. {
  51. $aMethodParams = array();
  52. switch($aParams['type']) {
  53. case 'object_and_author':
  54. $sMethod = 'getColumn';
  55. $aMethodParams[0] = $this->prepare("SELECT `list_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ?", $aParams['object_id'], $aParams['author_id']);
  56. break;
  57. case 'id':
  58. $sMethod = 'getRow';
  59. $aMethodParams[0] = $this->prepare("SELECT * FROM `{$this->_sTableLists}` WHERE `id` = ? ", $aParams['list_id']);
  60. break;
  61. case 'info':
  62. $sMethod = 'getRow';
  63. $aMethodParams[0] = $this->prepare("SELECT MAX(`t`.`date`) `updated`, COUNT(`t`.`object_id`) `count`, `l`.`title`, `l`.`date` `created`, `l`.`allow_view_favorite_list_to` FROM `{$this->_sTableTrack}` `t` LEFT JOIN `{$this->_sTableLists}` `l` ON `t`.`list_id` = `l`.`id` WHERE `t`.`list_id` = ? AND `t`.`author_id` = ?", $aParams['list_id'], $aParams['author_id']);
  64. break;
  65. case 'all':
  66. $sMethod = 'getAll';
  67. $sQ = "SELECT `id`, `title`, `allow_view_favorite_list_to` FROM `{$this->_sTableLists}` WHERE `author_id` = ? ORDER BY `date`";
  68. if (isset($aParams['need_default']) && (bool)$aParams['need_default']){
  69. $sQ = "SELECT 0 `id`, '" . _t('_sys_txt_default_favorite_list') . "' `title`, 3 `allow_view_favorite_list_to` UNION (" . $sQ . ")";
  70. }
  71. $aMethodParams[0] = $this->prepare($sQ, $aParams['author_id']);
  72. break;
  73. case 'active':
  74. $sMethod = 'getPairs';
  75. $aMethodParams[0] = "SELECT `l`.`id`, `l`.`title` FROM `{$this->_sTableLists}` `l` INNER JOIN `{$this->_sTableTrack}` `t` ON `t`.`list_id` = `l`.`id` WHERE `l`.`author_id` = :author GROUP BY `l`.`id`, `l`.`title` ORDER BY `l`.`date` DESC";
  76. $aMethodParams[1] = 'id';
  77. $aMethodParams[2] = 'title';
  78. $aBindings = array(
  79. 'author' => $aParams['author_id'],
  80. );
  81. if (isset($aParams['need_default']) && (bool)$aParams['need_default'])
  82. $aMethodParams[0] = "SELECT 0 `id`, '" . _t('_sys_txt_default_favorite_list') . "' `title` UNION (" . $aMethodParams[0] . ")";
  83. if (isset($aParams['limit'])){
  84. $aMethodParams[0] .= " LIMIT " . $aParams['start'] . ", " . $aParams['limit'];
  85. }
  86. $aMethodParams[3] = $aBindings;
  87. break;
  88. }
  89. return call_user_func_array(array($this, $sMethod), $aMethodParams);
  90. }
  91. public function addList($iAuthorId, $sTitle, $sVisibility)
  92. {
  93. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableLists}` SET `author_id` = ?, `title` = ?, `allow_view_favorite_list_to` = ?, `date` = ?", $iAuthorId, $sTitle, $sVisibility, time());
  94. $this->query($sQuery);
  95. return $this->lastId();
  96. }
  97. public function editList($iListId, $sTitle, $sVisibility)
  98. {
  99. $sQuery = $this->prepare("UPDATE `{$this->_sTableLists}` SET `title` = ?, `allow_view_favorite_list_to` = ? WHERE `id` = ?", $sTitle, $sVisibility, $iListId);
  100. $this->query($sQuery);
  101. return $this->lastId();
  102. }
  103. public function deleteList($iListId)
  104. {
  105. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableLists}` WHERE `id` = ?", $iListId);
  106. $this->query($sQuery);
  107. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `list_id` = ?", $iListId);
  108. $this->query($sQuery);
  109. }
  110. }
  111. /** @} */