BxDolFavoriteQuery.php 5.6 KB

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