From 8b200ef35e9d31d36c0d34384f9442da41c699a1 Mon Sep 17 00:00:00 2001 From: Adam Lavin Date: Tue, 21 Apr 2015 12:45:36 +0100 Subject: [PATCH] Refactored the way incidents are pulled out of the database + Sort fix Incidents are now pulled out via the database, grouped together via php, then missing days are added to the data. --- app/Http/Controllers/HomeController.php | 26 +++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 95687216..2cb6aa13 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -74,25 +74,31 @@ class HomeController extends AbstractController $metrics = Metric::where('display_chart', 1)->get(); } - $allIncidents = []; $daysToShow = Setting::get('app_incident_days') ?: 7; $incidentDays = range(0, $daysToShow); $dateFormat = Setting::get('date_format') ?: 'jS F Y'; + $allIncidents = Incident::notScheduled()->whereBetween('created_at', [ + $startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00', + $startDate->format('Y-m-d').' 23:59:59', + ])->orderBy('created_at', 'desc')->get()->groupBy(function (Incident $incident) use ($dateFormat) { + return $incident->created_at->format($dateFormat); + }); + + // Add in days that have no incidents foreach ($incidentDays as $i) { $date = $startDate->copy()->subDays($i); - $incidents = Incident::notScheduled()->whereBetween('created_at', [ - $date->format('Y-m-d').' 00:00:00', - $date->format('Y-m-d').' 23:59:59', - ])->orderBy('created_at', 'desc')->get(); - - $allIncidents[] = [ - 'date' => (new Date($date->toDateString()))->format($dateFormat), - 'incidents' => $incidents, - ]; + if (!isset($allIncidents[$date->format($dateFormat)])) { + $allIncidents[$date->format($dateFormat)] = []; + } } + // Sort the array so it takes into account the added days + $allIncidents->sortBy(function ($value, $key) { + return $key; + }, SORT_REGULAR, true); + // Scheduled maintenance code. $scheduledMaintenance = Incident::scheduled()->orderBy('scheduled_at')->get();