166 lines
5.8 KiB
PHP
166 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\Media;
|
|
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 MediaExport implements FromCollection, WithHeadings, WithStyles, WithMapping
|
|
{
|
|
/**
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
|
|
protected $no = 1;
|
|
protected $type;
|
|
protected $classification;
|
|
protected $status;
|
|
protected $edit_status;
|
|
protected $address;
|
|
protected $register_year;
|
|
protected $register_month;
|
|
protected $register_date;
|
|
|
|
public function __construct($type, $classification, $status, $edit_status, $address, $register_year, $register_month, $register_date)
|
|
{
|
|
$this->type = $type;
|
|
$this->classification = $classification;
|
|
$this->status = $status;
|
|
$this->edit_status = $edit_status;
|
|
$this->address = $address;
|
|
$this->register_year = $register_year;
|
|
$this->register_month = $register_month;
|
|
$this->register_date = $register_date;
|
|
}
|
|
|
|
public function collection()
|
|
{
|
|
$query = Media::with(['company.user']);
|
|
|
|
if($this->type !== 'all'){
|
|
if($this->type === 'online'){
|
|
$query->where('type', 'online');
|
|
}elseif($this->type === 'print'){
|
|
$query->where('type', 'cetak');
|
|
}else if($this->type === 'radio'){
|
|
$query->where('type', 'radio');
|
|
}else if($this->type === 'television'){
|
|
$query->where('type', 'televisi');
|
|
}
|
|
}
|
|
|
|
if($this->classification !== 'all'){
|
|
if($this->classification === 'local'){
|
|
$query->where('classification', 'lokal');
|
|
}elseif($this->classification === 'regional'){
|
|
$query->where('classification', 'regional');
|
|
}else if($this->classification === 'national'){
|
|
$query->where('classification', 'nasional');
|
|
}
|
|
}
|
|
|
|
if($this->status !== 'all'){
|
|
if($this->status === 'waiting'){
|
|
$query->whereHas('user', function ($q){
|
|
$q->where('status', '0');
|
|
});
|
|
}else if($this->status === 'accepted'){
|
|
$query->whereHas('user', function ($q){
|
|
$q->where('status', '1');
|
|
});
|
|
}else if($this->status === 'rejected'){
|
|
$query->whereHas('user', function ($q){
|
|
$q->where('status', '2');
|
|
});
|
|
}
|
|
}
|
|
|
|
if($this->edit_status !== 'all'){
|
|
if($this->edit_status === 'no-edit'){
|
|
$query->where('edit_status', '0');
|
|
}elseif($this->edit_status === 'waiting'){
|
|
$query->where('edit_status', '1');
|
|
}else if($this->edit_status === 'accepted'){
|
|
$query->where('edit_status', '2');
|
|
}else if($this->edit_status === 'rejected'){
|
|
$query->where('edit_status', '3');
|
|
}
|
|
}
|
|
|
|
if ($this->address) {
|
|
$query->where('address', 'LIKE', '%'.$this->address.'%');
|
|
}
|
|
|
|
if ($this->register_year) {
|
|
$query->whereYear('created_at', $this->register_year);
|
|
}
|
|
|
|
if ($this->register_month) {
|
|
$monthYear = Carbon::parse($this->register_month);
|
|
$query->whereYear('created_at', $monthYear->year)
|
|
->whereMonth('created_at', $monthYear->month);
|
|
}
|
|
|
|
if ($this->register_date) {
|
|
$query->whereDate('created_at', $this->register_date);
|
|
}
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
public function map($media): array
|
|
{
|
|
return [
|
|
$this->no++,
|
|
$media->name,
|
|
$media->company->name,
|
|
$media->company->email,
|
|
ucfirst($media->type),
|
|
ucfirst($media->classification),
|
|
$media->address,
|
|
get_user_status($media->company->user->status),
|
|
get_edit_status($media->edit_status),
|
|
Carbon::parse($media->created_at)->locale('id')->translatedFormat('l, d F Y'),
|
|
Carbon::parse($media->company->validated_at)->locale('id')->translatedFormat('l, d F Y')
|
|
];
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
['Data Media', '', '', '', '', '', '', '', '', '', '', ''],
|
|
['NO', 'Nama Media', 'Nama Perusahaan', 'Email', 'Tipe', 'Klasifikasi', 'Alamat', 'Status', 'Status Edit', 'Tanggal Daftar', 'Tanggal Validasi']
|
|
];
|
|
}
|
|
|
|
public function styles(Worksheet $sheet)
|
|
{
|
|
$sheet->mergeCells('A1:K1'); // A1 sampai D1 digabung
|
|
|
|
// Set alignment untuk pusat horizontal dan vertikal
|
|
$sheet->getStyle('A1:K1')->getAlignment()->setHorizontal('center');
|
|
$sheet->getStyle('A1:K1')->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);
|
|
|
|
// Menambahkan gaya lain jika diperlukan
|
|
$sheet->getStyle('A1:K1')->getFont()->setBold(true);
|
|
$sheet->getStyle('A1:K1')->getFont()->setSize(14);
|
|
}
|
|
}
|