simedkom/app/Filament/Exports/ContentRecapExcelExport.php

96 lines
3.0 KiB
PHP

<?php
namespace App\Filament\Exports;
use App\Enums\Channel;
use App\Enums\SocialMedia;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use pxlrbt\FilamentExcel\Columns\Column;
use pxlrbt\FilamentExcel\Exports\ExcelExport;
class ContentRecapExcelExport extends ExcelExport implements WithEvents, WithStyles
{
public function setUp()
{
$this->withFilename(fn ($resource) => $resource::getModelLabel().'-'.date('Y-m-d'));
$this->withColumns([
Column::make('title')
->heading('Judul'),
Column::make('type')
->heading('Tipe')
->formatStateUsing(fn ($state) => $state?->getLabel()),
Column::make('channel')
->heading('Kanal')
->formatStateUsing(fn ($state) => Channel::tryFrom($state)?->getLabel() ?? $state),
Column::make('social_media')
->heading('Media Sosial')
->formatStateUsing(fn ($state) => SocialMedia::tryFrom($state)?->getLabel() ?? $state),
Column::make('classification.name')
->heading('Klasifikasi'),
Column::make('link')
->heading('Tautan'),
Column::make('posting_date')
->heading('Tgl Posting')
->formatStateUsing(fn ($state) => $state?->translatedFormat('l, d F Y')),
]);
}
public function headings(): array
{
return [
['Data Rekap Konten'],
$this->getHeadings(),
];
}
public function styles(Worksheet $sheet)
{
return [
1 => [
'font' => ['bold' => true, 'size' => 14],
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
],
2 => [
'font' => ['bold' => true],
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER],
],
];
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$sheet = $event->sheet->getDelegate();
$lastColumn = 'G'; // A-G
$sheet->mergeCells("A1:{$lastColumn}1");
$sheet->getStyle("A2:{$lastColumn}".$sheet->getHighestRow())
->getAlignment()
->setVertical(Alignment::VERTICAL_TOP)
->setWrapText(true);
for ($i = 3; $i <= $sheet->getHighestRow(); $i++) {
$sheet->getRowDimension($i)->setRowHeight(-1);
}
foreach (range('A', $lastColumn) as $columnID) {
$sheet->getColumnDimension($columnID)->setAutoSize(true);
}
},
];
}
}