simedkom/app/Exports/MediaMonitoringExport.php
myuqinurrohman 04a464a2f9 feat: filter data
feat: export data
2025-04-22 08:27:43 +07:00

152 lines
5.4 KiB
PHP

<?php
namespace App\Exports;
use App\Models\MediaMonitoring;
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 MediaMonitoringExport implements FromCollection, WithHeadings, WithStyles, WithMapping
{
/**
* @return \Illuminate\Support\Collection
*/
protected $no = 1;
protected $news_title;
protected $channel;
protected $issue_status;
protected $input_year;
protected $input_month;
protected $input_date;
protected $release_year;
protected $release_month;
protected $release_date;
public function __construct($news_title, $channel, $issue_status, $input_year, $input_month, $input_date, $release_year, $release_month, $release_date)
{
$this->news_title = $news_title;
$this->channel = $channel;
$this->issue_status = $issue_status;
$this->input_year = $input_year;
$this->input_month = $input_month;
$this->input_date = $input_date;
$this->release_year = $release_year;
$this->release_month = $release_month;
$this->release_date = $release_date;
}
public function collection()
{
$query = MediaMonitoring::with(['enhancer', 'issue']);
if($this->news_title){
$query->where('title', 'LIKE', '%'. $this->news_title.'%');
}
if($this->channel && $this->channel !== 'all'){
$query->where('channel', $this->channel);
}
if ($this->issue_status && $this->issue_status !== 'all') {
if ($this->issue_status === 'added') {
$query->whereHas('issue');
} elseif ($this->issue_status === 'not_added') {
$query->whereDoesntHave('issue');
}
}
if ($this->input_year) {
$query->whereYear('created_at', $this->input_year);
}
if ($this->input_month) {
$monthYear = Carbon::parse($this->input_month);
$query->whereYear('created_at', $monthYear->year)
->whereMonth('created_at', $monthYear->month);
}
if ($this->input_date) {
$query->whereDate('created_at', $this->input_date);
}
if ($this->release_year) {
$query->whereYear('release_date', $this->release_year);
}
if ($this->release_month) {
$monthYear = Carbon::parse($this->release_month);
$query->whereYear('release_date', $monthYear->year)
->whereMonth('release_date', $monthYear->month);
}
if ($this->release_date) {
$query->whereDate('release_date', $this->release_date);
}
return $query->get();
}
public function map($mediaMonitoring): array
{
return [
$this->no++,
$mediaMonitoring->code,
$mediaMonitoring->title,
$mediaMonitoring->media_name,
$mediaMonitoring->theme,
$mediaMonitoring->writter,
$mediaMonitoring->channel,
$mediaMonitoring->keyword,
$mediaMonitoring->influencer,
$mediaMonitoring->link,
$mediaMonitoring->news_page,
$mediaMonitoring->release_date,
$mediaMonitoring->issue ? 'Sudah Ditambahkan' : 'Belum Ditambahkan',
$mediaMonitoring->enhancer->name ?? 'Admin',
Carbon::parse($mediaMonitoring->created_at)->locale('id')->translatedFormat('l, d F Y'),
];
}
public function headings(): array
{
return [
['Data Rekap Konten', '', '', '', '', '', '', '', '', '', '', '', '', ''],
['NO', 'Kode', 'Judul Berita', 'Media', 'Tema', 'Penulis', 'Channel', 'Kata Kunci', 'Influencer', 'Link', 'Halaman Berita', 'Tanggal Rilis', 'Status Isu', 'Oleh',
'Tanggal Input']
];
}
public function styles(Worksheet $sheet)
{
$sheet->mergeCells('A1:N1'); // A1 sampai D1 digabung
// Set alignment untuk pusat horizontal dan vertikal
$sheet->getStyle('A1:N1')->getAlignment()->setHorizontal('center');
$sheet->getStyle('A1:N1')->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);
$sheet->getColumnDimension('K')->setAutoSize(true);
$sheet->getColumnDimension('L')->setAutoSize(true);
$sheet->getColumnDimension('M')->setAutoSize(true);
$sheet->getColumnDimension('N')->setAutoSize(true);
// Menambahkan gaya lain jika diperlukan
$sheet->getStyle('A1:N1')->getFont()->setBold(true);
$sheet->getStyle('A1:N1')->getFont()->setSize(14);
}
}