BxDolFavoriteQuery.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 DISTINCT `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 $this->getRow("SELECT COUNT(DISTINCT `author_id`) AS `count` FROM `{$this->_sTableTrack}` WHERE `object_id`=:object_id LIMIT 1", array(
  38. 'object_id' => $iObjectId
  39. ));
  40. }
  41. public function clearFavorite($iObjectId, $iAuthorId)
  42. {
  43. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ?", $iObjectId, $iAuthorId);
  44. return (int)$this->query($sQuery) > 0;
  45. }
  46. public function doFavorite($iObjectId, $iAuthorId, $iListId = false)
  47. {
  48. $sQuery = '';
  49. if ($iListId === false)
  50. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `date` = ?", $iObjectId, $iAuthorId, time());
  51. else
  52. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET `object_id` = ?, `author_id` = ?, `date` = ?, `list_id` = ?", $iObjectId, $iAuthorId, time(), $iListId);
  53. return (int)$this->query($sQuery) > 0;
  54. }
  55. public function undoFavorite($iObjectId, $iAuthorId)
  56. {
  57. $sQuery = $this->prepare("SELECT `date` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  58. if((int)$this->getOne($sQuery) == 0)
  59. return true;
  60. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ? LIMIT 1", $iObjectId, $iAuthorId);
  61. return (int)$this->query($sQuery) > 0;
  62. }
  63. public function getList($aParams)
  64. {
  65. $aMethodParams = array();
  66. switch($aParams['type']) {
  67. case 'object_and_author':
  68. $sMethod = 'getColumn';
  69. $aMethodParams[0] = $this->prepare("SELECT `list_id` FROM `{$this->_sTableTrack}` WHERE `object_id` = ? AND `author_id` = ?", $aParams['object_id'], $aParams['author_id']);
  70. break;
  71. case 'id':
  72. $sMethod = 'getRow';
  73. $aMethodParams[0] = $this->prepare("SELECT * FROM `{$this->_sTableLists}` WHERE `id` = ? ", $aParams['list_id']);
  74. break;
  75. case 'info':
  76. $sMethod = 'getRow';
  77. $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']);
  78. break;
  79. case 'all':
  80. $sMethod = 'getAll';
  81. $sQ = "SELECT `id`, `title`, `allow_view_favorite_list_to` FROM `{$this->_sTableLists}` WHERE `author_id` = ? ORDER BY `date`";
  82. if (isset($aParams['need_default']) && (bool)$aParams['need_default']){
  83. $sQ = "SELECT 0 `id`, '" . _t('_sys_txt_default_favorite_list') . "' `title`, 3 `allow_view_favorite_list_to` UNION (" . $sQ . ")";
  84. }
  85. $aMethodParams[0] = $this->prepare($sQ, $aParams['author_id']);
  86. break;
  87. case 'active':
  88. $sMethod = 'getPairs';
  89. $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";
  90. $aMethodParams[1] = 'id';
  91. $aMethodParams[2] = 'title';
  92. $aBindings = array(
  93. 'author' => $aParams['author_id'],
  94. );
  95. if (isset($aParams['need_default']) && (bool)$aParams['need_default'])
  96. $aMethodParams[0] = "SELECT 0 `id`, '" . _t('_sys_txt_default_favorite_list') . "' `title` UNION (" . $aMethodParams[0] . ")";
  97. if (isset($aParams['limit'])){
  98. $aMethodParams[0] .= " LIMIT " . $aParams['start'] . ", " . $aParams['limit'];
  99. }
  100. $aMethodParams[3] = $aBindings;
  101. break;
  102. }
  103. return call_user_func_array(array($this, $sMethod), $aMethodParams);
  104. }
  105. public function addList($iAuthorId, $sTitle, $sVisibility)
  106. {
  107. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableLists}` SET `author_id` = ?, `title` = ?, `allow_view_favorite_list_to` = ?, `date` = ?", $iAuthorId, $sTitle, $sVisibility, time());
  108. $this->query($sQuery);
  109. return $this->lastId();
  110. }
  111. public function editList($iListId, $sTitle, $sVisibility)
  112. {
  113. $sQuery = $this->prepare("UPDATE `{$this->_sTableLists}` SET `title` = ?, `allow_view_favorite_list_to` = ? WHERE `id` = ?", $sTitle, $sVisibility, $iListId);
  114. $this->query($sQuery);
  115. return $this->lastId();
  116. }
  117. public function deleteList($iListId)
  118. {
  119. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableLists}` WHERE `id` = ?", $iListId);
  120. $this->query($sQuery);
  121. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `list_id` = ?", $iListId);
  122. $this->query($sQuery);
  123. }
  124. }
  125. /** @} */