forked from GitaminHQ/Gitamin
/
DashboardController.php
138 lines (119 loc) · 3.94 KB
/
DashboardController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/*
* This file is part of Gitamin.
*
* Copyright (C) 2015-2016 The Gitamin Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Gitamin\Http\Controllers;
use Gitamin\Facades\Setting;
use Gitamin\Models\Issue;
use Gitamin\Models\Project;
use Gitamin\Models\Subscriber;
use Illuminate\Support\Facades\View;
use Jenssegers\Date\Date;
class DashboardController extends Controller
{
/**
* Start date.
*
* @var \Jenssegers\Date\Date
*/
protected $startDate;
/**
* The timezone the system is running in.
*
* @var string
*/
protected $timeZone;
/**
* Creates a new dashboard controller.
*/
public function __construct()
{
$this->startDate = new Date();
$this->dateTimeZone = Setting::get('app_timezone');
}
/**
* Shows the dashboard view.
*
* @return \Illuminate\View\View
*/
public function indexAction()
{
$projects = Project::get();
$issues = $this->getIssues();
$subscribers = $this->getSubscribers();
return View::make('dashboard.index')
->withPageTitle(trans('dashboard.dashboard'))
->withProjects($projects)
->withIssues($issues)
->withSubscribers($subscribers);
}
/**
* Shows the notifications view.
*
* @return \Illuminate\View\View
*/
public function showNotifications()
{
return View::make('dashboard.notifications.index')
->withPageTitle(trans('dashboard.notifications.notifications').' '.trans('dashboard.dashboard'));
}
/**
* Fetches all of the issues over the last 30 days.
*
* @return \Illuminate\Support\Collection
*/
protected function getIssues()
{
$allIssues = Issue::whereBetween('created_at', [
$this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00',
$this->startDate->format('Y-m-d').' 23:59:59',
])->orderBy('created_at', 'desc')->get()->groupBy(function (Issue $issue) {
return (new Date($issue->created_at))
->setTimezone($this->dateTimeZone)->toDateString();
});
// Add in days that have no issues
foreach (range(0, 30) as $i) {
$date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i);
if (! isset($allIssues[$date->toDateString()])) {
$allIssues[$date->toDateString()] = [];
}
}
// Sort the array so it takes into account the added days
$allIssues = $allIssues->sortBy(function ($value, $key) {
return strtotime($key);
}, SORT_REGULAR, false);
return $allIssues;
}
/**
* Fetches all of the subscribers over the last 30 days.
*
* @return \Illuminate\Support\Collection
*/
protected function getSubscribers()
{
$allSubscribers = Subscriber::whereBetween('created_at', [
$this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00',
$this->startDate->format('Y-m-d').' 23:59:59',
])->orderBy('created_at', 'desc')->get()->groupBy(function (Subscriber $issue) {
return (new Date($issue->created_at))
->setTimezone($this->dateTimeZone)->toDateString();
});
// Add in days that have no issues
foreach (range(0, 30) as $i) {
$date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i);
if (! isset($allSubscribers[$date->toDateString()])) {
$allSubscribers[$date->toDateString()] = [];
}
}
// Sort the array so it takes into account the added days
$allSubscribers = $allSubscribers->sortBy(function ($value, $key) {
return strtotime($key);
}, SORT_REGULAR, false);
return $allSubscribers;
}
}