updated search and added past workshop page

This commit is contained in:
2024-09-27 13:33:50 +10:00
parent 5cbebd8840
commit b20c79b679
9 changed files with 139 additions and 94 deletions

View File

@@ -0,0 +1,57 @@
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use App\Models\Workshop;
use Carbon\Carbon;
use Illuminate\Http\Request;
class SearchController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$search = $request->get('q', '');
$search_words = explode(' ', $search); // Split the search query into words[1]
$workshopQuery = Workshop::query()->where('status', '!=', 'draft');
$workshopQuery->where(function ($query) use ($search_words) {
foreach ($search_words as $word) {
$query->orWhere(function ($subQuery) use ($word) {
$subQuery->where('title', 'like', '%' . $word . '%')
->orWhere('content', 'like', '%' . $word . '%')
->orWhereHas('location', function ($locationQuery) use ($word) {
$locationQuery->where('name', 'like', '%' . $word . '%');
});
});
}
});
$workshops = $workshopQuery->orderBy('starts_at', 'desc')
->paginate(6, ['*'], 'workshop');
$postQuery = Post::query()->where('status', 'published');
$postQuery->where(function ($query) use ($search_words) {
foreach ($search_words as $word) {
$query->where(function ($subQuery) use ($word) {
$subQuery->where('title', 'like', '%' . $word . '%')
->orWhere('content', 'like', '%' . $word . '%');
});
}
});
$posts = $postQuery->orderBy('created_at', 'desc')
->paginate(6, ['*'], 'post')
->onEachSide(1);
return view('search', [
'workshops' => $workshops,
'posts' => $posts,
'search' => $search,
]);
}
}

View File

@@ -11,93 +11,30 @@ class WorkshopController extends Controller
/** /**
* Display a listing of the resource. * Display a listing of the resource.
*/ */
public function index(Request $request) public function index()
{ {
$homeView = true;
$search = $request->get('search', '');
$query = Workshop::query(); $query = Workshop::query();
$query = $query->where('starts_at', '>=', Carbon::now()->subDays(8))
if(!auth()->user()?->admin) { ->orderBy('starts_at', 'asc');
$query = $query->where('status', '!=', 'draft');
}
if($request->has('search') && $request->search !== '') {
$homeView = false;
$query = $query->where(function ($query) use ($request) {
$query->where('title', 'like', '%' . $request->search . '%')
->orWhere('content', 'like', '%' . $request->search . '%');
});
}
if($request->has('location') && $request->location !== '') {
$homeView = false;
$query = $query->whereHas('location', function ($query) use ($request) {
$query->where('name', 'like', '%' . $request->location . '%');
});
}
if($request->has('date') && $request->date !== '') {
$homeView = false;
$dates = explode('-', $request->date);
$dates = array_map('trim', $dates);
$dates = array_map(function($date) {
$date = trim($date);
if(preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
return $date;
}
if(preg_match('/^(\d{2})-(\d{2})-(\d{2})$/', $date, $matches)) {
return '20' . $matches[1] . '-' . $matches[2] . '-' . $matches[3];
}
if(preg_match('/^\d{4}-\d{2}$/', $date)) {
return $date . '-01';
}
if(preg_match('/^\d{4}$/', $date)) {
return $date . '-01-01';
}
if(preg_match('/^(\d{2})\/(\d{2})\/(\d{2})$/', $date, $matches)) {
return '20' . $matches[3] . '-' . $matches[2] . '-' . $matches[1];
}
if(preg_match('/^(\d{2})\/(\d{2})\/(\d{4})$/', $date, $matches)) {
return $matches[3] . '-' . $matches[2] . '-' . $matches[1];
}
return '';
}, $dates);
if(count($dates) == 2) {
// If there are two dates, filter between starts_at and ends_at
$query = $query->whereDate('starts_at', '>=', $dates[0])
->whereDate('ends_at', '<=', $dates[1]);
} else {
// If there is one date, filter starts_at that date or newer
$query = $query->whereDate('starts_at', '>=', $dates[0]);
}
}
if($homeView) {
$query = $query->where('starts_at', '>=', Carbon::now()->subDays(8))
->orderBy('starts_at', 'asc');
} else {
$query = $query->orderBy('starts_at', 'asc');
}
$workshops = $query->paginate(12); $workshops = $query->paginate(12);
return view('workshop.index', [ return view('workshop.index', [
'workshops' => $workshops, 'workshops' => $workshops
'search' => $search,
]); ]);
} }
/** /**
* Display a listing of the resource. * Display a listing of the resource.
*/ */
public function past_index(Request $request) public function past_index()
{ {
$query = Workshop::query();
$query = $query->where('starts_at', '<', Carbon::now())
->orderBy('starts_at', 'desc');
$workshops = $query->paginate(12);
return view('workshop.index', [ return view('workshop.index', [
'workshops' => [], 'workshops' => $workshops
// 'search' => $search,
]); ]);
} }

