Files
Kol-Manager/app/Http/Controllers/EventController.php

241 lines
10 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Enums\ContentStatus;
use App\Enums\EventStatus;
use App\Models\BrandContract;
use App\Models\Client;
use App\Models\ClientCategory;
use App\Models\ContentBrief;
use App\Models\Event;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\View\View;
class EventController extends Controller
{
private function categoryTree(): \Illuminate\Support\Collection
{
return ClientCategory::with('children')
->whereNull('parent_id')
->orderBy('sort_order')
->get();
}
public function create(): View
{
$clients = Client::where('status', 'active')->orderBy('name')->get();
$categoryTree = $this->categoryTree();
return view('schedule.create', compact('clients', 'categoryTree'));
}
public function index(): View
{
$events = Event::with('client')->orderBy('start_time')->get();
$clients = Client::where('status', 'active')->orderBy('name')->get();
$categoryTree = $this->categoryTree();
$contracts = BrandContract::select('client_id', 'title')->get();
$contents = ContentBrief::select('client_id', 'title')->get();
foreach ($events as $e) {
$e->has_contract = $e->client_id && $contracts->contains(fn($c) => $c->client_id === $e->client_id && stripos($c->title, $e->title) !== false);
$e->has_content = $e->client_id && $contents->contains(fn($c) => $c->client_id === $e->client_id && stripos($c->title, $e->title) !== false);
}
// Format for FullCalendar
$calendarEvents = $events->map(fn (Event $e) => [
'id' => $e->id,
'title' => ($e->is_priority ? '⭐ ' : '') . $e->title,
'start' => $e->start_time->format('Y-m-d\TH:i:s'),
'end' => $e->end_time?->format('Y-m-d\TH:i:s'),
'backgroundColor' => $e->color,
'borderColor' => $e->color,
'extendedProps' => [
'status' => $e->status->value,
'client' => $e->client?->name,
'type' => $e->event_type,
'priority' => $e->is_priority,
'location' => $e->location,
'description' => $e->description,
'notes' => $e->notes,
'has_contract' => $e->has_contract,
'has_content' => $e->has_content,
],
]);
return view('schedule.index', compact('events', 'clients', 'calendarEvents', 'categoryTree'));
}
public function store(Request $request): RedirectResponse
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'description' => 'nullable|string',
'client_id' => 'nullable|exists:clients,id',
'category_id' => 'nullable|exists:client_categories,id',
'event_type' => 'required|in:task,meeting,deadline,posting',
'start_time' => 'required|date',
'end_time' => 'nullable|date|after:start_time',
'location' => 'nullable|string|max:255',
'is_priority' => 'boolean',
'is_all_day' => 'boolean',
'color' => 'nullable|string|max:20',
'notes' => 'nullable|string',
'create_brand_contract' => 'nullable|boolean',
'contract_title' => 'nullable|string|max:255',
'contract_start_date' => 'nullable|date',
'contract_end_date' => 'nullable|date|after_or_equal:contract_start_date',
'add_to_content_board' => 'nullable|boolean',
'content_platform' => 'nullable|string|max:50',
'content_type' => 'nullable|string|max:50',
]);
$validated['is_priority'] = $request->boolean('is_priority');
$validated['is_all_day'] = $request->boolean('is_all_day');
$event = Event::create(Arr::except($validated, [
'create_brand_contract', 'contract_title', 'contract_start_date', 'contract_end_date',
'add_to_content_board', 'content_platform', 'content_type',
]));
if ($request->boolean('create_brand_contract') && $event->client_id) {
BrandContract::create([
'title' => $request->input('contract_title') ?: $event->title,
'client_id' => $event->client_id,
'category_id' => $event->category_id,
'start_date' => $request->input('contract_start_date') ?? $event->start_time->toDateString(),
'end_date' => $request->input('contract_end_date') ?? ($event->end_time?->toDateString() ?? $event->start_time->toDateString()),
'is_active' => true,
]);
}
if ($request->boolean('add_to_content_board')) {
ContentBrief::create([
'title' => $event->title,
'client_id' => $event->client_id,
'category_id' => $event->category_id,
'platform' => $request->input('content_platform', 'instagram'),
'content_type' => $request->input('content_type', 'post'),
'status' => ContentStatus::Drafting,
'target_date' => $event->start_time->toDateString(),
'notes' => $event->notes,
]);
}
return redirect()->route('schedule.index')
->with('success', '🐾 Event scheduled!');
}
public function edit(Event $event): View
{
$clients = Client::where('status', 'active')->orderBy('name')->get();
$statuses = EventStatus::cases();
$categoryTree = $this->categoryTree();
return view('schedule.edit', compact('event', 'clients', 'statuses', 'categoryTree'));
}
public function update(Request $request, Event $event): RedirectResponse
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'description' => 'nullable|string',
'client_id' => 'nullable|exists:clients,id',
'category_id' => 'nullable|exists:client_categories,id',
'event_type' => 'required|in:task,meeting,deadline,posting',
'start_time' => 'required|date',
'end_time' => 'nullable|date',
'location' => 'nullable|string|max:255',
'is_priority' => 'boolean',
'status' => 'required|in:pending,in_progress,completed,cancelled',
'color' => 'nullable|string|max:20',
'notes' => 'nullable|string',
'create_brand_contract' => 'nullable|boolean',
'contract_title' => 'nullable|string|max:255',
'contract_start_date' => 'nullable|date',
'contract_end_date' => 'nullable|date|after_or_equal:contract_start_date',
]);
$validated['is_priority'] = $request->boolean('is_priority');
$event->update(Arr::except($validated, [
'create_brand_contract', 'contract_title', 'contract_start_date', 'contract_end_date',
]));
if ($request->boolean('create_brand_contract') && $event->client_id) {
BrandContract::create([
'title' => $request->input('contract_title') ?: $event->title,
'client_id' => $event->client_id,
'category_id' => $event->category_id,
'start_date' => $request->input('contract_start_date') ?? $event->start_time->toDateString(),
'end_date' => $request->input('contract_end_date') ?? ($event->end_time?->toDateString() ?? $event->start_time->toDateString()),
'is_active' => true,
]);
}
return redirect()->route('schedule.index')
->with('success', '🐾 Event updated!');
}
public function updateDates(Request $request, Event $event): JsonResponse
{
$validated = $request->validate([
'start_time' => 'required|date',
'end_time' => 'nullable|date',
]);
$event->update($validated);
return response()->json(['ok' => true]);
}
public function destroy(Event $event): RedirectResponse
{
$event->delete();
return redirect()->route('schedule.index')
->with('success', '🐾 Event removed.');
}
public function apiList(): JsonResponse
{
$eventsList = Event::with('client')->get();
$contracts = BrandContract::select('client_id', 'title')->get();
$contents = ContentBrief::select('client_id', 'title')->get();
$events = $eventsList->map(function (Event $e) use ($contracts, $contents) {
$hasContract = $e->client_id && $contracts->contains(fn($c) => $c->client_id === $e->client_id && stripos($c->title, $e->title) !== false);
$hasContent = $e->client_id && $contents->contains(fn($c) => $c->client_id === $e->client_id && stripos($c->title, $e->title) !== false);
return [
'id' => $e->id,
'title' => ($e->is_priority ? '⭐ ' : '') . $e->title,
'start' => $e->start_time->format('Y-m-d\TH:i:s'),
'end' => $e->end_time?->format('Y-m-d\TH:i:s'),
'backgroundColor' => $e->color,
'borderColor' => $e->color,
'allDay' => $e->is_all_day,
'extendedProps' => [
'status' => $e->status->value,
'client' => $e->client?->name,
'type' => $e->event_type,
'priority' => $e->is_priority,
'location' => $e->location,
'description' => $e->description,
'notes' => $e->notes,
'has_contract' => $hasContract,
'has_content' => $hasContent,
],
];
});
return response()->json($events);
}
}