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

Размер файла: 3.37Kb
  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\Bookmark;
  10. use App\Models\Topic;
  11. use Exception;
  12. use Illuminate\Http\Request;
  13.  
  14. class BookmarkController extends BaseController
  15. {
  16. /**
  17. * Конструктор
  18. */
  19. public function __construct()
  20. {
  21. parent::__construct();
  22.  
  23. if (! getUser()) {
  24. abort(403, __('main.not_authorized'));
  25. }
  26. }
  27.  
  28. /**
  29. * Главная страница
  30. *
  31. * @return string
  32. */
  33. public function index(): string
  34. {
  35. $topics = Bookmark::query()
  36. ->select('bookmarks.count_posts as bookmark_posts', 'bookmarks.topic_id', 'topics.*')
  37. ->where('bookmarks.user_id', getUser('id'))
  38. ->leftJoin('topics', 'bookmarks.topic_id', 'topics.id')
  39. ->with('topic.user', 'topic.lastPost.user')
  40. ->orderByDesc('updated_at')
  41. ->paginate(setting('forumtem'));
  42.  
  43. return view('forums/bookmarks', compact('topics'));
  44. }
  45.  
  46. /**
  47. * Добавление / удаление закладок
  48. *
  49. * @param Request $request
  50. * @param Validator $validator
  51. *
  52. * @return string
  53. * @throws Exception
  54. */
  55. public function perform(Request $request, Validator $validator): string
  56. {
  57. if (! $request->ajax()) {
  58. redirect('/');
  59. }
  60.  
  61. $tid = int($request->input('tid'));
  62.  
  63. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'));
  64.  
  65. /** @var Topic $topic */
  66. $topic = Topic::query()->find($tid);
  67. $validator->true($topic, __('forums.topic_not_exist'));
  68.  
  69. if ($validator->isValid()) {
  70. $bookmark = Bookmark::query()
  71. ->where('topic_id', $tid)
  72. ->where('user_id', getUser('id'))
  73. ->first();
  74.  
  75. if ($bookmark) {
  76. $bookmark->delete();
  77. return json_encode(['status' => 'deleted', 'message' => __('forums.bookmark_success_deleted')]);
  78. }
  79.  
  80. Bookmark::query()->create([
  81. 'user_id' => getUser('id'),
  82. 'topic_id' => $tid,
  83. 'count_posts' => $topic->count_posts,
  84. ]);
  85. return json_encode(['status' => 'added', 'message' => __('forums.bookmark_success_added')]);
  86. }
  87.  
  88. return json_encode(['status' => 'error', 'message' => current($validator->getErrors())]);
  89. }
  90.  
  91. /**
  92. * Удаление закладок
  93. *
  94. * @param Request $request
  95. * @param Validator $validator
  96. *
  97. * @return void
  98. */
  99. public function delete(Request $request, Validator $validator): void
  100. {
  101. $topicIds = intar($request->input('del'));
  102.  
  103. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'))
  104. ->notEmpty($topicIds, __('forums.bookmarks_missing'));
  105.  
  106. if ($validator->isValid()) {
  107. Bookmark::query()
  108. ->whereIn('topic_id', intar($request->input('del')))
  109. ->where('user_id', getUser('id'))
  110. ->delete();
  111.  
  112. setFlash('success', __('forums.bookmarks_selected_deleted'));
  113. } else {
  114. setFlash('danger', $validator->getErrors());
  115. }
  116.  
  117. redirect('/forums/bookmarks?page=' . int($request->input('page')));
  118. }
  119. }