Skip to content

Commit

Permalink
1- Improvement ScheduleResource
Browse files Browse the repository at this point in the history
2- add enable_custom' in config
  • Loading branch information
husam-tariq committed Mar 16, 2023
1 parent 2de7b1d commit 4094733
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 216 deletions.
1 change: 1 addition & 0 deletions config/filament-database-schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*/
'commands' => [

'enable_custom' => true,
/**
* By default, all commands possible to be used with "php artisan" will be shown, this parameter excludes from
* the list commands that you do not want to show for the schedule.
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ private function dispatch($task)
//ensure output is being captured to write history
$event->storeOutput();

if ($task->environments) {
$event->environments(explode(',', $task->environments));
if (!empty($task->environments)) {
$event->environments($task->environments);
}

if ($task->even_in_maintenance_mode) {
Expand Down
112 changes: 97 additions & 15 deletions src/Filament/Resources/ScheduleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace HusamTariq\FilamentDatabaseSchedule\Filament\Resources;

use Awcodes\FilamentTableRepeater\Components\TableRepeater;
use Carbon\Carbon;
use Closure;
use HusamTariq\FilamentDatabaseSchedule\Filament\Resources\ScheduleResource\Pages;
use HusamTariq\FilamentDatabaseSchedule\Models\Schedule;
use Filament\Forms;
use Filament\Forms\Components\Card;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
Expand All @@ -15,14 +18,19 @@
use HusamTariq\FilamentDatabaseSchedule\Filament\Columns\ScheduleArguments;
use HusamTariq\FilamentDatabaseSchedule\Filament\Columns\ScheduleOptions;
use HusamTariq\FilamentDatabaseSchedule\Http\Services\CommandService;
use HusamTariq\FilamentDatabaseSchedule\Rules\Corn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;

class ScheduleResource extends Resource
{

protected static ?string $navigationIcon = 'heroicon-o-collection';

public static Collection $commands;


public static function getModel(): string
{
Expand All @@ -46,14 +54,89 @@ protected static function getNavigationGroup(): ?string

public static function getSlug(): string
{
return config('filament-database-schedule.route_slug');
return config('filament-database-schedule.route_slug');
}

public static function form(Form $form): Form
{
static::$commands = CommandService::get();
return $form
->columns(2)
->schema([]);
->schema([
Card::make([
Forms\Components\Select::make('command')->label(__('filament-database-schedule::schedule.fields.command'))
->options(
fn () =>
config('filament-database-schedule.commands.enable_custom') ?
static::$commands->pluck('full_name', 'name')->prepend(__('filament-database-schedule::schedule.messages.custom'), 'custom') : static::$commands->pluck('full_name', 'name')
)
->reactive()
->searchable()
->required()
->afterStateUpdated(function (Closure $set, $state) {
$set('params', static::$commands->firstWhere('name', $state)['arguments'] ?? []);
$set('options_with_value', static::$commands->firstWhere('name', $state)['options']["withValue"] ?? []);
}),
Forms\Components\TextInput::make('command_custom')
->placeholder(__('filament-database-schedule::schedule.messages.custom-command-here'))
->label(__('filament-database-schedule::schedule.messages.custom'))
->required()
->visible(fn (Closure $get) => $get('command') === 'custom' && config('filament-database-schedule.commands.enable_custom')),
TableRepeater::make('params')->label(__('filament-database-schedule::schedule.fields.arguments'))
->schema([
Forms\Components\TextInput::make('value')->label(fn (Closure $get) => ucfirst($get('name')))->required(fn (Closure $get) => $get('required')),
Forms\Components\Hidden::make('name'),
])->disableItemCreation()->withoutHeader()->disableItemDeletion()->disableItemMovement()
->columnSpan('full')->visible(fn (Closure $get) => !empty(static::$commands->firstWhere('name', $get('command'))['arguments'])),
TableRepeater::make('options_with_value')->label(__('filament-database-schedule::schedule.fields.options_with_value'))
->schema([
Forms\Components\TextInput::make('value')->label(fn (Closure $get) => ucfirst($get('name')))->required(fn (Closure $get) => $get('required')),
Forms\Components\Hidden::make('type')->default('string'),
Forms\Components\Hidden::make('name'),
])->disableItemCreation()->withoutHeader()->disableItemDeletion()->disableItemMovement()->default([])
->columnSpan('full')->visible(fn ($state) => !empty($state)),
Forms\Components\CheckboxList::make('options')->label(__('filament-database-schedule::schedule.fields.options'))
->options(
fn (Closure $get) =>
collect(static::$commands->firstWhere('name', $get('command'))['options']['withoutValue'] ?? [])
->mapWithKeys(function ($value) {
return [$value => $value];
}),
)->columns(3)->columnSpanFull()->visible(fn (Forms\Components\CheckboxList $component) => !empty($component->getOptions())),
Forms\Components\TextInput::make('expression')
->placeholder('* * * * *')
->rules([new Corn()])
->label(__('filament-database-schedule::schedule.fields.expression'))
->required()->helperText(fn () => config('filament-database-schedule.tool-help-cron-expression.enable') ? new HtmlString(" <a href='" . config('filament-database-schedule.tool-help-cron-expression.url') . "' target='_blank'>" . __('filament-database-schedule::schedule.messages.help-cron-expression') . " </a>") : null),
Forms\Components\TagsInput::make('environments')
->placeholder(null)
->label(__('filament-database-schedule::schedule.fields.environments')),
Forms\Components\TextInput::make('log_filename')
->label(__('filament-database-schedule::schedule.fields.log_filename'))
->helperText(__('filament-database-schedule::schedule.messages.help-log-filename')),
Forms\Components\TextInput::make('webhook_before')
->label(__('filament-database-schedule::schedule.fields.webhook_before')),
Forms\Components\TextInput::make('webhook_after')
->label(__('filament-database-schedule::schedule.fields.webhook_after')),
Forms\Components\TextInput::make('email_output')
->label(__('filament-database-schedule::schedule.fields.email_output')),
Forms\Components\Toggle::make('sendmail_success')
->label(__('filament-database-schedule::schedule.fields.sendmail_success')),
Forms\Components\Toggle::make('sendmail_error')
->label(__('filament-database-schedule::schedule.fields.sendmail_error')),
Forms\Components\Toggle::make('log_success')
->label(__('filament-database-schedule::schedule.fields.log_success'))->default(true),
Forms\Components\Toggle::make('log_error')
->label(__('filament-database-schedule::schedule.fields.log_error'))->default(true),
Forms\Components\Toggle::make('even_in_maintenance_mode')
->label(__('filament-database-schedule::schedule.fields.even_in_maintenance_mode')),
Forms\Components\Toggle::make('without_overlapping')
->label(__('filament-database-schedule::schedule.fields.without_overlapping')),
Forms\Components\Toggle::make('on_one_server')
->label(__('filament-database-schedule::schedule.fields.on_one_server')),
Forms\Components\Toggle::make('run_in_background')
->label(__('filament-database-schedule::schedule.fields.run_in_background')),
])->inlineLabel(false)
]);
}

public static function table(Table $table): Table
Expand Down Expand Up @@ -104,26 +187,25 @@ public static function table(Table $table): Table
])
->actions([
ActionGroup::make([
Tables\Actions\EditAction::make()->hidden(fn($record)=>$record->trashed())->tooltip(__('filament-support::actions/edit.single.label')),
Tables\Actions\EditAction::make()->hidden(fn ($record) => $record->trashed())->tooltip(__('filament-support::actions/edit.single.label')),
Tables\Actions\RestoreAction::make()->tooltip(__('filament-support::actions/restore.single.label')),
Tables\Actions\DeleteAction::make()->tooltip(__('filament-support::actions/delete.single.label')),
Tables\Actions\ForceDeleteAction::make()->tooltip(__('filament-support::actions/force-delete.single.label')),
Tables\Actions\Action::make('toggle')->disabled(fn($record)=>$record->trashed())
->icon(fn ($record) => $record->status == Schedule::STATUS_ACTIVE ? 'schedule-pause-fill' : 'schedule-play-fill')->color(fn ($record) => $record->status == Schedule::STATUS_ACTIVE ? 'warning' : 'success')->action(function ($record): void {
if ($record->status == Schedule::STATUS_ACTIVE)
$record->status = Schedule::STATUS_INACTIVE;
else if ($record->status == Schedule::STATUS_INACTIVE)
$record->status = Schedule::STATUS_ACTIVE;
$record->save();
})->tooltip(fn ($record) => $record->status == Schedule::STATUS_ACTIVE ? __('filament-database-schedule::schedule.buttons.inactivate') : __('filament-database-schedule::schedule.buttons.activate')),
Tables\Actions\ViewAction::make()->icon('schedule-history')->color('gray')->tooltip(__('filament-database-schedule::schedule.buttons.history')) ,
Tables\Actions\Action::make('toggle')->disabled(fn ($record) => $record->trashed())
->icon(fn ($record) => $record->status == Schedule::STATUS_ACTIVE ? 'schedule-pause-fill' : 'schedule-play-fill')->color(fn ($record) => $record->status == Schedule::STATUS_ACTIVE ? 'warning' : 'success')->action(function ($record): void {
if ($record->status == Schedule::STATUS_ACTIVE)
$record->status = Schedule::STATUS_INACTIVE;
else if ($record->status == Schedule::STATUS_INACTIVE)
$record->status = Schedule::STATUS_ACTIVE;
$record->save();
})->tooltip(fn ($record) => $record->status == Schedule::STATUS_ACTIVE ? __('filament-database-schedule::schedule.buttons.inactivate') : __('filament-database-schedule::schedule.buttons.activate')),
Tables\Actions\ViewAction::make()->icon('schedule-history')->color('gray')->tooltip(__('filament-database-schedule::schedule.buttons.history')),
])

])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
])->defaultSort(config('filament-database-schedule.default_ordering'),config('filament-database-schedule.default_ordering_direction'))
;
])->defaultSort(config('filament-database-schedule.default_ordering'), config('filament-database-schedule.default_ordering_direction'));
}

public static function getEloquentQuery(): Builder
Expand Down
106 changes: 7 additions & 99 deletions src/Filament/Resources/ScheduleResource/Pages/CreateSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,28 @@

namespace HusamTariq\FilamentDatabaseSchedule\Filament\Resources\ScheduleResource\Pages;

use Awcodes\FilamentTableRepeater\Components\TableRepeater;
use Closure;
use HusamTariq\FilamentDatabaseSchedule\Filament\Resources\ScheduleResource;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;
use HusamTariq\FilamentDatabaseSchedule\Http\Services\CommandService;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Validation\ValidationException;

class CreateSchedule extends CreateRecord
{

protected static string $resource = ScheduleResource::class;
public Collection $commands;

public function mount(): void
protected function onValidationError(ValidationException $exception): void
{
$this->commands = CommandService::get();
parent::mount();
}

protected function getForms(): array
{
return [
'form' => $this->makeForm()
->context('create')
->model($this->getModel())
->schema($this->getFormSchema())
// ->columns(2)
->statePath('data')
->inlineLabel(false),
];
Notification::make()
->title($exception->getMessage())
->danger()
->send();
}

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}

protected function getFormSchema(): array
{
return [
Forms\Components\Select::make('command')->label(__('filament-database-schedule::schedule.fields.command'))
->options($this->commands->pluck('full_name', 'name')->prepend(__('filament-database-schedule::schedule.messages.custom'), 'custom'))
->reactive()
->searchable()
->required()
->afterStateUpdated(function (Closure $set, $state) {
$set('params', $this->commands->firstWhere('name', $state)['arguments'] ?? []);
$set('options_with_value', $this->commands->firstWhere('name', $state)['options']["withValue"] ?? []);
}),
Forms\Components\TextInput::make('command_custom')
->placeholder(__('filament-database-schedule::schedule.messages.custom-command-here'))
->label(__('filament-database-schedule::schedule.messages.custom'))
->required()
->visible(fn (Closure $get) => $get('command') === 'custom'),

TableRepeater::make('params')->label(__('filament-database-schedule::schedule.fields.arguments'))
->schema([
Forms\Components\TextInput::make('value')->label(fn (Closure $get) => ucfirst($get('name')))->required(fn (Closure $get) => $get('required')),
Forms\Components\Hidden::make('type')->default('string'),
Forms\Components\Hidden::make('name'),
])->disableItemCreation()->withoutHeader()->disableItemDeletion()->disableItemMovement()
->columnSpan('full')->visible(fn (Closure $get) => !empty($this->commands->firstWhere('name', $get('command'))['arguments'])),
TableRepeater::make('options_with_value')->label(__('filament-database-schedule::schedule.fields.options_with_value'))
->schema([
Forms\Components\TextInput::make('value')->label(fn (Closure $get) => ucfirst($get('name')))->required(fn (Closure $get) => $get('required')),
Forms\Components\Hidden::make('type')->default('string'),
Forms\Components\Hidden::make('name'),
])->disableItemCreation()->withoutHeader()->disableItemDeletion()->disableItemMovement()->default([])
->columnSpan('full')->visible(fn ($state) => !empty($state)),
Forms\Components\CheckboxList::make('options')->label(__('filament-database-schedule::schedule.fields.options'))
->options(
fn (Closure $get) =>
collect($this->commands->firstWhere('name', $get('command'))['options']['withoutValue'] ?? [])
->mapWithKeys(function ($value) {
return [$value => $value];
}),
)->columns(3)->columnSpanFull()->visible(fn (Forms\Components\CheckboxList $component) => !empty($component->getOptions())),
Forms\Components\TextInput::make('expression')
->placeholder('* * * * *')
->label(__('filament-database-schedule::schedule.fields.expression'))
->required()->helperText(fn () => config('filament-database-schedule.tool-help-cron-expression.enable') ? new HtmlString(" <a href='" . config('filament-database-schedule.tool-help-cron-expression.url') . "' target='_blank'>" . __('filament-database-schedule::schedule.messages.help-cron-expression') . " </a>") : null),
Forms\Components\TagsInput::make('environments')
->placeholder(null)
->label(__('filament-database-schedule::schedule.fields.environments')),
Forms\Components\TextInput::make('log_filename')
->label(__('filament-database-schedule::schedule.fields.log_filename'))
->helperText(__('filament-database-schedule::schedule.messages.help-log-filename')),
Forms\Components\TextInput::make('webhook_before')
->label(__('filament-database-schedule::schedule.fields.webhook_before')),
Forms\Components\TextInput::make('webhook_after')
->label(__('filament-database-schedule::schedule.fields.webhook_after')),
Forms\Components\TextInput::make('email_output')
->label(__('filament-database-schedule::schedule.fields.email_output')),
Forms\Components\Toggle::make('sendmail_success')
->label(__('filament-database-schedule::schedule.fields.sendmail_success')),
Forms\Components\Toggle::make('sendmail_error')
->label(__('filament-database-schedule::schedule.fields.sendmail_error')),
Forms\Components\Toggle::make('log_success')
->label(__('filament-database-schedule::schedule.fields.log_success'))->default(true),
Forms\Components\Toggle::make('log_error')
->label(__('filament-database-schedule::schedule.fields.log_error'))->default(true),
Forms\Components\Toggle::make('even_in_maintenance_mode')
->label(__('filament-database-schedule::schedule.fields.even_in_maintenance_mode')),
Forms\Components\Toggle::make('without_overlapping')
->label(__('filament-database-schedule::schedule.fields.without_overlapping')),
Forms\Components\Toggle::make('on_one_server')
->label(__('filament-database-schedule::schedule.fields.on_one_server')),
Forms\Components\Toggle::make('run_in_background')
->label(__('filament-database-schedule::schedule.fields.run_in_background')),

];
}


}
Loading

0 comments on commit 4094733

Please sign in to comment.