View File

@@ -1,5 +1,8 @@
<x-container class="bg-primary-color-light text-white py-10"> <x-container class="bg-primary-color-light text-white py-10">
<h1 class="font-bold text-4xl">{{ $title ?? $slot }}</h1> <h1 class="font-bold text-4xl">{{ $title ?? $slot }}</h1>
@if(isset($description))
<div class="text-lg">{{ $description }}</div>
@endif
@if(isset($backRoute) && isset($backTitle)) @if(isset($backRoute) && isset($backTitle))
<a href="{{ route($backRoute) }}" class="text-lg hover:text-gray-300"><i class="fa-solid fa-angle-left mr-3"></i>{{ $backTitle }}</a> <a href="{{ route($backRoute) }}" class="text-lg hover:text-gray-300"><i class="fa-solid fa-angle-left mr-3"></i>{{ $backTitle }}</a>
@endif @endif

View File

@@ -1,4 +1,11 @@
<nav class="shadow bg-white"> <nav class="shadow bg-white" x-data="{showSearch:false}" x-init="
document.addEventListener('keydown', (event) => {
if ((event.metaKey || event.ctrlKey) && event.key === 'f') {
event.preventDefault();
$data.showSearch = true;
}
})
">
<div class="mx-auto max-w-7xl px-2 sm:px-6 lg:px-8 relative" x-data="{open:false}"> <div class="mx-auto max-w-7xl px-2 sm:px-6 lg:px-8 relative" x-data="{open:false}">
<div class="relative flex h-16 items-center justify-between"> <div class="relative flex h-16 items-center justify-between">
<div class="absolute inset-y-0 left-0 flex items-center sm:hidden"> <div class="absolute inset-y-0 left-0 flex items-center sm:hidden">
@@ -21,6 +28,11 @@
<div class="flex space-x-2"> <div class="flex space-x-2">
<a href="{{ route('post.index') }}" class="text-gray-900 hover:text-sky-500 px-3 py-2 text-sm font-medium transition duration-300 ease-in-out transform hover:-translate-y-0.5" aria-current="page">Blog</a> <a href="{{ route('post.index') }}" class="text-gray-900 hover:text-sky-500 px-3 py-2 text-sm font-medium transition duration-300 ease-in-out transform hover:-translate-y-0.5" aria-current="page">Blog</a>
<a href="{{ route('workshop.index') }}" class="text-gray-900 hover:text-sky-500 px-3 py-2 text-sm font-medium transition duration-300 ease-in-out transform hover:-translate-y-0.5">Workshops</a> <a href="{{ route('workshop.index') }}" class="text-gray-900 hover:text-sky-500 px-3 py-2 text-sm font-medium transition duration-300 ease-in-out transform hover:-translate-y-0.5">Workshops</a>
<button type="button" class="text-gray-900 hover:text-sky-500 text-sm font-medium transition duration-300 ease-in-out" @click.prevent="showSearch=true">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4">
<path stroke-linecap="round" stroke-linejoin="round" d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z" />
</svg>
</button>
</div> </div>
</div> </div>
<div class="ml-3"> <div class="ml-3">
@@ -57,4 +69,13 @@
@endif @endif
</div> </div>
</div> </div>
<div class="fixed inset-0 z-10 flex items-center justify-center" x-cloak x-show="showSearch" x-on:click="showSearch=false" x-on:keydown.escape.window="showSearch=false" x-init="$watch('showSearch', value => { if(value) { $nextTick(() => document.getElementsByName('q')[0].focus()) } })">
<div class="absolute inset-0 backdrop-blur-sm bg-opacity-40 bg-black"></div>
<div class="relative w-full mx-8 max-w-2xl bg-gray-50 p-2 rounded-lg shadow-lg" x-on:click.stop>
<form action="{{ route('search.index') }}" method="GET">
<x-ui.search type="text" name="q" label="Search..." />
</form>
</div>
</div>
</nav> </nav>

