forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneralPage.php
More file actions
167 lines (148 loc) · 7.1 KB
/
GeneralPage.php
File metadata and controls
167 lines (148 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace App\Filament\Pages\Settings;
use App\Actions\GetOoklaSpeedtestServers;
use App\Helpers\TimeZoneHelper;
use App\Rules\Cron;
use App\Settings\GeneralSettings;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Pages\SettingsPage;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\HtmlString;
class GeneralPage extends SettingsPage
{
protected static ?string $navigationIcon = 'heroicon-o-cog';
protected static ?string $navigationGroup = 'Settings';
protected static ?int $navigationSort = 1;
protected static ?string $title = 'General';
protected static ?string $navigationLabel = 'General';
protected static string $settings = GeneralSettings::class;
public static function canAccess(): bool
{
return auth()->user()->is_admin;
}
public static function shouldRegisterNavigation(): bool
{
return auth()->user()->is_admin;
}
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Grid::make([
'default' => 1,
])
->schema([
Forms\Components\Section::make('Site Settings')
->schema([
Forms\Components\TextInput::make('site_name')
->disabled()
->helperText(new HtmlString('⚠️ DEPRECATED: Use <code>APP_NAME</code> environment variable.'))
->columnSpanFull(),
Forms\Components\Toggle::make('public_dashboard_enabled')
->label('Public dashboard')
->disabled()
->helperText(new HtmlString('⚠️ DEPRECATED: Use <code>PUBLIC_DASHBOARD</code> environment variable.')),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),
Forms\Components\Section::make('Time Zone Settings')
->schema([
Forms\Components\Select::make('timezone')
->label('Time zone')
->hint(new HtmlString('🔗<a href="https://docs.speedtest-tracker.dev/" target="_blank" rel="nofollow">Docs</a>'))
->options(TimeZoneHelper::list())
->searchable()
->required(),
Forms\Components\TextInput::make('time_format')
->hint(new HtmlString('🔗<a href="https://www.php.net/manual/en/datetime.format.php" target="_blank" rel="nofollow">DateTime Format</a>'))
->placeholder('M j, Y G:i:s')
->maxLength(25)
->required(),
Forms\Components\Toggle::make('db_has_timezone')
->label('Database has time zone')
->helperText(new HtmlString('Enable if your database <strong>has</strong> a time zone already set.')),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),
Forms\Components\Section::make('Speedtest Settings')
->schema([
Forms\Components\TextInput::make('speedtest_schedule')
->rules([new Cron()])
->helperText('Leave empty to disable scheduled tests.')
->hint(new HtmlString('🔗<a href="https://crontab.guru" target="_blank" rel="nofollow">Cron Generator</a>'))
->nullable()
->columnSpan(1),
Forms\Components\TextInput::make('prune_results_older_than')
->helperText('Set to zero to disable pruning.')
->suffix('days')
->numeric()
->required()
->minValue(0)
->maxValue(9999)
->columnSpan(1),
Forms\Components\Select::make('speedtest_server')
->label('Speedtest servers')
->helperText('Leave empty to let the system pick the best server.')
->maxItems(10)
->multiple()
->nullable()
->searchable()
->options(GetOoklaSpeedtestServers::run())
->getSearchResultsUsing(fn (string $search): array => $this->getServerSearchOptions($search))
->getOptionLabelsUsing(fn (array $values): array => $this->getServerLabels($values))
->columnSpanFull(),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),
])
->columnSpan('full'),
]);
}
protected function getServerLabels(array $values): array
{
if (count($values) && is_null($values[0])) {
return [];
}
return collect($values)->mapWithKeys(function (string $item, int $key) {
return [$item => $item];
})->toArray();
}
protected function getServerSearchOptions(string $search): array
{
$response = Http::get(
url: 'https://www.speedtest.net/api/js/servers',
query: [
'engine' => 'js',
'search' => $search,
'https_functional' => true,
'limit' => 20,
]
);
if ($response->failed()) {
return [
'' => 'There was an error retrieving Speedtest servers',
];
}
if (! $response->collect()->count() && is_numeric($search)) {
return collect([
[
'id' => $search,
'name' => $search.' (Manually entered server)',
],
])->pluck('name', 'id')->toArray();
}
return $response->collect()->mapWithKeys(function (array $item, int $key) {
return [$item['id'] => $item['id'].': '.$item['name'].' ('.$item['sponsor'].')'];
})->toArray();
}
}