-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSearchController.php
More file actions
executable file
·127 lines (94 loc) · 3.33 KB
/
SearchController.php
File metadata and controls
executable file
·127 lines (94 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace App\Http\Controllers;
use App\Country;
use App\Event;
use App\Filters\EventFilters;
use App\Http\Transformers\EventTransformer;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
class SearchController extends Controller
{
protected $eventTransformer;
/**
* EventController constructor.
*/
public function __construct(EventTransformer $eventTransformer)
{
$this->eventTransformer = $eventTransformer;
}
public function search(Request $request): View
{
$query = $request->input('q', '');
$selected_year = $request->input('year', Carbon::now()->year);
$country_iso = $request->input('country_iso', null);
$tag = $request->input('tag', '');
$selected_country = [];
if (! is_null($country_iso)) {
$country = Country::where('iso', $country_iso)->first();
if ($country) {
$country->translation = __('countries.'.$country->name);
$selected_country[] = $country;
}
}
$current_year = Carbon::now()->year;
$years = [];
for ($year = $current_year; $year >= 2014; $year--) {
$years[] = $year;
}
return view('event.search', compact(['query', 'years', 'selected_country', 'selected_year', 'tag']));
}
public function searchPOST(EventFilters $filters, Request $request)
{
$events = $this->getEvents($filters);
//Log::info($request->input('page'));
if ($request->input('page')) {
$result = [$events];
} else {
Log::info('no page');
$eventsMap = $this->getAllEventsToMap($filters);
$result = [$events, $eventsMap];
}
return $result;
}
protected function getEvents(EventFilters $filters)
{
$events = Event::where('status', 'like', 'APPROVED')
->filter($filters)
->orderBy('start_date')
->get()
->groupBy(function ($event) {
if ($event->start_date <= Carbon::today()) {
return 'past';
}
return 'future';
});
if (is_null($events->get('future')) || is_null($events->get('past'))) {
return $events->flatten()->paginate(12);
}
return $events->get('future')->merge($events->get('past'))->paginate(12);
}
protected function getAllEventsToMap(EventFilters $filters)
{
$flattened = Arr::flatten($filters->getFilters());
$composed_key = '';
foreach ($flattened as $value) {
$composed_key .= $value.',';
}
$value = Cache::get($composed_key, function () use ($composed_key, $filters) {
Log::info("Building cache [{$composed_key}]");
$events = Event::where('status', 'like', 'APPROVED')
->filter($filters)
->get();
$events = $this->eventTransformer->transformCollection($events);
$events = $events->groupBy('country');
Cache::put($composed_key, $events, 5 * 60);
return $events;
});
Log::info("Serving from cache [{$composed_key}]");
return $value;
}
}