BxDolFavoriteQuery.php 6.5 KB

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