Cachet/app/Http/Controllers/HomeController.php

120 lines
4.2 KiB
PHP
Raw Normal View History

2014-11-16 22:26:08 +00:00
<?php
/*
* This file is part of Cachet.
*
* (c) James Brooks <james@cachethq.io>
*
* 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\Component;
2015-03-20 18:30:45 -06:00
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Metric;
2015-01-27 13:05:54 -06:00
use Carbon\Carbon;
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;
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
2015-03-20 18:30:45 -06:00
class HomeController extends AbstractController
2015-01-01 15:45:04 +00:00
{
/**
* Returns the rendered Blade templates.
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()
{
2015-01-27 13:05:54 -06:00
$today = Carbon::now();
$startDate = Carbon::now();
2015-03-20 18:30:45 -06:00
segment_track('Status Page', [
'event' => 'Landed',
]);
// 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'));
segment_track('Status Page', [
'start_date' => $oldDate->format('Y-m-d'),
]);
if (Setting::get('app_tracking')) {
Segment::track([
'userId' => Config::get('app.key'),
'event' => 'Home Page',
'properties' => [
'start_date' => $oldDate,
],
]);
}
// If trying to get a future date fallback to today
if ($today->gt($oldDate)) {
$startDate = $oldDate;
}
} catch (Exception $e) {
// Fallback to today
}
}
$metrics = null;
if ($displayMetrics = Setting::get('display_graphs')) {
$metrics = Metric::where('display_chart', 1)->get();
}
2015-03-20 18:30:45 -06:00
$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);
});
2015-01-27 13:05:54 -06:00
// Add in days that have no incidents
foreach($incidentDays as $i) {
$date = $startDate->copy()->subDays($i);
2015-01-27 13:05:54 -06:00
if (!isset($allIncidents[$date->format($dateFormat)])) {
$allIncidents[$date->format($dateFormat)] = [];
}
2015-01-01 21:58:27 +00:00
}
2015-03-20 18:30:45 -06:00
// Scheduled maintenance code.
$scheduledMaintenance = Incident::scheduled()->orderBy('scheduled_at')->get();
// Component & Component Group lists.
$usedComponentGroups = Component::where('group_id', '>', 0)->groupBy('group_id')->lists('group_id');
$componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->get();
$ungroupedComponents = Component::where('group_id', 0)->orderBy('order')->orderBy('created_at')->get();
2014-12-30 22:25:38 +00:00
return View::make('index', [
2015-03-20 18:30:45 -06:00
'componentGroups' => $componentGroups,
'ungroupedComponents' => $ungroupedComponents,
'displayMetrics' => $displayMetrics,
'metrics' => $metrics,
'allIncidents' => $allIncidents,
'scheduledMaintenance' => $scheduledMaintenance,
'pageTitle' => Setting::get('app_name'),
2015-03-20 18:30:45 -06:00
'aboutApp' => Markdown::convertToHtml(Setting::get('app_about')),
'canPageForward' => (bool) $today->gt($startDate),
'previousDate' => $startDate->copy()->subWeek()->subDay()->toDateString(),
'nextDate' => $startDate->copy()->addWeek()->addDay()->toDateString(),
2014-12-30 22:25:38 +00:00
]);
}
}