279 lines
11 KiB
PHP
279 lines
11 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Dashboard;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\IssueManagement;
|
||
use App\Models\LoginHistory;
|
||
use App\Models\MediaMonitoring;
|
||
use App\Models\Report;
|
||
use Carbon\Carbon;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
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' => false
|
||
],
|
||
[
|
||
'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' => false
|
||
],
|
||
[
|
||
'name' => 'Pengguna',
|
||
'category' => 'users',
|
||
'is_available' => false
|
||
],
|
||
[
|
||
'name' => 'Perusahaan',
|
||
'category' => 'companies',
|
||
'is_available' => false
|
||
],
|
||
[
|
||
'name' => 'Rekap Konten',
|
||
'category' => 'content-recaps',
|
||
'is_available' => false
|
||
],
|
||
[
|
||
'name' => 'Pengunjung Website',
|
||
'category' => 'website-visitors',
|
||
'is_available' => false
|
||
],
|
||
];
|
||
|
||
|
||
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-cooperations',
|
||
'media',
|
||
'cooperation-completions',
|
||
'users',
|
||
'companies',
|
||
'content-recaps',
|
||
'website-visitors',
|
||
];
|
||
|
||
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 'media-monitorings-issue-management':
|
||
$data['title'] = 'Statistik Monitoring Media & Manajemen Isu';
|
||
|
||
// Statistik Isu
|
||
$issues = IssueManagement::with(['mediaMonitoring']);
|
||
$mediaMonitorings = MediaMonitoring::with(['issue']);
|
||
|
||
if($year && $year !== 'all'){
|
||
$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();
|
||
|
||
$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', 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', 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 'airing-proof-reports':
|
||
$data['title'] = 'Statistik Laporan Bukti Tayang';
|
||
|
||
$airingProofReport = Report::with(['order', 'media'])->get();
|
||
$airingProofThemeCount = $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['proofAiringThemeWaiting'] = $airingProofThemeCount->get('0', 0);
|
||
$data['proofAiringThemeAccepted'] = $airingProofThemeCount->get('1', 0);
|
||
$data['proofAiringThemeRejected'] = $airingProofThemeCount->get('2', 0);
|
||
|
||
return view('dashboard.statistics.show.airing-proof-report', $data);
|
||
case 'login-activities':
|
||
$data['title'] = 'Statistik Aktivitas Login';
|
||
|
||
$monthlyCount = array_fill(0, 12, 0);
|
||
|
||
$loginActivities = LoginHistory::with(['user'])->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);
|
||
|
||
default:
|
||
abort(404);
|
||
}
|
||
}
|
||
}
|