|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\Repositories\Contracts\ForumRepositoryInterface as ForumRepository; |
|
6
|
|
|
use Coyote\Repositories\Criteria\Forum\AccordingToUserOrder; |
|
7
|
|
|
use Coyote\Repositories\Criteria\Forum\OnlyThoseWithAccess; |
|
8
|
|
|
use Coyote\Services\Elasticsearch\Factory; |
|
9
|
|
|
use Coyote\Services\Elasticsearch\SearchOptions; |
|
10
|
|
|
use Coyote\Services\Forum\TreeBuilder\JsonDecorator; |
|
11
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
12
|
|
|
use GuzzleHttp\Exception\ConnectException; |
|
13
|
|
|
use GuzzleHttp\Exception\ServerException; |
|
14
|
|
|
use Illuminate\Http\Request; |
|
15
|
|
|
use Coyote\Services\Forum\TreeBuilder\Builder as TreeBuilder; |
|
16
|
|
|
use Illuminate\Validation\ValidationException; |
|
17
|
|
|
|
|
18
|
|
|
class SearchController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
const PAGE_LIMIT = 1500; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ForumRepository |
|
24
|
|
|
*/ |
|
25
|
|
|
private $forum; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param ForumRepository $forum |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(ForumRepository $forum) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct(); |
|
33
|
|
|
|
|
34
|
|
|
$this->forum = $forum; |
|
35
|
|
|
|
|
36
|
|
|
$this->breadcrumb->push('Szukaj', route('search')); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Request $request |
|
41
|
|
|
* @param Factory $factory |
|
42
|
|
|
* @return \Illuminate\View\View|string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function index(Request $request, Factory $factory) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->forum->pushCriteria(new OnlyThoseWithAccess($this->auth)); |
|
47
|
|
|
$this->forum->pushCriteria(new AccordingToUserOrder($this->userId, true)); |
|
48
|
|
|
|
|
49
|
|
|
$forums = (new JsonDecorator(new TreeBuilder($this->forum->list())))->build(); |
|
50
|
|
|
$strategy = $factory->make($request->input('model')); |
|
51
|
|
|
|
|
52
|
|
|
$this->formatArray($request); |
|
53
|
|
|
|
|
54
|
|
|
$payload = [ |
|
55
|
|
|
'hits' => null, |
|
56
|
|
|
'model' => $request->input('model'), |
|
57
|
|
|
'query' => $request->input('q'), |
|
58
|
|
|
'sort' => $request->input('sort'), |
|
59
|
|
|
'user' => $request->input('user'), |
|
60
|
|
|
'categories' => $request->input('categories', []), |
|
61
|
|
|
'page' => $request->input('page', 1), |
|
62
|
|
|
'posts_per_page' => $this->getSetting('forum.posts_per_page', 10), |
|
63
|
|
|
'page_limit' => self::PAGE_LIMIT, |
|
64
|
|
|
'forums' => $forums |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
|
|
$this->validate($request, [ |
|
69
|
|
|
'q' => 'nullable|string', |
|
70
|
|
|
'sort' => 'nullable|in:' . SearchOptions::DATE . ',' . SearchOptions::SCORE, |
|
71
|
|
|
'page' => 'nullable|integer|min:1|max:' . self::PAGE_LIMIT |
|
72
|
|
|
]); |
|
73
|
|
|
|
|
74
|
|
|
$payload['hits'] = $strategy->search($request)->content(); |
|
75
|
|
|
|
|
76
|
|
|
if ($request->wantsJson()) { |
|
77
|
|
|
return $payload['hits']; |
|
78
|
|
|
} |
|
79
|
|
|
} catch (ConnectException $exception) { |
|
80
|
|
|
$this->handleException($payload, 500, 'Brak połączenia z serwerem wyszukiwarki.'); |
|
81
|
|
|
} catch (ServerException | ClientException $exception) { |
|
82
|
|
|
$this->handleException($payload, 500, 'Serwer wyszukiwarki nie może przetworzyć tego żądania.'); |
|
83
|
|
|
} catch (ValidationException $exception) { |
|
84
|
|
|
$this->handleException($payload, 422, $exception->getMessage()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->view('search', $payload); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function handleException(array &$payload, int $code, string $message) |
|
91
|
|
|
{ |
|
92
|
|
|
if ($this->request->wantsJson()) { |
|
93
|
|
|
abort($code, $message); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$payload['error'] = $message; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function formatArray(Request $request) |
|
100
|
|
|
{ |
|
101
|
|
|
if (is_string($request->input('categories'))) { |
|
102
|
|
|
$value = array_filter(array_map('intval', explode(',', $request->input('categories')))); |
|
103
|
|
|
|
|
104
|
|
|
$request->replace(array_merge($request->all(), ['categories' => $value])); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|