Cachet/src/Http/Controllers/HomeController.php

69 lines
2.3 KiB
PHP
Raw Normal View History

2014-11-16 22:26:08 +00:00
<?php
namespace CachetHQ\Cachet\Http\Controllers;
2014-12-01 16:46:56 +00:00
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Setting;
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 15:45:04 +00:00
use Illuminate\Routing\Controller;
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-01-01 15:45:04 +00:00
class HomeController extends Controller
{
/**
* 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()
{
$components = Component::orderBy('order')->orderBy('created_at')->get();
2015-01-01 21:58:27 +00:00
$allIncidents = [];
$incidentDays = Setting::get('app_incident_days') ?: 7;
$today = Date::now();
$startDate = Date::now();
$dateFormat = Setting::get('date_format') ?: 'jS F Y';
// 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
}
}
foreach (range(0, $incidentDays) as $i) {
$date = $startDate->copy()->subDays($i);
2015-01-01 21:58:27 +00:00
$incidents = Incident::whereBetween('created_at', [
$date->format('Y-m-d').' 00:00:00',
$date->format('Y-m-d').' 23:59:59',
])->orderBy('created_at', 'desc')->get();
$allIncidents[] = ['date' => $date->format($dateFormat), 'incidents' => $incidents];
2015-01-01 21:58:27 +00:00
}
2014-12-30 22:25:38 +00:00
return View::make('index', [
'components' => $components,
'allIncidents' => $allIncidents,
'pageTitle' => Setting::get('app_name'),
'aboutApp' => Markdown::render(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
]);
}
}