|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Pages; |
| 4 | + |
| 5 | +use Filament\Schemas\Schema; |
| 6 | +use Filament\Schemas\Components\Section; |
| 7 | +use App\Enums\UserRole; |
| 8 | +use Filament\Forms\Components\Toggle; |
| 9 | +use Filament\Forms\Components\Select; |
| 10 | +use Filament\Forms\Components\ColorPicker; |
| 11 | +use Filament\Forms\Components\TextInput; |
| 12 | +use Filament\Forms\Components\TagsInput; |
| 13 | +use Filament\Forms\Components\Placeholder; |
| 14 | +use Filament\Forms\Components\Actions\Action; |
| 15 | +use App\Settings\WidgetSettings; |
| 16 | +use Filament\Pages\SettingsPage; |
| 17 | +use Illuminate\Contracts\Support\Htmlable; |
| 18 | +use Illuminate\Support\HtmlString; |
| 19 | + |
| 20 | +class Widget extends SettingsPage |
| 21 | +{ |
| 22 | + protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-code-bracket'; |
| 23 | + |
| 24 | + protected static string $settings = WidgetSettings::class; |
| 25 | + |
| 26 | + protected static ?int $navigationSort = 1500; |
| 27 | + |
| 28 | + public static function getNavigationGroup(): ?string |
| 29 | + { |
| 30 | + return trans('nav.manage'); |
| 31 | + } |
| 32 | + |
| 33 | + public static function getNavigationLabel(): string |
| 34 | + { |
| 35 | + return 'Widget'; |
| 36 | + } |
| 37 | + |
| 38 | + public function getHeading(): string|Htmlable |
| 39 | + { |
| 40 | + return 'Feedback Widget'; |
| 41 | + } |
| 42 | + |
| 43 | + public static function shouldRegisterNavigation(): bool |
| 44 | + { |
| 45 | + return auth()->user()->hasRole(UserRole::Admin); |
| 46 | + } |
| 47 | + |
| 48 | + public function mount(): void |
| 49 | + { |
| 50 | + parent::mount(); |
| 51 | + |
| 52 | + abort_unless(auth()->user()->hasRole(UserRole::Admin), 403); |
| 53 | + } |
| 54 | + |
| 55 | + public function form(Schema $schema): Schema |
| 56 | + { |
| 57 | + return $schema->components( |
| 58 | + [ |
| 59 | + Section::make('Widget Configuration') |
| 60 | + ->description('Configure the feedback widget that can be embedded on external websites.') |
| 61 | + ->columnSpanFull() |
| 62 | + ->schema( |
| 63 | + [ |
| 64 | + Toggle::make('enabled') |
| 65 | + ->label('Enable Widget') |
| 66 | + ->helperText('Enable or disable the feedback widget'), |
| 67 | + |
| 68 | + Select::make('position') |
| 69 | + ->label('Widget Position') |
| 70 | + ->options([ |
| 71 | + 'bottom-right' => 'Bottom Right', |
| 72 | + 'bottom-left' => 'Bottom Left', |
| 73 | + 'top-right' => 'Top Right', |
| 74 | + 'top-left' => 'Top Left', |
| 75 | + ]) |
| 76 | + ->default('bottom-right') |
| 77 | + ->required(), |
| 78 | + |
| 79 | + ColorPicker::make('primary_color') |
| 80 | + ->label('Primary Color') |
| 81 | + ->default('#2563EB'), |
| 82 | + |
| 83 | + TextInput::make('button_text') |
| 84 | + ->label('Button Text') |
| 85 | + ->default('Feedback') |
| 86 | + ->required(), |
| 87 | + |
| 88 | + Toggle::make('hide_button') |
| 89 | + ->label('Hide Button') |
| 90 | + ->helperText('Hide the default floating button. Use $roadmap.open() to open the modal programmatically.'), |
| 91 | + |
| 92 | + TagsInput::make('allowed_domains') |
| 93 | + ->label('Allowed Domains') |
| 94 | + ->helperText('Restrict widget usage to specific domains (e.g., example.com). Leave empty to allow all domains.') |
| 95 | + ->placeholder('example.com'), |
| 96 | + ] |
| 97 | + )->columns(), |
| 98 | + |
| 99 | + Section::make('Embed Code') |
| 100 | + ->description('Copy and paste this code into your website to enable the feedback widget.') |
| 101 | + ->columnSpanFull() |
| 102 | + ->schema( |
| 103 | + [ |
| 104 | + Placeholder::make('embed_code') |
| 105 | + ->label('JavaScript Embed Code') |
| 106 | + ->content(fn () => new HtmlString( |
| 107 | + '<div class="relative"> |
| 108 | + <pre id="widget-embed-code" class="bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-gray-100 p-4 rounded-md overflow-x-auto text-sm leading-relaxed font-mono">' . |
| 109 | + htmlspecialchars($this->getEmbedCode()) . |
| 110 | + '</pre> |
| 111 | + <button |
| 112 | + type="button" |
| 113 | + onclick=" |
| 114 | + const code = document.getElementById(\'widget-embed-code\').textContent; |
| 115 | + navigator.clipboard.writeText(code).then(() => { |
| 116 | + this.textContent = \'Copied!\'; |
| 117 | + setTimeout(() => { this.innerHTML = \'<svg class=\\\'w-4 h-4 mr-1\\\' fill=\\\'none\\\' stroke=\\\'currentColor\\\' viewBox=\\\'0 0 24 24\\\'><path stroke-linecap=\\\'round\\\' stroke-linejoin=\\\'round\\\' stroke-width=\\\'2\\\' d=\\\'M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z\\\'></path></svg> Copy\'; }, 2000); |
| 118 | + }); |
| 119 | + " |
| 120 | + class="absolute top-4 right-2 px-3 py-1.5 bg-transparent border-0 rounded-md text-sm font-medium text-blue-600 dark:text-blue-400 hover:underline transition-all flex items-center gap-1 cursor-pointer" |
| 121 | + > |
| 122 | + <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 123 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path> |
| 124 | + </svg> |
| 125 | + Copy |
| 126 | + </button> |
| 127 | + </div>' |
| 128 | + )) |
| 129 | + ->helperText('Click the copy button to copy this code and paste it before the closing </body> tag on your website.'), |
| 130 | + ] |
| 131 | + ), |
| 132 | + ] |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + protected function getEmbedCode(): string |
| 137 | + { |
| 138 | + $url = url('/widget.js'); |
| 139 | + return <<<HTML |
| 140 | +<script> |
| 141 | + (function() { |
| 142 | + const script = document.createElement('script'); |
| 143 | + script.src = '{$url}'; |
| 144 | + script.async = true; |
| 145 | + document.body.appendChild(script); |
| 146 | +
|
| 147 | + // Optional: Configure widget |
| 148 | + script.onload = function() { |
| 149 | + // Pre-fill user data: |
| 150 | + // \$roadmap.setName('John Doe'); |
| 151 | + // \$roadmap.setEmail('john@example.com'); |
| 152 | +
|
| 153 | + // If you hid the default button, use your own to trigger: |
| 154 | + // document.getElementById('my-feedback-btn').onclick = function() { |
| 155 | + // \$roadmap.open(); |
| 156 | + // }; |
| 157 | + }; |
| 158 | + })(); |
| 159 | +</script> |
| 160 | +HTML; |
| 161 | + } |
| 162 | +} |
0 commit comments