191 lines
7.6 KiB
PHP
191 lines
7.6 KiB
PHP
<?php
|
|
namespace App\Exports;
|
|
|
|
use App\Models\Events;
|
|
use App\Models\PesertaEvent;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Str;
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
|
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
|
|
use Maatwebsite\Excel\Concerns\WithTitle;
|
|
use Maatwebsite\Excel\Events\AfterSheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
|
|
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
|
|
|
|
class PesertaEventExport extends DefaultValueBinder implements FromCollection, WithHeadings, WithEvents, WithMapping, WithCustomStartCell, WithColumnFormatting, WithTitle, WithCustomValueBinder
|
|
{
|
|
protected $eventId;
|
|
|
|
public function __construct($eventId)
|
|
{
|
|
$this->eventId = $eventId;
|
|
}
|
|
|
|
public function collection()
|
|
{
|
|
return PesertaEvent::with('Peserta.Sekolah')->where('event_id', $this->eventId)->get();
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return ["No", "Nama Anak", "Orang Tua", "No HP Orangtua", "Kelas", "Sekolah", "Status", "Tgl. Daftar"];
|
|
}
|
|
|
|
// Mapping supaya nomor urut otomatis
|
|
public function map($peserta): array
|
|
{
|
|
static $no = 1;
|
|
return [
|
|
$no++, // No
|
|
Str::title($peserta->Peserta->nama_anak ?? '-'),
|
|
Str::title($peserta->Peserta->nama_orangtua ?? '-'),
|
|
$peserta->Peserta->no_hp_orangtua
|
|
? "+62" . $peserta->Peserta->no_hp_orangtua
|
|
: 'No Hp Belum Diisi',
|
|
$peserta->Peserta->kelas ?? '-',
|
|
$peserta->Peserta?->Sekolah?->nama_sekolah ?? ($peserta->Peserta?->asal_sekolah ?? '-'),
|
|
$peserta->status ?? '-',
|
|
Carbon::parse($peserta->created_at)->locale('id')->translatedFormat('d M Y'),
|
|
];
|
|
}
|
|
public function columnFormats(): array
|
|
{
|
|
return [
|
|
'D' => NumberFormat::FORMAT_TEXT,
|
|
];
|
|
}
|
|
|
|
public function bindValue(Cell $cell, $value)
|
|
{
|
|
if (is_string($value) && str_starts_with($value, '+')) {
|
|
$cell->setValueExplicit($value, DataType::TYPE_STRING);
|
|
return true;
|
|
}
|
|
|
|
return parent::bindValue($cell, $value);
|
|
}
|
|
|
|
public function startCell(): string
|
|
{
|
|
return 'A6';
|
|
}
|
|
|
|
public function registerEvents(): array
|
|
{
|
|
$eventData = Events::find($this->eventId);
|
|
|
|
return [
|
|
AfterSheet::class => function(AfterSheet $event) use ($eventData) {
|
|
$sheet = $event->sheet;
|
|
|
|
// Header utama
|
|
$sheet->mergeCells('A1:H1');
|
|
$sheet->mergeCells('A2:H2');
|
|
$sheet->mergeCells('A3:H3');
|
|
$sheet->mergeCells('A4:H4');
|
|
$sheet->mergeCells('A5:H5');
|
|
$sheet->setCellValue(
|
|
'A2',
|
|
strtoupper(($eventData ? $eventData->nama_event : ''))
|
|
);
|
|
|
|
if ($eventData) {
|
|
$sheet->setCellValue(
|
|
'A3',
|
|
"{$eventData->jenis_event} | {$eventData->lokasi_event} | " .
|
|
\Carbon\Carbon::parse($eventData->tanggal_event)->translatedFormat('d M Y')
|
|
);
|
|
|
|
$sheet->setCellValue(
|
|
'A4',
|
|
$eventData->waktu_mulai . " - " . $eventData->waktu_selesai . " WIB"
|
|
);
|
|
}
|
|
|
|
$sheet->getStyle('A2')->applyFromArray([
|
|
'font' => ['bold' => true, 'size' => 16],
|
|
'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER],
|
|
]);
|
|
|
|
$sheet->getStyle('A3:A4')->applyFromArray([
|
|
'font' => ['italic' => true, 'size' => 11],
|
|
'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER],
|
|
]);
|
|
|
|
$sheet->getStyle('A6:H6')->applyFromArray([
|
|
'font' => ['bold' => true],
|
|
'fill' => [
|
|
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
|
|
'startColor' => ['rgb' => 'EFEFEF'],
|
|
],
|
|
'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER],
|
|
]);
|
|
|
|
$lastRow = $sheet->getHighestRow();
|
|
$sheet->getStyle("A6:H{$lastRow}")->applyFromArray([
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
]);
|
|
|
|
// $sheet->mergeCells("A" . ($lastRow+1) . ":C" . ($lastRow+1));
|
|
$sheet->setCellValue("G" . ($lastRow+1), "Total");
|
|
$sheet->setCellValue("H" . ($lastRow+1), $lastRow - 6);
|
|
|
|
$sheet->getStyle("G" . ($lastRow+1) . ":H" . ($lastRow+1))->applyFromArray([
|
|
'font' => ['bold' => true],
|
|
'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT],
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
]);
|
|
$sheet->getStyle("G" . ($lastRow+1))->applyFromArray([
|
|
'fill' => [
|
|
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
|
|
'startColor' => ['rgb' => 'EFEFEF'],
|
|
],
|
|
]);
|
|
|
|
$lastRow = $sheet->getHighestRow();
|
|
$sheet->getStyle("A7:A{$lastRow}")
|
|
->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
|
|
$sheet->getStyle("D7:D{$lastRow}")
|
|
->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
|
|
$sheet->getStyle("E7:E{$lastRow}")
|
|
->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
|
|
$sheet->getStyle("F7:F{$lastRow}")
|
|
->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
|
|
$sheet->getStyle("G7:G{$lastRow}")
|
|
->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
|
|
|
|
$sheet->getColumnDimension('A')->setWidth(5);
|
|
$sheet->getColumnDimension('B')->setWidth(32);
|
|
$sheet->getColumnDimension('C')->setWidth(32);
|
|
$sheet->getColumnDimension('D')->setWidth(25);
|
|
$sheet->getColumnDimension('E')->setWidth(8);
|
|
$sheet->getColumnDimension('F')->setWidth(32);
|
|
$sheet->getColumnDimension('G')->setWidth(19);
|
|
$sheet->getColumnDimension('H')->setWidth(19);
|
|
}
|
|
];
|
|
}
|
|
public function title(): string
|
|
{
|
|
$event = Events::find($this->eventId);
|
|
return $event ? $event->nama_event : 'Event';
|
|
}
|
|
}
|