fedi-feed-router/app/Http/Controllers/PlatformChannelsController.php

98 lines
No EOL
3.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\PlatformChannel;
use App\Models\PlatformInstance;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\View as ViewFacade;
class PlatformChannelsController extends Controller
{
public function index(): View
{
$channels = PlatformChannel::with('platformInstance')
->orderBy('platform_instance_id')
->orderBy('name')
->get();
return ViewFacade::make('pages.channels.index', compact('channels'));
}
public function create(): View
{
$instances = PlatformInstance::where('is_active', true)
->orderBy('name')
->get();
return ViewFacade::make('pages.channels.create', compact('instances'));
}
public function store(Request $request): RedirectResponse
{
$validated = $request->validate([
'platform_instance_id' => 'required|exists:platform_instances,id',
'name' => 'required|string|max:255',
'display_name' => 'nullable|string|max:255',
'channel_id' => 'nullable|string|max:255',
'description' => 'nullable|string',
'language_id' => 'required|exists:languages,id',
'is_active' => 'boolean',
]);
// Default is_active to true if not provided
$validated['is_active'] = $validated['is_active'] ?? true;
// Set display_name to name if not provided
$validated['display_name'] = $validated['display_name'] ?? $validated['name'];
PlatformChannel::create($validated);
// Check if there's a redirect_to parameter for onboarding flow
$redirectTo = $request->input('redirect_to');
if ($redirectTo) {
return redirect($redirectTo)
->with('success', 'Channel created successfully!');
}
return redirect()->route('channels.index')
->with('success', 'Channel created successfully!');
}
public function edit(PlatformChannel $channel): View
{
$instances = PlatformInstance::where('is_active', true)
->orderBy('name')
->get();
return ViewFacade::make('pages.channels.edit', compact('channel', 'instances'));
}
public function update(Request $request, PlatformChannel $channel): RedirectResponse
{
$validated = $request->validate([
'platform_instance_id' => 'required|exists:platform_instances,id',
'name' => 'required|string|max:255',
'display_name' => 'nullable|string|max:255',
'channel_id' => 'nullable|string|max:255',
'description' => 'nullable|string',
'language_id' => 'required|exists:languages,id',
'is_active' => 'boolean',
]);
$channel->update($validated);
return redirect()->route('channels.index')
->with('success', 'Channel updated successfully!');
}
public function destroy(PlatformChannel $channel): RedirectResponse
{
$channel->delete();
return redirect()->route('channels.index')
->with('success', 'Channel deleted successfully!');
}
}