View File

@@ -5,7 +5,7 @@
@endphp @endphp
<form method="GET" action="{{ url()->current() }}" class="{{ $attributes->get('class') }}"> <form method="GET" action="{{ url()->current() }}" class="{{ $attributes->get('class') }}">
<div class="flex relative" x-data="{search:'{{request()->get('search')}}'}"> <div class="flex relative" x-data="{search:'{{request()->get('q')}}'}">
<input class="{{ $classes }}" autocomplete="off" placeholder="{{ $label }}" x-model="search" type="{{ $type }}" name="{{ $name }}" /> <input class="{{ $classes }}" autocomplete="off" placeholder="{{ $label }}" x-model="search" type="{{ $type }}" name="{{ $name }}" />
<x-ui.button type="submit" class="rounded-l-none px-6"><i class="fa-solid fa-magnifying-glass"></i></x-ui.button> <x-ui.button type="submit" class="rounded-l-none px-6"><i class="fa-solid fa-magnifying-glass"></i></x-ui.button>
<i x-show="search" cloak class="absolute z-10 top-1/2 right-[4.5rem] transform -translate-y-1/2 text-gray-300 hover:text-gray-400 cursor-pointer fa-solid fa-circle-xmark" x-data x-on:click="search='';$nextTick(()=>{if('{{request()->get('search')}}'!==''){$el.closest('form').submit();}})"></i> <i x-show="search" cloak class="absolute z-10 top-1/2 right-[4.5rem] transform -translate-y-1/2 text-gray-300 hover:text-gray-400 cursor-pointer fa-solid fa-circle-xmark" x-data x-on:click="search='';$nextTick(()=>{if('{{request()->get('search')}}'!==''){$el.closest('form').submit();}})"></i>

View File

@@ -2,15 +2,9 @@
<x-slot name="title">Blog</x-slot> <x-slot name="title">Blog</x-slot>
<x-mast>Blog</x-mast> <x-mast>Blog</x-mast>
<section class="bg-gray-100"> <section class="bg-gray-100">
<x-container class="pt-4">
<form method="GET" action="{{ request()->url() }}">
<x-ui.search name="search" label="Search" value="{{ request()->get('search') }}" />
</form>
</x-container>
@if($posts->isEmpty()) @if($posts->isEmpty())
<x-container class="mt-8"> <x-container class="mt-8">
<x-none-found item="posts" search="{{ request()->get('search') }}" /> <x-none-found item="posts" />
</x-container> </x-container>
@else @else
<x-container class="mt-4" inner-class="grid md:grid-cols-2 lg:grid-cols-3 gap-8 w-full"> <x-container class="mt-4" inner-class="grid md:grid-cols-2 lg:grid-cols-3 gap-8 w-full">

View File

