simedkom/app/Exports/ContentRecapExport.php
myuqinurrohman a9278afa77 feat: filter data
feat: export data
2025-04-21 10:10:19 +07:00

116 lines
3.9 KiB
PHP

<?php
namespace App\Exports;
use App\Models\ContentRecap;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class ContentRecapExport implements FromCollection, WithHeadings, WithStyles, WithMapping
{
/**
* @return \Illuminate\Support\Collection
*/
protected $no = 1;
protected $classification;
protected $channel;
protected $social_media;
protected $posted_year;
protected $posted_month;
protected $posted_date;
public function __construct($classification, $channel, $social_media, $posted_year, $posted_month, $posted_date)
{
$this->classification = $classification;
$this->channel = $channel;
$this->social_media = $social_media;
$this->posted_year = $posted_year;
$this->posted_month = $posted_month;
$this->posted_date = $posted_date;
}
public function collection()
{
$query = ContentRecap::with(['enhancer', 'classification']);
if($this->classification && $this->classification !== 'all'){
$query->where('classification_id', decrypt_id($this->classification));
}
if($this->channel && $this->channel !== 'all'){
$query->where('channel', $this->channel);
}
if($this->social_media && $this->social_media !== 'all'){
$query->where('social_media', $this->social_media);
}
if ($this->posted_year) {
$query->whereYear('posted_date', $this->posted_year);
}
if ($this->posted_month) {
$monthYear = Carbon::parse($this->posted_month);
$query->whereYear('posted_date', $monthYear->year)
->whereMonth('posted_date', $monthYear->month);
}
if ($this->posted_date) {
$query->whereDate('posted_date', $this->posted_date);
}
return $query->get();
}
public function map($contentRecap): array
{
return [
$this->no++,
$contentRecap->content_theme,
$contentRecap->link,
$contentRecap->classification->name,
$contentRecap->social_media,
Carbon::parse($contentRecap->posted_date)->locale('id')->translatedFormat('l, d F Y'),
$contentRecap->channel,
filled($contentRecap->description) ? strip_tags($contentRecap->description) : 'Tidak ada',
Carbon::parse($contentRecap->created_at)->locale('id')->translatedFormat('l, d F Y'),
];
}
public function headings(): array
{
return [
['Data Rekap Konten', '', '', '', '', '', '', '', ''],
['NO', 'Tema Konten', 'Link', 'Klasifikasi', 'Media Sosial', 'Tanggal Posting', 'Channel', 'Deskripsi', 'Dibuat Pada']
];
}
public function styles(Worksheet $sheet)
{
$sheet->mergeCells('A1:I1'); // A1 sampai D1 digabung
// Set alignment untuk pusat horizontal dan vertikal
$sheet->getStyle('A1:I1')->getAlignment()->setHorizontal('center');
$sheet->getStyle('A1:I1')->getAlignment()->setVertical('center');
$sheet->getColumnDimension('A')->setWidth(7); // Set column A width
$sheet->getColumnDimension('B')->setAutoSize(true);
$sheet->getColumnDimension('C')->setAutoSize(true);
$sheet->getColumnDimension('D')->setAutoSize(true);
$sheet->getColumnDimension('E')->setAutoSize(true);
$sheet->getColumnDimension('F')->setAutoSize(true);
$sheet->getColumnDimension('G')->setAutoSize(true);
$sheet->getColumnDimension('H')->setAutoSize(true);
$sheet->getColumnDimension('I')->setAutoSize(true);
// Menambahkan gaya lain jika diperlukan
$sheet->getStyle('A1:I1')->getFont()->setBold(true);
$sheet->getStyle('A1:I1')->getFont()->setSize(14);
}
}