mirror of
https://github.com/pelican-dev/panel.git
synced 2025-08-03 16:22:13 +02:00
Improve schedule form (#1514)
This commit is contained in:
parent
ae27b179fe
commit
d165da20ec
53
app/Filament/Components/Forms/Actions/CronPresetAction.php
Normal file
53
app/Filament/Components/Forms/Actions/CronPresetAction.php
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Components\Forms\Actions;
|
||||||
|
|
||||||
|
use Filament\Forms\Components\Actions\Action;
|
||||||
|
use Filament\Forms\Get;
|
||||||
|
use Filament\Forms\Set;
|
||||||
|
|
||||||
|
class CronPresetAction extends Action
|
||||||
|
{
|
||||||
|
protected string $minute = '0';
|
||||||
|
|
||||||
|
protected string $hour = '0';
|
||||||
|
|
||||||
|
protected string $dayOfMonth = '*';
|
||||||
|
|
||||||
|
protected string $month = '*';
|
||||||
|
|
||||||
|
protected string $dayOfWeek = '*';
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->disabled(fn (string $operation) => $operation === 'view');
|
||||||
|
|
||||||
|
$this->color(fn (Get $get) => $get('cron_minute') == $this->minute &&
|
||||||
|
$get('cron_hour') == $this->hour &&
|
||||||
|
$get('cron_day_of_month') == $this->dayOfMonth &&
|
||||||
|
$get('cron_month') == $this->month &&
|
||||||
|
$get('cron_day_of_week') == $this->dayOfWeek
|
||||||
|
? 'success' : 'primary');
|
||||||
|
|
||||||
|
$this->action(function (Set $set) {
|
||||||
|
$set('cron_minute', $this->minute);
|
||||||
|
$set('cron_hour', $this->hour);
|
||||||
|
$set('cron_day_of_month', $this->dayOfMonth);
|
||||||
|
$set('cron_month', $this->month);
|
||||||
|
$set('cron_day_of_week', $this->dayOfWeek);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cron(string $minute, string $hour, string $dayOfMonth, string $month, string $dayOfWeek): static
|
||||||
|
{
|
||||||
|
$this->minute = $minute;
|
||||||
|
$this->hour = $hour;
|
||||||
|
$this->dayOfMonth = $dayOfMonth;
|
||||||
|
$this->month = $month;
|
||||||
|
$this->dayOfWeek = $dayOfWeek;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
namespace App\Filament\Server\Resources;
|
namespace App\Filament\Server\Resources;
|
||||||
|
|
||||||
use App\Facades\Activity;
|
use App\Facades\Activity;
|
||||||
|
use App\Filament\Components\Forms\Actions\CronPresetAction;
|
||||||
use App\Filament\Components\Tables\Columns\DateTimeColumn;
|
use App\Filament\Components\Tables\Columns\DateTimeColumn;
|
||||||
use App\Filament\Server\Resources\ScheduleResource\Pages;
|
use App\Filament\Server\Resources\ScheduleResource\Pages;
|
||||||
use App\Filament\Server\Resources\ScheduleResource\RelationManagers\TasksRelationManager;
|
use App\Filament\Server\Resources\ScheduleResource\RelationManagers\TasksRelationManager;
|
||||||
@ -18,13 +19,14 @@ use Carbon\Carbon;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use Filament\Facades\Filament;
|
use Filament\Facades\Filament;
|
||||||
use Filament\Forms\Components\Actions;
|
use Filament\Forms\Components\Actions;
|
||||||
use Filament\Forms\Components\Actions\Action;
|
use Filament\Forms\Components\Group;
|
||||||
use Filament\Forms\Components\Section;
|
use Filament\Forms\Components\Section;
|
||||||
use Filament\Forms\Components\Select;
|
use Filament\Forms\Components\Select;
|
||||||
use Filament\Forms\Components\TextInput;
|
use Filament\Forms\Components\TextInput;
|
||||||
use Filament\Forms\Components\Toggle;
|
use Filament\Forms\Components\Toggle;
|
||||||
use Filament\Forms\Components\ToggleButtons;
|
use Filament\Forms\Components\ToggleButtons;
|
||||||
use Filament\Forms\Form;
|
use Filament\Forms\Form;
|
||||||
|
use Filament\Forms\Get;
|
||||||
use Filament\Forms\Set;
|
use Filament\Forms\Set;
|
||||||
use Filament\Notifications\Notification;
|
use Filament\Notifications\Notification;
|
||||||
use Filament\Resources\Pages\PageRegistration;
|
use Filament\Resources\Pages\PageRegistration;
|
||||||
@ -38,6 +40,7 @@ use Filament\Tables\Columns\IconColumn;
|
|||||||
use Filament\Tables\Columns\TextColumn;
|
use Filament\Tables\Columns\TextColumn;
|
||||||
use Filament\Tables\Table;
|
use Filament\Tables\Table;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\HtmlString;
|
||||||
|
|
||||||
class ScheduleResource extends Resource
|
class ScheduleResource extends Resource
|
||||||
{
|
{
|
||||||
@ -77,20 +80,31 @@ class ScheduleResource extends Resource
|
|||||||
{
|
{
|
||||||
return $form
|
return $form
|
||||||
->columns([
|
->columns([
|
||||||
'default' => 4,
|
'default' => 1,
|
||||||
'lg' => 5,
|
'lg' => 2,
|
||||||
])
|
])
|
||||||
->schema([
|
->schema([
|
||||||
TextInput::make('name')
|
TextInput::make('name')
|
||||||
->columnSpan([
|
->columnSpanFull()
|
||||||
'default' => 4,
|
|
||||||
'md' => 3,
|
|
||||||
'lg' => 4,
|
|
||||||
])
|
|
||||||
->label('Schedule Name')
|
->label('Schedule Name')
|
||||||
->placeholder('A human readable identifier for this schedule.')
|
->placeholder('A human readable identifier for this schedule.')
|
||||||
->autocomplete(false)
|
->autocomplete(false)
|
||||||
->required(),
|
->required(),
|
||||||
|
Toggle::make('only_when_online')
|
||||||
|
->label('Only when Server is Online?')
|
||||||
|
->hintIconTooltip('Only execute this schedule when the server is in a running state.')
|
||||||
|
->hintIcon('tabler-question-mark')
|
||||||
|
->inline(false)
|
||||||
|
->required()
|
||||||
|
->default(1),
|
||||||
|
Toggle::make('is_active')
|
||||||
|
->label('Enable Schedule?')
|
||||||
|
->hintIconTooltip('This schedule will be executed automatically if enabled.')
|
||||||
|
->hintIcon('tabler-question-mark')
|
||||||
|
->inline(false)
|
||||||
|
->hiddenOn('view')
|
||||||
|
->required()
|
||||||
|
->default(1),
|
||||||
ToggleButtons::make('Status')
|
ToggleButtons::make('Status')
|
||||||
->formatStateUsing(fn (Schedule $schedule) => !$schedule->is_active ? 'inactive' : ($schedule->is_processing ? 'processing' : 'active'))
|
->formatStateUsing(fn (Schedule $schedule) => !$schedule->is_active ? 'inactive' : ($schedule->is_processing ? 'processing' : 'active'))
|
||||||
->options(fn (Schedule $schedule) => !$schedule->is_active ? ['inactive' => 'Inactive'] : ($schedule->is_processing ? ['processing' => 'Processing'] : ['active' => 'Active']))
|
->options(fn (Schedule $schedule) => !$schedule->is_active ? ['inactive' => 'Inactive'] : ($schedule->is_processing ? ['processing' => 'Processing'] : ['active' => 'Active']))
|
||||||
@ -99,117 +113,29 @@ class ScheduleResource extends Resource
|
|||||||
'processing' => 'warning',
|
'processing' => 'warning',
|
||||||
'active' => 'success',
|
'active' => 'success',
|
||||||
])
|
])
|
||||||
->visibleOn('view')
|
->visibleOn('view'),
|
||||||
->columnSpan([
|
Section::make('Cron')
|
||||||
'default' => 4,
|
->description(fn (Get $get) => new HtmlString('Please keep in mind that the cron inputs below always assume UTC.<br>Next run in your timezone (' . auth()->user()->timezone . '): <b>'. Utilities::getScheduleNextRunDate($get('cron_minute'), $get('cron_hour'), $get('cron_day_of_month'), $get('cron_month'), $get('cron_day_of_week'))->timezone(auth()->user()->timezone) . '</b>'))
|
||||||
'md' => 1,
|
|
||||||
'lg' => 1,
|
|
||||||
]),
|
|
||||||
Toggle::make('only_when_online')
|
|
||||||
->label('Only when Server is Online?')
|
|
||||||
->hintIconTooltip('Only execute this schedule when the server is in a running state.')
|
|
||||||
->hintIcon('tabler-question-mark')
|
|
||||||
->inline(false)
|
|
||||||
->columnSpan([
|
|
||||||
'default' => 2,
|
|
||||||
'lg' => 3,
|
|
||||||
])
|
|
||||||
->required()
|
|
||||||
->default(1),
|
|
||||||
Toggle::make('is_active')
|
|
||||||
->label('Enable Schedule?')
|
|
||||||
->hintIconTooltip('This schedule will be executed automatically if enabled.')
|
|
||||||
->hintIcon('tabler-question-mark')
|
|
||||||
->inline(false)
|
|
||||||
->columnSpan([
|
|
||||||
'default' => 2,
|
|
||||||
'lg' => 2,
|
|
||||||
])
|
|
||||||
->required()
|
|
||||||
->default(1),
|
|
||||||
TextInput::make('cron_minute')
|
|
||||||
->columnSpan([
|
|
||||||
'default' => 2,
|
|
||||||
'lg' => 1,
|
|
||||||
])
|
|
||||||
->label('Minute')
|
|
||||||
->default('*/5')
|
|
||||||
->required(),
|
|
||||||
TextInput::make('cron_hour')
|
|
||||||
->columnSpan([
|
|
||||||
'default' => 2,
|
|
||||||
'lg' => 1,
|
|
||||||
])
|
|
||||||
->label('Hour')
|
|
||||||
->default('*')
|
|
||||||
->required(),
|
|
||||||
TextInput::make('cron_day_of_month')
|
|
||||||
->columnSpan([
|
|
||||||
'default' => 2,
|
|
||||||
'lg' => 1,
|
|
||||||
])
|
|
||||||
->label('Day of Month')
|
|
||||||
->default('*')
|
|
||||||
->required(),
|
|
||||||
TextInput::make('cron_month')
|
|
||||||
->columnSpan([
|
|
||||||
'default' => 2,
|
|
||||||
'lg' => 1,
|
|
||||||
])
|
|
||||||
->label('Month')
|
|
||||||
->default('*')
|
|
||||||
->required(),
|
|
||||||
TextInput::make('cron_day_of_week')
|
|
||||||
->columnSpan([
|
|
||||||
'default' => 2,
|
|
||||||
'lg' => 1,
|
|
||||||
])
|
|
||||||
->label('Day of Week')
|
|
||||||
->default('*')
|
|
||||||
->required(),
|
|
||||||
Section::make('Presets')
|
|
||||||
->hiddenOn('view')
|
|
||||||
->columns(1)
|
|
||||||
->schema([
|
->schema([
|
||||||
Actions::make([
|
Actions::make([
|
||||||
Action::make('hourly')
|
CronPresetAction::make('hourly')
|
||||||
->disabled(fn (string $operation) => $operation === 'view')
|
->cron('0', '*', '*', '*', '*'),
|
||||||
->action(function (Set $set) {
|
CronPresetAction::make('daily')
|
||||||
$set('cron_minute', '0');
|
->cron('0', '0', '*', '*', '*'),
|
||||||
$set('cron_hour', '*');
|
CronPresetAction::make('weekly_monday')
|
||||||
$set('cron_day_of_month', '*');
|
->label('Weekly (Monday)')
|
||||||
$set('cron_month', '*');
|
->cron('0', '0', '*', '*', '1'),
|
||||||
$set('cron_day_of_week', '*');
|
CronPresetAction::make('weekly_sunday')
|
||||||
}),
|
->label('Weekly (Sunday)')
|
||||||
Action::make('daily')
|
->cron('0', '0', '*', '*', '0'),
|
||||||
->disabled(fn (string $operation) => $operation === 'view')
|
CronPresetAction::make('monthly')
|
||||||
->action(function (Set $set) {
|
->cron('0', '0', '1', '*', '*'),
|
||||||
$set('cron_minute', '0');
|
CronPresetAction::make('every_x_minutes')
|
||||||
$set('cron_hour', '0');
|
->color(fn (Get $get) => str($get('cron_minute'))->startsWith('*/')
|
||||||
$set('cron_day_of_month', '*');
|
&& $get('cron_hour') == '*'
|
||||||
$set('cron_month', '*');
|
&& $get('cron_day_of_month') == '*'
|
||||||
$set('cron_day_of_week', '*');
|
&& $get('cron_month') == '*'
|
||||||
}),
|
&& $get('cron_day_of_week') == '*' ? 'success' : 'primary')
|
||||||
Action::make('weekly')
|
|
||||||
->disabled(fn (string $operation) => $operation === 'view')
|
|
||||||
->action(function (Set $set) {
|
|
||||||
$set('cron_minute', '0');
|
|
||||||
$set('cron_hour', '0');
|
|
||||||
$set('cron_day_of_month', '*');
|
|
||||||
$set('cron_month', '*');
|
|
||||||
$set('cron_day_of_week', '0');
|
|
||||||
}),
|
|
||||||
Action::make('monthly')
|
|
||||||
->disabled(fn (string $operation) => $operation === 'view')
|
|
||||||
->action(function (Set $set) {
|
|
||||||
$set('cron_minute', '0');
|
|
||||||
$set('cron_hour', '0');
|
|
||||||
$set('cron_day_of_month', '1');
|
|
||||||
$set('cron_month', '*');
|
|
||||||
$set('cron_day_of_week', '0');
|
|
||||||
}),
|
|
||||||
Action::make('every_x_minutes')
|
|
||||||
->disabled(fn (string $operation) => $operation === 'view')
|
|
||||||
->form([
|
->form([
|
||||||
TextInput::make('x')
|
TextInput::make('x')
|
||||||
->label('')
|
->label('')
|
||||||
@ -226,8 +152,12 @@ class ScheduleResource extends Resource
|
|||||||
$set('cron_month', '*');
|
$set('cron_month', '*');
|
||||||
$set('cron_day_of_week', '*');
|
$set('cron_day_of_week', '*');
|
||||||
}),
|
}),
|
||||||
Action::make('every_x_hours')
|
CronPresetAction::make('every_x_hours')
|
||||||
->disabled(fn (string $operation) => $operation === 'view')
|
->color(fn (Get $get) => $get('cron_minute') == '0'
|
||||||
|
&& str($get('cron_hour'))->startsWith('*/')
|
||||||
|
&& $get('cron_day_of_month') == '*'
|
||||||
|
&& $get('cron_month') == '*'
|
||||||
|
&& $get('cron_day_of_week') == '*' ? 'success' : 'primary')
|
||||||
->form([
|
->form([
|
||||||
TextInput::make('x')
|
TextInput::make('x')
|
||||||
->label('')
|
->label('')
|
||||||
@ -244,8 +174,12 @@ class ScheduleResource extends Resource
|
|||||||
$set('cron_month', '*');
|
$set('cron_month', '*');
|
||||||
$set('cron_day_of_week', '*');
|
$set('cron_day_of_week', '*');
|
||||||
}),
|
}),
|
||||||
Action::make('every_x_days')
|
CronPresetAction::make('every_x_days')
|
||||||
->disabled(fn (string $operation) => $operation === 'view')
|
->color(fn (Get $get) => $get('cron_minute') == '0'
|
||||||
|
&& $get('cron_hour') == '0'
|
||||||
|
&& str($get('cron_day_of_month'))->startsWith('*/')
|
||||||
|
&& $get('cron_month') == '*'
|
||||||
|
&& $get('cron_day_of_week') == '*' ? 'success' : 'primary')
|
||||||
->form([
|
->form([
|
||||||
TextInput::make('x')
|
TextInput::make('x')
|
||||||
->label('')
|
->label('')
|
||||||
@ -262,8 +196,12 @@ class ScheduleResource extends Resource
|
|||||||
$set('cron_month', '*');
|
$set('cron_month', '*');
|
||||||
$set('cron_day_of_week', '*');
|
$set('cron_day_of_week', '*');
|
||||||
}),
|
}),
|
||||||
Action::make('every_x_months')
|
CronPresetAction::make('every_x_months')
|
||||||
->disabled(fn (string $operation) => $operation === 'view')
|
->color(fn (Get $get) => $get('cron_minute') == '0'
|
||||||
|
&& $get('cron_hour') == '0'
|
||||||
|
&& $get('cron_day_of_month') == '1'
|
||||||
|
&& str($get('cron_month'))->startsWith('*/')
|
||||||
|
&& $get('cron_day_of_week') == '*' ? 'success' : 'primary')
|
||||||
->form([
|
->form([
|
||||||
TextInput::make('x')
|
TextInput::make('x')
|
||||||
->label('')
|
->label('')
|
||||||
@ -280,8 +218,12 @@ class ScheduleResource extends Resource
|
|||||||
$set('cron_month', '*/' . $data['x']);
|
$set('cron_month', '*/' . $data['x']);
|
||||||
$set('cron_day_of_week', '*');
|
$set('cron_day_of_week', '*');
|
||||||
}),
|
}),
|
||||||
Action::make('every_x_day_of_week')
|
CronPresetAction::make('every_x_day_of_week')
|
||||||
->disabled(fn (string $operation) => $operation === 'view')
|
->color(fn (Get $get) => $get('cron_minute') == '0'
|
||||||
|
&& $get('cron_hour') == '0'
|
||||||
|
&& $get('cron_day_of_month') == '*'
|
||||||
|
&& $get('cron_month') == '*'
|
||||||
|
&& $get('cron_day_of_week') != '*' ? 'success' : 'primary')
|
||||||
->form([
|
->form([
|
||||||
Select::make('x')
|
Select::make('x')
|
||||||
->label('')
|
->label('')
|
||||||
@ -305,6 +247,58 @@ class ScheduleResource extends Resource
|
|||||||
$set('cron_month', '*');
|
$set('cron_month', '*');
|
||||||
$set('cron_day_of_week', $data['x']);
|
$set('cron_day_of_week', $data['x']);
|
||||||
}),
|
}),
|
||||||
|
])
|
||||||
|
->hiddenOn('view'),
|
||||||
|
Group::make([
|
||||||
|
TextInput::make('cron_minute')
|
||||||
|
->columnSpan([
|
||||||
|
'default' => 2,
|
||||||
|
'lg' => 1,
|
||||||
|
])
|
||||||
|
->label('Minute')
|
||||||
|
->default('*/5')
|
||||||
|
->required()
|
||||||
|
->live(),
|
||||||
|
TextInput::make('cron_hour')
|
||||||
|
->columnSpan([
|
||||||
|
'default' => 2,
|
||||||
|
'lg' => 1,
|
||||||
|
])
|
||||||
|
->label('Hour')
|
||||||
|
->default('*')
|
||||||
|
->required()
|
||||||
|
->live(),
|
||||||
|
TextInput::make('cron_day_of_month')
|
||||||
|
->columnSpan([
|
||||||
|
'default' => 2,
|
||||||
|
'lg' => 1,
|
||||||
|
])
|
||||||
|
->label('Day of Month')
|
||||||
|
->default('*')
|
||||||
|
->required()
|
||||||
|
->live(),
|
||||||
|
TextInput::make('cron_month')
|
||||||
|
->columnSpan([
|
||||||
|
'default' => 2,
|
||||||
|
'lg' => 1,
|
||||||
|
])
|
||||||
|
->label('Month')
|
||||||
|
->default('*')
|
||||||
|
->required()
|
||||||
|
->live(),
|
||||||
|
TextInput::make('cron_day_of_week')
|
||||||
|
->columnSpan([
|
||||||
|
'default' => 2,
|
||||||
|
'lg' => 1,
|
||||||
|
])
|
||||||
|
->label('Day of Week')
|
||||||
|
->default('*')
|
||||||
|
->required()
|
||||||
|
->live(),
|
||||||
|
])
|
||||||
|
->columns([
|
||||||
|
'default' => 4,
|
||||||
|
'lg' => 5,
|
||||||
]),
|
]),
|
||||||
]),
|
]),
|
||||||
]);
|
]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user