Skip to content

Commit

Permalink
Refactor showIndex View
Browse files Browse the repository at this point in the history
  • Loading branch information
rarguelloF committed Dec 11, 2017
1 parent 0f3289a commit 7281b01
Showing 1 changed file with 77 additions and 80 deletions.
157 changes: 77 additions & 80 deletions app/Http/Controllers/StatusPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,80 +38,80 @@
*/
class StatusPageController extends AbstractApiController
{
private function showIndexByDate()
/**
* Displays the status page.
*
* @return \Illuminate\View\View
*/
public function showIndex()
{
$today = Date::now();
$startDate = Date::now();

// Check if we have another starting date
if (Binput::has('start_date')) {
try {
// If date provided is valid
$oldDate = Date::createFromFormat('Y-m-d', Binput::get('start_date'));

// If trying to get a future date fallback to today
if ($today->gt($oldDate)) {
$startDate = $oldDate;
}
} catch (Exception $e) {
// Fallback to today
}
}

$only_disrupted_days = Config::get('setting.only_disrupted_days');
$appIncidentDays = (int) Config::get('setting.app_incident_days', 1);
$incidentDays = array_pad([], $appIncidentDays, null);

$allIncidents = Incident::where('visible', '>=', (int) !Auth::check())->whereBetween('occurred_at', [
$startDate->copy()->subDays($appIncidentDays)->format('Y-m-d').' 00:00:00',
$startDate->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();
});
// Used for the database query
$startDate = Date::now();
$endDate = Date::now();

$canPageForward = false;
$canPageBackward = false;
$previousDate = null;
$nextDate = null;

// Add in days that have no incidents
foreach ($incidentDays as $i => $day) {
$date = app(DateFactory::class)->make($startDate)->subDays($i);
if ($only_disrupted_days) {
// In this case, start_date GET parameter means the page
$page = Binput::get('start_date', 0);

if (!isset($allIncidents[$date->toDateString()])) {
$allIncidents[$date->toDateString()] = [];
if (!is_numeric($page)) {
$page = 0;
}
}

// Sort the array so it takes into account the added days
$allIncidents = $allIncidents->sortBy(function ($value, $key) {
return strtotime($key);
}, SORT_REGULAR, true);
$page = (int) $page;

return View::make('index')
->withDaysToShow($appIncidentDays)
->withAllIncidents($allIncidents)
->withCanPageForward((bool) $today->gt($startDate))
->withCanPageBackward(Incident::where('occurred_at', '<', $startDate->format('Y-m-d'))->count() > 0)
->withPreviousDate($startDate->copy()->subDays($appIncidentDays)->toDateString())
->withNextDate($startDate->copy()->addDays($appIncidentDays)->toDateString());
}
$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();

private function showIndexByQuantity()
{
$appIncidentDays = (int) Config::get('setting.app_incident_days', 7);
$page = Binput::get('start_date', 0);
$numIncidentDays = count($allIncidentDays);
$numPages = round($numIncidentDays / $appIncidentDays);

$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();
$selectedDays = $allIncidentDays->slice($page * $appIncidentDays, $appIncidentDays)->all();

$numIncidentDays = count($allIncidentDays);
$numPages = round($numIncidentDays / $appIncidentDays);
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]);
}

$startDate = Date::now();
$endDate = Date::now();
$canPageForward = $page > 0;
$canPageBackward = ($page + 1) < $numPages;
$previousDate = $page + 1;
$nextDate = $page - 1;
} else {
$today = Date::now();
$date = Date::now();

// Check if we have another starting date
if (Binput::has('start_date')) {
try {
// If date provided is valid
$oldDate = Date::createFromFormat('Y-m-d', Binput::get('start_date'));

// If trying to get a future date fallback to today
if ($today->gt($oldDate)) {
$date = $oldDate;
}
} catch (Exception $e) {
// Fallback to today
}
}

$selectedDays = $allIncidentDays->slice($page * $appIncidentDays, $appIncidentDays)->all();
$startDate = $date->copy()->subDays($appIncidentDays);
$endDate = $date->copy();

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]);
$canPageForward = (bool) $today->gt($date);
$canPageBackward = Incident::where('occurred_at', '<', $date->format('Y-m-d'))->count() > 0;
$previousDate = $date->copy()->subDays($appIncidentDays)->toDateString();
$nextDate = $date->copy()->addDays($appIncidentDays)->toDateString();
}

$allIncidents = Incident::where('visible', '>=', (int) !Auth::check())->whereBetween('occurred_at', [
Expand All @@ -121,6 +121,19 @@ private function showIndexByQuantity()
return app(DateFactory::class)->make($incident->occurred_at)->toDateString();
});

if (!$only_disrupted_days) {
$incidentDays = array_pad([], $appIncidentDays, null);

// Add in days that have no incidents
foreach ($incidentDays as $i => $day) {
$date = app(DateFactory::class)->make($startDate)->subDays($i);

if (!isset($allIncidents[$date->toDateString()])) {
$allIncidents[$date->toDateString()] = [];
}
}
}

// Sort the array so it takes into account the added days
$allIncidents = $allIncidents->sortBy(function ($value, $key) {
return strtotime($key);
Expand All @@ -129,26 +142,10 @@ private function showIndexByQuantity()
return View::make('index')
->withDaysToShow($appIncidentDays)
->withAllIncidents($allIncidents)
->withCanPageForward($page > 0)
->withCanPageBackward(($page + 1) < $numPages)
->withPreviousDate($page + 1)
->withNextDate($page - 1);
}

/**
* Displays the status page.
*
* @return \Illuminate\View\View
*/
public function showIndex()
{
$only_disrupted_days = Config::get('setting.only_disrupted_days');

if ($only_disrupted_days) {
return $this->showIndexByQuantity();
}

return $this->showIndexByDate();
->withCanPageForward($canPageForward)
->withCanPageBackward($canPageBackward)
->withPreviousDate($previousDate)
->withNextDate($nextDate);
}

/**
Expand Down

0 comments on commit 7281b01

Please sign in to comment.