|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Livewire\Administration; |
| 4 | + |
| 5 | +use App\Models\TicketPriority; |
| 6 | +use Closure; |
| 7 | +use Filament\Forms\Components\ColorPicker; |
| 8 | +use Filament\Forms\Components\Select; |
| 9 | +use Filament\Forms\Components\TextInput; |
| 10 | +use Filament\Forms\Concerns\InteractsWithForms; |
| 11 | +use Filament\Forms\Contracts\HasForms; |
| 12 | +use Filament\Notifications\Actions\Action; |
| 13 | +use Filament\Notifications\Notification; |
| 14 | +use Illuminate\Support\Facades\DB; |
| 15 | +use Illuminate\Support\HtmlString; |
| 16 | +use Illuminate\Support\Str; |
| 17 | +use Illuminate\Validation\Rules\Unique; |
| 18 | +use Livewire\Component; |
| 19 | + |
| 20 | +class TicketPrioritiesDialog extends Component implements HasForms |
| 21 | +{ |
| 22 | + use InteractsWithForms; |
| 23 | + |
| 24 | + public TicketPriority $priority; |
| 25 | + public bool $deleteConfirmationOpened = false; |
| 26 | + |
| 27 | + protected $listeners = ['doDeletePriority', 'cancelDeletePriority']; |
| 28 | + |
| 29 | + public function mount(): void |
| 30 | + { |
| 31 | + $this->form->fill([ |
| 32 | + 'title' => $this->priority->title, |
| 33 | + 'text_color' => $this->priority->text_color, |
| 34 | + 'bg_color' => $this->priority->bg_color, |
| 35 | + 'icon' => $this->priority->icon, |
| 36 | + ]); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + public function render() |
| 41 | + { |
| 42 | + return view('livewire.administration.ticket-priorities-dialog'); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Form schema definition |
| 47 | + * |
| 48 | + * @return array |
| 49 | + */ |
| 50 | + protected function getFormSchema(): array |
| 51 | + { |
| 52 | + return [ |
| 53 | + TextInput::make('title') |
| 54 | + ->label(__('Title')) |
| 55 | + ->maxLength(255) |
| 56 | + ->unique(table: TicketPriority::class, column: 'title', ignorable: fn () => $this->priority, callback: function (Unique $rule) { |
| 57 | + return $rule->withoutTrashed(); |
| 58 | + }) |
| 59 | + ->required(), |
| 60 | + |
| 61 | + ColorPicker::make('text_color') |
| 62 | + ->label(__('Text color')) |
| 63 | + ->required(), |
| 64 | + |
| 65 | + ColorPicker::make('bg_color') |
| 66 | + ->label(__('Background color')) |
| 67 | + ->required(), |
| 68 | + |
| 69 | + Select::make('icon') |
| 70 | + ->label(__('Icon')) |
| 71 | + ->reactive() |
| 72 | + ->searchable() |
| 73 | + ->required() |
| 74 | + ->hint(fn(Closure $get) => $get('icon') ? new HtmlString(__('Selected icon:') . ' <i class="fa fa-2x ' . $get('icon') . '"></i>') : '') |
| 75 | + ->helperText(new HtmlString(__("Check the <a href='https://fontawesome.com/icons' target='_blank' class='text-blue-500 underline'>fontawesome icons here</a> to choose your right icon"))) |
| 76 | + ->options(DB::table('icons')->get()->pluck('icon', 'icon')->toArray()), |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Create / Update the priority |
| 82 | + * |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function save(): void |
| 86 | + { |
| 87 | + $data = $this->form->getState(); |
| 88 | + if (!$this->priority?->id) { |
| 89 | + $priority = TicketPriority::create([ |
| 90 | + 'title' => $data['title'], |
| 91 | + 'text_color' => $data['text_color'], |
| 92 | + 'bg_color' => $data['bg_color'], |
| 93 | + 'icon' => $data['icon'], |
| 94 | + 'slug' => Str::slug($data['title'], '_') |
| 95 | + ]); |
| 96 | + Notification::make() |
| 97 | + ->success() |
| 98 | + ->title(__('Priority created')) |
| 99 | + ->body(__('The priority has been created')) |
| 100 | + ->send(); |
| 101 | + } else { |
| 102 | + $this->priority->title = $data['title']; |
| 103 | + $this->priority->text_color = $data['text_color']; |
| 104 | + $this->priority->bg_color = $data['bg_color']; |
| 105 | + $this->priority->icon = $data['icon']; |
| 106 | + $this->priority->slug = Str::slug($data['title'], '_'); |
| 107 | + $this->priority->save(); |
| 108 | + Notification::make() |
| 109 | + ->success() |
| 110 | + ->title(__('Priority updated')) |
| 111 | + ->body(__('The priority\'s details has been updated')) |
| 112 | + ->send(); |
| 113 | + } |
| 114 | + $this->emit('prioritySaved'); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Delete an existing priority |
| 119 | + * |
| 120 | + * @return void |
| 121 | + */ |
| 122 | + public function doDeletePriority(): void |
| 123 | + { |
| 124 | + $this->priority->delete(); |
| 125 | + $this->deleteConfirmationOpened = false; |
| 126 | + $this->emit('priorityDeleted'); |
| 127 | + Notification::make() |
| 128 | + ->success() |
| 129 | + ->title(__('Priority deleted')) |
| 130 | + ->body(__('The priority has been deleted')) |
| 131 | + ->send(); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Cancel the deletion of a priority |
| 136 | + * |
| 137 | + * @return void |
| 138 | + */ |
| 139 | + public function cancelDeletePriority(): void |
| 140 | + { |
| 141 | + $this->deleteConfirmationOpened = false; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Show the delete priority confirmation dialog |
| 146 | + * |
| 147 | + * @return void |
| 148 | + * @throws \Exception |
| 149 | + */ |
| 150 | + public function deletePriority(): void |
| 151 | + { |
| 152 | + $this->deleteConfirmationOpened = true; |
| 153 | + Notification::make() |
| 154 | + ->warning() |
| 155 | + ->title(__('Priority deletion')) |
| 156 | + ->body(__('Are you sure you want to delete this priority?')) |
| 157 | + ->actions([ |
| 158 | + Action::make('confirm') |
| 159 | + ->label(__('Confirm')) |
| 160 | + ->color('danger') |
| 161 | + ->button() |
| 162 | + ->close() |
| 163 | + ->emit('doDeletePriority'), |
| 164 | + Action::make('cancel') |
| 165 | + ->label(__('Cancel')) |
| 166 | + ->close() |
| 167 | + ->emit('cancelDeletePriority') |
| 168 | + ]) |
| 169 | + ->persistent() |
| 170 | + ->send(); |
| 171 | + } |
| 172 | +} |
0 commit comments