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

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