la_bloger/app/Http/Controllers/Admin/ChannelController.php
2026-05-20 13:01:33 +02:00

94 lines
3.7 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Channel;
use App\Models\Prefix;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class ChannelController extends Controller
{
public function activate(Request $request): RedirectResponse
{
$request->validate(['channel_id' => ['required', 'exists:channels,id']]);
session(['admin.active_channel_id' => $request->integer('channel_id')]);
return back();
}
public function index(): View
{
$channels = Channel::withCount('prefixes')->orderBy('name')->paginate(25);
return view('admin::settings.channels.index', compact('channels'));
}
public function create(): View
{
$prefixes = Prefix::orderBy('slug')->get();
$channel = new Channel();
return view('admin::settings.channels.form', compact('channel', 'prefixes'));
}
public function store(Request $request): RedirectResponse
{
$data = $request->validate([
'name' => ['required', 'string', 'max:255'],
'hostname' => ['required', 'string', 'max:255', 'unique:channels,hostname'],
'locale' => ['required', 'string', 'max:10'],
'taxonomy_slug' => ['required', 'string', 'max:100'],
'template' => ['required', 'string', 'max:100', 'regex:/^[a-z0-9_-]+$/'],
'meta_title' => ['nullable', 'string', 'max:255'],
'meta_description' => ['nullable', 'string'],
'meta_keywords' => ['nullable', 'string', 'max:255'],
'og_image' => ['nullable', 'string', 'max:255'],
'prefixes' => ['nullable', 'array'],
'prefixes.*' => ['exists:prefixes,id'],
]);
$channel = Channel::create($data);
$channel->prefixes()->sync($request->input('prefixes', []));
return redirect()->route('admin.settings.channels.index')
->with('success', "Channel \"{$channel->name}\" created.");
}
public function edit(Channel $channel): View
{
$prefixes = Prefix::orderBy('slug')->get();
$channel->load('prefixes');
return view('admin::settings.channels.form', compact('channel', 'prefixes'));
}
public function update(Request $request, Channel $channel): RedirectResponse
{
$data = $request->validate([
'name' => ['required', 'string', 'max:255'],
'hostname' => ['required', 'string', 'max:255', "unique:channels,hostname,{$channel->id}"],
'locale' => ['required', 'string', 'max:10'],
'taxonomy_slug' => ['required', 'string', 'max:100'],
'template' => ['required', 'string', 'max:100', 'regex:/^[a-z0-9_-]+$/'],
'meta_title' => ['nullable', 'string', 'max:255'],
'meta_description' => ['nullable', 'string'],
'meta_keywords' => ['nullable', 'string', 'max:255'],
'og_image' => ['nullable', 'string', 'max:255'],
'prefixes' => ['nullable', 'array'],
'prefixes.*' => ['exists:prefixes,id'],
]);
$channel->update($data);
$channel->prefixes()->sync($request->input('prefixes', []));
return redirect()->route('admin.settings.channels.index')
->with('success', "Channel \"{$channel->name}\" updated.");
}
public function destroy(Channel $channel): RedirectResponse
{
$name = $channel->name;
$channel->delete();
return redirect()->route('admin.settings.channels.index')
->with('success', "Channel \"{$name}\" deleted.");
}
}