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

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