mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 06:24:44 +02:00

* Add front end badges * I identify as a `string` * Display even if there's no limit * use `const`'s --------- Co-authored-by: RMartinOscar <40749467+RMartinOscar@users.noreply.github.com>
86 lines
2.1 KiB
PHP
86 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Server\Resources;
|
|
|
|
use App\Filament\Server\Resources\BackupResource\Pages;
|
|
use App\Models\Backup;
|
|
use App\Models\Permission;
|
|
use App\Models\Server;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Resources\Resource;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class BackupResource extends Resource
|
|
{
|
|
protected static ?string $model = Backup::class;
|
|
|
|
protected static ?int $navigationSort = 3;
|
|
|
|
protected static ?string $navigationIcon = 'tabler-file-zip';
|
|
|
|
protected static bool $canCreateAnother = false;
|
|
|
|
public const WARNING_THRESHOLD = 0.7;
|
|
|
|
public static function getNavigationBadge(): string
|
|
{
|
|
/** @var Server $server */
|
|
$server = Filament::getTenant();
|
|
|
|
$limit = $server->backup_limit;
|
|
|
|
return $server->backups->count() . ($limit === 0 ? '' : ' / ' . $limit);
|
|
}
|
|
|
|
public static function getNavigationBadgeColor(): ?string
|
|
{
|
|
/** @var Server $server */
|
|
$server = Filament::getTenant();
|
|
|
|
$limit = $server->backup_limit;
|
|
$count = $server->backups->count();
|
|
|
|
if ($limit === 0) {
|
|
return null;
|
|
}
|
|
|
|
return $count >= $limit ? 'danger'
|
|
: ($count >= $limit * self::WARNING_THRESHOLD ? 'warning' : 'success');
|
|
}
|
|
|
|
// TODO: find better way handle server conflict state
|
|
public static function canAccess(): bool
|
|
{
|
|
/** @var Server $server */
|
|
$server = Filament::getTenant();
|
|
|
|
if ($server->isInConflictState()) {
|
|
return false;
|
|
}
|
|
|
|
return parent::canAccess();
|
|
}
|
|
|
|
public static function canViewAny(): bool
|
|
{
|
|
return auth()->user()->can(Permission::ACTION_BACKUP_READ, Filament::getTenant());
|
|
}
|
|
|
|
public static function canCreate(): bool
|
|
{
|
|
return auth()->user()->can(Permission::ACTION_BACKUP_CREATE, Filament::getTenant());
|
|
}
|
|
|
|
public static function canDelete(Model $record): bool
|
|
{
|
|
return auth()->user()->can(Permission::ACTION_BACKUP_DELETE, Filament::getTenant());
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListBackups::route('/'),
|
|
];
|
|
}
|
|
}
|