PBlock.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\PM;
  10. use ForkBB\Core\Container;
  11. use ForkBB\Models\Model;
  12. use ForkBB\Models\PM\Cnst;
  13. use ForkBB\Models\User\User;
  14. use InvalidArgumentException;
  15. use RuntimeException;
  16. class PBlock extends Model
  17. {
  18. /**
  19. * Ключ модели для контейнера
  20. * @var string
  21. */
  22. protected $cKey = 'PBlock';
  23. /**
  24. * @var array
  25. */
  26. protected $repository;
  27. /**
  28. * Текущий пользователь установленный в методе init()
  29. * @var User
  30. */
  31. protected $user;
  32. public function __construct(Container $container)
  33. {
  34. parent::__construct($container);
  35. $this->init($container->user);
  36. }
  37. public function init(User $user): void
  38. {
  39. $this->setAttrs([]);
  40. $this->repository = [];
  41. $this->user = $user;
  42. $vars = [
  43. ':id' => $user->id,
  44. ];
  45. $query = 'SELECT pb.bl_first_id, pb.bl_second_id
  46. FROM ::pm_block AS pb
  47. WHERE pb.bl_first_id=?i:id OR pb.bl_second_id=?i:id';
  48. $stmt = $this->c->DB->query($query, $vars);
  49. while ($row = $stmt->fetch()) {
  50. if ($row['bl_first_id'] === $user->id) {
  51. $this->repository[$user->id][$row['bl_second_id']] = $row['bl_second_id'];
  52. } else {
  53. $this->repository[$row['bl_first_id']] = true;
  54. }
  55. }
  56. }
  57. /**
  58. * Проверяет: $user в блоке у $this->user
  59. */
  60. public function isBlock(User $user): bool
  61. {
  62. return isset($this->repository[$this->user->id][$user->id]);
  63. }
  64. /**
  65. * Проверяет: $this->user в блоке у $user
  66. */
  67. public function inBlock(User $user): bool
  68. {
  69. return isset($this->repository[$user->id]);
  70. }
  71. /**
  72. * Установка блока: $this->user -> $user
  73. */
  74. public function add(User $user): bool
  75. {
  76. $vars = [
  77. ':first_id' => $this->user->id,
  78. ':second_id' => $user->id,
  79. ];
  80. $query = 'INSERT INTO ::pm_block (bl_first_id, bl_second_id) VALUES (:first_id, :second_id)';
  81. return false !== $this->c->DB->exec($query, $vars);
  82. }
  83. /**
  84. * Снятие блока: $this->user -> $user
  85. */
  86. public function remove(User $user): bool
  87. {
  88. $vars = [
  89. ':first_id' => $this->user->id,
  90. ':second_id' => $user->id,
  91. ];
  92. $query = 'DELETE FROM ::pm_block WHERE bl_first_id=:first_id AND bl_second_id=:second_id';
  93. return false !== $this->c->DB->exec($query, $vars);
  94. }
  95. /**
  96. * Возвращает массив заблокированных пользователей
  97. */
  98. protected function getlist(): array
  99. {
  100. if (empty($this->repository[$this->user->id])) {
  101. return [];
  102. }
  103. $list = $this->c->users->loadByIds($this->repository[$this->user->id]);
  104. foreach ($list as &$user) {
  105. if ($user instanceof User) {
  106. $user->linkPMUnblock = $this->c->Router->link(
  107. 'PMAction',
  108. [
  109. 'action' => Cnst::ACTION_BLOCK,
  110. 'more1' => $user->id,
  111. ]
  112. );
  113. }
  114. }
  115. unset($user);
  116. return $list;
  117. }
  118. /**
  119. * Возращает статус возможности блокировки пользователя
  120. */
  121. public function canBlock(User $user): bool
  122. {
  123. return $this->user->id !== $user->id
  124. && ! $user->isAdmin
  125. && ! $user->isGuest;
  126. }
  127. }