1152 lines
54 KiB
PHP
1152 lines
54 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Dashboard;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\ContentRecap;
|
||
use App\Models\CooperationCompletion;
|
||
use App\Models\IssueManagement;
|
||
use App\Models\LoginHistory;
|
||
use App\Models\MediaCooperation;
|
||
use App\Models\MediaMonitoring;
|
||
use App\Models\Report;
|
||
use App\Models\User;
|
||
use App\Models\Visitor;
|
||
use Barryvdh\DomPDF\Facade\Pdf;
|
||
use Carbon\Carbon;
|
||
use Illuminate\Http\Request;
|
||
|
||
class StatisticController extends Controller
|
||
{
|
||
public function index(Request $request)
|
||
{
|
||
|
||
$statistics = [
|
||
[
|
||
'name' => 'Aktivitas Login',
|
||
'category' => 'login-activities',
|
||
'is_available' => true,
|
||
],
|
||
// [
|
||
// 'name' => 'Jurnalis',
|
||
// 'category' => 'journalists',
|
||
// 'is_available' => false
|
||
// ],
|
||
[
|
||
'name' => 'Kerja Sama Media',
|
||
'category' => 'media-cooperations',
|
||
'is_available' => true,
|
||
],
|
||
[
|
||
'name' => 'Laporan Bukti Tayang',
|
||
'category' => 'airing-proof-reports',
|
||
'is_available' => true,
|
||
],
|
||
// [
|
||
// 'name' => 'Media',
|
||
// 'category' => 'media',
|
||
// 'is_available' => false
|
||
// ],
|
||
[
|
||
'name' => 'Monitoring Media & Manajemen Isu',
|
||
'category' => 'media-monitorings-issue-management',
|
||
'is_available' => true,
|
||
],
|
||
[
|
||
'name' => 'Penyelesaian Kerja Sama',
|
||
'category' => 'cooperation-completions',
|
||
'is_available' => true,
|
||
],
|
||
[
|
||
'name' => 'Pengguna',
|
||
'category' => 'users',
|
||
'is_available' => true,
|
||
],
|
||
// [
|
||
// 'name' => 'Perusahaan',
|
||
// 'category' => 'companies',
|
||
// 'is_available' => false
|
||
// ],
|
||
[
|
||
'name' => 'Rekap Konten',
|
||
'category' => 'content-recaps',
|
||
'is_available' => true,
|
||
],
|
||
[
|
||
'name' => 'Pengunjung Website',
|
||
'category' => 'website-visitors',
|
||
'is_available' => true,
|
||
],
|
||
];
|
||
|
||
return view('dashboard.statistics.index', [
|
||
'title' => 'Statistik',
|
||
'statistics' => $statistics,
|
||
]);
|
||
}
|
||
|
||
public function show(Request $request, $category)
|
||
{
|
||
|
||
$data = [
|
||
'category' => $category,
|
||
'year' => $request->input('year') ? $request->input('year') : null,
|
||
'from_month' => $request->input('from_month') ? $request->input('from_month') : null,
|
||
'to_month' => $request->input('to_month') ? $request->input('to_month') : null,
|
||
];
|
||
|
||
$upComings = [
|
||
'journalists',
|
||
'media',
|
||
'companies',
|
||
];
|
||
|
||
if (in_array($category, $upComings)) {
|
||
return redirect()->back()->with('waiting-status', 'Dalam pegembangan');
|
||
}
|
||
|
||
$year = $request->input('year', Carbon::now()->year);
|
||
$from_month = $request->input('from_month', null);
|
||
$to_month = $request->input('to_month', null);
|
||
|
||
switch ($category) {
|
||
case 'login-activities':
|
||
$data['title'] = 'Statistik Aktivitas Login';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$loginActivities = LoginHistory::with(['user']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$loginActivities->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
$loginActivities = $loginActivities->get();
|
||
|
||
$loginActivities->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
// Simpan jumlah item tiap bulan ke index bulan - 1 (0-based)
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
$data['loginActivities'] = $monthlyCount;
|
||
|
||
return view('dashboard.statistics.show.login-activity', $data);
|
||
case 'media-cooperations':
|
||
$data['title'] = 'Statistik Kerja Sama Media';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$mediaCooperation = MediaCooperation::with(['cooperation', 'media']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$mediaCooperation->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$mediaCooperation->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$mediaCooperation = $mediaCooperation->get();
|
||
|
||
$mediaCooperationCount = $mediaCooperation->groupBy('status')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
// Inisialisasi array 12 bulan untuk masing-masing status
|
||
$monthlyStatus = [
|
||
'waiting' => array_fill(0, 12, 0),
|
||
'accepted' => array_fill(0, 12, 0),
|
||
'rejected' => array_fill(0, 12, 0),
|
||
];
|
||
|
||
// Kelompokkan data berdasarkan status lalu bulan
|
||
$mediaCooperation->groupBy('status')->each(function ($groupedByStatus, $status) use (&$monthlyStatus) {
|
||
$label = match ((int) $status) {
|
||
0 => 'waiting',
|
||
1 => 'accepted',
|
||
2 => 'rejected',
|
||
default => null,
|
||
};
|
||
|
||
if ($label) {
|
||
$groupedByStatus->groupBy(function ($item) {
|
||
return (int) $item->created_at->format('m'); // bulan sebagai angka 1–12
|
||
})->each(function ($items, $month) use (&$monthlyStatus, $label) {
|
||
$monthlyStatus[$label][$month - 1] = $items->count();
|
||
});
|
||
}
|
||
});
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$mediaCooperation->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
// Simpan jumlah item tiap bulan ke index bulan - 1 (0-based)
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
// Simpan ke variabel `$data`
|
||
$data['mediaCooperationStatusMonthly'] = $monthlyStatus;
|
||
$data['mediaCooperationMonthly'] = $monthlyCount;
|
||
|
||
$data['mediaCooperationWaiting'] = $mediaCooperationCount->get('0', 0);
|
||
$data['mediaCooperationAccepted'] = $mediaCooperationCount->get('1', 0);
|
||
$data['mediaCooperationRejected'] = $mediaCooperationCount->get('2', 0);
|
||
|
||
return view('dashboard.statistics.show.media-cooperation', $data);
|
||
case 'airing-proof-reports':
|
||
$data['title'] = 'Statistik Laporan Bukti Tayang';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$airingProofReport = Report::with(['order', 'media']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$airingProofReport->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$airingProofReport->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$airingProofReport = $airingProofReport->get();
|
||
|
||
$airingProofReportCount = $airingProofReport->groupBy('status')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
// Inisialisasi array 12 bulan untuk masing-masing status
|
||
$monthlyStatus = [
|
||
'waiting' => array_fill(0, 12, 0),
|
||
'accepted' => array_fill(0, 12, 0),
|
||
'rejected' => array_fill(0, 12, 0),
|
||
];
|
||
|
||
// Kelompokkan data berdasarkan status lalu bulan
|
||
$airingProofReport->groupBy('status')->each(function ($groupedByStatus, $status) use (&$monthlyStatus) {
|
||
$label = match ((int) $status) {
|
||
0 => 'waiting',
|
||
1 => 'accepted',
|
||
2 => 'rejected',
|
||
default => null,
|
||
};
|
||
|
||
if ($label) {
|
||
$groupedByStatus->groupBy(function ($item) {
|
||
return (int) $item->created_at->format('m'); // bulan sebagai angka 1–12
|
||
})->each(function ($items, $month) use (&$monthlyStatus, $label) {
|
||
$monthlyStatus[$label][$month - 1] = $items->count();
|
||
});
|
||
}
|
||
});
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$airingProofReport->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->publication_date)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
// Simpan jumlah item tiap bulan ke index bulan - 1 (0-based)
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
// Simpan ke variabel `$data`
|
||
$data['airingProofReportStatusMonthly'] = $monthlyStatus;
|
||
$data['airingProofReportPublicationMonthly'] = $monthlyCount;
|
||
|
||
$data['airingProofReportWaiting'] = $airingProofReportCount->get('0', 0);
|
||
$data['airingProofReportAccepted'] = $airingProofReportCount->get('1', 0);
|
||
$data['airingProofReportRejected'] = $airingProofReportCount->get('2', 0);
|
||
|
||
return view('dashboard.statistics.show.airing-proof-report', $data);
|
||
case 'media-monitorings-issue-management':
|
||
$data['title'] = 'Statistik Monitoring Media & Manajemen Isu';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
// Statistik Isu
|
||
$issues = IssueManagement::with(['mediaMonitoring']);
|
||
$mediaMonitorings = MediaMonitoring::with(['issue']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$issues->whereYear('created_at', $year);
|
||
$mediaMonitorings->whereYear('created_at', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$issues->whereBetween('created_at', [$start, $end]);
|
||
$mediaMonitorings->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$issues = $issues->get();
|
||
$mediaMonitorings = $mediaMonitorings->get();
|
||
|
||
$issueCounts = $issues->groupBy('issue')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
$data['crisisIssue'] = $issueCounts->get('krisis', 0);
|
||
$data['negativeIssue'] = $issueCounts->get('negatif', 0);
|
||
$data['neutralIssue'] = $issueCounts->get('netral', 0);
|
||
$data['positiveIssue'] = $issueCounts->get('positif', 0);
|
||
// Akhir Statistik Isu
|
||
|
||
// Statistik Monitoring Media
|
||
$mediaMonitoringCount = $mediaMonitorings->groupBy('channel')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
$data['facebookMediaMonitoring'] = $mediaMonitoringCount->get('facebook', 0);
|
||
$data['twitterMediaMonitoring'] = $mediaMonitoringCount->get('twitter', 0);
|
||
$data['websiteMediaMonitoring'] = $mediaMonitoringCount->get('website', 0);
|
||
$data['youtubeMediaMonitoring'] = $mediaMonitoringCount->get('youtube', 0);
|
||
$data['instagramMediaMonitoring'] = $mediaMonitoringCount->get('instagram', 0);
|
||
$data['tiktokMediaMonitoring'] = $mediaMonitoringCount->get('tiktok', 0);
|
||
$data['cetakMediaMonitoring'] = $mediaMonitoringCount->get('cetak', 0);
|
||
// Akhir Statistik Monitoring Media
|
||
|
||
// Media Monitoring 2 (pakai koleksi yang sudah ada)
|
||
$mediaMonitoringMonthly = array_fill(0, 12, 0); // Init array 12 bulan
|
||
$mediaMonitorings->groupBy(function ($item) {
|
||
return (int) $item->created_at->format('m'); // bulan sebagai int
|
||
})->each(function ($group, $month) use (&$mediaMonitoringMonthly) {
|
||
$mediaMonitoringMonthly[$month - 1] = $group->count();
|
||
});
|
||
|
||
// Issue Management 2 (pakai koleksi yang sudah ada)
|
||
$issueManagementMonthly = array_fill(0, 12, 0);
|
||
$issues->groupBy(function ($item) {
|
||
return (int) $item->created_at->format('m');
|
||
})->each(function ($group, $month) use (&$issueManagementMonthly) {
|
||
$issueManagementMonthly[$month - 1] = $group->count();
|
||
});
|
||
|
||
$mediaMonitoringWithoutIssue = MediaMonitoring::doesntHave('issue')
|
||
->when($year && $year !== 'all' && ! (filled($from_month) || filled($to_month)), function ($query) use ($year) {
|
||
$query->whereYear('created_at', $year);
|
||
})
|
||
->when($from_month && $to_month, function ($query) use ($from_month, $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
$query->whereBetween('created_at', [$start, $end]);
|
||
})
|
||
->get();
|
||
|
||
$mediaMonitoringWithIssue = MediaMonitoring::has('issue')
|
||
->when($year && $year !== 'all' && ! (filled($from_month) || filled($to_month)), function ($query) use ($year) {
|
||
$query->whereYear('created_at', $year);
|
||
})
|
||
->when($from_month && $to_month, function ($query) use ($from_month, $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
$query->whereBetween('created_at', [$start, $end]);
|
||
})
|
||
->get();
|
||
|
||
$data['mediaMonitoringWithoutIssueCount'] = $mediaMonitoringWithoutIssue->count();
|
||
$data['mediaMonitoringWithIssueCount'] = $mediaMonitoringWithIssue->count();
|
||
|
||
$data['mediaMonitoringMonthly'] = $mediaMonitoringMonthly;
|
||
$data['issueManagementMonthly'] = $issueManagementMonthly;
|
||
|
||
return view('dashboard.statistics.show.media-monitoring-issue-management', $data);
|
||
case 'cooperation-completions':
|
||
$data['title'] = 'Statistik Penyelesaian Kerja Sama';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$cooperationCompletion = CooperationCompletion::with(['order', 'media']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$cooperationCompletion->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$cooperationCompletion->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$cooperationCompletion = $cooperationCompletion->get();
|
||
|
||
$cooperationCompletionCount = $cooperationCompletion->groupBy('status')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
// Inisialisasi array 12 bulan untuk masing-masing status
|
||
$monthlyStatus = [
|
||
'waiting' => array_fill(0, 12, 0),
|
||
'accepted' => array_fill(0, 12, 0),
|
||
'rejected' => array_fill(0, 12, 0),
|
||
];
|
||
|
||
// Kelompokkan data berdasarkan status lalu bulan
|
||
$cooperationCompletion->groupBy('status')->each(function ($groupedByStatus, $status) use (&$monthlyStatus) {
|
||
$label = match ((int) $status) {
|
||
0 => 'waiting',
|
||
1 => 'accepted',
|
||
2 => 'rejected',
|
||
default => null,
|
||
};
|
||
|
||
if ($label) {
|
||
$groupedByStatus->groupBy(function ($item) {
|
||
return (int) $item->created_at->format('m'); // bulan sebagai angka 1–12
|
||
})->each(function ($items, $month) use (&$monthlyStatus, $label) {
|
||
$monthlyStatus[$label][$month - 1] = $items->count();
|
||
});
|
||
}
|
||
});
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$cooperationCompletion->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
// Simpan jumlah item tiap bulan ke index bulan - 1 (0-based)
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
// Simpan ke variabel `$data`
|
||
$data['cooperationCompletionStatusMonthly'] = $monthlyStatus;
|
||
$data['cooperationCompletionMonthly'] = $monthlyCount;
|
||
|
||
$data['cooperationCompletionWaiting'] = $cooperationCompletionCount->get('0', 0);
|
||
$data['cooperationCompletionAccepted'] = $cooperationCompletionCount->get('1', 0);
|
||
$data['cooperationCompletionRejected'] = $cooperationCompletionCount->get('2', 0);
|
||
|
||
return view('dashboard.statistics.show.cooperation-completion', $data);
|
||
case 'users':
|
||
$data['title'] = 'Statistik Pengguna';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$registerUsers = User::query();
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$registerUsers->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$registerUsers->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$registerUsers = $registerUsers->get();
|
||
|
||
$registerUsers->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
$data['registerUserMonth'] = $monthlyCount;
|
||
|
||
return view('dashboard.statistics.show.user', $data);
|
||
case 'content-recaps':
|
||
$data['title'] = 'Statistik Rekap Konten';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$contenteRecap = ContentRecap::with(['enhancer', 'classification']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$contenteRecap->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$contenteRecap->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$contenteRecap = $contenteRecap->get();
|
||
|
||
$contentRecapChannelCount = $contenteRecap->groupBy('channel')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
$contentRecapSocialMediaCount = $contenteRecap->groupBy('social_media')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$contenteRecap->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
// Simpan jumlah item tiap bulan ke index bulan - 1 (0-based)
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
$data['contentRecapMonthly'] = $monthlyCount;
|
||
|
||
$data['facebookContentRecap'] = $contentRecapChannelCount->get('facebook', 0);
|
||
$data['twitterContentRecap'] = $contentRecapChannelCount->get('twitter', 0);
|
||
$data['websiteContentRecap'] = $contentRecapChannelCount->get('website', 0);
|
||
$data['youtubeContentRecap'] = $contentRecapChannelCount->get('youtube', 0);
|
||
$data['instagramContentRecap'] = $contentRecapChannelCount->get('instagram', 0);
|
||
$data['tiktokContentRecap'] = $contentRecapChannelCount->get('tiktok', 0);
|
||
$data['cetakContentRecap'] = $contentRecapChannelCount->get('cetak', 0);
|
||
|
||
$data['socialMedia1ContentRecap'] = $contentRecapSocialMediaCount->get('Diskominfo Purwakarta', 0);
|
||
$data['socialMedia2ContentRecap'] = $contentRecapSocialMediaCount->get('PPID Diskominfo Purwakarta', 0);
|
||
$data['socialMedia3ContentRecap'] = $contentRecapSocialMediaCount->get('Purwakarta Saber Hoaks', 0);
|
||
|
||
return view('dashboard.statistics.show.content-recap', $data);
|
||
case 'website-visitors':
|
||
$data['title'] = 'Statistik Pengunjung Website';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$websiteVisitors = Visitor::query();
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$websiteVisitors->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$websiteVisitors->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$websiteVisitors = $websiteVisitors->get();
|
||
|
||
$websiteVisitors->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
$data['websiteVisitorMonth'] = $monthlyCount;
|
||
|
||
return view('dashboard.statistics.show.website-visitor', $data);
|
||
default:
|
||
abort(404);
|
||
}
|
||
}
|
||
|
||
public function export(Request $request, $category)
|
||
{
|
||
|
||
$data = [
|
||
'category' => $category,
|
||
];
|
||
|
||
$year = $request->input('year') ? $request->input('year') : Carbon::now()->year;
|
||
$from_month = $request->input('from_month') ? $request->input('from_month') : null;
|
||
$to_month = $request->input('to_month') ? $request->input('to_month') : null;
|
||
|
||
$data['year'] = $year;
|
||
$data['from_month'] = $from_month;
|
||
$data['to_month'] = $to_month;
|
||
|
||
// dd($from_month.' - '. $to_month);
|
||
|
||
switch ($category) {
|
||
case 'login-activities':
|
||
$data['title'] = 'Statistik Aktivitas Login';
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$loginActivities = LoginHistory::with(['user']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$loginActivities->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
$loginActivities = $loginActivities->get();
|
||
|
||
$loginActivities->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
// Simpan jumlah item tiap bulan ke index bulan - 1 (0-based)
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
$data['loginActivities'] = $monthlyCount;
|
||
|
||
$pdf = Pdf::loadView('dashboard.statistics.exports.pdf.login-activity', $data);
|
||
|
||
return $pdf->stream('aktivitas-login-'.$data['year'].'.pdf');
|
||
case 'media-cooperations':
|
||
$data['title'] = 'Statistik Kerja Sama Media';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$mediaCooperation = MediaCooperation::with(['cooperation', 'media']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$mediaCooperation->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$mediaCooperation->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$mediaCooperation = $mediaCooperation->get();
|
||
|
||
$mediaCooperationCount = $mediaCooperation->groupBy('status')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
// Inisialisasi array 12 bulan untuk masing-masing status
|
||
$monthlyStatus = [
|
||
'waiting' => array_fill(0, 12, 0),
|
||
'accepted' => array_fill(0, 12, 0),
|
||
'rejected' => array_fill(0, 12, 0),
|
||
];
|
||
|
||
// Kelompokkan data berdasarkan status lalu bulan
|
||
$mediaCooperation->groupBy('status')->each(function ($groupedByStatus, $status) use (&$monthlyStatus) {
|
||
$label = match ((int) $status) {
|
||
0 => 'waiting',
|
||
1 => 'accepted',
|
||
2 => 'rejected',
|
||
default => null,
|
||
};
|
||
|
||
if ($label) {
|
||
$groupedByStatus->groupBy(function ($item) {
|
||
return (int) $item->created_at->format('m'); // bulan sebagai angka 1–12
|
||
})->each(function ($items, $month) use (&$monthlyStatus, $label) {
|
||
$monthlyStatus[$label][$month - 1] = $items->count();
|
||
});
|
||
}
|
||
});
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$mediaCooperation->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
// Simpan jumlah item tiap bulan ke index bulan - 1 (0-based)
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
// Simpan ke variabel `$data`
|
||
$data['mediaCooperationStatusMonthly'] = $monthlyStatus;
|
||
$data['mediaCooperationMonthly'] = $monthlyCount;
|
||
|
||
$data['mediaCooperationWaiting'] = $mediaCooperationCount->get('0', 0);
|
||
$data['mediaCooperationAccepted'] = $mediaCooperationCount->get('1', 0);
|
||
$data['mediaCooperationRejected'] = $mediaCooperationCount->get('2', 0);
|
||
|
||
// dd($data);
|
||
|
||
$pdf = Pdf::loadView('dashboard.statistics.exports.pdf.media-cooperation', $data);
|
||
|
||
return $pdf->stream('kerja-sama-media.pdf');
|
||
case 'airing-proof-reports':
|
||
$data['title'] = 'Statistik Laporan Bukti Tayang';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$airingProofReport = Report::with(['order', 'media']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$airingProofReport->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$airingProofReport->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$airingProofReport = $airingProofReport->get();
|
||
|
||
$airingProofReportCount = $airingProofReport->groupBy('status')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
// Inisialisasi array 12 bulan untuk masing-masing status
|
||
$monthlyStatus = [
|
||
'waiting' => array_fill(0, 12, 0),
|
||
'accepted' => array_fill(0, 12, 0),
|
||
'rejected' => array_fill(0, 12, 0),
|
||
];
|
||
|
||
// Kelompokkan data berdasarkan status lalu bulan
|
||
$airingProofReport->groupBy('status')->each(function ($groupedByStatus, $status) use (&$monthlyStatus) {
|
||
$label = match ((int) $status) {
|
||
0 => 'waiting',
|
||
1 => 'accepted',
|
||
2 => 'rejected',
|
||
default => null,
|
||
};
|
||
|
||
if ($label) {
|
||
$groupedByStatus->groupBy(function ($item) {
|
||
return (int) $item->created_at->format('m'); // bulan sebagai angka 1–12
|
||
})->each(function ($items, $month) use (&$monthlyStatus, $label) {
|
||
$monthlyStatus[$label][$month - 1] = $items->count();
|
||
});
|
||
}
|
||
});
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$airingProofReport->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->publication_date)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
// Simpan jumlah item tiap bulan ke index bulan - 1 (0-based)
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
// Simpan ke variabel `$data`
|
||
$data['airingProofReportStatusMonthly'] = $monthlyStatus;
|
||
$data['airingProofReportPublicationMonthly'] = $monthlyCount;
|
||
|
||
$data['airingProofReportWaiting'] = $airingProofReportCount->get('0', 0);
|
||
$data['airingProofReportAccepted'] = $airingProofReportCount->get('1', 0);
|
||
$data['airingProofReportRejected'] = $airingProofReportCount->get('2', 0);
|
||
|
||
$pdf = Pdf::loadView('dashboard.statistics.exports.pdf.airing-proof-report', $data);
|
||
|
||
return $pdf->stream('laporan-bukti-tayang.pdf');
|
||
case 'media-monitorings-issue-management':
|
||
$data['title'] = 'Statistik Monitoring Media & Manajemen Isu';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
// Statistik Isu
|
||
$issues = IssueManagement::with(['mediaMonitoring']);
|
||
$mediaMonitorings = MediaMonitoring::with(['issue']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$issues->whereYear('created_at', $year);
|
||
$mediaMonitorings->whereYear('created_at', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$issues->whereBetween('created_at', [$start, $end]);
|
||
$mediaMonitorings->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$issues = $issues->get();
|
||
$mediaMonitorings = $mediaMonitorings->get();
|
||
|
||
$issueCounts = $issues->groupBy('issue')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
$data['crisisIssue'] = $issueCounts->get('krisis', 0);
|
||
$data['negativeIssue'] = $issueCounts->get('negatif', 0);
|
||
$data['neutralIssue'] = $issueCounts->get('netral', 0);
|
||
$data['positiveIssue'] = $issueCounts->get('positif', 0);
|
||
// Akhir Statistik Isu
|
||
|
||
// Statistik Monitoring Media
|
||
$mediaMonitoringCount = $mediaMonitorings->groupBy('channel')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
$data['facebookMediaMonitoring'] = $mediaMonitoringCount->get('facebook', 0);
|
||
$data['twitterMediaMonitoring'] = $mediaMonitoringCount->get('twitter', 0);
|
||
$data['websiteMediaMonitoring'] = $mediaMonitoringCount->get('website', 0);
|
||
$data['youtubeMediaMonitoring'] = $mediaMonitoringCount->get('youtube', 0);
|
||
$data['instagramMediaMonitoring'] = $mediaMonitoringCount->get('instagram', 0);
|
||
$data['tiktokMediaMonitoring'] = $mediaMonitoringCount->get('tiktok', 0);
|
||
$data['cetakMediaMonitoring'] = $mediaMonitoringCount->get('cetak', 0);
|
||
// Akhir Statistik Monitoring Media
|
||
|
||
// Media Monitoring 2 (pakai koleksi yang sudah ada)
|
||
$mediaMonitoringMonthly = array_fill(0, 12, 0); // Init array 12 bulan
|
||
$mediaMonitorings->groupBy(function ($item) {
|
||
return (int) $item->created_at->format('m'); // bulan sebagai int
|
||
})->each(function ($group, $month) use (&$mediaMonitoringMonthly) {
|
||
$mediaMonitoringMonthly[$month - 1] = $group->count();
|
||
});
|
||
|
||
// Issue Management 2 (pakai koleksi yang sudah ada)
|
||
$issueManagementMonthly = array_fill(0, 12, 0);
|
||
$issues->groupBy(function ($item) {
|
||
return (int) $item->created_at->format('m');
|
||
})->each(function ($group, $month) use (&$issueManagementMonthly) {
|
||
$issueManagementMonthly[$month - 1] = $group->count();
|
||
});
|
||
|
||
$mediaMonitoringWithoutIssue = MediaMonitoring::doesntHave('issue')
|
||
->when($year && $year !== 'all' && ! (filled($from_month) || filled($to_month)), function ($query) use ($year) {
|
||
$query->whereYear('created_at', $year);
|
||
})
|
||
->when($from_month && $to_month, function ($query) use ($from_month, $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
$query->whereBetween('created_at', [$start, $end]);
|
||
})
|
||
->get();
|
||
|
||
$mediaMonitoringWithIssue = MediaMonitoring::has('issue')
|
||
->when($year && $year !== 'all' && ! (filled($from_month) || filled($to_month)), function ($query) use ($year) {
|
||
$query->whereYear('created_at', $year);
|
||
})
|
||
->when($from_month && $to_month, function ($query) use ($from_month, $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
$query->whereBetween('created_at', [$start, $end]);
|
||
})
|
||
->get();
|
||
|
||
$data['mediaMonitoringWithoutIssueCount'] = $mediaMonitoringWithoutIssue->count();
|
||
$data['mediaMonitoringWithIssueCount'] = $mediaMonitoringWithIssue->count();
|
||
|
||
$data['mediaMonitoringMonthly'] = $mediaMonitoringMonthly;
|
||
$data['issueManagementMonthly'] = $issueManagementMonthly;
|
||
|
||
$pdf = Pdf::loadView('dashboard.statistics.exports.pdf.media-monitoring-issue-management', $data);
|
||
|
||
return $pdf->stream('monitoring-media-manajemen-isu.pdf');
|
||
case 'cooperation-completions':
|
||
$data['title'] = 'Statistik Penyelesaian Kerja Sama';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$cooperationCompletion = CooperationCompletion::with(['order', 'media']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$cooperationCompletion->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$cooperationCompletion->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$cooperationCompletion = $cooperationCompletion->get();
|
||
|
||
$cooperationCompletionCount = $cooperationCompletion->groupBy('status')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
// Inisialisasi array 12 bulan untuk masing-masing status
|
||
$monthlyStatus = [
|
||
'waiting' => array_fill(0, 12, 0),
|
||
'accepted' => array_fill(0, 12, 0),
|
||
'rejected' => array_fill(0, 12, 0),
|
||
];
|
||
|
||
// Kelompokkan data berdasarkan status lalu bulan
|
||
$cooperationCompletion->groupBy('status')->each(function ($groupedByStatus, $status) use (&$monthlyStatus) {
|
||
$label = match ((int) $status) {
|
||
0 => 'waiting',
|
||
1 => 'accepted',
|
||
2 => 'rejected',
|
||
default => null,
|
||
};
|
||
|
||
if ($label) {
|
||
$groupedByStatus->groupBy(function ($item) {
|
||
return (int) $item->created_at->format('m'); // bulan sebagai angka 1–12
|
||
})->each(function ($items, $month) use (&$monthlyStatus, $label) {
|
||
$monthlyStatus[$label][$month - 1] = $items->count();
|
||
});
|
||
}
|
||
});
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$cooperationCompletion->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
// Simpan jumlah item tiap bulan ke index bulan - 1 (0-based)
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
// Simpan ke variabel `$data`
|
||
$data['cooperationCompletionStatusMonthly'] = $monthlyStatus;
|
||
$data['cooperationCompletionMonthly'] = $monthlyCount;
|
||
|
||
$data['cooperationCompletionWaiting'] = $cooperationCompletionCount->get('0', 0);
|
||
$data['cooperationCompletionAccepted'] = $cooperationCompletionCount->get('1', 0);
|
||
$data['cooperationCompletionRejected'] = $cooperationCompletionCount->get('2', 0);
|
||
|
||
$pdf = Pdf::loadView('dashboard.statistics.exports.pdf.cooperation-completion', $data);
|
||
|
||
return $pdf->stream('penyelesaian-kerja-sama.pdf');
|
||
case 'users':
|
||
$data['title'] = 'Statistik Pengguna';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$registerUsers = User::query();
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$registerUsers->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$registerUsers->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$registerUsers = $registerUsers->get();
|
||
|
||
$registerUsers->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
$data['registerUserMonth'] = $monthlyCount;
|
||
|
||
$pdf = Pdf::loadView('dashboard.statistics.exports.pdf.user', $data);
|
||
|
||
return $pdf->stream('pengguna.pdf');
|
||
case 'content-recaps':
|
||
$data['title'] = 'Statistik Rekap Konten';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$contenteRecap = ContentRecap::with(['enhancer', 'classification']);
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$contenteRecap->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$contenteRecap->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$contenteRecap = $contenteRecap->get();
|
||
|
||
$contentRecapChannelCount = $contenteRecap->groupBy('channel')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
$contentRecapSocialMediaCount = $contenteRecap->groupBy('social_media')->map(function ($group) {
|
||
return $group->count();
|
||
});
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$contenteRecap->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
// Simpan jumlah item tiap bulan ke index bulan - 1 (0-based)
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
$data['contentRecapMonthly'] = $monthlyCount;
|
||
|
||
$data['facebookContentRecap'] = $contentRecapChannelCount->get('facebook', 0);
|
||
$data['twitterContentRecap'] = $contentRecapChannelCount->get('twitter', 0);
|
||
$data['websiteContentRecap'] = $contentRecapChannelCount->get('website', 0);
|
||
$data['youtubeContentRecap'] = $contentRecapChannelCount->get('youtube', 0);
|
||
$data['instagramContentRecap'] = $contentRecapChannelCount->get('instagram', 0);
|
||
$data['tiktokContentRecap'] = $contentRecapChannelCount->get('tiktok', 0);
|
||
$data['cetakContentRecap'] = $contentRecapChannelCount->get('cetak', 0);
|
||
|
||
$data['socialMedia1ContentRecap'] = $contentRecapSocialMediaCount->get('Diskominfo Purwakarta', 0);
|
||
$data['socialMedia2ContentRecap'] = $contentRecapSocialMediaCount->get('PPID Diskominfo Purwakarta', 0);
|
||
$data['socialMedia3ContentRecap'] = $contentRecapSocialMediaCount->get('Purwakarta Saber Hoaks', 0);
|
||
|
||
$pdf = Pdf::loadView('dashboard.statistics.exports.pdf.content-recap', $data);
|
||
|
||
return $pdf->stream('rekap-konten.pdf');
|
||
case 'website-visitors':
|
||
$data['title'] = 'Statistik Pengunjung Website';
|
||
$data['data_time'] = $year === 'all' ? 'Semua' : 'Tahun '.$year;
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$websiteVisitors = Visitor::query();
|
||
|
||
if ($year && $year !== 'all' && ! (filled($from_month) || filled($to_month))) {
|
||
$websiteVisitors->whereYear('created_at', '=', $year);
|
||
}
|
||
|
||
if ($from_month && $to_month) {
|
||
$start = Carbon::parse($from_month)->startOfMonth();
|
||
$end = Carbon::parse($to_month)->endOfMonth();
|
||
|
||
// Asumsikan from_month dan to_month dalam format 'YYYY-MM'
|
||
$fromDate = $from_month ? Carbon::createFromFormat('Y-m', $from_month) : null;
|
||
$toDate = $to_month ? Carbon::createFromFormat('Y-m', $to_month) : null;
|
||
|
||
// Format ke bentuk "Mei 2025"
|
||
$fromFormatted = $fromDate ? $fromDate->translatedFormat('F Y') : '-';
|
||
$toFormatted = $toDate ? $toDate->translatedFormat('F Y') : '-';
|
||
|
||
$data['data_time'] = $fromFormatted.' - '.$toFormatted;
|
||
|
||
$websiteVisitors->whereBetween('created_at', [$start, $end]);
|
||
}
|
||
|
||
$websiteVisitors = $websiteVisitors->get();
|
||
|
||
$websiteVisitors->groupBy(function ($item) {
|
||
return (int) \Carbon\Carbon::parse($item->created_at)->format('m');
|
||
})->each(function ($items, $month) use (&$monthlyCount) {
|
||
$monthlyCount[$month - 1] = $items->count();
|
||
});
|
||
|
||
$data['websiteVisitorMonth'] = $monthlyCount;
|
||
|
||
$pdf = Pdf::loadView('dashboard.statistics.exports.pdf.website-visitor', $data);
|
||
|
||
return $pdf->stream('pengunjung-aplikasi.pdf');
|
||
default:
|
||
abort(404);
|
||
}
|
||
}
|
||
}
|