*/ #[Validate('array')] public array $tag_ids = []; /** @var array */ #[Validate('array')] public array $attachments = []; /** * Initialise configured Helpdesk defaults for a new ticket. */ public function mount(): void { $this->applyDefaults(); } /** * Store a new helpdesk ticket. */ public function save(TicketService $tickets): mixed { $this->validate(); $this->validate([ 'attachments.*' => 'file|max:'.config('helpdesk.max_upload_kb').'|mimes:'.implode(',', config('helpdesk.allowed_mimes')), 'tag_ids.*' => 'integer|exists:helpdesk_tags,id', ]); $ticket = $tickets->create(Auth::user(), [ 'company_id' => $this->company_id, 'subject' => $this->subject, 'body' => $this->body, 'status_id' => $this->status_id, 'priority_id' => $this->priority_id, 'category_id' => $this->category_id, 'project_id' => $this->project_id, 'product_id' => $this->product_id, 'team_id' => $this->team_id, 'due_at' => $this->due_at, 'tag_ids' => $this->tag_ids, ], $this->attachments); session()->flash('success', 'Ticket '.$ticket->reference.' has been created.'); return $this->redirectRoute('helpdesk.tickets.index', navigate: true); } public function render(): View { $this->applyDefaults(); return view('helpdesk::livewire.ticket-create', [ 'statuses' => $this->statusOptions(), 'priorities' => $this->priorityOptions(), 'categories' => $this->categoryOptions(), 'companies' => $this->companyOptions(), 'teams' => $this->teamOptions(), 'projects' => $this->projectOptions(), 'products' => Product::query()->where('is_active', true)->orderBy('name')->get(), 'tags' => Tag::query()->orderBy('name')->get(), ])->layout('layouts.app'); } /** * Apply default values from Helpdesk/company lookup configuration. */ private function applyDefaults(): void { if (blank($this->company_id) && Schema::hasTable('companies') && Schema::hasColumn('companies', 'is_default_helpdesk_company')) { $this->company_id = $this->nullableInt(DB::table('companies') ->where('is_default_helpdesk_company', true) ->orderBy('name') ->value('id')); } if (blank($this->status_id) && $this->hasDefaultColumn('helpdesk_statuses')) { $this->status_id = $this->nullableInt(Status::query() ->where('is_default', true) ->orderBy('sort_order') ->value('id')); } if (blank($this->priority_id) && $this->hasDefaultColumn('helpdesk_priorities')) { $this->priority_id = $this->nullableInt(Priority::query() ->where('is_default', true) ->orderBy('sort_order') ->value('id')); } if (blank($this->category_id) && $this->hasDefaultColumn('helpdesk_categories')) { $this->category_id = $this->nullableInt(Category::query() ->where('is_default', true) ->when($this->company_id, fn ($query) => $query->where(function ($query): void { $query->where('company_id', $this->company_id) ->orWhereNull('company_id'); })) ->orderByRaw('company_id is null') ->orderBy('name') ->value('id')); } if (blank($this->team_id) && $this->hasDefaultColumn('helpdesk_teams')) { $this->team_id = $this->nullableInt(Team::query() ->where('is_default', true) ->whereNull('archived_at') ->when($this->company_id, fn ($query) => $query->where(function ($query): void { $query->where('company_id', $this->company_id) ->orWhereNull('company_id'); })) ->orderByRaw('company_id is null') ->orderBy('name') ->value('id')); } if (blank($this->project_id) && $this->hasDefaultColumn('helpdesk_projects')) { $this->project_id = $this->nullableInt(Project::query() ->where('is_default', true) ->whereNull('archived_at') ->when($this->company_id, fn ($query) => $query->where(function ($query): void { $query->where('company_id', $this->company_id) ->orWhereNull('company_id'); })) ->when($this->team_id, fn ($query) => $query->where(function ($query): void { $query->where('team_id', $this->team_id) ->orWhereNull('team_id'); })) ->orderByRaw('company_id is null') ->orderByRaw('team_id is null') ->orderBy('title') ->value('id')); } } /** * Return status options with default records listed first. */ private function statusOptions(): mixed { return Status::query() ->when($this->hasDefaultColumn('helpdesk_statuses'), fn ($query) => $query->orderByDesc('is_default')) ->orderBy('sort_order') ->get(); } /** * Return priority options with default records listed first. */ private function priorityOptions(): mixed { return Priority::query() ->when($this->hasDefaultColumn('helpdesk_priorities'), fn ($query) => $query->orderByDesc('is_default')) ->orderBy('sort_order') ->get(); } /** * Return category options with default records listed first. */ private function categoryOptions(): mixed { return Category::query() ->when($this->hasDefaultColumn('helpdesk_categories'), fn ($query) => $query->orderByDesc('is_default')) ->orderBy('name') ->get(); } /** * Return team options with default records listed first. */ private function teamOptions(): mixed { return Team::query() ->whereNull('archived_at') ->when($this->hasDefaultColumn('helpdesk_teams'), fn ($query) => $query->orderByDesc('is_default')) ->orderBy('name') ->get(); } /** * Return project options with default records listed first. */ private function projectOptions(): mixed { return Project::query() ->whereNull('archived_at') ->when($this->hasDefaultColumn('helpdesk_projects'), fn ($query) => $query->orderByDesc('is_default')) ->orderBy('title') ->get(); } /** * Return company options with default flag support when available. */ private function companyOptions(): mixed { if (! Schema::hasTable('companies')) { return collect(); } $columns = ['id', 'name']; if (Schema::hasColumn('companies', 'is_default_helpdesk_company')) { $columns[] = 'is_default_helpdesk_company'; } return DB::table('companies') ->select($columns) ->orderByDesc(Schema::hasColumn('companies', 'is_default_helpdesk_company') ? 'is_default_helpdesk_company' : 'id') ->orderBy('name') ->get(); } /** * Reload dependent defaults after the company changes. */ public function updatedCompanyId(mixed $value): void { $this->company_id = $this->nullableInt($value); $this->category_id = null; $this->team_id = null; $this->project_id = null; $this->applyDefaults(); } /** * Convert nullable database / Livewire values to nullable integers. */ private function nullableInt(mixed $value): ?int { if ($value === null || $value === '') { return null; } return (int) $value; } /** * Check whether a Helpdesk lookup table has the default flag column. */ private function hasDefaultColumn(string $table): bool { return Schema::hasTable($table) && Schema::hasColumn($table, 'is_default'); } }