la_bloger/app/Http/Controllers/Admin/TaxonomyTermController.php

92 lines
2.8 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Admin\Concerns\ResolvesActiveChannel;
use App\Http\Controllers\Controller;
use App\Models\TaxonomyTerm;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\View\View;
class TaxonomyTermController extends Controller
{
use ResolvesActiveChannel;
public function index(): View
{
$channel = $this->activeChannel();
$terms = TaxonomyTerm::withCount('publications')
->where('channel_id', $channel->id)
->orderBy('title')
->get();
return view('admin::taxonomy.index', compact('channel', 'terms'));
}
public function create(): View
{
$channel = $this->activeChannel();
$term = new TaxonomyTerm();
return view('admin::taxonomy.form', compact('channel', 'term'));
}
public function store(Request $request): RedirectResponse
{
$channel = $this->activeChannel();
$request->validate([
'title' => ['required', 'string', 'max:255'],
]);
$slug = Str::slug($request->title);
$exists = TaxonomyTerm::where('channel_id', $channel->id)->where('slug', $slug)->exists();
if ($exists) {
return back()->withErrors(['title' => 'A term with this slug already exists in this channel.'])->withInput();
}
TaxonomyTerm::create([
'channel_id' => $channel->id,
'title' => $request->title,
'slug' => $slug,
]);
return redirect()->route('admin.taxonomy.index')
->with('success', "Term \"{$request->title}\" created.");
}
public function edit(TaxonomyTerm $term): View
{
$channel = $this->activeChannel();
abort_if($term->channel_id !== $channel->id, 403);
return view('admin::taxonomy.form', compact('channel', 'term'));
}
public function update(Request $request, TaxonomyTerm $term): RedirectResponse
{
$channel = $this->activeChannel();
abort_if($term->channel_id !== $channel->id, 403);
$request->validate([
'title' => ['required', 'string', 'max:255'],
]);
$slug = Str::slug($request->title);
$term->update(['title' => $request->title, 'slug' => $slug]);
return redirect()->route('admin.taxonomy.index')
->with('success', "Term \"{$term->title}\" updated.");
}
public function destroy(TaxonomyTerm $term): RedirectResponse
{
$channel = $this->activeChannel();
abort_if($term->channel_id !== $channel->id, 403);
$title = $term->title;
$term->delete();
return redirect()->route('admin.taxonomy.index')
->with('success', "Term \"{$title}\" deleted.");
}
}