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

Размер файла: 2.24Kb
  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\Post;
  10. use App\Models\Topic;
  11. use Illuminate\Http\RedirectResponse;
  12. use Illuminate\Http\Request;
  13. use Illuminate\View\View;
  14.  
  15. class SearchController extends Controller
  16. {
  17. /**
  18. * Поиск
  19. *
  20. * @param Request $request
  21. * @param Validator $validator
  22. *
  23. * @return View|RedirectResponse
  24. */
  25. public function index(Request $request, Validator $validator)
  26. {
  27. $find = $request->input('find');
  28. $type = $request->input('type') === 'title' ? 'title' : 'text';
  29. $data = collect();
  30.  
  31. if ($find) {
  32. $find = rawurldecode(trim(preg_replace('/[^\w\x7F-\xFF\s]/', ' ', $find)));
  33.  
  34. $validator->length($find, 3, 64, ['find' => __('main.request_length')]);
  35. if ($validator->isValid()) {
  36. if (config('database.default') === 'mysql') {
  37. [$sql, $bindings] = ['MATCH (' . $type . ') AGAINST (? IN BOOLEAN MODE)', [$find . '*']];
  38. } else {
  39. [$sql, $bindings] = [$type . ' ILIKE ?', ['%' . $find . '%']];
  40. }
  41.  
  42. if ($type === 'title') {
  43. $data = Topic::query()
  44. ->whereRaw($sql, $bindings)
  45. ->with('forum', 'lastPost.user')
  46. ->paginate(setting('forumtem'))
  47. ->appends(compact('find', 'type'));
  48. } else {
  49. $data = Post::query()
  50. ->whereRaw($sql, $bindings)
  51. ->with('user', 'topic.forum')
  52. ->paginate(setting('forumpost'))
  53. ->appends(compact('find', 'type'));
  54. }
  55.  
  56. if ($data->isEmpty()) {
  57. setInput($request->all());
  58. setFlash('danger', __('main.empty_found'));
  59. return redirect('forums/search');
  60. }
  61. } else {
  62. setInput($request->all());
  63. setFlash('danger', $validator->getErrors());
  64. }
  65. }
  66.  
  67. return view('forums/search', compact('data', 'type', 'find'));
  68. }
  69. }