Просмотр файла app/Controllers/IgnoreController.php

Размер файла: 4.59Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\Ignore;
  9. use App\Models\User;
  10. use Illuminate\Http\Request;
  11.  
  12. class IgnoreController extends BaseController
  13. {
  14. /**
  15. * Конструктор
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20.  
  21. if (! getUser()) {
  22. abort(403, __('main.not_authorized'));
  23. }
  24. }
  25.  
  26. /**
  27. * Главная страница
  28. *
  29. * @param Request $request
  30. * @param Validator $validator
  31. *
  32. * @return string
  33. */
  34. public function index(Request $request, Validator $validator): string
  35. {
  36. $login = $request->input('user');
  37.  
  38. if ($request->isMethod('post')) {
  39. $page = int($request->input('page', 1));
  40.  
  41. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'));
  42.  
  43. $user = getUserByLogin($login);
  44. $validator->notEmpty($user, ['user' => __('validator.user')]);
  45.  
  46. if ($user) {
  47. $validator->notEqual($user->login, getUser('login'), ['user' => __('ignores.forbidden_yourself')]);
  48.  
  49. $totalIgnore = Ignore::query()->where('user_id', getUser('id'))->count();
  50. $validator->lte($totalIgnore, setting('limitignore'), __('ignores.ignore_full', ['max' => setting('limitignore')]));
  51.  
  52. $validator->false(getUser()->isIgnore($user), ['user' => __('ignores.already_ignore')]);
  53. $validator->notIn($user->level, User::ADMIN_GROUPS, ['user' => __('ignores.forbidden_admins')]);
  54. }
  55.  
  56. if ($validator->isValid()) {
  57. Ignore::query()->create([
  58. 'user_id' => getUser('id'),
  59. 'ignore_id' => $user->id,
  60. 'created_at' => SITETIME,
  61. ]);
  62.  
  63. if (! $user->isIgnore(getUser())) {
  64. $text = textNotice('ignore', ['login' => getUser('login')]);
  65. $user->sendMessage(null, $text);
  66. }
  67.  
  68. setFlash('success', __('ignores.success_added'));
  69. redirect('/ignores?page=' . $page);
  70. } else {
  71. setInput($request->all());
  72. setFlash('danger', $validator->getErrors());
  73. }
  74. }
  75.  
  76. $ignores = Ignore::query()
  77. ->where('user_id', getUser('id'))
  78. ->orderByDesc('created_at')
  79. ->with('ignoring')
  80. ->paginate(setting('ignorlist'));
  81.  
  82. return view('ignores/index', compact('ignores', 'login'));
  83. }
  84.  
  85. /**
  86. * Заметка для пользователя
  87. *
  88. * @param int $id
  89. * @param Request $request
  90. * @param Validator $validator
  91. *
  92. * @return string
  93. */
  94. public function note(int $id, Request $request, Validator $validator): string
  95. {
  96. $ignore = Ignore::query()
  97. ->where('user_id', getUser('id'))
  98. ->where('id', $id)
  99. ->first();
  100.  
  101. if (! $ignore) {
  102. abort(404, __('main.record_not_found'));
  103. }
  104.  
  105. if ($request->isMethod('post')) {
  106. $msg = $request->input('msg');
  107.  
  108. $validator->equal($request->input('token'), $_SESSION['token'], ['msg' => __('validator.token')])
  109. ->length($msg, 0, 1000, ['msg' => __('users.note_to_big')]);
  110.  
  111. if ($validator->isValid()) {
  112. $ignore->update([
  113. 'text' => $msg,
  114. ]);
  115.  
  116. setFlash('success', __('users.note_saved_success'));
  117. redirect('/ignores');
  118. } else {
  119. setInput($request->all());
  120. setFlash('danger', $validator->getErrors());
  121. }
  122. }
  123.  
  124. return view('ignores/note', compact('ignore'));
  125. }
  126.  
  127. /**
  128. * Удаление контактов
  129. *
  130. * @param Request $request
  131. * @param Validator $validator
  132. */
  133. public function delete(Request $request, Validator $validator): void
  134. {
  135. $page = int($request->input('page', 1));
  136. $del = intar($request->input('del'));
  137.  
  138. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'))
  139. ->true($del, __('validator.deletion'));
  140.  
  141. if ($validator->isValid()) {
  142. Ignore::query()
  143. ->where('user_id', getUser('id'))
  144. ->whereIn('id', $del)
  145. ->delete();
  146.  
  147. setFlash('success', __('main.records_deleted_success'));
  148. } else {
  149. setFlash('danger', $validator->getErrors());
  150. }
  151.  
  152. redirect('/ignores?page=' . $page);
  153. }
  154. }