265 lines
11 KiB
PHP
265 lines
11 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\OsurgInitialResource\Pages;
|
|
use App\Models\OsurgInitial;
|
|
use App\Services\PdfService;
|
|
use Filament\Actions\Action;
|
|
use Filament\Actions\ActionGroup;
|
|
use Filament\Actions\BulkAction;
|
|
use Filament\Actions\BulkActionGroup;
|
|
use Filament\Actions\DeleteAction;
|
|
use Filament\Actions\DeleteBulkAction;
|
|
use Filament\Actions\EditAction;
|
|
use Filament\Forms\Components\FileUpload;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Schemas\Components\Grid;
|
|
use Filament\Schemas\Components\Section;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Tables\Columns\ImageColumn;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Support\Collection;
|
|
use OpenSpout\Common\Entity\Row;
|
|
use OpenSpout\Writer\XLSX\Writer;
|
|
|
|
class OsurgInitialResource extends Resource
|
|
{
|
|
protected static ?string $model = OsurgInitial::class;
|
|
|
|
protected static ?string $recordTitleAttribute = 'init_parameter';
|
|
|
|
public static function getNavigationIcon(): \BackedEnum|string|null
|
|
{
|
|
return 'heroicon-o-adjustments-horizontal';
|
|
}
|
|
|
|
public static function getNavigationGroup(): ?string
|
|
{
|
|
return __('navigation.groups.settings');
|
|
}
|
|
|
|
public static function getNavigationSort(): ?int
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
public static function getNavigationLabel(): string
|
|
{
|
|
return __('navigation.osurg_initials.title');
|
|
}
|
|
|
|
public static function getModelLabel(): string
|
|
{
|
|
return __('navigation.osurg_initials.singular');
|
|
}
|
|
|
|
public static function getPluralModelLabel(): string
|
|
{
|
|
return __('navigation.osurg_initials.title');
|
|
}
|
|
|
|
public static function form(Schema $schema): Schema
|
|
{
|
|
return $schema
|
|
->components([
|
|
Grid::make()
|
|
->schema([
|
|
Section::make()
|
|
->schema([
|
|
TextInput::make('init_parameter')
|
|
->label(__('osurg_initials.fields.init_parameter'))
|
|
->required()
|
|
->maxLength(50)
|
|
->unique(table: OsurgInitial::class, column: 'init_parameter', ignoreRecord: true),
|
|
|
|
TextInput::make('init_value')
|
|
->label(__('osurg_initials.fields.init_value'))
|
|
->maxLength(50),
|
|
|
|
FileUpload::make('attachment')
|
|
->label(__('osurg_initials.fields.attachment'))
|
|
->disk('public')
|
|
->directory('initials')
|
|
->image()
|
|
->imagePreviewHeight('120')
|
|
->columnSpanFull(),
|
|
])
|
|
->columns(2)
|
|
->columnSpanFull()
|
|
->footerActions([
|
|
fn (string $operation) => Action::make('create')
|
|
->label(__('filament-panels::resources/pages/create-record.form.actions.create.label'))
|
|
->submit('create')
|
|
->keyBindings(['mod+s'])
|
|
->visible($operation === 'create'),
|
|
|
|
fn (string $operation) => Action::make('createAnother')
|
|
->label(__('filament-panels::resources/pages/create-record.form.actions.create_another.label'))
|
|
->action('createAnother')
|
|
->keyBindings(['mod+shift+s'])
|
|
->color('gray')
|
|
->visible($operation === 'create'),
|
|
|
|
fn (string $operation) => Action::make('cancelCreate')
|
|
->label(__('filament-panels::resources/pages/create-record.form.actions.cancel.label'))
|
|
->url(static::getUrl('index'))
|
|
->color('gray')
|
|
->visible($operation === 'create'),
|
|
|
|
fn (string $operation) => Action::make('save')
|
|
->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
|
|
->submit('save')
|
|
->keyBindings(['mod+s'])
|
|
->visible($operation === 'edit'),
|
|
|
|
fn (string $operation) => Action::make('cancelEdit')
|
|
->label(__('filament-panels::resources/pages/edit-record.form.actions.cancel.label'))
|
|
->url(static::getUrl('index'))
|
|
->color('gray')
|
|
->visible($operation === 'edit'),
|
|
]),
|
|
])
|
|
->columnSpanFull(),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
TextColumn::make('init_parameter')
|
|
->label(__('osurg_initials.fields.init_parameter'))
|
|
->searchable()
|
|
->sortable(),
|
|
|
|
TextColumn::make('init_value')
|
|
->label(__('osurg_initials.fields.init_value'))
|
|
->searchable()
|
|
->placeholder('—'),
|
|
|
|
ImageColumn::make('attachment')
|
|
->label(__('osurg_initials.fields.attachment'))
|
|
->disk('public')
|
|
->square()
|
|
->size(48),
|
|
|
|
TextColumn::make('created_at')
|
|
->label(__('osurg_initials.fields.created_at'))
|
|
->sortable(),
|
|
])
|
|
->recordActions([
|
|
EditAction::make(),
|
|
DeleteAction::make(),
|
|
])
|
|
->toolbarActions([
|
|
BulkActionGroup::make([
|
|
DeleteBulkAction::make(),
|
|
BulkAction::make('export_excel_selected')
|
|
->label(__('table.export_excel_selected'))
|
|
->icon('heroicon-o-table-cells')
|
|
->color('success')
|
|
->action(function (Collection $records) {
|
|
$tempPath = tempnam(sys_get_temp_dir(), 'export_') . '.xlsx';
|
|
$writer = new Writer();
|
|
$writer->openToFile($tempPath);
|
|
|
|
$writer->addRow(Row::fromValues([
|
|
'ID',
|
|
__('osurg_initials.fields.init_parameter'),
|
|
__('osurg_initials.fields.init_value'),
|
|
__('osurg_initials.fields.created_at'),
|
|
]));
|
|
|
|
foreach ($records as $record) {
|
|
$writer->addRow(Row::fromValues([
|
|
$record->id,
|
|
$record->init_parameter,
|
|
$record->init_value,
|
|
$record->created_at,
|
|
]));
|
|
}
|
|
|
|
$writer->close();
|
|
|
|
return response()->streamDownload(function () use ($tempPath) {
|
|
readfile($tempPath);
|
|
@unlink($tempPath);
|
|
}, 'osurg-initials-' . now()->format('Y-m-d') . '.xlsx', [
|
|
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
]);
|
|
}),
|
|
]),
|
|
|
|
ActionGroup::make([
|
|
Action::make('export_excel')
|
|
->label(__('table.export_excel'))
|
|
->color('success')
|
|
->icon('heroicon-o-table-cells')
|
|
->action(function () {
|
|
$records = OsurgInitial::all();
|
|
|
|
$tempPath = tempnam(sys_get_temp_dir(), 'export_') . '.xlsx';
|
|
$writer = new Writer();
|
|
$writer->openToFile($tempPath);
|
|
|
|
$writer->addRow(Row::fromValues([
|
|
'ID',
|
|
__('osurg_initials.fields.init_parameter'),
|
|
__('osurg_initials.fields.init_value'),
|
|
__('osurg_initials.fields.created_at'),
|
|
]));
|
|
|
|
foreach ($records as $record) {
|
|
$writer->addRow(Row::fromValues([
|
|
$record->id,
|
|
$record->init_parameter,
|
|
$record->init_value,
|
|
$record->created_at,
|
|
]));
|
|
}
|
|
|
|
$writer->close();
|
|
|
|
return response()->streamDownload(function () use ($tempPath) {
|
|
readfile($tempPath);
|
|
@unlink($tempPath);
|
|
}, 'osurg-initials-' . now()->format('Y-m-d') . '.xlsx', [
|
|
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
]);
|
|
}),
|
|
|
|
Action::make('export_pdf')
|
|
->label(__('table.export_pdf'))
|
|
->color('danger')
|
|
->icon('heroicon-o-document-arrow-down')
|
|
->action(function () {
|
|
$records = OsurgInitial::all();
|
|
|
|
return PdfService::download(
|
|
'exports.osurg-initials-pdf',
|
|
['records' => $records],
|
|
'osurg-initials-' . now()->format('Y-m-d') . '.pdf',
|
|
);
|
|
}),
|
|
])
|
|
->label(__('table.export'))
|
|
->color('orange')
|
|
->button(),
|
|
]);
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListOsurgInitials::route('/'),
|
|
'create' => Pages\CreateOsurgInitial::route('/create'),
|
|
'edit' => Pages\EditOsurgInitial::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|