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\Routing\Controller; |
|
|
|
|
19
|
|
|
use Illuminate\Support\Facades\View; |
20
|
|
|
use Jenssegers\Date\Date; |
21
|
|
|
|
22
|
|
|
class DashboardController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Start date. |
26
|
|
|
* |
27
|
|
|
* @var \Jenssegers\Date\Date |
28
|
|
|
*/ |
29
|
|
|
protected $startDate; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The timezone the system is running in. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $timeZone; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates a new dashboard controller. |
40
|
|
|
* |
41
|
|
|
* @return void |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->startDate = new Date(); |
46
|
|
|
$this->dateTimeZone = Setting::get('app_timezone'); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Shows the dashboard view. |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\View\View |
53
|
|
|
*/ |
54
|
|
|
public function showDashboard() |
55
|
|
|
{ |
56
|
|
|
$projects = Project::orderBy('order')->get(); |
57
|
|
|
//var_dump($projects); |
58
|
|
|
//$projects = []; |
|
|
|
|
59
|
|
|
$issues = $this->getIssues(); |
60
|
|
|
$subscribers = $this->getSubscribers(); |
61
|
|
|
|
62
|
|
|
return View::make('dashboard.index') |
63
|
|
|
->withPageTitle(trans('dashboard.dashboard')) |
64
|
|
|
->withProjects($projects) |
65
|
|
|
->withIssues($issues) |
66
|
|
|
->withSubscribers($subscribers); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Shows the notifications view. |
71
|
|
|
* |
72
|
|
|
* @return \Illuminate\View\View |
73
|
|
|
*/ |
74
|
|
|
public function showNotifications() |
75
|
|
|
{ |
76
|
|
|
return View::make('dashboard.notifications.index') |
77
|
|
|
->withPageTitle(trans('dashboard.notifications.notifications').' '.trans('dashboard.dashboard')); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Fetches all of the issues over the last 30 days. |
82
|
|
|
* |
83
|
|
|
* @return \Illuminate\Support\Collection |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
protected function getIssues() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$allIssues = Issue::whereBetween('created_at', [ |
88
|
|
|
$this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00', |
89
|
|
|
$this->startDate->format('Y-m-d').' 23:59:59', |
90
|
|
|
])->orderBy('created_at', 'desc')->get()->groupBy(function (Issue $issue) { |
91
|
|
|
return (new Date($issue->created_at)) |
|
|
|
|
92
|
|
|
->setTimezone($this->dateTimeZone)->toDateString(); |
|
|
|
|
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
// Add in days that have no issues |
96
|
|
|
foreach (range(0, 30) as $i) { |
97
|
|
|
$date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i); |
|
|
|
|
98
|
|
|
|
99
|
|
|
if (!isset($allIssues[$date->toDateString()])) { |
100
|
|
|
$allIssues[$date->toDateString()] = []; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Sort the array so it takes into account the added days |
105
|
|
|
$allIssues = $allIssues->sortBy(function ($value, $key) { |
106
|
|
|
return strtotime($key); |
107
|
|
|
}, SORT_REGULAR, false); |
108
|
|
|
|
109
|
|
|
return $allIssues; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Fetches all of the subscribers over the last 30 days. |
114
|
|
|
* |
115
|
|
|
* @return \Illuminate\Support\Collection |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
protected function getSubscribers() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$allSubscribers = Subscriber::whereBetween('created_at', [ |
120
|
|
|
$this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00', |
121
|
|
|
$this->startDate->format('Y-m-d').' 23:59:59', |
122
|
|
|
])->orderBy('created_at', 'desc')->get()->groupBy(function (Subscriber $issue) { |
123
|
|
|
return (new Date($issue->created_at)) |
|
|
|
|
124
|
|
|
->setTimezone($this->dateTimeZone)->toDateString(); |
|
|
|
|
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
// Add in days that have no issues |
128
|
|
|
foreach (range(0, 30) as $i) { |
129
|
|
|
$date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i); |
|
|
|
|
130
|
|
|
|
131
|
|
|
if (!isset($allSubscribers[$date->toDateString()])) { |
132
|
|
|
$allSubscribers[$date->toDateString()] = []; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// Sort the array so it takes into account the added days |
137
|
|
|
$allSubscribers = $allSubscribers->sortBy(function ($value, $key) { |
138
|
|
|
return strtotime($key); |
139
|
|
|
}, SORT_REGULAR, false); |
140
|
|
|
|
141
|
|
|
return $allSubscribers; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: