Skip to content

Commit ccbbdef

Browse files
update
1 parent aab1b81 commit ccbbdef

File tree

7 files changed

+166
-2
lines changed

7 files changed

+166
-2
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Modules\Customer\App\Filament\Resources;
4+
5+
use App\Models\CronJob;
6+
use App\Models\HostingSubscription;
7+
use Modules\Customer\App\Filament\Resources\CronJobResource\Pages;
8+
use Modules\Customer\App\Filament\Resources\CronJobResource\RelationManagers;
9+
use Filament\Forms;
10+
use Filament\Forms\Form;
11+
use Filament\Resources\Resource;
12+
use Filament\Tables;
13+
use Filament\Tables\Table;
14+
use Illuminate\Database\Eloquent\Builder;
15+
use Illuminate\Database\Eloquent\SoftDeletingScope;
16+
17+
class CronJobResource extends Resource
18+
{
19+
protected static ?string $model = CronJob::class;
20+
21+
protected static ?string $navigationIcon = 'heroicon-o-clock';
22+
23+
protected static ?int $navigationSort = 5;
24+
25+
public static function form(Form $form): Form
26+
{
27+
return $form
28+
->schema([
29+
30+
Forms\Components\Select::make('hosting_subscription_id')
31+
->label('Hosting Subscription')
32+
->options(
33+
\App\Models\HostingSubscription::all()->pluck('domain', 'id')
34+
)
35+
->live()
36+
->disabled(function ($record) {
37+
return $record;
38+
})
39+
->columnSpanFull()
40+
->required(),
41+
42+
Forms\Components\TextInput::make('schedule')
43+
->autofocus()
44+
->required()
45+
->columnSpanFull()
46+
->helperText('The schedule to run the command. Example: * * * * *')
47+
->label('Schedule'),
48+
Forms\Components\TextInput::make('command')
49+
->required()
50+
->columnSpanFull()
51+
->helperText('The command to run. Example: ls -la')
52+
->label('Command'),
53+
]);
54+
}
55+
56+
public static function table(Table $table): Table
57+
{
58+
return $table
59+
->columns([
60+
Tables\Columns\TextColumn::make('schedule')
61+
->searchable()
62+
->sortable(),
63+
Tables\Columns\TextColumn::make('command')
64+
->searchable()
65+
->sortable(),
66+
Tables\Columns\TextColumn::make('user')
67+
->searchable()
68+
->sortable(),
69+
])
70+
->filters([
71+
//
72+
])
73+
->actions([
74+
Tables\Actions\EditAction::make(),
75+
])
76+
->bulkActions([
77+
Tables\Actions\BulkActionGroup::make([
78+
Tables\Actions\DeleteBulkAction::make(),
79+
]),
80+
]);
81+
}
82+
83+
public static function getRelations(): array
84+
{
85+
return [
86+
//
87+
];
88+
}
89+
90+
public static function getPages(): array
91+
{
92+
return [
93+
'index' => Pages\ListCronJobs::route('/'),
94+
'create' => Pages\CreateCronJob::route('/create'),
95+
'edit' => Pages\EditCronJob::route('/{record}/edit'),
96+
];
97+
}
98+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Modules\Customer\App\Filament\Resources\CronJobResource\Pages;
4+
5+
use Modules\Customer\App\Filament\Resources\CronJobResource;
6+
use Filament\Actions;
7+
use Filament\Resources\Pages\CreateRecord;
8+
9+
class CreateCronJob extends CreateRecord
10+
{
11+
protected static string $resource = CronJobResource::class;
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Modules\Customer\App\Filament\Resources\CronJobResource\Pages;
4+
5+
use Modules\Customer\App\Filament\Resources\CronJobResource;
6+
use Filament\Actions;
7+
use Filament\Resources\Pages\EditRecord;
8+
9+
class EditCronJob extends EditRecord
10+
{
11+
protected static string $resource = CronJobResource::class;
12+
13+
protected function getHeaderActions(): array
14+
{
15+
return [
16+
Actions\DeleteAction::make(),
17+
];
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Modules\Customer\App\Filament\Resources\CronJobResource\Pages;
4+
5+
use Modules\Customer\App\Filament\Resources\CronJobResource;
6+
use Filament\Actions;
7+
use Filament\Resources\Pages\ListRecords;
8+
9+
class ListCronJobs extends ListRecords
10+
{
11+
protected static string $resource = CronJobResource::class;
12+
13+
protected function getHeaderActions(): array
14+
{
15+
return [
16+
Actions\CreateAction::make(),
17+
];
18+
}
19+
}

web/app/Models/CronJob.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models;
44

5+
use Illuminate\Database\Eloquent\Builder;
56
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Support\Str;
78

@@ -13,6 +14,22 @@ class CronJob extends Model
1314
'user',
1415
];
1516

17+
protected static function booted(): void
18+
{
19+
static::addGlobalScope('customer', function (Builder $query) {
20+
if (auth()->check() && auth()->guard()->name == 'web_customer') {
21+
$query->whereHas('hostingSubscription', function ($query) {
22+
$query->where('customer_id', auth()->user()->id);
23+
});
24+
}
25+
});
26+
}
27+
28+
public function hostingSubscription()
29+
{
30+
return $this->belongsTo(HostingSubscription::class);
31+
}
32+
1633
public static function boot()
1734
{
1835
parent::boot();

web/app/Models/HostingSubscription.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ protected static function booted(): void
3636
});
3737
}
3838

39-
4039
public static function boot()
4140
{
4241
parent::boot();

web/database/migrations/2023_11_23_214641_create_cron_jobs_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function up(): void
1313
{
1414
Schema::create('cron_jobs', function (Blueprint $table) {
1515
$table->id();
16-
$table->string('hosting_account_id')->nullable();
16+
$table->string('hosting_subscription_id')->nullable();
1717
$table->string('command');
1818
$table->string('schedule');
1919
$table->string('user');

0 commit comments

Comments
 (0)