Skip to content

Commit a4b4215

Browse files
authored
Merge pull request #2419 from codeeu/fix/events
Fix events page restore
2 parents 171cf7d + 1c6ed6c commit a4b4215

File tree

2 files changed

+123
-5
lines changed

2 files changed

+123
-5
lines changed
Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,132 @@
1-
<?php
1+
<?php
22

33
namespace App\Http\Controllers;
44

5+
use App\Country;
6+
use App\Event;
7+
use App\Filters\EventFilters;
8+
use App\Http\Transformers\EventTransformer;
9+
use Carbon\Carbon;
510
use Illuminate\Http\Request;
11+
use Illuminate\Support\Arr;
12+
use Illuminate\Support\Facades\Cache;
13+
use Illuminate\Support\Facades\Log;
14+
use Illuminate\View\View;
615

716
class SearchController extends Controller
817
{
18+
protected $eventTransformer;
19+
20+
/**
21+
* EventController constructor.
22+
*/
23+
public function __construct(EventTransformer $eventTransformer)
24+
{
25+
$this->eventTransformer = $eventTransformer;
26+
}
27+
928
public function index()
1029
{
1130
return view('static.search');
1231
}
13-
}
32+
33+
public function search(Request $request): View
34+
{
35+
36+
$query = $request->input('q', '');
37+
$selected_year = $request->input('year', Carbon::now()->year);
38+
39+
$country_iso = $request->input('country_iso', null);
40+
$tag = $request->input('tag', '');
41+
42+
$selected_country = [];
43+
44+
if (! is_null($country_iso)) {
45+
$country = Country::where('iso', $country_iso)->first();
46+
if ($country) {
47+
$country->translation = __('countries.'.$country->name);
48+
$selected_country[] = $country;
49+
}
50+
51+
}
52+
53+
$current_year = Carbon::now()->year;
54+
$years = [];
55+
for ($year = $current_year; $year >= 2014; $year--) {
56+
$years[] = $year;
57+
}
58+
59+
return view('event.search', compact(['query', 'years', 'selected_country', 'selected_year', 'tag']));
60+
}
61+
62+
public function searchPOST(EventFilters $filters, Request $request)
63+
{
64+
$events = $this->getEvents($filters);
65+
66+
//Log::info($request->input('page'));
67+
if ($request->input('page')) {
68+
$result = [$events];
69+
} else {
70+
Log::info('no page');
71+
$eventsMap = $this->getAllEventsToMap($filters);
72+
$result = [$events, $eventsMap];
73+
}
74+
75+
return $result;
76+
}
77+
78+
protected function getEvents(EventFilters $filters)
79+
{
80+
81+
$events = Event::where('status', 'like', 'APPROVED')
82+
->filter($filters)
83+
->orderBy('start_date')
84+
->get()
85+
->groupBy(function ($event) {
86+
if ($event->start_date <= Carbon::today()) {
87+
return 'past';
88+
}
89+
90+
return 'future';
91+
});
92+
93+
if (is_null($events->get('future')) || is_null($events->get('past'))) {
94+
return $events->flatten()->paginate(12);
95+
}
96+
97+
return $events->get('future')->merge($events->get('past'))->paginate(12);
98+
99+
}
100+
101+
protected function getAllEventsToMap(EventFilters $filters)
102+
{
103+
104+
$flattened = Arr::flatten($filters->getFilters());
105+
106+
$composed_key = '';
107+
108+
foreach ($flattened as $value) {
109+
$composed_key .= $value.',';
110+
}
111+
112+
$value = Cache::get($composed_key, function () use ($composed_key, $filters) {
113+
Log::info("Building cache [{$composed_key}]");
114+
$events = Event::where('status', 'like', 'APPROVED')
115+
->filter($filters)
116+
->get();
117+
118+
$events = $this->eventTransformer->transformCollection($events);
119+
120+
$events = $events->groupBy('country');
121+
122+
Cache::put($composed_key, $events, 5 * 60);
123+
124+
return $events;
125+
});
126+
127+
Log::info("Serving from cache [{$composed_key}]");
128+
129+
return $value;
130+
131+
}
132+
}

routes/web.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@
294294
'codingathome.turning-code-into-pictures'
295295
)->name('codingathome-turning-code-into-pictures');
296296

297-
Route::get('/events', [EventController::class, 'index'])->name('events_map');
298297
Route::get('/add', [EventController::class, 'create'])->name('create_event')->middleware('auth');
299298
Route::get('/map', [MapController::class, 'index'])->name('map');
300299
//Route::get('/resources', 'ResourcesPageController@index')->name('resources');
@@ -329,8 +328,8 @@
329328
->middleware('auth')
330329
->name('my_events');
331330

332-
// Route::get('/search', [SearchController::class, 'search'])->name('search_event');
333-
// Route::post('/search', [SearchController::class, 'searchPOST'])->name('search_events');
331+
Route::get('/events', [SearchController::class, 'search'])->name('events_map');
332+
Route::post('/search', [SearchController::class, 'searchPOST'])->name('search_events');
334333
Route::get('/search', [SearchController::class, 'index'])->name('search_event');
335334
Route::get('/scoreboard', [ScoreboardController::class, 'index'])->name('scoreboard');
336335
Route::patch('user', [UserController::class, 'update'])

0 commit comments

Comments
 (0)