simedkom/app/Exports/ContentRecapExport.php
2025-10-13 08:35:36 +07:00

130 lines
4.4 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, WithMapping, WithStyles
{
/**
* @return \Illuminate\Support\Collection
*/
protected $no = 1;
protected $classification;
protected $channel;
protected $social_media;
protected $content_type;
protected $posted_year;
protected $posted_month;
protected $posted_date;
public function __construct($classification, $channel, $social_media, $content_type, $posted_year, $posted_month, $posted_date)
{
$this->classification = $classification;
$this->channel = $channel;
$this->social_media = $social_media;
$this->content_type = $content_type;
$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->content_type && $this->content_type !== 'all') {
$query->where('content_type', 'LIKE', '%'.$this->content_type.'%');
}
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,
filled($contentRecap->content_type) ? titlefy($contentRecap->content_type) : '-',
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', 'Jenis Konten', '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:J1')->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);
$sheet->getColumnDimension('J')->setAutoSize(true);
// Menambahkan gaya lain jika diperlukan
$sheet->getStyle('A1:I1')->getFont()->setBold(true);
$sheet->getStyle('A1:I1')->getFont()->setSize(14);
}
}