simedkom/app/Exports/IssueManagementExport.php

219 lines
6.8 KiB
PHP

<?php
namespace App\Exports;
use App\Models\IssueManagement;
use Carbon\Carbon;
use Illuminate\Support\Str;
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 IssueManagementExport implements FromCollection, WithHeadings, WithMapping, WithStyles
{
/**
* @return \Illuminate\Support\Collection
*/
protected $no = 1;
protected $classification;
protected $sub_classification;
protected $location;
protected $sub_location;
protected $government_agency;
protected $issue;
protected $response;
protected $year;
protected $month;
protected $date;
protected $start_date;
protected $end_date;
protected $user;
public function __construct(
$classification,
$sub_classification,
$location,
$sub_location,
$government_agency,
$issue,
$response,
$year,
$month,
$date,
$start_date,
$end_date,
$user
) {
$this->classification = $classification;
$this->sub_classification = $sub_classification;
$this->location = $location;
$this->sub_location = $sub_location;
$this->government_agency = $government_agency;
$this->issue = $issue;
$this->response = $response;
$this->year = $year;
$this->month = $month;
$this->date = $date;
$this->start_date = $start_date;
$this->end_date = $end_date;
$this->user = $user;
}
public function collection()
{
$query = IssueManagement::with(['location', 'subLocation', 'classification', 'subClassification', 'enhancer', 'governmentAgencies']);
if ($this->classification) {
$query->where('classification_id', decrypt_id($this->classification));
}
if ($this->sub_classification) {
$query->where('sub_classification_id', decrypt_id($this->sub_classification));
}
if ($this->location) {
$query->where('location_id', decrypt_id($this->location));
}
if ($this->sub_location) {
$query->where('sub_location_id', decrypt_id($this->sub_location));
}
if ($this->government_agency) {
$query->whereHas('governmentAgencies', function ($query) {
$query->where('issue_managements_government_agencies.government_agency_id', decrypt_id($this->government_agency));
});
}
if ($this->issue) {
switch ($this->issue) {
case 'positive':
$query->where('issue', 'positif');
break;
case 'negative':
$query->where('issue', 'negatif');
break;
case 'neutral':
$query->where('issue', 'netral');
break;
case 'crisis':
$query->where('issue', 'krisis');
break;
}
}
if ($this->response) {
switch ($this->response) {
case 'positive':
$query->where('response', 'positif');
break;
case 'negative':
$query->where('response', 'negatif');
break;
case 'neutral':
$query->where('response', 'netral');
break;
case 'crisis':
$query->where('response', 'krisis');
break;
}
}
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);
}
if ($this->start_date && $this->end_date) {
$start = \Carbon\Carbon::createFromFormat('m/d/Y', $this->start_date)->startOfDay();
$end = \Carbon\Carbon::createFromFormat('m/d/Y', $this->end_date)->endOfDay();
$query->whereBetween('created_at', [$start, $end]);
}
if ($this->user) {
$query->where('enhancer_id', decrypt_id($this->user));
}
return $query->get();
}
public function map($issueManagement): array
{
return [
$this->no++,
$issueManagement->mediaMonitoring->code,
$issueManagement->mediaMonitoring->title,
$issueManagement->classification->name,
$issueManagement->subClassification->name,
$issueManagement->location->name,
$issueManagement->subLocation->name,
ucfirst($issueManagement->issue),
ucfirst($issueManagement->response),
$issueManagement->governmentAgencies->map(function ($agency) {
return Str::ucfirst($agency->name);
})->implode(', '),
$issueManagement->enhancer->name ?? 'Admin',
Carbon::parse($issueManagement->created_at)->locale('id')->translatedFormat('l, d F Y'),
];
}
public function headings(): array
{
return [
['Data Managemen Isu', '', '', '', '', '', '', '', '', '', '', ''],
['NO', 'Kode', 'Monitoring Media', 'Klasifikasi', 'Sub Klasifikasi', 'Lokus', 'Sub Lokus', 'Isu', 'Tanggapan', 'Dinas', 'Oleh', 'Dibuat Pada'],
];
}
public function styles(Worksheet $sheet)
{
$sheet->mergeCells('A1:L1'); // A1 sampai D1 digabung
// Set alignment untuk pusat horizontal dan vertikal
$sheet->getStyle('A1:L1')->getAlignment()->setHorizontal('center');
$sheet->getStyle('A1:L1')->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);
// Menambahkan gaya lain jika diperlukan
$sheet->getStyle('A1:L1')->getFont()->setBold(true);
$sheet->getStyle('A1:L1')->getFont()->setSize(14);
}
}