Pagination on status page, previous and next week

This commit is contained in:
Joseph Cohen
2015-01-03 00:36:27 -06:00
parent da8f4e7b0f
commit 44bb043a2b
3 changed files with 44 additions and 5 deletions

View File

@@ -35,6 +35,8 @@ return [
'setup' => 'Setup Cachet',
'no_incidents' => 'No incidents reported.',
'past_incidents' => 'Past incidents',
'previous_week' => 'Previous week',
'next_week' => 'Next week',
// Dashboard
'dashboard' => [
'dashboard' => 'Dashboard',

View File

@@ -29,4 +29,20 @@
@foreach($allIncidents as $incidents)
@include('partials.incidents', $incidents)
@endforeach
<hr/>
<nav>
<ul class="pager">
<li class="previous">
<a href="{{ route('status-page') }}?start_date={{ $previousDate }}">
<span aria-hidden="true">&larr;</span> {{ trans('cachet.previous_week') }}
</a>
</li>
<li class="next @if( ! $canPageForward) disabled @endif">
<a @if($canPageForward) href="{{ route('status-page') }}?start_date={{ $nextDate }}" @endif>
{{ trans('cachet.next_week') }} <span aria-hidden="true">&rarr;</span>
</a>
</li>
</ul>
</nav>
@stop

View File

@@ -6,6 +6,7 @@ use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Setting;
use Carbon\Carbon;
use GrahamCampbell\Binput\Facades\Binput;
use GrahamCampbell\Markdown\Facades\Markdown;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\View;
@@ -25,8 +26,25 @@ class HomeController extends Controller
$incidentDays = Setting::get('app_incident_days') ?: 7;
$today = Carbon::now();
$startDate = Carbon::now();
// Check if we have another starting date
if (Binput::has('start_date')) {
try {
// If date provided is valid
$oldDate = Carbon::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 = Carbon::now()->subDays($i);
$date = $startDate->copy()->subDays($i);
$incidents = Incident::whereBetween('created_at', [
$date->format('Y-m-d').' 00:00:00',
$date->format('Y-m-d').' 23:59:59',
@@ -35,10 +53,13 @@ class HomeController extends Controller
}
return View::make('index', [
'components' => $components,
'allIncidents' => $allIncidents,
'pageTitle' => Setting::get('app_name'),
'aboutApp' => Markdown::render(Setting::get('app_about')),
'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(),
]);
}
}