Cachet/app/Http/Controllers/StatusPageController.php

100 lines
3.3 KiB
PHP
Raw Normal View History

2014-11-16 22:26:08 +00:00
<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Http\Controllers;
2014-12-01 16:46:56 +00:00
2015-01-14 17:19:41 -06:00
use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Models\Incident;
2015-01-04 12:33:38 +00:00
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
2015-01-01 19:44:15 +00:00
use GrahamCampbell\Markdown\Facades\Markdown;
use Illuminate\Routing\Controller;
2015-05-20 08:41:02 +01:00
use Illuminate\Support\Facades\Auth;
2015-01-01 16:18:24 +00:00
use Illuminate\Support\Facades\View;
use Jenssegers\Date\Date;
2014-12-01 16:46:56 +00:00
class StatusPageController extends Controller
2015-01-01 15:45:04 +00:00
{
/**
* Displays the status page.
2014-12-29 23:07:46 +00:00
*
2014-12-01 08:38:26 +00:00
* @return \Illuminate\View\View
*/
2014-12-20 21:20:17 +00:00
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
}
}
2015-03-20 18:30:45 -06:00
$daysToShow = Setting::get('app_incident_days') ?: 7;
2015-05-02 09:19:01 +01:00
$incidentDays = range(0, $daysToShow - 1);
2015-05-07 09:09:03 -05:00
$dateTimeZone = Setting::get('app_timezone');
2015-05-20 08:41:02 +01:00
$incidentVisiblity = Auth::check() ? 0 : 1;
$allIncidents = Incident::notScheduled()->where('visible', '>=', $incidentVisiblity)->whereBetween('created_at', [
$startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00',
$startDate->format('Y-m-d').' 23:59:59',
2015-05-07 09:09:03 -05:00
])->orderBy('created_at', 'desc')->get()->groupBy(function (Incident $incident) use ($dateTimeZone) {
return (new Date($incident->created_at))
->setTimezone($dateTimeZone)->toDateString();
});
// Add in days that have no incidents
2015-04-20 23:44:29 +01:00
foreach ($incidentDays as $i) {
2015-05-07 09:09:03 -05:00
$date = (new Date($startDate))->setTimezone($dateTimeZone)->subDays($i);
2015-01-27 13:05:54 -06:00
2015-05-07 09:09:03 -05:00
if (!isset($allIncidents[$date->toDateString()])) {
$allIncidents[$date->toDateString()] = [];
}
2015-01-01 21:58:27 +00:00
}
// Sort the array so it takes into account the added days
2015-05-29 20:43:12 +01:00
$allIncidents = $allIncidents->sortBy(function ($value, $key) {
2015-05-02 09:09:02 +01:00
return strtotime($key);
2015-05-29 21:56:06 +01:00
}, SORT_REGULAR, true)->all();
return View::make('index')
->withAllIncidents($allIncidents)
->withAboutApp(Markdown::convertToHtml(Setting::get('app_about')))
->withCanPageForward((bool) $today->gt($startDate))
->withCanPageBackward(Incident::notScheduled()->where('created_at', '<', $startDate->format('Y-m-d'))->count() > 0)
->withPreviousDate($startDate->copy()->subDays($daysToShow)->toDateString())
->withNextDate($startDate->copy()->addDays($daysToShow)->toDateString());
}
/**
* Shows an incident in more detail.
*
* @param \CachetHQ\Cachet\Models\Incident $incident
*
* @return \Illuminate\View\View
*/
public function showIncident(Incident $incident)
{
}
}