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