View file app/Controllers/Forum/ActiveController.php

File size: 2.71Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers\Forum;
  6.  
  7. use App\Classes\Validator;
  8. use App\Controllers\BaseController;
  9. use App\Models\Post;
  10. use App\Models\Topic;
  11. use App\Models\User;
  12. use Exception;
  13. use Illuminate\Http\Request;
  14.  
  15. class ActiveController extends BaseController
  16. {
  17. /**
  18. * @var User
  19. */
  20. public $user;
  21.  
  22. /**
  23. * Конструктор
  24. *
  25. * @param Request $request
  26. */
  27. public function __construct(Request $request)
  28. {
  29. parent::__construct();
  30.  
  31. $login = $request->input('user', getUser('login'));
  32. $this->user = getUserByLogin($login);
  33.  
  34. if (! $this->user) {
  35. abort(404, __('validator.user'));
  36. }
  37. }
  38.  
  39. /**
  40. * Вывод тем
  41. *
  42. * @return string
  43. */
  44. public function topics(): string
  45. {
  46. $user = $this->user;
  47.  
  48. $topics = Topic::query()
  49. ->where('user_id', $user->id)
  50. ->orderByDesc('updated_at')
  51. ->with('forum', 'user', 'lastPost.user')
  52. ->paginate(setting('forumtem'))
  53. ->appends(['user' => $user->login]);
  54.  
  55. return view('forums/active_topics', compact('topics', 'user'));
  56. }
  57.  
  58. /**
  59. * Вывод сообшений
  60. *
  61. * @return string
  62. */
  63. public function posts(): string
  64. {
  65. $user = $this->user;
  66.  
  67. $posts = Post::query()
  68. ->where('user_id', $user->id)
  69. ->orderByDesc('created_at')
  70. ->with('topic', 'user')
  71. ->paginate(setting('forumpost'))
  72. ->appends(['user' => $user->login]);
  73.  
  74. return view('forums/active_posts', compact('posts', 'user'));
  75. }
  76.  
  77. /**
  78. * Удаление сообщений
  79. *
  80. * @param Request $request
  81. * @param Validator $validator
  82. *
  83. * @return string
  84. * @throws Exception
  85. */
  86. public function delete(Request $request, Validator $validator): string
  87. {
  88. if (! $request->ajax()) {
  89. redirect('/');
  90. }
  91.  
  92. if (! isAdmin()) {
  93. abort(403, __('forums.posts_deleted_moderators'));
  94. }
  95.  
  96. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'));
  97.  
  98. $post = Post::query()
  99. ->where('id', int($request->input('tid')))
  100. ->with('topic.forum')
  101. ->first();
  102.  
  103. $validator->true($post, __('forums.post_not_exist'));
  104.  
  105. if ($validator->isValid()) {
  106. $post->delete();
  107. $post->topic->decrement('count_posts');
  108. $post->topic->forum->decrement('count_posts');
  109.  
  110. return json_encode(['status' => 'success']);
  111. }
  112.  
  113. return json_encode(['status' => 'error', 'message' => current($validator->getErrors())]);
  114. }
  115. }