Subscription.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Subscription;
  10. use ForkBB\Models\Model;
  11. use ForkBB\Models\DataModel;
  12. use ForkBB\Models\Forum\Forum;
  13. use ForkBB\Models\Topic\Topic;
  14. use ForkBB\Models\User\User;
  15. use PDO;
  16. use InvalidArgumentException;
  17. class Subscription extends Model
  18. {
  19. /**
  20. * Ключ модели для контейнера
  21. * @var string
  22. */
  23. protected $cKey = 'Subscription';
  24. /**
  25. * @var array
  26. */
  27. protected $forums;
  28. /**
  29. * @var array
  30. */
  31. protected $topics;
  32. /**
  33. * @var array
  34. */
  35. protected $users;
  36. /**
  37. * Проверяет список моделей на форумы/темы
  38. * Заполняет forums, topics и users
  39. */
  40. protected function check(array $models, bool $mayBeUsers = false): void
  41. {
  42. $this->forums = [];
  43. $this->topics = [];
  44. $this->users = [];
  45. if (empty($models)) {
  46. if ($mayBeUsers) {
  47. throw new InvalidArgumentException('Expected at least one Forum, Topic or User');
  48. } else {
  49. throw new InvalidArgumentException('Expected at least one Forum or Topic');
  50. }
  51. }
  52. foreach ($models as $model) {
  53. if (
  54. $mayBeUsers
  55. && $model instanceof User
  56. ) {
  57. $this->users[$model->id] = $model->id;
  58. } elseif ($model instanceof Forum) {
  59. $this->forums[$model->id] = $model->id;
  60. $mayBeUsers = false;
  61. } elseif ($model instanceof Topic) {
  62. $this->topics[$model->id] = $model->id;
  63. $mayBeUsers = false;
  64. } else {
  65. throw new InvalidArgumentException('Expected only Forum or Topic');
  66. }
  67. }
  68. }
  69. /**
  70. * Подписывает юзера на форум(ы)/тему(ы)
  71. */
  72. public function subscribe(User $user, DataModel ...$models): bool
  73. {
  74. if (
  75. $user->isGuest
  76. || $user->isUnverified
  77. ) {
  78. return false;
  79. }
  80. $this->check($models);
  81. $vars = [
  82. ':uid' => $user->id,
  83. ];
  84. if (! empty($this->forums)) {
  85. $query = 'INSERT INTO ::forum_subscriptions (user_id, forum_id)
  86. SELECT ?i:uid, ?i:id
  87. FROM ::groups
  88. WHERE NOT EXISTS (
  89. SELECT 1
  90. FROM ::forum_subscriptions
  91. WHERE user_id=?i:uid AND forum_id=?i:id
  92. )
  93. LIMIT 1';
  94. foreach ($this->forums as $id) {
  95. $vars[':id'] = $id;
  96. $this->c->DB->exec($query, $vars);
  97. }
  98. }
  99. if (! empty($this->topics)) {
  100. $query = 'INSERT INTO ::topic_subscriptions (user_id, topic_id)
  101. SELECT ?i:uid, ?i:id
  102. FROM ::groups
  103. WHERE NOT EXISTS (
  104. SELECT 1
  105. FROM ::topic_subscriptions
  106. WHERE user_id=?i:uid AND topic_id=?i:id
  107. )
  108. LIMIT 1';
  109. foreach ($this->topics as $id) {
  110. $vars[':id'] = $id;
  111. $this->c->DB->exec($query, $vars);
  112. }
  113. }
  114. return true;
  115. }
  116. /**
  117. * Отписывает юзеров от форумов/топиков
  118. * Убирает подписки с удаляемых форумов/топиков
  119. * Убирает подписки с удаляемых юзеров
  120. */
  121. public function unsubscribe(DataModel ...$models): bool
  122. {
  123. $where = [];
  124. $vars = [];
  125. $this->check($models, true);
  126. if (! empty($this->users)) {
  127. if (1 === \count($this->users)) {
  128. $where[':uid'] = 'user_id=?i:uid';
  129. $vars[':uid'] = \reset($this->users);
  130. } else {
  131. $where[':uid'] = 'user_id IN(?ai:uid)';
  132. $vars[':uid'] = $this->users;
  133. }
  134. }
  135. $all = empty($this->forums) && empty($this->topics);
  136. if ($all || ! empty($this->forums)) {
  137. if (! empty($this->forums)) {
  138. if (1 === \count($this->forums)) {
  139. $where[':id'] = 'forum_id=?i:id';
  140. $vars[':id'] = \reset($this->forums);
  141. } else {
  142. $where[':id'] = 'forum_id IN(?ai:id)';
  143. $vars[':id'] = $this->forums;
  144. }
  145. }
  146. $query = 'DELETE
  147. FROM ::forum_subscriptions
  148. WHERE ' . \implode(' AND ', $where);
  149. $this->c->DB->exec($query, $vars);
  150. }
  151. unset($where[':id'], $vars[':id']);
  152. if ($all || ! empty($this->topics)) {
  153. if (! empty($this->topics)) {
  154. if (1 === \count($this->topics)) {
  155. $where[':id'] = 'topic_id=?i:id';
  156. $vars[':id'] = \reset($this->topics);
  157. } else {
  158. $where[':id'] = 'topic_id IN(?ai:id)';
  159. $vars[':id'] = $this->topics;
  160. }
  161. }
  162. $query = 'DELETE
  163. FROM ::topic_subscriptions
  164. WHERE ' . \implode(' AND ', $where);
  165. $this->c->DB->exec($query, $vars);
  166. }
  167. return true;
  168. }
  169. const FORUMS_DATA = 1;
  170. const TOPICS_DATA = 2;
  171. const ALL_DATA = 3;
  172. /**
  173. * Возвращает информацию по подпискам
  174. */
  175. public function info(DataModel $model, int $type = self::ALL_DATA): array
  176. {
  177. $result = [];
  178. if ($model instanceof User) {
  179. $vars = [
  180. ':uid' => $model->id,
  181. ];
  182. if (self::FORUMS_DATA & $type) {
  183. if (
  184. 1 !== $this->c->config->b_forum_subscriptions
  185. || $model->isGuest
  186. ) {
  187. $result[self::FORUMS_DATA] = null;
  188. } else {
  189. $query = 'SELECT forum_id
  190. FROM ::forum_subscriptions
  191. WHERE user_id=?i:uid';
  192. $result[self::FORUMS_DATA] = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
  193. }
  194. }
  195. if (self::TOPICS_DATA & $type) {
  196. if (
  197. 1 !== $this->c->config->b_topic_subscriptions
  198. || $model->isGuest
  199. ) {
  200. $result[self::TOPICS_DATA] = null;
  201. } else {
  202. $query = 'SELECT topic_id
  203. FROM ::topic_subscriptions
  204. WHERE user_id=?i:uid';
  205. $result[self::TOPICS_DATA] = $this->c->DB->query($query, $vars)->fetchAll(PDO::FETCH_COLUMN);
  206. }
  207. }
  208. } else {
  209. throw new InvalidArgumentException('Expected only User');
  210. }
  211. return $result;
  212. }
  213. }