|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Livewire\Partials\Advancedsearch; |
| 4 | + |
| 5 | +use Livewire\Component; |
| 6 | +use Livewire\Attributes\On; |
| 7 | +use Livewire\Attributes\Validate; |
| 8 | + |
| 9 | +use App\Models\PredefinedFilter; |
| 10 | +use App\Services\PredefinedFilterService; |
| 11 | + |
| 12 | +enum FilterVisibility: string |
| 13 | +{ |
| 14 | + case Private = "private"; |
| 15 | + case Public = "public"; |
| 16 | +} |
| 17 | + |
| 18 | +enum AdvancedsearchModalAction: string |
| 19 | +{ |
| 20 | + case Create = "create"; |
| 21 | + case Edit = "edit"; |
| 22 | + case Delete = "delete"; |
| 23 | +} |
| 24 | + |
| 25 | +class Modal extends Component |
| 26 | +{ |
| 27 | + public $showModal = false; |
| 28 | + public AdvancedsearchModalAction $modalActionType; |
| 29 | + |
| 30 | + #[Validate("required")] |
| 31 | + public ?string $name = ""; |
| 32 | + public FilterVisibility $visibility = FilterVisibility::Private; |
| 33 | + public $groupSelect = []; |
| 34 | + public array $groupSelectOtherOptions = []; |
| 35 | + |
| 36 | + protected $listeners = ["groupSelect"]; |
| 37 | + |
| 38 | + public ?int $filterId; |
| 39 | + public $filterData; |
| 40 | + |
| 41 | + #[On("openPredefinedFiltersModal")] |
| 42 | + public function openPredefinedFiltersModal( |
| 43 | + PredefinedFilterService $predefinedFilterService, |
| 44 | + string $action, |
| 45 | + ?array $predefinedFilterData = null, |
| 46 | + ?int $predefinedFilterId = null |
| 47 | + ) { |
| 48 | + $this->modalActionType = AdvancedsearchModalAction::from($action); |
| 49 | + $this->showModal = true; |
| 50 | + $this->groupSelect = []; |
| 51 | + $this->groupSelectOtherOptions = []; |
| 52 | + $this->filterData = $predefinedFilterData; |
| 53 | + $this->filterId = $predefinedFilterId; |
| 54 | + |
| 55 | + $user = auth()->user(); |
| 56 | + $this->groupSelectOtherOptions = $user |
| 57 | + ->groups() |
| 58 | + ->pluck("id") |
| 59 | + ->toArray(); |
| 60 | + |
| 61 | + if ( |
| 62 | + $this->modalActionType === AdvancedsearchModalAction::Edit && |
| 63 | + $predefinedFilterId !== null |
| 64 | + ) { |
| 65 | + $predefinedFilter = $predefinedFilterService->getFilterById( |
| 66 | + $predefinedFilterId |
| 67 | + ); |
| 68 | + $this->name = $predefinedFilter["name"]; |
| 69 | + |
| 70 | + if ($predefinedFilter["is_public"] === 1) { |
| 71 | + $this->visibility = FilterVisibility::Public; |
| 72 | + } else { |
| 73 | + $this->visibility = FilterVisibility::Private; |
| 74 | + } |
| 75 | + |
| 76 | + foreach ($predefinedFilter["permissions"] as $permission) { |
| 77 | + array_push( |
| 78 | + $this->groupSelect, |
| 79 | + $permission->permission_group_id |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + $this->groupSelectOtherOptions = array_diff( |
| 84 | + $this->groupSelectOtherOptions, |
| 85 | + $this->groupSelect |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + $this->dispatch("openPredefinedFiltersModalEvent"); |
| 90 | + } |
| 91 | + |
| 92 | + #[On("closePredefinedFiltersModal")] |
| 93 | + public function closePredefinedFiltersModal() |
| 94 | + { |
| 95 | + $this->showModal = false; |
| 96 | + $this->groupSelect = []; |
| 97 | + $this->groupSelectOtherOptions = []; |
| 98 | + $this->filterData = null; |
| 99 | + $this->filterId = null; |
| 100 | + $this->name = ""; |
| 101 | + $this->dispatch("closePredefinedFiltersModalEvent"); |
| 102 | + } |
| 103 | + |
| 104 | + #[On("savePredefinedFiltersModal")] |
| 105 | + public function savePredefinedFiltersModal( |
| 106 | + PredefinedFilterService $predefinedFilterService |
| 107 | + ) { |
| 108 | + $this->validate(); |
| 109 | + |
| 110 | + $validated = [ |
| 111 | + "name" => $this->name, |
| 112 | + "filter_data" => $this->filterData, |
| 113 | + "is_public" => |
| 114 | + $this->visibility === FilterVisibility::Public ? 1 : 0, |
| 115 | + "permissions" => self::formatPermissions($this->groupSelect), |
| 116 | + ]; |
| 117 | + |
| 118 | + $createFilterResponse = $predefinedFilterService->createFilter($validated); |
| 119 | + |
| 120 | + if ($createFilterResponse === true) { |
| 121 | + $this->dispatch('showNotification', [ |
| 122 | + 'type' => 'success', |
| 123 | + 'title' => trans('general.notification_success'), |
| 124 | + 'message' => trans('general.predefined_filter_saved_successfully'), |
| 125 | + 'tag' => 'predefinedFilter', |
| 126 | + ]); |
| 127 | + } else { |
| 128 | + $this->dispatch('showNotification', [ |
| 129 | + 'type' => 'error', |
| 130 | + 'title' => trans('general.notification_error'), |
| 131 | + 'message' => trans('general.notification_error'), |
| 132 | + 'tag' => 'predefinedFilter', |
| 133 | + ]); |
| 134 | + } |
| 135 | + |
| 136 | + $this->dispatch("savePredefinedFiltersModalEvent"); |
| 137 | + $this->dispatch("closePredefinedFiltersModal"); |
| 138 | + } |
| 139 | + |
| 140 | + #[On("updatePredefinedFiltersModal")] |
| 141 | + public function updatePredefinedFiltersModal( |
| 142 | + PredefinedFilterService $predefinedFilterService |
| 143 | + ) { |
| 144 | + $this->validate([ |
| 145 | + 'name' => 'required|string', |
| 146 | + 'filterData' => 'array', |
| 147 | + 'groupSelect' => 'array', |
| 148 | + 'groupSelect.*' => 'required|integer|exists:permission_groups,id', |
| 149 | + ]); |
| 150 | + |
| 151 | + $predefinedFilter = PredefinedFilter::find($this->filterId); |
| 152 | + |
| 153 | + $validated = [ |
| 154 | + 'name' => $this->name ?? $predefinedFilter->name, |
| 155 | + 'filter_data' => $this->filterData ?? $predefinedFilter->filter_data, |
| 156 | + 'is_public' => isset($this->visibility) |
| 157 | + ? ($this->visibility === FilterVisibility::Public ? 1 : 0) |
| 158 | + : $predefinedFilter->is_public, |
| 159 | + 'permissions' => self::formatPermissions($this->getGroupSelectArrayAsArray()), |
| 160 | + ]; |
| 161 | + |
| 162 | + $updateFilterResponse = $predefinedFilterService->updateFilter($predefinedFilter, $validated); |
| 163 | + |
| 164 | + if ($updateFilterResponse === true) { |
| 165 | + $this->dispatch('showNotification', [ |
| 166 | + 'type' => 'success', |
| 167 | + 'title' => trans('general.notification_success'), |
| 168 | + 'message' => trans('general.predefined_filter_saved_successfully'), |
| 169 | + 'tag' => 'predefinedFilter', |
| 170 | + ]); |
| 171 | + } else { |
| 172 | + $this->dispatch('showNotification', [ |
| 173 | + 'type' => 'error', |
| 174 | + 'title' => trans('general.notification_error'), |
| 175 | + 'message' => trans('general.notification_error'), |
| 176 | + 'tag' => 'predefinedFilter', |
| 177 | + ]); |
| 178 | + } |
| 179 | + |
| 180 | + $this->dispatch("updatePredefinedFiltersModalEvent"); |
| 181 | + $this->dispatch("closePredefinedFiltersModal"); |
| 182 | + } |
| 183 | + |
| 184 | + #[On("deletePredefinedFiltersModal")] |
| 185 | + public function deletePredefinedFiltersModal( |
| 186 | + PredefinedFilterService $predefinedFilterService |
| 187 | + ) { |
| 188 | + |
| 189 | + |
| 190 | + $predefinedFilter = $predefinedFilterService->getFilterById($this->filterId); |
| 191 | + $deleteFilterResponse = $predefinedFilterService->deleteFilter($predefinedFilter); |
| 192 | + |
| 193 | + if ($deleteFilterResponse === true) { |
| 194 | + $this->dispatch('showNotification', [ |
| 195 | + 'type' => 'success', |
| 196 | + 'title' => trans('general.notification_success'), |
| 197 | + 'message' => trans('general.predefined_filter_saved_successfully'), |
| 198 | + 'tag' => 'predefinedFilter', |
| 199 | + ]); |
| 200 | + } else { |
| 201 | + $this->dispatch('showNotification', [ |
| 202 | + 'type' => 'error', |
| 203 | + 'title' => trans('general.notification_error'), |
| 204 | + 'message' => trans('general.notification_error'), |
| 205 | + 'tag' => 'predefinedFilter', |
| 206 | + ]); |
| 207 | + } |
| 208 | + |
| 209 | + $this->dispatch("deletePredefinedFiltersModalEvent"); |
| 210 | + $this->dispatch("closePredefinedFiltersModal"); |
| 211 | + } |
| 212 | + |
| 213 | + public function updateGroupSelect($values) |
| 214 | + { |
| 215 | + $this->groupSelect = is_array($values) |
| 216 | + ? $values |
| 217 | + : ($values |
| 218 | + ? [$values] |
| 219 | + : []); |
| 220 | + } |
| 221 | + |
| 222 | + public function render() |
| 223 | + { |
| 224 | + return view("livewire.partials.advancedsearch.modal"); |
| 225 | + } |
| 226 | + |
| 227 | + private function getGroupSelectArrayAsArray(): array |
| 228 | + { |
| 229 | + if (is_array($this->groupSelect) === true) { |
| 230 | + return $this->groupSelect; |
| 231 | + } |
| 232 | + return [$this->groupSelect]; |
| 233 | + } |
| 234 | + |
| 235 | + private static function formatPermissions(array $permissions): array |
| 236 | + { |
| 237 | + $result = []; |
| 238 | + |
| 239 | + foreach ($permissions as $value) { |
| 240 | + $result[] = ["permission_group_id" => $value]; |
| 241 | + } |
| 242 | + |
| 243 | + return $result; |
| 244 | + } |
| 245 | +} |
0 commit comments