97 lines
2.8 KiB
PHP
97 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\News;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
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 NewsExport implements FromCollection, WithHeadings, WithStyles, WithMapping
|
|
{
|
|
/**
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
|
|
protected $category;
|
|
protected $year;
|
|
protected $month;
|
|
protected $date;
|
|
|
|
public function __construct($category, $year, $month, $date)
|
|
{
|
|
$this->category = $category;
|
|
$this->year = $year;
|
|
$this->month = $month;
|
|
$this->date = $date;
|
|
}
|
|
public function collection()
|
|
{
|
|
$query = News::query()->select(['id', 'title', 'category', 'created_at']);
|
|
|
|
if($this->category !== 'all'){
|
|
if($this->category === 'news'){
|
|
$query->where('category', '0');
|
|
}else if($this->category === 'announcement'){
|
|
$query->where('category', '1');
|
|
}
|
|
}
|
|
|
|
if ($this->year) {
|
|
$query->whereYear('created_at', $this->year);
|
|
}
|
|
|
|
if ($this->month) {
|
|
$monthYear = Carbon::parse($this->month);
|
|
$query->whereYear('created_at', $monthYear->year)
|
|
->whereMonth('created_at', $monthYear->month);
|
|
}
|
|
|
|
if ($this->date) {
|
|
$query->whereDate('created_at', $this->date);
|
|
}
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
public function map($news): array
|
|
{
|
|
return [
|
|
$news->id,
|
|
$news->title,
|
|
$news->category === '0' ? 'Berita' : 'Pengumuman',
|
|
Carbon::parse($news->created_at)->locale('id')->translatedFormat('l, d F Y')
|
|
];
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
['Data Berita', '', '', ''],
|
|
['NO', 'Judul', 'Kategori', 'Dibuat Pada']
|
|
];
|
|
}
|
|
|
|
public function styles(Worksheet $sheet)
|
|
{
|
|
$sheet->mergeCells('A1:D1'); // A1 sampai D1 digabung
|
|
|
|
// Set alignment untuk pusat horizontal dan vertikal
|
|
$sheet->getStyle('A1:D1')->getAlignment()->setHorizontal('center');
|
|
$sheet->getStyle('A1:D1')->getAlignment()->setVertical('center');
|
|
|
|
$sheet->getColumnDimension('A')->setWidth(7); // Set column A width
|
|
$sheet->getColumnDimension('B')->setAutoSize(true);
|
|
$sheet->getColumnDimension('C')->setWidth(15); // Set column C width
|
|
$sheet->getColumnDimension('D')->setWidth(25); // Set column D width
|
|
|
|
// Menambahkan gaya lain jika diperlukan
|
|
$sheet->getStyle('A1:D1')->getFont()->setBold(true);
|
|
$sheet->getStyle('A1:D1')->getFont()->setSize(14);
|
|
}
|
|
}
|