Skip to content

Commit a79d93e

Browse files
Merge pull request #2342 from codeeu/dev
Search
2 parents 46ea5ed + 60ba966 commit a79d93e

13 files changed

Lines changed: 229 additions & 354 deletions

File tree

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

app/Http/Controllers/SearchController.php

100755100644
Lines changed: 4 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,13 @@
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;
105
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;
156

167
class SearchController extends Controller
178
{
18-
protected $eventTransformer;
19-
20-
/**
21-
* EventController constructor.
22-
*/
23-
public function __construct(EventTransformer $eventTransformer)
24-
{
25-
$this->eventTransformer = $eventTransformer;
26-
}
27-
28-
public function search(Request $request): View
29-
{
30-
31-
$query = $request->input('q', '');
32-
$selected_year = $request->input('year', Carbon::now()->year);
33-
34-
$country_iso = $request->input('country_iso', null);
35-
$tag = $request->input('tag', '');
36-
37-
$selected_country = [];
38-
39-
if (! is_null($country_iso)) {
40-
$country = Country::where('iso', $country_iso)->first();
41-
if ($country) {
42-
$country->translation = __('countries.'.$country->name);
43-
$selected_country[] = $country;
44-
}
45-
46-
}
47-
48-
$current_year = Carbon::now()->year;
49-
$years = [];
50-
for ($year = $current_year; $year >= 2014; $year--) {
51-
$years[] = $year;
52-
}
53-
54-
return view('event.search', compact(['query', 'years', 'selected_country', 'selected_year', 'tag']));
55-
}
56-
57-
public function searchPOST(EventFilters $filters, Request $request)
58-
{
59-
$events = $this->getEvents($filters);
60-
61-
//Log::info($request->input('page'));
62-
if ($request->input('page')) {
63-
$result = [$events];
64-
} else {
65-
Log::info('no page');
66-
$eventsMap = $this->getAllEventsToMap($filters);
67-
$result = [$events, $eventsMap];
68-
}
69-
70-
return $result;
71-
}
72-
73-
protected function getEvents(EventFilters $filters)
9+
public function index()
7410
{
75-
76-
$events = Event::where('status', 'like', 'APPROVED')
77-
->filter($filters)
78-
->orderBy('start_date')
79-
->get()
80-
->groupBy(function ($event) {
81-
if ($event->start_date <= Carbon::today()) {
82-
return 'past';
83-
}
84-
85-
return 'future';
86-
});
87-
88-
if (is_null($events->get('future')) || is_null($events->get('past'))) {
89-
return $events->flatten()->paginate(12);
90-
}
91-
92-
return $events->get('future')->merge($events->get('past'))->paginate(12);
93-
94-
}
95-
96-
protected function getAllEventsToMap(EventFilters $filters)
97-
{
98-
99-
$flattened = Arr::flatten($filters->getFilters());
100-
101-
$composed_key = '';
102-
103-
foreach ($flattened as $value) {
104-
$composed_key .= $value.',';
105-
}
106-
107-
$value = Cache::get($composed_key, function () use ($composed_key, $filters) {
108-
Log::info("Building cache [{$composed_key}]");
109-
$events = Event::where('status', 'like', 'APPROVED')
110-
->filter($filters)
111-
->get();
112-
113-
$events = $this->eventTransformer->transformCollection($events);
114-
115-
$events = $events->groupBy('country');
116-
117-
Cache::put($composed_key, $events, 5 * 60);
118-
119-
return $events;
120-
});
121-
122-
Log::info("Serving from cache [{$composed_key}]");
123-
124-
return $value;
125-
11+
return view('static.search');
12612
}
127-
}
13+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Livewire;
4+
5+
use Livewire\Component;
6+
use App\Podcast;
7+
8+
class SearchContentComponent extends Component
9+
{
10+
public $searchQuery = '';
11+
12+
protected $queryString = [
13+
'searchQuery' => ['except' => ''],
14+
];
15+
16+
public function render()
17+
{
18+
$results = collect(); // Initialize an empty collection
19+
20+
if ($this->searchQuery) {
21+
$results = Podcast::active()
22+
->where('title', 'like', '%' . $this->searchQuery . '%')
23+
->get(); // get() returns a collection
24+
}
25+
26+
return view('livewire.search-content-component', [
27+
'results' => $results,
28+
]);
29+
}
30+
}

0 commit comments

Comments
 (0)