Skip to content

Commit

Permalink
Modify only_display_incidents behaviour to take into account number o…
Browse files Browse the repository at this point in the history
…f days instead of number of incidents
  • Loading branch information
rarguelloF committed Nov 30, 2017
1 parent 7575ac3 commit 57fa6c5
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions app/Http/Controllers/StatusPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,31 @@ private function showIndexByDate()

private function showIndexByQuantity()
{
$appIncidentQuantity = (int) Config::get('setting.app_incident_days', 7);
$appIncidentDays = (int) Config::get('setting.app_incident_days', 7);
$page = Binput::get('start_date', 0);

$allIncidents = Incident::where('visible', '>=', (int) !Auth::check())->
limit($appIncidentQuantity)->offset($appIncidentQuantity * $page)->
orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) {
$allIncidentDays = Incident::where('visible', '>=', (int) !Auth::check())
->select('occurred_at')->distinct()->orderBy('occurred_at', 'desc')->get()->map(function (Incident $incident) {
return app(DateFactory::class)->make($incident->occurred_at)->toDateString();
})->unique()->values();

$numIncidentDays = count($allIncidentDays);
$numPages = round($numIncidentDays / $appIncidentDays);

$startDate = Date::now();
$endDate = Date::now();

$selectedDays = $allIncidentDays->slice($page * $appIncidentDays, $appIncidentDays)->all();

if (count($selectedDays) > 0) {
$startDate = Date::createFromFormat('Y-m-d', array_values(array_slice($selectedDays, -1))[0]);
$endDate = Date::createFromFormat('Y-m-d', array_values($selectedDays)[0]);
}

$allIncidents = Incident::where('visible', '>=', (int) !Auth::check())->whereBetween('occurred_at', [
$startDate->format('Y-m-d').' 00:00:00',
$endDate->format('Y-m-d').' 23:59:59',
])->orderBy('occurred_at', 'desc')->get()->groupBy(function (Incident $incident) {
return app(DateFactory::class)->make($incident->occurred_at)->toDateString();
});

Expand All @@ -109,11 +128,8 @@ private function showIndexByQuantity()
return strtotime($key);
}, SORT_REGULAR, true);

$numIncidents = Incident::count();
$numPages = round($numIncidents / $appIncidentQuantity);

return View::make('index')
->withDaysToShow($appIncidentQuantity)
->withDaysToShow($appIncidentDays)
->withAllIncidents($allIncidents)
->withCanPageForward($page > 0)
->withCanPageBackward(($page + 1) < $numPages)
Expand Down

0 comments on commit 57fa6c5

Please sign in to comment.