@@ -0,0 +1,40 @@
<x-layout>
<x-mast title="Search" description='Results for "{{ $search }}"' />
<x-container>
<section class="bg-gray-100">
<h2 class="text-2xl font-bold my-6">Posts</h2>
@if($posts->isEmpty())
<x-container class="mt-8">
<x-none-found item="posts" search="{{ request()->get('search') }}" />
</x-container>
@else
<x-container class="mt-4" inner-class="grid md:grid-cols-2 lg:grid-cols-3 gap-8 w-full">
@foreach ($posts as $post)
<x-panel-post :post="$post" />
@endforeach
</x-container>
<x-container>
{{ $posts->appends(request()->except('post'))->links('', ['pageName' => 'post']) }}
</x-container>
@endif
</section>
<section class="bg-gray-100">
<h2 class="text-2xl font-bold my-6">Workshops</h2>
@if($workshops->isEmpty())
<x-container class="mt-8">
<x-none-found item="workshops" search="{{ request()->get('search') }}" />
</x-container>
@else
<x-container class="mt-4" inner-class="grid md:grid-cols-2 lg:grid-cols-3 gap-8 w-full">
@foreach ($workshops as $workshop)
<x-panel-workshop :workshop="$workshop" />
@endforeach
</x-container>
<x-container>
{{ $workshops->appends(request()->except('workshop'))->links('', ['pageName' => 'workshop']) }}
</x-container>
@endif
</section>
</x-container>
</x-layout>

View File

@@ -5,19 +5,9 @@
['title' => 'Past', 'route' => route('workshop.past.index')], ['title' => 'Past', 'route' => route('workshop.past.index')],
]"/> ]"/>
<section class="bg-gray-100"> <section class="bg-gray-100">
<x-container class="my-4">
<x-ui.search class="md:hidden" name="search" label="Search" value="{{ request()->get('search') }}" />
<form class="hidden md:flex gap-4" method="GET" action="{{ request()->url() }}">
<x-ui.input no-label class="my-0 flex-1" type="text" name="search" label="Keywords" value="{{ request()->get('search') }}"/>
<x-ui.input no-label class="my-0 flex-1" type="text" name="location" label="Location" value="{{ request()->get('location') }}"/>
<x-ui.input no-label class="my-0 flex-1" type="text" name="date" label="Date Range" value="{{ request()->get('date') }}"/>
<x-ui.button type="submit"><i class="fa-solid fa-magnifying-glass"></i></x-ui.button>
</form>
</x-container>
@if($workshops->isEmpty()) @if($workshops->isEmpty())
<x-container class="mt-8"> <x-container class="mt-8">
<x-none-found item="workshops" search="{{ request()->get('search') }}" /> <x-none-found item="workshops" />
</x-container> </x-container>
@else @else
<x-container class="mt-4" inner-class="grid md:grid-cols-2 lg:grid-cols-3 gap-8 w-full"> <x-container class="mt-4" inner-class="grid md:grid-cols-2 lg:grid-cols-3 gap-8 w-full">

View File

@@ -6,6 +6,7 @@ use App\Http\Controllers\HomeController;
use App\Http\Controllers\LocationController; use App\Http\Controllers\LocationController;
use App\Http\Controllers\MediaController; use App\Http\Controllers\MediaController;
use App\Http\Controllers\PostController; use App\Http\Controllers\PostController;
use App\Http\Controllers\SearchController;
use App\Http\Controllers\UserController; use App\Http\Controllers\UserController;
use App\Http\Controllers\WorkshopController; use App\Http\Controllers\WorkshopController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
@@ -18,6 +19,8 @@ Route::get('workshops', [WorkshopController::class, 'index'])->name('workshop.in
Route::get('workshops/past', [WorkshopController::class, 'past_index'])->name('workshop.past.index'); Route::get('workshops/past', [WorkshopController::class, 'past_index'])->name('workshop.past.index');
Route::get('workshops/{workshop}', [WorkshopController::class, 'show'])->name('workshop.show'); Route::get('workshops/{workshop}', [WorkshopController::class, 'show'])->name('workshop.show');
Route::get('search', [SearchController::class, 'index'])->name('search.index');
Route::middleware('auth')->group(function () { Route::middleware('auth')->group(function () {
Route::get('/account', [AccountController::class, 'show'])->name('account.show'); Route::get('/account', [AccountController::class, 'show'])->name('account.show');
Route::post('/account', [AccountController::class, 'update'])->name('account.update'); Route::post('/account', [AccountController::class, 'update'])->name('account.update');