updates
This commit is contained in:
@@ -16,7 +16,9 @@ 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;
|
||||
|
||||
final class TicketCreate extends Component
|
||||
@@ -32,6 +34,9 @@ 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;
|
||||
|
||||
@@ -50,10 +55,23 @@ final class TicketCreate extends Component
|
||||
#[Validate('nullable|date')]
|
||||
public ?string $due_at = null;
|
||||
|
||||
|
||||
/** @var array<int, int> */
|
||||
#[Validate('array')]
|
||||
public array $tag_ids = [];
|
||||
|
||||
/** @var array<int, TemporaryUploadedFile> */
|
||||
#[Validate('array')]
|
||||
public array $attachments = [];
|
||||
|
||||
/**
|
||||
* Initialise configured Helpdesk defaults for a new ticket.
|
||||
*/
|
||||
public function mount(): void
|
||||
{
|
||||
$this->applyDefaults();
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a new helpdesk ticket.
|
||||
*/
|
||||
@@ -63,34 +81,226 @@ final class TicketCreate extends Component
|
||||
|
||||
$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 redirect()->route('helpdesk.tickets.index');
|
||||
return $this->redirectRoute('helpdesk.tickets.index', navigate: true);
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
$this->applyDefaults();
|
||||
|
||||
return view('helpdesk::livewire.ticket-create', [
|
||||
'priorities' => Priority::query()->orderBy('sort_order')->get(),
|
||||
'categories' => Category::query()->orderBy('name')->get(),
|
||||
'companies' => Schema::hasTable('companies') ? DB::table('companies')->select(['id', 'name'])->orderBy('name')->get() : collect(),
|
||||
'teams' => Team::query()->whereNull('archived_at')->orderBy('name')->get(),
|
||||
'projects' => Project::query()->whereNull('archived_at')->orderBy('title')->get(),
|
||||
'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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user