Issues (62)

src/Filament/Pages/EventChannelConfiguration.php (6 issues)

1
<?php
2
3
namespace Usamamuneerchaudhary\Notifier\Filament\Pages;
4
5
use Filament\Forms;
6
use Filament\Forms\Components\Checkbox;
7
use Filament\Forms\Concerns\InteractsWithForms;
8
use Filament\Forms\Contracts\HasForms;
9
use Filament\Notifications\Notification;
10
use Filament\Pages\Page;
11
use Filament\Schemas\Components\Grid;
12
use Filament\Schemas\Components\Section;
13
use Usamamuneerchaudhary\Notifier\Models\NotificationChannel;
14
use Usamamuneerchaudhary\Notifier\Models\NotificationEvent;
15
16
class EventChannelConfiguration extends Page implements HasForms
17
{
18
    use InteractsWithForms;
0 ignored issues
show
The trait Filament\Forms\Concerns\InteractsWithForms requires some properties which are not provided by Usamamuneerchaudhary\Not...entChannelConfiguration: $timestamp, $map
Loading history...
19
20
    protected static string|null|\BackedEnum $navigationIcon = 'heroicon-o-cog-6-tooth';
0 ignored issues
show
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
    protected static ?string $navigationLabel = 'Event Channels';
22
    protected static ?string $title = 'Event Channel Configuration';
23
    protected static string|null|\UnitEnum $navigationGroup = 'Notifier';
0 ignored issues
show
The type UnitEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
    protected static ?int $navigationSort = 3;
25
    protected string $view = 'notifier::filament.pages.event-channel-configuration';
26
27
    public ?array $data = [];
28
29
    public function mount(): void
30
    {
31
        $events = NotificationEvent::where('is_active', true)
32
            ->orderBy('group')
33
            ->orderBy('name')
34
            ->get();
35
36
        $activeChannels = NotificationChannel::where('is_active', true)
37
            ->orderBy('title')
38
            ->get();
39
40
        $groupedEvents = $events->groupBy('group');
41
42
        $formData = [];
43
44
        foreach ($groupedEvents as $group => $groupEvents) {
45
            foreach ($groupEvents as $event) {
46
                $eventChannels = $event->settings['channels'] ?? [];
47
48
                $channelData = [];
49
                foreach ($activeChannels as $channel) {
50
                    $channelData[$channel->type] = in_array($channel->type, $eventChannels);
51
                }
52
53
                $formData["event_{$event->id}"] = $channelData;
54
            }
55
        }
56
57
        $this->form->fill($formData);
0 ignored issues
show
Bug Best Practice introduced by
The property form does not exist on Usamamuneerchaudhary\Not...entChannelConfiguration. Since you implemented __get, consider adding a @property annotation.
Loading history...
58
    }
59
60
    protected function getFormSchema(): array
61
    {
62
        $events = NotificationEvent::where('is_active', true)
63
            ->orderBy('group')
64
            ->orderBy('name')
65
            ->get();
66
67
        $activeChannels = NotificationChannel::where('is_active', true)
68
            ->orderBy('title')
69
            ->get();
70
71
        $groupedEvents = $events->groupBy('group');
72
73
        $sections = [];
74
75
        foreach ($groupedEvents as $group => $groupEvents) {
76
            $fields = [];
77
78
            foreach ($groupEvents as $event) {
79
                $channelCheckboxes = [];
80
81
                foreach ($activeChannels as $channel) {
82
                    $channelCheckboxes[] = Checkbox::make("event_{$event->id}.{$channel->type}")
83
                        ->label($channel->title)
84
                        ->inline(false);
85
                }
86
87
                $fields[] = Grid::make([
88
                    'default' => 1,
89
                    'md' => $activeChannels->count() + 1,
90
                ])
91
                    ->schema([
92
                        Forms\Components\Placeholder::make("event_label_{$event->id}")
93
                            ->label($event->name)
94
                            ->content($event->description ?: '')
95
                            ->extraAttributes([
96
                                'class' => 'font-medium text-gray-900'
97
                            ]),
98
                        ...$channelCheckboxes,
99
                    ])
100
                    ->extraAttributes([
101
                        'class' => 'border-b border-gray-200 py-4 first:pt-0 last:border-b-0 last:pb-0'
102
                    ]);
103
            }
104
105
            $sections[] = Section::make($group ?: 'General')
106
                ->description('Configure which channels should be used for each event. These are the default channels that will be used when sending notifications for these events.')
107
                ->schema([
108
                    ...$fields,
109
                ])
110
                ->collapsible()
111
                ->collapsed(false);
112
        }
113
114
        return $sections;
115
    }
116
117
    public function save(): void
118
    {
119
        $data = $this->form->getState();
0 ignored issues
show
Bug Best Practice introduced by
The property form does not exist on Usamamuneerchaudhary\Not...entChannelConfiguration. Since you implemented __get, consider adding a @property annotation.
Loading history...
120
        $activeChannels = NotificationChannel::where('is_active', true)
121
            ->pluck('type')
122
            ->toArray();
123
124
        $updated = 0;
125
126
        foreach ($data as $key => $channels) {
127
            if (!str_starts_with($key, 'event_')) {
128
                continue;
129
            }
130
131
            $eventId = (int) str_replace('event_', '', $key);
132
            $event = NotificationEvent::find($eventId);
133
134
            if (!$event) {
135
                continue;
136
            }
137
138
            $enabledChannels = [];
139
            foreach ($channels as $channelType => $enabled) {
140
                if ($enabled && in_array($channelType, $activeChannels)) {
141
                    $enabledChannels[] = $channelType;
142
                }
143
            }
144
145
            $settings = $event->settings ?? [];
0 ignored issues
show
The property settings does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
146
            $settings['channels'] = $enabledChannels;
147
148
            $event->settings = $settings;
149
            $event->save();
150
151
            $updated++;
152
        }
153
154
        Notification::make()
155
            ->title('Configuration Saved')
156
            ->body("Successfully updated channel configuration for {$updated} event(s).")
157
            ->success()
158
            ->send();
159
    }
160
}
161
162