Skip to content

Instantly share code, notes, and snippets.

@ap1969
Created February 7, 2022 23:48
Show Gist options
  • Save ap1969/f622583bb43f2ac6a701a03b77187fd7 to your computer and use it in GitHub Desktop.
Save ap1969/f622583bb43f2ac6a701a03b77187fd7 to your computer and use it in GitHub Desktop.
Filament package issue
<?php
namespace Snackdelayer\Snackdelayer\Resources\TimerResource\Pages;
use Snackdelayer\Snackdelayer\Resources\TimerResource;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Facades\Log;
class CreateTimer extends CreateRecord
{
protected static string $resource = TimerResource::class;
}
<?php
namespace Snackdelayer\Snackdelayer;
use Filament\Forms\Components\Select;
use Filament\PluginServiceProvider;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Snackdelayer\Snackdelayer\Commands\SnackdelayerCommand;
use Snackdelayer\Snackdelayer\Resources\TimerResource;
use TorMorten\Eventy\Facades\Events as Eventy;
use Illuminate\Support\Facades\Log;
class SnackdelayerServiceProvider extends PluginServiceProvider
{
public static string $name = 'snackdelayer';
public function boot() {
// Add hooks and filters
Eventy::addFilter('profile.schema.personal_info.fields', function($schema) {
$timeZones = collect(\DateTimeZone::listIdentifiers(\DateTimeZone::ALL))
->mapWithKeys(static function ($timezone) {
return [$timezone => $timezone];
})->toArray();
return array_merge($schema, [
Select::make('timezone')
->options($timeZones)
]);
}, 20, 1);
}
public function configurePackage(Package $package): void
{
/*
* This class is a Package Service Provider
*
* More info: https://github.com/spatie/laravel-package-tools
*/
$package
->name('snackdelayer')
->hasConfigFile()
->hasViews()
->hasMigrations([
'create_snackdelayer_table',
'add_timezone_to_user'
])
->hasCommand(SnackdelayerCommand::class);
}
protected function getResources(): array
{
return [
TimerResource::class
];
}
}
<?php
namespace Snackdelayer\Snackdelayer\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Timer extends Model
{
use HasFactory;
protected $table = 'snackdelayer_timers';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'start_time', 'end_time', 'user_id'];
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$user = Auth::user();
$model->user_id = $user->id;
/*
$user->timezone = "UTC";
// Save the UTC times
$date = Carbon::createFromFormat(
"h:i A",
$model->start_time,
$user->timezone
);
$date->setTimezone("UTC");
$model->start_time_utc = $date->setTimezone("UTC")->toTimeString();
$date = Carbon::createFromFormat(
"h:i A",
$model->end_time,
$user->timezone
);
$date->setTimezone("UTC");
$model->end_time_utc = $date->setTimezone("UTC")->toTimeString();
*/
$model->start_time_utc="09:00:00";
$model->end_time_utc="09:00:00";
});
static::updating(function ($model) {
$user = User::find($model->user_id);
// Save the UTC times
$date = Carbon::createFromFormat(
"h:i A",
$model->start_time,
$user->timezone
);
$date->setTimezone("UTC");
$model->start_time_utc = $date->setTimezone("UTC")->toTimeString();
$date = Carbon::createFromFormat(
"h:i A",
$model->end_time,
$user->timezone
);
$date->setTimezone("UTC");
$model->end_time_utc = $date->setTimezone("UTC")->toTimeString();
// Now clear the sent time, so if the user is shifting to a later time today, it still sends. If not, it'll run tomorrow anyway
if ($model->script_updated === true) {
Log::debug("Automated update<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
} else {
$model->start_time_last_sent = null;
$model->end_time_last_sent = null;
Log::debug("Manual update<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
// Now remove this as it's not a valid model field
unset($model->script_updated);
});
}
}
<?php
namespace Snackdelayer\Snackdelayer\Resources;
use Snackdelayer\Snackdelayer\Resources\TimerResource\Pages;
use Snackdelayer\Snackdelayer\Resources\TimerResource\RelationManagers;
use Snackdelayer\Snackdelayer\Models\Timer;
use Filament\Forms;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\TimePicker;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
class TimerResource extends Resource
{
protected static ?string $model = Timer::class;
protected static ?string $recordTitleAttribute = 'name';
protected static ?string $navigationIcon = 'heroicon-o-clock';
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->label("package name")
->required()
->columnSpan([
'sm' => 2,
]),
TextInput::make('start_time')
->columnSpan(1),
TextInput::make('end_time')
->columnSpan(1),
])->columns(2);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name'),
])
->filters([
//
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListTimers::route('/'),
'create' => Pages\CreateTimer::route('/create'),
'edit' => Pages\EditTimer::route('/{record}/edit'),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment