This commit is contained in:
Mark Gregory
2026-06-15 18:05:23 +01:00
parent 0a9746fa77
commit a96369c90a
16 changed files with 164 additions and 143 deletions
+10 -63
View File
@@ -16,7 +16,6 @@ use Modules\Helpdesk\Models\Category;
use Modules\Helpdesk\Models\Priority;
use Modules\Helpdesk\Models\Product;
use Modules\Helpdesk\Models\Project;
use Modules\Helpdesk\Models\Status;
use Modules\Helpdesk\Models\Team;
use Modules\Helpdesk\Models\Tag;
use Modules\Helpdesk\Services\TicketService;
@@ -34,9 +33,6 @@ final class TicketCreate extends Component
#[Validate('required|string|min:10')]
public string $body = '';
#[Validate('nullable|integer|exists:helpdesk_statuses,id')]
public ?int $status_id = null;
#[Validate('required|integer|exists:helpdesk_priorities,id')]
public ?int $priority_id = null;
@@ -88,7 +84,6 @@ final class TicketCreate extends Component
'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,
@@ -105,10 +100,7 @@ final class TicketCreate extends Component
public function render(): View
{
$this->applyDefaults();
return view('helpdesk::livewire.ticket-create', [
'statuses' => $this->statusOptions(),
'priorities' => $this->priorityOptions(),
'categories' => $this->categoryOptions(),
'companies' => $this->companyOptions(),
@@ -125,28 +117,21 @@ final class TicketCreate extends Component
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')
$this->company_id = 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'));
->value('id');
}
if (blank($this->priority_id) && $this->hasDefaultColumn('helpdesk_priorities')) {
$this->priority_id = $this->nullableInt(Priority::query()
$this->priority_id = Priority::query()
->where('is_default', true)
->orderBy('sort_order')
->value('id'));
->value('id');
}
if (blank($this->category_id) && $this->hasDefaultColumn('helpdesk_categories')) {
$this->category_id = $this->nullableInt(Category::query()
$this->category_id = Category::query()
->where('is_default', true)
->when($this->company_id, fn ($query) => $query->where(function ($query): void {
$query->where('company_id', $this->company_id)
@@ -154,11 +139,11 @@ final class TicketCreate extends Component
}))
->orderByRaw('company_id is null')
->orderBy('name')
->value('id'));
->value('id');
}
if (blank($this->team_id) && $this->hasDefaultColumn('helpdesk_teams')) {
$this->team_id = $this->nullableInt(Team::query()
$this->team_id = Team::query()
->where('is_default', true)
->whereNull('archived_at')
->when($this->company_id, fn ($query) => $query->where(function ($query): void {
@@ -167,11 +152,11 @@ final class TicketCreate extends Component
}))
->orderByRaw('company_id is null')
->orderBy('name')
->value('id'));
->value('id');
}
if (blank($this->project_id) && $this->hasDefaultColumn('helpdesk_projects')) {
$this->project_id = $this->nullableInt(Project::query()
$this->project_id = Project::query()
->where('is_default', true)
->whereNull('archived_at')
->when($this->company_id, fn ($query) => $query->where(function ($query): void {
@@ -185,23 +170,11 @@ final class TicketCreate extends Component
->orderByRaw('company_id is null')
->orderByRaw('team_id is null')
->orderBy('title')
->value('id'));
->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.
*/
@@ -270,32 +243,6 @@ final class TicketCreate extends Component
->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.
*/