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 c341aa9 commit 7ec57e6
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions app/Http/Controllers/StatusPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
*/
class StatusPageController extends AbstractApiController
{

private function showIndexByDate() {
private function showIndexByDate()
{
$today = Date::now();
$startDate = Date::now();

Expand Down Expand Up @@ -93,14 +93,33 @@ private function showIndexByDate() {
->withNextDate($startDate->copy()->addDays($appIncidentDays)->toDateString());
}


private function showIndexByQuantity() {
$appIncidentQuantity = (int) Config::get('setting.app_incident_days', 7);
private function showIndexByQuantity()
{
$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 7ec57e6

Please sign in to comment.